Repository navigation
Description
Describe the bug
The closure form of reduce in the cuTile DSL, cutile::core::reduce(operand, dim, identity, f), does not lower its dim or identity arguments. compile_reduce_op in cutile-rs/cutile-compiler/src/compiler/compile_cuda_tile_op.rs hardcodes the dim attribute to 0 and the identities attribute to zero of the element type, and it sets the result IR type to a rank-0 tile of the element type regardless of the operand's shape. The declared Rust signature compounds this: it returns Tile<E, S>, the operand's own shape, which matches neither the documented "reduce along dim" semantics nor the emitted IR.
The reduce_min / reduce_max / reduce_sum / reduce_prod intrinsics are unaffected; they lower dim and a correct identity through their own path.
Reproduction
#[cutile::entry()]
fn reduce2d_kernel<const N: i32, const K: i32>(
input: &mut Tensor<f32, { [N, K] }>,
out: &mut Tensor<f32, { [1] }>,
) {
let tile: Tile<f32, { [N, K] }> = input.load_tile(const_shape![N, K], [0i32, 0i32]);
let r = reduce(tile, 1i32, f32::NEG_INFINITY, |acc, x| max(acc, x));
let s: Tile<f32, { [1] }> = r.reshape(const_shape![1]);
out.store(s);
}Compiled to Tile IR at N = 8, K = 4, the reduce comes out as:
%43 = reduce %38 dim=0 identities=[0] : tile<8x4xf32> -> tile<f32>
The requested dim was 1 and the identity was f32::NEG_INFINITY; both were replaced with 0, and the result type is a scalar tile rather than tile<8xf32>.
Impact
Every in-tree caller reduces a 1D tile along dim 0, where the hardcoded attribute happens to be correct, so nothing in the repository miscompiles today. Out-of-tree code that reduces a multi-dimensional tile gets an op whose axis, identity, and result shape do not match the source, with no compile-time signal from the DSL. The identity replacement also applies to 1D reductions (a max with f32::NEG_INFINITY is emitted with identities=[0]), although exact power-of-two tiles may never read the identity lanes.
Related
The scan lowering had the same pattern: scan_sum and scan hardcoded dim=0 and identities=[0], which silently scanned the wrong axis of multi-dimensional tiles (scans preserve shape, so nothing failed loudly). A fix for the scan half, with a compile-level regression test (compile_scan_sum_2d_dim1_test in cutile-rs/cutile/tests/reduce_scan_ops.rs), is being prepared alongside the #1658 rank-by-key kernels.
Possible resolutions
- Give closure
reducea generic result shape like thereduce_*intrinsics (-> Tile<E, R>), lower the realdimandidentity, and compute the result type as the operand shape withdimremoved. This is a breaking change to a public DSL function: current callers bind the result without a type annotation and reshape it afterwards, and would need an annotation once the result shape is generic. - Keep closure
reduce1D-only for now: rejectdim != 0and rank above 1 with a clear compile error, honor the identity argument, and point multi-dimensional reductions at thereduce_*intrinsics. This is cheap and turns the silent mismatch into a diagnostic without an API break. - Deprecate the closure form in favor of the intrinsics.
Option 2 can land first and does not preclude option 1 later.
Observed at 11edcb6b1.
Activity