# 1bit-monster/engine architecture

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

- Interactive diagram: https://gitdiagram.com/1bit-monster/engine
- Repository: https://github.com/1bit-monster/engine

## Overview

1bit engine is a model-serving backend hosted by Lemonade. Its main workflow is an OpenAI-compatible request entering `1bit serve`, then being proxied or routed to a model/device and returned to the client. The repository also contains NPU inference, Laya request classification, Project Manager delegation, MoE expert caching, and model-registry tooling. Several subsystems are documented but not sampled in implementation excerpts, so their links are limited to README-supported relationships; unsampled implementation details are not inferred.

Main entry points: [`app/serve.cpp`](https://github.com/1bit-monster/engine/blob/main/app/serve.cpp) (Serve gateway), [`app/main.cpp`](https://github.com/1bit-monster/engine/blob/main/app/main.cpp) (Command entry), [`npu/generate.cpp`](https://github.com/1bit-monster/engine/blob/main/npu/generate.cpp) (NPU generation), [`npu/forward/src/q4nx_forward.cpp`](https://github.com/1bit-monster/engine/blob/main/npu/forward/src/q4nx_forward.cpp) (Q4NX forward)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_serving["Serving and routing"]
  node_serve["Serve gateway<br/>[serve.cpp]"]
  node_cli["Command entry<br/>[main.cpp]"]
  node_admission["Memory admission<br/>[mem_admit.h]"]
  node_unified["Unified serving<br/>[unified.cpp]"]
  node_forward_serve["Forward server<br/>[forward_serve.cpp]"]
end

subgraph group_npu["NPU inference"]
  node_npu_model["NPU model reader<br/>[model.c]"]
  node_npu_tokenizer["NPU tokenizer<br/>[tokenizer.cpp]"]
  node_npu_generation["NPU generation<br/>[generate.cpp]"]
  node_q4nx_forward["Q4NX forward<br/>[q4nx_forward.cpp]"]
  node_npu_runtime["NPU runtime interface<br/>[rt.h]"]
end

subgraph group_routing["Classification and delegation"]
  node_pm["Project Manager<br/>[pm.cpp]"]
  node_laya_policy["Laya policy<br/>[policy.cpp]"]
  node_laya_route["Laya routing<br/>[route.cpp]"]
  node_laya_scorer["Laya scorer<br/>[scorer.cpp]"]
end

subgraph group_model_ops["Model capabilities"]
  node_moe_cache[("MoE expert cache<br/>[expert_cache.cpp]")]
  node_moe_index["GGUF expert index<br/>[gguf_index.cpp]"]
  node_registry["Model registry<br/>[registry_build.py]"]
  node_census["Architecture census<br/>[census_watch.py]"]
  node_hrx["HRX GPU route"]
  node_dwarfstar["DwarfStar route"]
  node_onnx["ONNX Runtime GenAI"]
end

node_lemonade(("Lemonade host"))
node_api_client(("API client"))

node_lemonade -->|"hosts backend"| node_serve
node_api_client -->|"sends requests"| node_serve
node_cli -->|"dispatches"| node_serve
node_serve -->|"checks memory"| node_admission
node_serve -->|"configures delegation"| node_pm
node_serve -->|"configures classification"| node_laya_scorer
node_serve -->|"routes requests"| node_laya_policy
node_serve -->|"routes requests"| node_laya_route
node_cli -->|"dispatches"| node_unified
node_cli -->|"dispatches"| node_forward_serve
node_npu_generation -.->|"loads model"| node_npu_model
node_npu_generation -.->|"tokenizes input"| node_npu_tokenizer
node_npu_generation -.->|"runs inference"| node_q4nx_forward
node_q4nx_forward -->|"uses runtime"| node_npu_runtime
node_moe_cache -->|"uses index"| node_moe_index
node_census -.->|"updates coverage"| node_registry
node_serve -->|"selects GPU route"| node_hrx
node_serve -->|"selects model route"| node_dwarfstar
node_serve -->|"selects CPU route"| node_onnx

click node_serve "https://github.com/1bit-monster/engine/blob/main/app/serve.cpp"
click node_cli "https://github.com/1bit-monster/engine/blob/main/app/main.cpp"
click node_admission "https://github.com/1bit-monster/engine/blob/main/app/mem_admit.h"
click node_unified "https://github.com/1bit-monster/engine/blob/main/app/unified.cpp"
click node_forward_serve "https://github.com/1bit-monster/engine/blob/main/app/forward_serve.cpp"
click node_pm "https://github.com/1bit-monster/engine/blob/main/app/pm.cpp"
click node_laya_policy "https://github.com/1bit-monster/engine/blob/main/laya/policy.cpp"
click node_laya_route "https://github.com/1bit-monster/engine/blob/main/laya/route.cpp"
click node_laya_scorer "https://github.com/1bit-monster/engine/blob/main/laya/scorer.cpp"
click node_npu_model "https://github.com/1bit-monster/engine/blob/main/npu/forward/src/model.c"
click node_npu_tokenizer "https://github.com/1bit-monster/engine/blob/main/npu/tokenizer.cpp"
click node_npu_generation "https://github.com/1bit-monster/engine/blob/main/npu/generate.cpp"
click node_q4nx_forward "https://github.com/1bit-monster/engine/blob/main/npu/forward/src/q4nx_forward.cpp"
click node_npu_runtime "https://github.com/1bit-monster/engine/blob/main/npu/rt.h"
click node_moe_cache "https://github.com/1bit-monster/engine/blob/main/moe/expert_cache.cpp"
click node_moe_index "https://github.com/1bit-monster/engine/blob/main/moe/gguf_index.cpp"
click node_registry "https://github.com/1bit-monster/engine/blob/main/tools/registry_build.py"
click node_census "https://github.com/1bit-monster/engine/blob/main/tools/census_watch.py"
click node_hrx "https://github.com/1bit-monster/engine/tree/main/app"
click node_dwarfstar "https://github.com/1bit-monster/engine/tree/main/app"
click node_onnx "https://github.com/1bit-monster/engine/tree/main/app"

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_serve,node_cli,node_admission,node_unified,node_forward_serve,node_api_client toneBlue
class node_npu_model,node_npu_tokenizer,node_npu_generation,node_q4nx_forward,node_npu_runtime toneAmber
class node_pm,node_laya_policy,node_laya_route,node_laya_scorer toneMint
class node_moe_cache,node_moe_index,node_registry,node_census,node_hrx,node_dwarfstar,node_onnx toneRose
class node_lemonade toneIndigo
```

## Components

### Serving and routing

- [Serve gateway](https://github.com/1bit-monster/engine/blob/main/app/serve.cpp): `app/serve.cpp`
- [Command entry](https://github.com/1bit-monster/engine/blob/main/app/main.cpp): `app/main.cpp`
- [Memory admission](https://github.com/1bit-monster/engine/blob/main/app/mem_admit.h): `app/mem_admit.h`
- [Unified serving](https://github.com/1bit-monster/engine/blob/main/app/unified.cpp): `app/unified.cpp`
- [Forward server](https://github.com/1bit-monster/engine/blob/main/app/forward_serve.cpp): `app/forward_serve.cpp`

### NPU inference

- [NPU model reader](https://github.com/1bit-monster/engine/blob/main/npu/forward/src/model.c): `npu/forward/src/model.c`
- [NPU tokenizer](https://github.com/1bit-monster/engine/blob/main/npu/tokenizer.cpp): `npu/tokenizer.cpp`
- [NPU generation](https://github.com/1bit-monster/engine/blob/main/npu/generate.cpp): `npu/generate.cpp`
- [Q4NX forward](https://github.com/1bit-monster/engine/blob/main/npu/forward/src/q4nx_forward.cpp): `npu/forward/src/q4nx_forward.cpp`
- [NPU runtime interface](https://github.com/1bit-monster/engine/blob/main/npu/rt.h): `npu/rt.h`

### Classification and delegation

- [Project Manager](https://github.com/1bit-monster/engine/blob/main/app/pm.cpp): `app/pm.cpp`
- [Laya policy](https://github.com/1bit-monster/engine/blob/main/laya/policy.cpp): `laya/policy.cpp`
- [Laya routing](https://github.com/1bit-monster/engine/blob/main/laya/route.cpp): `laya/route.cpp`
- [Laya scorer](https://github.com/1bit-monster/engine/blob/main/laya/scorer.cpp): `laya/scorer.cpp`

### Model capabilities

- [MoE expert cache](https://github.com/1bit-monster/engine/blob/main/moe/expert_cache.cpp): `moe/expert_cache.cpp`
- [GGUF expert index](https://github.com/1bit-monster/engine/blob/main/moe/gguf_index.cpp): `moe/gguf_index.cpp`
- [Model registry](https://github.com/1bit-monster/engine/blob/main/tools/registry_build.py): `tools/registry_build.py`
- [Architecture census](https://github.com/1bit-monster/engine/blob/main/tools/census_watch.py): `tools/census_watch.py`
- [HRX GPU route](https://github.com/1bit-monster/engine/tree/main/app): `app`
- [DwarfStar route](https://github.com/1bit-monster/engine/tree/main/app): `app`
- [ONNX Runtime GenAI](https://github.com/1bit-monster/engine/tree/main/app): `app`

### Other components

- **Lemonade host**
- **API client**

## Connections

- Lemonade host → Serve gateway: hosts backend (evidence: `README.md`)
- API client → Serve gateway: sends requests (evidence: `README.md`)
- Command entry → Serve gateway: dispatches (evidence: `app/main.cpp`)
- Serve gateway → Memory admission: checks memory (evidence: `app/serve.cpp`)
- Serve gateway → Project Manager: configures delegation (evidence: `app/serve.cpp`)
- Serve gateway → Laya scorer: configures classification (evidence: `app/serve.cpp`)
- Serve gateway → Laya policy: routes requests (evidence: `app/serve.cpp`)
- Serve gateway → Laya routing: routes requests (evidence: `app/serve.cpp`)
- Command entry → Unified serving: dispatches (evidence: `app/main.cpp`)
- Command entry → Forward server: dispatches (evidence: `app/main.cpp`)
- NPU generation → NPU model reader: loads model
- NPU generation → NPU tokenizer: tokenizes input
- NPU generation → Q4NX forward: runs inference
- Q4NX forward → NPU runtime interface: uses runtime (evidence: `npu/forward/src/q4nx_forward.cpp`)
- MoE expert cache → GGUF expert index: uses index
- Architecture census → Model registry: updates coverage
- Serve gateway → HRX GPU route: selects GPU route (evidence: `README.md`)
- Serve gateway → DwarfStar route: selects model route (evidence: `README.md`)
- Serve gateway → ONNX Runtime GenAI: selects CPU route (evidence: `README.md`)
