Skip to content

Fix softmax reading the next row when the last 512-wide window is partial - #1054

Open
bananighosh wants to merge 1 commit into
aws-neuron:contribute-2.33from
bananighosh:fix/softmax-partial-second-chunk
Open

bananighosh wants to merge 1 commit into
aws-neuron:contribute-2.33from
bananighosh:fix/softmax-partial-second-chunk

Conversation

@bananighosh

@bananighosh bananighosh commented Nov 25, 2026 •

Copy link
Copy Markdown

Description

Fix torch.softmax returning 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: -inf for 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_data in misc.py) and the compile decompositions (_softmax, _softmax_backward_data in decompositions.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 as softmax512x1x128). 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's init_value to -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 covers row[512:640] plus next_row[0:384]. The even rows come out wrong, and their denominator matches exactly:

x = torch.randn(1024, 640); torch.softmax(x.to("neuron"), -1)
bad rows 512/1024  first [0, 2, 4, 6]
row 0 denominator == sum(exp(row0)) + sum(exp(row1[:384]))   (rel err 5.6e-07)
changing only row 1 changes row 0 (max diff 3.2e-02) and no other row

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:

dtype wrong sizes
fp32 513–1023
bf16 513–880, 1025–1312, 1537–1648
fp16 513–992, 1025–1456, 1537–1936

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

Testing

torch 2.14.0, torch-neuronx 2.14.3.0.282 (its misc.py and decompositions.py match contribute-2.33), neuronx-cc 2.28.

Lint:

$ pre-commit run --files tests/python_ops/test_softmax_partial_chunk.py torch_neuronx/neuron_dynamo_backend/decompositions.py torch_neuronx/python_ops/torch_mlir/ops/misc.py
ruff.....................................................................Passed
ruff-format..............................................................Passed
clang-format.........................................(no files to check)Skipped
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check yaml...........................................(no files to check)Skipped
check for added large files..............................................Passed
pretty format json...................................(no files to check)Skipped
torch-mlir Python bindings match the Torch dialect...(no files to check)Skipped

Ran on Trainium3 (trn3-dev1.48xlarge), with and without the fix:

$ pytest -q -p no:cacheprovider -n 0 <file>
                                       without fix                       with fix
test_softmax_partial_chunk.py (new)    22 failed, 40 passed              62 passed
test_softmax_backward.py               13 passed                         13 passed
test_cross_entropy_loss.py             81 passed                         81 passed
test_scaled_dot_product_attention.py   47 passed, 3 xfailed, 1 xpassed   47 passed, 3 xfailed, 1 xpassed
test_sdpa_math.py                      6 passed                          6 passed

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

  • Added or updated tests for this change
  • I've performed a self-review for my code
  • Ran the tests, or explained under Testing why I could not
  • Updated the corresponding documentation
  • My changes generate no new warnings
  • Said under Description if an AI assistant helped with this change (see AI_POLICY.md)

@bananighosh
bananighosh requested a review from a team November 25, 2026 18:33
@bananighosh
bananighosh requested review from a team as code owners November 25, 2026 18:33
@github-actions github-actions Bot added module:aten-ops Touches aten ops / kernels module:torch-mlir Touches torch-mlir lowerings module:dynamo Touches Dynamo backend labels Nov 25, 2026
…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
bananighosh force-pushed the fix/softmax-partial-second-chunk branch from 847441a to 1f7ba6f Compare December 1, 2026 01:23
@bananighosh bananighosh changed the title Fix softmax returning wrong rows when the last 512-wide chunk is partial Fix softmax reading the next row when the last 512-wide window is partial Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:aten-ops Touches aten ops / kernels module:dynamo Touches Dynamo backend module:torch-mlir Touches torch-mlir lowerings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant