Repository navigation
Clip gradients in configure_gradient_clipping instead of training_step - #22
Merged
knightnemo merged 1 commit intoSep 14, 2026
Conversation
training_step runs before backward in Lightning's closure, so the clip scaled the previous step's stale grads (no effect on the update, and with gradient accumulation it shrank earlier micro-batches' grads). Also logs the post-clip norm as gradient_norm_clipped. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018SMDa4kp6u9QUxuvWWcXi1
eshaanpathak
force-pushed
the
upstream-fix-grad-clipping
branch
from
September 14, 2026 12:13
e5ff4dd to
42dbfaf
Compare
Member
|
lgtm. |
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.
Gradient clipping wasn't working :(
The clip call (
clip_grad_norm_) was insidetraining_step, before.backward()runs. Lightning callstraining_step-->backward()--> clip -->optimizer.step(), so at the point this ran,p.gradwas eitherNoneor leftover from the previous step, not the gradient about to be used. Withgradient_accumulation: 1(the default config), clipping did nothing. With accumulation > 1, it scales down the gradient mid-accumulation before later micro-batches even get added in.Moved the clip into
configure_gradient_clipping, which Lightning runs after backward (and after DDP grad sync), once per optimizer step.Checked this against pytorch-lightning 1.9.5 (what's pinned) with a small repro, with and without accumulation. Before the fix, the optimizer's gradient was never capped no matter what
gradient_clip_normwas set to. After the fix it is, every step.Also added a
gradient_norm_clippedlog (post-clip) next to the existinggradient_norm(now correctly the current step's value, not the previous one).All of the HF checkpoints may have been trained without clipping. Since it looks like clipping was enabled at 20K steps, it may have unlikely affected the checkpoints, but you would need to check your training run charts to confirm this!
Here are two charts to visualize the gradient getting clipped at 100 steps to 0.1:
true_gradient_normis the main branch right now before my changes, andgradient_norm_clippedis with my changes in this PR.