This document is relevant for: Trn2, Trn3

Quantize MXFP8 Kernel API Reference#

Block-wise BF16-to-MXFP8 quantization with a configurable (1x32 / wrapX) swizzle layout.

Background#

The quantize_block_mxfp8_kernel quantizes a BF16 tensor to MXFP8 block-wise, emitting the quantized data and its MX scales in the swizzle layout selected by quant_scheme (1x32 or wrapX), or the logical layout when requested.

API Reference#

Source code for this kernel API can be found at: quantize_mxfp8.py

quantize_block_mxfp8_kernel#

nkilib.experimental.quantize_mxfp8.quantize_block_mxfp8_kernel(src_tensor: nl.NkiTensor, return_fp8_dtype: str, run_with_lnc2: bool = False, quant_scheme: str = None, use_fast_dma_transpose: bool = False, src_orientation: Optional[TensorOrientation] = None, logical_layout: bool = False) → tuple[nl.NkiTensor, nl.NkiTensor]#

Kernel for quantizing BF16 tensor to MXFP8 format with block-wise quantization.

Parameters:
  • src_tensor (nl.ndarray) – [F, K], Input tensor in BF16 format on HBM

  • return_fp8_dtype (str) – FP8 dtype string like “float8_e4m3fn” or “float8_e5m2”

  • run_with_lnc2 (bool) – Enable LNC2 parallelization along F dimension (default: False)

  • quant_scheme (str) – Required. Swizzle layout of the quantized output, “1x32” or “wrapX”. “wrapX” scatters a load tile’s K across the four output columns of a feature and is loaded with a DMA gather-transpose; “1x32” packs INTERLEAVE_FACTOR consecutive K values of a feature into its four adjacent output columns and is loaded with the FP32 PE transpose. The two produce different data orderings and are not interchangeable – the consuming matmul must use the same scheme. Prefer 1x32: its layout is independent of the K tiling, so any matmul tile_k can consume it, and K-shards quantized separately concatenate into the whole tensor’s output. A wrapX output is only correct consumed whole by a matmul that tiles K exactly as this kernel does; composing wrapX outputs along K (concatenating, sharding, all-gathering) is illegal and silently gives wrong results, so wrapX logs a warning.

  • use_fast_dma_transpose (bool) – For wrapX, load every K tile with the fast (direct access pattern) DMA gather-transpose instead of the legacy vector-offset gather. Honoured for any K. When False the loader falls back to legacy DGT where legacy can express the shape and to the wrapX PE transpose where it cannot. Ignored for 1x32, which always uses its own PE loader. All three produce the identical output layout. Correct for any supported K: the fast path needed a uniform-height load destination to be reliable, which this kernel now gives it (see the loader-selection comment).

  • src_orientation (Optional[TensorOrientation]) – Layout of src_tensor relative to the contraction dim K. None or F_BY_K (default): src_tensor is [F, K]. K_BY_F: src_tensor is [K, F] – e.g. a [O, I] weight quantized for the GEMM that contracts O – and is read as is, so the caller need not materialize its transpose. The outputs are identical either way. K_BY_F requires quant_scheme=’1x32’; it loads with the DMA + tensor_copy 1x32 swizzle.

  • logical_layout (bool) – Return the logical layout instead of the swizzled one: data shaped like src_tensor and scales blocked along K in place – [F, K] input -> data [F, K], scales [F, K // 32]; [K, F] input -> data [K, F], scales [K // 32, F] (see _prequant_logical_layout.py). This is the layout of a generic (data, scale) MX tensor, e.g. what a compiler-lowered MX quantize returns. matmul_mxfp8 and matmul_mxfp8_backward take it as is (as *_scales with the data); the scale shape identifies the layout. Requires quant_scheme=’1x32’. The swizzled layout (default) loads into the matmul with no conversion; the logical one costs the matmul a conversion per loaded block.

Returns:

[K // 4, F], Scales in uint8 format on HBM

Return type:

nl.ndarray

Returns:

[K // 4, F * INTERLEAVE_FACTOR], Quantized data in FP8 format on HBM

Return type:

nl.ndarray

Notes:

  • K dimension must be divisible by 512 for mxfp8 quantization

  • F dimension must be divisible by 8 for quantization

  • LNC2 splits work along F dimension; supports uneven splits

Dimensions:

  • F: Feature dimension (rows in input tensor)

This document is relevant for: Trn2, Trn3