# fareedkhan-dev/kimi-k3-in-c architecture

> Architecture diagram of the GitHub repository fareedkhan-dev/kimi-k3-in-c, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fareedkhan-dev/kimi-k3-in-c
- Repository: https://github.com/fareedkhan-dev/kimi-k3-in-c

## Overview

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: [`src/cli/k3_run.c`](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/cli/k3_run.c) (Inference CLI), [`src/core/k3_ops.c`](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/core/k3_ops.c) (Numeric kernels), [`src/tokenizer/k3_tok.h`](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/tokenizer/k3_tok.h) (K3 tokenizer), [`src/io/k3_st.c`](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/io/k3_st.c) (Checkpoint index)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_input["Input and chat"]
  node_cli["Inference CLI<br/>[k3_run.c]"]
  node_chat["Chat history and XTML<br/>[k3_chat.c]"]
  node_tokenizer["K3 tokenizer<br/>[k3_tok.h]"]
end

subgraph group_loading["Checkpoint and memory"]
  node_config["Model config<br/>[k3_load.c]"]
  node_safetensors["Checkpoint index<br/>[k3_st.c]"]
  node_trunk["Trunk streaming<br/>[k3_trunk.c]"]
  node_binding["Layer weight binding<br/>[k3_bind.c]"]
  node_expertcache[("Expert LRU cache<br/>[k3_cache.c]")]
end

subgraph group_inference["Model inference"]
  node_ops["Numeric kernels<br/>[k3_ops.c]"]
  node_kda["KDA recurrence<br/>[k3_ops.c]"]
  node_mla["MLA attention<br/>[k3_ops.c]"]
  node_moe["Routed MoE<br/>[k3_ops.c]"]
  node_attentionres["Attention residuals<br/>[k3_ops.c]"]
end

subgraph group_output["Generation and state"]
  node_sampler["Token sampler<br/>[k3_sampler.c]"]
  node_state[("Decode state<br/>[k3_run.c]")]
end

node_user(("User"))
node_checkpoint[("Model checkpoint")]
node_result["Generated text"]

node_user -->|"submits prompt"| node_cli
node_cli -.->|"formats chat"| node_chat
node_chat -.->|"formats input"| node_tokenizer
node_cli -->|"tokenizes prompt"| node_tokenizer
node_cli -->|"loads config"| node_config
node_cli -->|"opens checkpoint"| node_safetensors
node_checkpoint -->|"provides tensors"| node_safetensors
node_safetensors -->|"resolves weights"| node_binding
node_cli -->|"sets memory budget"| node_trunk
node_checkpoint -->|"streams trunk"| node_trunk
node_cli -->|"binds layers"| node_binding
node_cli -->|"configures cache"| node_expertcache
node_checkpoint -->|"supplies experts"| node_expertcache
node_cli -->|"runs inference"| node_ops
node_ops -->|"reads bound weights"| node_trunk
node_ops -->|"runs KDA layers"| node_kda
node_ops -->|"runs MLA layers"| node_mla
node_ops -->|"routes experts"| node_moe
node_moe -->|"requests experts"| node_expertcache
node_ops -->|"applies residuals"| node_attentionres
node_cli -->|"saves or restores"| node_state
node_ops -->|"provides logits"| node_sampler
node_sampler -->|"returns token"| node_cli
node_cli -->|"decodes tokens"| node_tokenizer
node_tokenizer -->|"renders text"| node_result
node_cli -->|"prints output"| node_result
node_user -->|"receives answer"| node_result

click node_cli "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/cli/k3_run.c"
click node_chat "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/chat/k3_chat.c"
click node_sampler "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/chat/k3_sampler.c"
click node_tokenizer "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/tokenizer/k3_tok.h"
click node_config "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/io/k3_load.c"
click node_safetensors "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/io/k3_st.c"
click node_trunk "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/io/k3_trunk.c"
click node_binding "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/model/k3_bind.c"
click node_expertcache "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/cache/k3_cache.c"
click node_ops "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/core/k3_ops.c"
click node_kda "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/core/k3_ops.c"
click node_mla "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/core/k3_ops.c"
click node_moe "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/core/k3_ops.c"
click node_attentionres "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/core/k3_ops.c"
click node_state "https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/cli/k3_run.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_chat,node_tokenizer,node_user toneBlue
class node_config,node_safetensors,node_trunk,node_binding,node_expertcache,node_checkpoint toneAmber
class node_ops,node_kda,node_mla,node_moe,node_attentionres toneMint
class node_sampler,node_state toneRose
class node_result toneIndigo
```

## Components

### Input and chat

- [Inference CLI](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/cli/k3_run.c): `src/cli/k3_run.c`
- [Chat history and XTML](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/chat/k3_chat.c): `src/chat/k3_chat.c`
- [K3 tokenizer](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/tokenizer/k3_tok.h): `src/tokenizer/k3_tok.h`

### Checkpoint and memory

- [Model config](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/io/k3_load.c): `src/io/k3_load.c`
- [Checkpoint index](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/io/k3_st.c): `src/io/k3_st.c`
- [Trunk streaming](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/io/k3_trunk.c): `src/io/k3_trunk.c`
- [Layer weight binding](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/model/k3_bind.c): `src/model/k3_bind.c`
- [Expert LRU cache](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/cache/k3_cache.c): `src/cache/k3_cache.c`

### Model inference

- [Numeric kernels](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/core/k3_ops.c): `src/core/k3_ops.c`
- [KDA recurrence](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/core/k3_ops.c): `src/core/k3_ops.c`
- [MLA attention](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/core/k3_ops.c): `src/core/k3_ops.c`
- [Routed MoE](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/core/k3_ops.c): `src/core/k3_ops.c`
- [Attention residuals](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/core/k3_ops.c): `src/core/k3_ops.c`

### Generation and state

- [Token sampler](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/chat/k3_sampler.c): `src/chat/k3_sampler.c`
- [Decode state](https://github.com/fareedkhan-dev/kimi-k3-in-c/blob/main/src/cli/k3_run.c): `src/cli/k3_run.c`

### Other components

- **User**
- **Model checkpoint**
- **Generated text**

## Connections

- 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
