Skip to content

Commit

Permalink
refine found_inf of loss_scaler (PaddlePaddle#37770)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangbo9674 authored and Zjq9409 committed Dec 10, 2021
1 parent cd6b215 commit 507cb48
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions python/paddle/fluid/dygraph/amp/loss_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def __init__(self,
self._use_dynamic_loss_scaling = use_dynamic_loss_scaling

self._found_inf = to_variable(np.array([0]).astype(np.bool))
self._temp_found_inf_fp16 = to_variable(
np.array([0]).astype(np.bool))
self._temp_found_inf_fp32 = to_variable(
np.array([0]).astype(np.bool))
self._scale = to_variable(
np.array([self._init_loss_scaling]).astype(np.float32))
self._cache_founf_inf = None
Expand Down Expand Up @@ -282,17 +286,20 @@ def _unscale(self, optimizer):
) and (param._grad_ivar().dtype == core.VarDesc.VarType.FP32
)
]
temp_found_inf_fp16 = to_variable(np.array([0]).astype(np.bool))
temp_found_inf_fp32 = to_variable(np.array([0]).astype(np.bool))
if len(param_grads_fp16):
_C_ops.check_finite_and_unscale(param_grads_fp16, self._scale,
param_grads_fp16,
temp_found_inf_fp16)
self._temp_found_inf_fp16)
if len(param_grads_fp32):
_C_ops.check_finite_and_unscale(param_grads_fp32, self._scale,
param_grads_fp32,
temp_found_inf_fp32)
self._found_inf = temp_found_inf_fp16 or temp_found_inf_fp32
self._temp_found_inf_fp32)
if len(param_grads_fp16) and len(param_grads_fp32):
self._found_inf = self._temp_found_inf_fp16 or self._temp_found_inf_fp32
elif len(param_grads_fp16):
self._found_inf = self._temp_found_inf_fp16
else:
self._found_inf = self._temp_found_inf_fp32

optimizer_state["state"] = OptimizerState.UNSCALED

Expand Down

0 comments on commit 507cb48

Please sign in to comment.