File size: 13,927 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 326 327 328 329 330 331 332 | # Copyright (c) 2025, Wentao Guo, Ted Zadouri, Tri Dao.
import cutlass
import cutlass.cute as cute
from cutlass import Int32, const_expr
def transpose_view(a: cute.Tensor) -> cute.Tensor:
"""Transpose the first two dimensions of a tensor on smem."""
shape = (a.shape[1], a.shape[0], *a.shape[2:])
order = (1, 0, *range(2, cute.rank(a)))
return cute.composition(a, cute.make_ordered_layout(shape, order=order))
def select(a: cute.Tensor, mode: list[int]) -> cute.Tensor:
return cute.make_tensor(a.iterator, cute.select(a.layout, mode))
def expand(a: cute.Tensor, dim: int, size: Int32 | int) -> cute.Tensor:
shape = (*a.shape[:dim], size, *a.shape[dim:])
stride = (*a.layout.stride[:dim], 0, *a.layout.stride[dim:])
return cute.make_tensor(a.iterator, cute.make_layout(shape, stride=stride))
@cute.jit
def permute_gated_Cregs_b16(t: cute.Tensor) -> None:
assert t.element_type.width == 16
assert cute.size(t.shape) % 4 == 0, "Tensor size must be a multiple of 4 for b16 permutation"
t_u32 = cute.recast_tensor(t, Int32)
quad_idx = cute.arch.lane_idx() % 4
lane_03 = quad_idx == 0 or quad_idx == 3
selector_upper = Int32(0x5410) if lane_03 else Int32(0x1054)
selector_lower = Int32(0x7632) if lane_03 else Int32(0x3276)
# upper_map = [0, 3, 1, 2]
# lower_map = [1, 2, 0, 3]
# upper_idx = upper_map[quad_idx]
# indexing isn't supported so we have to do arithmetic
upper_idx = quad_idx // 2 if quad_idx % 2 == 0 else 3 - quad_idx // 2
lower_idx = upper_idx ^ 1
# 1 -> 0b11111, 2 -> 0b11110, 4 -> 0b11100, 8 -> 0b11000, 16 -> 0b10000, 32 -> 0b00000
width = 4
mask = cute.arch.WARP_SIZE - width
clamp = cute.arch.WARP_SIZE - 1
mask_and_clamp = mask << 8 | clamp
for i in cutlass.range(cute.size(t_u32.shape) // 2, unroll_full=True):
upper, lower = t_u32[i * 2 + 0], t_u32[i * 2 + 1]
upper0 = upper if lane_03 else lower
lower0 = lower if lane_03 else upper
upper0 = cute.arch.shuffle_sync(upper0, offset=upper_idx, mask_and_clamp=mask_and_clamp)
lower0 = cute.arch.shuffle_sync(lower0, offset=lower_idx, mask_and_clamp=mask_and_clamp)
t_u32[i * 2 + 0] = cute.arch.prmt(upper0, lower0, selector_upper)
t_u32[i * 2 + 1] = cute.arch.prmt(upper0, lower0, selector_lower)
@cute.jit
def permute_Cregs_b32_for_stsm(t: cute.Tensor) -> None:
"""Permute and shuffle within 4 threads to change the layout from
T0 | T1 | T2 | T3
a b | c d | e f | g h
to
T0 | T1 | T2 | T3 | T0 | T1 | T2 | T3
a | b | c | d | e | f | g | h
This is so that we can use STSM (instead of STS.64) to store C registers without bank conflict.
"""
assert t.element_type.width == 32
assert cute.size(t.shape) % 4 == 0, "Tensor size must be a multiple of 4 for b32 permutation"
quad_idx = cute.arch.lane_idx() % 4
# left_map = [0, 2, 1, 3]
# right_map = [2, 0, 3, 1]
# indexing isn't supported so we have to do arithmetic
left_idx = quad_idx // 2 if quad_idx % 2 == 0 else 2 + quad_idx // 2
right_idx = left_idx ^ 0b10
# 1 -> 0b11111, 2 -> 0b11110, 4 -> 0b11100, 8 -> 0b11000, 16 -> 0b10000, 32 -> 0b00000
width = 4
mask = cute.arch.WARP_SIZE - width
clamp = cute.arch.WARP_SIZE - 1
mask_and_clamp = mask << 8 | clamp
for i in cutlass.range(cute.size(t.shape) // 4, unroll_full=True):
for r in cutlass.range(2, unroll_full=True):
left, right = t[i * 4 + r * 2 + 0], t[i * 4 + r * 2 + 1]
# a b | c d | e f | g h -> a b | c d | f e | h g
left0 = left if quad_idx < 2 else right
right0 = right if quad_idx < 2 else left
# a b | c d | f e | h g -> a b | f d | c e | h g
left0 = cute.arch.shuffle_sync(left0, offset=left_idx, mask_and_clamp=mask_and_clamp)
# a b | f d | c e | h g -> a e | f b | c g | h d
right0 = cute.arch.shuffle_sync(right0, offset=right_idx, mask_and_clamp=mask_and_clamp)
# a e | f b | c g | h d -> a e | b f | c g | d h
t[i * 4 + r * 2 + 0] = left0 if quad_idx % 2 == 0 else right0
t[i * 4 + r * 2 + 1] = right0 if quad_idx % 2 == 0 else left0
t[i * 4 + 1], t[i * 4 + 2] = t[i * 4 + 2], t[i * 4 + 1]
@cute.jit
def permute_Cregs_b32_for_ldsm(t: cute.Tensor) -> None:
"""Permute and shuffle within 4 threads to change the layout from
T0 | T1 | T2 | T3 | T0 | T1 | T2 | T3
a | b | c | d | e | f | g | h
to
T0 | T1 | T2 | T3
a b | c d | e f | g h
This is so that we can use LDSM (instead of LDS.64) to store C registers without bank conflict.
"""
assert t.element_type.width == 32
assert cute.size(t.shape) % 4 == 0, "Tensor size must be a multiple of 4 for b32 permutation"
quad_idx = cute.arch.lane_idx() % 4
# left_map = [0, 2, 1, 3]
# right_map = [1, 3, 0, 2]
# indexing isn't supported so we have to do arithmetic
left_idx = quad_idx // 2 if quad_idx % 2 == 0 else 2 + quad_idx // 2
right_idx = left_idx ^ 0b01
# 1 -> 0b11111, 2 -> 0b11110, 4 -> 0b11100, 8 -> 0b11000, 16 -> 0b10000, 32 -> 0b00000
width = 4
mask = cute.arch.WARP_SIZE - width
clamp = cute.arch.WARP_SIZE - 1
mask_and_clamp = mask << 8 | clamp
# This is just the inverse of permute_Cregs_b32_for_stsm
for i in cutlass.range(cute.size(t.shape) // 4, unroll_full=True):
t[i * 4 + 1], t[i * 4 + 2] = t[i * 4 + 2], t[i * 4 + 1]
for r in cutlass.range(2, unroll_full=True):
left, right = t[i * 4 + r * 2 + 0], t[i * 4 + r * 2 + 1]
# a e | b f | c g | d h -> a e | f b | c g | h d
left0 = left if quad_idx % 2 == 0 else right
right0 = right if quad_idx % 2 == 0 else left
# a e | f b | c g | h d -> a b | f d | c e | h g
right0 = cute.arch.shuffle_sync(right0, offset=right_idx, mask_and_clamp=mask_and_clamp)
# a b | f d | c e | h g -> a b | c d | f e | h g
left0 = cute.arch.shuffle_sync(left0, offset=left_idx, mask_and_clamp=mask_and_clamp)
# a b | c d | f e | h g -> a b | c d | e f | g h
t[i * 4 + r * 2 + 0] = left0 if quad_idx < 2 else right0
t[i * 4 + r * 2 + 1] = right0 if quad_idx < 2 else left0
@cute.jit
def concat_layout(*layouts: cute.Layout) -> cute.Layout:
return cute.make_layout(
tuple(l.shape for l in layouts),
stride=tuple(l.stride for l in layouts),
)
def convert_layout_acc_mn(acc_layout: cute.Layout, transpose: bool = False) -> cute.Layout:
"""
For Sm80, convert ((2, 2), MMA_M, MMA_N, ...) to ((2, MMA_M), (2, MMA_N), ...).
For Sm90, convert ((2, 2, V), MMA_M, MMA_N, ...) to ((2, MMA_M), (2, V, MMA_N), ...).
"""
acc_layout_col_major = cute.make_layout(acc_layout.shape)
shape = (
(acc_layout_col_major.shape[0][1], acc_layout_col_major.shape[1]), # MMA_M
(
acc_layout_col_major.shape[0][0],
*acc_layout_col_major.shape[0][2:],
acc_layout_col_major.shape[2],
), # MMA_N
*acc_layout_col_major.shape[3:],
)
stride = (
(acc_layout_col_major.stride[0][1], acc_layout_col_major.stride[1]), # MMA_M
(
acc_layout_col_major.stride[0][0],
*acc_layout_col_major.stride[0][2:],
acc_layout_col_major.stride[2],
), # MMA_N
*acc_layout_col_major.stride[3:],
)
if const_expr(transpose):
shape = (shape[1], shape[0], *shape[2:])
stride = (stride[1], stride[0], *stride[2:])
acc_layout_mn = cute.make_layout(shape, stride=stride)
return cute.composition(acc_layout, acc_layout_mn)
def make_acc_tensor_mn_view(acc: cute.Tensor, transpose: bool = False) -> cute.Tensor:
return cute.make_tensor(acc.iterator, convert_layout_acc_mn(acc.layout, transpose=transpose))
def reshape_acc_to_mn(acc: cute.Tensor, transpose: bool = False) -> cute.Tensor:
return cute.make_tensor(acc.iterator, convert_layout_acc_mn(acc.layout, transpose=transpose))
@cute.jit
def convert_layout_acc_frgA(acc_layout: cute.Layout) -> cute.Layout:
# For back to back gemm, convert layout of acc0 to gemm 1 accept layout.
# For Sm80, as the mma instruction shape is 16x8x16, we need to convert from (4, MMA_M, MMA_N) to ((4, 2), MMA_M, MMA_N / 2)
# For Sm90, FP16/BF16, convert acc_layout from ((2, 2, N / 8), MMA_M, MMA_N) to ((2, 2, 2), MMA_M, (N / 16, MMA_N))
# If N / 8 is odd, we'll convert to ((2, 2, 1), MMA_M, N / 8, MMA_N).
# TODO: Sm90 FP8
if const_expr(cute.rank(acc_layout.shape[0]) == 3): # Sm90
div = 2 if const_expr(acc_layout.shape[0][2] % 2 == 0) else 1
l = cute.logical_divide(
acc_layout, ((None, None, div), None, None)
) # ((2, 2, (2, N / 16)), MMA_M, MMA_N)
rA_mma_view = cute.make_layout(
(
(l.shape[0][0], l.shape[0][1], l.shape[0][2][0]),
l.shape[1],
(l.shape[0][2][1], l.shape[2]),
),
stride=(
(l.stride[0][0], l.stride[0][1], l.stride[0][2][0]),
l.stride[1],
(l.stride[0][2][1], l.stride[2]),
),
)
else: # Sm80
# (4, MMA_M, MMA_N) -> (4, MMA_M, (2, MMA_N / 2))
l = cute.logical_divide(acc_layout, (None, None, 2))
rA_mma_view = cute.make_layout(
(
(l.shape[0], l.shape[2][0]),
l.shape[1],
l.shape[2][1],
),
stride=(
(l.stride[0], l.stride[2][0]),
l.stride[1],
l.stride[2][1],
),
)
return rA_mma_view
def reshape_acc_to_frgA(acc: cute.Tensor) -> cute.Tensor:
return cute.make_tensor(acc.iterator, convert_layout_acc_frgA(acc.layout))
def convert_layout_zero_stride(
input: cute.Tensor | cute.Layout, ref_layout: cute.Layout
) -> cute.Layout:
layout = input.layout if const_expr(isinstance(input, cute.Tensor)) else input
# Group the modes with non-zero stride in the ref_layout together,
# and the modes with zero stride together
layout_flat = cute.flatten(layout)
ref_layout_flat = cute.flatten(ref_layout)
nonzero_modes = [i for i in range(cute.rank(layout_flat)) if ref_layout_flat[i].stride != 0]
zero_modes = [i for i in range(cute.rank(layout_flat)) if ref_layout_flat[i].stride == 0]
# There's an edge case when all modes are zero stride
new_shape = (
tuple(layout_flat[i].shape for i in nonzero_modes) if len(nonzero_modes) > 0 else (1,),
tuple(layout_flat[i].shape for i in zero_modes),
)
new_stride = (
tuple(layout_flat[i].stride for i in nonzero_modes) if len(nonzero_modes) > 0 else (0,),
tuple(layout_flat[i].stride for i in zero_modes),
)
out_layout = cute.make_layout(new_shape, stride=new_stride)
if const_expr(isinstance(input, cute.Tensor)):
return cute.make_tensor(input.iterator, out_layout)
else:
return out_layout
def mma_partition_C_vec(
sVec: cute.Tensor, thr_mma: cute.core.ThrMma, expand_shape: int, is_colvec: bool
) -> cute.Tensor:
assert cute.rank(sVec) == 2
assert sVec.stride[0] == 1
stage = sVec.shape[1]
shape = (
(sVec.shape[0], expand_shape, stage)
if const_expr(is_colvec)
else (expand_shape, sVec.shape[0], stage)
)
stride = (1, 0, sVec.stride[1]) if const_expr(is_colvec) else (0, 1, sVec.stride[1])
sVec_mma = cute.make_tensor(sVec.iterator, cute.make_layout(shape, stride=stride))
tC_sVec = make_acc_tensor_mn_view(thr_mma.partition_C(sVec_mma))
return tC_sVec[None, 0, None] if const_expr(is_colvec) else tC_sVec[0, None, None]
def mma_partition_A_vec(
sVec: cute.Tensor, thr_mma: cute.core.ThrMma, expand_shape: int, is_colvec: bool
) -> cute.Tensor:
assert cute.rank(sVec) == 2
assert sVec.stride[0] == 1
stage = sVec.shape[1]
shape = (
(sVec.shape[0], expand_shape, stage)
if const_expr(is_colvec)
else (expand_shape, sVec.shape[0], stage)
)
stride = (1, 0, sVec.stride[1]) if const_expr(is_colvec) else (0, 1, sVec.stride[1])
sVec_mma = cute.make_tensor(sVec.iterator, cute.make_layout(shape, stride=stride))
tC_sVec = make_acc_tensor_mn_view(thr_mma.partition_A(sVec_mma))
return tC_sVec[None, 0, None] if const_expr(is_colvec) else tC_sVec[0, None, None]
def copy_partition_S_vec(
sVec: cute.Tensor, thr_copy: cute.core.ThrCopy, expand_shape: int, is_colvec: bool
) -> cute.Tensor:
assert cute.rank(sVec) == 2
assert sVec.stride[0] == 1
stage = sVec.shape[1]
shape = (
(sVec.shape[0], expand_shape, stage)
if const_expr(is_colvec)
else (expand_shape, sVec.shape[0], stage)
)
stride = (1, 0, sVec.stride[1]) if const_expr(is_colvec) else (0, 1, sVec.stride[1])
sVec_thr = cute.make_tensor(sVec.iterator, cute.make_layout(shape, stride=stride))
tC_sVec = reshape_acc_to_mn(thr_copy.partition_S(sVec_thr))
return tC_sVec[None, 0, None] if const_expr(is_colvec) else tC_sVec[0, None, None]
def copy_partition_D_vec(
sVec: cute.Tensor, thr_copy: cute.core.ThrCopy, expand_shape: int, is_colvec: bool
) -> cute.Tensor:
assert cute.rank(sVec) == 2
assert sVec.stride[0] == 1
stage = sVec.shape[1]
shape = (
(sVec.shape[0], expand_shape, stage)
if const_expr(is_colvec)
else (expand_shape, sVec.shape[0], stage)
)
stride = (1, 0, sVec.stride[1]) if const_expr(is_colvec) else (0, 1, sVec.stride[1])
sVec_thr = cute.make_tensor(sVec.iterator, cute.make_layout(shape, stride=stride))
tC_sVec = reshape_acc_to_mn(thr_copy.partition_D(sVec_thr))
return tC_sVec[None, 0, None] if const_expr(is_colvec) else tC_sVec[0, None, None]
|