flashinfer.gemm.gemm_fp8_nt_groupwise¶
- flashinfer.gemm.gemm_fp8_nt_groupwise(a: Tensor, b: Tensor, a_scale: Tensor, b_scale: Tensor, scale_major_mode: Literal['MN', 'K'] | None = None, mma_sm: int = 1, scale_granularity_mnk: Tuple[int, int, int] = (1, 128, 128), out: Tensor | None = None, out_dtype: dtype | None = None, backend: Literal['cutlass', 'trtllm', 'cutile', 'cute-dsl', 'auto'] = 'cutlass', *, batch_invariant: bool = False) Tensor¶
Performs matrix multiplication with FP8 data types using groupwise scaling.
This function implements a GEMM operation that allows for fine-grained control over scale granularity across different dimensions. Currently only supported on NVIDIA Blackwell architecture.
- Parameters:
a (torch.Tensor) – Row-major input tensor shape (m, k), fp8 e4m3 or fp8 e5m2.
b (torch.Tensor) – Column-major input tensor shape (n, k), fp8 e4m3 or fp8 e5m2.
a_scale (torch.Tensor) –
- if the backend is
cutlass: Column-major scale tensor for a, shape
(m, k // block_size)if scale_major_mode isKor shape(k // block_size, m)if scale_major_mode isMN- if the backend is
trtllm: scale_major_mode should be None, the scale tensor should be (m, k // block_size), contiguous on the first dimension
- if the backend is
b_scale (torch.Tensor) –
- if the backend is
cutlass: Row-major scale tensor for b, shape
(n // block_size, k // block_size)if scale_major_mode isKor shape(k // block_size, n // block_size)if scale_major_mode isMN- if the backend is
trtllm: scale_major_mode should be None, the scale tensor should be (k // block_size, n // block_size), contiguous on the first dimension
- if the backend is
scale_granularity_mnk (Tuple[int, int, int]) – The granularity of the scale tensor, (m_granularity, n_granularity, k_granularity).
scale_major_mode (Literal["MN", "K"]) – The layout mode of scale tensor, MN for MN-major scale with shape of
(k // block_size, *)and K for K-major scale with shape of(*, k // block_size)mma_sm (int) – How many SMs to use for the MMA operation, must be 1 or 2. 2 is faster when number of rows (M) per group is large (>= 256).
out (Optional[torch.Tensor]) – Output tensor, shape (m, n). If not specified, we will create an output tensor explicitly.
out_dtype (Optional[torch.dtype]) – If out is not specified, we will create an output tensor with this dtype. Defaults to
torch.bfloat16.backend (Literal["cutlass", "trtllm", "cutile", "cute-dsl", "auto"]) –
The backend to use for the operation. Defaults to
"cutlass"."auto"chooses between"cutlass"and"cute-dsl": on sm_107 it runs"cute-dsl"where that is faster (see_sm107_groupwise_prefers_cute_dsl), and"cutlass"everywhere else."cute-dsl"(sm_107 only) runs the CuTe-DSLSM107BlockwiseGemmKernelwith split-K. It requiresscale_major_mode="MN",scale_granularity_mnk=(1, 128, 128), e4m3 inputs,nandkmultiples of 128 andma multiple of 4;mma_smis ignored."cutile"(sm_100 / sm_103 / sm_110 / sm_120 / sm_121) is a purecuda.tilePython kernel. v1 restrictions enforced by_cutile_gemm_fp8_nt_groupwise_requirement:scale_major_modemust be"K"(orNone), andscale_granularity_mnkmust be(1, 128, 128).out_dtypeis restricted to bfloat16 / float16 (the function-level_validate_fp8_output_dtyperejects fp32 for all FP8 backends).batch_invariant (bool) –
Trueruns the batch-invariant block-scale fp8 GEMM class in place of the backends (requiresbackend="auto",scale_granularity_mnk = (1, 128, 128), e4m3 operands,ka multiple of 128 and bf16 / fp16 output; both scale layouts are accepted): a row of the output is bit-identical whatever the number of rows, the row’s position in the batch or the configuration chosen, and the result is deterministic. Under autotuning the class’s top configurations for the shape are profiled; all of them compute the same bits. The specification isflashinfer.batch_invariant.fingerprint("gemm_fp8_nt_groupwise", K=, out_dtype=). Unsupported inputs raiseflashinfer.batch_invariant.BatchInvariantUnsupportedErrorinstead of falling back; the operator’s own problem-size check runs first on this path, as for every backend, and raisesValueErrorfor an operand that is not 2-D, akmismatch or an output dtype other than bf16 / fp16. Seeflashinfer/batch_invariant/README.md. Defaults toFalse.
- Returns:
out – Output tensor, shape (m, n).
- Return type:
torch.Tensor
Notes
The
mshould be padded to a multiple of 4 before calling this function, to accommodate the kernel’s requirement.