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 is K or shape (k // block_size, m) if scale_major_mode is MN

    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

  • 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 is K or shape (k // block_size, n // block_size) if scale_major_mode is MN

    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

  • 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-DSL SM107BlockwiseGemmKernel with split-K. It requires scale_major_mode="MN", scale_granularity_mnk=(1, 128, 128), e4m3 inputs, n and k multiples of 128 and m a multiple of 4; mma_sm is ignored.

    "cutile" (sm_100 / sm_103 / sm_110 / sm_120 / sm_121) is a pure cuda.tile Python kernel. v1 restrictions enforced by _cutile_gemm_fp8_nt_groupwise_requirement: scale_major_mode must be "K" (or None), and scale_granularity_mnk must be (1, 128, 128). out_dtype is restricted to bfloat16 / float16 (the function-level _validate_fp8_output_dtype rejects fp32 for all FP8 backends).

  • batch_invariant (bool) – True runs the batch-invariant block-scale fp8 GEMM class in place of the backends (requires backend="auto", scale_granularity_mnk = (1, 128, 128), e4m3 operands, k a 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 is flashinfer.batch_invariant.fingerprint("gemm_fp8_nt_groupwise", K=, out_dtype=). Unsupported inputs raise flashinfer.batch_invariant.BatchInvariantUnsupportedError instead of falling back; the operator’s own problem-size check runs first on this path, as for every backend, and raises ValueError for an operand that is not 2-D, a k mismatch or an output dtype other than bf16 / fp16. See flashinfer/batch_invariant/README.md. Defaults to False.

Returns:

out – Output tensor, shape (m, n).

Return type:

torch.Tensor

Notes

The m should be padded to a multiple of 4 before calling this function, to accommodate the kernel’s requirement.