Skip to content

Commit f341ad9

Browse files
committed
feat(kimi-k3): model config, schema and checkpoint param mapping
Add Kimi-K3 architecture configuration, Pydantic configuration schema, and HuggingFace parameter mapping and transformation hooks. TAG=agy CONV=67891ee2-c466-4bf5-a353-bce763286f24
1 parent a0a7ebf commit f341ad9

11 files changed

Lines changed: 977 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,6 @@ gha-creds-*.json
158158
maxtext.code-workspace
159159
maxtext_output/
160160
maxtext_local_output/
161+
162+
# Kimi-K3 HuggingFace reference checkout used by parity tests (~61MB, fetched locally)
163+
kimi-k3-hf-reference/

‎src/maxtext/checkpoint_conversion/utils/hf_model_configs.py‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,106 @@ def __init__(self, **kwargs):
11421142
deepseek4_284b_config = DeepseekV4Config(**deepseek4_284b_dict)
11431143

11441144

1145+
# from https://huggingface.co/moonshotai/Kimi-K3/blob/main/config.json (`text_config`, flattened).
1146+
# The release stores routed experts as compressed-tensors `mxfp4-pack-quantized`.
1147+
# `linear_attn_config.full_attn_layers` / `kda_layers` are 1-indexed, as in the HF config;
1148+
# `param_mapping._resolve_kimi_k3_full_attn_layers` converts them to MaxText's 0-index.
1149+
kimi_k3_dict = {
1150+
"architectures": ["KimiLinearForCausalLM"],
1151+
"activation_situ_beta": 4.0,
1152+
"activation_situ_linear_beta": 25.0,
1153+
"attn_res_block_size": 12,
1154+
"bos_token_id": 163584,
1155+
"dtype": "bfloat16",
1156+
"eos_token_id": 163586,
1157+
"first_k_dense_replace": 1,
1158+
"hidden_act": "situ",
1159+
"hidden_size": 7168,
1160+
"initializer_range": 0.02,
1161+
"intermediate_size": 33792,
1162+
"kv_lora_rank": 512,
1163+
"latent_moe_use_norm": True,
1164+
"linear_attn_config": {
1165+
"full_attn_layers": [
1166+
4,
1167+
8,
1168+
12,
1169+
16,
1170+
20,
1171+
24,
1172+
28,
1173+
32,
1174+
36,
1175+
40,
1176+
44,
1177+
48,
1178+
52,
1179+
56,
1180+
60,
1181+
64,
1182+
68,
1183+
72,
1184+
76,
1185+
80,
1186+
84,
1187+
88,
1188+
92,
1189+
93,
1190+
],
1191+
"gate_lower_bound": -5.0,
1192+
"head_dim": 128,
1193+
"kda_layers": [i for i in range(1, 94) if i % 4 != 0 and i != 93],
1194+
"num_heads": 96,
1195+
"short_conv_kernel_size": 4,
1196+
"use_full_rank_gate": True,
1197+
},
1198+
"max_position_embeddings": 1048576,
1199+
"mla_use_nope": True,
1200+
"mla_use_output_gate": True,
1201+
"model_type": "kimi_linear",
1202+
"moe_intermediate_size": 3072,
1203+
"moe_layer_freq": 1,
1204+
"moe_renormalize": True,
1205+
"moe_router_activation_func": "sigmoid",
1206+
"num_attention_heads": 96,
1207+
"num_expert_group": 1,
1208+
"num_experts": 896,
1209+
"num_experts_per_token": 16,
1210+
"num_hidden_layers": 93,
1211+
"num_key_value_heads": 96,
1212+
"num_nextn_predict_layers": 0,
1213+
"num_shared_experts": 2,
1214+
"pad_token_id": 163839,
1215+
"q_lora_rank": 1536,
1216+
"qk_nope_head_dim": 128,
1217+
"qk_rope_head_dim": 64,
1218+
"rms_norm_eps": 1e-05,
1219+
"routed_expert_hidden_size": 3584,
1220+
"routed_scaling_factor": 1.0,
1221+
"tie_word_embeddings": False,
1222+
"topk_group": 1,
1223+
"topk_method": "noaux_tc",
1224+
"transformers_version": "4.56.2",
1225+
"use_cache": True,
1226+
"use_grouped_topk": True,
1227+
"v_head_dim": 128,
1228+
"vocab_size": 163840,
1229+
}
1230+
1231+
1232+
class KimiK3Config(PTConfig): # pyrefly: ignore[invalid-inheritance]
1233+
"""Minimal stand-in for `configuration_kimi_k3.KimiLinearConfig` (not in transformers)."""
1234+
1235+
model_type = "kimi_linear"
1236+
1237+
def __init__(self, **kwargs):
1238+
self.max_position_embeddings = kwargs.get("max_position_embeddings", 1048576)
1239+
super().__init__(**kwargs)
1240+
1241+
1242+
kimi_k3_config = KimiK3Config(**kimi_k3_dict)
1243+
1244+
11451245
# from https://huggingface.co/openai/gpt-oss-20b/blob/main/config.json
11461246
# remove mxfp4 quantization_config, since we are using bf16
11471247
gpt_oss_20b_dict = {
@@ -2027,6 +2127,7 @@ def __init__(self, **kwargs):
20272127
"deepseek3-671b": deepseek3_671b_config,
20282128
"deepseek3.2-671b": deepseek32_671b_config,
20292129
"deepseek4-284b": deepseek4_284b_config,
2130+
"kimi-k3": kimi_k3_config,
20302131
"gpt-oss-20b": gpt_oss_20b_config,
20312132
"gpt-oss-120b": gpt_oss_120b_config,
20322133
"qwen3-omni-30b-a3b": qwen3_omni_30b_a3b_config,

0 commit comments

Comments
 (0)