From 58f3a1e8c1a754a9eea6cf932b3ad40dcd3d141f Mon Sep 17 00:00:00 2001 From: co63oc Date: Fri, 30 May 2025 09:16:01 +0800 Subject: [PATCH 1/3] Fix --- paddle/phi/kernels/cpu/yolo_box_kernel.cc | 6 ++++++ paddle/phi/kernels/gpu/yolo_box_kernel.cu | 6 ++++++ paddle/phi/kernels/xpu/yolo_box_kernel.cc | 7 +++++++ test/legacy_test/test_yolo_box_op.py | 25 ++++++++++++++++++++++- 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/paddle/phi/kernels/cpu/yolo_box_kernel.cc b/paddle/phi/kernels/cpu/yolo_box_kernel.cc index c80e99e9ea8bdc..c3287e47e7891b 100644 --- a/paddle/phi/kernels/cpu/yolo_box_kernel.cc +++ b/paddle/phi/kernels/cpu/yolo_box_kernel.cc @@ -35,6 +35,12 @@ void YoloBoxKernel(const Context& dev_ctx, float iou_aware_factor, DenseTensor* boxes, DenseTensor* scores) { + if (x.numel() == 0 || img_size.numel() == 0) { + phi::Full( + dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out); + return; + } + auto* input = &x; auto* imgsize = &img_size; float scale = scale_x_y; diff --git a/paddle/phi/kernels/gpu/yolo_box_kernel.cu b/paddle/phi/kernels/gpu/yolo_box_kernel.cu index 8616b8bb429556..597edb5fa5ee13 100644 --- a/paddle/phi/kernels/gpu/yolo_box_kernel.cu +++ b/paddle/phi/kernels/gpu/yolo_box_kernel.cu @@ -115,6 +115,12 @@ void YoloBoxKernel(const Context& dev_ctx, float iou_aware_factor, DenseTensor* boxes, DenseTensor* scores) { + if (x.numel() == 0 || img_size.numel() == 0) { + phi::Full( + dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out); + return; + } + auto* input = &x; float scale = scale_x_y; float bias = -0.5 * (scale - 1.); diff --git a/paddle/phi/kernels/xpu/yolo_box_kernel.cc b/paddle/phi/kernels/xpu/yolo_box_kernel.cc index cfb1e443650bc9..d4d3ccbdc6e655 100644 --- a/paddle/phi/kernels/xpu/yolo_box_kernel.cc +++ b/paddle/phi/kernels/xpu/yolo_box_kernel.cc @@ -16,6 +16,7 @@ #include "paddle/phi/backends/xpu/enforce_xpu.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/funcs/yolo_box_util.h" namespace phi { @@ -34,6 +35,12 @@ void YoloBoxKernel(const Context& dev_ctx, float iou_aware_factor, DenseTensor* boxes, DenseTensor* scores) { + if (x.numel() == 0 || img_size.numel() == 0) { + phi::Full( + dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out); + return; + } + using XPUType = typename XPUTypeTrait::Type; int r = 0; auto* input = &x; diff --git a/test/legacy_test/test_yolo_box_op.py b/test/legacy_test/test_yolo_box_op.py index a7199b2ca5a5a6..fe6371bbb1ea24 100644 --- a/test/legacy_test/test_yolo_box_op.py +++ b/test/legacy_test/test_yolo_box_op.py @@ -56,7 +56,13 @@ def YoloBox(x, img_size, attrs): (anchors[i], anchors[(i + 1)]) for i in range(0, len(anchors), 2) ] anchors_s = np.array( - [((an_w / input_w), (an_h / input_h)) for (an_w, an_h) in anchors] + [ + ( + (an_w / input_w if input_w > 0 else 0), + (an_h / input_h if input_h > 0 else 0), + ) + for (an_w, an_h) in anchors + ] ) anchor_w = anchors_s[:, 0:1].reshape((1, an_num, 1, 1)) anchor_h = anchors_s[:, 1:2].reshape((1, an_num, 1, 1)) @@ -309,6 +315,23 @@ def initTestCase(self): self.iou_aware_factor = 0.5 +class TestYoloBoxOp_ZeroSize(TestYoloBoxOp): + def initTestCase(self): + self.__class__.op_type = "yolo_box" + self.anchors = [10, 13, 16, 30, 33, 23] + an_num = int(len(self.anchors) // 2) + self.batch_size = 32 + self.class_num = 2 + self.conf_thresh = 0.5 + self.downsample = 32 + self.clip_bbox = False + self.x_shape = (self.batch_size, (an_num * (5 + self.class_num)), 13, 0) + self.imgsize_shape = (self.batch_size, 2) + self.scale_x_y = 1.0 + self.iou_aware = False + self.iou_aware_factor = 0.5 + + if __name__ == '__main__': paddle.enable_static() unittest.main() From 4dcd1661bf56198c9854cfe7a21d57b1b6c62dbd Mon Sep 17 00:00:00 2001 From: co63oc Date: Fri, 30 May 2025 09:19:24 +0800 Subject: [PATCH 2/3] Fix --- paddle/phi/kernels/cpu/yolo_box_kernel.cc | 4 +++- paddle/phi/kernels/gpu/yolo_box_kernel.cu | 4 +++- paddle/phi/kernels/xpu/yolo_box_kernel.cc | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/paddle/phi/kernels/cpu/yolo_box_kernel.cc b/paddle/phi/kernels/cpu/yolo_box_kernel.cc index c3287e47e7891b..ba92226f159dfb 100644 --- a/paddle/phi/kernels/cpu/yolo_box_kernel.cc +++ b/paddle/phi/kernels/cpu/yolo_box_kernel.cc @@ -37,7 +37,9 @@ void YoloBoxKernel(const Context& dev_ctx, DenseTensor* scores) { if (x.numel() == 0 || img_size.numel() == 0) { phi::Full( - dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out); + dev_ctx, phi::IntArray(common::vectorize(boxes->dims())), 0, boxes); + phi::Full( + dev_ctx, phi::IntArray(common::vectorize(scores->dims())), 0, scores); return; } diff --git a/paddle/phi/kernels/gpu/yolo_box_kernel.cu b/paddle/phi/kernels/gpu/yolo_box_kernel.cu index 597edb5fa5ee13..b1e1a5da69c25a 100644 --- a/paddle/phi/kernels/gpu/yolo_box_kernel.cu +++ b/paddle/phi/kernels/gpu/yolo_box_kernel.cu @@ -117,7 +117,9 @@ void YoloBoxKernel(const Context& dev_ctx, DenseTensor* scores) { if (x.numel() == 0 || img_size.numel() == 0) { phi::Full( - dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out); + dev_ctx, phi::IntArray(common::vectorize(boxes->dims())), 0, boxes); + phi::Full( + dev_ctx, phi::IntArray(common::vectorize(scores->dims())), 0, scores); return; } diff --git a/paddle/phi/kernels/xpu/yolo_box_kernel.cc b/paddle/phi/kernels/xpu/yolo_box_kernel.cc index d4d3ccbdc6e655..ce2493a6798be4 100644 --- a/paddle/phi/kernels/xpu/yolo_box_kernel.cc +++ b/paddle/phi/kernels/xpu/yolo_box_kernel.cc @@ -37,7 +37,9 @@ void YoloBoxKernel(const Context& dev_ctx, DenseTensor* scores) { if (x.numel() == 0 || img_size.numel() == 0) { phi::Full( - dev_ctx, phi::IntArray(common::vectorize(out->dims())), 0, out); + dev_ctx, phi::IntArray(common::vectorize(boxes->dims())), 0, boxes); + phi::Full( + dev_ctx, phi::IntArray(common::vectorize(scores->dims())), 0, scores); return; } From a216318f32f5d33fa3dd47f666eede31d388cbe0 Mon Sep 17 00:00:00 2001 From: co63oc Date: Fri, 30 May 2025 09:21:49 +0800 Subject: [PATCH 3/3] Fix --- paddle/phi/kernels/cpu/yolo_box_kernel.cc | 1 + paddle/phi/kernels/gpu/yolo_box_kernel.cu | 1 + 2 files changed, 2 insertions(+) diff --git a/paddle/phi/kernels/cpu/yolo_box_kernel.cc b/paddle/phi/kernels/cpu/yolo_box_kernel.cc index ba92226f159dfb..dff9f544639347 100644 --- a/paddle/phi/kernels/cpu/yolo_box_kernel.cc +++ b/paddle/phi/kernels/cpu/yolo_box_kernel.cc @@ -17,6 +17,7 @@ #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/funcs/yolo_box_util.h" namespace phi { diff --git a/paddle/phi/kernels/gpu/yolo_box_kernel.cu b/paddle/phi/kernels/gpu/yolo_box_kernel.cu index b1e1a5da69c25a..5e68e424fc8196 100644 --- a/paddle/phi/kernels/gpu/yolo_box_kernel.cu +++ b/paddle/phi/kernels/gpu/yolo_box_kernel.cu @@ -18,6 +18,7 @@ #include "paddle/phi/backends/gpu/gpu_launch_config.h" #include "paddle/phi/common/memory_utils.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/yolo_box_util.h"