Skip to content

max_grad_norm is ignored in FP16 training #102

Open

Description

@LvanderGoten

I am currently fighting with a dynamic loss scale that is constantly decreasing due to gradient overflows. Setting max_grad_norm in the JSON config has no effect since it is overidden in deepspeed_light.py https://github.com/microsoft/DeepSpeed/blob/13fd3dca2abb6d6a6af62d46457fff8a1a678a4a/deepspeed/pt/deepspeed_light.py#L407-L408:

I think this modification should be removed.

Kind regards

Activity

  1. changed the title [-]max_grad_norm is ignore in FP16 training[/-] [+]max_grad_norm is ignored in FP16 training[/+] on Feb 24, 2020
  2. LvanderGoten commented on Feb 24, 2020

    @LvanderGoten
    Author

    I just saw that there is a "gradient_clipping" option in the config. However, setting it to 1.0, does not eliminate the gradient overflow messages. Is this expected?

  3. tjruwase commented on Feb 24, 2020

    @tjruwase
    Contributor

    Gradient overflow messages, typically at the beginning of training, are not surprising. But this should eventually stop once the dynamic loss scale has decreased to a point that allows training stability.

    Are you not seeing the overflow messages stop?

  4. LiweiPeng commented on Mar 6, 2020

    @LiweiPeng

    Regarding to the original question, what's the purpose to disable 'max_grad_norm' for fp16?
    if self.fp16_enabled() and 'max_grad_norm' in optimizer_parameters.keys():
    optimizer_parameters['max_grad_norm'] = 0.0

  5. tjruwase commented on Mar 19, 2020

    @tjruwase
    Contributor

    The rationale for this was to support both the old and new ways of specifying gradient clipping. The difference is that in the old way, gradient clipping is specified as max_grad_norm parameter of the fp32 optimizer, while in the new (and more intuitive way IMHO) gradient clipping is handled in the fp16 wrapper optimizer, such as here. In summary, this logic allows the fp16 optimizers in DeepSpeed to handle both old and new gradient clipping styles.

    All that said, we are thinking of ways to improve the interface and avoid this confusion, and we certainly appreciate your input on that.

  6. ebarkhordar commented on Aug 20, 2026

    @ebarkhordar
    Contributor

    The line this report points at is gone, and has been since 2020. Recording it here because the last word in the thread still defends the old behaviour, so the issue reads as unresolved.

    optimizer_parameters['max_grad_norm'] = 0.0 was removed in abe2204d ("Support fp32 grad clipping and fix max_grad_norm confusion", #232, 2020-05-26), three months after this was filed. That commit deleted the assignment and turned the neighbouring warnings.warn into an unconditional raise ValueError, so the fp16-only silent override became a hard error at every precision. The intermediate step was 3ce531c9 (#205, 2020-04-30), which added the warning text while the assignment was still in place. First tag containing abe2204d is v0.2.0 (2020-06-11).

    git blame on the current lines attributes them to those two commits and nothing has put the assignment back at that site, so @LiweiPeng's question about the purpose of disabling max_grad_norm under fp16 was effectively answered by the project deciding not to do it.

    At HEAD 6098f799, clean python:3.11-slim container, torch 2.13.0+cpu, deepspeed installed with pip install -e . from the checkout (deepspeed.__file__ = /src/deepspeed/__init__.py):

    [A: max_grad_norm in optimizer params] ValueError: 'max_grad_norm' is not supported as an
    optimizer parameter, please switch to using the deepspeed parameter 'gradient_clipping'
    see: https://www.deepspeed.ai/docs/config-json/#gradient-clipping for more details
    [B: gradient_clipping] OK -> engine built, gradient_clipping=1.0
    

    Arm A passes {"optimizer": {"type": "Adam", "params": {"lr": 1e-3, "max_grad_norm": 1.0}}}, arm B drops it and sets a top-level "gradient_clipping": 1.0 instead. So the config option @LvanderGoten found on the day he filed this is the supported route, and max_grad_norm now fails loudly rather than being silently zeroed.

    Two things I did not check. I could not run the fp16 arm: fp16 is unsupported on the CPU accelerator and this machine has no GPU, so deepspeed.initialize rejects it before reaching the guard. The check at deepspeed/runtime/engine.py:2060 carries no precision condition, so by inspection it covers fp16 too, but that is read from the source rather than run. And the separate question in the thread, whether gradient_clipping: 1.0 ought to stop the overflow messages, was answered by @tjruwase at the time and I am not reopening it.

    Looks closeable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions