How fareedkhan-dev/kimi-k3-in-c works
Kimi K3 in C is a CPU inference engine for loading and running a very large checkpoint with a bounded resident working set. The principal workflow is: CLI accepts a prompt, tokenizes it, prepares model weights and memory-backed state, runs inference, then returns generated text. Chat adds XTML formatting and multi-turn history; the README also documents incremental decoding, state resume, and draft/verify hybrid decoding. The graph uses documented capabilities where source excerpts are incomplete; it omits uncertain internal call links rather than asserting them.
Main entry points: k3_run.c Inference CLI · k3_ops.c Numeric kernels · k3_tok.h K3 tokenizer · k3_st.c Checkpoint index
Components 18
Input and chat
- Inference CLI
src/cli/k3_run.c - Chat history and XTML
src/chat/k3_chat.c - K3 tokenizer
src/tokenizer/k3_tok.h
Checkpoint and memory
- Model config
src/io/k3_load.c - Checkpoint index
src/io/k3_st.c - Trunk streaming
src/io/k3_trunk.c - Layer weight binding
src/model/k3_bind.c - Expert LRU cache
src/cache/k3_cache.c
Model inference
- Numeric kernels
src/core/k3_ops.c - KDA recurrence
src/core/k3_ops.c - MLA attention
src/core/k3_ops.c - Routed MoE
src/core/k3_ops.c - Attention residuals
src/core/k3_ops.c
Generation and state
- Token sampler
src/chat/k3_sampler.c - Decode state
src/cli/k3_run.c
Other components
- User
- Model checkpoint
- Generated text
Connections 27
- User → Inference CLI: submits prompt
- Inference CLI → Chat history and XTML: formats chat
- Chat history and XTML → K3 tokenizer: formats input
- Inference CLI → K3 tokenizer: tokenizes prompt
- Inference CLI → Model config: loads config
- Inference CLI → Checkpoint index: opens checkpoint
- Model checkpoint → Checkpoint index: provides tensors
- Checkpoint index → Layer weight binding: resolves weights
- Inference CLI → Trunk streaming: sets memory budget
- Model checkpoint → Trunk streaming: streams trunk
- Inference CLI → Layer weight binding: binds layers
- Inference CLI → Expert LRU cache: configures cache
- Model checkpoint → Expert LRU cache: supplies experts
- Inference CLI → Numeric kernels: runs inference
- Numeric kernels → Trunk streaming: reads bound weights
- Numeric kernels → KDA recurrence: runs KDA layers
- Numeric kernels → MLA attention: runs MLA layers
- Numeric kernels → Routed MoE: routes experts
- Routed MoE → Expert LRU cache: requests experts
- Numeric kernels → Attention residuals: applies residuals
- Inference CLI → Decode state: saves or restores
- Numeric kernels → Token sampler: provides logits
- Token sampler → Inference CLI: returns token
- Inference CLI → K3 tokenizer: decodes tokens
- K3 tokenizer → Generated text: renders text
- Inference CLI → Generated text: prints output
- User → Generated text: receives answer