Views
No views yet
FLASHRT_W4A16_EDGE_UNROLL=2; the SM120 value remains 4.w4a16_decode_gemv_bf16(x_bf16, weight_packed, sfb, alpha=1.0, out=None)grouped_w4a16_gemv_bf16(activations, weight_stack, sfb_stack, alpha_stack, expert_idx, n, w_stride=None, sfb_stride=None, out=None)quantize_activations_nvfp4_bf16(activations, packed=None, sfa=None)quantize_weights_nvfp4_bf16(weights, packed=None, sfb=None)grouped_w4a4_gemv_bf16(activations_packed, weight_stack, sfa, sfb_stack, alpha_stack, expert_idx, out=None)grouped_w4a4_gemv_from_bf16(activations, weight_stack, sfb_stack, alpha_stack, expert_idx, packed=None, sfa=None, out=None)[M,K/2], expert weights
[E,N,K/2], and a contiguous device routing tensor [M,top_k]. It emits
[M,top_k,N] in one grouped compute launch. For down projections with a
different activation per routed pair, flatten to M=routed_pairs, top_k=1.K must be divisible by 16 and N by 8. Target K%64==0 shapes use tuned
SM120 paths; the remaining K%16 shapes use a fixed-order SIMT contract path.
No atomics, host synchronization, or dynamic workspace are used by the native
ops. Pass packed, sfa, and out buffers to the composed helper for
allocation-free CUDA Graph capture.1from kernels import get_kernel
2import torch
3
4try:
5 moe = get_kernel(
6 "flashrt/grouped-moe-gemv", version=2, trust_remote_code=True
7 )
8except TypeError: # kernels==0.12.x compatibility
9 moe = get_kernel("flashrt/grouped-moe-gemv", version=2)
10
11M, TOP_K, E, N, K = 7, 8, 8, 1024, 2048
12x = torch.randn(M, K, device="cuda", dtype=torch.bfloat16)
13expert_idx = torch.randint(E, (M, TOP_K), device="cuda", dtype=torch.int32)
14
15def sf_bytes(rows, dim):
16 return ((rows + 127) // 128) * (((dim // 16) + 3) // 4) * 512
17
18# Do this once while loading the checkpoint, not in the inference hot path.
19weights_bf16 = torch.randn(E, N, K, device="cuda", dtype=torch.bfloat16)
20weights_packed = torch.empty(E, N, K // 2, device="cuda", dtype=torch.uint8)
21weight_sfs = torch.empty(E, sf_bytes(N, K), device="cuda", dtype=torch.uint8)
22for expert in range(E):
23 moe.quantize_weights_nvfp4_bf16(
24 weights_bf16[expert],
25 packed=weights_packed[expert],
26 sfb=weight_sfs[expert],
27 )
28weight_alpha = torch.ones(E, device="cuda", dtype=torch.float32)
29
30packed = torch.empty(M, K // 2, device="cuda", dtype=torch.uint8)
31sfa = torch.empty(sf_bytes(M, K), device="cuda", dtype=torch.uint8)
32out = torch.empty(M, TOP_K, N, device="cuda", dtype=torch.bfloat16)
33y = moe.grouped_w4a4_gemv_from_bf16(
34 x, weights_packed, weight_sfs, weight_alpha, expert_idx,
35 packed=packed, sfa=sfa, out=out,
36)1python grouped-moe-gemv/tests/test_grouped_moe_gemv.py --backend source --mode full
2python grouped-moe-gemv/benchmarks/benchmark.py --backend source