Skip to content

optimize clip_grad_norm_ function - #4915

Merged
tjruwase merged 4 commits into
deepspeedai:masterfrom
mmhab:optimize_clip_grad_norm_
Feb 7, 2024
Merged

optimize clip_grad_norm_ function#4915
tjruwase merged 4 commits into
deepspeedai:masterfrom
mmhab:optimize_clip_grad_norm_

Conversation

@mmhab

@mmhab mmhab commented Jan 8, 2024

Copy link
Copy Markdown
Contributor

Optimize clip_grad_norm_ function by removing .item() calls to reduce wait time for the device on the host.

Optimize clip_grad_norm_ function by removing .item() calls to reduce wait time for the device on the host.
@mmhab
mmhab force-pushed the optimize_clip_grad_norm_ branch from 2865e2d to 9949642 Compare January 21, 2024 09:07
@nelyahu

nelyahu commented Jan 28, 2024

Copy link
Copy Markdown
Contributor

@tjruwase can test workflows be triggered on this PR? thanks

@tjruwase
tjruwase added this pull request to the merge queue Jan 29, 2024
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jan 29, 2024
@nelyahu

nelyahu commented Jan 30, 2024

Copy link
Copy Markdown
Contributor

@tjruwase can this commit be re-added to merge queue? we could not reproduce the merge queue failure locally, probably some other instability.

@tjruwase
tjruwase added this pull request to the merge queue Jan 30, 2024
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jan 30, 2024
@mmhab

mmhab commented Jan 31, 2024

Copy link
Copy Markdown
Contributor Author

Still getting failures that don't reproduce locally.
@tjruwase do you know what is the problem?

@tjruwase
tjruwase added this pull request to the merge queue Jan 31, 2024
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jan 31, 2024
@tjruwase
tjruwase added this pull request to the merge queue Jan 31, 2024
@tjruwase

Copy link
Copy Markdown
Contributor

@tjruwase do you know what is the problem?

Sorry about the inconvenience, we will take a look.

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jan 31, 2024
@tjruwase
tjruwase added this pull request to the merge queue Feb 7, 2024
Merged via the queue into deepspeedai:master with commit 961bc85 Feb 7, 2024
mauryaavinash95 pushed a commit to mauryaavinash95/DeepSpeed that referenced this pull request Feb 17, 2024
Optimize clip_grad_norm_ function by removing .item() calls to reduce
wait time for the device on the host.

Co-authored-by: Olatunji Ruwase <olruwase@microsoft.com>
Co-authored-by: Michael Wyatt <michaelwyatt@microsoft.com>
rraminen pushed a commit to ROCm/DeepSpeed that referenced this pull request May 9, 2024
Optimize clip_grad_norm_ function by removing .item() calls to reduce
wait time for the device on the host.

Co-authored-by: Olatunji Ruwase <olruwase@microsoft.com>
Co-authored-by: Michael Wyatt <michaelwyatt@microsoft.com>
vineethsaivs added a commit to vineethsaivs/DeepSpeed that referenced this pull request Aug 24, 2026
The p-norm over a set of tensors is (sum_i ||g_i||_p ** p) ** (1/p), so combining
per-tensor norms means raising each to norm_type. Three sites take the
1/norm_type root but hardcode the exponent at 2, so they are only correct for
norm_type == 2:

  runtime/utils.py            clip_grad_norm_          .square().sum()
  zero/stage_1_and_2.py       get_grad_norm_direct     .square().sum()
  zero/stage3.py              get_grad_norm_direct     .norm(2), then pow(..., 2)

stage3 is doubly wrong: it takes an L2 norm per tensor whatever norm_type says,
then sums the squares, then takes the 1/norm_type root.

Measured against the p-norm of the concatenated gradients, for grads [3, -4]
and [2]:

              p=1     p=2      p=3
  truth       9.000   5.385    4.626
  clip_grad_ 53.000   5.385    2.894
  zero 1/2   53.000   5.385    2.894
  zero 3     29.000   5.385    3.072

The returned norm is wrong, and so is the clip coefficient derived from it, so
the gradients get scaled by the wrong factor.

clip_grad_norm_ is a regression from deepspeedai#4915, which vectorized the accumulation:
the loop there read `total_norm += param_norm.item()**norm_type` before it, and
the rewrite replaced that with `.square()` while keeping the 1/norm_type root.
The four norm-combining sites that were not touched by that commit all still
raise to norm_type: get_flattened_grad_norm, get_weight_norm,
get_global_norm_of_tensors and get_norm_with_moe_layers.

norm_type is float()'d in every one of these functions, so the default path
becomes pow(2.0), which is bit-identical to square() on float32.

Signed-off-by: Vineeth Sai <vineethsai4444@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants