.. meta::
    :description: Shard-on-I blockwise MoE forward over the packed rows of one or more experts.
    :date-modified: 11/30/2026

.. currentmodule:: nkilib.experimental.moe.forward

Blockwise MM Shard-on-I Packed Kernel API Reference
===================================================

Shard-on-I blockwise MoE forward over the packed rows of one or more experts.

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

``blockwise_mm_shard_on_I_packed`` computes a shard-on-I blockwise Mixture-of-Experts forward pass over the packed rows of one or more experts, optionally returning the gate/up and down activation checkpoints consumed by its backward kernel.

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

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

blockwise_mm_shard_on_I_packed
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. py:function:: blockwise_mm_shard_on_I_packed(hidden_states, expert_affinities, gate_up_proj_weight, down_proj_weight, packed_valid_token_count, packed_active_block_count, block_size: int, activation_function: ActFnType = ActFnType.SiLU, skip_gate_proj: bool = False, compute_dtype = nl.bfloat16, accumulation_dtype = None, checkpoint_activation: bool = False, output_out: Optional[nl.NkiTensor] = None, gate_up_activations_T_out: Optional[nl.NkiTensor] = None, down_activations_out: Optional[nl.NkiTensor] = None, packed_block_expert: Optional[nl.NkiTensor] = None, packed_block_row_start: Optional[nl.NkiTensor] = None)

   Shard-on-I blockwise MoE forward over the packed rows of one or more experts.

   :param hidden_states: [T, H], Packed rows of the expert on HBM. Rows past the valid count of a block are ignored.
   :param expert_affinities: [T, 1], Affinity per row, applied after the down projection, or None for an affinity of one on every row: the down projection is the output as is, with no affinity load or multiply, and there is no down checkpoint (the backward reads it only for the affinity gradient).
   :param gate_up_proj_weight: [E, H, 2, I_TP], Gate and up projection weights.
   :param down_proj_weight: [E, I_TP, H], Down projection weights.
   :param packed_valid_token_count: [N, 1] int32, Valid rows of each block.
   :param packed_active_block_count: [1, 1] int32, Number of leading blocks with valid rows. Only these blocks run.
   :param block_size: B.
   :type block_size: ``int``
   :param activation_function: SiLU with a gate projection, or SquaredReLU without one.
   :type activation_function: ``ActFnType``
   :param skip_gate_proj: Skip the gate projection (SquaredReLU only).
   :type skip_gate_proj: ``bool``
   :param compute_dtype: Matmul input dtype.
   :param accumulation_dtype: Dtype of the cross-core down-projection reduction; defaults to the hidden-state dtype.
   :param checkpoint_activation: Also return the gate/up projections and, with affinities, the down projection before the affinity, for the backward. output_out, gate_up_activations_T_out, down_activations_out (nl.NkiTensor, optional): Caller-owned buffers for the output [T, H] and the checkpoints [N, 2, I_TP, B] and [N, B, H] (with affinities only). Blocks that do not run are left unchanged. packed_block_expert, packed_block_row_start (nl.NkiTensor, optional): [N, 1] int32, Expert and first row of each block, required when E > 1. Blocks run in table order and each writes all B output rows from its first row, so the rows of an expert's last block past its valid count fall on the next expert's first rows, which the next expert's blocks then overwrite. The tables are read on device and not checked there; the active blocks must satisfy: blocks in expert order and, within an expert, in row order, each expert's blocks consecutive (sorted, contiguous packed_block_expert); 0 <= expert < E; and row_start + B <= T (keep B rows of capacity past the last valid row). T >= B is checked at trace time.
   :type checkpoint_activation: ``bool``

   **Notes**:

   * Without block tables, E must be 1 and T a multiple of B.
   * No projection bias, quantization or clamping.

   **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
