Skip to content

fix: align _input_chunk dtype in FLCE grad_weight addmm under AMP#1283

Open
justinhh4 wants to merge 1 commit into
linkedin:mainfrom
justinhh4:fix/flce-amp-addmm-dtype-mismatch
Open

fix: align _input_chunk dtype in FLCE grad_weight addmm under AMP#1283
justinhh4 wants to merge 1 commit into
linkedin:mainfrom
justinhh4:fix/flce-amp-addmm-dtype-mismatch

Conversation

@justinhh4

Copy link
Copy Markdown
Contributor

Summary

PR #1239 replaced grad_weight += torch.mm(...).float() with torch.addmm(..., out_dtype=torch.float32, out=grad_weight) in the FLCE backward, guarded only on grad_logits_chunk.dtype.

Unlike torch.mm, the addmm out_dtype=/out= overload does not participate in autocast operand casting. So under AMP (fp32 master params, bias=False), grad_logits is the autocast dtype (bf16/fp16) while _input_chunk stays fp32. addmm requires mat1 and mat2 to share a dtype, raising:

RuntimeError: mat1 and mat2 must have the same dtype, but got BFloat16 and Float

bias=True sidesteps the bug because logits + bias promotes grad_logits to fp32. As-is this regresses AMP training of bias=False LM heads (the common LLM case) on torch>=2.8.0.

Details

Align _input_chunk to the grad_logits dtype inside the addmm fast path — a no-op in the intended bf16-native case, exactly what autocast previously did to torch.mm. This preserves #1239's memory optimization and is numerically unchanged on the non-AMP path.

Scope: 1 file, +11/-3, branched directly on top of current main (which already includes #1239).

Testing Done

  • test_amp: the 8 bias=False cases that crashed after perf: optimize grad_weight accumulation with addmm #1239 now pass — 16/16 green.

  • test_fused_linear_cross_entropy.py: 137/137 pass.

  • Numerically verified vs a pure-torch reference (loss / grad_input / grad_weight / grad_bias) across {bf16, fp16, no-AMP} x {bias, nobias}; the non-AMP mm().float() path is bit-for-bit unchanged.

  • Hardware Type: B200

  • run make test to ensure correctness

  • run make checkstyle to ensure code style

  • run make test-convergence to ensure convergence

PR linkedin#1239 replaced `grad_weight += torch.mm(...).float()` with
`torch.addmm(..., out_dtype=fp32, out=grad_weight)`, guarded only on
`grad_logits_chunk.dtype`. Unlike torch.mm, the addmm out_dtype/out=
overload does not participate in autocast operand casting, so under AMP
(fp32 master params, bias=False) grad_logits is the autocast dtype
(bf16/fp16) while _input_chunk stays fp32. addmm requires mat1 and mat2
to share a dtype, raising:

    RuntimeError: mat1 and mat2 must have the same dtype, but got BFloat16 and Float

bias=True sidesteps the bug because `logits + bias` promotes grad_logits
to fp32. This regresses AMP training of bias=False LM heads (the common
LLM case) on torch>=2.8.0.

Align _input_chunk to the grad_logits dtype inside the addmm fast path
(a no-op in the intended bf16-native case, exactly what autocast did to
torch.mm). Preserves linkedin#1239's memory optimization and is numerically
unchanged on the non-AMP path.

Verification (torch 2.12 / B200):
- test_amp: 8 bias=False cases previously crashed at linkedin#1239; now 16/16 pass.
- test_fused_linear_cross_entropy.py: 137/137 pass.
- Numerically verified vs a pure-torch reference (loss / grad_input /
  grad_weight / grad_bias) across {bf16, fp16, no-AMP} x {bias, nobias};
  the non-AMP mm().float() path is bit-for-bit unchanged.
- make checkstyle: clean (ruff check + ruff format).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant