Repository navigation
fix(kernel): align fused entropy backward with native rounding - #389
Merged
kevssim merged 1 commit intoNov 18, 2026
Merged
Conversation
diaochengxi
force-pushed
the
codex/fix-fused-entropy-normalization
branch
from
November 17, 2026 09:12
4e0007a to
bdc34f8
Compare
kevssim
approved these changes
Nov 18, 2026
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.
Why this PR is needed
PR #385 added the Ascend fused lm_head + selective log-softmax path and optional entropy output for
task='fused_lm_rl'. That feature is useful, but its entropy backward does not satisfy Twinkle's strict BF16 gradient-parity requirement on all supported shapes.This is not a test-only issue and it is not a general failure of the forward path. It is a silent numerical defect in the gradient sent back to the transformer backbone:
grad_weightpasses;grad_xcan contain a small number of out-of-tolerance elements;This PR is a focused follow-up because #385 is already merged into
main. It fixes the defect without changing the public API, adding model parameters, or changing the logp-only fast path.Trigger conditions
The defect is reached only when all of the following are true:
task='fused_lm_rl'is used;entropy_coef > 0inGRPOLoss/subclasses;grad_xis needed by the backbone;FP32, non-NPU, Triton-unavailable, non-fused, logp-only, and inference-only paths are unaffected.
Observed failure on current
mainReproduction configuration:
Acceptance threshold:
On current
main, the added regression test fails in the backward comparison:The exact mismatch count depends on the random output-gradient seed. Across the validation runs it ranged from 2 to 28 out of 524288, with a maximum absolute difference of 0.03125 in one configuration. Other outputs in the same comparison pass, which makes the failure easy to miss unless the joint gradient check is run.
Root cause
The fused entropy backward approximated probabilities as:
However, the row-wise value used by the forward reduction and the value used by backward were not normalized from the same
max + denominatorrepresentation. On large logits this creates a small bias inE[logits], which is part of the entropy gradient:That small bias is then amplified by the final input-gradient GEMM. Near cancellation points, the result lands one or more BF16 ULPs away from native PyTorch and exceeds the fixed
atol.There was a second, independent issue: native PyTorch rounds the entropy gradient to BF16 at the scaled-logits/temperature boundary before running the linear backward. The fused path previously did not reproduce that rounding order exactly.
Fix
row_maxandrow_sumfrom the fused forward reduction and reuse them for normalized probabilities:exp(logits - row_max) / row_sum.E[logits]follows the native reference path.mainand passes with the fix.Verification
The fixed branch passes the strict BF16 comparison:
grad_xmismatchesmainThe same strict check also passes at
rows=1024.Test suites run on the A2 validation host:
Performance impact
Fused entropy,
rows=256, hidden=2048, vocab=151936:mainThe fix is approximately
1.43xfaster than the currently merged fused implementation and uses about 32% less peak memory than native PyTorch. It is still slower than native PyTorch in this entropy configuration; this PR does not claim a native-level single-op speedup.End-to-end Qwen3-0.6B,
B=2,S=512, 50 measured steps,entropy_coef=0.01:The fixed branch improves step time over the broken fused path while remaining within a
0.005loss difference from native over the measured run.Merge / maintenance assessment
torch_npuis unavailable.