File size: 10,872 Bytes
8c8128e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | # Copyright (c) 2025, Wentao Guo, Ted Zadouri, Tri Dao.
import math
from typing import Optional, Tuple, Union
import cutlass
import cutlass.cute as cute
from cutlass import Float32, Int32, const_expr
from cutlass._mlir.dialects import arith as _arith
from cutlass._mlir.dialects import llvm, nvvm, vector
from cutlass.cutlass_dsl import T, dsl_user_op
@dsl_user_op
def elem_pointer(x: cute.Tensor, coord: cute.Coord, *, loc=None, ip=None) -> cute.Pointer:
return x.iterator + cute.crd2idx(coord, x.layout, loc=loc, ip=ip)
@cute.jit
def load_scalar_or_pointer(x, dtype=Float32):
if const_expr(isinstance(x, cute.Pointer)):
return dtype(cute.make_tensor(x, cute.make_layout(1))[0])
else:
return x
@dsl_user_op
def set_block_rank(
smem_ptr: cute.Pointer, peer_cta_rank_in_cluster: Int32, *, loc=None, ip=None
) -> Int32:
"""Map the given smem pointer to the address at another CTA rank in the cluster."""
smem_ptr_i32 = smem_ptr.toint(loc=loc, ip=ip).ir_value()
return Int32(
llvm.inline_asm(
T.i32(),
[smem_ptr_i32, peer_cta_rank_in_cluster.ir_value()],
"mapa.shared::cluster.u32 $0, $1, $2;",
"=r,r,r",
has_side_effects=False,
is_align_stack=False,
asm_dialect=llvm.AsmDialect.AD_ATT,
)
)
@dsl_user_op
def store_shared_remote(
val: float | Float32 | Int32 | cutlass.Int64,
smem_ptr: cute.Pointer,
mbar_ptr: cute.Pointer,
peer_cta_rank_in_cluster: cute.typing.Int,
*,
loc=None,
ip=None,
) -> None:
remote_smem_ptr_i32 = set_block_rank(
smem_ptr, peer_cta_rank_in_cluster, loc=loc, ip=ip
).ir_value()
remote_mbar_ptr_i32 = set_block_rank(
mbar_ptr, peer_cta_rank_in_cluster, loc=loc, ip=ip
).ir_value()
if const_expr(isinstance(val, float)):
val = Float32(val)
assert isinstance(val, (Float32, Int32, cutlass.Int64)), "val must be Float32, Int32, or Int64"
suffix = {Float32: "f32", Int32: "s32", cutlass.Int64: "s64"}[type(val)]
constraint = {Float32: "f", Int32: "r", cutlass.Int64: "l"}[type(val)]
llvm.inline_asm(
None,
[remote_smem_ptr_i32, val.ir_value(loc=loc, ip=ip), remote_mbar_ptr_i32],
f"st.async.shared::cluster.mbarrier::complete_tx::bytes.{suffix} [$0], $1, [$2];",
f"r,{constraint},r",
has_side_effects=True,
is_align_stack=False,
asm_dialect=llvm.AsmDialect.AD_ATT,
)
@dsl_user_op
def store_shared_remote_x4(
val0: Float32 | Int32,
val1: Float32 | Int32,
val2: Float32 | Int32,
val3: Float32 | Int32,
smem_ptr: cute.Pointer,
mbar_ptr: cute.Pointer,
peer_cta_rank_in_cluster: cute.typing.Int,
*,
loc=None,
ip=None,
) -> None:
remote_smem_ptr_i32 = set_block_rank(
smem_ptr, peer_cta_rank_in_cluster, loc=loc, ip=ip
).ir_value()
remote_mbar_ptr_i32 = set_block_rank(
mbar_ptr, peer_cta_rank_in_cluster, loc=loc, ip=ip
).ir_value()
assert isinstance(val0, (Float32, Int32)), "val must be Float32, or Int32"
dtype = Float32 if isinstance(val0, Float32) else Int32
suffix = {Float32: "f32", Int32: "s32"}[dtype]
constraint = {Float32: "f", Int32: "r"}[dtype]
llvm.inline_asm(
None,
[
remote_smem_ptr_i32,
remote_mbar_ptr_i32,
dtype(val0).ir_value(loc=loc, ip=ip),
dtype(val1).ir_value(loc=loc, ip=ip),
dtype(val2).ir_value(loc=loc, ip=ip),
dtype(val3).ir_value(loc=loc, ip=ip),
],
"{\n\t"
f".reg .v4 .{suffix} abcd;\n\t"
f"mov.{suffix} abcd.x, $2;\n\t"
f"mov.{suffix} abcd.y, $3;\n\t"
f"mov.{suffix} abcd.z, $4;\n\t"
f"mov.{suffix} abcd.w, $5;\n\t"
f"st.async.shared::cluster.mbarrier::complete_tx::bytes.v4.{suffix} [$0], abcd, [$1];\n\t"
"}\n",
f"r,r,{constraint},{constraint},{constraint},{constraint}",
has_side_effects=True,
is_align_stack=False,
asm_dialect=llvm.AsmDialect.AD_ATT,
)
@dsl_user_op
def fmin(a: Union[float, Float32], b: Union[float, Float32], *, loc=None, ip=None) -> Float32:
if cutlass.const_expr(cutlass.CUDA_VERSION.major) == 12:
return Float32(
nvvm.fmin(
T.f32(),
Float32(a).ir_value(loc=loc, ip=ip),
Float32(b).ir_value(loc=loc, ip=ip),
loc=loc,
ip=ip,
)
)
return Float32(
nvvm.fmin(
Float32(a).ir_value(loc=loc, ip=ip),
Float32(b).ir_value(loc=loc, ip=ip),
loc=loc,
ip=ip,
)
)
@dsl_user_op
def sqrt(a: float | Float32, *, loc=None, ip=None) -> Float32:
return Float32(
llvm.inline_asm(
T.f32(),
[Float32(a).ir_value(loc=loc, ip=ip)],
"sqrt.approx.f32 $0, $1;",
"=f,f",
has_side_effects=False,
is_align_stack=False,
asm_dialect=llvm.AsmDialect.AD_ATT,
)
)
@dsl_user_op
def ceil(a: float | Float32, *, loc=None, ip=None) -> Int32:
return Int32(
llvm.inline_asm(
T.i32(),
[Float32(a).ir_value(loc=loc, ip=ip)],
"cvt.rpi.ftz.s32.f32 $0, $1;",
"=r,f",
has_side_effects=False,
is_align_stack=False,
asm_dialect=llvm.AsmDialect.AD_ATT,
)
)
@cute.jit
def fill_oob(tXsX: cute.Tensor, tXpX: Optional[cute.Tensor], fill_value: cute.Numeric) -> None:
"""Fill out-of-bounds values in shared memory tensor.
Args:
tXsX: Shared memory tensor to fill
tXpX: Predicate tensor indicating valid elements
fill_value: Value to fill OOB locations with
"""
tXrX_fill = cute.make_rmem_tensor_like(tXsX[(None, 0), None, 0])
tXrX_fill.fill(fill_value)
for rest_v in cutlass.range_constexpr(tXsX.shape[0][1]):
for rest_k in cutlass.range_constexpr(tXsX.shape[2]):
if const_expr(tXpX is not None):
if not tXpX[rest_v, 0, rest_k]:
cute.autovec_copy(tXrX_fill, tXsX[(None, rest_v), None, rest_k])
else:
cute.autovec_copy(tXrX_fill, tXsX[(None, rest_v), None, rest_k])
# ---------------------------------------------------------------------------
# General-purpose DSL store / vector helpers
# ---------------------------------------------------------------------------
@dsl_user_op
def make_vector(elem_type, *values, loc=None, ip=None):
"""Build an MLIR vector <N x elem_type> from N scalar DSL values.
Example: make_vector(cutlass.Uint32, v0, v1) -> <2 x i32> MLIR vector
"""
from cutlass._mlir import ir
n = len(values)
mlir_ty = elem_type.mlir_type
vec_ty = ir.VectorType.get([n], mlir_ty)
vec = llvm.mlir_undef(vec_ty, loc=loc, ip=ip)
for i, v in enumerate(values):
vec = vector.insertelement(
elem_type(v).ir_value(loc=loc, ip=ip),
vec,
position=_arith.constant(T.i32(), i, loc=loc, ip=ip),
loc=loc,
ip=ip,
)
return vec
@dsl_user_op
def f32x2_to_i64(a: Float32, b: Float32, *, loc=None, ip=None) -> cutlass.Int64:
vec_f32x2 = vector.from_elements(
T.vector(2, T.f32()), (a.ir_value(), b.ir_value()), loc=loc, ip=ip
)
vec_i64x1 = vector.bitcast(T.vector(1, T.i64()), vec_f32x2)
res = cutlass.Int64(
vector.extract(vec_i64x1, dynamic_position=[], static_position=[0], loc=loc, ip=ip)
)
return res
@dsl_user_op
def i64_to_f32x2(c: cutlass.Int64, *, loc=None, ip=None) -> Tuple[Float32, Float32]:
vec_i64x1 = vector.from_elements(T.vector(1, T.i64()), (c.ir_value(),), loc=loc, ip=ip)
vec_f32x2 = vector.bitcast(T.vector(2, T.f32()), vec_i64x1)
res0 = Float32(
vector.extract(vec_f32x2, dynamic_position=[], static_position=[0], loc=loc, ip=ip)
)
res1 = Float32(
vector.extract(vec_f32x2, dynamic_position=[], static_position=[1], loc=loc, ip=ip)
)
return res0, res1
@cute.jit
def warp_prefix_sum(val: Int32, lane: Optional[Int32] = None) -> Int32:
if const_expr(lane is None):
lane = cute.arch.lane_idx()
for i in cutlass.range_constexpr(int(math.log2(cute.arch.WARP_SIZE))):
offset = 1 << i
# Very important that we set mask_and_clamp to 0
partial_sum = cute.arch.shuffle_sync_up(val, offset=offset, mask_and_clamp=0)
if lane >= offset:
val += partial_sum
return val
@dsl_user_op
def atomic_inc_i32(a: int | Int32, gmem_ptr: cute.Pointer, *, loc=None, ip=None) -> Int32:
from cutlass import CUDA_VERSION
# * NVVM call based on nvvm version
if CUDA_VERSION.major == 12 and CUDA_VERSION.minor == 9:
# Old API: requires explicit result type as first positional argument
return nvvm.atomicrmw(
res=T.i32(), op=nvvm.AtomicOpKind.INC, ptr=gmem_ptr.llvm_ptr, a=Int32(a).ir_value()
)
else:
# New API: infers result type automatically
return nvvm.atomicrmw(
op=nvvm.AtomicOpKind.INC, ptr=gmem_ptr.llvm_ptr, a=Int32(a).ir_value()
)
@dsl_user_op
def atomic_add_i32(a: int | Int32, gmem_ptr: cute.Pointer, *, loc=None, ip=None) -> Int32:
from cutlass import CUDA_VERSION
# * NVVM call based on nvvm version
if CUDA_VERSION.major == 12 and CUDA_VERSION.minor == 9:
# Old API: requires explicit result type as first positional argument
return nvvm.atomicrmw(
res=T.i32(), op=nvvm.AtomicOpKind.ADD, ptr=gmem_ptr.llvm_ptr, a=Int32(a).ir_value()
)
else:
# New API: infers result type automatically
return nvvm.atomicrmw(
op=nvvm.AtomicOpKind.ADD, ptr=gmem_ptr.llvm_ptr, a=Int32(a).ir_value()
)
@dsl_user_op
def issue_clc_query_nomulticast(
mbar_ptr: cute.Pointer,
clc_response_ptr: cute.Pointer,
loc=None,
ip=None,
) -> None:
"""
The clusterlaunchcontrol.try_cancel instruction requests atomically cancelling the launch
of a cluster that has not started running yet. It asynchronously writes an opaque response
to shared memory indicating whether the operation succeeded or failed. On success, the
opaque response contains the ctaid of the first CTA of the canceled cluster.
:param mbar_ptr: A pointer to the mbarrier address in SMEM
:type mbar_ptr: Pointer
:param clc_response_ptr: A pointer to the cluster launch control response address in SMEM
:type clc_response_ptr: Pointer
"""
mbar_llvm_ptr = mbar_ptr.llvm_ptr
clc_response_llvm_ptr = clc_response_ptr.llvm_ptr
nvvm.clusterlaunchcontrol_try_cancel(
clc_response_llvm_ptr,
mbar_llvm_ptr,
loc=loc,
ip=ip,
)
|