Skip to content

Commit 116ca84

Browse files
committed
fix(pt): throw errors for GPU tensors and the CPU OP library
@iProzd has triggered a situtation where he used the CUDA-compiled PyTorch along with CPU version of customized OP libraries, and then the OP gave incorrect results since it didn't do any computation. Signed-off-by: Jinzhe Zeng <[email protected]>
1 parent 228062c commit 116ca84

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

source/op/pt/tabulate_multi_device.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ void TabulateFusionSeAForward(const torch::Tensor& table_tensor,
6161
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
6262
deepmd::tabulate_fusion_se_a_gpu(descriptor, table, table_info, em_x, em,
6363
two_embed, nloc, nnei, last_layer_size);
64+
#else
65+
throw std::runtime_error(
66+
"The input tensor is on the GPU, but the GPU support for the "
67+
"customized OP library is not enabled.");
6468
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
6569
} else if (device == "CPU") {
6670
deepmd::tabulate_fusion_se_a_cpu(descriptor, table, table_info, em_x, em,
@@ -110,6 +114,10 @@ void TabulateFusionSeAGradForward(const torch::Tensor& table_tensor,
110114
deepmd::tabulate_fusion_se_a_grad_gpu(dy_dem_x, dy_dem, dy_dtwo, table,
111115
table_info, em_x, em, two_embed, dy,
112116
nloc, nnei, last_layer_size);
117+
#else
118+
throw std::runtime_error(
119+
"The input tensor is on the GPU, but the GPU support for the "
120+
"customized OP library is not enabled.");
113121
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
114122
} else if (device == "CPU") {
115123
deepmd::tabulate_fusion_se_a_grad_cpu(dy_dem_x, dy_dem, dy_dtwo, table,
@@ -166,6 +174,10 @@ void TabulateFusionSeAGradGradForward(const torch::Tensor& table_tensor,
166174
deepmd::tabulate_fusion_se_a_grad_grad_gpu(
167175
dz_dy, table, table_info, em_x, em, two_embed, dz_dy_dem_x, dz_dy_dem,
168176
dz_dy_dtwo, nloc, nnei, last_layer_size, is_sorted);
177+
#else
178+
throw std::runtime_error(
179+
"The input tensor is on the GPU, but the GPU support for the "
180+
"customized OP library is not enabled.");
169181
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
170182
TORCH_CHECK(last_layer_size <= 1024,
171183
"In the process of model compression, the size of the "
@@ -212,6 +224,10 @@ void TabulateFusionSeTForward(const torch::Tensor& table_tensor,
212224
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
213225
deepmd::tabulate_fusion_se_t_gpu(descriptor, table, table_info, em_x, em,
214226
nloc, nnei_i, nnei_j, last_layer_size);
227+
#else
228+
throw std::runtime_error(
229+
"The input tensor is on the GPU, but the GPU support for the "
230+
"customized OP library is not enabled.");
215231
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
216232
} else if (device == "CPU") {
217233
deepmd::tabulate_fusion_se_t_cpu(descriptor, table, table_info, em_x, em,
@@ -254,6 +270,10 @@ void TabulateFusionSeTGradForward(const torch::Tensor& table_tensor,
254270
deepmd::tabulate_fusion_se_t_grad_gpu(dy_dem_x, dy_dem, table, table_info,
255271
em_x, em, dy, nloc, nnei_i, nnei_j,
256272
last_layer_size);
273+
#else
274+
throw std::runtime_error(
275+
"The input tensor is on the GPU, but the GPU support for the "
276+
"customized OP library is not enabled.");
257277
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
258278
} else if (device == "CPU") {
259279
deepmd::tabulate_fusion_se_t_grad_cpu(dy_dem_x, dy_dem, table, table_info,
@@ -300,6 +320,10 @@ void TabulateFusionSeTGradGradForward(const torch::Tensor& table_tensor,
300320
deepmd::tabulate_fusion_se_t_grad_grad_gpu(dz_dy, table, table_info, em_x,
301321
em, dz_dy_dem_x, dz_dy_dem, nloc,
302322
nnei_i, nnei_j, last_layer_size);
323+
#else
324+
throw std::runtime_error(
325+
"The input tensor is on the GPU, but the GPU support for the "
326+
"customized OP library is not enabled.");
303327
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
304328
TORCH_CHECK(last_layer_size <= 1024,
305329
"In the process of model compression, the size of the "
@@ -340,6 +364,10 @@ void TabulateFusionSeRForward(const torch::Tensor& table_tensor,
340364
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
341365
deepmd::tabulate_fusion_se_r_gpu(descriptor, table, table_info, em, nloc,
342366
nnei, last_layer_size);
367+
#else
368+
throw std::runtime_error(
369+
"The input tensor is on the GPU, but the GPU support for the "
370+
"customized OP library is not enabled.");
343371
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
344372
} else if (device == "CPU") {
345373
deepmd::tabulate_fusion_se_r_cpu(descriptor, table, table_info, em, nloc,
@@ -376,6 +404,10 @@ void TabulateFusionSeRGradForward(const torch::Tensor& table_tensor,
376404
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
377405
deepmd::tabulate_fusion_se_r_grad_gpu(dy_dem, table, table_info, em, dy,
378406
nloc, nnei, last_layer_size);
407+
#else
408+
throw std::runtime_error(
409+
"The input tensor is on the GPU, but the GPU support for the "
410+
"customized OP library is not enabled.");
379411
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
380412
} else if (device == "CPU") {
381413
deepmd::tabulate_fusion_se_r_grad_cpu(dy_dem, table, table_info, em, dy,
@@ -412,6 +444,10 @@ void TabulateFusionSeRGradGradForward(const torch::Tensor& table_tensor,
412444
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
413445
deepmd::tabulate_fusion_se_r_grad_grad_gpu(
414446
dz_dy, table, table_info, em, dz_dy_dem, nloc, nnei, last_layer_size);
447+
#else
448+
throw std::runtime_error(
449+
"The input tensor is on the GPU, but the GPU support for the "
450+
"customized OP library is not enabled.");
415451
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
416452
TORCH_CHECK(last_layer_size <= 1024,
417453
"In the process of model compression, the size of the "

0 commit comments

Comments
 (0)