Skip to content

[Linear] Produce the first WGRAD in the weight's grad_dtype - #5399

Merged
anijain2305 merged 8 commits into
mainfrom
gh/anijain2305/72/head
Oct 15, 2026
Merged

anijain2305 merged 8 commits into
mainfrom
gh/anijain2305/72/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

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.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-poisoned]
[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Oct 14, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 14, 2026
@anijain2305
anijain2305 marked this pull request as draft October 14, 2026 22:22
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 14, 2026
@anijain2305
anijain2305 marked this pull request as ready for review October 14, 2026 22:24
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 14, 2026
[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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@anijain2305
anijain2305 changed the base branch from gh/anijain2305/72/base to main October 15, 2026 14:36
@anijain2305
anijain2305 changed the base branch from main to gh/anijain2305/72/base October 15, 2026 14:38
[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.
@anijain2305
anijain2305 changed the base branch from gh/anijain2305/72/base to main October 15, 2026 14:39
@anijain2305
anijain2305 merged commit b1be9ef into main 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants