Skip to content

[Codegen][CUDA] linalg.batch_reduce_matmul silently drops a batch contribution #25154

Open

Description

@quiet-maple-47

What happened?

On CUDA, linalg.batch_reduce_matmul produces an incorrect result: the output contains only the first batch's matrix product instead of the sum across both batches. The same computation produces the expected result on llvm-cpu and vulkan-spirv.

Minimal reproducer (input.mlir):

func.func @main(%a: tensor<2x2x2xi32>, %w: tensor<2x2x2xi32>) -> tensor<2x2xi32> {
  %e = tensor.empty() : tensor<2x2xi32>
  %z = arith.constant 0 : i32
  %f = linalg.fill ins(%z : i32) outs(%e : tensor<2x2xi32>) -> tensor<2x2xi32>
  %r = linalg.batch_reduce_matmul
    ins(%a, %w : tensor<2x2x2xi32>, tensor<2x2x2xi32>)
    outs(%f : tensor<2x2xi32>) -> tensor<2x2xi32>
  return %r : tensor<2x2xi32>
}

Expected:

2x2xi32=[-1 13][-7 60]

Actual (CUDA):

2x2xi32=[-7 29][13 52]

The actual output is exactly A[0] @ W[0], whereas the expected output is A[0] @ W[0] + A[1] @ W[1].

Steps to reproduce your issue

  1. Save the MLIR above as input.mlir.
  2. Compile and run on CUDA:
iree-compile input.mlir \
  --iree-hal-target-backends=cuda \
  --iree-cuda-target=sm_89 \
  -o module.vmfb

iree-run-module \
  --device=cuda \
  --module=module.vmfb \
  --function=main \
  '--input=2x2x2xi32=-6 5 3 7 0 2 8 4' \
  '--input=2x2x2xi32=2 1 1 7 -4 5 3 -8'

What component(s) does this issue relate to?

Compiler

Version information

  • IREE 3.13.0 (89ece87b89852100c0a4ac3d515e14842d090967)
  • IREE nightly 3.14.0rc20261024 (5240b82af62635c93016bc22d835544e22bc787c)
  • Linux x86_64, NVIDIA GeForce RTX 4070 (sm_89), driver 595.58.03

Additional context

The loop order of linalg.batch_reduce_matmul is (b, m, n, k), with b and k being reduction loops. However, CUDA codegen assigns the following TileAndFuse configuration:

workgroup = [0, 128, 1, 1]
thread    = [1, 16, 0, 0]
reduction = [0, 0, 0, 1]

This tiles the batch reduction dimension (b) across threads. Compilation also emits:

tiling is not thread safe at axis #0

The likely cause is setContractConfig in KernelConfig.cpp, which assigns tile sizes by loop position, assuming parallel loops precede reduction loops. For this operation, the first loop is instead a reduction.

An equivalent linalg.generic produces the correct result: its loops are reordered to (m, n, b, k) before configuration, placing the tiles on the intended parallel dimensions.

The tiling configuration should use the actual contraction dimensions (e.g., via inferContractionDims) rather than assuming a fixed loop order.

Related: #25016 (multiple trailing reduction loops).


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