.. meta::
    :description: Fused (Layer|RMS)Norm + modulation + optional FP8 ROW quantization along the last dim.
    :date-modified: 11/30/2026

.. currentmodule:: nkilib.experimental.norm

AdaLN-Quant Kernel API Reference
================================

Fused (Layer|RMS)Norm + modulation + optional FP8 ROW quantization along the last dim.

The diffusion-model analog of ``rmsnorm_quant``: adds mean-subtracted LayerNorm (``elementwise_affine=False``) and a unified per-hidden modulation ``y = norm(x) * mul + add``. Intended for DiT-style transformer blocks with H <= 16384 (MAX_H), e.g. Wan2.2 at H = 5120; the outer (token) dimension is tiled by 128 and has no upper bound beyond MAX_S.

Background
-----------

``adaln_quant_kernel`` fuses (Layer|RMS)Norm, per-hidden modulation (``y = norm(x) * mul + add``), and optional FP8 row quantization along the last dimension in a single pass. It is the diffusion-model analog of ``rmsnorm_quant`` for DiT-style transformer blocks (e.g. Wan2.2).

API Reference
--------------

**Source code for this kernel API can be found at**: `adaln_quant.py <https://github.com/aws-neuron/nki-library/blob/2.34/src/nkilib_src/nkilib/experimental/norm/adaln_quant.py>`_

adaln_quant_kernel
^^^^^^^^^^^^^^^^^^

.. py:function:: adaln_quant_kernel(hidden: nl.NkiTensor, kargs: AdaLNQuantKernelArgs, mul: nl.NkiTensor = None, add: nl.NkiTensor = None, dtype_mode: DtypeMode = DtypeMode.NON_OCP, residual: nl.NkiTensor = None, gate: nl.NkiTensor = None) -> nl.NkiTensor

   Fused (Layer|RMS)Norm + modulation + optional FP8 ROW quantization along the last dim.

   :param hidden: ``[..., H]`` input hidden states on HBM (>= 2 dims). When ``residual`` is given this is the sub-layer output being gated and added.
   :type hidden: ``nl.NkiTensor``
   :param kargs: Kernel configuration.
   :type kargs: ``AdaLNQuantKernelArgs``
   :param mul: ``[H]`` or ``[1, H]`` multiplicative modulation (gamma, or 1+scale). ``None`` skips the multiply.
   :type mul: ``nl.NkiTensor``
   :param add: ``[H]`` or ``[1, H]`` additive modulation (beta, or shift). ``None`` skips the add.
   :type add: ``nl.NkiTensor``
   :param dtype_mode: FP8 E4M3 dtype selection for ROW quant output. ``NON_OCP`` -> ``nl.float8_e4m3`` (max 240); ``OCP`` -> ``nl.float8_e4m3fn`` (max 448).
   :type dtype_mode: ``DtypeMode``
   :param residual: ``[..., H]`` residual added to ``gate * hidden`` before the norm. ``None`` disables the fused add (plain ``Norm(hidden)`` semantics, single return value).
   :type residual: ``nl.NkiTensor``
   :param gate: ``[H]`` or ``[1, H]`` multiplier applied to ``hidden`` before the residual add, or ``None`` (plain add). Ignored when ``residual`` is ``None``.
   :type gate: ``nl.NkiTensor``

   **Notes**:

   * The modulation covers both flavors used by Wan2.2's DiT: * plain affine LayerNorm (cross-attention norm2): ``mul = gamma``, ``add = beta``; * adaptive LayerNorm (AdaLN — norm1 / norm3 / norm_out): ``mul = 1 + scale``, ``add = shift``, where ``scale`` / ``shift`` come from the timestep embedding.
   * ``mul`` and ``add`` are per-hidden vectors shared across all rows of the tensor, so the kernel processes a single "batch" of modulation at a time (the AdaLN scale/shift differ per batch element in the model; the caller loops over the batch dimension).
   * ``QuantizationType.NONE`` emits the normalized tensor in the input dtype (norm-only fusion). ``QuantizationType.ROW`` emits per-row FP8 with the fp32 dequant scale appended as 4 fp8 elements (the ``H + 4`` packed layout).
   * ``QuantizationType.MX`` output is not supported and is rejected by ``AdaLNQuantKernelArgs``. An MX path (``nisa.quantize_mx`` + swizzle transpose, as in ``rmsnorm_mx_prefill``) is left as a follow-up: it requires on-device validation and has no activation consumer today (Wan2.2 quantizes activations with ROW, MX being the *weight* layout).
   * LNC2 shards the outer (token) dimension across programs; rows are independent.

   **Dimensions**:

   * OD: Outer dimension (product of all but the last dim, i.e. tokens).

