Repository navigation
Fix softmax reading the next row when the last 512-wide window is partial - #1054
Open
bananighosh wants to merge 1 commit into
Open
bananighosh wants to merge 1 commit into
bananighosh wants to merge 1 commit into
Conversation
…tial With --model-type transformer, neuronx-cc replaces softmax with AwsNeuronSoftmax/AwsNeuronSoftmaxBackward, which reduce in 512-wide windows. When the last window is partial, lowering drops its per-element bound and relies on padding after the row (pad_reduce_window). If the layout packs rows back to back, that window reads the start of the next row: for [1024, 640] fp32, every even row's denominator includes next_row[0:384]. Forward and backward, eager and torch.compile. Seen at 1024/1536/2048/4096 rows, not at 1023; on 1024 rows for fp32 513..1023 and bf16/fp16 parts of 513..1936. Pad softmax reductions above 512 up to a multiple of 512 (-inf for the input, 0 for the backward inputs, both exact) in the eager ops and in the compile decompositions (_softmax, _softmax_backward_data), so no window is partial. Tested on trn3-dev1.48xlarge, torch-neuronx 2.14.3.0.282, torch 2.14.0: - test_softmax_partial_chunk.py: 62 passed with the fix, 22 failed without - test_softmax_backward, test_cross_entropy_loss, test_scaled_dot_product_attention, test_sdpa_math: same results with and without the fix
bananighosh
force-pushed
the
fix/softmax-partial-second-chunk
branch
from
December 1, 2026 01:23
847441a to
1f7ba6f
Compare
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.
Description
Fix
torch.softmaxreturning wrong rows on Neuron when the softmax dimension is above 512 and not a multiple of 512. Such reductions are now padded up to the next multiple of 512:-inffor the forward input and 0 for the backward inputs, so the result is exact. This is applied in the eager ops (_softmax,softmax,_softmax_backward_datainmisc.py) and the compile decompositions (_softmax,_softmax_backward_dataindecompositions.py).Related Issue
Fixes #1059
Motivation and Context
With
--model-type transformer, neuronx-cc replaces the softmax with a custom softmax that reduces in 512-wide windows (NativeToCustomSoftmax, tiled assoftmax512x1x128). When the last window is partial, lowering drops the per-element bound and relies on padding after the row:pad_reduce_window(SundaISAInst.py) marks the bound approximate and sets the input'sinit_valueto-inf. If the layout packs rows back to back, the last window reads the start of the next row instead of padding.For
[1024, 640]fp32, the input buffer holds two rows per partition at stride 640, so window 2 of each even row coversrow[512:640]plusnext_row[0:384]. The even rows come out wrong, and their denominator matches exactly:Whether it happens depends on the layout neuronx-cc picks. At n=640: 1024, 1536, 2048 and 4096 rows are wrong; 1023, 1025, 1280 and 3072 rows are correct (1023 rows gets one row per 1024-wide slot). On 1024 rows, wrong sizes were all rows under ~4 KB:
No failures were seen above 2048 (n = 2176 to 5000, 128 to 4096 rows). The fix still pads every partial last window, because the layout is chosen inside the compiler and the dropped bound is what allows the wrong read. The same graph with
NEURON_CC_FLAGS=--model-type=generic(pass off) is correct.Found while validating DeepSeek-V4's sparse attention layer on Trainium3: its softmax spans 128 window + 512 top-k + 1 sink = 641 slots.
Type of Change
Testing
torch 2.14.0, torch-neuronx 2.14.3.0.282 (its
misc.pyanddecompositions.pymatchcontribute-2.33), neuronx-cc 2.28.Lint:
Ran on Trainium3 (trn3-dev1.48xlarge), with and without the fix:
Without the fix, the new tests fail at sizes 640, 1023 (fp32), 1025, 1300 and 1800 (fp16), and at 1024/1536/2048 rows of 640; 1023 rows passes.
Checklist