Skip to content

TRT 11.2: Transpose of a network input folded into a fused MHA operand with the permutation applied to the shape but not to the strides #4826

Open

Description

@vient

Description

Wrong results produces when a Transpose of a network input is absorbed into a fused MHA operand (_gemm_mha_v2 / _gemv_mha_v1): the fold applies the permutation to the operand's shape but not its strides — the operand is addressed as if the buffer were contiguous in the permuted dimension order, instead of permuting the source tensor's strides.

Details (LLM text):
Boundary (every point rebuilt and re-adjudicated, 245 configurations): fires iff the fused MHA kernel is selected — head dim H ≥ 9 (at H ≤ 8 a non-fused path is taken and the transposes fold correctly, max|diff| exactly 0), batch-of-heads G ≥ 2, any sequence length S from 2 to 16384; float16 and bfloat16 fire, float32 does not (fp32 never selects the fused kernel). Softmax and the second MatMul are both required for fusion. Deterministic: 16/16 independent builds (fresh builder, no shared timing cache) fire with bit-identical wrong outputs. No build-flag workaround found: default settings, DIRECT_IO + allowed_formats=LINEAR on every binding, builder optimization levels 0–5, non-strongly-typed network, and TF32-on all produce the same wrong engine and the same operand strides. The only correct-engine formulation found without changing the input's at-rest layout is listing the transposed tensor as an additional graph output, which forces a real transpose layer (at the cost of materializing the tensor).

Environment

TensorRT Version: 11.2.1.2 (TensorRT v110201); reproduced identically via the python wheel tensorrt-cu12==11.2.1.2 and via the apt packages libnvinfer*=11.2.1.2-1+cuda12.9

NVIDIA GPU: RTX PRO 6000 Blackwell Server Edition

NVIDIA Driver Version: 580.126.20

CUDA Version: runtime 12.9 (nvidia-cuda-runtime-cu12, cudaRuntimeGetVersion() = 12090); driver reports 13000

CUDNN Version: not used (no cuDNN in the reproducer stack)

Operating System: Ubuntu 20.04.6 LTS (x86_64)

Python Version (if applicable): 3.8.10

Tensorflow Version (if applicable): n/a

PyTorch Version (if applicable): n/a (reproducer is pure numpy + onnx; no torch involved)

Baremetal or Container (if so, version): Container (privileged Ubuntu 20.04.6 container, host driver 580.126.20); nothing container-specific — the defect is in build-time operand stride assignment

Relevant Files

Attached repro.py, see reproduce commands

Steps To Reproduce

Commands or scripts:

python3 -m pip install -U pip
python3 -m pip install --extra-index-url https://pypi.nvidia.com tensorrt-cu12==11.2.1.2 nvidia-cuda-runtime-cu12 numpy onnx onnxruntime

python3 repro.py --emit repro.onnx
python3 repro.py repro.onnx --ort-check ; echo "exit=$?"

Expected: exit=0, the engine agrees with the numpy/ONNX-semantics reference to float16 rounding (~1e-3), as onnxruntime does.

Actual: exit=1

1. MAIN -- inputs uploaded exactly as the ONNX declares them
   max|diff| vs reference : 0.185562
   |reference| max        : 0.215576
   ratio                  : 0.8608   (float16 rounding would be ~1e-3)
   elements differing     : 32 / 32

2. CONTROL -- the TRANSPOSED arrays' bytes fed into the same [S,G,H] bindings
   max|diff| vs the SAME reference : 0

Have you tried the latest release?: yes, 11.2.1.2 is the latest release

Can this model run on other frameworks? For example run ONNX model with ONNXRuntime (polygraphy run <model.onnx> --onnxrt): yes, onnxruntime 1.19.2 (CPUExecutionProvider) runs the same repro.onnx and agrees with the numpy reference. repro.py checks this.

Activity

  1. added
    Module:Engine Build
    on Aug 10, 2026
  2. kvnloo commented on Sep 28, 2026

    @kvnloo

    The boundary in this report is unusually clean because the wrong answer appears iff the fused MHA path is selected.

    I would use a two-arm representation oracle:

    • original network input + folded Transpose;
    • identical logical tensor materialized by a real Transpose layer before MHA.

    Both represent the same Q/K/V values.

    Then inspect the MHA operand metadata and require that permutation transforms both:

    • shape;
    • source strides.

    The finding that exporting the transposed value as an additional graph output restores correctness is especially strong evidence that materialization fixes the missing-stride problem.

    A minimal regression should therefore compare fused and forced-materialized versions over:

    G=1/2, H=8/9, and FP16/BF16.

    That preserves the exact fusion boundary already found in the issue.

  3. shoumikhin commented on Oct 4, 2026

    @shoumikhin

    Still reproduces on TensorRT 11.3.0.99. Your repro.py fails the same way on a Jetson AGX Thor (sm_110) with CUDA 13.2: main ratio 0.86, and the control with the transposed bytes matches the reference. In the head-dim sweep, H=8 is also wrong here, not only H of 9 and up.

    We also hit it in a real model, where the transposed tensor is not a network input. It is the output of the attention's input projection: torch.nn.MultiheadAttention(512, 8), sequence-first, exported to ONNX, built strongly typed in FP16. The key and value get the same permutations as in your graph ([1,2,0] and [1,0,2]) right before the fused attention. With 100 queries and 602 keys, the output is off by up to 1.05 on values of about 2, while PyTorch in FP16 stays within 0.002. It reproduces on the Thor and on a Jetson Orin Nano (sm_87). In the full model, a robot policy, the actions are off by about 10 instead of 0.2.

    Blocking the fusion fixes it for us, as does keeping that attention in FP32.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Module:Engine Build

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions