Skip to content

model: add Kimi-K3.1-Pro text model - #31779

Merged
ngxson merged 6 commits into
ggml-org:masterfrom
usrlocalben:k31-text-model
Nov 6, 2026
Merged

ngxson merged 6 commits into
ggml-org:masterfrom
usrlocalben:k31-text-model

Conversation

@usrlocalben

@usrlocalben usrlocalben commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

K3.1 reuses most of K3, but replaces the full-attention MLA layers with DSA.

The change extracts and reuses the top-k mask impl from build_attn.

The model is sometimes named with the -Pro suffix (e.g. huggingface model name) but not always (e.g. model arch in config.json). It's unclear what the right name to use is but the non-pro-suffix seems the most appropriate in the code, mirroring the strings in the safetensors/json text.

I tested this against the BF16/MXFP4 conversion and a q2/q2/q3 gate/up/down quant without issue. Code review was done with llama.cpp and DeepSeek Harness, three agents in parallel (default -np 4, unified KV) each ending with ~250K sequence length, numerous tool calls, etc. and no errors or hints of badness.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes, Most of the work was done using DsV41F-1017, with code review by this K3.1 impl. once operable. All GitHub commentary is my own writing unless explicitly stated otherwise.

Closes #31778

@usrlocalben
usrlocalben requested review from a team, CISC and ggerganov as code owners November 6, 2026 05:16
@github-actions github-actions Bot added model Model specific testing Everything test related conversion labels Nov 6, 2026
@sanmai

sanmai commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Tested the conversion with tiny-random/kimi-k3.1-pro.

llama-completion gives the same tokens on CPU and CUDA.

llama-completion -hf akopytko/tiny-kimi-k3.1-pro-GGUF -p "Hello world" -n 16 --temp 0 -no-cnv -lv 0 -dev CUDA0
llama-completion -hf akopytko/tiny-kimi-k3.1-pro-GGUF -p "Hello world" -n 16 --temp 0 -no-cnv -lv 0 -dev none

@CISC CISC left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebase and adapt to refactor on master.

Comment thread conversion/kimi_k3.py Resolved
Comment thread src/models/kimi-k31.cpp Resolved

@CISC CISC left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread conversion/kimi_k3.py Resolved
Comment thread src/models/kimi-k31.cpp Resolved
K3 with the full-attention MLA layers replaced by DSA: on this checkpoint 24
of the 93 layers carry a lightning indexer (32 heads x 128, top_k 2048, no
rope). The KDA (linear-attention) layers and the rest of the K3 skeleton -
cross-layer residuals, latent MoE, situ, MLA output gate, full-rank KDA gate -
are unchanged, so this reuses the K3 graph and converter and adds the indexer.

Indexer:
  - the selection is an additive mask over the whole cache, not a gather of
    the top-k rows, so it reuses the existing sparse flash attention path;
    off that path the mask stays correct but still costs a kq over the cache
  - the indexer key LayerNorm takes its eps from the model's rms_norm_eps,
    because dsa_config declares no eps of its own

Graph and cache:
  - new arch with four indexer tensors per DSA layer, and a hybrid memory
    that keeps a K-only MLA cache for those layers, recurrent state for the
    KDA layers, and the indexer key cache
  - the graph node budget shares the K3 entry

Conversion:
  - KimiK31Model(KimiK3Model), picked by KimiK31ForConditionalGeneration
  - dsa_config becomes attention.indexer.{head_count,key_length,top_k,types}
    plus the indexer LayerNorm eps; the layer list is 1-indexed like
    linear_attn_config's, and the converter asserts that it covers exactly
    the full-attention layers
  - the routed experts stay MXFP4 and are repacked as in K3, not dequantized
  - the vision tower and mm_projector are skipped: text only for now
  - the arch key follows the checkpoint's own model_type ("kimi_k31") and the
    loader registers KimiK31ForConditionalGeneration; the release name carries
    a -Pro suffix that nothing in the config or the tensor names uses

Quantisation: the indexer's q_b, k and proj are kept out of the quant
overrides, since the top-k selection is sensitive to their precision.

Verified on an 8-layer slice against the HF checkpoint: 38 tensors checked
exactly, the indexer metadata against the config, and the selection against
full attention - identical PPL at top_k beyond the context length, divergent
once top_k is forced below it. Arch test fixture and the full arch suite pass.
@CISC CISC added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Nov 6, 2026
@ngxson
ngxson merged commit bcb0152 into ggml-org:master Nov 6, 2026
30 checks passed
Maziar123 added a commit to Maziar123/Strata that referenced this pull request Nov 9, 2026
Upstream ggml-org/llama.cpp#31779 (bcb01528) supports K3.1 natively with
arch kimi-k31, which is incompatible with the legacy patch's kimi-k3.
- LLAMA_REF defaults to bcb01528; the patch is applied only on older trees
- check_arch stops imatrix/quantize/serve on an architecture mismatch
- fetch downloads one quant set of a prebuilt GGUF repo and verifies
  SHA256SUMS; serve accepts MODEL=
- docs: upstream status, GGUF incompatibility, candidate model list

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Lissanro Lissanro mentioned this pull request Nov 9, 2026
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conversion merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. model Model specific testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Kimi-K3.1(-Pro) model support

4 participants