Skip to content
Closed
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions flashinfer/experimental/b12x/_lib/intrinsics/atomics.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,40 @@ def red_add_global_bf16x2(addr: Int64, packed: Uint32, *, loc=None, ip=None):
)


@dsl_user_op
def red_add_global_v4_bf16x2(
addr: Int64,
v0: Uint32,
v1: Uint32,
v2: Uint32,
v3: Uint32,
*,
loc=None,
ip=None,
):
"""No-return global 128-bit atomic add of four packed bf16x2 values (8 contiguous bf16).

Requires SM90+/SM120+ and a 16-byte-aligned global address.
"""
llvm.inline_asm(
None,
[
Int64(addr).ir_value(loc=loc, ip=ip),
Uint32(v0).ir_value(loc=loc, ip=ip),
Uint32(v1).ir_value(loc=loc, ip=ip),
Uint32(v2).ir_value(loc=loc, ip=ip),
Uint32(v3).ir_value(loc=loc, ip=ip),
],
"red.relaxed.gpu.global.add.noftz.v4.bf16x2 [$0], {$1, $2, $3, $4};",
"l,r,r,r,r",
has_side_effects=True,
is_align_stack=False,
asm_dialect=llvm.AsmDialect.AD_ATT,
loc=loc,
ip=ip,
)


@dsl_user_op
def red_max_global_f32_nonnegative(addr: Int64, val: Float32, *, loc=None, ip=None):
"""No-return global max reduction for a non-negative fp32 value.
Expand Down
61 changes: 46 additions & 15 deletions flashinfer/experimental/b12x/moe/_shared/kernels/dynamic/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2916,7 +2916,9 @@ class Storage:
):
scatter_rows = Int32(scatter_output.shape[0])
scatter_total_u32 = scatter_rows * cols_u32
scatter_vecs = scatter_total_u32 // Int32(4)
scatter_vecs = Int32(0)
if (scatter_base & Int64(15)) == Int64(0):
scatter_vecs = scatter_total_u32 // Int32(4)
zero_u32 = Uint32(0)
zv = flat_tid
while zv < scatter_vecs:
Expand Down Expand Up @@ -7308,6 +7310,11 @@ class Storage:
# handles B_down availability for FC2 GEMM.
# ============================================================
scatter_N = Int32(scatter_output.shape[1])
scatter_vec16_aligned = (
cutlass.const_expr(self.tile_shape_mnk[1] % 8 == 0)
and ((scatter_base & Int64(15)) == Int64(0))
and ((scatter_N & Int32(7)) == Int32(0))
)
lane_id = Int32(tidx) & Int32(31)
warp_in_tile = Int32(tidx) >> Int32(5)
warp_m_base = (warp_in_tile >> Int32(1)) * Int32(64)
Expand Down Expand Up @@ -8199,20 +8206,44 @@ class Storage:
+ sc_v7
)
else:
scatter_add_v4_bf16x2(
get_ptr_as_int64(
scatter_output,
tok * scatter_N + global_col,
),
sc_v0,
sc_v1,
sc_v2,
sc_v3,
sc_v4,
sc_v5,
sc_v6,
sc_v7,
)
scatter_addr = get_ptr_as_int64(
scatter_output,
Int64(tok) * Int64(scatter_N)
+ Int64(global_col),
)
if scatter_vec16_aligned:
scatter_add_v4_bf16x2(
scatter_addr,
sc_v0,
sc_v1,
sc_v2,
sc_v3,
sc_v4,
sc_v5,
sc_v6,
sc_v7,
)
else:
scatter_add_bf16x2(
scatter_addr,
sc_v0,
sc_v1,
)
scatter_add_bf16x2(
scatter_addr + Int64(4),
sc_v2,
sc_v3,
)
scatter_add_bf16x2(
scatter_addr + Int64(8),
sc_v4,
sc_v5,
)
scatter_add_bf16x2(
scatter_addr + Int64(12),
sc_v6,
sc_v7,
)
vec_idx += Int32(self.num_threads_per_warp)

# All warps must finish the current scatter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
_covering_count,
_materialize_w4a16_topk_route_f32,
)
from b12x._lib.intrinsics.memory import ld_global_v4_u32, st_global_v4_u32
from b12x._lib.intrinsics.memory import (
ld_global_v4_u32,
st_global_u32,
st_global_v4_u32,
)
from b12x.comm.pcie._cute_intrinsics import (
pack_f32x2_to_bf16x2,
pack_f32x2_to_f16x2,
Expand Down Expand Up @@ -569,6 +573,7 @@ class RouteShm:
)
fc2_base_addr = Int64(fc2_flat.iterator.toint())
out_base_addr = Int64(output_flat.iterator.toint())
out_aligned16 = (out_base_addr & Int64(15)) == Int64(0)
pack = tile_start + tid
while pack < tile_end:
acc = cute.make_rmem_tensor((8,), cutlass.Float32)
Expand All @@ -591,13 +596,18 @@ class RouteShm:
acc[p * 2] = acc[p * 2] + _materialize_w4a16_topk_route_f32(lo)
acc[p * 2 + 1] = acc[p * 2 + 1] + _materialize_w4a16_topk_route_f32(hi)
i += Int32(1)
st_global_v4_u32(
out_base_addr + (Int64(tok) * Int64(packs) + Int64(pack)) * Int64(16),
self._pack_pair(acc[0], acc[1]),
self._pack_pair(acc[2], acc[3]),
self._pack_pair(acc[4], acc[5]),
self._pack_pair(acc[6], acc[7]),
)
w0 = self._pack_pair(acc[0], acc[1])
w1 = self._pack_pair(acc[2], acc[3])
w2 = self._pack_pair(acc[4], acc[5])
w3 = self._pack_pair(acc[6], acc[7])
out_addr = out_base_addr + (Int64(tok) * Int64(packs) + Int64(pack)) * Int64(16)
if out_aligned16:
st_global_v4_u32(out_addr, w0, w1, w2, w3)
else:
st_global_u32(out_addr, w0)
st_global_u32(out_addr + Int64(4), w1)
st_global_u32(out_addr + Int64(8), w2)
st_global_u32(out_addr + Int64(12), w3)
pack += Int32(self.vec8_threads)
return
idx = Int32(bidx) * Int32(self.cta_threads) + Int32(tidx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def compile_w4a16_topk_sum(

fc2_fake = make_ptr(cutlass_dtype, 16, cute.AddressSpace.gmem, assumed_align=16)
output_dtype = cutlass.Float32 if full_rotation or float32_output else cutlass_dtype
output_fake = make_ptr(output_dtype, 16, cute.AddressSpace.gmem, assumed_align=16)
output_fake = make_ptr(output_dtype, 4, cute.AddressSpace.gmem, assumed_align=4)
topk_weights_fake = make_ptr(
cutlass.Float32, 4, cute.AddressSpace.gmem, assumed_align=4
)
Expand Down Expand Up @@ -126,7 +126,7 @@ def compile_w4a16_topk_sum(
current_cuda_stream(),
compile_spec=KernelCompileSpec.from_key(
"moe.w4a16.topk_sum",
7,
8,
cache_key,
),
)
Expand Down Expand Up @@ -206,7 +206,7 @@ def _w4a16_topk_sum_launch_flat(
broadcast_svh=broadcast_svh,
float32_output=output.dtype == torch.float32,
)
dummy_addr = output.data_ptr()
dummy_addr = fc2_out.data_ptr()
weights_addr = dummy_addr if topk_weights is None else topk_weights.data_ptr()
route_ids_addr = (
dummy_addr if route_expert_ids is None else route_expert_ids.data_ptr()
Expand All @@ -230,7 +230,7 @@ def _w4a16_topk_sum_launch_flat(
else _cutlass_element_dtype(element_dtype),
output.data_ptr(),
cute.AddressSpace.gmem,
assumed_align=16,
assumed_align=4,
),
make_ptr(
cutlass.Float32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,8 @@ class Storage:
self._run_task(*task_args, 32)
elif valid_rows <= Int32(64):
self._run_task(*task_args, 64)
elif valid_rows <= Int32(96):
self._run_task(*task_args, 96)
else:
self._run_task(*task_args, 128)
elif cutlass.const_expr(self.fast_m128_src and self.tile_m == 64):
Expand Down
71 changes: 50 additions & 21 deletions flashinfer/experimental/b12x/moe/_shared/kernels/w4a8_phase2.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
ld_shared_v4_u32,
shared_ptr_to_u32,
st_global_u32,
st_global_v4_u32,
)
from b12x._lib.intrinsics.mma import e2m1x8_to_qmma_e2m1x8, mxfp8_mma_m16n8k32_f32_e2m1
from b12x._lib.intrinsics.atomics import (
ld_shared_u32,
red_add_global_bf16x2,
red_add_global_v4_bf16x2,
scatter_add_bf16x2,
)
from b12x._lib.intrinsics.packed import pack_f32x2_to_bfloat2
Expand Down Expand Up @@ -1286,7 +1288,7 @@ def _run_task_m128(
down_sfb_rp.iterator, expert_idx, output_tile, tid
)

no_tail_barrier = False
no_tail_barrier = self.stages >= 3
preload_slices = self.stages - 2 if no_tail_barrier else self.stages - 1
for preload_slice in cutlass.range_constexpr(preload_slices):
if Int32(preload_slice) < total_k64:
Expand Down Expand Up @@ -1532,27 +1534,52 @@ def _run_task_m128(
cute.arch.sync_threads()

out_col_base = output_tile * Int32(self.tile_n)
assert compute_m % 8 == 0
rows_per_warp = compute_m // 4
for r in cutlass.range_constexpr(rows_per_warp):
row = warp_idx * Int32(rows_per_warp) + Int32(r)
if row < valid_rows:
tok = ld_shared_u32(epi_tok_base + (row << Int32(2))).to(Int32)
row_smem = smem_base + row * epi_row_bytes
v0 = ld_shared_u32(row_smem + (lane << Int32(2)))
v1 = ld_shared_u32(row_smem + Int32(128) + (lane << Int32(2)))
row_ptr_base = Int64(tok) * Int64(scatter_n) + Int64(out_col_base)
addr0 = get_ptr_as_int64(
scatter_output, row_ptr_base + Int64(lane << Int32(1))
)
addr1 = get_ptr_as_int64(
scatter_output, row_ptr_base + Int64(Int32(64) + (lane << Int32(1)))
)
if cutlass.const_expr(self.deterministic_output):
st_global_u32(addr0, v0)
st_global_u32(addr1, v1)
else:
red_add_global_bf16x2(addr0, v0)
red_add_global_bf16x2(addr1, v1)
sub_lane = lane & Int32(15)
lane_row_off = lane >> Int32(4)
scatter_base_ptr = get_ptr_as_int64(scatter_output, Int64(0))
vec16_aligned = (
cutlass.const_expr(self.tile_n % 8 == 0)
and ((scatter_base_ptr & Int64(15)) == Int64(0))
and ((scatter_n & Int32(7)) == Int32(0))
)
if vec16_aligned:
for rp in cutlass.range_constexpr(rows_per_warp // 2):
row = warp_idx * Int32(rows_per_warp) + Int32(rp * 2) + lane_row_off
if row < valid_rows:
tok = ld_shared_u32(epi_tok_base + (row << Int32(2))).to(Int32)
row_smem = smem_base + row * epi_row_bytes
v0, v1, v2, v3 = ld_shared_v4_u32(row_smem + (sub_lane << Int32(4)))
row_ptr_base = Int64(tok) * Int64(scatter_n) + Int64(out_col_base)
addr = get_ptr_as_int64(
scatter_output, row_ptr_base + Int64(sub_lane << Int32(3))
)
if cutlass.const_expr(self.deterministic_output):
st_global_v4_u32(addr, v0, v1, v2, v3)
else:
red_add_global_v4_bf16x2(addr, v0, v1, v2, v3)
else:
for rp in cutlass.range_constexpr(rows_per_warp // 2):
row = warp_idx * Int32(rows_per_warp) + Int32(rp * 2) + lane_row_off
if row < valid_rows:
tok = ld_shared_u32(epi_tok_base + (row << Int32(2))).to(Int32)
row_smem = smem_base + row * epi_row_bytes
v0, v1, v2, v3 = ld_shared_v4_u32(row_smem + (sub_lane << Int32(4)))
row_ptr_base = Int64(tok) * Int64(scatter_n) + Int64(out_col_base)
addr = get_ptr_as_int64(
scatter_output, row_ptr_base + Int64(sub_lane << Int32(3))
)
if cutlass.const_expr(self.deterministic_output):
st_global_u32(addr, v0)
st_global_u32(addr + Int64(4), v1)
st_global_u32(addr + Int64(8), v2)
st_global_u32(addr + Int64(12), v3)
else:
red_add_global_bf16x2(addr, v0)
red_add_global_bf16x2(addr + Int64(4), v1)
red_add_global_bf16x2(addr + Int64(8), v2)
red_add_global_bf16x2(addr + Int64(12), v3)
cute.arch.sync_threads()

@cute.jit
Expand Down Expand Up @@ -1861,6 +1888,8 @@ class Storage:
self._run_task_m128(*task_args, 32)
elif valid_rows <= Int32(64):
self._run_task_m128(*task_args, 64)
elif valid_rows <= Int32(96):
self._run_task_m128(*task_args, 96)
else:
self._run_task_m128(*task_args, 128)
else:
Expand Down
13 changes: 9 additions & 4 deletions flashinfer/experimental/b12x/moe/fused_moe/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12391,9 +12391,9 @@ def _get_dynamic_kernel(
)
scatter_fake = make_ptr(
cutlass.Float32 if raw_routes else a_dtype,
16,
4,
cute.AddressSpace.gmem,
assumed_align=16,
assumed_align=4,
)
token_map_fake = make_ptr(cutlass.Int32, 4, cute.AddressSpace.gmem, assumed_align=4)
token_weights_fake = make_ptr(
Expand Down Expand Up @@ -12486,7 +12486,7 @@ def _get_dynamic_kernel(
),
compile_spec=KernelCompileSpec.from_key(
"integration.tp_moe.dynamic",
22,
23,
cache_key,
),
dsl_compile_options=dsl_compile_options,
Expand Down Expand Up @@ -12983,7 +12983,7 @@ def _gptr(dtype, t, align=16):
w1_alpha,
w2_alpha,
down_input_scale,
_gptr(cutlass.Float32 if raw_routes else cutlass.BFloat16, scatter_output),
_gptr(cutlass.Float32 if raw_routes else cutlass.BFloat16, scatter_output, 4),
_gptr(cutlass.Int32, token_map, 4),
_gptr(cutlass.Float32, token_weights, 4),
m,
Expand Down Expand Up @@ -14930,6 +14930,11 @@ def b12x_moe_fp4(*, binding: TPMoEFP4Binding) -> torch.Tensor:
)
if not scatter_output.is_contiguous():
raise ValueError("output must be contiguous")
required_output_align = 4 if (impl == "dynamic" and not raw_routes) else 16
if int(scatter_output.data_ptr()) % required_output_align != 0:
raise ValueError(
f"output must be at least {required_output_align}-byte aligned"
)

compact_w4a8_micro = bool(
impl == "micro"
Expand Down
Loading
Loading