cutlass icon indicating copy to clipboard operation
cutlass copied to clipboard

[BUG] CuTe DSL: range() with a runtime step fails to compile with Int64 bounds

Open ruiling-smartbear opened this issue 3 weeks ago • 0 comments

Which component has the problem? CuTe DSL

Describe the bug

A range() or cutlass.range() loop with a runtime step does not compile when any bound is Int64, e.g. a grid-stride loop over an Int64 size. It fails even when all three bounds are Int64.

Steps/Code to reproduce bug

import torch
import cutlass.cute as cute
from cutlass import Int64
from cutlass.cute.runtime import from_dlpack

@cute.kernel
def k(x: cute.Tensor, n: Int64):
    tidx, _, _ = cute.arch.thread_idx()
    bidx, _, _ = cute.arch.block_idx()
    bdim, _, _ = cute.arch.block_dim()
    gdim, _, _ = cute.arch.grid_dim()
    for i in range(bidx * bdim + tidx, n, gdim * bdim):
        x[i] = x[i] + 1.0

@cute.jit
def run(x: cute.Tensor, n: Int64):
    k(x, n).launch(grid=[4, 1, 1], block=[32, 1, 1])

x = torch.zeros(1000, device="cuda")
run(from_dlpack(x), Int64(1000))
error[TYPE_CONDITIONAL_BRANCH_MISMATCH]: The two branches of this conditional expression produce different types. Both branches must produce the same type.
  --> repro.py:12:20
   |
12 |     for i in range(bidx * bdim + tidx, n, gdim * bdim):

Expected behavior

The loop compiles, with the bounds promoted to a common type as for a constant step.

Environment details

nvidia-cutlass-dsl 4.8.1 (latest on PyPI), torch 2.9.0, Python 3.13, Cloud (Modal), A10G (sm_86). The code involved is unchanged on main (b2f249a).

Additional context

Because the step may be negative, _handle_negative_step in base_dsl/ast_preprocessor.py rewrites the loop to select between start and stop, and between start + stop and 0. Those selects run before the bounds are promoted to a common type, and the literal 0 is Int32. I have a fix with a test and will open a PR.

ruiling-smartbear avatar Nov 09 '26 03:11 ruiling-smartbear