Expand description
Loads ferrox-models::kimi_decoder weights from Kimi K3’s real
safetensors checkpoint (via ferrox-safetensors::ShardedSafetensors),
using the exact real tensor names/shapes/dtypes fetched live from
huggingface.co/moonshotai/Kimi-K3 (a real shard header, not
guessed) – confirmed to match this crate’s kda/mla/latent_moe/
kimi_decoder struct field names and shapes exactly.
One real, non-obvious fact confirmed by reading actual tensor shapes
rather than assuming they match modeling_kimi_linear.py’s
KimiDeltaAttention.__init__ literally: self_attn.A_log’s real
on-disk shape is [128], not [num_heads] = [96] (confirmed
independently by self_attn.b_proj.weight’s real shape [96, 7168], which is unambiguously [num_heads, hidden_dim]). The real
fused_recurrent_kda kernel only ever indexes A_log[i_hv] for
i_hv in 0..num_heads, so this is real, harmless padding (likely
to a GPU-friendly round size) rather than a spec mismatch – this
loader reads the real 128-element tensor but only uses its first
num_heads elements, matching what the real kernel actually
consumes.
Dequantizes small F32/BF16 tensors (per-head attention
parameters, layernorms, dense-FFN and shared-expert projections) to
owned f32 eagerly at load time – matching this project’s
established BF16-handling convention, e.g. ferrox-models::loader’s
GGUF path – since these are cheap regardless. Routed-expert MXFP4
weights are the one format this loader does not eagerly
dequantize: load_mxfp4_weight_matrix builds a zero-copy
WeightMatrix::Mxfp4 (mmap-backed packed/scale buffers, real
Kimi K3 stores these as two separate tensors per projection – see
ferrox_quant::dot_mxfp4_row_f32’s doc comment), matching the
zero-copy-mmap-plus-fused-dot discipline every other quantized
format in this codebase already uses. This is a real fix, not just a
design preference: real-hardware testing (rented 62GB instance, see
docs/MODELS.md) found that eagerly dequantizing all 896 of a real MoE
layer’s routed experts to owned f32 needs roughly 117GB of RAM and
reproducibly OOM-killed a 62GB rented instance – the zero-copy path
here keeps a loaded layer’s resident memory close to its on-disk
size (the real MXFP4 packing ratio: 2 values/byte plus one scale
byte per 32 values) instead of expanding every value to 4 bytes
whether it’s ever used by real top-k routing or not.
Structs§
- Block
Residual Weights - The four block-residual weight vectors real Kimi K3 attaches to
every layer (
{prefix}.self_attention_res_{norm,proj}.weight,{prefix}.mlp_res_{norm,proj}.weight). Real on-disk*_proj.weightshape is[1, hidden_dim](aLinear(hidden_dim, 1)’s weight); the raw bytes are already exactly[hidden_dim]flattened. - Kimi
Expert Source ExpertSourceover a Kimi safetensors checkpoint: each expert’s six tensors (three matrices’ packed+scale buffers) are read positionally from the owning shard files and concatenated inKimiStoredExpertLayout’s fixed order.- Kimi
Real Hparams - Kimi K3’s real per-layer hyperparameters needed to load any layer
(not tied to
ferrox_moe::MoeLayerConfig/ferrox_models::ModelConfig, neither of which model the “latent MoE” down-projected dimension or the dense leading layer’s own intermediate size – kept as a small, dedicated struct here rather than widening those shared types for one model’s real values). - Kimi
Stored Expert Layout - Loads one routed expert’s real MXFP4 weights
(
{prefix}.experts.{expert_idx}.{w1,w2,w3}.{weight_packed,weight_scale}).moe_hidden_dimis the real latent dimension (routed_expert_hidden_size=3584 for Kimi K3, not the outerhidden_dim=7168 – seeferrox-models::latent_moe’s module doc comment);moe_intermediate_dimis the per-expert FFN size (3072). Per-layer byte layout of one store-backed Kimi routed expert’s combined buffer: w1_packed, w1_scale, w2_packed, w2_scale, w3_packed, w3_scale concatenated in that fixed order. Every expert in a Kimi layer has identical dims, so one layout serves the layer.
Enums§
Functions§
- load_
block_ residual - load_
dense_ mlp - Loads the dense leading layer’s feed-forward block (real tensor
names under
{prefix}.mlp.*) – Kimi K3’s layer 0 only (first_k_dense_replace=1). - load_
f32_ vec - Reads any real tensor as an owned
f32vector, dispatching on its real declared dtype (F32direct,BF16dequantized) – exposedpubsince not every real weight (e.g. the per-layerinput_layernorm.weight/post_attention_layernorm.weight, which aren’t nested insideKdaAttnWeights/MlaAttnWeights/DenseMlpWeights/BlockResidualWeights) has a dedicated loader function above. - load_
kda_ attn - Loads one KDA-attention layer’s weights (real tensor names under
{prefix}.self_attn.*).num_heads/head_dim/hidden_dimmust match the real config (Kimi K3: 96/128/7168) – passed explicitly rather than hardcoded so this loader can also be exercised against small synthetic on-disk fixtures in tests. - load_
kimi_ checkpoint - load_
kimi_ checkpoint_ with_ expert_ cache - Loads a complete
KimiDecoderWeights– every one ofmodel_cfg’s real layers (dispatched per-layer viamodel_cfg.layer_attention_kind/layer_is_dense, driven byhp’s per-layer dimensions), plus the real top-level tensors (real names confirmed against a real shard header:language_model.model.embed_tokens.weight,language_model.lm_head.weight,language_model.model.norm.weight,language_model.model.output_attn_res_{norm,proj}.weight). This is the assembly stepload_kimi_layeritself doesn’t do – calling it once per real layer and building the surroundingKimiDecoderWeights– analogous toferrox-models::loader::Decoder::from_gguf, but for Kimi K3’s real safetensors format. Not blocked on anything (the zero-copy MXFP4 fix removes the memory obstacle a full loader would otherwise hit for every non-dense layer’s routed experts); simply not runnable against the real 2.8T-parameter checkpoint in this environment (96 shards, 1.56TB) – tested here against small synthetic on-disk fixtures instead, real safetensors bytes and real tensor names throughout. Likeload_kimi_checkpoint, but withexpert_cache_bytes: Some(budget)every MoE layer’s routed experts are converted to store-backed lazy materialization after loading: one bounded, lease-protectedExpertStoreshared by the whole model reads each expert’s six tensors positionally from the owning shard files on miss, instead of holding 896 expert objects per layer resident. Attention, dense layers, shared experts, router/projections, embeddings, and the output head are untouched. Bit-identical to the eager path (same bytes, same kernels) – pinned by the equivalence test against the synthetic multi-layer checkpoint. - load_
kimi_ expert - load_
kimi_ layer - Loads any one layer (KDA or Gated-MLA attention, dense or MoE FFN),
dispatching on
kind/is_dense– passModelConfig::layer_attention_kind(layer_idx)/ModelConfig::layer_is_dense(layer_idx)for Kimi K3’s real per-layer topology. - load_
latent_ moe - Loads one full MoE layer: the gate (with its real aux-loss-free
e_score_correction_bias), the shared down/up latent projections + norm, every routed expert (n_experts, real MXFP4), and the shared expert (realBF16, on the fullhidden_dim, not the latent space – seeferrox-models::latent_moe’s module doc comment). - load_
mla_ attn - Loads one Gated-MLA-attention layer’s weights (real tensor names
under
{prefix}.self_attn.*).