Repository navigation
Description
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
- Save the MLIR above as
input.mlir. - 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
github-actions commentedon Oct 29, 2026
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:
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.
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
quiet-maple-47 commentedon Oct 29, 2026
ack