Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions paddle/phi/kernels/cpu/yolo_loss_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ static inline Box<T> GetGtBox(const T* gt, int batch, int max_boxes, int idx) {
return b;
}

static inline int GetEntryIndex(int batch,
int an_idx,
int hw_idx,
int an_num,
int an_stride,
int stride,
int entry) {
return (batch * an_num + an_idx) * an_stride + entry * stride + hw_idx;
static inline int64_t GetEntryIndex(int batch,
int an_idx,
int hw_idx,
int an_num,
int an_stride,
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

}

} // namespace phi
8 changes: 4 additions & 4 deletions paddle/phi/kernels/cpu/yolo_loss_grad_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static void CalcBoxLocationLossGrad(T* input_grad,
Box<T> gt,
std::vector<int> anchors,
int an_idx,
int box_idx,
int64_t box_idx,
int gi,
int gj,
int grid_size,
Expand All @@ -68,7 +68,7 @@ template <typename T>
static inline void CalcLabelLossGrad(T* input_grad,
const T loss,
const T* input,
const int index,
const int64_t index,
const int label,
const int class_num,
const int stride,
Expand Down Expand Up @@ -190,7 +190,7 @@ void YoloLossGradKernel(const Context& dev_ctx,
int gi = static_cast<int>(gt.x * w);
int gj = static_cast<int>(gt.y * h);

int box_idx = GetEntryIndex(
int64_t box_idx = GetEntryIndex(
i, mask_idx, gj * w + gi, mask_num, an_stride, stride, 0);
CalcBoxLocationLossGrad<T>(input_grad_data,
loss_grad_data[i],
Expand All @@ -207,7 +207,7 @@ void YoloLossGradKernel(const Context& dev_ctx,
score);

int label = gt_label_data[i * b + t];
int label_idx = GetEntryIndex(
int64_t label_idx = GetEntryIndex(
i, mask_idx, gj * w + gi, mask_num, an_stride, stride, 5);
CalcLabelLossGrad<T>(input_grad_data,
loss_grad_data[i],
Expand Down
13 changes: 7 additions & 6 deletions paddle/phi/kernels/cpu/yolo_loss_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "paddle/phi/kernels/yolo_loss_kernel.h"

#include <algorithm>
#include <cstdint>
#include <vector>

#include "paddle/phi/backends/cpu/cpu_context.h"
Expand Down Expand Up @@ -56,7 +57,7 @@ static inline Box<T> GetYoloBox(const T* x,
int an_idx,
int grid_size,
int input_size,
int index,
int64_t index,
int stride,
float scale,
float bias) {
Expand Down Expand Up @@ -94,7 +95,7 @@ static void CalcBoxLocationLoss(T* loss,
Box<T> gt,
std::vector<int> anchors,
int an_idx,
int box_idx,
int64_t box_idx,
int gi,
int gj,
int grid_size,
Expand All @@ -116,7 +117,7 @@ static void CalcBoxLocationLoss(T* loss,
template <typename T>
static inline void CalcLabelLoss(T* loss,
const T* input,
const int index,
const int64_t index,
const int label,
const int class_num,
const int stride,
Expand Down Expand Up @@ -253,7 +254,7 @@ void YoloLossKernel(const Context& dev_ctx,
for (int l = 0; l < w; l++) {
// each predict box find a best match gt box, if overlap is bigger
// then ignore_thresh, ignore the objectness loss.
int box_idx =
int64_t box_idx =
GetEntryIndex(i, j, k * w + l, mask_num, an_stride, stride, 0);
Box<T> pred = GetYoloBox(input_data,
anchors,
Expand Down Expand Up @@ -323,7 +324,7 @@ void YoloLossKernel(const Context& dev_ctx,
gt_match_mask_data[i * b + t] = mask_idx;
if (mask_idx >= 0) {
T score = gt_score_data[i * b + t];
int box_idx = GetEntryIndex(
int64_t box_idx = GetEntryIndex(
i, mask_idx, gj * w + gi, mask_num, an_stride, stride, 0);
CalcBoxLocationLoss<T>(loss_data + i,
input_data,
Expand All @@ -342,7 +343,7 @@ void YoloLossKernel(const Context& dev_ctx,
obj_mask_data[obj_idx] = score;

int label = gt_label_data[i * b + t];
int label_idx = GetEntryIndex(
int64_t label_idx = GetEntryIndex(
i, mask_idx, gj * w + gi, mask_num, an_stride, stride, 5);
CalcLabelLoss<T>(loss_data + i,
input_data,
Expand Down