Log gradient clipping norm and frequency #3915
Open
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.
Context
Currently
torch.nn.utils.clip_grad_norm_
andFSDP.clip_grad_norm_
apply the gradient normalization in place but also return the pre-clip gradient norm value, however the value is not capture nor logged anywhere.We can't change the API for all gradient clipping methods since some don't have top level scalar, but we can for gradient norm clipping, the most frequent one we use.
This PR propagates the value outside of the helper and into the algorithm where it can be logged. Since clipping is fairly bursty, we also compute the rolling window over
clipping_frequency_window
samples to provide a more parseable metric.Known caveats
_clipping_history
is not persisted so the metric will change slightly upon resumptionclipping_threshold: 100
Experiments
Couple of example experiments that showcase the functionality with SFT and GRPO are
2025-07-21-debug-gradient-clipping
and2025-07-25-math-rlvr-grpo
respectively