From 8aeee2bf0278e43fd3a38c4e8fe80ff3c080d498 Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Thu, 23 Feb 2023 19:18:31 +0800 Subject: [PATCH] Revert "fix found_inf bug for custom optimizer (#50158)" This reverts commit 64573f9fc7cc5badd1e8b461bd02aa6a43c14be2. --- python/paddle/amp/grad_scaler.py | 26 +++++-------------- .../hybrid_parallel_gradscaler.py | 13 +++------- 2 files changed, 9 insertions(+), 30 deletions(-) diff --git a/python/paddle/amp/grad_scaler.py b/python/paddle/amp/grad_scaler.py index db1ad4d5774..b6a38e0e285 100644 --- a/python/paddle/amp/grad_scaler.py +++ b/python/paddle/amp/grad_scaler.py @@ -228,16 +228,9 @@ class AmpScaler: optimize_ops, params_grads = (None, None) - if hasattr(optimizer, "_set_auxiliary_var"): - optimizer._set_auxiliary_var('found_inf', self._found_inf) - optimize_ops, params_grads = optimizer.minimize(*args, **kwargs) - self._cache_founf_inf = optimizer._get_auxiliary_var('found_inf') - else: - if self._found_inf: - self._cache_founf_inf = True - else: - optimize_ops, params_grads = optimizer.minimize(*args, **kwargs) - self._cache_founf_inf = False + optimizer._set_auxiliary_var('found_inf', self._found_inf) + optimize_ops, params_grads = optimizer.minimize(*args, **kwargs) + self._cache_founf_inf = optimizer._get_auxiliary_var('found_inf') if self._use_dynamic_loss_scaling: # uopdate the scale @@ -778,16 +771,9 @@ class GradScaler(AmpScaler): if optimizer_state["state"] is OptimizerState.INIT: self._unscale(optimizer) - if hasattr(optimizer, "_set_auxiliary_var"): - optimizer._set_auxiliary_var('found_inf', self._found_inf) - optimizer.step() - self._cache_founf_inf = optimizer._get_auxiliary_var('found_inf') - else: - if self._found_inf: - self._cache_founf_inf = True - else: - optimizer.step() - self._cache_founf_inf = False + optimizer._set_auxiliary_var('found_inf', self._found_inf) + optimizer.step() + self._cache_founf_inf = optimizer._get_auxiliary_var('found_inf') optimizer_state["state"] = OptimizerState.STEPPED diff --git a/python/paddle/distributed/fleet/meta_optimizers/dygraph_optimizer/hybrid_parallel_gradscaler.py b/python/paddle/distributed/fleet/meta_optimizers/dygraph_optimizer/hybrid_parallel_gradscaler.py index 4924d523ded..c12843f1065 100644 --- a/python/paddle/distributed/fleet/meta_optimizers/dygraph_optimizer/hybrid_parallel_gradscaler.py +++ b/python/paddle/distributed/fleet/meta_optimizers/dygraph_optimizer/hybrid_parallel_gradscaler.py @@ -41,16 +41,9 @@ class HybridParallelGradScaler: optimize_ops, params_grads = (None, None) - if hasattr(optimizer, "_set_auxiliary_var"): - optimizer._set_auxiliary_var('found_inf', self._found_inf) - optimize_ops, params_grads = optimizer.minimize(*args, **kwargs) - self._cache_founf_inf = optimizer._get_auxiliary_var('found_inf') - else: - if self._found_inf: - self._cache_founf_inf = True - else: - optimize_ops, params_grads = optimizer.minimize(*args, **kwargs) - self._cache_founf_inf = False + optimizer._set_auxiliary_var('found_inf', self._found_inf) + optimize_ops, params_grads = optimizer.minimize(*args, **kwargs) + self._cache_founf_inf = optimizer._get_auxiliary_var('found_inf') if self._use_dynamic_loss_scaling: self._update() -- GitLab