Repository navigation
[Linear] In-place WGRAD accumulation for the bf16 Linear - #5250
Merged
Merged
Conversation
[ghstack-poisoned]
[ghstack-poisoned]
This was referenced Oct 9, 2026
anijain2305
added a commit
that referenced
this pull request
Oct 9, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's; autocast is replicated by casting the operands explicitly. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. Loss goldens for FSDP runs with PP or gradient accumulation need regenerating. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: 620cecf Pull-Request: #5250
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 9, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's. It raises under autocast, whose casts its backward does not replicate; DeepSeek-V4's compressor, the only Linears run under autocast, opts out. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. Loss goldens for FSDP runs with PP or gradient accumulation need regenerating. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: e27ba03 Pull-Request: #5250
anijain2305
marked this pull request as ready for review
October 9, 2026 23:02
anijain2305
requested review from
IvanKobzarev,
SherlockNoMad,
aditvenk,
fegin,
sanketpurandare,
tianyu-l,
wconstab and
wwwjn
as code owners
October 9, 2026 23:02
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's. It raises under autocast, whose casts its backward does not replicate; DeepSeek-V4's compressor, the only Linears run under autocast, opts out. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. Loss goldens for FSDP runs with PP or gradient accumulation need regenerating. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: 7220329 Pull-Request: #5250
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's. It raises under autocast, whose casts its backward does not replicate; DeepSeek-V4's compressor, the only Linears run under autocast, opts out. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. Loss goldens for FSDP runs with PP or gradient accumulation need regenerating. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: a1459e5 Pull-Request: #5250
tianyu-l
approved these changes
Oct 10, 2026
| @@ -88,29 +89,18 @@ def log_timer(label: str): | |||
|
|
|||
| def inplace_wgrad_accum_configs( | |||
Contributor
There was a problem hiding this comment.
I imagine this function is becoming not as useful as before
Comment on lines
+110
to
+111
| # The compressor runs these under fp32 autocast, which the in-place | ||
| # WGRAD accumulation path does not support. |
Contributor
There was a problem hiding this comment.
DeepSeek-V4's compressor, the only Linears run under autocast, opts out.
Do we have to use autocast though?
Contributor
Author
There was a problem hiding this comment.
I think we can use HiMidLoLinear. Let me do that in a separate PR though.
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's. It raises under autocast, whose casts its backward does not replicate; DeepSeek-V4's compressor, the only Linears run under autocast, opts out. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. Loss goldens for FSDP runs with PP or gradient accumulation need regenerating. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: d1b364a Pull-Request: #5250
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True; the async TP transform raises for a projection with it on unless disable_inplace_wgrad_accum=True acknowledges turning it off. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's. It raises under autocast, whose casts its backward does not replicate; DeepSeek-V4's compressor, the only Linears run under autocast, opts out. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. Loss goldens for FSDP runs with PP or gradient accumulation need regenerating. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: cb6996f Pull-Request: #5250
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
…n place (#5137) Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.18.0) (oldest at bottom): * #5250 * #5249 * __->__ #5137 ---- - LM-head (wide) backward adds WGRAD into the running fp32 weight.grad with addmm(out=), as MXFP8Linear already does; skips AccumulateGrad's add and a [vocab, D] fp32 temporary on ChunkedLossWrapper chunks / microbatches. - Router (narrow) and fp32-fallback paths unchanged; comments explain why. - Explicit `inplace_wgrad_accum: bool = True` config on HiMidLoLinear and MXFP8Linear (like DistMoeTransform). Raises instead of silently falling back when traced (torch.compile, make_fx) or for a non-leaf weight; HiMidLoLinear rejects num_linears > 1 with it on. GraphTrainer conversion and flavor builders turn it off, and GraphTrainer.Config rejects it on. - Shared helpers in models/common/inplace_wgrad_accum.py (uses_inplace_wgrad_accum, read-only running_grad); each backward clears weight.grad itself next to its in-place add. - Pipeline schedules that split backward into input and weight passes (ZBVZeroBubble, DualPipeV, InterleavedZeroBubble) turn the option off: both passes run under autograd.grad(), where AccumulateGrad never runs, and adding into .grad there dropped the earlier microbatches' gradients (an existing MXFP8 bug). 1F1B, Interleaved1F1B and GPipe keep it. - The in-place add uses the running grad's dtype (fp32 under an FSDP fp32 reduce dtype, bf16 without FSDP), so it needs no dtype check; keep MXFP8's grad_dtype fallback, since FSDP clears grad_dtype during backward. - Qwen3-8B LM head, chunk-2 backward on H100: 11.98 -> 9.19 ms, peak 3.54 -> 1.22 GiB; weight.grad bitwise identical.
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True; the async TP transform raises for a projection with it on unless disable_inplace_wgrad_accum=True acknowledges turning it off. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's. It raises under autocast, whose casts its backward does not replicate; DeepSeek-V4's compressor, the only Linears run under autocast, opts out. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. The real-PG A10G loss goldens are regenerated (torch nightly 2026-10-09; some steps also move from drift on main since the goldens were last updated). Against main on the same nightly, llama3 losses match at step 1 and stay within 7e-5 relative over 10 steps. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: d04b42f Pull-Request: #5250
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.18.0) (oldest at bottom): * #5250 * __->__ #5249 Why: - In-place WGRAD accumulation (#5137, and the bf16 Linear next in this stack) needs the parameter itself in backward, to read and clear its .grad and return a gradient in its shape. _linear only got a flattened view, so implementations re-read self.weight or gave up on stacked weights. - Re-reading self.weight is not free: SimpleFSDP's parametrization all-gathers on every read, and MXFP8Linear also quantizes, so each MXFP8 forward did both twice (the quantize-count test asserted 2). - A stacked weight's flattened view routes its gradient through the view's backward, which rounds HiMidLoLinear's fp32 grad_weight to bf16 (pytorch/pytorch#189633), so HiMidLoLinear rejected stacked weights with inplace_wgrad_accum on. What: - Linear.forward reads weight and bias once and passes them to _linear unflattened ([N, K] or [num_linears, N, K]); implementations flatten them with the static Linear._flatten_weight_and_bias(). Updates the TP, vision, async TP, LoRA and NVFP4 call sites. - MXFP8Linear uses its weight argument (quantize count back to 1). - HiMidLoLinear's Function takes the parameter and flattens inside, so stacked weights accumulate in place and keep an fp32 grad. - No numerics change for unstacked linears.
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True; the async TP transform raises for a projection with it on unless disable_inplace_wgrad_accum=True acknowledges turning it off. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's. It raises under autocast, whose casts its backward does not replicate; DeepSeek-V4's compressor, the only Linears run under autocast, opts out. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. The real-PG A10G loss goldens are regenerated (torch nightly 2026-10-09; some steps also move from drift on main since the goldens were last updated). Against main on the same nightly, llama3 losses match at step 1 and stay within 7e-5 relative over 10 steps. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: d04b42f Pull-Request: #5250
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True; the async TP transform raises for a projection with it on unless disable_inplace_wgrad_accum=True acknowledges turning it off. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's. It raises under autocast, whose casts its backward does not replicate; DeepSeek-V4's compressor, the only Linears run under autocast, opts out. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. All nine real-PG A10G loss goldens are regenerated from this commit (torch nightly 2026-10-10; some also move from drift on main since they were last updated). Against main on the same nightly, llama3 losses match at step 1 and stay within 7e-5 relative over 10 steps. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: e78e916 Pull-Request: #5250
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True; the async TP transform raises for a projection with it on unless disable_inplace_wgrad_accum=True acknowledges turning it off. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's. It raises under autocast, whose casts its backward does not replicate; DeepSeek-V4's compressor, the only Linears run under autocast, opts out. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. All nine real-PG A10G loss goldens are regenerated from this commit (torch nightly 2026-10-10; some also move from drift on main since they were last updated). Against main on the same nightly, llama3 losses match at step 1 and stay within 7e-5 relative over 10 steps. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: e78e916 Pull-Request: #5250
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True; the async TP transform raises for a projection with it on unless disable_inplace_wgrad_accum=True acknowledges turning it off. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's. It raises under autocast, whose casts its backward does not replicate; DeepSeek-V4's compressor, the only Linears run under autocast, opts out. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. All nine real-PG A10G loss goldens are regenerated from this commit (torch nightly 2026-10-10; some also move from drift on main since they were last updated). Against main on the same nightly, llama3 losses match at step 1 and stay within 7e-5 relative over 10 steps. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: 43a12ca Pull-Request: #5250
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True; the async TP transform raises for a projection with it on unless disable_inplace_wgrad_accum=True acknowledges turning it off. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's. It raises under autocast, whose casts its backward does not replicate; DeepSeek-V4's compressor, the only Linears run under autocast, opts out. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. All nine real-PG A10G loss goldens are regenerated from this commit (torch nightly 2026-10-10; some also move from drift on main since they were last updated). Against main on the same nightly, llama3 losses match at step 1 and stay within 7e-5 relative over 10 steps. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: 43a12ca Pull-Request: #5250
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 10, 2026
- Move `inplace_wgrad_accum: bool = True` to Linear.Config; MXFP8Linear and HiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute WGRAD themselves, so they default to False and reject True; the async TP transform raises for a projection with it on unless disable_inplace_wgrad_accum=True acknowledges turning it off. - Linear._linear uses a new autograd Function (_LinearFunction) when in-place accumulation is active, plain F.linear otherwise (tracing, no grad, frozen weight). Without a running grad its gradients are bitwise identical to F.linear's. It raises under autocast, whose casts its backward does not replicate; DeepSeek-V4's compressor, the only Linears run under autocast, opts out. - Numerics change: later microbatches / chunks add the GEMM's fp32 accumulator into the running grad (addmm(out=weight.grad)) instead of rounding each WGRAD to bf16 first. All nine real-PG A10G loss goldens are regenerated from this commit (torch nightly 2026-10-10; some also move from drift on main since they were last updated). Against main on the same nightly, llama3 losses match at step 1 and stay within 7e-5 relative over 10 steps. - MXFP8 converter and LoRA adapters carry the option; GraphTrainer disables it on every Linear.Config, and tests that trace a Linear directly turn it off. ghstack-source-id: a7315a7 Pull-Request: #5250
tianyu-l
added a commit
that referenced
this pull request
Oct 11, 2026
#5250 (in-place WGRAD accumulation for the bf16 Linear) changes gradient accumulation order, which moves SFT grad_norm from the 5th significant digit (step-1 loss is unchanged). Its fake-PG run failed on this golden and the job does not run on main pushes, so every PR since fails it with identical values. Values are the A10G baseline from CI.
tianyu-l
added a commit
that referenced
this pull request
Oct 11, 2026
#5250 changed numerics and regenerated the real-PG goldens but not the fake-PG ones. The fake-PG models suite never ran afterwards because the SFT golden failed first; with that fixed, the stale model goldens surface. Values are the baselines printed by the a10g fake-PG CI run.
pianpwk
added a commit
that referenced
this pull request
Oct 14, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0) (oldest at bottom): * __->__ #5368 #4772 exported its A10G goldens on a base that predated #5250 (in-place bf16 Linear WGRAD accumulation), which itself re-baselined every A10G golden. The final ghstack rebase onto #5250 kept #4772's stale values, so the committed real_pg goldens never matched the landed code: step-1 grad_norm already differs (the WGRAD accumulation change), and the divergence grows from step 2. The PR's real-PG CI only ran the required subset, which excludes these tests; its fake-PG job failed on both. These tests intentionally verify the new batch-wise aux-loss policy (the expert-bias policy is kept only for gpt_oss_pp via _use_expert_bias_load_balancing), so this refreshes values without changing configs. Provenance (torch 2.16.0.dev20261011+cu132, A10G, seed checkpoint per the test definitions): - real_pg: bit-identical across two independent main runs -- job 114675094754 at 8b4a130 and job 115529499679 at 50474ad. - fake_pg: bit-identical across four independent ciflow/fake-pg runs on PRs based on post-#4772 main (runs 38459347990, 38460226133, 38460414111, 38466591432) that do not touch core model code.
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.
Stack from ghstack (oldest at bottom):
inplace_wgrad_accum: bool = Trueto Linear.Config; MXFP8Linear andHiMidLoLinear inherit it. NVFP4Linear and the async TP linears compute
WGRAD themselves, so they default to False and reject True; the async TP
transform raises for a projection with it on unless
disable_inplace_wgrad_accum=True acknowledges turning it off.
accumulation is active, plain F.linear otherwise (tracing, no grad, frozen
weight). Without a running grad its gradients are bitwise identical to
F.linear's. It raises under autocast, whose casts its backward does not
replicate; DeepSeek-V4's compressor, the only Linears run under autocast,
opts out.
accumulator into the running grad (addmm(out=weight.grad)) instead of
rounding each WGRAD to bf16 first. Loss goldens for FSDP runs with PP or
gradient accumulation need regenerating.
on every Linear.Config, and tests that trace a Linear directly turn it off.