xref: /aosp_15_r20/external/pytorch/c10/cuda/CUDAException.cpp (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #include <c10/cuda/CUDAException.h>
2 
3 #include <c10/cuda/CUDADeviceAssertionHost.h>
4 #include <c10/util/Exception.h>
5 #include <cuda_runtime.h>
6 
7 #include <string>
8 
9 namespace c10::cuda {
10 
c10_cuda_check_implementation(const int32_t err,const char * filename,const char * function_name,const int line_number,const bool include_device_assertions)11 void c10_cuda_check_implementation(
12     const int32_t err,
13     const char* filename,
14     const char* function_name,
15     const int line_number,
16     const bool include_device_assertions) {
17   const auto cuda_error = static_cast<cudaError_t>(err);
18   const auto cuda_kernel_failure = include_device_assertions
19       ? c10::cuda::CUDAKernelLaunchRegistry::get_singleton_ref().has_failed()
20       : false;
21 
22   if (C10_LIKELY(cuda_error == cudaSuccess && !cuda_kernel_failure)) {
23     return;
24   }
25 
26   auto error_unused C10_UNUSED = cudaGetLastError();
27   (void)error_unused;
28 
29   std::string check_message;
30 #ifndef STRIP_ERROR_MESSAGES
31   check_message.append("CUDA error: ");
32   check_message.append(cudaGetErrorString(cuda_error));
33   check_message.append(c10::cuda::get_cuda_check_suffix());
34   check_message.append("\n");
35   if (include_device_assertions) {
36     check_message.append(c10_retrieve_device_side_assertion_info());
37   } else {
38     check_message.append(
39         "Device-side assertions were explicitly omitted for this error check; the error probably arose while initializing the DSA handlers.");
40   }
41 #endif
42 
43   TORCH_CHECK(false, check_message);
44 }
45 
46 } // namespace c10::cuda
47