From 9ec0d0c7b2a16ec5fb7cf39130c1c6dbc4ad2901 Mon Sep 17 00:00:00 2001 From: zky <51477259+zkyue@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:20:56 +0800 Subject: [PATCH 1/2] [CuTeDSL] examples: guard elementwise_apply with its out-of-bounds predicate The kernel computed frgPred but never used it, so tensors whose shape is not a multiple of the tile size were read and written out of bounds. Load and store through predicated cute.copy, and remap the blocks of the coordinate tensor like the data tensors so the predicate is correct for non-square block grids. --- .../kernel/elementwise/elementwise_apply.py | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/examples/python/CuTeDSL/cute/ampere/kernel/elementwise/elementwise_apply.py b/examples/python/CuTeDSL/cute/ampere/kernel/elementwise/elementwise_apply.py index 2d39cbce85..23f9e78c5b 100644 --- a/examples/python/CuTeDSL/cute/ampere/kernel/elementwise/elementwise_apply.py +++ b/examples/python/CuTeDSL/cute/ampere/kernel/elementwise/elementwise_apply.py @@ -123,6 +123,13 @@ def elementwise_apply_kernel( thrC = tidfrgC[thr_crd] # (V) thrCrd = tidfrgCrd[thr_crd] + # `cute.copy` expects tensors of profile ((AtomV, AtomRest), Rest...). The copy atom + # used below moves one element at a time, so prepend a unit mode as AtomV: (V) -> ((1, V)) + thrInputs = [ + cute.group_modes(cute.prepend_ones(t, up_to_rank=2), 0, 2) for t in thrInputs + ] + thrC = cute.group_modes(cute.prepend_ones(thrC, up_to_rank=2), 0, 2) + print("[DSL INFO] Sliced Tensors per thread:") for i in cutlass.range_constexpr(len(thrInputs)): print(f"[DSL INFO] thrInputs{i} = {thrInputs[i].type}") @@ -132,7 +139,7 @@ def elementwise_apply_kernel( ############################################################################### # Compute predicate for out of boundary checks ############################################################################### - frgPred = cute.make_rmem_tensor(thrCrd.shape, cutlass.Boolean) + frgPred = cute.make_rmem_tensor(thrC.shape, cutlass.Boolean) print(f"[DSL INFO] frgPred = {frgPred.type}") for i in cutlass.range_constexpr(cute.size(frgPred)): @@ -145,10 +152,26 @@ def elementwise_apply_kernel( # Load data and compute result ########################################################## + # Predicated copies skip the elements outside of the tensor, so tensors whose shape + # is not a multiple of the tile size are neither read nor written out of bounds. + copy_atom_load = cute.make_copy_atom( + cute.nvgpu.CopyUniversalOp(), mInputs[0].element_type + ) + copy_atom_store = cute.make_copy_atom(cute.nvgpu.CopyUniversalOp(), mC.element_type) + + # Move data to reg address space + frgInputs = [cute.make_fragment_like(t) for t in thrInputs] + for i in cutlass.range_constexpr(len(thrInputs)): + cute.copy(copy_atom_load, thrInputs[i], frgInputs[i], pred=frgPred) + # Load data before use. The compiler will optimize the copy and load # operations to convert some memory ld/st into register uses. - result = op(*[thrInput.load() for thrInput in thrInputs]) - thrC.store(result) + result = op(*[frgInput.load() for frgInput in frgInputs]) + + # Save the results to registers, then copy them back to global memory + frgC = cute.make_fragment_like(thrC) + frgC.store(result) + cute.copy(copy_atom_store, frgC, thrC, pred=frgPred) @cute.jit @@ -254,6 +277,8 @@ def elementwise_apply( idC = cute.make_identity_tensor(result.shape) cC = cute.zipped_divide(idC, tiler=tiler_mn) + # Remap the blocks of the coordinate tensor the same way as the data tensors + cC = cute.composition(cC, (None, remap_block)) print(f"[DSL INFO] coord tensor = {cC}") # Launch the kernel asynchronously From 7da31f2ac993f341d579f4715b473a50dfab6573 Mon Sep 17 00:00:00 2001 From: zky <51477259+zkyue@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:28:02 +0800 Subject: [PATCH 2/2] [CuTeDSL] examples: slice and reshape per-thread tensors in one step --- .../kernel/elementwise/elementwise_apply.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/python/CuTeDSL/cute/ampere/kernel/elementwise/elementwise_apply.py b/examples/python/CuTeDSL/cute/ampere/kernel/elementwise/elementwise_apply.py index 23f9e78c5b..b8f5cc9cb3 100644 --- a/examples/python/CuTeDSL/cute/ampere/kernel/elementwise/elementwise_apply.py +++ b/examples/python/CuTeDSL/cute/ampere/kernel/elementwise/elementwise_apply.py @@ -118,17 +118,15 @@ def elementwise_apply_kernel( ############################################################################### # Slice to local tile of thread ############################################################################### - # vid -> address - thrInputs = [t[thr_crd] for t in tidfrgInputs] # (V) - thrC = tidfrgC[thr_crd] # (V) - thrCrd = tidfrgCrd[thr_crd] - # `cute.copy` expects tensors of profile ((AtomV, AtomRest), Rest...). The copy atom # used below moves one element at a time, so prepend a unit mode as AtomV: (V) -> ((1, V)) - thrInputs = [ - cute.group_modes(cute.prepend_ones(t, up_to_rank=2), 0, 2) for t in thrInputs - ] - thrC = cute.group_modes(cute.prepend_ones(thrC, up_to_rank=2), 0, 2) + def thr_slice(t): + return cute.group_modes(cute.prepend_ones(t[thr_crd], up_to_rank=2), 0, 2) + + # vid -> address + thrInputs = [thr_slice(t) for t in tidfrgInputs] # ((1, V)) + thrC = thr_slice(tidfrgC) # ((1, V)) + thrCrd = tidfrgCrd[thr_crd] # (V) print("[DSL INFO] Sliced Tensors per thread:") for i in cutlass.range_constexpr(len(thrInputs)):