Skip to content

Align weight dtype in DISABLED grad_input path under autocast - #4689

Open
guptaishaan wants to merge 1 commit into
pytorch:mainfrom
guptaishaan:fix-4616
Open

Align weight dtype in DISABLED grad_input path under autocast#4689
guptaishaan wants to merge 1 commit into
pytorch:mainfrom
guptaishaan:fix-4616

Conversation

@guptaishaan

Copy link
Copy Markdown

Fixes #4616

Float8Linear.forward casts only the input to the autocast dtype, so ctx.save_for_backward holds a (bf16 input, fp32 weight) pair. The forward gemm gets away with this because autocast is still active there and torch.mm is autocast-eligible, but backward runs with autocast off, so the ScalingType.DISABLED branch for cast_config_weight_for_grad_input hands the raw fp32 weight to torch.mm against a bf16 grad_output and raises expected mat1 and mat2 to have the same dtype.

Cast the saved weight to the dtype of the grad_output tensor it is multiplied against. No-op without autocast, since both are already fp32. The grad_weight gemm is unaffected, input_hp was already cast to the autocast dtype in forward.

Verified on 1x A40 (sm_86), torch 2.13.0+cu126:

  • Reproduced the crash at float8_linear.py:143, both with the issue's config plus emulate=True and with all six cast configs set to DISABLED. Both pass after the change.
  • New test TestFloat8Linear::test_autocast_backward_scaling_disabled fails before the change with the reported RuntimeError and passes after. It uses the all-DISABLED config so it needs no fp8 hardware, and asserts both gradients match a plain nn.Linear reference bitwise under the same autocast.
  • pytest test/float8/test_base.py 66 passed, 60 skipped. pytest test/float8/ minus the distributed modules, 95 passed, 76 skipped. Ruff lint and format clean.

Not verified: sm_86 cannot run torch._scaled_mm, so the real fp8 path (DYNAMIC forward with DISABLED weight_for_grad_input, emulate=False) was not executed, and neither were the FSDP/DTensor tests. Float8TrainingTensor.dtype is _orig_dtype, so the new .to() is well defined on that path, but it was not run on hardware. Only bf16 autocast was tested, not fp16.

Thanks to @TangSiLiYang for the report and the diagnosis, which pinned the exact line and the correct fix.

Float8Linear.forward casts only the input to the autocast dtype, so
save_for_backward stores a bf16 input next to an fp32 weight. The
forward gemm survives because autocast is still active there and
torch.mm is autocast-eligible. Backward runs with autocast off, so when
cast_config_weight_for_grad_input is ScalingType.DISABLED the raw fp32
weight reaches torch.mm against a bf16 grad_output and it raises
"expected mat1 and mat2 to have the same dtype".

Cast the saved weight to the dtype of the grad_output tensor it is
multiplied against. This is a no-op without autocast, where both are
already fp32. The grad_weight gemm needs no change, input_hp was
already cast to the autocast dtype in forward.

Adds test_autocast_backward_scaling_disabled, which uses an all-DISABLED
config so it runs without sm_89 and checks both gradients against a
plain nn.Linear reference under the same autocast.

Fixes pytorch#4616
@guptaishaan
guptaishaan requested a review from vkuzo as a code owner August 4, 2026 21:02
@pytorch-bot

pytorch-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/4689

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] DISABLED backward path crashes with dtype mismatch under autocast

1 participant