Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
remove multiply_grads
Browse files Browse the repository at this point in the history
  • Loading branch information
zheyuye committed Jul 9, 2020
1 parent 007f07e commit e4fba39
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 38 deletions.
6 changes: 3 additions & 3 deletions scripts/question_answering/run_squad.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from gluonnlp.models import get_backbone
from gluonnlp.utils.misc import grouper, repeat, set_seed, parse_ctx, logging_config, count_parameters
from gluonnlp.initializer import TruncNorm
from gluonnlp.utils.parameter import clip_grad_global_norm, multiply_grads
from gluonnlp.utils.parameter import clip_grad_global_norm, grad_global_norm

mx.npx.set_np()

Expand Down Expand Up @@ -577,9 +577,9 @@ def train(args):
params, args.max_grad_norm * num_samples_per_update / loss_denom)
total_norm = total_norm / (num_samples_per_update / loss_denom)
else:
total_norm, is_finite = multiply_grads(params, loss_denom / num_samples_per_update)
total_norm = grad_global_norm(parameters)

trainer.update(num_samples_per_update / loss_denom, ignore_stale_grad=True)
trainer.update(num_samples_per_update / loss_denom)
if args.num_accumulated != 1:
# set grad to zero for gradient accumulation
qa_net.collect_params().zero_grad()
Expand Down
36 changes: 1 addition & 35 deletions src/gluonnlp/utils/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.
"""Utility functions for trainer and parameters."""
__all__ = ['grad_global_norm', 'clip_grad_global_norm', 'multiply_grads']
__all__ = ['grad_global_norm', 'clip_grad_global_norm']


import warnings
Expand Down Expand Up @@ -152,37 +152,3 @@ def clip_grad_global_norm(parameters: Iterable[Parameter],
for arr in p.list_grad():
arr *= scale
return total_norm, ratio, is_finite


def multiply_grads(parameters: Iterable[Parameter],
scale: float,
check_isfinite: bool = True) -> Tuple[float]:
"""
Multiplies grads by a constant scale
Parameters
----------
parameters
The list of parameters to calculate the norm
scale
The normalize multiplier to normalize the gradient
Returns
-------
total_norm
The total norm
is_finite
Whether the total norm is finite
"""
total_norm = grad_global_norm(parameters)
is_finite = bool(np.isfinite(total_norm))
if check_isfinite and not is_finite:
warnings.warn(
UserWarning('nan or inf is detected. Clipping results will be undefined.'
' Thus, skip clipping'),
stacklevel=2)
return total_norm, is_finite
for p in parameters:
if p.grad_req != 'null':
for arr in p.list_grad():
arr *= scale
return total_norm, is_finite

0 comments on commit e4fba39

Please sign in to comment.