perf!: remove the zero-gradient-ratio metric - #3249
Open
samsja wants to merge 1 commit into
Open
Conversation
This was referenced Aug 12, 2026
get_zero_gradient_ratio launched a device-scalar transfer and a count_nonzero kernel per parameter plus two all-reduces and a sync every step, serializing the post-backward path on models with many tensors: on Qwen3-30B-A3B (8xH200, seq 8K) it cost 1.4 s of a 4.6 s step. The metric also read 1.0 on every validation run, so it was likely not measuring what it intended on FSDP2 DTensor gradients. Removes the metric, its trainer call sites, and the Prometheus gauge. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
samsja
force-pushed
the
perf/batch-zero-grad-scan
branch
from
August 12, 2026 17:36
2105501 to
95261f8
Compare
samsja
marked this pull request as ready for review
August 13, 2026 04:17
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
Removes the
zero_grad_ratiometric entirely:count_zero_gradient_elements/get_zero_gradient_ratio, both trainer call sites, theoptim/zero_grad_ratiolog key, and thetrainer_zero_grad_ratioPrometheus gauge.Why remove it:
zero_grad_ratio = 1.0(all gradients zero) on every validation run — Qwen3-30B-A3B actively training on 8×H200 — so it is likely not reading FSDP2 DTensor gradients correctly.count_nonzerokernel per parameter tensor (thousands on MoE models), two all-reduces, and an.item()sync, every step.Measured cost (Qwen3-30B-A3B, 8×H200, fake data, no offload, paired same-session runs, median of steps 2–5):
An earlier revision of this PR claimed ~1.4 s/step based on a cross-session comparison; that did not replicate in paired runs and is withdrawn — the honest number is ~8% at short sequences on many-tensor models, shrinking into noise as compute grows. The primary motivation is the metric's unreliable output.
🤖 Generated with Claude Code
Note
Low Risk
Observability-only removal with no changes to optimizer, loss, or gradient math; dashboards/alerts referencing
optim/zero_grad_ratioortrainer_zero_grad_ratiowill stop updating.Overview
Removes the
zero_grad_ratioobservability path end-to-end:count_zero_gradient_elements/get_zero_gradient_ratiointrainer/utils.py, the post–grad-clip computation in RL and SFT trainers, theoptim/zero_grad_ratiomonitor key, and thetrainer_zero_grad_ratioPrometheus gauge plusMetricsServer.update(..., zero_grad_ratio=...).Training steps no longer pay the per-parameter gradient scan (Python loop,
count_nonzero, all-reduces, sync) that existed only to populate that metric. Related unused imports (Iterable,nn,DTensor) are dropped with the helper code.Note:
docs/training.mdstill documentsoptim/zero_grad_ratio; that file is unchanged in this diff.Reviewed by Cursor Bugbot for commit 95261f8. Bugbot is set up for automated code reviews on this repo. Configure here.