koboldcpp/models/templates
Piotr Wilkin (ilintar) ad1de39e07
model: add Kimi-K3 text model (#26185)
* model: add Kimi-K3 text model

Hybrid KDA (linear) + MLA (full) attention as in Kimi-Linear-48B, plus five
things that architecture does not have:

  1. cross-layer residual attention  (attn_res_block_size)
  2. latent MoE                      (routed experts run at n_expert_latent)
  3. situ activation                 (replaces SwiGLU everywhere)
  4. MLA output gate                 (sigmoid gate before o_proj)
  5. full-rank KDA gate              (single ssm_g instead of ssm_g_a/ssm_g_b)

K3's text_config reports KimiLinearForCausalLM - the older 48B architecture -
so get_model_architecture routes on the top-level name instead.

The KDA decay gate has two forms, selected by linear_attn_config's
gate_lower_bound. It is not a clamp: when set it swaps the activation entirely
(fla/ops/kda/gate.py), from -exp(A_log)*softplus(x) to
lower_bound*sigmoid(exp(A_log)*x). K3 sets it to -5.0; kimi-linear leaves it
unset, so that path is unchanged.

Cross-layer residuals reuse ggml_dsv4_hc_pre for the weighted sum. That op is
CPU + CUDA only, so Metal/Vulkan will fall back per-node until those kernels
exist.

The routed experts ship as compressed-tensors "mxfp4-pack-quantized". That is
bit-compatible with ggml's MXFP4 - same E2M1 code assignment, same E8M0 scale
byte, only the nibble positions within a block differ - so they are repacked
rather than dequantized, losslessly and without a ~5.5 TB bf16 round-trip.
The repack is built lazily because gguf_writer holds every added tensor until
the final write. DeepSeek-V4 was already doing the identical bit-shuffling, so
it now shares the helper.

Verified against Moonshot's own code path (transformers + fla's Triton KDA
kernels) on a tiny model exercising every K3-specific feature. Final-position
logits vs the fp32 reference: 6.7e-05 rel / corr 1.00000000 for both the
chunked and the recurrent delta-net path. MXFP4 blocks dequantize to the source
weights with 0.0e+00 error.

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* model: fix ty errors in the Kimi-K3 converter

- `_res_parts` buffers (kind, tensor) pairs, not bare tensors
- `get_tensors` must return an Iterator, matching ModelBase
- LazyBase's `func` takes one argument, so pass the expert loaders through
  `args` instead of the closure
- borrowing KimiLinearModel.set_vocab from an unrelated TextModel is
  deliberate and safe, but not expressible in the signature

No behaviour change: the MXFP4 repack still dequantizes to the source weights
with 0.0e+00 error and end-to-end logits are unchanged (8.386e-03 rel,
corr 0.99996630).

Assisted-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* Update conversion/kimi_k3.py

Co-authored-by: Boris Dvorkin  <b_dvorkin@niuitmo.ru>

* Increase LLAMA_MAX_EXPERTS from 512 to 1024

* tests : support for Kimi K3 in archs test

* chat : add Kimi K3 chat format (reasoning, content, typed tool calls)

K3's assistant output is an XTML-ish tagged format built by the template's
open_tag/close_tag macros. Two properties break generic parsing:

1. The generation prompt ends with open_tag('think'), so the completion
   starts inside the think section with no opening marker in the output
   (thinking_forced_open).
2. Only <|open|>/<|close|>/<|sep|>/<|end_of_msg|> are special tokens; tag
   names ("think", "response", "message") are ordinary text tokens.

Adds common_chat_params_init_kimi_k3 (PEG_NATIVE) with detection on the
marker trio, reasoning extraction, response unwrapping, and tool-call
parsing of the tools/call/argument tag structure with argument types
taken from the tool schema. Includes the K3 chat template fixture and 9
test-chat cases derived from real generations of the full 2.8T model.

Verified end-to-end against Kimi-K3-Q2_K (GrEarl/Kimi-K3-GGUF) on 8x B200:
content, reasoning_content, streaming deltas, and tool_calls all correct;
finish_reason stop/tool_calls as appropriate.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chat : add message_delimiters for Kimi K3

Per-role message-start markers for token-level span splitting. User and
assistant messages carry only the role attribute, so their full opener
(through <|sep|>) is used; system and tool messages continue with more
attributes (type=/tool=/index=), so those delimiters stop after the
role's closing quote. Verified against the K3 tiktoken vocabulary that
the closing quote is always a standalone token across all attribute
variants, so the token-level prefix match stays exact.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: apply nits from @ngxson and text fixes from @danielhanchen

* tests : added missing hyperparameters and tensors for Kimi K3 in test-llama-archs

* chore : move overly verbose header file comments to Kimi K3 source file

* tests : re-enabled KIMI_K3 in test-llama-archs for WebGPU backend

* model-saver : emit kda_gate_lower_bound for Kimi K3

Quick fix. The Kimi K3 loader reads kda_gate_lower_bound and gates a graph branch on it (it scales the KDA gate when the bound is above -INFINITY), but the model
saver never wrote the key, so a save->load roundtrip silently dropped it back to the -INFINITY default and changed the model's output. The real K3 config sets gate_lower_bound = -5.0.

I propose to emit it from the saver, and set it to -5.0 in the test-llama-archs K3 case so the roundtrip check exercises it (the roundtrip fails without the saver line).

* Refactor conditional for model architecture check

* tests : re-enabled (again) KIMI_K3 and MINIMAX_M3 in test-llama-archs for WebGPU backend

* fix code comments

* add template on conversion

* move repack_mxfp4_blocks to model base

* nits

* add_value_length

* optimize res_stack construction

* nits

---------

Co-authored-by: Boris Dvorkin <b_dvorkin@niuitmo.ru>
Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
Co-authored-by: Deepankar Singh <singh.deepankar39@gmail.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Caleb DeLeeuw <caleb.deleeuw@gmail.com>
Co-authored-by: Xuan Son Nguyen <son@huggingface.co>
2026-08-15 17:11:05 +02:00
..
Apertus-8B-Instruct.jinja Autoparser - complete refactoring of parser architecture (#18675) 2026-03-06 21:01:00 +01:00
Apriel-1.6-15b-Thinker-fixed.jinja common/parser: add proper reasoning tag prefill reading (#20424) 2026-03-19 16:58:21 +01:00
Bielik-11B-v3.0-Instruct.jinja Autoparser - complete refactoring of parser architecture (#18675) 2026-03-06 21:01:00 +01:00
ByteDance-Seed-OSS.jinja chat : Seed OSS thinking + tool call support (#15552) 2025-08-29 14:53:41 +02:00
Cohere2MoE.jinja chat: add dedicated Cohere2MoE (North Code) parser (#24615) 2026-06-14 20:17:40 +02:00
CohereForAI-c4ai-command-r-plus-tool_use.jinja
CohereForAI-c4ai-command-r7b-12-2024-tool_use.jinja Autoparser - complete refactoring of parser architecture (#18675) 2026-03-06 21:01:00 +01:00
deepseek-ai-DeepSeek-R1-Distill-Llama-8B.jinja Autoparser - complete refactoring of parser architecture (#18675) 2026-03-06 21:01:00 +01:00
deepseek-ai-DeepSeek-R1-Distill-Qwen-32B.jinja common/parser: add proper reasoning tag prefill reading (#20424) 2026-03-19 16:58:21 +01:00
deepseek-ai-DeepSeek-V3.1.jinja common/parser: add proper reasoning tag prefill reading (#20424) 2026-03-19 16:58:21 +01:00
deepseek-ai-DeepSeek-V3.2.jinja chat: dedicated DeepSeek v3.2 parser + "official" template (#21785) 2026-04-13 22:23:53 +02:00
deepseek-ai-DeepSeek-V4-Flash-0731.jinja chat : add new template for DeepSeek V4 Flash 0731 (#26398) 2026-08-03 17:59:11 -05:00
deepseek-ai-DeepSeek-V4.jinja chat : add new template for DeepSeek V4 Flash 0731 (#26398) 2026-08-03 17:59:11 -05:00
fireworks-ai-llama-3-firefunction-v2.jinja Autoparser - complete refactoring of parser architecture (#18675) 2026-03-06 21:01:00 +01:00
GigaChat3-10B-A1.8B.jinja common/parser: add GigaChatV3/3.1 models support (#19931) 2026-03-12 01:22:25 +01:00
GigaChat3.1-10B-A1.8B.jinja common/parser: add GigaChatV3/3.1 models support (#19931) 2026-03-12 01:22:25 +01:00
GLM-4.6.jinja common : Generalized XML-style tool-call parsing with streaming support (GLM 4.5/4.6 + MiniMax M2 + SeedOSS + Kimi-K2 + Qwen3-Coder + Apriel-1.5 + Xiaomi-MiMo) (#16932) 2025-11-18 18:54:15 +01:00
GLM-4.7-Flash.jinja Autoparser - complete refactoring of parser architecture (#18675) 2026-03-06 21:01:00 +01:00
google-gemma-2-2b-it.jinja
google-gemma-4-31B-it-interleaved.jinja common : better align to the updated official gemma4 template (#21704) 2026-04-10 16:12:53 -05:00
google-gemma-4-31B-it.jinja common : better align to the updated official gemma4 template (#21704) 2026-04-10 16:12:53 -05:00
HuggingFaceTB-SmolLM3-3B.jinja common/autoparser : detect reasoning markers when enable_thinking changes system prompt (#20859) 2026-03-23 08:35:27 +01:00
ibm-granite-granite-3.3-2B-Instruct.jinja chat : support Granite model reasoning and tool call (#14864) 2025-08-06 20:27:30 +02:00
ibm-granite-granite-4.0.jinja chat : add Granite 4.0 chat template with correct tool_call role mapping (#20804) 2026-04-02 11:28:56 +02:00
ibm-granite-granite-4.1.jinja chat : add Granite 4.1 chat template (#23518) 2026-05-28 13:13:33 +02:00
Kimi-K2-Instruct.jinja Fix Kimi-K2 tool-call parsing issues (#17376) 2025-12-08 14:32:04 +01:00
Kimi-K2-Thinking.jinja Fix Kimi-K2 tool-call parsing issues (#17376) 2025-12-08 14:32:04 +01:00
Kimi-K3.jinja model: add Kimi-K3 text model (#26185) 2026-08-15 17:11:05 +02:00
LFM2-8B-A1B.jinja PEG parser for LFM2 (#20251) 2026-03-09 01:11:22 +01:00
LFM2.5-8B-A1B.jinja common/chat : fix LFM2/LFM2.5 reasoning round-trip and <think> leak (#24234) 2026-06-06 22:39:21 +02:00
LFM2.5-Instruct.jinja fix: tool call parsing for LFM2 and LFM2.5 models (#21242) 2026-04-01 16:22:44 +02:00
llama-cpp-deepseek-r1.jinja common/parser: add proper reasoning tag prefill reading (#20424) 2026-03-19 16:58:21 +01:00
llama-cpp-rwkv-world.jinja llama : add jinja template for rwkv-world (#14665) 2025-07-14 07:43:43 +08:00
meetkai-functionary-medium-v3.1.jinja common/parser: add proper reasoning tag prefill reading (#20424) 2026-03-19 16:58:21 +01:00
meetkai-functionary-medium-v3.2.jinja
meta-llama-Llama-3.1-8B-Instruct.jinja
meta-llama-Llama-3.2-3B-Instruct.jinja
meta-llama-Llama-3.3-70B-Instruct.jinja
microsoft-Phi-3.5-mini-instruct.jinja
MiMo-VL.jinja common : Generalized XML-style tool-call parsing with streaming support (GLM 4.5/4.6 + MiniMax M2 + SeedOSS + Kimi-K2 + Qwen3-Coder + Apriel-1.5 + Xiaomi-MiMo) (#16932) 2025-11-18 18:54:15 +01:00
MiniMax-M1.jinja model : add support for MiniMaxText01ForCausalLM and MiniMaxM1ForCausalLM (#27018) 2026-08-15 00:02:38 +02:00
MiniMax-M2.jinja common : Generalized XML-style tool-call parsing with streaming support (GLM 4.5/4.6 + MiniMax M2 + SeedOSS + Kimi-K2 + Qwen3-Coder + Apriel-1.5 + Xiaomi-MiMo) (#16932) 2025-11-18 18:54:15 +01:00
MiniMax-M3.jinja common/chat: add specialized minimax m3 parser (#26210) 2026-07-28 04:27:20 -05:00
Mistral-Small-3.2-24B-Instruct-2506.jinja jinja : Add Mistral-Small-3.2-24B-Instruct-2506.jinja (#14349) 2025-06-24 09:17:58 +03:00
mistralai-Ministral-3-14B-Reasoning-2512.jinja common : add parser for ministral/mistral large 3/devstral 2 (#17713) 2025-12-09 17:31:04 -06:00
mistralai-Mistral-Nemo-Instruct-2407.jinja
moonshotai-Kimi-K2.jinja Autoparser - complete refactoring of parser architecture (#18675) 2026-03-06 21:01:00 +01:00
muse-glimmer.jinja chat : fix muse-glimmer detection of tool calls after EOM (#26879) 2026-08-11 15:15:20 -05:00
NousResearch-Hermes-2-Pro-Llama-3-8B-tool_use.jinja
NousResearch-Hermes-3-Llama-3.1-8B-tool_use.jinja
NVIDIA-Nemotron-3-Nano-30B-A3B-BF16.jinja common : implement new jinja template engine (#18462) 2026-01-16 11:22:06 +01:00
NVIDIA-Nemotron-Nano-v2.jinja chat : nemotron thinking & toolcalling support (#15676) 2025-09-05 01:22:22 +02:00
openai-gpt-oss-120b.jinja gpt-oss: implement harmony parsing (#15181) 2025-08-14 17:23:11 +03:00
openbmb-MiniCPM5-1B.jinja chat : implement minicpm5 parser (#24889) 2026-06-28 16:53:32 +02:00
poolside-Laguna-S-2.1.jinja chat : Align Laguna-S-2.1 chat template to huggingface (#26232) 2026-08-10 05:20:59 -05:00
poolside-Laguna-XS-2.1.jinja Add support for Laguna XS.2 & M.1 (#25165) 2026-07-22 09:54:08 +08:00
poolside-Laguna-XS.2.jinja Add support for Laguna XS.2 & M.1 (#25165) 2026-07-22 09:54:08 +08:00
Qwen-Qwen2.5-7B-Instruct.jinja
Qwen-Qwen3-0.6B.jinja
Qwen-QwQ-32B.jinja Autoparser - complete refactoring of parser architecture (#18675) 2026-03-06 21:01:00 +01:00
Qwen3-Coder.jinja Autoparser - complete refactoring of parser architecture (#18675) 2026-03-06 21:01:00 +01:00
Qwen3.5-4B.jinja common/parser: fix handling of tool definition with missing properties key (#21128) 2026-03-28 20:41:32 +01:00
README.md chat : Deepseek V3.1 reasoning and tool calling support (OpenAI Style) (#15533) 2025-09-08 16:59:48 +02:00
Reka-Edge.jinja autoparser: support case of JSON_NATIVE with per-call markers (test case: Reka-Edge) (#21892) 2026-04-15 10:51:50 +02:00
StepFun3.5-Flash.jinja Autoparser - complete refactoring of parser architecture (#18675) 2026-03-06 21:01:00 +01:00
tencent-Hy3.jinja model: add Hy3 (hy_v3) support with MTP speculative decoding (#25395) 2026-07-14 00:31:04 +02:00
unsloth-Apriel-1.5.jinja Autoparser - complete refactoring of parser architecture (#18675) 2026-03-06 21:01:00 +01:00
unsloth-mistral-Devstral-Small-2507.jinja mtmd : add support for Voxtral (#14862) 2025-07-28 15:01:48 +02:00
upstage-Solar-Open-100B.jinja chat : add parsing for solar-open-100b (#18540) 2026-01-29 16:06:15 +01:00

These templates can be updated with the following commands:

./scripts/get_chat_template.py CohereForAI/c4ai-command-r-plus tool_use      > models/templates/CohereForAI-c4ai-command-r-plus-tool_use.jinja
./scripts/get_chat_template.py CohereForAI/c4ai-command-r7b-12-2024 default  > models/templates/CohereForAI-c4ai-command-r7b-12-2024-default.jinja
./scripts/get_chat_template.py CohereForAI/c4ai-command-r7b-12-2024 rag      > models/templates/CohereForAI-c4ai-command-r7b-12-2024-rag.jinja
./scripts/get_chat_template.py CohereForAI/c4ai-command-r7b-12-2024 tool_use > models/templates/CohereForAI-c4ai-command-r7b-12-2024-tool_use.jinja
./scripts/get_chat_template.py deepseek-ai/DeepSeek-R1-Distill-Llama-8B      > models/templates/deepseek-ai-DeepSeek-R1-Distill-Llama-8B.jinja
./scripts/get_chat_template.py deepseek-ai/DeepSeek-R1-Distill-Qwen-32B      > models/templates/deepseek-ai-DeepSeek-R1-Distill-Qwen-32B.jinja
./scripts/get_chat_template.py fireworks-ai/llama-3-firefunction-v2          > models/templates/fireworks-ai-llama-3-firefunction-v2.jinja
./scripts/get_chat_template.py google/gemma-2-2b-it                          > models/templates/google-gemma-2-2b-it.jinja
./scripts/get_chat_template.py meetkai/functionary-medium-v3.1               > models/templates/meetkai-functionary-medium-v3.1.jinja
./scripts/get_chat_template.py meetkai/functionary-medium-v3.2               > models/templates/meetkai-functionary-medium-v3.2.jinja
./scripts/get_chat_template.py meta-llama/Llama-3.1-8B-Instruct              > models/templates/meta-llama-Llama-3.1-8B-Instruct.jinja
./scripts/get_chat_template.py meta-llama/Llama-3.2-3B-Instruct              > models/templates/meta-llama-Llama-3.2-3B-Instruct.jinja
./scripts/get_chat_template.py meta-llama/Llama-3.3-70B-Instruct             > models/templates/meta-llama-Llama-3.3-70B-Instruct.jinja
./scripts/get_chat_template.py microsoft/Phi-3.5-mini-instruct               > models/templates/microsoft-Phi-3.5-mini-instruct.jinja
./scripts/get_chat_template.py mistralai/Mistral-Nemo-Instruct-2407          > models/templates/mistralai-Mistral-Nemo-Instruct-2407.jinja
./scripts/get_chat_template.py NousResearch/Hermes-2-Pro-Llama-3-8B tool_use > models/templates/NousResearch-Hermes-2-Pro-Llama-3-8B-tool_use.jinja
./scripts/get_chat_template.py NousResearch/Hermes-3-Llama-3.1-8B tool_use   > models/templates/NousResearch-Hermes-3-Llama-3.1-8B-tool_use.jinja
./scripts/get_chat_template.py Qwen/Qwen2.5-7B-Instruct                      > models/templates/Qwen-Qwen2.5-7B-Instruct.jinja
./scripts/get_chat_template.py Qwen/QwQ-32B                                  > models/templates/Qwen-QwQ-32B.jinja
./scripts/get_chat_template.py Qwen/Qwen3-0.6B                               > models/templates/Qwen-Qwen3-0.6B.jinja
./scripts/get_chat_template.py zai-org/GLM-4.5                               > models/templates/zai-org-GLM-4.5.jinja
./scripts/get_chat_template.py deepseek-ai/DeepSeek-V3.1                     > models/templates/deepseek-ai-DeepSeek-V3.1.jinja