.. meta::
    :description: Backward of blockwise_mm_shard_on_I_packed over the packed rows of one or more experts.
    :date-modified: 11/30/2026

.. currentmodule:: nkilib.experimental.moe.bwd

Blockwise MM Backward Packed Kernel API Reference
=================================================

Backward of blockwise_mm_shard_on_I_packed over the packed rows of one or more experts.

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

``blockwise_mm_bwd_packed`` computes the backward pass of ``blockwise_mm_shard_on_I_packed`` over the packed rows of one or more experts, emitting the hidden-state, expert-affinity, and gate/up and down weight gradients.

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

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

blockwise_mm_bwd_packed
^^^^^^^^^^^^^^^^^^^^^^^

.. py:function:: blockwise_mm_bwd_packed(hidden_states: nl.NkiTensor, expert_affinities: nl.NkiTensor, gate_up_proj_weight: nl.NkiTensor, down_proj_weight: nl.NkiTensor, gate_up_proj_act_checkpoint_T: nl.NkiTensor, down_proj_act_checkpoint: nl.NkiTensor, output_hidden_states_grad: nl.NkiTensor, packed_valid_token_count: nl.NkiTensor, packed_active_block_count: nl.NkiTensor, block_size: int, activation_type: ActFnType = ActFnType.SiLU, skip_gate_proj: bool = False, compute_dtype: nki.dtype = nl.bfloat16, accumulation_dtype: nki.dtype = None, clamp_limits: ClampLimits = None, blocking_params: MOEBwdDroplessBlockingParams = None, hidden_states_grad_out: nl.NkiTensor = None, expert_affinities_grad_out: nl.NkiTensor = None, packed_block_expert: nl.NkiTensor = None, packed_block_row_start: nl.NkiTensor = None, packed_group_blocks: int = None, gate_up_proj_weight_grad_out: nl.NkiTensor = None, down_proj_weight_grad_out: nl.NkiTensor = None) -> tuple

   Backward of blockwise_mm_shard_on_I_packed over the packed rows of one or more experts.

   :param hidden_states: [T, H], Packed forward input rows.
   :type hidden_states: ``nl.NkiTensor``
   :param expert_affinities: [T, 1], Affinity per row, applied after the down projection, or None for an affinity of one on every row, as in the forward. Without affinities there is no affinity gradient: it is neither computed nor returned.
   :type expert_affinities: ``nl.NkiTensor``
   :param gate_up_proj_weight: [E, H, 2, I_TP], Gate and up projection weights.
   :type gate_up_proj_weight: ``nl.NkiTensor``
   :param down_proj_weight: [E, I_TP, H], Down projection weights.
   :type down_proj_weight: ``nl.NkiTensor``
   :param gate_up_proj_act_checkpoint_T: [N, 2, I_TP, B], Forward gate/up checkpoint.
   :type gate_up_proj_act_checkpoint_T: ``nl.NkiTensor``
   :param down_proj_act_checkpoint: [N, B, H], Forward down projection before the affinity, read only for the affinity gradient: None (or unread) without affinities.
   :type down_proj_act_checkpoint: ``nl.NkiTensor``
   :param output_hidden_states_grad: [T, H], Gradient of the forward output.
   :type output_hidden_states_grad: ``nl.NkiTensor``
   :param packed_valid_token_count: [N, 1] int32, Valid rows of each block.
   :type packed_valid_token_count: ``nl.NkiTensor``
   :param packed_active_block_count: [1, 1] int32, Number of leading blocks with valid rows. Only these blocks run.
   :type packed_active_block_count: ``nl.NkiTensor``
   :param block_size: B, a multiple of 256.
   :type block_size: ``int``
   :param activation_type: SiLU with a gate projection, or SquaredReLU without one.
   :type activation_type: ``ActFnType``
   :param skip_gate_proj: Skip the gate projection (SquaredReLU only).
   :type skip_gate_proj: ``bool``
   :param compute_dtype: Matmul input dtype.
   :type compute_dtype: ``nki.dtype``
   :param accumulation_dtype: float32 accumulates the weight gradients across blocks in fp32; each group of blocks also stores them in their own dtype, so the expert's last group leaves the result. The row gradients are written once in their own dtype.
   :type accumulation_dtype: ``nki.dtype``
   :param clamp_limits: Activation clamp limits.
   :type clamp_limits: ``ClampLimits``
   :param blocking_params: Tiling of the backward steps.
   :type blocking_params: ``MOEBwdDroplessBlockingParams``
   :param hidden_states_grad_out: Caller-owned [T, H] hidden-state gradient.
   :type hidden_states_grad_out: ``nl.NkiTensor``
   :param expert_affinities_grad_out: Caller-owned [T, 1] affinity gradient; not allowed without affinities. packed_block_expert, packed_block_row_start (nl.NkiTensor, optional): [N, 1] int32, Expert and first row of each block, as in the forward; required when E > 1. The tables are read on device and not checked there; the active blocks must
   :type expert_affinities_grad_out: ``nl.NkiTensor``
   :param satisfy: blocks in expert order and, within an expert, in row order, each expert's blocks consecutive (sorted, contiguous packed_block_expert, which the per-expert weight-gradient groups rely on); 0 <= expert < E; and row_start + B <= T. T >= B is checked at trace time.
   :param packed_group_blocks: Blocks per weight-gradient group (a power of two); each group of an expert accumulates the weight gradients once.
   :type packed_group_blocks: ``int``
   :param gate_up_proj_weight_grad_out: Caller-owned [E, H, 2, I_TP] gate/up weight gradient in the weight dtype.
   :type gate_up_proj_weight_grad_out: ``nl.NkiTensor``
   :param down_proj_weight_grad_out: Caller-owned [E, I_TP, H] down weight gradient in the weight dtype. With all four outputs caller-owned (the three other than the affinity gradient without affinities) the kernel allocates no HBM result, so the compiler cannot place the kernel's internal HBM scratch (such as the fp32 weight-gradient accumulators) over one.
   :type down_proj_weight_grad_out: ``nl.NkiTensor``

   **Dimensions**:

   * T: Packed row capacity
   * H: Hidden dimension size
   * B: Rows per block
   * N: Number of blocks: T / B with one expert, else the length of the block tables
   * E: Number of experts

