Fix softmax StableHLO lowering to use real reductions (#467)#476
Merged
michalharakal merged 2 commits intodevelopfrom Apr 13, 2026
Merged
Fix softmax StableHLO lowering to use real reductions (#467)#476michalharakal merged 2 commits intodevelopfrom
michalharakal merged 2 commits intodevelopfrom
Conversation
Extends testSoftmaxOperation to assert that the converter must not emit hardcoded dense<0.0> / dense<1.0> placeholder constants at the output shape in place of the max(x) and sum(exp(...)) terms, and must invoke real reductions plus a broadcast_in_dim back to the input shape. Red against current ActivationOperationsConverter. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replaces the dense<0.0>/dense<1.0> placeholder constants with custom_call @reduce_max and @reduce_sum (matching the codebase's existing reduction-converter style) and broadcasts the reduced values back to the input shape via stablehlo.broadcast_in_dim before subtract / divide. Handles negative axis correctly. Branch is parked pending P0 roadmap work (quant-in-IR + backend-api extraction) — see issue #467 for context. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.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.
Closes #467.
Summary
Unparks the P1 softmax fix that was written during the initial NPU roadmap scoping and held while P0 landed. With P0-1 step 2 (#475) and P0-2 step 2 (#474) now merged, it's time to address the first correctness bug surfaced by the NPU audit.
`ActivationOperationsConverter.convertSoftmax` currently emits numerically wrong MLIR: it hardcodes placeholder constants in place of the `max(x)` and `sum(exp(...))` terms instead of invoking real reductions.
```mlir
%maxValue = stablehlo.constant dense<0.0> : tensor<2x3xf32> // fake max
%shifted = stablehlo.subtract %input, %maxValue
%exp = stablehlo.exponential %shifted
%sumValue = stablehlo.constant dense<1.0> : tensor<2x3xf32> // fake sum
%result = stablehlo.divide %exp, %sumValue
```
Every softmax output from the exporter is therefore mathematically incorrect, and any transformer model exported through `StableHloConverter` with a softmax — which is every transformer — is unusable downstream (IREE, MLIR tools, NPU compile path).
Two commits
1. Failing test
Extends `ActivationOperationsConverterTest.testSoftmaxOperation` to assert that:
Red against pre-fix `StableHloConverter`.
2. The fix
Rewrites `convertSoftmax` to emit the correct lowering:
```mlir
%max = stablehlo.custom_call @reduce_max(%input) {dimensions = [axis], keepdim = false} : tensor
%maxB = stablehlo.broadcast_in_dim %max, dims = [non-axis dims] : (tensor) -> tensor
%shift = stablehlo.subtract %input, %maxB : tensor
%exp = stablehlo.exponential %shift : tensor
%sum = stablehlo.custom_call @reduce_sum(%exp) {dimensions = [axis], keepdim = false} : tensor
%sumB = stablehlo.broadcast_in_dim %sum, dims = [non-axis dims] : (tensor) -> tensor
%out = stablehlo.divide %exp, %sumB : tensor
```
Handles negative-axis normalization against rank correctly, and uses `stablehlo.custom_call @reduce_max` / `@reduce_sum` to match the existing reduction-converter style (`ReductionOperationsConverter` emits `custom_call @reduce_sum` today). Migrating every reduction to proper `stablehlo.reduce` regions is a separate, larger refactor that's deliberately out of scope.
Test plan
Out of scope
🤖 Generated with Claude Code