Skip to content

fix type error of std::pow in sigmoid_focal_loss_op.cu and sigmoid_focal_loss_op.h #18152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions paddle/fluid/operators/detection/sigmoid_focal_loss_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ __global__ void GPUSigmoidFocalLossForward(const T *x_data,
T p = 1. / (1. + real_exp(-x));

// (1 - p)**gamma * log(p)
T term_pos =
std::pow((1. - p), gamma) * real_log(p > FLT_MIN ? p : FLT_MIN);
T term_pos = std::pow(static_cast<T>(1. - p), gamma) *
real_log(p > FLT_MIN ? p : FLT_MIN);
// p**gamma * log(1 - p)
T term_neg =
std::pow(p, gamma) *
Expand Down Expand Up @@ -97,7 +97,7 @@ __global__ void GPUSigmoidFocalLossBackward(
T p = 1. / (1. + real_exp(-x));

// (1-p)**g * (1 - p - g*p*log(p))
T term_pos = std::pow((1. - p), gamma) *
T term_pos = std::pow(static_cast<T>(1. - p), gamma) *
(1. - p - (p * gamma * real_log(p > FLT_MIN ? p : FLT_MIN)));
// (p**g) * (g*(1-p)*log(1-p) - p)
T term_neg =
Expand Down
10 changes: 5 additions & 5 deletions paddle/fluid/operators/detection/sigmoid_focal_loss_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ class SigmoidFocalLossKernel : public framework::OpKernel<T> {
T p = 1. / (1. + std::exp(-x));

// (1 - p)**gamma * log(p) where
T term_pos =
std::pow((1. - p), gamma) * std::log(p > FLT_MIN ? p : FLT_MIN);
T term_pos = std::pow(static_cast<T>(1. - p), gamma) *
std::log(p > FLT_MIN ? p : FLT_MIN);
// p**gamma * log(1 - p)
float term_neg =
T term_neg =
std::pow(p, gamma) *
(-1. * x * (x >= 0) - std::log(1. + std::exp(x - 2. * x * (x >= 0))));

out_data[idx] = 0.0;
out_data[idx] += -c_pos * term_pos * s_pos;
out_data[idx] += -c_neg * term_neg * s_neg;
Expand Down Expand Up @@ -107,15 +108,14 @@ class SigmoidFocalLossGradKernel : public framework::OpKernel<T> {
T p = 1. / (1. + std::exp(-x));

// (1-p)**g * (1 - p - g*p*log(p))
T term_pos = std::pow((1. - p), gamma) *
T term_pos = std::pow(static_cast<T>(1. - p), gamma) *
(1. - p - (p * gamma * std::log(p > FLT_MIN ? p : FLT_MIN)));
// (p**g) * (g*(1-p)*log(1-p) - p)
T term_neg = std::pow(p, gamma) *
((-1. * x * (x >= 0) -
std::log(1. + std::exp(x - 2. * x * (x >= 0)))) *
(1. - p) * gamma -
p);

dx_data[idx] = 0.0;
dx_data[idx] += -c_pos * s_pos * term_pos;
dx_data[idx] += -c_neg * s_neg * term_neg;
Expand Down