Skip to content
Merged
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
65 changes: 50 additions & 15 deletions flashinfer/experimental/b12x/moe/_shared/kernels/dynamic/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2439,6 +2439,8 @@ def __call__(
Int32(b_w13.shape[0]) // Int32(256),
max_active_clusters,
stream,
task_head=task_head,
p2_task_head=producers_done_count,
)
if cutlass.const_expr(self.external_materialized_fc2):
if cutlass.const_expr(self.nvfp4_split_materialized):
Expand Down Expand Up @@ -2477,6 +2479,8 @@ def __call__(
max_active_clusters,
Int32(0),
stream,
task_head=producers_done_count,
p1_task_head=task_head,
)

@cute.kernel
Expand Down Expand Up @@ -2916,7 +2920,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 +7314,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 +8210,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
Loading
Loading