enforce_xpu_test.cc 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/fluid/platform/device/xpu/enforce_xpu.h"
16

17 18 19 20 21 22 23 24 25 26 27 28 29
#include "gtest/gtest.h"

template <typename T>
bool CheckXPUStatusSuccess(T value, const std::string& msg = "success") {
  PADDLE_ENFORCE_XPU_SUCCESS(value);
  return true;
}

template <typename T>
bool CheckXPUStatusFailure(T value, const std::string& msg) {
  try {
    PADDLE_ENFORCE_XPU_SUCCESS(value);
    return false;
H
houj04 已提交
30
  } catch (phi::enforce::EnforceNotMet& error) {
31 32 33 34 35 36
    std::string ex_msg = error.what();
    std::cout << ex_msg << std::endl;
    return ex_msg.find(msg) != std::string::npos;
  }
}

37 38 39 40 41 42 43 44 45 46 47
template <typename T>
bool CheckXDNNStatusSuccess(T value, const std::string& msg = "success") {
  PADDLE_ENFORCE_XDNN_SUCCESS(value, "XDNN Error ");
  return true;
}

template <typename T>
bool CheckXDNNStatusFailure(T value, const std::string& msg) {
  try {
    PADDLE_ENFORCE_XDNN_SUCCESS(value, "XDNN Error ");
    return false;
H
houj04 已提交
48
  } catch (phi::enforce::EnforceNotMet& error) {
49 50 51 52 53 54
    std::string ex_msg = error.what();
    std::cout << ex_msg << std::endl;
    return ex_msg.find(msg) != std::string::npos;
  }
}

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
TEST(enforce, xpu_status) {
  EXPECT_TRUE(CheckXPUStatusSuccess(static_cast<int>(XPU_SUCCESS)));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_INVALID_DEVICE),
                                    "Invalid XPU device"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_UNINIT),
                                    "XPU runtime not properly inited"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_NOMEM),
                                    "Device memory not enough"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_NOCPUMEM),
                                    "CPU memory not enough"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_INVALID_PARAM),
                                    "Invalid parameter"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_NOXPUFUNC),
                                    "Cannot get XPU Func"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_LDSO),
                                    "Error loading dynamic library"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_LDSYM),
                                    "Error loading func from dynamic library"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_SIMULATOR),
                                    "Error from XPU Simulator"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_NOSUPPORT),
                                    "Operation not supported"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_ABNORMAL),
                                    "Device abnormal due to previous error"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_KEXCEPTION),
                                    "Exception in kernel execution"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_TIMEOUT),
                                    "Kernel execution timed out"));
  EXPECT_TRUE(
      CheckXPUStatusFailure(static_cast<int>(XPUERR_BUSY), "Resource busy"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_USEAFCLOSE),
                                    "Use a stream after closed"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_UCECC),
                                    "Uncorrectable ECC"));
  EXPECT_TRUE(
      CheckXPUStatusFailure(static_cast<int>(XPUERR_OVERHEAT), "Overheat"));
  EXPECT_TRUE(
      CheckXPUStatusFailure(static_cast<int>(XPUERR_UNEXPECT),
                            "Execution error, reach unexpected control flow"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_DEVRESET),
                                    "Device is being reset, try again later"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_HWEXCEPTION),
                                    "Hardware module exception"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_HBM_INIT),
                                    "Error init HBM"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_DEVINIT),
                                    "Error init device"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_PEERRESET),
                                    "Device is being reset, try again later"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_MAXDEV),
                                    "Device count exceed limit"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_NOIOC),
                                    "Unknown IOCTL command"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_DMATIMEOUT),
                                    "DMA timed out, a reboot maybe needed"));
  EXPECT_TRUE(CheckXPUStatusFailure(
      static_cast<int>(XPUERR_DMAABORT),
      "DMA aborted due to error, possibly wrong address or hardware state"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_MCUUNINIT),
                                    "Firmware not initialized"));
  EXPECT_TRUE(
      CheckXPUStatusFailure(static_cast<int>(XPUERR_OLDFW),
                            "Firmware version too old (<15), please update."));
  EXPECT_TRUE(
      CheckXPUStatusFailure(static_cast<int>(XPUERR_PCIE), "Error in PCIE"));
  EXPECT_TRUE(
      CheckXPUStatusFailure(static_cast<int>(XPUERR_FAULT),
                            "Error copy between kernel and user space"));
  EXPECT_TRUE(CheckXPUStatusFailure(static_cast<int>(XPUERR_INTERRUPTED),
                                    "Execution interrupted by user"));
}

127
#ifdef PADDLE_WITH_XPU_BKCL
128 129 130 131 132 133 134 135 136
TEST(enforce, bkcl_status) {
  EXPECT_TRUE(CheckXPUStatusSuccess(BKCL_SUCCESS));
  EXPECT_TRUE(
      CheckXPUStatusFailure(BKCL_INVALID_ARGUMENT, "BKCL_INVALID_ARGUMENT"));
  EXPECT_TRUE(CheckXPUStatusFailure(BKCL_RUNTIME_ERROR, "BKCL_RUNTIME_ERROR"));
  EXPECT_TRUE(CheckXPUStatusFailure(BKCL_SYSTEM_ERROR, "BKCL_SYSTEM_ERROR"));
  EXPECT_TRUE(
      CheckXPUStatusFailure(BKCL_INTERNAL_ERROR, "BKCL_INTERNAL_ERROR"));
}
137
#endif
138 139 140 141 142 143 144 145 146 147 148 149

TEST(enforce, xdnn_status) {
  EXPECT_TRUE(CheckXDNNStatusSuccess(xpu::Error_t::SUCCESS));
  EXPECT_TRUE(CheckXDNNStatusFailure(xpu::Error_t::INVALID_PARAM,
                                     "XDNN_INVALID_PARAM"));
  EXPECT_TRUE(CheckXDNNStatusFailure(xpu::Error_t::RUNTIME_ERROR,
                                     "XDNN_RUNTIME_ERROR"));
  EXPECT_TRUE(CheckXDNNStatusFailure(xpu::Error_t::NO_ENOUGH_WORKSPACE,
                                     "XDNN_NO_ENOUGH_WORKSPACE"));
  EXPECT_TRUE(CheckXDNNStatusFailure(xpu::Error_t::NOT_IMPLEMENT,
                                     "XDNN_NOT_IMPLEMENT"));
}