.. meta::
    :description: Block-wise BF16-to-MXFP8 quantization kernel with a configurable swizzle layout.
    :date-modified: 11/30/2026

.. currentmodule:: nkilib.experimental.quantize_mxfp8

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 <https://github.com/aws-neuron/nki-library/blob/2.34/src/nkilib_src/nkilib/experimental/quantize_mxfp8/quantize_mxfp8.py>`_

quantize_block_mxfp8_kernel
^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. py:function:: 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.

   :param src_tensor: [F, K], Input tensor in BF16 format on HBM
   :type src_tensor: ``nl.ndarray``
   :param return_fp8_dtype: FP8 dtype string like "float8_e4m3fn" or "float8_e5m2"
   :type return_fp8_dtype: ``str``
   :param run_with_lnc2: Enable LNC2 parallelization along F dimension (default: False)
   :type run_with_lnc2: ``bool``
   :param quant_scheme: 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.
   :type quant_scheme: ``str``
   :param use_fast_dma_transpose: 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).
   :type use_fast_dma_transpose: ``bool``
   :param src_orientation: 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.
   :type src_orientation: ``Optional[TensorOrientation]``
   :param logical_layout: 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.
   :type logical_layout: ``bool``
   :return: [K // 4, F], Scales in uint8 format on HBM
   :rtype: ``nl.ndarray``
   :return: [K // 4, F * INTERLEAVE_FACTOR], Quantized data in FP8 format on HBM
   :rtype: ``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)
