This document is relevant for: Trn2, Trn3
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
blockwise_mm_bwd_packed#
- nkilib.experimental.moe.bwd.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.
- Parameters:
hidden_states (
nl.NkiTensor) – [T, H], Packed forward input rows.expert_affinities (
nl.NkiTensor) – [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.gate_up_proj_weight (
nl.NkiTensor) – [E, H, 2, I_TP], Gate and up projection weights.down_proj_weight (
nl.NkiTensor) – [E, I_TP, H], Down projection weights.gate_up_proj_act_checkpoint_T (
nl.NkiTensor) – [N, 2, I_TP, B], Forward gate/up checkpoint.down_proj_act_checkpoint (
nl.NkiTensor) – [N, B, H], Forward down projection before the affinity, read only for the affinity gradient: None (or unread) without affinities.output_hidden_states_grad (
nl.NkiTensor) – [T, H], Gradient of the forward output.packed_valid_token_count (
nl.NkiTensor) – [N, 1] int32, Valid rows of each block.packed_active_block_count (
nl.NkiTensor) – [1, 1] int32, Number of leading blocks with valid rows. Only these blocks run.block_size (
int) – B, a multiple of 256.activation_type (
ActFnType) – SiLU with a gate projection, or SquaredReLU without one.skip_gate_proj (
bool) – Skip the gate projection (SquaredReLU only).compute_dtype (
nki.dtype) – Matmul input dtype.accumulation_dtype (
nki.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.clamp_limits (
ClampLimits) – Activation clamp limits.blocking_params (
MOEBwdDroplessBlockingParams) – Tiling of the backward steps.hidden_states_grad_out (
nl.NkiTensor) – Caller-owned [T, H] hidden-state gradient.expert_affinities_grad_out (
nl.NkiTensor) – 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 mustsatisfy – 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.
packed_group_blocks (
int) – Blocks per weight-gradient group (a power of two); each group of an expert accumulates the weight gradients once.gate_up_proj_weight_grad_out (
nl.NkiTensor) – Caller-owned [E, H, 2, I_TP] gate/up weight gradient in the weight dtype.down_proj_weight_grad_out (
nl.NkiTensor) – 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.
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
This document is relevant for: Trn2, Trn3