Problems
Loading...
Step 8 of 11
MXFP4 Experts
Hard

Kimi K3 stores routed expert matrices in grouped MXFP4 while the shared-expert output remains in higher precision. Let there be TT tokens, EE experts, input width I=32GI=32G, output width OO, and KK selected experts per token. Latent tokens have shape (T,I)(T,I), selected expert indices and mixture weights have shape (T,K)(T,K), and the supplied shared output has shape (T,O)(T,O).

Packed weights have shape (E,O,G,16)(E,O,G,16) and scale bytes have shape (E,O,G)(E,O,G). Each group of sixteen bytes stores 32 E2M1 values. Decode the low nibble before the high nibble in every byte, preserving this interleaved order. Map nibble codes through

[0,12,1,32,2,3,4,6,−0,−12,−1,−32,−2,−3,−4,−6].[0,\tfrac12,1,\tfrac32,2,3,4,6,-0,-\tfrac12,-1,-\tfrac32,-2,-3,-4,-6].

For scale byte ss, multiply all 32 decoded values in that group by

2s−127.2^{s-127}.

Concatenate the GG decoded groups to reconstruct selected expert matrix We∈RO×IW_e\in\mathbb{R}^{O\times I}. For token xtx_t, selected set Tt\mathcal{T}_t, mixture weight pt,ep_{t,e}, and shared output ytsharedy_t^{\mathrm{shared}}, compute

yt=ytshared+∑e∈Ttpt,eWext.y_t=y_t^{\mathrm{shared}}+\sum_{e\in\mathcal{T}_t}p_{t,e}W_ex_t.

Reconstruct only the selected expert matrices. Return the combined output as one tensor of shape (T,O)(T,O).

Loading visualization...

Examples

Input: latent_tokens = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]], packed_weights = [[[[34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34]]]], scale_bytes = [[[127]]], selected_experts = [[0]], mixture_weights = [[1]], shared_output = [[0]]

Output: tensor([[32.0]])

Explanation: Every packed nibble decodes to 1 at unit scale, so the dot product over 32 values is 32.

Input: latent tokens shape (1, 32), packed weights shape (2, 1, 1, 16), selected experts shape (1, 2), shared output shape (1, 1)

Output: tensor of shape (1, 1)

Input: latent tokens shape (1, 64), packed weights shape (2, 2, 2, 16), selected experts shape (1, 1), shared output shape (1, 2)

Output: tensor of shape (1, 2)

Hint 1

Extract each low nibble with a bit mask and each high nibble with a right shift followed by the same mask.

Hint 2

Interleave the decoded low and high values before flattening the 32-value groups.

Hint 3

Convert each scale byte to a power of two and apply it only to its matching group.

Requirements

  • Return one floating-point tensor shaped like the supplied shared output.
  • Decode each weight block with its own E8M0 scale.
  • Preserve the latent token dtype and device in the result.
  • Do not mutate packed weights, routing data, or shared output.

Constraints

  • Input width is a positive multiple of 32.
  • Packed weights have sixteen bytes per 32-value group.
  • Selected expert indices are valid, and mixture weights sum to one per token.
  • Scale bytes and packed values use unsigned 8-bit storage.
Prev8 / 11Next