Skip to content

fix(fused_linear_jsd): project logits directly in FP32 - #1446

Closed
feiiiiii5 wants to merge 2 commits into
linkedin:mainfrom
feiiiiii5:fix/fused-jsd-fp32-cast
Closed

feiiiiii5 wants to merge 2 commits into
linkedin:mainfrom
feiiiiii5:fix/fused-jsd-fp32-cast

Conversation

@feiiiiii5

Copy link
Copy Markdown

Fixes #1432.

Root cause: forward projected hidden states with low-precision GEMM then cast rounded result to FP32, discarding FP32 accumulator. JSD gradients difference near-equal distributions so rounding cancels catastrophically (4-23% grad error in bf16). Test oracle shared same defect with degenerate rand inputs masking it.

Fix: use torch.mm(..., out_dtype=torch.float32) where supported with explicit FP32 GEMM fallback. Same fix to Ascend and cuTile forwards. Fix oracle + rand to randn.

Tests: py_compile clean, needs GPU CI for numerical verification.

feiiiiii5 and others added 2 commits September 8, 2026 12:26
The forward projected hidden states with a low-precision GEMM and then
cast the rounded result to FP32, discarding the FP32 accumulator cuBLAS
already computed. JSD gradients difference near-equal distributions, so
the rounding cancels catastrophically (4-23% grad error in bf16).

Use torch.mm(..., out_dtype=torch.float32) where supported (torch>=2.8,
CUDA sm_80+, fp16/bf16) to keep the accumulator at no cost, with an
explicit FP32 GEMM fallback elsewhere. Apply the same FP32 projection to
the Ascend and cuTile forwards. Fix the test oracle, which shared the
same cast-after-matmul defect, and switch its degenerate rand inputs to
randn so the bug is no longer masked.

Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
@feiiiiii5

Copy link
Copy Markdown
Author

Closing as duplicate of #1433: same issue (#1432), same fix direction, and the reporter's own PR has priority. One thing #1433 may want as a follow-up: the same round-then-cast pattern exists in the ascend and cutile backends of this kernel, which my branch had covered. Happy to help verify those if useful.

@feiiiiii5 feiiiiii5 closed this Sep 10, 2026
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.

fused_linear_jsd rounds logits to the input dtype before the documented FP32 cast, costing up to 23% gradient error in bf16

1 participant