fix: align _input_chunk dtype in FLCE grad_weight addmm under AMP#1283
Open
justinhh4 wants to merge 1 commit into
Open
fix: align _input_chunk dtype in FLCE grad_weight addmm under AMP#1283justinhh4 wants to merge 1 commit into
justinhh4 wants to merge 1 commit into
Conversation
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>
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.
Summary
PR #1239 replaced
grad_weight += torch.mm(...).float()withtorch.addmm(..., out_dtype=torch.float32, out=grad_weight)in the FLCE backward, guarded only ongrad_logits_chunk.dtype.Unlike
torch.mm, theaddmmout_dtype=/out=overload does not participate in autocast operand casting. So under AMP (fp32 master params,bias=False),grad_logitsis the autocast dtype (bf16/fp16) while_input_chunkstays fp32.addmmrequiresmat1andmat2to share a dtype, raising:bias=Truesidesteps the bug becauselogits + biaspromotesgrad_logitsto fp32. As-is this regresses AMP training ofbias=FalseLM heads (the common LLM case) ontorch>=2.8.0.Details
Align
_input_chunkto thegrad_logitsdtype inside theaddmmfast path — a no-op in the intended bf16-native case, exactly what autocast previously did totorch.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 8bias=Falsecases 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 testto ensure correctnessrun
make checkstyleto ensure code stylerun
make test-convergenceto ensure convergence