From 4976153dd53605e0be3fecc4fec393396ab181c2 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Tue, 31 Jan 2023 10:38:32 +0800 Subject: [PATCH] add dims check for nms_kernel (#49993) --- paddle/phi/kernels/cpu/nms_kernel.cc | 12 ++++++++++++ paddle/phi/kernels/gpu/nms_kernel.cu | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/paddle/phi/kernels/cpu/nms_kernel.cc b/paddle/phi/kernels/cpu/nms_kernel.cc index 4b56f6bb95..6743f13fff 100644 --- a/paddle/phi/kernels/cpu/nms_kernel.cc +++ b/paddle/phi/kernels/cpu/nms_kernel.cc @@ -69,6 +69,18 @@ void NMSKernel(const Context& dev_ctx, const DenseTensor& boxes, float threshold, DenseTensor* output) { + PADDLE_ENFORCE_EQ( + boxes.dims().size(), + 2, + phi::errors::InvalidArgument("The shape [%s] of boxes must be (N, 4).", + boxes.dims())); + + PADDLE_ENFORCE_EQ( + boxes.dims()[1], + 4, + phi::errors::InvalidArgument("The shape [%s] of boxes must be (N, 4).", + boxes.dims())); + int64_t num_boxes = boxes.dims()[0]; DenseTensor output_tmp; output_tmp.Resize(phi::make_ddim({num_boxes})); diff --git a/paddle/phi/kernels/gpu/nms_kernel.cu b/paddle/phi/kernels/gpu/nms_kernel.cu index 79b0b8dfb1..81f5ca8d16 100644 --- a/paddle/phi/kernels/gpu/nms_kernel.cu +++ b/paddle/phi/kernels/gpu/nms_kernel.cu @@ -59,6 +59,18 @@ void NMSKernel(const Context& dev_ctx, const DenseTensor& boxes, float threshold, DenseTensor* output) { + PADDLE_ENFORCE_EQ( + boxes.dims().size(), + 2, + phi::errors::InvalidArgument("The shape [%s] of boxes must be (N, 4).", + boxes.dims())); + + PADDLE_ENFORCE_EQ( + boxes.dims()[1], + 4, + phi::errors::InvalidArgument("The shape [%s] of boxes must be (N, 4).", + boxes.dims())); + const int64_t num_boxes = boxes.dims()[0]; const auto blocks_per_line = CeilDivide(num_boxes, threadsPerBlock); dim3 block(threadsPerBlock); -- GitLab