Skip to content
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

[Phi] fix api sigmoid_focal_loss to final state #45207

Merged
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
59 changes: 44 additions & 15 deletions python/paddle/nn/functional/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -2610,23 +2610,54 @@ def sigmoid_focal_loss(logit,
"Expected one dimension of normalizer in sigmoid_focal_loss but got {}."
.format(normalizer_dims))

if _non_static_mode():
if in_dygraph_mode():
place = _current_expected_place()
one = _C_ops.final_state_full(logit.shape, float(1.0), logit.dtype,
place)
if in_dygraph_mode():
place = _current_expected_place()
one = _C_ops.final_state_full(logit.shape, float(1.0), logit.dtype,
place)

loss = _C_ops.final_state_sigmoid_cross_entropy_with_logits(
logit, label, False, -100)
loss = _C_ops.final_state_sigmoid_cross_entropy_with_logits(
logit, label, False, -100)

elif _in_legacy_dygraph():
one = _varbase_creator(dtype=logit.dtype)
_C_ops.fill_constant(one, 'value', float(1.0), 'force_cpu', False,
'dtype', one.dtype, 'str_value', '1.0',
'shape', logit.shape)
loss = _C_ops.sigmoid_cross_entropy_with_logits(logit, label)
pred = _C_ops.final_state_sigmoid(logit)

p_t = _C_ops.final_state_add(
_C_ops.final_state_multiply(pred, label),
_C_ops.final_state_multiply(_C_ops.final_state_subtract(one, pred),
_C_ops.final_state_subtract(one,
label)))

alpha = fluid.dygraph.base.to_variable([alpha], dtype=loss.dtype)
alpha_t = _C_ops.final_state_add(
_C_ops.final_state_multiply(alpha, label),
_C_ops.final_state_multiply(_C_ops.final_state_subtract(one, alpha),
_C_ops.final_state_subtract(one,
label)))
loss = _C_ops.final_state_multiply(alpha_t, loss)

gamma = fluid.dygraph.base.to_variable([gamma], dtype=loss.dtype)
gamma_t = _C_ops.final_state_pow(_C_ops.elementwise_sub(one, p_t),
gamma)
loss = _C_ops.final_state_multiply(gamma_t, loss)

if normalizer is not None:
loss = _C_ops.final_state_divide(loss, normalizer)

if reduction == "sum":
return _C_ops.final_state_sum(loss, [], None, False)
elif reduction == "mean":
return _C_ops.final_state_mean_all(loss)

return loss

elif _in_legacy_dygraph():
one = _varbase_creator(dtype=logit.dtype)
_C_ops.fill_constant(one, 'value', float(1.0), 'force_cpu', False,
'dtype', one.dtype, 'str_value', '1.0', 'shape',
logit.shape)
loss = _C_ops.sigmoid_cross_entropy_with_logits(logit, label)

pred = _C_ops.sigmoid(logit)

p_t = _C_ops.elementwise_add(
_C_ops.elementwise_mul(pred, label),
_C_ops.elementwise_mul(_C_ops.elementwise_sub(one, pred),
Expand All @@ -2650,8 +2681,6 @@ def sigmoid_focal_loss(logit,
if reduction == "sum":
return _C_ops.reduce_sum(loss, 'reduce_all', True)
elif reduction == "mean":
if in_dygraph_mode():
return _C_ops.final_state_mean_all(loss)
return _C_ops.mean(loss)

return loss
Expand Down