Skip to content

[Codegen][CUDA] Dynamic batch_reduce_matmul produces nondeterministic wrong results (missing cross-thread reduction) #25155

Open

Description

@quiet-maple-47

What happened?

On CUDA, linalg.batch_reduce_matmul with dynamic M/N/K dimensions produces silently incorrect, nondeterministic results. The same computation is correct on llvm-cpu and vulkan-spirv.

For two 1×4×4 inputs, the expected and observed outputs are:

Expected:  4x4xi32=[-9 14 8 -12][0 8 11 -1][-4 3 17 5][-8 0 10 2]
Run 1:     4x4xi32=[0 0 0 -12][0 0 0 0][0 0 0 5][0 0 0 0]
Run 2:     4x4xi32=[0 0 0 0][0 0 11 0][0 0 0 5][0 0 0 0]

The generated CUDA code distributes the K reduction across threads but appears to omit the cross-thread combination of partial sums.

Steps to reproduce your issue

Save as input.mlir:

func.func @main(%a: tensor<1x?x?xi32>, %w: tensor<1x?x?xi32>) -> tensor<?x?xi32> {
  %c1 = arith.constant 1 : index
  %c2 = arith.constant 2 : index
  %m = tensor.dim %a, %c1 : tensor<1x?x?xi32>
  %n = tensor.dim %w, %c2 : tensor<1x?x?xi32>
  %e = tensor.empty(%m, %n) : tensor<?x?xi32>
  %z = arith.constant 0 : i32
  %f = linalg.fill ins(%z : i32) outs(%e : tensor<?x?xi32>) -> tensor<?x?xi32>
  %r = linalg.batch_reduce_matmul
    ins(%a, %w : tensor<1x?x?xi32>, tensor<1x?x?xi32>)
    outs(%f : tensor<?x?xi32>) -> tensor<?x?xi32>
  return %r : tensor<?x?xi32>
}

Compile:

iree-compile input.mlir \
  --iree-hal-target-backends=cuda \
  --iree-cuda-target=sm_89 \
  -o module.vmfb

Run multiple times with identical inputs:

iree-run-module \
  --device=cuda \
  --module=module.vmfb \
  --function=main \
  '--input=1x4x4xi32=-2 3 -2 2 -2 1 0 3 0 3 3 2 1 3 3 -2' \
  '--input=1x4x4xi32=0 -3 -3 1 -3 2 2 -2 1 -1 3 3 1 0 1 1'

Expected: The matrix product shown above, consistently across runs.

Actual: Mostly zero output, with incorrect values varying between runs.

What component(s) does this issue relate to?

Compiler

Version information

Reproduced with:

  • IREE 3.13.0 (3.13.0rc20261016 @ 89ece87b89852100c0a4ac3d515e14842d090967)
  • IREE nightly 3.14.0rc20261024 (5240b82af62635c93016bc22d835544e22bc787c)

Linux x86_64, NVIDIA GeForce RTX 4070 (sm_89), driver 595.58.03.

Additional context

The dynamic batch_reduce_matmul uses the VectorDistribute pipeline, which distributes the K reduction across threads and subgroups. After LLVMGPUVectorDistributePass, the final reduction remains:

%40 = vector.multi_reduction <add>, %39, %cst_3 [2, 3]
  : vector<1x1x1x1024xi32> to vector<1x1xi32>

There is no gpu.subgroup_reduce or shared-memory combination to collect partial sums from other threads. This appears to leave each thread reducing only its local partial sums, explaining the incorrect output.

In contrast, an equivalent dynamic linalg.matmul (batch = 1) uses the same VectorDistribute pipeline without the leading batch loop. Its accumulator has a vector layout, and the final reduction correctly generates gpu.subgroup_reduce and a shared-memory combination.

Additional validation: Compared against NumPy across M/N/K sizes from 4×4×4 to 64×64×4096 (including K=1025), with i32 and f32, three runs per configuration. Dynamic linalg.matmul was correct in all tested cases; dynamic linalg.batch_reduce_matmul was incorrect for both batch sizes 1 and 2. This suggests the leading batch reduction dimension affects the lowering.

Related: #25154 reports a separate wrong-result bug for static batch_reduce_matmul, caused by setContractConfig tiling the batch reduction loop across threads.


This investigation and write-up were AI-assisted. I've reviewed and verified everything above, and I'm happy to answer questions.

Assisted-by: Claude Opus 5.5

Activity

  1. added
    bug 🐞
    on Oct 29, 2026
  2. github-actions commented on Oct 29, 2026

    @github-actions

    Hello @quiet-maple-47 👋

    Thank you for submitting an issue to IREE! It looks like it's your first one.

    The volume of issues is huge, and triage times may vary. If you need quicker assistance on the issue, consider posting it on IREE Discord and providing short context there.
    Feel free to identify the most frequent contributors to the corresponding part of the codebase via Git history/CODEOWNERS and (gently!) tag them below.


    Action required: acknowledge IREE project policies

    IREE is a Linux Foundation project. All participants are expected to follow the LF Projects Code of Conduct.

    Please note that all contributions to IREE, including GitHub issues, must follow our IREE AI Tool Use Policy. This means:

    • Issue reporters must fully understand the primary issue and the impact of the problem.
      For bug reports, specifics of the use-case, workflow and/or crash scenario usually suffice.
      For improvement suggestions, this usually implies deeper understanding of suboptimal code, IR design choices, assumed performance tradeoffs, etc.
    • Contributors must write the summary of the issue themselves, so that the exact impact and scenario is clear to other participants.
    • Issue authors are expected to review and understand all LLM/AI-assisted segments of the report, if any. E.g.: CLI commands and IR reproducers, model/framework details, lowering pipeline details, assumptions about problematic behaviors of IREE components.
    • Substantial use of LLM/generative AI tools must be disclaimed in the header or footer of the issue description.

    Before the issue can be triaged, we kindly ask you to reply to this message and confirm that you understand and accept the proposed policy. As required, please adjust your initial issue description.


    If you have any questions, feel free to leave a comment here, or ask away on IREE Discord.

    Thank you,
    The IREE Community

  3. quiet-maple-47 commented on Oct 29, 2026

    @quiet-maple-47
    Author

    Hello @quiet-maple-47 👋

    Thank you for submitting an issue to IREE! It looks like it's your first one.

    The volume of issues is huge, and triage times may vary. If you need quicker assistance on the issue, consider posting it on IREE Discord and providing short context there. Feel free to identify the most frequent contributors to the corresponding part of the codebase via Git history/CODEOWNERS and (gently!) tag them below.

    Action required: acknowledge IREE project policies

    IREE is a Linux Foundation project. All participants are expected to follow the LF Projects Code of Conduct.

    Please note that all contributions to IREE, including GitHub issues, must follow our IREE AI Tool Use Policy. This means:

    • Issue reporters must fully understand the primary issue and the impact of the problem.
      For bug reports, specifics of the use-case, workflow and/or crash scenario usually suffice.
      For improvement suggestions, this usually implies deeper understanding of suboptimal code, IR design choices, assumed performance tradeoffs, etc.
    • Contributors must write the summary of the issue themselves, so that the exact impact and scenario is clear to other participants.
    • Issue authors are expected to review and understand all LLM/AI-assisted segments of the report, if any. E.g.: CLI commands and IR reproducers, model/framework details, lowering pipeline details, assumptions about problematic behaviors of IREE components.
    • Substantial use of LLM/generative AI tools must be disclaimed in the header or footer of the issue description.

    Before the issue can be triaged, we kindly ask you to reply to this message and confirm that you understand and accept the proposed policy. As required, please adjust your initial issue description.

    If you have any questions, feel free to leave a comment here, or ask away on IREE Discord.

    Thank you, The IREE Community

    ack

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

    bug 🐞

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions