Skip to content

Conversation

@zrr1999
Copy link
Member

@zrr1999 zrr1999 commented Jul 8, 2025

PR Category

Operator Mechanism

PR Types

Bug fixes

Description

非法配置已根据文档修改为以下内容

paddle.vision.ops.yolo_loss(Tensor([2863312, 30, 5, 5],"float64"), gt_box=Tensor([2863312, 5, 4],"float64"), gt_label=Tensor([2863312, 5],"int32"), anchors=list[10,13,16,30,33,23,30,61,62,45,59,119,116,90,156,198,373,326,], anchor_mask=list[0,1,2,], class_num=5, ignore_thresh=0.7, downsample_ratio=32, gt_score=None, use_label_smooth=True, scale_x_y=1.0, ) paddle.vision.ops.yolo_loss(Tensor([2863312, 30, 5, 5],"float64"), gt_box=Tensor([2863312, 5, 4],"float64"), gt_label=Tensor([2863312, 5],"int32"), anchors=list[10,13,16,30,33,23,30,61,62,45,59,119,116,90,156,198,373,326,], anchor_mask=list[0,1,2,], class_num=5, ignore_thresh=0.7, downsample_ratio=32, gt_score=Tensor([2863312, 5],"float64"), use_label_smooth=False, scale_x_y=1.0, ) paddle.vision.ops.yolo_loss(Tensor([2863312, 30, 5, 5],"float64"), gt_box=Tensor([2863312, 5, 4],"float64"), gt_label=Tensor([2863312, 5],"int32"), anchors=list[10,13,16,30,33,23,30,61,62,45,59,119,116,90,156,198,373,326,], anchor_mask=list[0,1,2,], class_num=5, ignore_thresh=0.7, downsample_ratio=32, gt_score=Tensor([2863312, 5],"float64"), use_label_smooth=True, scale_x_y=1.0, ) paddle.vision.ops.yolo_loss(Tensor([4793491, 14, 8, 8],"float32"), gt_box=Tensor([4793491, 10, 4],"float32"), gt_label=Tensor([4793491, 10],"int32"), anchors=list[10,13,16,30,], anchor_mask=list[0,1,], class_num=2, ignore_thresh=0.7, downsample_ratio=8, use_label_smooth=True, scale_x_y=1.0, ) paddle.vision.ops.yolo_loss(x=Tensor([11665, 255, 38, 38],"float32"), gt_box=Tensor([11665, 1, 4],"float32"), gt_label=Tensor([11665, 1],"int32"), gt_score=Tensor([11665, 1],"float32"), anchors=list[10,13,16,30,33,23,30,61,62,45,59,119,116,90,156,198,373,326,], anchor_mask=list[3,4,5,], class_num=80, ignore_thresh=0.7, downsample_ratio=16, use_label_smooth=True, ) paddle.vision.ops.yolo_loss(x=Tensor([2917, 255, 76, 76],"float32"), gt_box=Tensor([11665, 1, 4],"float32"), gt_label=Tensor([11665, 1],"int32"), gt_score=Tensor([11665, 1],"float32"), anchors=list[10,13,16,30,33,23,30,61,62,45,59,119,116,90,156,198,373,326,], anchor_mask=list[0,1,2,], class_num=80, ignore_thresh=0.7, downsample_ratio=8, use_label_smooth=True, )

问题出现在 GetEntryIndex 返回值的溢出,改为int64并修改其他对应位置即可
image

Pcard-67164

@paddle-bot
Copy link

paddle-bot bot commented Jul 8, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@zrr1999 zrr1999 requested review from Copilot and wanghuancoder July 8, 2025 07:16
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes integer overflow in the YOLO loss kernels by switching index calculations from 32-bit to 64-bit integers for very large tensors.

  • Changed GetEntryIndex return type and computation to int64_t.
  • Updated all kernel and gradient locals (index, box_idx, label_idx) to int64_t.
  • Added <cstdint> include where needed.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
paddle/phi/kernels/cpu/yolo_loss_kernel.cc Switched index vars to int64_t and included <cstdint>
paddle/phi/kernels/cpu/yolo_loss_grad_kernel.cc Updated grad kernel index vars to int64_t
paddle/phi/kernels/cpu/yolo_loss_functor.h Changed GetEntryIndex signature/return to int64_t and logic
Comments suppressed due to low confidence (2)

paddle/phi/kernels/cpu/yolo_loss_kernel.cc:257

  • [nitpick] Consider adding a unit test with tensors large enough (e.g. >2^31 elements) to validate that the int64_t indexing fully prevents overflow in real scenarios.
 int64_t box_idx = 

paddle/phi/kernels/cpu/yolo_loss_functor.h:39

  • This header introduces int64_t but does not include <cstdint>. Add #include <cstdint> at the top to ensure int64_t is defined in all translation units.
static inline int64_t GetEntryIndex(int batch, 
int stride,
int entry) {
return (static_cast<int64_t>(batch) * an_num + an_idx) * an_stride +
entry * stride + hw_idx;
Copy link

Copilot AI Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expression entry * stride is evaluated as 32-bit int before being promoted to int64_t, which can still overflow. Cast one operand to int64_t (e.g. static_cast<int64_t>(entry) * stride) so the multiplication occurs in 64-bit.

Suggested change
entry * stride + hw_idx;
static_cast<int64_t>(entry) * stride + hw_idx;
Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码中entry 取值只有5或0,stride 值较小,所以不需要int64

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (develop@6026060). Learn more about missing BASE report.

Additional details and impacted files
@@ Coverage Diff @@ ## develop #73895 +/- ## =========================================== Coverage ? 100.00% =========================================== Files ? 3 Lines ? 7 Branches ? 0 =========================================== Hits ? 7 Misses ? 0 Partials ? 0 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Copy link
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 新的case麻烦提个PR同步到PaddleAPITest。case的numel需要超过int32上限

@zrr1999
Copy link
Member Author

zrr1999 commented Jul 9, 2025

LGTM 新的case麻烦提个PR同步到PaddleAPITest。case的numel需要超过int32上限

好的,已在PFCCLab/PaddleAPITest#358 添加,第一个case,2863312305*5-2^31=352 满足numel超过int32上限

@zrr1999 zrr1999 merged commit bc36adc into PaddlePaddle:develop Jul 9, 2025
112 of 123 checks passed
@zrr1999 zrr1999 deleted the big-tensor/yolo_loss branch July 9, 2025 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants