Repository navigation
[Linear] Produce the first WGRAD in the weight's grad_dtype - #5399
Merged
Merged
Conversation
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
October 14, 2026 22:21
anijain2305
marked this pull request as draft
October 14, 2026 22:22
anijain2305
marked this pull request as ready for review
October 14, 2026 22:24
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 14, 2026
FSDP2 sets the unsharded parameter's grad_dtype to the reduce dtype. With the deferred upcast, it stays fp32 only on a microbatch with gradient sync disabled and no gradient yet, i.e. the first microbatch of gradient accumulation. There `_LinearFunction.backward` computed the WGRAD as a bf16 `mm`, and autograd then cast it to fp32: an extra cast kernel and a full-size bf16 temporary, with the WGRAD rounded to bf16 before the upcast. Without a running gradient, the backward now reads `weight.grad_dtype` and, when it differs from the input dtype, computes the WGRAD with `mm(out_dtype=grad_dtype)` so the GEMM's fp32 accumulator is written directly. The branch stays because CPU has no `mm(out_dtype=)` kernel, even when the dtype matches the inputs; grad_dtype None (FSDP deferring the upcast) keeps the plain `mm`. MXFP8Linear (`output_dtype=ctx.wgrad_dtype`) and HiMidLoLinear (fp32 WGRAD in every path) already produce the WGRAD in the gradient's dtype. Numerics: unchanged without FSDP or with gradient sync on every microbatch. Under FSDP with an fp32 reduce dtype and gradient accumulation, the first microbatch's WGRAD is no longer rounded to bf16, so it is more accurate and not bitwise identical to before. Test: test_first_wgrad_is_produced_in_grad_dtype checks the Function returns fp32 and matches `mm(out_dtype=fp32)` bitwise; it fails without this change. ghstack-source-id: 8b3ca1d Pull-Request: #5399 Goldens: regenerated tests/assets/losses/real_pg/a10g/* with export runs of this commit. Export runs of the parent commit on the same nightly reproduce the old goldens exactly, so every change comes from this commit. Tests without PP change too: ChunkedLossWrapper disables the LM head's gradient sync for all but the last chunk, so its first chunk takes the no-sync first-WGRAD path.
tianyu-l
approved these changes
Oct 15, 2026
Contributor
There was a problem hiding this comment.
should also update fake_pg/ numerics?
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 15, 2026
FSDP2 sets the unsharded parameter's grad_dtype to the reduce dtype. With the deferred upcast, it stays fp32 only on a microbatch with gradient sync disabled and no gradient yet, i.e. the first microbatch of gradient accumulation. There `_LinearFunction.backward` computed the WGRAD as a bf16 `mm`, and autograd then cast it to fp32: an extra cast kernel and a full-size bf16 temporary, with the WGRAD rounded to bf16 before the upcast. Without a running gradient, the backward now reads `weight.grad_dtype` and, when it differs from the input dtype, computes the WGRAD with `mm(out_dtype=grad_dtype)` so the GEMM's fp32 accumulator is written directly. The branch stays because CPU has no `mm(out_dtype=)` kernel, even when the dtype matches the inputs; grad_dtype None (FSDP deferring the upcast) keeps the plain `mm`. MXFP8Linear (`output_dtype=ctx.wgrad_dtype`) and HiMidLoLinear (fp32 WGRAD in every path) already produce the WGRAD in the gradient's dtype. Numerics: unchanged without FSDP or with gradient sync on every microbatch. Under FSDP with an fp32 reduce dtype and gradient accumulation, the first microbatch's WGRAD is no longer rounded to bf16, so it is more accurate and not bitwise identical to before. Test: test_first_wgrad_is_produced_in_grad_dtype checks the Function returns fp32 and matches `mm(out_dtype=fp32)` bitwise; it fails without this change. ghstack-source-id: 48e9221 Pull-Request: #5399 Goldens: regenerated tests/assets/losses/{real_pg,fake_pg}/a10g/* with export runs of this commit. Real PG export runs of the parent commit on the same nightly reproduce the old goldens exactly, so every change comes from this commit. Tests without PP change too: ChunkedLossWrapper disables the LM head's gradient sync for all but the last chunk, so its first chunk takes the no-sync first-WGRAD path.
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 15, 2026
FSDP2 sets the unsharded parameter's grad_dtype to the reduce dtype. With the deferred upcast, it stays fp32 only on a microbatch with gradient sync disabled and no gradient yet, i.e. the first microbatch of gradient accumulation. There `_LinearFunction.backward` computed the WGRAD as a bf16 `mm`, and autograd then cast it to fp32: an extra cast kernel and a full-size bf16 temporary, with the WGRAD rounded to bf16 before the upcast. Without a running gradient, the backward now reads `weight.grad_dtype` and, when it differs from the input dtype, computes the WGRAD with `mm(out_dtype=grad_dtype)` so the GEMM's fp32 accumulator is written directly. The branch stays because CPU has no `mm(out_dtype=)` kernel, even when the dtype matches the inputs; grad_dtype None (FSDP deferring the upcast) keeps the plain `mm`. MXFP8Linear (`output_dtype=ctx.wgrad_dtype`) and HiMidLoLinear (fp32 WGRAD in every path) already produce the WGRAD in the gradient's dtype. Numerics: unchanged without FSDP or with gradient sync on every microbatch. Under FSDP with an fp32 reduce dtype and gradient accumulation, the first microbatch's WGRAD is no longer rounded to bf16, so it is more accurate and not bitwise identical to before. Test: test_first_wgrad_is_produced_in_grad_dtype checks the Function returns fp32 and matches `mm(out_dtype=fp32)` bitwise; it fails without this change. ghstack-source-id: 693f9a8 Pull-Request: #5399 Goldens: regenerated tests/assets/losses/{real_pg,fake_pg}/a10g/* with export runs of this commit. Real PG export runs of the parent commit on the same nightly reproduce the old goldens exactly, so every change comes from this commit. Tests without PP change too: ChunkedLossWrapper disables the LM head's gradient sync for all but the last chunk, so its first chunk takes the no-sync first-WGRAD path.
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 15, 2026
FSDP2 sets the unsharded parameter's grad_dtype to the reduce dtype. With the deferred upcast, it stays fp32 only on a microbatch with gradient sync disabled and no gradient yet, i.e. the first microbatch of gradient accumulation. There `_LinearFunction.backward` computed the WGRAD as a bf16 `mm`, and autograd then cast it to fp32: an extra cast kernel and a full-size bf16 temporary, with the WGRAD rounded to bf16 before the upcast. Without a running gradient, the backward now reads `weight.grad_dtype` and, when it differs from the input dtype, computes the WGRAD with `mm(out_dtype=grad_dtype)` so the GEMM's fp32 accumulator is written directly. The branch stays because CPU has no `mm(out_dtype=)` kernel, even when the dtype matches the inputs; grad_dtype None (FSDP deferring the upcast) keeps the plain `mm`. MXFP8Linear (`output_dtype=ctx.wgrad_dtype`) and HiMidLoLinear (fp32 WGRAD in every path) already produce the WGRAD in the gradient's dtype. Numerics: unchanged without FSDP or with gradient sync on every microbatch. Under FSDP with an fp32 reduce dtype and gradient accumulation, the first microbatch's WGRAD is no longer rounded to bf16, so it is more accurate and not bitwise identical to before. Test: test_first_wgrad_is_produced_in_grad_dtype checks the Function returns fp32 and matches `mm(out_dtype=fp32)` bitwise; it fails without this change. ghstack-source-id: 7b08586 Pull-Request: #5399 Goldens: regenerated tests/assets/losses/{real_pg,fake_pg}/a10g/* with export runs of this commit. Real PG export runs of the parent commit on the same nightly reproduce the old goldens exactly, so every change comes from this commit. Tests without PP change too: ChunkedLossWrapper disables the LM head's gradient sync for all but the last chunk, so its first chunk takes the no-sync first-WGRAD path.
This was referenced Oct 15, 2026
acisseJZhong
added a commit
that referenced
this pull request
Oct 15, 2026
Fix collection of `test_linear_inplace_wgrad_accum.py`, which currently aborts both the single- and multi-GPU unit-test jobs on main. The test added in #5399 imports `_LinearFunction` from the public package, but that private implementation is defined in `torchtitan.models.common.linear.linear` and is not re-exported. Import the private function from its defining module and keep `Linear` on the public import path. Validation: - All five tests collected; four single-GPU cases passed locally. - The multi-GPU case failed its existing addmm-call assertion under local PyTorch 2.14.0. The pre-#5399 version of that test reproduces the same failure; CI uses PyTorch 2.16 nightly. - All non-Pyrefly pre-commit hooks passed. Full-tree Pyrefly reported 54 existing local dependency/type errors; unrelated automatic edits were reverted. Fixes the collection error in [main GPU CI](https://github.com/pytorch/torchtitan/actions/runs/38778179219).
acisseJZhong
added a commit
that referenced
this pull request
Oct 15, 2026
) ROCm model/SFT numerical checks still use MI350X goldens from before #5399. That change produces the first accumulated WGRAD directly in the parameter's FP32 gradient dtype instead of rounding through BF16, and refreshed the A10G goldens only. Refresh the nine MI350X Real-PG golden files used by ten integration configurations. Values come from full-precision TensorBoard scalars in [main ROCm CI](https://github.com/pytorch/torchtitan/actions/runs/38753451439), using `torch==2.16.0.dev20261015+rocm10.0`. Preserve the existing ten-step coverage, parallelism/config metadata, and strict loss/gradient-norm equality checks. The two Llama3 configurations sharing one golden produce identical values. Validation in progress: - Extracted and checked both metrics for all ten steps in every affected configuration. - All nine model configurations match bitwise across two independent main CI runs (`38753451439` and `38778179164`), covering both metrics at every step. - Changed-file pre-commit checks passed. Full-tree pre-commit passed except for 54 existing local Pyrefly dependency/type errors; unrelated automatic edits were reverted. - A controlled ROCm run restores only the pre-#5399 Linear implementation while pinning the failing nightly. This checks whether the old goldens still pass with the old WGRAD math, separating the computation change from the nightly update: https://github.com/pytorch/torchtitan/actions/runs/38787345295 - Full ROCm verification of the updated goldens is queued: https://github.com/pytorch/torchtitan/actions/runs/38787809162 . Keeping this PR in draft until the control and verification checks finish.
acisseJZhong
added a commit
that referenced
this pull request
Oct 20, 2026
## Summary - Refresh the Kimi K3 multimodal per-head DistMuon B200 golden. - Preserve the strict equality comparison and ten-step coverage. Fixes #5449. ## Root cause #5399 intentionally changed the first accumulated weight gradient from a BF16 GEMM result later upcast to FP32 into a GEMM that produces FP32 directly. Kimi uses eight chunked LM-head losses even when outer gradient accumulation is one, so this changes the first WGRAD and the seven following in-place FP32 accumulations. The next optimizer update consequently changes losses from step 2 onward. #5399 refreshed affected A10G goldens and #5423 refreshed MI350X goldens, but the Kimi B200 golden was omitted. The observed B200 sequence is bitwise identical across independent runs and later PyTorch nightlies: - https://github.com/pytorch/torchtitan/actions/runs/38830975496 - https://github.com/pytorch/torchtitan/actions/runs/39182322613 - https://github.com/pytorch/torchtitan/actions/runs/39349362724 Step 1 loss and grad norm remain unchanged, matching the expected first-update boundary. ## Test plan - `SKIP=pyrefly-check pre-commit run --all-files` (pass) - Validate the strict ten-step comparison through `ciflow/b200`. Local Pyrefly is unavailable because the shared venv uses PyTorch 2.14 and older optional APIs than current main.
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):
FSDP2 sets the unsharded parameter's grad_dtype to the reduce dtype. With
the deferred upcast, it stays fp32 only on a microbatch with gradient sync
disabled and no gradient yet, i.e. the first microbatch of gradient
accumulation. There
_LinearFunction.backwardcomputed the WGRAD as a bf16mm, and autograd then cast it to fp32: an extra cast kernel and afull-size bf16 temporary, with the WGRAD rounded to bf16 before the upcast.
Without a running gradient, the backward now reads
weight.grad_dtypeand,when it differs from the input dtype, computes the WGRAD with
mm(out_dtype=grad_dtype)so the GEMM's fp32 accumulator is writtendirectly. The branch stays because CPU has no
mm(out_dtype=)kernel, evenwhen the dtype matches the inputs; grad_dtype None (FSDP deferring the
upcast) keeps the plain
mm.MXFP8Linear (
output_dtype=ctx.wgrad_dtype) and HiMidLoLinear (fp32 WGRADin every path) already produce the WGRAD in the gradient's dtype.
Numerics: unchanged without FSDP or with gradient sync on every
microbatch. Under FSDP with an fp32 reduce dtype and gradient
accumulation, the first microbatch's WGRAD is no longer rounded to bf16,
so it is more accurate and not bitwise identical to before.
Test: test_first_wgrad_is_produced_in_grad_dtype checks the Function
returns fp32 and matches
mm(out_dtype=fp32)bitwise; it fails withoutthis change.