This document is relevant for: Trn2, Trn3
Blockwise MM Backward MXFP8 Kernel API Reference#
MXFP8 backward pass for blockwise Mixture of Experts.
Computes gradients for all parameters in a Mixture of Experts layer using MXFP8 quantized matrix multiplication. Processes tokens in blocks assigned to specific experts. Only weights (gate_up_proj_weight, down_proj_weight) support pre-quantized MXFP8 inputs. Activations (hidden_states, output_hidden_states_grad) must be BF16 because they are gathered per-block via indirect DMA using token indices, which breaks MXFP8 32-element quantization group alignment.
Background#
The blockwise_mm_bwd_mxfp8 kernel computes gradients for all parameters of a blockwise Mixture of Experts layer using MXFP8 quantized matrix multiplication, processing tokens in blocks assigned to specific experts. Only the weights support pre-quantized MXFP8 inputs; activations remain BF16 because per-block indirect-DMA gathers break MXFP8 32-element group alignment.
API Reference#
Source code for this kernel API can be found at: blockwise_mm_backward_mxfp8.py
blockwise_mm_bwd_mxfp8#
- nkilib.experimental.moe_mxfp8.bwd.blockwise_mm_bwd_mxfp8(hidden_states: nl.NkiTensor, expert_affinities_masked: nl.NkiTensor, gate_up_proj_weight: nl.NkiTensor, down_proj_weight: nl.NkiTensor, token_position_to_id: nl.NkiTensor, block_to_expert: nl.NkiTensor, output_hidden_states_grad: nl.NkiTensor, block_size: int = 4096, gate_up_proj_act_checkpoint_T: Optional[nl.NkiTensor] = None, gate_act_checkpoint_T: nl.NkiTensor = None, intermediate_checkpoint_T: nl.NkiTensor = None, scaled_intermediate_checkpoint_T: nl.NkiTensor = None, down_proj_act_checkpoint: Optional[nl.NkiTensor] = None, config: Optional[MXFP8MOEBwdConfig] = None, gate_up_weight_scales: Optional[nl.NkiTensor] = None, down_weight_scales: Optional[nl.NkiTensor] = None, num_valid_tokens: nl.NkiTensor = None, token_offset: nl.NkiTensor = None, down_proj_bias: nl.NkiTensor = None, hidden_states_grad_out: nl.NkiTensor = None, expert_affinities_masked_grad_out: nl.NkiTensor = None, gate_up_proj_weight_grad_out: nl.NkiTensor = None, down_proj_weight_grad_out: nl.NkiTensor = None, gate_and_up_proj_bias_grad_out: nl.NkiTensor = None, down_proj_bias_grad_out: nl.NkiTensor = None) tuple#
MXFP8 backward pass for blockwise Mixture of Experts.
- Parameters:
hidden_states (
nl.NkiTensor) – [T, H], Input hidden states (BF16) on HBM.expert_affinities_masked (
nl.NkiTensor) – [T * E, 1], Expert affinities on HBM.gate_up_proj_weight (
nl.NkiTensor) – [E, H, 2, I_TP], Gate/up projection weights on HBM.down_proj_weight (
nl.NkiTensor) – [E, I_TP, H], Down projection weights on HBM.token_position_to_id (
nl.NkiTensor) – [N * B], Token position to block mapping.block_to_expert (
nl.NkiTensor) – [N, 1], Expert index per block.output_hidden_states_grad (
nl.NkiTensor) – [T, H], Upstream gradient (BF16) from output.block_size (
int) – Number of tokens per block (128, 256, 512, or 1024).gate_up_proj_act_checkpoint_T (
Optional[nl.NkiTensor]) – [N, 2, I_TP, B], Checkpointed gate/up activations (gate_pre = checkpoint[block, 0], up = checkpoint[block, 1]). If None, gate_act_checkpoint_T and intermediate_checkpoint_T must be provided so the kernel can avoid recomputing from this checkpoint.gate_act_checkpoint_T (
nl.NkiTensor) – [N, I_TP, B], Pre-computed SiLU(gate_pre). If None, recomputed per-block as SiLU(gate_up_proj_act_checkpoint_T[block, 0]).intermediate_checkpoint_T (
nl.NkiTensor) – [N, I_TP, B], Pre-computed gate_act * up. If None, recomputed per-block as gate_act * up. Used for Phase 4 (dW_down).scaled_intermediate_checkpoint_T (
nl.NkiTensor) – [N, I_TP, B], Pre-computed intermediate * expert_affinity (Affinity I mode), saved from the forward pass. If provided, Phase 4 reads its per-block slice directly as the dW_down RHS. If None, Phase 4 reuses Phase 1’s scaled_intermediate (already EA-scaled under AFFINITY_ON_I) and transposes it inline — no separate recompute.down_proj_act_checkpoint (
Optional[nl.NkiTensor]) – [N, B, H], Pre-computed output_grad * expert_affinity (Affinity H mode). If None, recomputed per-block as output_grad[block] * ea_scale. Used for Phase 1 when affinity_option=AFFINITY_ON_H.config (
MXFP8MOEBwdConfig) – fused backward configuration carrying every non-tensor knob — compute/quant dtypes, activation, sharding + affinity placement, the four per-phase matmul configs (which own their own blocking, spill/reload and scale packing), the P3/P4 transpose modes, the per-operand swizzle modes, clamp limits, skip-DMA mode, and the accumulation / grad-init / single-expert-dense / fast-DMA flags. When None a defaultMXFP8MOEBwdConfig()is used, so the framework can call the kernel with only the tensors +block_size. SeeMXFP8MOEBwdConfigfor per-field docs and defaults.gate_up_weight_scales (
nl.NkiTensor) – MXFP8 scales for pre-quantized gate/up weights.down_weight_scales (
nl.NkiTensor) – MXFP8 scales for pre-quantized down weights.num_valid_tokens (
nl.NkiTensor) – [1] int32, how many leading rows of hidden_states hold real tokens. Enables the hardware dynamic block loop. Real tokens MUST be the leading rows. Values above T are clamped to T. Requires single_expert_dense, and is unsupported alongside a scaled-intermediate checkpoint or bias gradients. hidden_states_grad rows and expert_affinities_masked_grad entries past the effective count are UNDEFINED; give the forward the same count.token_offset (
nl.NkiTensor) – [E] int32, the forward’s window starts, one per expert; requires ExecutionMode.DENSE_MOE and config.num_static_block >= 1 (the window length in blocks). Pass the same values, num_valid_tokens and config.num_static_block as the forward. With one entry, gradient rows outside the window are UNDEFINED. With E > 1 (ascending offsets), each expert runs over its window in turn, writing its rows into hidden_states_grad (hidden_states_grad_out when given) as the forward does its output. expert_affinities_masked_grad is [E, T, 1], zero outside the rows each expert owns. The weight gradients are written per expert straight into the*_grad_outbuffers when given. The checkpoints and weight scales go to each expert’s call by expert. Bias is unsupported (dense).down_proj_bias (
nl.NkiTensor) – [E, H] down bias the forward applied, required exactly when config.bias=True. The forward scales x @ W_down + b by the affinity, so the affinity gradient adds dY . b for each token’s expert.hidden_states_grad_out (
nl.NkiTensor) – [T, H] buffer, in hidden_states’ dtype, that receives (and is returned as) hidden_states_grad.expert_affinities_masked_grad_out (
nl.NkiTensor) – [T * E, 1] buffer, in expert_affinities_masked’s dtype, for expert_affinities_masked_grad.gate_up_proj_weight_grad_out (
nl.NkiTensor) – [E, H, 2, I_TP] BF16 buffer for gate_up_proj_weight_grad.down_proj_weight_grad_out (
nl.NkiTensor) – [E, I_TP, H] BF16 buffer for down_proj_weight_grad.gate_and_up_proj_bias_grad_out (
nl.NkiTensor) – [E, 2, I_TP] buffer, in config.compute_dtype, for gate_and_up_proj_bias_grad (config.bias=True).down_proj_bias_grad_out (
nl.NkiTensor) – [E, H] buffer, in config.compute_dtype, for down_proj_bias_grad (config.bias=True). Routed execution accumulates every block into the*_grad_outbuffers, so they must start at zero. With config.skip_grad_initialization (routed only) the caller zeroes them, and every gradient the call returns needs one; otherwise the kernel does. Dense execution initializes each gradient from its first block, so its buffers need no zeroing. MLP mode takes no affinity-gradient buffer.
- Returns:
Gradient tensors: - hidden_states_grad (nl.NkiTensor): [T, H], Gradient for hidden states. - expert_affinities_masked_grad (nl.NkiTensor): [T * E, 1], Gradient for affinities. - gate_up_proj_weight_grad (nl.NkiTensor): [E, H, 2, I_TP], Gradient for gate/up weights. - down_proj_weight_grad (nl.NkiTensor): [E, I_TP, H], Gradient for down weights. - gate_and_up_proj_bias_grad (nl.NkiTensor, optional): [E, 2, I_TP], if bias=True. - down_proj_bias_grad (nl.NkiTensor, optional): [E, H], if bias=True.
- Return type:
tuple
Dimensions:
T: Total number of input tokens (after linearizing across batch dimension)
H: Hidden dimension size
I_TP: Intermediate size / tensor parallel degree
E: Number of experts
B: Number of tokens per block (block_size)
N: Total number of blocks (
(T*TopK - (E-1)) / B + E - 1)
This document is relevant for: Trn2, Trn3