.. meta::
    :description: Gradients for the MXFP8 SwiGLU MLP (hidden, gate/up weight, down weight).
    :date-modified: 11/30/2026

.. currentmodule:: nkilib.experimental.mlp_mxfp8.mlp_bwd_mxfp8

MLP Backward MXFP8 Kernel API Reference
=======================================

Gradients for the MXFP8 SwiGLU MLP (hidden, gate/up weight, down weight).

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

``mlp_backward_mxfp8_nki`` returns the hidden-state, gate/up weight, and down weight gradients of the MXFP8 SwiGLU MLP, consuming the forward's activation checkpoint. Forward and backward both run two blocks of T/2 tokens.

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

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

mlp_backward_mxfp8_nki
^^^^^^^^^^^^^^^^^^^^^^

.. py:function:: mlp_backward_mxfp8_nki(hidden_states: nl.NkiTensor, gate_up_proj_weight: nl.NkiTensor, down_proj_weight: nl.NkiTensor, output_hidden_states_grad: nl.NkiTensor, gate_up_proj_act_checkpoint_T: nl.NkiTensor, config: MXFP8MOEBwdConfig=None, gate_up_weight_scales: nl.NkiTensor=None, down_weight_scales: nl.NkiTensor=None) -> tuple

   Return gradients for ``hidden``, gate/up weight, and down weight.

   :param hidden_states: Forward input hidden states ``[T, H]``.
   :type hidden_states: ``nl.NkiTensor``
   :param gate_up_proj_weight: Fused gate/up projection weights, BF16 ``[H, 2, I]`` (packed MXFP8 ``[2*I/4, H]`` with scales).
   :type gate_up_proj_weight: ``nl.NkiTensor``
   :param down_proj_weight: Down projection weights, BF16 ``[I, H]`` (packed MXFP8 ``[H/4, I]`` with scales).
   :type down_proj_weight: ``nl.NkiTensor``
   :param output_hidden_states_grad: Gradient of the forward output.
   :type output_hidden_states_grad: ``nl.NkiTensor``
   :param gate_up_proj_act_checkpoint_T: Forward activation checkpoint ``[2, 2, T/2, I]`` produced by ``mlp_forward_mxfp8_nki``.
   :type gate_up_proj_act_checkpoint_T: ``nl.NkiTensor``
   :param config: Kernel configuration; ``None`` uses defaults.
   :type config: ``MXFP8MOEBwdConfig``
   :param gate_up_weight_scales: MX scales for the gate/up weights.
   :type gate_up_weight_scales: ``nl.NkiTensor``
   :param down_weight_scales: MX scales for the down weights.
   :type down_weight_scales: ``nl.NkiTensor``
