/* 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" #include "gtest/gtest.h" template bool CheckXPUStatusSuccess(T value, const std::string& msg = "success") { PADDLE_ENFORCE_XPU_SUCCESS(value); return true; } template bool CheckXPUStatusFailure(T value, const std::string& msg) { try { PADDLE_ENFORCE_XPU_SUCCESS(value); return false; } catch (phi::enforce::EnforceNotMet& error) { std::string ex_msg = error.what(); std::cout << ex_msg << std::endl; return ex_msg.find(msg) != std::string::npos; } } template bool CheckXDNNStatusSuccess(T value, const std::string& msg = "success") { PADDLE_ENFORCE_XDNN_SUCCESS(value, "XDNN Error "); return true; } template bool CheckXDNNStatusFailure(T value, const std::string& msg) { try { PADDLE_ENFORCE_XDNN_SUCCESS(value, "XDNN Error "); return false; } catch (phi::enforce::EnforceNotMet& error) { std::string ex_msg = error.what(); std::cout << ex_msg << std::endl; return ex_msg.find(msg) != std::string::npos; } } TEST(enforce, xpu_status) { EXPECT_TRUE(CheckXPUStatusSuccess(static_cast(XPU_SUCCESS))); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_INVALID_DEVICE), "Invalid XPU device")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_UNINIT), "XPU runtime not properly inited")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_NOMEM), "Device memory not enough")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_NOCPUMEM), "CPU memory not enough")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_INVALID_PARAM), "Invalid parameter")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_NOXPUFUNC), "Cannot get XPU Func")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_LDSO), "Error loading dynamic library")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_LDSYM), "Error loading func from dynamic library")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_SIMULATOR), "Error from XPU Simulator")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_NOSUPPORT), "Operation not supported")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_ABNORMAL), "Device abnormal due to previous error")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_KEXCEPTION), "Exception in kernel execution")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_TIMEOUT), "Kernel execution timed out")); EXPECT_TRUE( CheckXPUStatusFailure(static_cast(XPUERR_BUSY), "Resource busy")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_USEAFCLOSE), "Use a stream after closed")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_UCECC), "Uncorrectable ECC")); EXPECT_TRUE( CheckXPUStatusFailure(static_cast(XPUERR_OVERHEAT), "Overheat")); EXPECT_TRUE( CheckXPUStatusFailure(static_cast(XPUERR_UNEXPECT), "Execution error, reach unexpected control flow")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_DEVRESET), "Device is being reset, try again later")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_HWEXCEPTION), "Hardware module exception")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_HBM_INIT), "Error init HBM")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_DEVINIT), "Error init device")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_PEERRESET), "Device is being reset, try again later")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_MAXDEV), "Device count exceed limit")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_NOIOC), "Unknown IOCTL command")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_DMATIMEOUT), "DMA timed out, a reboot maybe needed")); EXPECT_TRUE(CheckXPUStatusFailure( static_cast(XPUERR_DMAABORT), "DMA aborted due to error, possibly wrong address or hardware state")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_MCUUNINIT), "Firmware not initialized")); EXPECT_TRUE( CheckXPUStatusFailure(static_cast(XPUERR_OLDFW), "Firmware version too old (<15), please update.")); EXPECT_TRUE( CheckXPUStatusFailure(static_cast(XPUERR_PCIE), "Error in PCIE")); EXPECT_TRUE( CheckXPUStatusFailure(static_cast(XPUERR_FAULT), "Error copy between kernel and user space")); EXPECT_TRUE(CheckXPUStatusFailure(static_cast(XPUERR_INTERRUPTED), "Execution interrupted by user")); } #ifdef PADDLE_WITH_XPU_BKCL 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")); } #endif 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")); }