optimize clip_grad_norm_ function - #4915
Merged
Merged
Conversation
tjruwase
approved these changes
Jan 19, 2024
Optimize clip_grad_norm_ function by removing .item() calls to reduce wait time for the device on the host.
mmhab
force-pushed
the
optimize_clip_grad_norm_
branch
from
January 21, 2024 09:07
2865e2d to
9949642
Compare
Contributor
|
@tjruwase can test workflows be triggered on this PR? thanks |
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Jan 29, 2024
Contributor
|
@tjruwase can this commit be re-added to merge queue? we could not reproduce the merge queue failure locally, probably some other instability. |
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Jan 30, 2024
Contributor
Author
|
Still getting failures that don't reproduce locally. |
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Jan 31, 2024
Contributor
Sorry about the inconvenience, we will take a look. |
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Jan 31, 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Optimize clip_grad_norm_ function by removing .item() calls to reduce wait time for the device on the host.