Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,15 @@ def elementwise_apply_kernel(
###############################################################################
# Slice to local tile of thread
###############################################################################
# `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))
def thr_slice(t):
return cute.group_modes(cute.prepend_ones(t[thr_crd], up_to_rank=2), 0, 2)

# vid -> address
thrInputs = [t[thr_crd] for t in tidfrgInputs] # (V)
thrC = tidfrgC[thr_crd] # (V)
thrCrd = tidfrgCrd[thr_crd]
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)):
Expand All @@ -132,7 +137,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)):
Expand All @@ -145,10 +150,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
Expand Down Expand Up @@ -254,6 +275,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
Expand Down