fix(clip_grads): handle empty grads_for_norm in inf-norm and p-norm paths#5530
Open
Mattral wants to merge 1 commit into
Open
fix(clip_grads): handle empty grads_for_norm in inf-norm and p-norm paths#5530Mattral wants to merge 1 commit into
Mattral wants to merge 1 commit into
Conversation
Contributor
|
This PR has been automatically converted to draft because all PRs must start as drafts. When you are ready for review, click Ready for Review to begin the review process. This will:
See the contribution guide for more details. |
get_grad_norm_fp32 crashes when grads_for_norm is empty: - inf-norm path (line 95): max() over an empty generator raises ValueError. Fix: pass default=torch.tensor(0.0) to max(). - generic p-norm path (lines 127-129): total_norm stays as a Python float after the loop body never executes, then torch.distributed.all_reduce receives a float instead of a Tensor, raising TypeError. Fix: initialise total_norm as a zero CUDA tensor before the loop, mirroring the existing L2 path pattern. The L2 path (norm_type == 2.0) already handles the empty case correctly via an explicit `if grads_for_norm` guard; this commit applies the same intent to the remaining two branches without changing the non-empty behaviour. Signed-off-by: Mattral <mattralminn@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.
Summary
get_grad_norm_fp32crashes when called with an empty gradient list,which occurs in practice when all parameters on a rank are filtered out
(frozen layers, shared params, TP duplicates). Two out of three norm-type
branches are affected; the L2 branch already has a correct guard.
Changes
megatron/core/optimizer/clip_grads.pyinfnorm (line 95)max()over empty generator →ValueErrordefault=torch.tensor(0.0)tomax()total_normstaysfloat 0.0;all_reducereceives a non-Tensor →TypeErrortotal_norm = torch.zeros(1, dtype=torch.float, device='cuda')before loopNo change to the L2 path or any non-empty-list behaviour.
Testing
tests/unit_tests/optimizer/test_clip_grads.pycovering empty-list calls for all three
norm_typepaths (inf, 2.0,custom p). Tests run without GPU using the local fallback
implementations.
Checklist
git commit -s)mainFixes #5529