# currentf/sparseengine architecture

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

- Interactive diagram: https://gitdiagram.com/currentf/sparseengine
- Repository: https://github.com/currentf/sparseengine

## Overview

SparseEngine is a sparse-first long-context LLM inference and serving engine. Its main workflow accepts generation requests, schedules prefill and decode, runs supported model architectures, and manages KV or recurrent state using configurable sparse policies before returning generated output. The repository also documents OpenAI-compatible serving, multimodal inputs, distributed execution, prefix/chain caching, and multiple independent prefill and cache methods. The graph follows the source index and README; several entrypoint-to-runtime and method-to-kernel details are not shown in excerpts, so those links are omitted or marked unverified.

Main entry points: [`src/sparseengine/engine/llm_engine.py`](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/llm_engine.py) (Generation engine), [`src/sparseengine/engine/model_runner.py`](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/model_runner.py) (Model runner), [`src/sparseengine/entrypoints/openai/api_server.py`](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/entrypoints/openai/api_server.py) (OpenAI API), [`src/sparseengine/engine/cache_manager/standard.py`](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/cache_manager/standard.py) (KV cache manager)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_serving["Serving &amp; API"]
  node_client["Request client<br/>[client.py]"]
  node_openai["OpenAI API<br/>[api_server.py]"]
  node_routes["API routes"]
  node_serving_handlers["Request handlers"]
end

subgraph group_runtime["Inference Runtime"]
  node_llm_api["LLM interface<br/>[llm.py]"]
  node_config["Runtime config<br/>[config.py]"]
  node_engine["Generation engine<br/>[llm_engine.py]"]
  node_scheduler["Request scheduler<br/>[scheduler.py]"]
  node_input["Input processing<br/>[input_processor.py]"]
  node_sequence["Sequence state<br/>[sequence.py]"]
  node_async_sched["Async scheduling<br/>[scheduler.py]"]
  node_prefill["Prefill processing<br/>[prefill.py]"]
end

subgraph group_cache["Cache &amp; Sparsity"]
  node_cache_manager["KV cache manager<br/>[standard.py]"]
  node_sparse_methods["Sparse methods"]
  node_cache_storage[("Cache storage")]
  node_offload["KV offload<br/>[storage.py]"]
  node_chain_cache[("Prefix &amp; chain cache<br/>[chain_cache.py]")]
end

subgraph group_compute["Models &amp; Compute"]
  node_model_runner["Model runner<br/>[model_runner.py]"]
  node_models["Model architectures"]
  node_layers["Inference layers"]
  node_kernels["GPU kernels"]
end

subgraph group_platform["Parallel &amp; Media"]
  node_distributed["Distributed runtime"]
  node_recurrent[("Recurrent state")]
  node_multimodal["Multimodal inputs"]
end

node_user(("API caller"))

node_user -.->|"sends request"| node_openai
node_client -.->|"calls API"| node_openai
node_openai -.->|"dispatches"| node_routes
node_routes -.->|"routes request"| node_serving_handlers
node_serving_handlers -.->|"invokes generation"| node_llm_api
node_llm_api -.->|"creates runtime"| node_engine
node_engine -->|"uses settings"| node_config
node_engine -->|"processes input"| node_input
node_engine -->|"schedules requests"| node_scheduler
node_engine -->|"dispatches execution"| node_model_runner
node_engine -->|"tracks sequences"| node_sequence
node_engine -->|"uses async scheduler"| node_async_sched
node_model_runner -->|"runs architectures"| node_models
node_model_runner -->|"uses methods"| node_sparse_methods
node_model_runner -->|"reads sequence state"| node_sequence
node_models -.->|"uses layers"| node_layers
node_layers -.->|"dispatches kernels"| node_kernels
node_cache_manager -.->|"manages storage"| node_cache_storage
node_cache_manager -->|"supports offload"| node_offload
node_engine -.->|"coordinates chains"| node_chain_cache
node_engine -.->|"runs prefill"| node_prefill
node_engine -.->|"supports parallelism"| node_distributed
node_model_runner -.->|"uses recurrent state"| node_recurrent
node_input -.->|"handles media"| node_multimodal

click node_client "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/entrypoints/openai/client.py"
click node_openai "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/entrypoints/openai/api_server.py"
click node_routes "https://github.com/currentf/sparseengine/tree/main/src/sparseengine/entrypoints/openai/routes"
click node_serving_handlers "https://github.com/currentf/sparseengine/tree/main/src/sparseengine/entrypoints/openai/serving"
click node_llm_api "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/llm.py"
click node_config "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/config.py"
click node_engine "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/llm_engine.py"
click node_scheduler "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/scheduler.py"
click node_input "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/input_processor.py"
click node_sequence "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/sequence.py"
click node_async_sched "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/async_scheduling/scheduler.py"
click node_model_runner "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/model_runner.py"
click node_models "https://github.com/currentf/sparseengine/tree/main/src/sparseengine/models"
click node_layers "https://github.com/currentf/sparseengine/tree/main/src/sparseengine/layers"
click node_kernels "https://github.com/currentf/sparseengine/tree/main/src/sparseengine/kernels"
click node_cache_manager "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/cache_manager/standard.py"
click node_sparse_methods "https://github.com/currentf/sparseengine/tree/main/src/sparseengine/engine/sparse_methods"
click node_cache_storage "https://github.com/currentf/sparseengine/tree/main/src/sparseengine/engine/cache_manager/storage"
click node_offload "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/cache_manager/methods/omnikv/storage.py"
click node_chain_cache "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/chain_cache.py"
click node_prefill "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/prefill.py"
click node_distributed "https://github.com/currentf/sparseengine/tree/main/src/sparseengine/distributed"
click node_recurrent "https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/recurrent_state_manager.py"
click node_multimodal "https://github.com/currentf/sparseengine/tree/main/src/sparseengine/multimodal"

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_client,node_openai,node_routes,node_serving_handlers toneBlue
class node_llm_api,node_config,node_engine,node_scheduler,node_input,node_sequence,node_async_sched,node_prefill toneAmber
class node_cache_manager,node_sparse_methods,node_cache_storage,node_offload,node_chain_cache,node_user toneMint
class node_model_runner,node_models,node_layers,node_kernels toneRose
class node_distributed,node_recurrent,node_multimodal toneIndigo
```

## Components

### Serving & API

- [Request client](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/entrypoints/openai/client.py): `src/sparseengine/entrypoints/openai/client.py`
- [OpenAI API](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/entrypoints/openai/api_server.py): `src/sparseengine/entrypoints/openai/api_server.py`
- [API routes](https://github.com/currentf/sparseengine/tree/main/src/sparseengine/entrypoints/openai/routes): `src/sparseengine/entrypoints/openai/routes`
- [Request handlers](https://github.com/currentf/sparseengine/tree/main/src/sparseengine/entrypoints/openai/serving): `src/sparseengine/entrypoints/openai/serving`

### Inference Runtime

- [LLM interface](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/llm.py): `src/sparseengine/llm.py`
- [Runtime config](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/config.py): `src/sparseengine/config.py`
- [Generation engine](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/llm_engine.py): `src/sparseengine/engine/llm_engine.py`
- [Request scheduler](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/scheduler.py): `src/sparseengine/engine/scheduler.py`
- [Input processing](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/input_processor.py): `src/sparseengine/engine/input_processor.py`
- [Sequence state](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/sequence.py): `src/sparseengine/engine/sequence.py`
- [Async scheduling](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/async_scheduling/scheduler.py): `src/sparseengine/engine/async_scheduling/scheduler.py`
- [Prefill processing](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/prefill.py): `src/sparseengine/engine/prefill.py`

### Cache & Sparsity

- [KV cache manager](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/cache_manager/standard.py): `src/sparseengine/engine/cache_manager/standard.py`
- [Sparse methods](https://github.com/currentf/sparseengine/tree/main/src/sparseengine/engine/sparse_methods): `src/sparseengine/engine/sparse_methods`
- [Cache storage](https://github.com/currentf/sparseengine/tree/main/src/sparseengine/engine/cache_manager/storage): `src/sparseengine/engine/cache_manager/storage`
- [KV offload](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/cache_manager/methods/omnikv/storage.py): `src/sparseengine/engine/cache_manager/methods/omnikv/storage.py`
- [Prefix & chain cache](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/chain_cache.py): `src/sparseengine/engine/chain_cache.py`

### Models & Compute

- [Model runner](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/model_runner.py): `src/sparseengine/engine/model_runner.py`
- [Model architectures](https://github.com/currentf/sparseengine/tree/main/src/sparseengine/models): `src/sparseengine/models`
- [Inference layers](https://github.com/currentf/sparseengine/tree/main/src/sparseengine/layers): `src/sparseengine/layers`
- [GPU kernels](https://github.com/currentf/sparseengine/tree/main/src/sparseengine/kernels): `src/sparseengine/kernels`

### Parallel & Media

- [Distributed runtime](https://github.com/currentf/sparseengine/tree/main/src/sparseengine/distributed): `src/sparseengine/distributed`
- [Recurrent state](https://github.com/currentf/sparseengine/blob/main/src/sparseengine/engine/recurrent_state_manager.py): `src/sparseengine/engine/recurrent_state_manager.py`
- [Multimodal inputs](https://github.com/currentf/sparseengine/tree/main/src/sparseengine/multimodal): `src/sparseengine/multimodal`

### Other components

- **API caller**

## Connections

- API caller → OpenAI API: sends request
- Request client → OpenAI API: calls API
- OpenAI API → API routes: dispatches
- API routes → Request handlers: routes request
- Request handlers → LLM interface: invokes generation
- LLM interface → Generation engine: creates runtime
- Generation engine → Runtime config: uses settings (evidence: `src/sparseengine/engine/llm_engine.py`)
- Generation engine → Input processing: processes input (evidence: `src/sparseengine/engine/llm_engine.py`)
- Generation engine → Request scheduler: schedules requests (evidence: `src/sparseengine/engine/llm_engine.py`)
- Generation engine → Model runner: dispatches execution (evidence: `src/sparseengine/engine/llm_engine.py`)
- Generation engine → Sequence state: tracks sequences (evidence: `src/sparseengine/engine/llm_engine.py`)
- Generation engine → Async scheduling: uses async scheduler (evidence: `src/sparseengine/engine/llm_engine.py`)
- Model runner → Model architectures: runs architectures (evidence: `src/sparseengine/engine/model_runner.py`)
- Model runner → Sparse methods: uses methods (evidence: `src/sparseengine/engine/model_runner.py`)
- Model runner → Sequence state: reads sequence state (evidence: `src/sparseengine/engine/model_runner.py`)
- Model architectures → Inference layers: uses layers
- Inference layers → GPU kernels: dispatches kernels
- KV cache manager → Cache storage: manages storage
- KV cache manager → KV offload: supports offload (evidence: `src/sparseengine/engine/cache_manager/methods/omnikv/manager.py`)
- Generation engine → Prefix & chain cache: coordinates chains (evidence: `src/sparseengine/engine/llm_engine.py`)
- Generation engine → Prefill processing: runs prefill
- Generation engine → Distributed runtime: supports parallelism
- Model runner → Recurrent state: uses recurrent state (evidence: `src/sparseengine/engine/model_runner.py`)
- Input processing → Multimodal inputs: handles media
