Skip to content

Commit b8e9308

Browse files
authored
Added rois shape check in C++ (#2794)
* Added rois shape check in C++ * Fixes code formatting * Remove accidental include * - Updated code according to the review - Replaced old AT_ASSERT/ERROR by new TORCH_CHECK
1 parent 5bb81c8 commit b8e9308

17 files changed

+77
-60
lines changed

torchvision/csrc/DeformConv.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ at::Tensor DeformConv2d_forward(
3232
groups,
3333
offset_groups);
3434
#else
35-
AT_ERROR("Not compiled with GPU support");
35+
TORCH_CHECK(false, "Not compiled with GPU support");
3636
#endif
3737
}
3838
return DeformConv2d_forward_cpu(
@@ -72,7 +72,7 @@ std::tuple<at::Tensor, at::Tensor, at::Tensor, at::Tensor> DeformConv2d_backward
7272
groups,
7373
offset_groups);
7474
#else
75-
AT_ERROR("Not compiled with GPU support");
75+
TORCH_CHECK(false, "Not compiled with GPU support");
7676
#endif
7777
}
7878
return DeformConv2d_backward_cpu(

torchvision/csrc/PSROIAlign.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ std::tuple<at::Tensor, at::Tensor> PSROIAlign_forward(
2828
pooled_width,
2929
sampling_ratio);
3030
#else
31-
AT_ERROR("Not compiled with GPU support");
31+
TORCH_CHECK(false, "Not compiled with GPU support");
3232
#endif
3333
}
3434
return PSROIAlign_forward_cpu(
@@ -62,7 +62,7 @@ at::Tensor PSROIAlign_backward(
6262
height,
6363
width);
6464
#else
65-
AT_ERROR("Not compiled with GPU support");
65+
TORCH_CHECK(false, "Not compiled with GPU support");
6666
#endif
6767
}
6868
return PSROIAlign_backward_cpu(

torchvision/csrc/PSROIPool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ std::tuple<at::Tensor, at::Tensor> PSROIPool_forward(
2020
return PSROIPool_forward_cuda(
2121
input, rois, spatial_scale, pooled_height, pooled_width);
2222
#else
23-
AT_ERROR("Not compiled with GPU support");
23+
TORCH_CHECK(false, "Not compiled with GPU support");
2424
#endif
2525
}
2626
return PSROIPool_forward_cpu(
@@ -52,7 +52,7 @@ at::Tensor PSROIPool_backward(
5252
height,
5353
width);
5454
#else
55-
AT_ERROR("Not compiled with GPU support");
55+
TORCH_CHECK(false, "Not compiled with GPU support");
5656
#endif
5757
}
5858
return PSROIPool_backward_cpu(

torchvision/csrc/ROIPool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ std::tuple<at::Tensor, at::Tensor> ROIPool_forward(
2020
return ROIPool_forward_cuda(
2121
input, rois, spatial_scale, pooled_height, pooled_width);
2222
#else
23-
AT_ERROR("Not compiled with GPU support");
23+
TORCH_CHECK(false, "Not compiled with GPU support");
2424
#endif
2525
}
2626
return ROIPool_forward_cpu(
@@ -52,7 +52,7 @@ at::Tensor ROIPool_backward(
5252
height,
5353
width);
5454
#else
55-
AT_ERROR("Not compiled with GPU support");
55+
TORCH_CHECK(false, "Not compiled with GPU support");
5656
#endif
5757
}
5858
return ROIPool_backward_cpu(

torchvision/csrc/cpu/PSROIAlign_cpu.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,10 @@ std::tuple<at::Tensor, at::Tensor> PSROIAlign_forward_cpu(
308308
const int pooled_width,
309309
const int sampling_ratio) {
310310
// Check if input tensors are CPU tensors
311-
AT_ASSERTM(input.device().is_cpu(), "input must be a CPU tensor");
312-
AT_ASSERTM(rois.device().is_cpu(), "rois must be a CPU tensor");
311+
TORCH_CHECK(input.device().is_cpu(), "input must be a CPU tensor");
312+
TORCH_CHECK(rois.device().is_cpu(), "rois must be a CPU tensor");
313+
TORCH_CHECK(
314+
rois.size(1) == 5, "Tensor rois should have shape as Tensor[K, 5]");
313315

314316
at::TensorArg input_t{input, "input", 1}, rois_t{rois, "rois", 2};
315317

@@ -321,7 +323,7 @@ std::tuple<at::Tensor, at::Tensor> PSROIAlign_forward_cpu(
321323
int height = input.size(2);
322324
int width = input.size(3);
323325

324-
AT_ASSERTM(
326+
TORCH_CHECK(
325327
channels % (pooled_height * pooled_width) == 0,
326328
"input channels must be a multiple of pooling height * pooling width");
327329
int channels_out = channels / (pooled_height * pooled_width);
@@ -370,9 +372,9 @@ at::Tensor PSROIAlign_backward_cpu(
370372
const int height,
371373
const int width) {
372374
// Check if input tensors are CPU tensors
373-
AT_ASSERTM(grad.device().is_cpu(), "grad must be a CPU tensor");
374-
AT_ASSERTM(rois.device().is_cpu(), "rois must be a CPU tensor");
375-
AT_ASSERTM(
375+
TORCH_CHECK(grad.device().is_cpu(), "grad must be a CPU tensor");
376+
TORCH_CHECK(rois.device().is_cpu(), "rois must be a CPU tensor");
377+
TORCH_CHECK(
376378
channel_mapping.device().is_cpu(),
377379
"channel_mapping must be a CPU tensor");
378380

torchvision/csrc/cpu/PSROIPool_cpu.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,10 @@ std::tuple<at::Tensor, at::Tensor> PSROIPool_forward_cpu(
150150
const int pooled_height,
151151
const int pooled_width) {
152152
// Check if input tensors are CPU tensors
153-
AT_ASSERTM(input.device().is_cpu(), "input must be a CPU tensor");
154-
AT_ASSERTM(rois.device().is_cpu(), "rois must be a CPU tensor");
153+
TORCH_CHECK(input.device().is_cpu(), "input must be a CPU tensor");
154+
TORCH_CHECK(rois.device().is_cpu(), "rois must be a CPU tensor");
155+
TORCH_CHECK(
156+
rois.size(1) == 5, "Tensor rois should have shape as Tensor[K, 5]");
155157

156158
at::TensorArg input_t{input, "input", 1}, rois_t{rois, "rois", 2};
157159

@@ -163,7 +165,7 @@ std::tuple<at::Tensor, at::Tensor> PSROIPool_forward_cpu(
163165
int height = input.size(2);
164166
int width = input.size(3);
165167

166-
AT_ASSERTM(
168+
TORCH_CHECK(
167169
channels % (pooled_height * pooled_width) == 0,
168170
"input channels must be a multiple of pooling height * pooling width");
169171
int channels_out = channels / (pooled_height * pooled_width);
@@ -210,9 +212,9 @@ at::Tensor PSROIPool_backward_cpu(
210212
const int height,
211213
const int width) {
212214
// Check if input tensors are CPU tensors
213-
AT_ASSERTM(grad.device().is_cpu(), "grad must be a CPU tensor");
214-
AT_ASSERTM(rois.device().is_cpu(), "rois must be a CPU tensor");
215-
AT_ASSERTM(
215+
TORCH_CHECK(grad.device().is_cpu(), "grad must be a CPU tensor");
216+
TORCH_CHECK(rois.device().is_cpu(), "rois must be a CPU tensor");
217+
TORCH_CHECK(
216218
channel_mapping.device().is_cpu(),
217219
"channel_mapping must be a CPU tensor");
218220

torchvision/csrc/cpu/ROIAlign_cpu.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,9 @@ at::Tensor ROIAlign_forward_cpu(
394394
const int64_t pooled_width,
395395
const int64_t sampling_ratio,
396396
const bool aligned) {
397-
AT_ASSERTM(input.device().is_cpu(), "input must be a CPU tensor");
398-
AT_ASSERTM(rois.device().is_cpu(), "rois must be a CPU tensor");
397+
TORCH_CHECK(input.device().is_cpu(), "input must be a CPU tensor");
398+
TORCH_CHECK(rois.device().is_cpu(), "rois must be a CPU tensor");
399+
TORCH_CHECK(rois.size(1) == 5, "rois must have shape as Tensor[K, 5]");
399400

400401
at::TensorArg input_t{input, "input", 1}, rois_t{rois, "rois", 2};
401402

@@ -447,8 +448,8 @@ at::Tensor ROIAlign_backward_cpu(
447448
const int64_t width,
448449
const int64_t sampling_ratio,
449450
const bool aligned) {
450-
AT_ASSERTM(grad.device().is_cpu(), "grad must be a CPU tensor");
451-
AT_ASSERTM(rois.device().is_cpu(), "rois must be a CPU tensor");
451+
TORCH_CHECK(grad.device().is_cpu(), "grad must be a CPU tensor");
452+
TORCH_CHECK(rois.device().is_cpu(), "rois must be a CPU tensor");
452453

453454
at::TensorArg grad_t{grad, "grad", 1}, rois_t{rois, "rois", 2};
454455

torchvision/csrc/cpu/ROIPool_cpu.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ std::tuple<at::Tensor, at::Tensor> ROIPool_forward_cpu(
126126
const float spatial_scale,
127127
const int pooled_height,
128128
const int pooled_width) {
129-
AT_ASSERTM(input.device().is_cpu(), "input must be a CPU tensor");
130-
AT_ASSERTM(rois.device().is_cpu(), "rois must be a CPU tensor");
129+
TORCH_CHECK(input.device().is_cpu(), "input must be a CPU tensor");
130+
TORCH_CHECK(rois.device().is_cpu(), "rois must be a CPU tensor");
131131

132132
at::TensorArg input_t{input, "input", 1}, rois_t{rois, "rois", 2};
133133

@@ -180,9 +180,11 @@ at::Tensor ROIPool_backward_cpu(
180180
const int height,
181181
const int width) {
182182
// Check if input tensors are CPU tensors
183-
AT_ASSERTM(grad.device().is_cpu(), "grad must be a CPU tensor");
184-
AT_ASSERTM(rois.device().is_cpu(), "rois must be a CPU tensor");
185-
AT_ASSERTM(argmax.device().is_cpu(), "argmax must be a CPU tensor");
183+
TORCH_CHECK(grad.device().is_cpu(), "grad must be a CPU tensor");
184+
TORCH_CHECK(rois.device().is_cpu(), "rois must be a CPU tensor");
185+
TORCH_CHECK(argmax.device().is_cpu(), "argmax must be a CPU tensor");
186+
TORCH_CHECK(
187+
rois.size(1) == 5, "Tensor rois should have shape as Tensor[K, 5]");
186188

187189
at::TensorArg grad_t{grad, "grad", 1}, rois_t{rois, "rois", 2};
188190

torchvision/csrc/cpu/image/readpng_cpu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#if !PNG_FOUND
99
torch::Tensor decodePNG(const torch::Tensor& data) {
10-
AT_ERROR("decodePNG: torchvision not compiled with libPNG support");
10+
TORCH_CHECK(false, "decodePNG: torchvision not compiled with libPNG support");
1111
}
1212
#else
1313
#include <png.h>

torchvision/csrc/cpu/nms_cpu.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ at::Tensor nms_cpu_kernel(
55
const at::Tensor& dets,
66
const at::Tensor& scores,
77
const double iou_threshold) {
8-
AT_ASSERTM(!dets.is_cuda(), "dets must be a CPU tensor");
9-
AT_ASSERTM(!scores.is_cuda(), "scores must be a CPU tensor");
10-
AT_ASSERTM(
8+
TORCH_CHECK(!dets.is_cuda(), "dets must be a CPU tensor");
9+
TORCH_CHECK(!scores.is_cuda(), "scores must be a CPU tensor");
10+
TORCH_CHECK(
1111
dets.scalar_type() == scores.scalar_type(),
1212
"dets should have the same type as scores");
1313

0 commit comments

Comments
 (0)