flashinfer.gemm.grouped_gemm_nt_masked¶
- flashinfer.gemm.grouped_gemm_nt_masked(lhs: ~typing.Tuple[~torch.Tensor, ~torch.Tensor], rhs: ~typing.Tuple[~torch.Tensor, ~torch.Tensor], out: ~torch.Tensor, masked_m: ~torch.Tensor, *, ab_dtype: str, sf_dtype: str, c_dtype: str, sf_vec_size: int, topk_weights: ~torch.Tensor | None = None, idx_src_info: ~torch.Tensor | None = None, rank_src_info: ~torch.Tensor | None = None, out_ptrs: ~torch.Tensor | None = None, num_ranks: int = 0, dst_signals: ~torch.Tensor | None = None, sm_count: int | None = None, barrier_flag_local: ~torch.Tensor | None = None, barrier_flag_multicast: ~torch.Tensor | None = None, is_combine_fusion: bool = False, is_swap_ab: bool = False, activation_type: ~flashinfer.tllm_enums.ActivationType | int | None = None, swiglu_alpha: float = 1.0, swiglu_beta: float = 0.0, swiglu_limit: float = 3.4028234663852886e+38, alpha_up: ~torch.Tensor | None = None, a_per_token_scale: ~torch.Tensor | None = None, out_scale: ~torch.Tensor | None = None, out_global_scale: ~torch.Tensor | None = None, nvfp4_4over6: ~flashinfer.quantization.nvfp4_quantization_utils.NVFP44Over6Config | None = <nvfp4_4over6 unset: read FLASHINFER_NVFP4_4OVER6*>, **kwargs)¶
Masked, batched, block-scaled GEMM on Blackwell (SM100/SM103) and Rubin (SM107).
Routes to the architecture-specific kernel for the device’s compute capability: SM100/SM103 use the Blackwell kernel, SM107 the Rubin one.
Executes a masked, batched matrix multiplication with scale factors and optional per-batch alpha scaling on the output.
alphais currently applied internally by the kernel; see Notes for the canonical tensor layouts.- Parameters:
lhs (Tuple[torch.Tensor, torch.Tensor]) –
(A, SFA)— left-hand-side input tensor and its scale-factor tensor.Ahas logical shape(m, k, l)(physically(l, m, k)); for FP4 with 8-bit storage the physical shape is(m, k/2, l).SFAhas logical shape(m32, m4, rm, k4, rk, l)(physically(l, rm, rk, m32, m4, k4)).rhs (Tuple[torch.Tensor, torch.Tensor]) –
(B, SFB)— right-hand-side input tensor and its scale-factor tensor.Bhas logical shape(n, k, l)(physically(l, n, k); FP4 with 8-bit storage is(n, k/2, l)).SFBhas logical shape(n32, n4, rn, k4, rk, l)(physically(l, rn, rk, n32, n4, k4)).out (torch.Tensor) – Output tensor of logical shape
(m, n, l)(physically(l, m, n)), or(m, n / 2, l)withactivation_type. Mutated in place.masked_m (torch.Tensor) – 1-D
int32tensor of shape(l,)giving the valid row count of each batch. Rows abovemasked_m[batch]are ignored.ab_dtype (str) – Data type for
AandB. One of"float4_e2m1fn","float8_e4m3fn","float8_e5m2".sf_dtype (str) – Data type for the scale factors. One of
"float8_e8m0fnu"or"float8_e4m3fn".c_dtype (str) – Data type for output matrix
C. One of"float16","bfloat16","float32","float8_e4m3fn","float8_e5m2", or"float4_e2m1fn"(SM100/SM103 only) to quantize the output to NVFP4; seeout_scale.sf_vec_size (int) – Vector size for scale factors (typically 16 or 32).
topk_weights (Optional[torch.Tensor]) – 2-D
float32tensor of shape(l, m)containing top-k routing weights. Defaults toNone.idx_src_info (Optional[torch.Tensor]) – 2-D
int32tensor of shape(l, m)carrying source-index metadata for the combine fusion path. Defaults toNone.rank_src_info (Optional[torch.Tensor]) – 2-D
int32tensor of shape(l, m)carrying rank-source metadata for the combine fusion path. Defaults toNone.out_ptrs (Optional[torch.Tensor]) – 1-D
int64tensor of shape(num_ranks,)containing remote output pointers for multi-rank combine. Defaults toNone.num_ranks (int) – Number of ranks participating in the combine path. Defaults to
0.dst_signals (Optional[torch.Tensor]) – Optional 1-D signal tensor used by the combine-fusion path to notify consumers. Defaults to
None.sm_count (Optional[int]) – Number of SMs to use. If
None, the runtime picks the max available under the CTA configuration.barrier_flag_local (Optional[torch.Tensor]) – 1-D
int32tensor of shape(sm_count,)containing flags for local barrier synchronization (spin-lock wait in multi-rank ops). Defaults toNone.barrier_flag_multicast (Optional[torch.Tensor]) – 1-D
int32tensor of shape(sm_count,)containing flags for multicast barrier synchronization (release across ranks). Defaults toNone.is_combine_fusion (bool) – If
True, enable the fused GEMM + combine operation mode. Defaults toFalse.is_swap_ab (bool) – If
True, swap thelhs/rhsinput tensors. Defaults toFalse.activation_type (Optional[Union[ActivationType, int]]) – Gated activation fused into the epilogue (SM100/SM103 only). Only
ActivationType.Swigluis supported.None(the default) keeps the plain GEMM epilogue. When set,BandSFBrows must be interleaved in 64-row blocks with the up projection first,[up_0:64, gate_0:64, up_64:128, gate_64:128, ...],nmust be a multiple ofmma_tiler_mn[1], andouthas shape(m, n / 2, l)((m, n / 4, l)bytes for NVFP4 output). The NVFP4w1_weightandw1_weight_sfofflashinfer.fused_moe.CuteDslConfig.prepare_weights()already use this layout; pass them as(w1_weight.permute(1, 2, 0), w1_weight_sf). Each output element isgate * sigmoid(swiglu_alpha * gate) * (up + swiglu_beta)withgate = min(alpha * s * acc_gate, swiglu_limit),up = clamp(alpha_up * s * acc_up, -swiglu_limit, swiglu_limit)andsthe optional per-rowa_per_token_scale. Requiresmma_tiler_mn(128, 128)or(128, 256)and does not support combine fusion,is_swap_abordst_signals.swiglu_alpha (float) – Sigmoid multiplier of the gated activation. Defaults to
1.0. Theswiglu_*scalars,alpha_upanda_per_token_scalerequireactivation_type; setting them without it raises.swiglu_beta (float) – Bias added to the up projection. Defaults to
0.0.swiglu_limit (float) – Clamp limit of the gated activation. Defaults to the float32 maximum.
alpha_up (Optional[torch.Tensor]) – Optional contiguous
float32tensor of shape(l,)scaling the up projection when the up and gate weights have different global scales. Defaults toalpha.a_per_token_scale (Optional[torch.Tensor]) – Optional contiguous
float32tensor of shape(l, m)holding a per-row scale ofA, applied before the gated activation.out_scale (Optional[torch.Tensor]) – Required when
c_dtype="float4_e2m1fn". Receives thefloat8_e4m3fnblock scales (sf_vec_size16) of the output in the same swizzled layout asSFA, sooutandout_scalecan feed the next masked GEMM directly: logical shape(32, 4, rm, 4, rk, l), physically(l, rm, rk, 32, 4, 4), withrk = n_out / 64.outis then auint8tensor of shape(m, n_out / 2, l)holding two E2M1 values per byte, wheren_outis the number of output columns. The epilogue rounds the output to BF16 and quantizes it with the default recipe ofscaled_fp4_grouped_quantize, giving the same bits as quantizing a BF16 output with that function. Requiresna multiple ofmma_tiler_mn[1]andn_outa multiple of 64. Works with or withoutactivation_type. RaisesValueErrorwhen 4over6 is enabled (seenvfp4_4over6) orFLASHINFER_DISABLE_FP4_QUANT_FAST_MATHis set, since the epilogue does not implement those recipes.out_global_scale (Optional[torch.Tensor]) – Required when
c_dtype="float4_e2m1fn". Contiguousfloat32tensor of shape(l,)with the per-batch NVFP4 global scale, as thea_global_sfargument ofscaled_fp4_grouped_quantize.nvfp4_4over6 (NVFP44Over6Config or None) –
NVFP4 4over6 recipe of the NVFP4 output, with the semantics of the same argument of
scaled_fp4_grouped_quantize. The epilogue only implements the standard recipe, so a resolved 4over6 recipe raisesValueError.omitted (the default): read the legacy
FLASHINFER_NVFP4_4OVER6*environment variables.None: 4over6 off. The environment is ignored.NVFP44Over6Config: not supported.
**kwargs –
Additional keyword arguments. Currently recognized:
mma_tiler_mn(Tuple[int, int]): shape of the MMA tiler(M, N). Defaults to(128, 128).mma_tiler_mn[0] == 256enables the 2-CTA MMA path. Must be(128, 128)whenis_combine_fusion=True.cluster_shape_mn(Tuple[int, int]): shape of the CTA cluster(ClusterM, ClusterN). Defaults to(1, 1).alpha(Optional[torch.Tensor]): optional 1-D tensor of shape(l,)containing per-batch scaling factors. When provided, each batch output is multiplied by its corresponding alpha value:out = alpha * (A @ B).alpha_dtype(str): elemental dtype string for thealphatensor (e.g."float32"). Required whenalphais provided.
Other entries are reserved for forward-compatible kernel options.
Notes
Tensor-layout conventions:
lis the batch size,m/nare the row/column counts, andkis the contraction dimension.m32andn32are the constants32;m4andn4are the constants4; andk4is the constant4.m32 * m4 * rmequalsM, whereMismpadded up to the nearest multiple of 128.n32 * n4 * rnequalsN, whereNisnpadded up to the nearest multiple of 128.k4 * rkequalsK, whereKisk / sf_vec_sizepadded up to the nearest multiple of 4.
Masking is applied per batch via
masked_m. Whenalphais provided (see**kwargs), each batch output is multiplied by its corresponding alpha value:out = alpha * (A @ B).