Skip to content

feat(kimi): add bounded K3 source and explicit AttnRes model contracts - #3606

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
tpurtell:upstream/26-kimi-k3-native-source-20261108
Nov 9, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
tpurtell:upstream/26-kimi-k3-native-source-20261108

Conversation

@tpurtell

@tpurtell tpurtell commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Register Kimi K3 and Kimi Linear routed-expert definitions and carry the bounded native source adapter, MXFP4 decode, AttnRes prefix/residual boundaries, and checkpoint reference loading. Ordinary looper entry points continue to reject unsupported AttnRes continuation explicitly.

This is layer 26/36, extracted from an already-used fork for initial maintainer feedback. It depends on #3605. Review only this feature; the cross-fork Files changed view is cumulative because the PR targets upstream main.

Incremental change: 12 files, +606/−1 lines, including carried tests and the mechanism reference. No merge is automatic. Review value and API shape first; substantial rework can follow that feedback.

Flow and existing-code boundary

flowchart TD
    S0["KimiK3Source"]
    M0_0["KimiK3Source.__init__"]
    M0_1["KimiK3Source.load_block"]
    S1["prepare_moe"]
    S2["finish_moe"]
    S3["KimiLinearQModel"]
    M3_0["KimiLinearQModel.quantize"]
    M3_1["KimiLinearQModel.pre_quantize"]
    S4["KimiK3QModel"]
    E["KeyError, NotImplementedError, ValueError"]
    S0 -. "exposes" .-> M0_0
    S0 -. "exposes" .-> M0_1
    S0 -->|"raises on rejected input/state"| E
    S3 -. "exposes" .-> M3_0
    S3 -. "exposes" .-> M3_1
    S3 -->|"raises on rejected input/state"| E
    style E fill:#fee,stroke:#b22
Loading
Before this extraction layer Carried implementation
API absent in the previous stack layer (path/symbol checked) gptqmodel/utils/kimi_k3.py:82: KimiK3Source
API absent in the previous stack layer (path/symbol checked) gptqmodel/utils/kimi_k3.py:138: prepare_moe
API absent in the previous stack layer (path/symbol checked) gptqmodel/utils/kimi_k3.py:160: finish_moe
API absent in the previous stack layer (path/symbol checked) gptqmodel/models/definitions/kimi_k3.py:18: KimiLinearQModel

Committed reference: docs/kimi-k3-native-source.md, linked from README. Class entry points/calls and rejected-state paths describe code present in this branch, rather than planned integrations.

Validation

Carried test files: tests/test_kimi_k3_support.py, tests/module_tree/test_kimi_k3_fork_plan.py, tests/module_tree/test_kimi_linear_fork_plan.py

This packaging pass checked Python parsing, whitespace, dependency order and balanced Mermaid blocks. The author reports successful prior use of these features. Per the requested resource limit and feedback-first scope, existing model quantization, GPU/ROCm tests, heavyweight CPU workloads and full model benchmarks were not rerun; no new performance or model-accuracy claim is made. Tests accompanying earlier stack layers retain their previously reported results.

New registrations include explicit module-plan inventory entries, targeted plan tests and unqualified fusion entries. Three model-specific plan tests plus the selected registry/consumer metadata cases passed as part of the 10-test CPU metadata group; actual checkpoint/hardware tests were not rerun.

Extract the existing working fork feature for first maintainer feedback. Preserve carried code and tests; limit validation to lightweight structural checks. See the linked mechanism reference for actual API/call and failure paths.
@Qubitium
Qubitium force-pushed the upstream/26-kimi-k3-native-source-20261108 branch from 1a40b17 to 2f3028d Compare November 9, 2026 09:48

@Qubitium Qubitium left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed against current main.

The K3 source path is deliberately bounded and fails closed where the ordinary looper cannot be correct:

  • load_calibration_reference compiles the checkpoint's own decoder after AST-stripping the unused HF wrapper classes and the transformers.utils.generic import, so the reference math is the checkpoint's, not the installed Transformers'.
  • KimiK3Source validates model_type, every index shard name, and shard presence before use, and reads one tensor at a time (safe_open → owned clone → device copy) so the 2.8T checkpoint never lands in host RAM.
  • decode_mxfp4 checks packed/scale geometry, rejects the NaN E8M0 exponent, decodes interleaved E2M1 nibbles against 2^(e-127) scales, and rejects non-finite output.
  • load_block materializes a meta KimiDecoderLayer parameter-by-parameter, validates geometry, and deliberately keeps the padded self_attn.A_log bytes rather than trimming them.
  • prepare_moe/finish_moe split the reference attention half from routed MoE and carry both prefix_sum and block_residual, which is the state the stock looper cannot preserve.
  • KimiLinearQModel.quantize and pre_quantize raise when AttnRes layers are reached, so direct ModuleLooper use cannot silently drop residual state. Only routed w1/w3/w2 leaves are declared.

Cleanup: import ordering and dict.fromkeys in the new support test.

Validation: ruff check clean; pytest on the Kimi support/module-tree tests reports 221 passed, 12 skipped.

@Qubitium
Qubitium marked this pull request as ready for review November 9, 2026 09:48
@Qubitium
Qubitium merged commit bd1f8f2 into ModelCloud:main Nov 9, 2026
Qubitium added a commit that referenced this pull request Nov 29, 2026
…ers (#6408)

ds4rt is deprecated and is being refactored into method-agnostic features.
The per-model calibration adapters it added (#3606-#3616) have no production
caller: nothing in gptqmodel/ or scripts/ imports them, MODEL_MAP reaches
none of them, and only their own doc pages and unit tests cite them.

Delete 14 adapter utils plus exl3_hessian.py (its only user was kimi_k31):
kimi_k3, kimi_k3_core_format, kimi_k3_core_stream, kimi_k31, glm5_core,
glm5_mtp, glm5_flash_core, glm5_flash_mtp, step5_core, v41_capture,
v41_checkpoint, v41_mixed_replay, v41_recovery and v41_routed_batch, with
their 9 dedicated test files, their 9 doc pages and the index links.

This also removes two policy violations that lived only in this code:
step5_core.py ran an external families/step5/golden.py through exec_module
with no trust_remote_code / GPTQ_BYPASS_SAFETY_CHECKS gate, and
glm5_flash_core.py replaced chunk_kimi_delta_attention on the vendored
glm5_next port module. The GPTQMODEL_KIMI_K3_UTILS env var in
test_kimi_k3_support.py, which exec'd an arbitrary file, goes too.

Kept:
- ple_mmap.py: used by the deepseek_v41_flash port and definitions/deepseek_v41.
- v41_source, v41_replay, v41_inputs, v41_dspark, v41_native: imported by
  tests/test_deepseek_v41_local_port.py, test_v41_checkpoint_mapped_ple.py
  and (v41_native) ports/deepseek_v41. They go with the separate
  ports/deepseek_v41 removal.
- Tests of live code that lived in deleted files move to
  test_exl3_bitstream_oracle.py (ExllamaV3TorchLinear bitstream decode) and
  test_exllamav3_padded_output.py (ExllamaV3Linear padded output crop).

The Kimi K3 definition, auto.py, kimi_linear.py and docs no longer point at
the deleted "bounded KimiK3Source runner"; the NotImplementedError types and
the substrings the tests match are unchanged.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants