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. alpha is 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. A has logical shape (m, k, l) (physically (l, m, k)); for FP4 with 8-bit storage the physical shape is (m, k/2, l). SFA has 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. B has logical shape (n, k, l) (physically (l, n, k); FP4 with 8-bit storage is (n, k/2, l)). SFB has 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) with activation_type. Mutated in place.

  • masked_m (torch.Tensor) – 1-D int32 tensor of shape (l,) giving the valid row count of each batch. Rows above masked_m[batch] are ignored.

  • ab_dtype (str) – Data type for A and B. 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; see out_scale.

  • sf_vec_size (int) – Vector size for scale factors (typically 16 or 32).

  • topk_weights (Optional[torch.Tensor]) – 2-D float32 tensor of shape (l, m) containing top-k routing weights. Defaults to None.

  • idx_src_info (Optional[torch.Tensor]) – 2-D int32 tensor of shape (l, m) carrying source-index metadata for the combine fusion path. Defaults to None.

  • rank_src_info (Optional[torch.Tensor]) – 2-D int32 tensor of shape (l, m) carrying rank-source metadata for the combine fusion path. Defaults to None.

  • out_ptrs (Optional[torch.Tensor]) – 1-D int64 tensor of shape (num_ranks,) containing remote output pointers for multi-rank combine. Defaults to None.

  • 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 int32 tensor of shape (sm_count,) containing flags for local barrier synchronization (spin-lock wait in multi-rank ops). Defaults to None.

  • barrier_flag_multicast (Optional[torch.Tensor]) – 1-D int32 tensor of shape (sm_count,) containing flags for multicast barrier synchronization (release across ranks). Defaults to None.

  • is_combine_fusion (bool) – If True, enable the fused GEMM + combine operation mode. Defaults to False.

  • is_swap_ab (bool) – If True, swap the lhs/rhs input tensors. Defaults to False.

  • activation_type (Optional[Union[ActivationType, int]]) – Gated activation fused into the epilogue (SM100/SM103 only). Only ActivationType.Swiglu is supported. None (the default) keeps the plain GEMM epilogue. When set, B and SFB rows must be interleaved in 64-row blocks with the up projection first, [up_0:64, gate_0:64, up_64:128, gate_64:128, ...], n must be a multiple of mma_tiler_mn[1], and out has shape (m, n / 2, l) ((m, n / 4, l) bytes for NVFP4 output). The NVFP4 w1_weight and w1_weight_sf of flashinfer.fused_moe.CuteDslConfig.prepare_weights() already use this layout; pass them as (w1_weight.permute(1, 2, 0), w1_weight_sf). Each output element is gate * sigmoid(swiglu_alpha * gate) * (up + swiglu_beta) with gate = min(alpha * s * acc_gate, swiglu_limit), up = clamp(alpha_up * s * acc_up, -swiglu_limit, swiglu_limit) and s the optional per-row a_per_token_scale. Requires mma_tiler_mn (128, 128) or (128, 256) and does not support combine fusion, is_swap_ab or dst_signals.

  • swiglu_alpha (float) – Sigmoid multiplier of the gated activation. Defaults to 1.0. The swiglu_* scalars, alpha_up and a_per_token_scale require activation_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 float32 tensor of shape (l,) scaling the up projection when the up and gate weights have different global scales. Defaults to alpha.

  • a_per_token_scale (Optional[torch.Tensor]) – Optional contiguous float32 tensor of shape (l, m) holding a per-row scale of A, applied before the gated activation.

  • out_scale (Optional[torch.Tensor]) – Required when c_dtype="float4_e2m1fn". Receives the float8_e4m3fn block scales (sf_vec_size 16) of the output in the same swizzled layout as SFA, so out and out_scale can feed the next masked GEMM directly: logical shape (32, 4, rm, 4, rk, l), physically (l, rm, rk, 32, 4, 4), with rk = n_out / 64. out is then a uint8 tensor of shape (m, n_out / 2, l) holding two E2M1 values per byte, where n_out is the number of output columns. The epilogue rounds the output to BF16 and quantizes it with the default recipe of scaled_fp4_grouped_quantize, giving the same bits as quantizing a BF16 output with that function. Requires n a multiple of mma_tiler_mn[1] and n_out a multiple of 64. Works with or without activation_type. Raises ValueError when 4over6 is enabled (see nvfp4_4over6) or FLASHINFER_DISABLE_FP4_QUANT_FAST_MATH is set, since the epilogue does not implement those recipes.

  • out_global_scale (Optional[torch.Tensor]) – Required when c_dtype="float4_e2m1fn". Contiguous float32 tensor of shape (l,) with the per-batch NVFP4 global scale, as the a_global_sf argument of scaled_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 raises ValueError.

    • 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] == 256 enables the 2-CTA MMA path. Must be (128, 128) when is_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 the alpha tensor (e.g. "float32"). Required when alpha is provided.

    Other entries are reserved for forward-compatible kernel options.

Notes

Tensor-layout conventions:

  • l is the batch size, m/n are the row/column counts, and k is the contraction dimension.

  • m32 and n32 are the constants 32; m4 and n4 are the constants 4; and k4 is the constant 4.

  • m32 * m4 * rm equals M, where M is m padded up to the nearest multiple of 128.

  • n32 * n4 * rn equals N, where N is n padded up to the nearest multiple of 128.

  • k4 * rk equals K, where K is k / sf_vec_size padded up to the nearest multiple of 4.

Masking is applied per batch via masked_m. When alpha is provided (see **kwargs), each batch output is multiplied by its corresponding alpha value: out = alpha * (A @ B).