# shifulegend/project-zero architecture

> Architecture diagram of the GitHub repository shifulegend/project-zero, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shifulegend/project-zero
- Repository: https://github.com/shifulegend/project-zero

## Overview

Project Zero is a CPU-only LLM inference engine for BitNet ternary and dense GGUF models. Its primary workflow is to start through the CLI, load model weights and tokenizer, then generate tokens using transformer, sampling, and CPU-optimized math components. The repository also includes an HTTP/UI surface, image input, agent tooling, and retrieval-augmented memory. Edges are limited to relationships shown in the README or SOURCE INDEX; unsampled implementation details are not inferred.

Main entry points: [`src/transformer/forward.c`](https://github.com/shifulegend/project-zero/blob/master/src/transformer/forward.c) (Transformer Forward), [`src/cli/main.c`](https://github.com/shifulegend/project-zero/blob/master/src/cli/main.c) (CLI Entry), [`src/transformer/generate.c`](https://github.com/shifulegend/project-zero/blob/master/src/transformer/generate.c) (Token Generation), [`src/math/parallel_matmul.c`](https://github.com/shifulegend/project-zero/blob/master/src/math/parallel_matmul.c) (Parallel Matmul)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime and Models"]
  node_loader["GGUF Loader<br/>[gguf_loader.c]"]
  node_weights["Model Weights<br/>[weights.h]"]
  node_tokenizer["Tokenizer<br/>[tokenizer_encode.c]"]
  node_kv["KV Cache<br/>[run_state.h]"]
end

subgraph group_compute["Inference Compute"]
  node_generation["Token Generation<br/>[generate.c]"]
  node_forward["Transformer Forward<br/>[forward.c]"]
  node_attention["Attention<br/>[attention.c]"]
  node_ffn["Feed-Forward<br/>[ffn.c]"]
  node_matmul["Parallel Matmul<br/>[parallel_matmul.c]"]
  node_sampling["Token Sampling<br/>[top_p.c]"]
end

subgraph group_interfaces["User Interfaces"]
  node_cli["CLI Entry<br/>[main.c]"]
  node_http["HTTP API<br/>[http_server.c]"]
  node_webui["Web UI<br/>[App.svelte]"]
end

subgraph group_extensions["Inference Extensions"]
  node_vision["Vision Pipeline<br/>[vision_pipeline.c]"]
  node_rag["Memory Retrieval<br/>[auto_retrieve.c]"]
  node_vector[("Vector Database<br/>[vector_db.c]")]
  node_agent["Agent Loop<br/>[agent_loop.c]"]
end

subgraph group_foundation["Runtime Foundation"]
  node_simd["SIMD Dispatch<br/>[simd_dispatch.c]"]
  node_pool["Thread Pool<br/>[thread_pool.c]"]
end

node_user(("User"))

node_user -->|"runs"| node_cli
node_cli -->|"loads model"| node_loader
node_loader -->|"populates"| node_weights
node_cli -->|"initializes"| node_tokenizer
node_cli -->|"starts generation"| node_generation
node_generation -.->|"runs forward pass"| node_forward
node_forward -.->|"uses attention"| node_attention
node_forward -.->|"uses feed-forward"| node_ffn
node_forward -.->|"uses kernels"| node_matmul
node_generation -.->|"selects token"| node_sampling
node_generation -.->|"updates cache"| node_kv
node_matmul -->|"dispatches work"| node_pool
node_matmul -->|"selects kernels"| node_simd
node_user -.->|"sends request"| node_http
node_webui -.->|"calls API"| node_http
node_http -->|"accepts image input"| node_vision
node_cli -->|"accepts image input"| node_vision
node_vision -->|"injects vision context"| node_forward
node_agent -->|"invokes inference"| node_forward
node_agent -->|"uses retrieval"| node_rag
node_rag -->|"searches memories"| node_vector
node_rag -->|"encodes context"| node_tokenizer

click node_cli "https://github.com/shifulegend/project-zero/blob/master/src/cli/main.c"
click node_http "https://github.com/shifulegend/project-zero/blob/master/src/api/http_server.c"
click node_webui "https://github.com/shifulegend/project-zero/blob/master/webui/src/App.svelte"
click node_loader "https://github.com/shifulegend/project-zero/blob/master/src/core/gguf_loader.c"
click node_weights "https://github.com/shifulegend/project-zero/blob/master/include/core/weights.h"
click node_tokenizer "https://github.com/shifulegend/project-zero/blob/master/src/tokenizer/tokenizer_encode.c"
click node_generation "https://github.com/shifulegend/project-zero/blob/master/src/transformer/generate.c"
click node_forward "https://github.com/shifulegend/project-zero/blob/master/src/transformer/forward.c"
click node_attention "https://github.com/shifulegend/project-zero/blob/master/src/transformer/attention.c"
click node_ffn "https://github.com/shifulegend/project-zero/blob/master/src/transformer/ffn.c"
click node_matmul "https://github.com/shifulegend/project-zero/blob/master/src/math/parallel_matmul.c"
click node_sampling "https://github.com/shifulegend/project-zero/blob/master/src/sampling/top_p.c"
click node_kv "https://github.com/shifulegend/project-zero/blob/master/include/core/run_state.h"
click node_simd "https://github.com/shifulegend/project-zero/blob/master/src/math/simd_dispatch.c"
click node_pool "https://github.com/shifulegend/project-zero/blob/master/src/threading/thread_pool.c"
click node_vision "https://github.com/shifulegend/project-zero/blob/master/src/multimodal/vision_pipeline.c"
click node_rag "https://github.com/shifulegend/project-zero/blob/master/src/rag/auto_retrieve.c"
click node_vector "https://github.com/shifulegend/project-zero/blob/master/src/rag/vector_db.c"
click node_agent "https://github.com/shifulegend/project-zero/blob/master/src/agent/agent_loop.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_loader,node_weights,node_tokenizer,node_kv,node_user toneBlue
class node_generation,node_forward,node_attention,node_ffn,node_matmul,node_sampling toneAmber
class node_cli,node_http,node_webui toneMint
class node_vision,node_rag,node_vector,node_agent toneRose
class node_simd,node_pool toneIndigo
```

## Components

### Runtime and Models

- [GGUF Loader](https://github.com/shifulegend/project-zero/blob/master/src/core/gguf_loader.c): `src/core/gguf_loader.c`
- [Model Weights](https://github.com/shifulegend/project-zero/blob/master/include/core/weights.h): `include/core/weights.h`
- [Tokenizer](https://github.com/shifulegend/project-zero/blob/master/src/tokenizer/tokenizer_encode.c): `src/tokenizer/tokenizer_encode.c`
- [KV Cache](https://github.com/shifulegend/project-zero/blob/master/include/core/run_state.h): `include/core/run_state.h`

### Inference Compute

- [Token Generation](https://github.com/shifulegend/project-zero/blob/master/src/transformer/generate.c): `src/transformer/generate.c`
- [Transformer Forward](https://github.com/shifulegend/project-zero/blob/master/src/transformer/forward.c): `src/transformer/forward.c`
- [Attention](https://github.com/shifulegend/project-zero/blob/master/src/transformer/attention.c): `src/transformer/attention.c`
- [Feed-Forward](https://github.com/shifulegend/project-zero/blob/master/src/transformer/ffn.c): `src/transformer/ffn.c`
- [Parallel Matmul](https://github.com/shifulegend/project-zero/blob/master/src/math/parallel_matmul.c): `src/math/parallel_matmul.c`
- [Token Sampling](https://github.com/shifulegend/project-zero/blob/master/src/sampling/top_p.c): `src/sampling/top_p.c`

### User Interfaces

- [CLI Entry](https://github.com/shifulegend/project-zero/blob/master/src/cli/main.c): `src/cli/main.c`
- [HTTP API](https://github.com/shifulegend/project-zero/blob/master/src/api/http_server.c): `src/api/http_server.c`
- [Web UI](https://github.com/shifulegend/project-zero/blob/master/webui/src/App.svelte): `webui/src/App.svelte`

### Inference Extensions

- [Vision Pipeline](https://github.com/shifulegend/project-zero/blob/master/src/multimodal/vision_pipeline.c): `src/multimodal/vision_pipeline.c`
- [Memory Retrieval](https://github.com/shifulegend/project-zero/blob/master/src/rag/auto_retrieve.c): `src/rag/auto_retrieve.c`
- [Vector Database](https://github.com/shifulegend/project-zero/blob/master/src/rag/vector_db.c): `src/rag/vector_db.c`
- [Agent Loop](https://github.com/shifulegend/project-zero/blob/master/src/agent/agent_loop.c): `src/agent/agent_loop.c`

### Runtime Foundation

- [SIMD Dispatch](https://github.com/shifulegend/project-zero/blob/master/src/math/simd_dispatch.c): `src/math/simd_dispatch.c`
- [Thread Pool](https://github.com/shifulegend/project-zero/blob/master/src/threading/thread_pool.c): `src/threading/thread_pool.c`

### Other components

- **User**

## Connections

- User → CLI Entry: runs (evidence: `README.md`)
- CLI Entry → GGUF Loader: loads model (evidence: `src/cli/main.c`)
- GGUF Loader → Model Weights: populates
- CLI Entry → Tokenizer: initializes (evidence: `src/cli/main.c`)
- CLI Entry → Token Generation: starts generation (evidence: `src/cli/main.c`)
- Token Generation → Transformer Forward: runs forward pass
- Transformer Forward → Attention: uses attention
- Transformer Forward → Feed-Forward: uses feed-forward
- Transformer Forward → Parallel Matmul: uses kernels
- Token Generation → Token Sampling: selects token
- Token Generation → KV Cache: updates cache
- Parallel Matmul → Thread Pool: dispatches work (evidence: `src/math/parallel_matmul.c`)
- Parallel Matmul → SIMD Dispatch: selects kernels (evidence: `src/math/parallel_matmul.c`)
- User → HTTP API: sends request
- Web UI → HTTP API: calls API
- HTTP API → Vision Pipeline: accepts image input
- CLI Entry → Vision Pipeline: accepts image input
- Vision Pipeline → Transformer Forward: injects vision context (evidence: `src/multimodal/vision_pipeline.c`)
- Agent Loop → Transformer Forward: invokes inference (evidence: `src/agent/agent_loop.c`)
- Agent Loop → Memory Retrieval: uses retrieval (evidence: `src/agent/agent_loop.c`)
- Memory Retrieval → Vector Database: searches memories
- Memory Retrieval → Tokenizer: encodes context
