From df23c7c3d3b716c108f5a2d03fa867d514155711 Mon Sep 17 00:00:00 2001 From: PuQing Date: Thu, 24 Nov 2022 15:32:48 +0800 Subject: [PATCH] [PHI decoupling] remove "paddle/fluid/platform/enforce.h" in phi (#48049) --- paddle/fluid/inference/check_symbol.sh | 2 +- paddle/fluid/platform/CMakeLists.txt | 3 - paddle/fluid/platform/enforce.h | 530 --------------- paddle/phi/backends/callback_manager.cc | 2 +- paddle/phi/backends/dynload/cudnn.cc | 2 +- paddle/phi/backends/dynload/cufft.cc | 2 +- paddle/phi/backends/dynload/dynamic_loader.cc | 2 +- paddle/phi/backends/dynload/miopen.cc | 2 +- paddle/phi/backends/dynload/tensorrt.h | 2 +- paddle/phi/backends/gpu/cuda/cuda_info.cc | 3 +- paddle/phi/backends/gpu/gpu_context.cc | 4 +- paddle/phi/backends/gpu/gpu_resources.cc | 3 +- paddle/phi/backends/gpu/rocm/rocm_info.cc | 5 +- paddle/phi/backends/xpu/enforce_xpu.h | 2 +- paddle/phi/backends/xpu/xpu_header.h | 2 +- paddle/phi/core/CMakeLists.txt | 4 + paddle/phi/core/cuda_stream.h | 3 +- paddle/phi/core/enforce.h | 601 +++++++++++++++++- .../core}/external_error.proto | 2 +- paddle/phi/kernels/autotune/CMakeLists.txt | 7 +- paddle/phi/kernels/funcs/concat_funcs.h | 2 +- paddle/phi/kernels/funcs/cpu_vec.h | 2 +- paddle/phi/kernels/funcs/cufft_util.h | 2 +- paddle/phi/kernels/funcs/gru_compute.h | 2 +- paddle/phi/kernels/funcs/hipfft_util.h | 2 +- paddle/phi/kernels/funcs/lstm_compute.h | 2 +- paddle/phi/kernels/funcs/math_function.h | 2 +- .../phi/kernels/gpu/batch_norm_grad_kernel.cu | 2 +- paddle/phi/kernels/gpu/batch_norm_kernel.cu | 2 +- .../phi/kernels/gpu/cholesky_solve_kernel.cu | 2 +- .../kernels/gpu/class_center_sample_kernel.cu | 2 +- tools/externalError/start.sh | 2 +- 32 files changed, 633 insertions(+), 574 deletions(-) rename paddle/{fluid/platform => phi/core}/external_error.proto (97%) diff --git a/paddle/fluid/inference/check_symbol.sh b/paddle/fluid/inference/check_symbol.sh index 1d9b566e6c..0d8892f205 100755 --- a/paddle/fluid/inference/check_symbol.sh +++ b/paddle/fluid/inference/check_symbol.sh @@ -18,7 +18,7 @@ lib=$1 if [ $# -ne 1 ]; then echo "No input library"; exit -1 ; fi num_paddle_syms=$(nm -D "${lib}" | grep -c paddle ) -num_google_syms=$(nm -D "${lib}" | grep google | grep -v paddle | grep -v brpc | grep -c "T " ) +num_google_syms=$(nm -D "${lib}" | grep google | grep -v paddle | grep -v phi | grep -v brpc | grep -c "T " ) if [ $num_paddle_syms -le 0 ]; then echo "Have no paddle symbols"; exit -1 ; fi if [ $num_google_syms -ge 1 ]; then echo "Have some google symbols"; exit -1 ; fi diff --git a/paddle/fluid/platform/CMakeLists.txt b/paddle/fluid/platform/CMakeLists.txt index 627ea8b9aa..63099e71af 100644 --- a/paddle/fluid/platform/CMakeLists.txt +++ b/paddle/fluid/platform/CMakeLists.txt @@ -1,8 +1,5 @@ proto_library(profiler_proto SRCS profiler.proto DEPS framework_proto simple_threadpool) -if(WITH_GPU) - proto_library(external_error_proto SRCS external_error.proto) -endif() if(WITH_PYTHON) py_proto_compile(profiler_py_proto SRCS profiler.proto) add_custom_target(profiler_py_proto_init ALL COMMAND ${CMAKE_COMMAND} -E diff --git a/paddle/fluid/platform/enforce.h b/paddle/fluid/platform/enforce.h index 5bd9029179..4be05f24bc 100644 --- a/paddle/fluid/platform/enforce.h +++ b/paddle/fluid/platform/enforce.h @@ -36,8 +36,6 @@ limitations under the License. */ #include #include #include - -#include "paddle/fluid/platform/external_error.pb.h" #endif // PADDLE_WITH_CUDA #ifdef PADDLE_WITH_HIP @@ -224,533 +222,5 @@ struct EOFException : public std::exception { END_HANDLE_THE_ERROR \ } while (0) -/**************************************************************************/ -/**************************** NVIDIA ERROR ********************************/ -#ifdef PADDLE_WITH_CUDA - -namespace details { - -template -struct ExternalApiType {}; - -#define DEFINE_EXTERNAL_API_TYPE(type, success_value, proto_type) \ - template <> \ - struct ExternalApiType { \ - using Type = type; \ - static constexpr Type kSuccess = success_value; \ - static constexpr const char* kTypeString = #proto_type; \ - static constexpr platform::proto::ApiType kProtoType = \ - platform::proto::ApiType::proto_type; \ - } - -DEFINE_EXTERNAL_API_TYPE(cudaError_t, cudaSuccess, CUDA); -DEFINE_EXTERNAL_API_TYPE(curandStatus_t, CURAND_STATUS_SUCCESS, CURAND); -DEFINE_EXTERNAL_API_TYPE(cudnnStatus_t, CUDNN_STATUS_SUCCESS, CUDNN); -DEFINE_EXTERNAL_API_TYPE(cublasStatus_t, CUBLAS_STATUS_SUCCESS, CUBLAS); -DEFINE_EXTERNAL_API_TYPE(cusparseStatus_t, CUSPARSE_STATUS_SUCCESS, CUSPARSE); -DEFINE_EXTERNAL_API_TYPE(cusolverStatus_t, CUSOLVER_STATUS_SUCCESS, CUSOLVER); -DEFINE_EXTERNAL_API_TYPE(cufftResult_t, CUFFT_SUCCESS, CUFFT); -DEFINE_EXTERNAL_API_TYPE(CUresult, CUDA_SUCCESS, CU); - -#if !defined(__APPLE__) && defined(PADDLE_WITH_NCCL) -DEFINE_EXTERNAL_API_TYPE(ncclResult_t, ncclSuccess, NCCL); -#endif - -} // namespace details - -template -inline const char* GetErrorMsgUrl(T status) { - using __CUDA_STATUS_TYPE__ = decltype(status); - platform::proto::ApiType proto_type = - details::ExternalApiType<__CUDA_STATUS_TYPE__>::kProtoType; - switch (proto_type) { - case platform::proto::ApiType::CUDA: - case platform::proto::ApiType::CU: - return "https://docs.nvidia.com/cuda/cuda-runtime-api/" - "group__CUDART__TYPES.html#group__CUDART__TYPES_" - "1g3f51e3575c2178246db0a94a430e0038"; - break; - case platform::proto::ApiType::CURAND: - return "https://docs.nvidia.com/cuda/curand/" - "group__HOST.html#group__HOST_1gb94a31d5c165858c96b6c18b70644437"; - break; - case platform::proto::ApiType::CUDNN: - return "https://docs.nvidia.com/deeplearning/cudnn/api/" - "index.html#cudnnStatus_t"; - break; - case platform::proto::ApiType::CUBLAS: - return "https://docs.nvidia.com/cuda/cublas/index.html#cublasstatus_t"; - break; - case platform::proto::ApiType::CUSOLVER: - return "https://docs.nvidia.com/cuda/cusolver/" - "index.html#cuSolverSPstatus"; - break; - case platform::proto::ApiType::NCCL: - return "https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/api/" - "types.html#ncclresult-t"; - break; - case platform::proto::ApiType::CUFFT: - return "https://docs.nvidia.com/cuda/cufft/index.html#cufftresult"; - case platform::proto::ApiType::CUSPARSE: - return "https://docs.nvidia.com/cuda/cusparse/" - "index.html#cusparseStatus_t"; - break; - default: - return "Unknown type of External API, can't get error message URL!"; - break; - } -} - -template -inline std::string GetExternalErrorMsg(T status) { - std::ostringstream sout; - bool _initSucceed = false; - platform::proto::ExternalErrorDesc externalError; - if (externalError.ByteSizeLong() == 0) { - std::string filePath; -#if !defined(_WIN32) - Dl_info info; - if (dladdr(reinterpret_cast(GetCurrentTraceBackString), &info)) { - std::string strModule(info.dli_fname); - const size_t last_slash_idx = strModule.find_last_of("/"); - std::string compare_path = strModule.substr(strModule.length() - 6); - if (std::string::npos != last_slash_idx) { - strModule.erase(last_slash_idx, std::string::npos); - } - if (compare_path.compare("avx.so") == 0) { - filePath = - strModule + - "/../include/third_party/externalError/data/externalErrorMsg.pb"; - } else { - filePath = strModule + - "/../../third_party/externalError/data/externalErrorMsg.pb"; - } - } -#else - char buf[512]; - MEMORY_BASIC_INFORMATION mbi; - HMODULE h_module = - (::VirtualQuery(GetCurrentTraceBackString, &mbi, sizeof(mbi)) != 0) - ? (HMODULE)mbi.AllocationBase - : NULL; - GetModuleFileName(h_module, buf, 512); - std::string strModule(buf); - const size_t last_slash_idx = strModule.find_last_of("\\"); - std::string compare_path = strModule.substr(strModule.length() - 7); - if (std::string::npos != last_slash_idx) { - strModule.erase(last_slash_idx, std::string::npos); - } - if (compare_path.compare("avx.pyd") == 0) { - filePath = strModule + - "\\..\\include\\third_" - "party\\externalerror\\data\\externalErrorMsg.pb"; - } else { - filePath = - strModule + - "\\..\\..\\third_party\\externalerror\\data\\externalErrorMsg.pb"; - } -#endif - std::ifstream fin(filePath, std::ios::in | std::ios::binary); - _initSucceed = externalError.ParseFromIstream(&fin); - } - using __CUDA_STATUS_TYPE__ = decltype(status); - platform::proto::ApiType proto_type = - details::ExternalApiType<__CUDA_STATUS_TYPE__>::kProtoType; - if (_initSucceed) { - for (int i = 0; i < externalError.errors_size(); ++i) { - if (proto_type == externalError.errors(i).type()) { - for (int j = 0; j < externalError.errors(i).messages_size(); ++j) { - if (status == externalError.errors(i).messages(j).code()) { - sout << "\n [Hint: " - << externalError.errors(i).messages(j).message() << "]"; - return sout.str(); - } - } - } - } - } - - sout << "\n [Hint: Please search for the error code(" << status - << ") on website (" << GetErrorMsgUrl(status) - << ") to get Nvidia's official solution and advice about " - << details::ExternalApiType<__CUDA_STATUS_TYPE__>::kTypeString - << " Error.]"; - return sout.str(); -} - -template std::string GetExternalErrorMsg(cudaError_t); -template std::string GetExternalErrorMsg(curandStatus_t); -template std::string GetExternalErrorMsg(cudnnStatus_t); -template std::string GetExternalErrorMsg(cublasStatus_t); -template std::string GetExternalErrorMsg(cusparseStatus_t); -template std::string GetExternalErrorMsg(cusolverStatus_t); -template std::string GetExternalErrorMsg(cufftResult_t); -template std::string GetExternalErrorMsg(CUresult); -#if !defined(__APPLE__) && defined(PADDLE_WITH_NCCL) -template std::string GetExternalErrorMsg(ncclResult_t); -#endif - -/*************** CUDA ERROR ***************/ -inline bool is_error(cudaError_t e) { return e != cudaSuccess; } - -inline std::string build_nvidia_error_msg(cudaError_t e) { - std::ostringstream sout; - sout << "CUDA error(" << e << "), " << cudaGetErrorString(e) << ". " - << GetExternalErrorMsg(e); - return sout.str(); -} - -/*************** CURAND ERROR ***************/ -inline bool is_error(curandStatus_t stat) { - return stat != CURAND_STATUS_SUCCESS; -} - -inline std::string build_nvidia_error_msg(curandStatus_t stat) { - std::ostringstream sout; - sout << "CURAND error(" << stat << "). " << GetExternalErrorMsg(stat); - return sout.str(); -} - -/*************** CUDNN ERROR ***************/ -inline bool is_error(cudnnStatus_t stat) { - return stat != CUDNN_STATUS_SUCCESS; -} - -inline std::string build_nvidia_error_msg(cudnnStatus_t stat) { - std::ostringstream sout; - sout << "CUDNN error(" << stat << "), " - << phi::dynload::cudnnGetErrorString(stat) << ". " - << GetExternalErrorMsg(stat); - return sout.str(); -} - -/*************** CUBLAS ERROR ***************/ -inline bool is_error(cublasStatus_t stat) { - return stat != CUBLAS_STATUS_SUCCESS; -} - -inline std::string build_nvidia_error_msg(cublasStatus_t stat) { - std::ostringstream sout; - sout << "CUBLAS error(" << stat << "). " << GetExternalErrorMsg(stat); - return sout.str(); -} - -/*************** CUSPARSE ERROR ***************/ -inline bool is_error(cusparseStatus_t stat) { - return stat != CUSPARSE_STATUS_SUCCESS; -} - -inline std::string build_nvidia_error_msg(cusparseStatus_t stat) { - std::ostringstream sout; - sout << "CUSparse error(" << stat << "). " << GetExternalErrorMsg(stat); - return sout.str(); -} - -/*************** CUSOLVER ERROR ***************/ -inline bool is_error(cusolverStatus_t stat) { - return stat != CUSOLVER_STATUS_SUCCESS; -} - -inline std::string build_nvidia_error_msg(cusolverStatus_t stat) { - std::ostringstream sout; - sout << "CUSOLVER error(" << stat << "). " << GetExternalErrorMsg(stat); - return sout.str(); -} - -/*************** CUFFT ERROR ***************/ -inline bool is_error(cufftResult_t stat) { return stat != CUFFT_SUCCESS; } - -inline std::string build_nvidia_error_msg(cufftResult_t stat) { - std::ostringstream sout; - sout << "CUFFT error(" << stat << "). " << GetExternalErrorMsg(stat); - return sout.str(); -} - -/*************** CUresult ERROR ***************/ -inline bool is_error(CUresult stat) { return stat != CUDA_SUCCESS; } - -inline std::string build_nvidia_error_msg(CUresult stat) { - std::ostringstream sout; - sout << "CU error(" << stat << "). " << GetExternalErrorMsg(stat); - return sout.str(); -} - -/**************** NCCL ERROR ****************/ -#if !defined(__APPLE__) && defined(PADDLE_WITH_NCCL) -inline bool is_error(ncclResult_t nccl_result) { - return nccl_result != ncclSuccess; -} - -inline std::string build_nvidia_error_msg(ncclResult_t nccl_result) { - std::ostringstream sout; - sout << "NCCL error(" << nccl_result << "), " - << phi::dynload::ncclGetErrorString(nccl_result) << ". "; - if (errno == ENOSPC || errno == EAGAIN) { - std::string detail(strerror(errno)); - detail += "\nPlease try one of the following solutions:"; - detail += "\n1. export NCCL_SHM_DISABLE=1;"; - detail += "\n2. export NCCL_P2P_LEVEL=SYS;"; - detail += - "\n3. Increase shared memory by setting the -shm-size " - "option when starting docker container, e.g., setting " - " -shm-size=2g.\n"; - sout << " Detail: " + detail; - } - sout << GetExternalErrorMsg(nccl_result); - return sout.str(); -} -#endif // not(__APPLE__) and PADDLE_WITH_NCCL - -#define PADDLE_ENFORCE_GPU_SUCCESS(COND) \ - do { \ - auto __cond__ = (COND); \ - using __CUDA_STATUS_TYPE__ = decltype(__cond__); \ - constexpr auto __success_type__ = \ - ::paddle::platform::details::ExternalApiType< \ - __CUDA_STATUS_TYPE__>::kSuccess; \ - if (UNLIKELY(__cond__ != __success_type__)) { \ - auto __summary__ = phi::errors::External( \ - ::paddle::platform::build_nvidia_error_msg(__cond__)); \ - __THROW_ERROR_INTERNAL__(__summary__); \ - } \ - } while (0) - -#define PADDLE_ENFORCE_CUDA_LAUNCH_SUCCESS(OP) \ - do { \ - auto res = cudaGetLastError(); \ - if (UNLIKELY(res != cudaSuccess)) { \ - auto msg = ::paddle::platform::build_nvidia_error_msg(res); \ - PADDLE_THROW(platform::errors::Fatal( \ - "CUDA error after kernel (%s): %s", OP, msg)); \ - } \ - } while (0) - -inline void retry_sleep(unsigned milliseconds) { -#ifdef _WIN32 - Sleep(milliseconds); -#else - if (milliseconds < 1000) { - // usleep argument must be less than 1,000,000. Reference: - // https://pubs.opengroup.org/onlinepubs/7908799/xsh/usleep.html - usleep(milliseconds * 1000); - } else { - // clip to sleep in seconds because we can not and don't have to - // sleep for exact milliseconds - sleep(milliseconds / 1000); - } -#endif -} - -#define PADDLE_RETRY_CUDA_SUCCESS(COND) \ - do { \ - auto __cond__ = (COND); \ - int retry_count = 1; \ - using __CUDA_STATUS_TYPE__ = decltype(__cond__); \ - constexpr auto __success_type__ = \ - ::paddle::platform::details::ExternalApiType< \ - __CUDA_STATUS_TYPE__>::kSuccess; \ - while (UNLIKELY(__cond__ != __success_type__) && retry_count < 5) { \ - paddle::platform::retry_sleep(10000); \ - __cond__ = (COND); \ - ++retry_count; \ - } \ - if (UNLIKELY(__cond__ != __success_type__)) { \ - auto __summary__ = phi::errors::External( \ - ::paddle::platform::build_nvidia_error_msg(__cond__)); \ - __THROW_ERROR_INTERNAL__(__summary__); \ - } \ - } while (0) - -#undef DEFINE_EXTERNAL_API_TYPE -#endif // PADDLE_WITH_CUDA - -/**************************************************************************/ -/***************************** HIP ERROR **********************************/ -#ifdef PADDLE_WITH_HIP - -/***** HIP ERROR *****/ -inline bool is_error(hipError_t e) { return e != hipSuccess; } - -inline std::string build_rocm_error_msg(hipError_t e) { - std::ostringstream sout; - sout << " Hip error(" << e << "), " << hipGetErrorString(e) << "."; - return sout.str(); -} - -/***** HIPRAND ERROR *****/ -inline bool is_error(hiprandStatus_t stat) { - return stat != HIPRAND_STATUS_SUCCESS; -} - -inline const char* hiprandGetErrorString(hiprandStatus_t stat) { - switch (stat) { - case HIPRAND_STATUS_SUCCESS: - return "HIPRAND_STATUS_SUCCESS"; - case HIPRAND_STATUS_VERSION_MISMATCH: - return "HIPRAND_STATUS_VERSION_MISMATCH"; - case HIPRAND_STATUS_NOT_INITIALIZED: - return "HIPRAND_STATUS_NOT_INITIALIZED"; - case HIPRAND_STATUS_ALLOCATION_FAILED: - return "HIPRAND_STATUS_ALLOCATION_FAILED"; - case HIPRAND_STATUS_TYPE_ERROR: - return "HIPRAND_STATUS_TYPE_ERROR"; - case HIPRAND_STATUS_OUT_OF_RANGE: - return "HIPRAND_STATUS_OUT_OF_RANGE"; - case HIPRAND_STATUS_LENGTH_NOT_MULTIPLE: - return "HIPRAND_STATUS_LENGTH_NOT_MULTIPLE"; - case HIPRAND_STATUS_DOUBLE_PRECISION_REQUIRED: - return "HIPRAND_STATUS_DOUBLE_PRECISION_REQUIRED"; - case HIPRAND_STATUS_LAUNCH_FAILURE: - return "HIPRAND_STATUS_LAUNCH_FAILURE"; - case HIPRAND_STATUS_PREEXISTING_FAILURE: - return "HIPRAND_STATUS_PREEXISTING_FAILURE"; - case HIPRAND_STATUS_INITIALIZATION_FAILED: - return "HIPRAND_STATUS_INITIALIZATION_FAILED"; - case HIPRAND_STATUS_ARCH_MISMATCH: - return "HIPRAND_STATUS_ARCH_MISMATCH"; - case HIPRAND_STATUS_INTERNAL_ERROR: - return "HIPRAND_STATUS_INTERNAL_ERROR"; - case HIPRAND_STATUS_NOT_IMPLEMENTED: - return "HIPRAND_STATUS_NOT_IMPLEMENTED"; - default: - return "Unknown hiprand status"; - } -} - -inline std::string build_rocm_error_msg(hiprandStatus_t stat) { - std::string msg(" Hiprand error, "); - return msg + hiprandGetErrorString(stat) + " "; -} - -/***** MIOPEN ERROR *****/ -inline bool is_error(miopenStatus_t stat) { - return stat != miopenStatusSuccess; -} - -inline std::string build_rocm_error_msg(miopenStatus_t stat) { - std::string msg(" Miopen error, "); - return msg + phi::dynload::miopenGetErrorString(stat) + " "; -} - -/***** ROCBLAS ERROR *****/ -inline bool is_error(rocblas_status stat) { - return stat != rocblas_status_success; -} - -inline const char* rocblasGetErrorString(rocblas_status stat) { - switch (stat) { - case rocblas_status_invalid_handle: - return "rocblas_status_invalid_handle"; - case rocblas_status_memory_error: - return "rocblas_status_memory_error"; - case rocblas_status_invalid_value: - return "rocblas_status_invalid_value"; - case rocblas_status_not_implemented: - return "rocblas_status_not_implemented"; - case rocblas_status_invalid_pointer: - return "rocblas_status_invalid_pointer"; - case rocblas_status_invalid_size: - return "rocblas_status_invalid_size"; - case rocblas_status_internal_error: - return "rocblas_status_internal_error"; - default: - return "Unknown cublas status"; - } -} - -inline std::string build_rocm_error_msg(rocblas_status stat) { - std::string msg(" Rocblas error, "); - return msg + rocblasGetErrorString(stat) + " "; -} - -/****** RCCL ERROR ******/ -#if !defined(__APPLE__) && defined(PADDLE_WITH_RCCL) -inline bool is_error(ncclResult_t nccl_result) { - return nccl_result != ncclSuccess; -} - -inline std::string build_rocm_error_msg(ncclResult_t nccl_result) { - std::string msg(" Rccl error, "); - return msg + phi::dynload::ncclGetErrorString(nccl_result) + " "; -} -#endif // not(__APPLE__) and PADDLE_WITH_NCCL - -/***** HIPFFT ERROR *****/ -inline bool is_error(hipfftResult_t stat) { return stat != HIPFFT_SUCCESS; } - -inline std::string build_rocm_error_msg(hipfftResult_t stat) { - std::string msg(" HIPFFT error, "); - return msg + phi::dynload::hipfftGetErrorString(stat) + " "; -} - -namespace details { - -template -struct ExternalApiType {}; - -#define DEFINE_EXTERNAL_API_TYPE(type, success_value) \ - template <> \ - struct ExternalApiType { \ - using Type = type; \ - static constexpr Type kSuccess = success_value; \ - } - -DEFINE_EXTERNAL_API_TYPE(hipError_t, hipSuccess); -DEFINE_EXTERNAL_API_TYPE(hiprandStatus_t, HIPRAND_STATUS_SUCCESS); -DEFINE_EXTERNAL_API_TYPE(miopenStatus_t, miopenStatusSuccess); -DEFINE_EXTERNAL_API_TYPE(rocblas_status, rocblas_status_success); -DEFINE_EXTERNAL_API_TYPE(hipfftResult_t, HIPFFT_SUCCESS); - -#if !defined(__APPLE__) && defined(PADDLE_WITH_RCCL) -DEFINE_EXTERNAL_API_TYPE(ncclResult_t, ncclSuccess); -#endif - -} // namespace details - -#define PADDLE_ENFORCE_GPU_SUCCESS(COND) \ - do { \ - auto __cond__ = (COND); \ - using __CUDA_STATUS_TYPE__ = decltype(__cond__); \ - constexpr auto __success_type__ = \ - ::paddle::platform::details::ExternalApiType< \ - __CUDA_STATUS_TYPE__>::kSuccess; \ - if (UNLIKELY(__cond__ != __success_type__)) { \ - auto __summary__ = phi::errors::External( \ - ::paddle::platform::build_rocm_error_msg(__cond__)); \ - __THROW_ERROR_INTERNAL__(__summary__); \ - } \ - } while (0) - -inline void retry_sleep(unsigned millisecond) { -#ifdef _WIN32 - Sleep(millisecond); -#else - sleep(millisecond); -#endif -} - -#define PADDLE_RETRY_CUDA_SUCCESS(COND) \ - do { \ - auto __cond__ = (COND); \ - int retry_count = 1; \ - using __CUDA_STATUS_TYPE__ = decltype(__cond__); \ - constexpr auto __success_type__ = \ - ::paddle::platform::details::ExternalApiType< \ - __CUDA_STATUS_TYPE__>::kSuccess; \ - while (UNLIKELY(__cond__ != __success_type__) && retry_count < 5) { \ - ::paddle::platform::retry_sleep(10000); \ - __cond__ = (COND); \ - ++retry_count; \ - } \ - if (UNLIKELY(__cond__ != __success_type__)) { \ - auto __summary__ = phi::errors::External( \ - ::paddle::platform::build_rocm_error_msg(__cond__)); \ - __THROW_ERROR_INTERNAL__(__summary__); \ - } \ - } while (0) - -#undef DEFINE_EXTERNAL_API_TYPE -#endif // PADDLE_WITH_HIP - } // namespace platform } // namespace paddle diff --git a/paddle/phi/backends/callback_manager.cc b/paddle/phi/backends/callback_manager.cc index 7ce5988038..c996b8d9be 100644 --- a/paddle/phi/backends/callback_manager.cc +++ b/paddle/phi/backends/callback_manager.cc @@ -17,8 +17,8 @@ #include #include "paddle/fluid/platform/device/device_wrapper.h" -#include "paddle/fluid/platform/enforce.h" #include "paddle/phi/backends/device_guard.h" +#include "paddle/phi/core/enforce.h" namespace phi { diff --git a/paddle/phi/backends/dynload/cudnn.cc b/paddle/phi/backends/dynload/cudnn.cc index 9bd38a89ab..924dd60d2c 100644 --- a/paddle/phi/backends/dynload/cudnn.cc +++ b/paddle/phi/backends/dynload/cudnn.cc @@ -14,7 +14,7 @@ limitations under the License. */ #include "paddle/phi/backends/dynload/cudnn.h" -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" namespace phi { namespace dynload { diff --git a/paddle/phi/backends/dynload/cufft.cc b/paddle/phi/backends/dynload/cufft.cc index 5a7080032d..a15969ecc3 100644 --- a/paddle/phi/backends/dynload/cufft.cc +++ b/paddle/phi/backends/dynload/cufft.cc @@ -14,7 +14,7 @@ limitations under the License. */ #include "paddle/phi/backends/dynload/cufft.h" -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" namespace phi { namespace dynload { diff --git a/paddle/phi/backends/dynload/dynamic_loader.cc b/paddle/phi/backends/dynload/dynamic_loader.cc index c9ae1d0cd7..3f22e24429 100644 --- a/paddle/phi/backends/dynload/dynamic_loader.cc +++ b/paddle/phi/backends/dynload/dynamic_loader.cc @@ -17,8 +17,8 @@ limitations under the License. */ #include #include -#include "paddle/fluid/platform/enforce.h" #include "paddle/phi/backends/dynload/cupti_lib_path.h" +#include "paddle/phi/core/enforce.h" #if defined(_WIN32) #include diff --git a/paddle/phi/backends/dynload/miopen.cc b/paddle/phi/backends/dynload/miopen.cc index 9c58da1d6f..b8f328b4aa 100644 --- a/paddle/phi/backends/dynload/miopen.cc +++ b/paddle/phi/backends/dynload/miopen.cc @@ -14,7 +14,7 @@ limitations under the License. */ #include "paddle/phi/backends/dynload/miopen.h" -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" namespace phi { namespace dynload { diff --git a/paddle/phi/backends/dynload/tensorrt.h b/paddle/phi/backends/dynload/tensorrt.h index cd8c6457f1..c971a66031 100644 --- a/paddle/phi/backends/dynload/tensorrt.h +++ b/paddle/phi/backends/dynload/tensorrt.h @@ -21,8 +21,8 @@ limitations under the License. */ #include // NOLINT -#include "paddle/fluid/platform/enforce.h" #include "paddle/phi/backends/dynload/dynamic_loader.h" +#include "paddle/phi/core/enforce.h" namespace phi { namespace dynload { diff --git a/paddle/phi/backends/gpu/cuda/cuda_info.cc b/paddle/phi/backends/gpu/cuda/cuda_info.cc index 4b5de3db54..f6bff1c7b3 100644 --- a/paddle/phi/backends/gpu/cuda/cuda_info.cc +++ b/paddle/phi/backends/gpu/cuda/cuda_info.cc @@ -14,8 +14,7 @@ #include "paddle/phi/backends/gpu/gpu_info.h" -// TODO(phi): remove fluid headers. -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" static std::once_flag g_device_props_size_init_flag; static std::vector> g_device_props_init_flags; diff --git a/paddle/phi/backends/gpu/gpu_context.cc b/paddle/phi/backends/gpu/gpu_context.cc index 0e10291144..c8f5463d82 100644 --- a/paddle/phi/backends/gpu/gpu_context.cc +++ b/paddle/phi/backends/gpu/gpu_context.cc @@ -21,6 +21,7 @@ limitations under the License. */ #include #include #include +#include #include "glog/logging.h" #include "paddle/phi/api/ext/exception.h" @@ -54,8 +55,7 @@ limitations under the License. */ // without eigen. #include "unsupported/Eigen/CXX11/Tensor" -// TODO(phi): remove fluid header. -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" namespace phi { diff --git a/paddle/phi/backends/gpu/gpu_resources.cc b/paddle/phi/backends/gpu/gpu_resources.cc index 4a16480101..e05e75636c 100644 --- a/paddle/phi/backends/gpu/gpu_resources.cc +++ b/paddle/phi/backends/gpu/gpu_resources.cc @@ -32,8 +32,7 @@ #include "unsupported/Eigen/CXX11/Tensor" -// TODO(phi): remove fluid header. -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" namespace phi { diff --git a/paddle/phi/backends/gpu/rocm/rocm_info.cc b/paddle/phi/backends/gpu/rocm/rocm_info.cc index 1646d9666f..8e4e06af80 100644 --- a/paddle/phi/backends/gpu/rocm/rocm_info.cc +++ b/paddle/phi/backends/gpu/rocm/rocm_info.cc @@ -16,12 +16,11 @@ #include "paddle/phi/backends/gpu/gpu_info.h" -// TODO(phi): remove fluid headers. -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" static std::once_flag g_device_props_size_init_flag; static std::vector> g_device_props_init_flags; -static std::vector g_device_props; +static std::vector g_device_props; namespace phi { namespace backends { diff --git a/paddle/phi/backends/xpu/enforce_xpu.h b/paddle/phi/backends/xpu/enforce_xpu.h index 44763d408f..71bea73634 100644 --- a/paddle/phi/backends/xpu/enforce_xpu.h +++ b/paddle/phi/backends/xpu/enforce_xpu.h @@ -14,8 +14,8 @@ limitations under the License. */ #pragma once -#include "paddle/fluid/platform/enforce.h" #include "paddle/phi/backends/xpu/xpu_header.h" +#include "paddle/phi/core/enforce.h" #ifdef PADDLE_WITH_XPU_BKCL #include "xpu/bkcl.h" #endif diff --git a/paddle/phi/backends/xpu/xpu_header.h b/paddle/phi/backends/xpu/xpu_header.h index 1fe6f6d077..ca4ea6145c 100644 --- a/paddle/phi/backends/xpu/xpu_header.h +++ b/paddle/phi/backends/xpu/xpu_header.h @@ -19,9 +19,9 @@ limitations under the License. */ #include #include -#include "paddle/fluid/platform/enforce.h" #include "paddle/phi/common/bfloat16.h" #include "paddle/phi/common/float16.h" +#include "paddle/phi/core/enforce.h" #include "xpu/runtime.h" #include "xpu/runtime_ex.h" #include "xpu/xdnn.h" diff --git a/paddle/phi/core/CMakeLists.txt b/paddle/phi/core/CMakeLists.txt index 90f5d38bfc..3ecd022ff5 100644 --- a/paddle/phi/core/CMakeLists.txt +++ b/paddle/phi/core/CMakeLists.txt @@ -1,6 +1,10 @@ # compatible utils used for fluid op system add_subdirectory(compat) +if(WITH_GPU) + proto_library(external_error_proto SRCS external_error.proto) +endif() + cc_library(errors SRCS errors.cc) set(phi_enforce_deps errors flags) if(WITH_GPU) diff --git a/paddle/phi/core/cuda_stream.h b/paddle/phi/core/cuda_stream.h index 61aa9648db..160a31262b 100644 --- a/paddle/phi/core/cuda_stream.h +++ b/paddle/phi/core/cuda_stream.h @@ -28,8 +28,7 @@ using gpuStream_t = cudaStream_t; using gpuStream_t = hipStream_t; #endif -// TODO(phi): remove fluid headers. -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" namespace phi { diff --git a/paddle/phi/core/enforce.h b/paddle/phi/core/enforce.h index cfe8b47ef9..d8449d5ca4 100644 --- a/paddle/phi/core/enforce.h +++ b/paddle/phi/core/enforce.h @@ -1,11 +1,8 @@ /* Copyright (c) 2016 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. @@ -28,21 +25,81 @@ limitations under the License. */ #include // GetModuleFileName, Sleep #endif +#ifdef PADDLE_WITH_CUDA +#include +#include +#include +#include +#include +#include +#include + +#include "paddle/phi/core/external_error.pb.h" +#endif // PADDLE_WITH_CUDA + +#ifdef PADDLE_WITH_HIP +#include +#include +#include +#include +#include // NOLINT +#endif + +#include +#include +#include #include #include #include #include +#include #if !defined(_WIN32) && !defined(PADDLE_WITH_MUSL) #include #endif -#include "gflags/gflags.h" - #define GLOG_NO_ABBREVIATED_SEVERITIES // msvc conflict logging with windows.h +#include "gflags/gflags.h" +#include "glog/logging.h" #include "paddle/phi/core/errors.h" + +#include "paddle/phi/backends/dynload/port.h" #include "paddle/utils/string/printf.h" #include "paddle/utils/string/to_string.h" + +#ifdef PADDLE_WITH_CUDA +#include "paddle/phi/backends/dynload/cublas.h" +#include "paddle/phi/backends/dynload/cudnn.h" +#include "paddle/phi/backends/dynload/curand.h" +#include "paddle/phi/backends/dynload/cusolver.h" +#if !defined(__APPLE__) && defined(PADDLE_WITH_NCCL) +#include + +#include "paddle/phi/backends/dynload/nccl.h" +#endif // __APPLE__ +#endif // PADDLE_WITH_CUDA + +#ifdef PADDLE_WITH_HIP +#include "paddle/phi/backends/dynload/hipfft.h" +#include "paddle/phi/backends/dynload/hiprand.h" +#include "paddle/phi/backends/dynload/miopen.h" +#include "paddle/phi/backends/dynload/rocblas.h" +#if !defined(__APPLE__) && defined(PADDLE_WITH_RCCL) +#include // NOLINT + +#include "paddle/phi/backends/dynload/rccl.h" +#endif // __APPLE__ +#endif // PADDLE_WITH_HIP + +// Note: these headers for simplify demangle type string +#include "paddle/phi/core/enforce.h" +#include "paddle/phi/core/type_defs.h" +// Note: this header for simplify HIP and CUDA type string +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +#include "paddle/phi/backends/gpu/gpu_types.h" +#endif +#include "paddle/fluid/platform/flags.h" + #include "paddle/utils/variant.h" DECLARE_int32(call_stack_level); @@ -51,6 +108,10 @@ namespace phi { class ErrorSummary; } // namespace phi +namespace phi { +namespace proto {} // namespace proto +} // namespace phi + namespace phi { namespace enforce { @@ -420,7 +481,7 @@ struct EnforceNotMet : public std::exception { * the direct use of paddle::get by CI rule. * * Parameters: - *     __TYPE: the target variable type + * __TYPE: the target variable type * __VALUE: the target variable to get * * Examples: @@ -480,6 +541,534 @@ DEFINE_SAFE_PADDLE_GET(InputType&&, phi::enforce::details::SafeBoostGetMutable<__TYPE>( \ __VALUE, #__VALUE, __FILE__, __LINE__) +/**************************************************************************/ +/**************************** NVIDIA ERROR ********************************/ +#ifdef PADDLE_WITH_CUDA + +namespace details { + +template +struct ExternalApiType {}; + +#define DEFINE_EXTERNAL_API_TYPE(type, success_value, proto_type) \ + template <> \ + struct ExternalApiType { \ + using Type = type; \ + static constexpr Type kSuccess = success_value; \ + static constexpr const char* kTypeString = #proto_type; \ + static constexpr phi::proto::ApiType kProtoType = \ + phi::proto::ApiType::proto_type; \ + } + +DEFINE_EXTERNAL_API_TYPE(cudaError_t, cudaSuccess, CUDA); +DEFINE_EXTERNAL_API_TYPE(curandStatus_t, CURAND_STATUS_SUCCESS, CURAND); +DEFINE_EXTERNAL_API_TYPE(cudnnStatus_t, CUDNN_STATUS_SUCCESS, CUDNN); +DEFINE_EXTERNAL_API_TYPE(cublasStatus_t, CUBLAS_STATUS_SUCCESS, CUBLAS); +DEFINE_EXTERNAL_API_TYPE(cusparseStatus_t, CUSPARSE_STATUS_SUCCESS, CUSPARSE); +DEFINE_EXTERNAL_API_TYPE(cusolverStatus_t, CUSOLVER_STATUS_SUCCESS, CUSOLVER); +DEFINE_EXTERNAL_API_TYPE(cufftResult_t, CUFFT_SUCCESS, CUFFT); +DEFINE_EXTERNAL_API_TYPE(CUresult, CUDA_SUCCESS, CU); + +#if !defined(__APPLE__) && defined(PADDLE_WITH_NCCL) +DEFINE_EXTERNAL_API_TYPE(ncclResult_t, ncclSuccess, NCCL); +#endif + +} // namespace details + +template +inline const char* GetErrorMsgUrl(T status) { + using __CUDA_STATUS_TYPE__ = decltype(status); + phi::proto::ApiType proto_type = + details::ExternalApiType<__CUDA_STATUS_TYPE__>::kProtoType; + switch (proto_type) { + case phi::proto::ApiType::CUDA: + case phi::proto::ApiType::CU: + return "https://docs.nvidia.com/cuda/cuda-runtime-api/" + "group__CUDART__TYPES.html#group__CUDART__TYPES_" + "1g3f51e3575c2178246db0a94a430e0038"; + break; + case phi::proto::ApiType::CURAND: + return "https://docs.nvidia.com/cuda/curand/" + "group__HOST.html#group__HOST_1gb94a31d5c165858c96b6c18b70644437"; + break; + case phi::proto::ApiType::CUDNN: + return "https://docs.nvidia.com/deeplearning/cudnn/api/" + "index.html#cudnnStatus_t"; + break; + case phi::proto::ApiType::CUBLAS: + return "https://docs.nvidia.com/cuda/cublas/index.html#cublasstatus_t"; + break; + case phi::proto::ApiType::CUSOLVER: + return "https://docs.nvidia.com/cuda/cusolver/" + "index.html#cuSolverSPstatus"; + break; + case phi::proto::ApiType::NCCL: + return "https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/api/" + "types.html#ncclresult-t"; + break; + case phi::proto::ApiType::CUFFT: + return "https://docs.nvidia.com/cuda/cufft/index.html#cufftresult"; + case phi::proto::ApiType::CUSPARSE: + return "https://docs.nvidia.com/cuda/cusparse/" + "index.html#cusparseStatus_t"; + break; + default: + return "Unknown type of External API, can't get error message URL!"; + break; + } +} + +template +inline std::string GetExternalErrorMsg(T status) { + std::ostringstream sout; + bool _initSucceed = false; + phi::proto::ExternalErrorDesc externalError; + if (externalError.ByteSizeLong() == 0) { + std::string filePath; +#if !defined(_WIN32) + Dl_info info; + if (dladdr(reinterpret_cast(GetCurrentTraceBackString), &info)) { + std::string strModule(info.dli_fname); + const size_t last_slash_idx = strModule.find_last_of("/"); + std::string compare_path = strModule.substr(strModule.length() - 6); + if (std::string::npos != last_slash_idx) { + strModule.erase(last_slash_idx, std::string::npos); + } + if (compare_path.compare("avx.so") == 0) { + filePath = + strModule + + "/../include/third_party/externalError/data/externalErrorMsg.pb"; + } else { + filePath = strModule + + "/../../third_party/externalError/data/externalErrorMsg.pb"; + } + } +#else + char buf[512]; + MEMORY_BASIC_INFORMATION mbi; + HMODULE h_module = + (::VirtualQuery(GetCurrentTraceBackString, &mbi, sizeof(mbi)) != 0) + ? (HMODULE)mbi.AllocationBase + : NULL; + GetModuleFileName(h_module, buf, 512); + std::string strModule(buf); + const size_t last_slash_idx = strModule.find_last_of("\\"); + std::string compare_path = strModule.substr(strModule.length() - 7); + if (std::string::npos != last_slash_idx) { + strModule.erase(last_slash_idx, std::string::npos); + } + if (compare_path.compare("avx.pyd") == 0) { + filePath = strModule + + "\\..\\include\\third_" + "party\\externalerror\\data\\externalErrorMsg.pb"; + } else { + filePath = + strModule + + "\\..\\..\\third_party\\externalerror\\data\\externalErrorMsg.pb"; + } +#endif + std::ifstream fin(filePath, std::ios::in | std::ios::binary); + _initSucceed = externalError.ParseFromIstream(&fin); + } + using __CUDA_STATUS_TYPE__ = decltype(status); + phi::proto::ApiType proto_type = + details::ExternalApiType<__CUDA_STATUS_TYPE__>::kProtoType; + if (_initSucceed) { + for (int i = 0; i < externalError.errors_size(); ++i) { + if (proto_type == externalError.errors(i).type()) { + for (int j = 0; j < externalError.errors(i).messages_size(); ++j) { + if (status == externalError.errors(i).messages(j).code()) { + sout << "\n [Hint: " + << externalError.errors(i).messages(j).message() << "]"; + return sout.str(); + } + } + } + } + } + + sout << "\n [Hint: Please search for the error code(" << status + << ") on website (" << GetErrorMsgUrl(status) + << ") to get Nvidia's official solution and advice about " + << details::ExternalApiType<__CUDA_STATUS_TYPE__>::kTypeString + << " Error.]"; + return sout.str(); +} + +template std::string GetExternalErrorMsg(cudaError_t); +template std::string GetExternalErrorMsg(curandStatus_t); +template std::string GetExternalErrorMsg(cudnnStatus_t); +template std::string GetExternalErrorMsg(cublasStatus_t); +template std::string GetExternalErrorMsg(cusparseStatus_t); +template std::string GetExternalErrorMsg(cusolverStatus_t); +template std::string GetExternalErrorMsg(cufftResult_t); +template std::string GetExternalErrorMsg(CUresult); +#if !defined(__APPLE__) && defined(PADDLE_WITH_NCCL) +template std::string GetExternalErrorMsg(ncclResult_t); +#endif + +/*************** CUDA ERROR ***************/ +inline bool is_error(cudaError_t e) { return e != cudaSuccess; } + +inline std::string build_nvidia_error_msg(cudaError_t e) { + std::ostringstream sout; + sout << "CUDA error(" << e << "), " << cudaGetErrorString(e) << ". " + << GetExternalErrorMsg(e); + return sout.str(); +} + +/*************** CURAND ERROR ***************/ +inline bool is_error(curandStatus_t stat) { + return stat != CURAND_STATUS_SUCCESS; +} + +inline std::string build_nvidia_error_msg(curandStatus_t stat) { + std::ostringstream sout; + sout << "CURAND error(" << stat << "). " << GetExternalErrorMsg(stat); + return sout.str(); +} + +/*************** CUDNN ERROR ***************/ +inline bool is_error(cudnnStatus_t stat) { + return stat != CUDNN_STATUS_SUCCESS; +} + +inline std::string build_nvidia_error_msg(cudnnStatus_t stat) { + std::ostringstream sout; + sout << "CUDNN error(" << stat << "), " + << phi::dynload::cudnnGetErrorString(stat) << ". " + << GetExternalErrorMsg(stat); + return sout.str(); +} + +/*************** CUBLAS ERROR ***************/ +inline bool is_error(cublasStatus_t stat) { + return stat != CUBLAS_STATUS_SUCCESS; +} + +inline std::string build_nvidia_error_msg(cublasStatus_t stat) { + std::ostringstream sout; + sout << "CUBLAS error(" << stat << "). " << GetExternalErrorMsg(stat); + return sout.str(); +} + +/*************** CUSPARSE ERROR ***************/ +inline bool is_error(cusparseStatus_t stat) { + return stat != CUSPARSE_STATUS_SUCCESS; +} + +inline std::string build_nvidia_error_msg(cusparseStatus_t stat) { + std::ostringstream sout; + sout << "CUSparse error(" << stat << "). " << GetExternalErrorMsg(stat); + return sout.str(); +} + +/*************** CUSOLVER ERROR ***************/ +inline bool is_error(cusolverStatus_t stat) { + return stat != CUSOLVER_STATUS_SUCCESS; +} + +inline std::string build_nvidia_error_msg(cusolverStatus_t stat) { + std::ostringstream sout; + sout << "CUSOLVER error(" << stat << "). " << GetExternalErrorMsg(stat); + return sout.str(); +} + +/*************** CUFFT ERROR ***************/ +inline bool is_error(cufftResult_t stat) { return stat != CUFFT_SUCCESS; } + +inline std::string build_nvidia_error_msg(cufftResult_t stat) { + std::ostringstream sout; + sout << "CUFFT error(" << stat << "). " << GetExternalErrorMsg(stat); + return sout.str(); +} + +/*************** CUresult ERROR ***************/ +inline bool is_error(CUresult stat) { return stat != CUDA_SUCCESS; } + +inline std::string build_nvidia_error_msg(CUresult stat) { + std::ostringstream sout; + sout << "CU error(" << stat << "). " << GetExternalErrorMsg(stat); + return sout.str(); +} + +/**************** NCCL ERROR ****************/ +#if !defined(__APPLE__) && defined(PADDLE_WITH_NCCL) +inline bool is_error(ncclResult_t nccl_result) { + return nccl_result != ncclSuccess; +} + +inline std::string build_nvidia_error_msg(ncclResult_t nccl_result) { + std::ostringstream sout; + sout << "NCCL error(" << nccl_result << "), " + << phi::dynload::ncclGetErrorString(nccl_result) << ". "; + if (errno == ENOSPC || errno == EAGAIN) { + std::string detail(strerror(errno)); + detail += "\nPlease try one of the following solutions:"; + detail += "\n1. export NCCL_SHM_DISABLE=1;"; + detail += "\n2. export NCCL_P2P_LEVEL=SYS;"; + detail += + "\n3. Increase shared memory by setting the -shm-size " + "option when starting docker container, e.g., setting " + " -shm-size=2g.\n"; + sout << " Detail: " + detail; + } + sout << GetExternalErrorMsg(nccl_result); + return sout.str(); +} +#endif // not(__APPLE__) and PADDLE_WITH_NCCL + +#define PADDLE_ENFORCE_GPU_SUCCESS(COND) \ + do { \ + auto __cond__ = (COND); \ + using __CUDA_STATUS_TYPE__ = decltype(__cond__); \ + constexpr auto __success_type__ = \ + ::phi::enforce::details::ExternalApiType< \ + __CUDA_STATUS_TYPE__>::kSuccess; \ + if (UNLIKELY(__cond__ != __success_type__)) { \ + auto __summary__ = phi::errors::External( \ + ::phi::enforce::build_nvidia_error_msg(__cond__)); \ + __THROW_ERROR_INTERNAL__(__summary__); \ + } \ + } while (0) + +#define PADDLE_ENFORCE_CUDA_LAUNCH_SUCCESS(OP) \ + do { \ + auto res = cudaGetLastError(); \ + if (UNLIKELY(res != cudaSuccess)) { \ + auto msg = ::phi::enforce::build_nvidia_error_msg(res); \ + PADDLE_THROW( \ + phi::errors::Fatal("CUDA error after kernel (%s): %s", OP, msg)); \ + } \ + } while (0) + +inline void retry_sleep(unsigned milliseconds) { +#ifdef _WIN32 + Sleep(milliseconds); +#else + if (milliseconds < 1000) { + // usleep argument must be less than 1,000,000. Reference: + // https://pubs.opengroup.org/onlinepubs/7908799/xsh/usleep.html + usleep(milliseconds * 1000); + } else { + // clip to sleep in seconds because we can not and don't have to + // sleep for exact milliseconds + sleep(milliseconds / 1000); + } +#endif +} + +#define PADDLE_RETRY_CUDA_SUCCESS(COND) \ + do { \ + auto __cond__ = (COND); \ + int retry_count = 1; \ + using __CUDA_STATUS_TYPE__ = decltype(__cond__); \ + constexpr auto __success_type__ = \ + ::phi::enforce::details::ExternalApiType< \ + __CUDA_STATUS_TYPE__>::kSuccess; \ + while (UNLIKELY(__cond__ != __success_type__) && retry_count < 5) { \ + phi::enforce::retry_sleep(10000); \ + __cond__ = (COND); \ + ++retry_count; \ + } \ + if (UNLIKELY(__cond__ != __success_type__)) { \ + auto __summary__ = phi::errors::External( \ + ::phi::enforce::build_nvidia_error_msg(__cond__)); \ + __THROW_ERROR_INTERNAL__(__summary__); \ + } \ + } while (0) + +#undef DEFINE_EXTERNAL_API_TYPE +#endif // PADDLE_WITH_CUDA + +/**************************************************************************/ +/***************************** HIP ERROR **********************************/ +#ifdef PADDLE_WITH_HIP + +/***** HIP ERROR *****/ +inline bool is_error(hipError_t e) { return e != hipSuccess; } + +inline std::string build_rocm_error_msg(hipError_t e) { + std::ostringstream sout; + sout << " Hip error(" << e << "), " << hipGetErrorString(e) << "."; + return sout.str(); +} + +/***** HIPRAND ERROR *****/ +inline bool is_error(hiprandStatus_t stat) { + return stat != HIPRAND_STATUS_SUCCESS; +} + +inline const char* hiprandGetErrorString(hiprandStatus_t stat) { + switch (stat) { + case HIPRAND_STATUS_SUCCESS: + return "HIPRAND_STATUS_SUCCESS"; + case HIPRAND_STATUS_VERSION_MISMATCH: + return "HIPRAND_STATUS_VERSION_MISMATCH"; + case HIPRAND_STATUS_NOT_INITIALIZED: + return "HIPRAND_STATUS_NOT_INITIALIZED"; + case HIPRAND_STATUS_ALLOCATION_FAILED: + return "HIPRAND_STATUS_ALLOCATION_FAILED"; + case HIPRAND_STATUS_TYPE_ERROR: + return "HIPRAND_STATUS_TYPE_ERROR"; + case HIPRAND_STATUS_OUT_OF_RANGE: + return "HIPRAND_STATUS_OUT_OF_RANGE"; + case HIPRAND_STATUS_LENGTH_NOT_MULTIPLE: + return "HIPRAND_STATUS_LENGTH_NOT_MULTIPLE"; + case HIPRAND_STATUS_DOUBLE_PRECISION_REQUIRED: + return "HIPRAND_STATUS_DOUBLE_PRECISION_REQUIRED"; + case HIPRAND_STATUS_LAUNCH_FAILURE: + return "HIPRAND_STATUS_LAUNCH_FAILURE"; + case HIPRAND_STATUS_PREEXISTING_FAILURE: + return "HIPRAND_STATUS_PREEXISTING_FAILURE"; + case HIPRAND_STATUS_INITIALIZATION_FAILED: + return "HIPRAND_STATUS_INITIALIZATION_FAILED"; + case HIPRAND_STATUS_ARCH_MISMATCH: + return "HIPRAND_STATUS_ARCH_MISMATCH"; + case HIPRAND_STATUS_INTERNAL_ERROR: + return "HIPRAND_STATUS_INTERNAL_ERROR"; + case HIPRAND_STATUS_NOT_IMPLEMENTED: + return "HIPRAND_STATUS_NOT_IMPLEMENTED"; + default: + return "Unknown hiprand status"; + } +} + +inline std::string build_rocm_error_msg(hiprandStatus_t stat) { + std::string msg(" Hiprand error, "); + return msg + hiprandGetErrorString(stat) + " "; +} + +/***** MIOPEN ERROR *****/ +inline bool is_error(miopenStatus_t stat) { + return stat != miopenStatusSuccess; +} + +inline std::string build_rocm_error_msg(miopenStatus_t stat) { + std::string msg(" Miopen error, "); + return msg + phi::dynload::miopenGetErrorString(stat) + " "; +} + +/***** ROCBLAS ERROR *****/ +inline bool is_error(rocblas_status stat) { + return stat != rocblas_status_success; +} + +inline const char* rocblasGetErrorString(rocblas_status stat) { + switch (stat) { + case rocblas_status_invalid_handle: + return "rocblas_status_invalid_handle"; + case rocblas_status_memory_error: + return "rocblas_status_memory_error"; + case rocblas_status_invalid_value: + return "rocblas_status_invalid_value"; + case rocblas_status_not_implemented: + return "rocblas_status_not_implemented"; + case rocblas_status_invalid_pointer: + return "rocblas_status_invalid_pointer"; + case rocblas_status_invalid_size: + return "rocblas_status_invalid_size"; + case rocblas_status_internal_error: + return "rocblas_status_internal_error"; + default: + return "Unknown cublas status"; + } +} + +inline std::string build_rocm_error_msg(rocblas_status stat) { + std::string msg(" Rocblas error, "); + return msg + rocblasGetErrorString(stat) + " "; +} + +/****** RCCL ERROR ******/ +#if !defined(__APPLE__) && defined(PADDLE_WITH_RCCL) +inline bool is_error(ncclResult_t nccl_result) { + return nccl_result != ncclSuccess; +} + +inline std::string build_rocm_error_msg(ncclResult_t nccl_result) { + std::string msg(" Rccl error, "); + return msg + phi::dynload::ncclGetErrorString(nccl_result) + " "; +} +#endif // not(__APPLE__) and PADDLE_WITH_NCCL + +/***** HIPFFT ERROR *****/ +inline bool is_error(hipfftResult_t stat) { return stat != HIPFFT_SUCCESS; } + +inline std::string build_rocm_error_msg(hipfftResult_t stat) { + std::string msg(" HIPFFT error, "); + return msg + phi::dynload::hipfftGetErrorString(stat) + " "; +} + +namespace details { + +template +struct ExternalApiType {}; + +#define DEFINE_EXTERNAL_API_TYPE(type, success_value) \ + template <> \ + struct ExternalApiType { \ + using Type = type; \ + static constexpr Type kSuccess = success_value; \ + } + +DEFINE_EXTERNAL_API_TYPE(hipError_t, hipSuccess); +DEFINE_EXTERNAL_API_TYPE(hiprandStatus_t, HIPRAND_STATUS_SUCCESS); +DEFINE_EXTERNAL_API_TYPE(miopenStatus_t, miopenStatusSuccess); +DEFINE_EXTERNAL_API_TYPE(rocblas_status, rocblas_status_success); +DEFINE_EXTERNAL_API_TYPE(hipfftResult_t, HIPFFT_SUCCESS); + +#if !defined(__APPLE__) && defined(PADDLE_WITH_RCCL) +DEFINE_EXTERNAL_API_TYPE(ncclResult_t, ncclSuccess); +#endif + +} // namespace details + +#define PADDLE_ENFORCE_GPU_SUCCESS(COND) \ + do { \ + auto __cond__ = (COND); \ + using __CUDA_STATUS_TYPE__ = decltype(__cond__); \ + constexpr auto __success_type__ = \ + ::phi::enforce::details::ExternalApiType< \ + __CUDA_STATUS_TYPE__>::kSuccess; \ + if (UNLIKELY(__cond__ != __success_type__)) { \ + auto __summary__ = phi::errors::External( \ + ::phi::enforce::build_rocm_error_msg(__cond__)); \ + __THROW_ERROR_INTERNAL__(__summary__); \ + } \ + } while (0) + +inline void retry_sleep(unsigned millisecond) { +#ifdef _WIN32 + Sleep(millisecond); +#else + sleep(millisecond); +#endif +} + +#define PADDLE_RETRY_CUDA_SUCCESS(COND) \ + do { \ + auto __cond__ = (COND); \ + int retry_count = 1; \ + using __CUDA_STATUS_TYPE__ = decltype(__cond__); \ + constexpr auto __success_type__ = \ + ::phi::enforce::details::ExternalApiType< \ + __CUDA_STATUS_TYPE__>::kSuccess; \ + while (UNLIKELY(__cond__ != __success_type__) && retry_count < 5) { \ + ::phi::enforce::retry_sleep(10000); \ + __cond__ = (COND); \ + ++retry_count; \ + } \ + if (UNLIKELY(__cond__ != __success_type__)) { \ + auto __summary__ = phi::errors::External( \ + ::phi::enforce::build_rocm_error_msg(__cond__)); \ + __THROW_ERROR_INTERNAL__(__summary__); \ + } \ + } while (0) + +#undef DEFINE_EXTERNAL_API_TYPE +#endif // PADDLE_WITH_HIP + } // namespace enforce using namespace enforce; // NOLINT } // namespace phi diff --git a/paddle/fluid/platform/external_error.proto b/paddle/phi/core/external_error.proto similarity index 97% rename from paddle/fluid/platform/external_error.proto rename to paddle/phi/core/external_error.proto index 8861c2c2ff..9922072429 100644 --- a/paddle/fluid/platform/external_error.proto +++ b/paddle/phi/core/external_error.proto @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ syntax = "proto2"; -package paddle.platform.proto; +package phi.proto; // (NOTE:zhouwei): ApiType describes which kind of external third party API // More external third party API can be added. diff --git a/paddle/phi/kernels/autotune/CMakeLists.txt b/paddle/phi/kernels/autotune/CMakeLists.txt index 5b195ef3ec..aa05fcd74c 100644 --- a/paddle/phi/kernels/autotune/CMakeLists.txt +++ b/paddle/phi/kernels/autotune/CMakeLists.txt @@ -2,9 +2,12 @@ if(WITH_CUDNN_FRONTEND) cc_library( cache SRCS cache.cc - DEPS cudnn-frontend) + DEPS cudnn-frontend phi_enforce) else() - cc_library(cache SRCS cache.cc) + cc_library( + cache + SRCS cache.cc + DEPS phi_enforce) endif() cc_library( switch_autotune diff --git a/paddle/phi/kernels/funcs/concat_funcs.h b/paddle/phi/kernels/funcs/concat_funcs.h index 1c1e0eb1d0..db965c2ef9 100644 --- a/paddle/phi/kernels/funcs/concat_funcs.h +++ b/paddle/phi/kernels/funcs/concat_funcs.h @@ -14,7 +14,7 @@ #pragma once -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" #include "paddle/phi/core/errors.h" namespace phi { namespace funcs { diff --git a/paddle/phi/kernels/funcs/cpu_vec.h b/paddle/phi/kernels/funcs/cpu_vec.h index 21a0b429c9..2719f86f52 100644 --- a/paddle/phi/kernels/funcs/cpu_vec.h +++ b/paddle/phi/kernels/funcs/cpu_vec.h @@ -18,7 +18,7 @@ limitations under the License. */ #include #include "paddle/fluid/platform/cpu_info.h" -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" #ifdef PADDLE_WITH_MKLML #include "paddle/phi/backends/dynload/mklml.h" diff --git a/paddle/phi/kernels/funcs/cufft_util.h b/paddle/phi/kernels/funcs/cufft_util.h index 584425c611..3a4a3ef5e5 100644 --- a/paddle/phi/kernels/funcs/cufft_util.h +++ b/paddle/phi/kernels/funcs/cufft_util.h @@ -15,9 +15,9 @@ #pragma once #include -#include "paddle/fluid/platform/enforce.h" #include "paddle/phi/backends/dynload/cufft.h" #include "paddle/phi/core/ddim.h" +#include "paddle/phi/core/enforce.h" #include "paddle/phi/kernels/funcs/fft.h" #include "paddle/phi/kernels/funcs/fft_key.h" diff --git a/paddle/phi/kernels/funcs/gru_compute.h b/paddle/phi/kernels/funcs/gru_compute.h index 02b2b91423..eb2ea85dce 100644 --- a/paddle/phi/kernels/funcs/gru_compute.h +++ b/paddle/phi/kernels/funcs/gru_compute.h @@ -12,7 +12,7 @@ limitations under the License. */ #pragma once #include "paddle/fluid/platform/device_context.h" -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" #include "paddle/phi/kernels/funcs/detail/activation_functions.h" namespace phi { diff --git a/paddle/phi/kernels/funcs/hipfft_util.h b/paddle/phi/kernels/funcs/hipfft_util.h index 6583a97f17..74ca06fcf1 100644 --- a/paddle/phi/kernels/funcs/hipfft_util.h +++ b/paddle/phi/kernels/funcs/hipfft_util.h @@ -15,8 +15,8 @@ #pragma once #include -#include "paddle/fluid/platform/enforce.h" #include "paddle/phi/backends/dynload/hipfft.h" +#include "paddle/phi/core/enforce.h" #include "paddle/phi/kernels/funcs/fft.h" #include "paddle/phi/kernels/funcs/fft_key.h" diff --git a/paddle/phi/kernels/funcs/lstm_compute.h b/paddle/phi/kernels/funcs/lstm_compute.h index d51b92fc4f..0352ce2710 100644 --- a/paddle/phi/kernels/funcs/lstm_compute.h +++ b/paddle/phi/kernels/funcs/lstm_compute.h @@ -15,7 +15,7 @@ limitations under the License. */ #pragma once #include "paddle/fluid/platform/device_context.h" -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" #include "paddle/phi/kernels/funcs/detail/activation_functions.h" namespace phi { diff --git a/paddle/phi/kernels/funcs/math_function.h b/paddle/phi/kernels/funcs/math_function.h index 86d2f5c8ef..48649a454a 100644 --- a/paddle/phi/kernels/funcs/math_function.h +++ b/paddle/phi/kernels/funcs/math_function.h @@ -21,8 +21,8 @@ limitations under the License. */ #include "paddle/fluid/framework/tensor.h" #include "paddle/fluid/framework/tensor_util.h" #include "paddle/fluid/platform/device_context.h" -#include "paddle/fluid/platform/enforce.h" #include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/enforce.h" #include "paddle/phi/core/utils/data_type.h" #include "paddle/phi/kernels/funcs/eigen/common.h" diff --git a/paddle/phi/kernels/gpu/batch_norm_grad_kernel.cu b/paddle/phi/kernels/gpu/batch_norm_grad_kernel.cu index e6c681588e..afa73f0a57 100644 --- a/paddle/phi/kernels/gpu/batch_norm_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/batch_norm_grad_kernel.cu @@ -15,10 +15,10 @@ #include "paddle/fluid/operators/layout_utils.h" #include "paddle/fluid/operators/norm_utils.cu.h" #include "paddle/fluid/platform/device/gpu/gpu_dnn.h" -#include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/flags.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/common/layout.h" +#include "paddle/phi/core/enforce.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/batch_norm_kernel.h" #include "paddle/phi/kernels/empty_kernel.h" diff --git a/paddle/phi/kernels/gpu/batch_norm_kernel.cu b/paddle/phi/kernels/gpu/batch_norm_kernel.cu index 44fe99046e..126b5c81ab 100644 --- a/paddle/phi/kernels/gpu/batch_norm_kernel.cu +++ b/paddle/phi/kernels/gpu/batch_norm_kernel.cu @@ -23,10 +23,10 @@ namespace cub = hipcub; #include "paddle/fluid/operators/layout_utils.h" #include "paddle/fluid/operators/norm_utils.cu.h" #include "paddle/fluid/platform/device/gpu/gpu_dnn.h" -#include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/flags.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/common/layout.h" +#include "paddle/phi/core/enforce.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/batch_norm_kernel.h" #include "paddle/phi/kernels/funcs/batch_norm_utils.h" diff --git a/paddle/phi/kernels/gpu/cholesky_solve_kernel.cu b/paddle/phi/kernels/gpu/cholesky_solve_kernel.cu index f74f4bf381..f350106f67 100644 --- a/paddle/phi/kernels/gpu/cholesky_solve_kernel.cu +++ b/paddle/phi/kernels/gpu/cholesky_solve_kernel.cu @@ -15,10 +15,10 @@ #ifndef PADDLE_WITH_HIP // HIP not support cusolver -#include "paddle/fluid/platform/enforce.h" #include "paddle/phi/backends/dynload/cusolver.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/common/complex.h" +#include "paddle/phi/core/enforce.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/lapack/lapack_function.h" #include "paddle/phi/kernels/impl/cholesky_solve_kernel_impl.h" diff --git a/paddle/phi/kernels/gpu/class_center_sample_kernel.cu b/paddle/phi/kernels/gpu/class_center_sample_kernel.cu index eb92a4488e..65dace12b0 100644 --- a/paddle/phi/kernels/gpu/class_center_sample_kernel.cu +++ b/paddle/phi/kernels/gpu/class_center_sample_kernel.cu @@ -30,7 +30,7 @@ namespace cub = hipcub; #include #include "paddle/fluid/framework/tensor_util.h" -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/enforce.h" #if defined(PADDLE_WITH_NCCL) || defined(PADDLE_WITH_RCCL) #include "paddle/fluid/distributed/collective/ProcessGroup.h" diff --git a/tools/externalError/start.sh b/tools/externalError/start.sh index 82715dd473..ea9da231ec 100644 --- a/tools/externalError/start.sh +++ b/tools/externalError/start.sh @@ -29,7 +29,7 @@ else echo "please run on Mac/Linux" exit 1 fi -protobuf/bin/protoc -I../../paddle/fluid/platform/ --python_out . ../../paddle/fluid/platform/external_error.proto +protobuf/bin/protoc -I../../paddle/phi/core/ --python_out . ../../paddle/phi/core/external_error.proto python3.7 spider.py tar czvf externalErrorMsg_$(date +'%Y%m%d').tar.gz externalErrorMsg.pb -- GitLab