Repository navigation
Description
Describe the bug
Take a, an iota comparison matrix built inside the graph: (arange(n)[:, None] <= arange(n)[None, :]).float(). When it is combined element-wise with its own transpose (a - a.T, a + a.T, a * a.T) in one compiled graph, both operands evaluate to the same value, consistent with both being a.T. No error or warning is raised. a or a.T alone is computed correctly.
The StableHLO module that neuronx-cc receives is correct. We decoded the exact bytecode module.mlir passed to neuronx-cc compile (see Logs). So the fault is downstream of the StableHLO: in neuronx-cc, or less likely in the runtime.
We hit this in a rotary-embedding implementation, where it silently produced wrong query and key values.
Here a = [[1, 1], [0, 1]] and a.T = [[1, 0], [1, 1]].
| Expression inside the graph | Expected | Got |
|---|---|---|
a |
a |
correct |
a.T |
a.T |
correct |
a - a |
zeros | correct |
a - (arange[None, :] <= arange[:, None]) (the transposed comparison written directly, no transpose op) |
[[0,1],[-1,0]] |
correct |
M - M.T, M a graph input |
correct | |
a - a.T |
[[0,1],[-1,0]] |
zeros |
a - a.T, n = 4 and n = 128 |
antisymmetric | zeros |
(i < j) - (i < j).T (strict <) |
[[0,1],[-1,0]] |
zeros |
a + a.T |
[[2,1],[1,2]] |
[[2,0],[2,2]] = 2·a.T |
a * a.T |
identity | [[1,0],[1,1]] = a.T |
Every failing case matches both operands evaluating to a.T.
Expected: [[0.0, 1.0], [-1.0, 0.0]] for a - a.T, which is the CPU result and the semantics of the StableHLO module.
Workaround: build the matrix on the host and pass it in as a graph input, or write the transposed comparison directly instead of transposing.
Model Name
No specific model is needed; the minimal reproducer below is a few tensor ops. We found it in our own Qwen3 (Qwen3-0.6B / Qwen3-8B) inference implementation, in the rotary-embedding step. There it silently corrupted the query and key projections, and the first generated token differed from the reference.
Describe the workload type
Inference. A torch.compile(fullgraph=True) graph with the Neuron native backend (libtorch_neuronx_lite.compile.native_backend), on one logical NeuronCore (LNC=2). The minimal reproducer is a single small graph; in the original workload the pattern sat inside an LLM decode/prefill graph with tensor parallelism 4.
Instance Type
trn2.3xlarge (1 Trainium2 chip, LNC=2)
Release version
Neuron SDK 2.33.1. AMI: Deep Learning AMI Neuron PyTorch Inference vLLM 0.29.0.1.2.0 (Ubuntu 24.04) SDK 2.33.1 20261112 (ami-0fc79fb7e05be7de4). neuronx-cc 2.28.7631 was the newest version on pip.repos.neuron.amazonaws.com on 2026-11-16.
$ apt list --installed | grep -i -e neuron
aws-neuronx-collectives/unknown,now 2.35.49.0-354ad3064 amd64 [installed]
aws-neuronx-dkms/unknown,now 2.31.8.0 all [installed]
aws-neuronx-oci-hook/unknown,now 2.19.74.0 amd64 [installed]
aws-neuronx-runtime-lib/unknown,now 2.35.33.0-d2725ccb8 amd64 [installed]
aws-neuronx-tools/unknown,now 2.33.38.0-f874d5618 amd64 [installed]
$ pip list | grep -i -e neuron -e torch -e transformers -e jax
libtorch-neuronx-lite 2.13.0.1.0.4089+71ce3449
neuron-agentic-development 1.4
neuronx-cc 2.28.7631.0+8f7b0cf5
torch 2.13.0
torch_c_dlpack_ext 0.1.5
torch-xla 2.13.0
torchaudio 2.11.0
torchcodec 0.17.0
torchvision 0.28.0
transformers 5.17.0
vllm-neuron 0.29.0.1.2.0
Reproduction Steps
Run on a trn2.3xlarge with the AMI above, inside /opt/aws_neuronx_venv_pytorch_inference_vllm_0_29_0_1_2_0: python repro_min.py. The file is also in the attachment.
import os
os.environ.setdefault("NEURON_RT_VISIBLE_CORES", "0")
os.environ.setdefault("NEURON_RT_ROOT_COMM_ID", "localhost:61236")
import torch
import libtorch_neuronx_lite # noqa: F401
from libtorch_neuronx_lite.compile.native_backend import register
B = register()
dev = torch.device("neuron", 0)
X = torch.randn(4, 2)
ar = lambda x: torch.arange(2, device=x.device)
le = lambda x: (ar(x).reshape(2, 1) <= ar(x).reshape(1, 2)).to(torch.float32)
f = lambda x: le(x) - le(x).permute(1, 0) + 0 * x.sum()
print("got", torch.compile(f, backend=B, fullgraph=True)(X.to(dev)).cpu().tolist())
print("ref", f(X).tolist())Output:
got [[0.0, 0.0], [0.0, 0.0]]
ref [[0.0, 1.0], [-1.0, 0.0]]
repro_variants.py in the attachment runs every case in the table above.
Regression Issue
- Select this option if this issue appears to be a regression.
Possible Solution
Not tested on earlier Neuron SDK releases, so unknown. Leave the regression box unchecked.
Logs/Context/Additional Information
Possible Solution
This hypothesis has not been verified against compiler internals.
The pattern fits a pass that sinks the transpose through convert and compare into the two broadcast_in_dims (where a transpose can be folded into the broadcast dimensions) and rewrites that shared chain in place, although its result has a second user, the subtract's first operand. Both users would then see a.T. That explains:
- why
aalone anda.Talone are correct: each has a single user, so the rewrite is harmless; - why both operands become
a.Tina - a.T,a + a.Tanda * a.T; - why
M - M.Twith a graph-inputMis unaffected: there is no broadcast to sink the transpose into; - why writing the transposed comparison directly is correct: there is no transpose op.
The compiler log is consistent with the rewrite happening after the HLO graph-level stage. The "Post-Partition Histogram after graph level optimizations" in log-neuron-cc.txt still lists 1 transpose, 1 subtract, 1 compare and 2 iota. If so, the faulty rewrite would be in a later Tensorizer pass. Passes reported changed=True include Simplifier, TensorOpTransform, LoopFusion and InsertLocalTransposes. We could not tell which one is responsible.
A likely fix is to rewrite a node in place only when it has a single user, or to clone the chain before sinking the transpose into it.
Logs/Context/Additional Information
The compiler's input is correct. libtorch-neuronx-lite passes neuronx-cc a bytecode module.mlir. We decoded that exact file with the MLIR Python bindings bundled in libtorch-neuronx-lite (libtorch_neuronx_lite._compiler.torch_mlir.ir.Module.parse). It is identical to the text StableHLO dump (input.stablehlo.mlir). The relevant part of the decoded module.mlir:
%6 = stablehlo.dynamic_iota %5, dim = 0 : (tensor<1xi64>) -> tensor<2xi64>
%7 = stablehlo.multiply %6, %c_0 : tensor<2xi64>
%8 = stablehlo.add %7, %c : tensor<2xi64>
%9 = stablehlo.reshape %8 : (tensor<2xi64>) -> tensor<2x1xi64>
%10 = stablehlo.reshape %8 : (tensor<2xi64>) -> tensor<1x2xi64>
%11 = stablehlo.broadcast_in_dim %9, dims = [0, 1] : (tensor<2x1xi64>) -> tensor<2x2xi64>
%12 = stablehlo.broadcast_in_dim %10, dims = [0, 1] : (tensor<1x2xi64>) -> tensor<2x2xi64>
%13 = stablehlo.compare LE, %11, %12, SIGNED : (tensor<2x2xi64>, tensor<2x2xi64>) -> tensor<2x2xi1>
%14 = stablehlo.convert %13 : (tensor<2x2xi1>) -> tensor<2x2xf32>
%15 = stablehlo.transpose %14, dims = [1, 0] : (tensor<2x2xf32>) -> tensor<2x2xf32>
%16 = stablehlo.subtract %14, %15 : tensor<2x2xf32>Compiler command, as invoked by libtorch-neuronx-lite (compiler.txt):
neuronx-cc compile module.mlir --framework XLA --target trn2 --lnc 2 -O2 --logical-nc-config=2 --internal-hlo2tensorizer-options=--experimental-unsafe-fp8e4m3fn-as-fp8e4m3 --model-type transformer --auto-cast=none --logical-nc-config=2 --internal-hlo2tensorizer-options=--experimental-unsafe-fp8e4m3fn-as-fp8e4m3 --output module.neff
Optimization levels. We tried other levels through NEURON_CC_FLAGS=-O1 and -O3. The flag is appended after the hardcoded -O2 (... -O2 -O1 ... -O1 ...). The backend log still shows walrus_driver --optlevel 2, and the NEFF was byte-identical in all three runs. So we could not test another optimization level through this path, and we make no claim about levels other than the default.
How the artifacts were captured: TORCH_NEURONX_PRESERVE_COMPILATION_ARTIFACTS=1 TORCH_NEURONX_DEBUG_DIR=<dir> NEURON_LIBTORCH_DISABLE_COMPILE_CACHE=1 TORCH_NEURONX_NEFF_DISABLE_CACHE=1.
Found by autonomous research at Numaro.tech.
Activity
github-actions commentedon Nov 16, 2026
Hi @unnir, Thank you for filing the issue! We will take a look and get back to you.
jluntamazon commentedon Nov 25, 2026
Thanks for reporting the issue!
We looked into it and will be including a fix in an upcoming release. Please wait for us to give you an update on when it is available