# sgl-project/mini-sglang architecture

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

- Interactive diagram: https://gitdiagram.com/sgl-project/mini-sglang
- Repository: https://github.com/sgl-project/mini-sglang
- Video tour (about a minute): https://gitdiagram.com/sgl-project/mini-sglang/video

## Overview

#### Purpose and Entry Points
Mini-SGLang is a Linux/CUDA LLM inference and OpenAI-compatible serving framework. The primary CLI is `python -m minisgl`, rooted at `python/minisgl/__main__.py`; it launches server or shell modes through `python/minisgl/server/launch.py` and `python/minisgl/shell.py`. The HTTP boundary is `python/minisgl/server/api_server.py`.

#### Request, Tokenization, and Process Boundaries
Client requests enter the API server, are converted into frontend/backend messages, and pass through tokenization before scheduling. `python/minisgl/message/frontend.py`, `python/minisgl/message/backend.py`, and `python/minisgl/message/tokenizer.py` define this protocol boundary. Tokenization can run as a dedicated service/process via `python/minisgl/tokenizer/server.py`, separating CPU tokenizer work from GPU inference.

#### Serving Scheduler
The scheduler batches requests and coordinates prefill, iterative decode, output I/O, and cache allocation. Its central controller is `python/minisgl/scheduler/scheduler.py`; distinct execution stages live in `python/minisgl/scheduler/prefill.py` and `python/minisgl/scheduler/decode.py`. This layer implements chunked prefill and overlap scheduling, bridging asynchronous request state to engine-ready model batches.

#### KV Cache and Prefix Reuse
KV-cache implementations provide the scheduler’s memory and reuse boundary. `python/minisgl/kvcache/radix_cache.py` supports shared-prefix lookup/reuse, while `python/minisgl/kvcache/mha_pool.py` manages multi-head-attention cache storage and `python/minisgl/kvcache/naive_cache.py` provides a simpler cache mode. Scheduler cache coordination is in `python/minisgl/scheduler/cache.py`.

#### Inference Engine and Execution Graph
The engine converts scheduled batches into GPU execution and sampling results. `python/minisgl/engine/engine.py` is the runtime coordinator, `python/minisgl/engine/graph.py` captures execution-graph behavior, and `python/minisgl/engine/sample.py` performs generation sampling. `python/minisgl/llm/llm.py` provides the higher-level model-facing runtime interface.

#### Model Loading and Neural Network Composition
Model adapters support Llama, Mistral, Qwen2, Qwen3, and Qwen3-MoE families through registration and shared weight/config machinery: `python/minisgl/models/register.py`, `python/minisgl/models/weight.py`, and `python/minisgl/models/qwen3_moe.py`. These compose reusable GPU-oriented layers such as attention, linear projections, norms, embeddings, rotary position encoding, and MoE in `python/minisgl/layers/attention.py`, `python/minisgl/layers/linear.py`, and `python/minisgl/layers/moe.py`.

#### Attention, Kernels, and Distributed Execution
Attention backends abstract optimized FlashAttention, FlashInfer, and TensorRT-LLM-style implementations in `python/minisgl/attention/base.py`, `python/minisgl/attention/fa.py`, and `python/minisgl/attention/fi.py`. CUDA/C++ extensions, Triton MoE kernels, tensor operations, radix primitives, and NCCL bindings reside under `python/minisgl/kernel/csrc/`, `python/minisgl/kernel/triton/fused_moe.py`, and `python/minisgl/kernel/pynccl.py`. Tensor parallel coordination is implemented in `python/minisgl/distributed/impl.py`.

#### Runtime and External Dependencies
The system requires Linux, NVIDIA GPUs, CUDA toolkit/driver compatibility, and JIT compilation of CUDA kernels. It integrates Hugging Face model acquisition, with optional ModelScope source selection, through `python/minisgl/utils/hf.py`. Docker deployment is defined by `Dockerfile` and expects NVIDIA Container Toolkit access to GPUs; persistent Hugging Face, TVM FFI, and FlashInfer caches improve startup behavior.

Main entry points: [`python/minisgl/engine/engine.py`](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/engine/engine.py) (Inference engine), [`python/minisgl/scheduler/scheduler.py`](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/scheduler/scheduler.py) (Batch scheduler), [`python/minisgl/models/register.py`](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/models/register.py) (Model adapters & weights), [`python/minisgl/__main__.py`](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/__main__.py) (CLI entry point)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_serving["Serving path"]
  node_cli["CLI entry point<br/>Python CLI<br/>[__main__.py]"]
  node_launch["Server launcher<br/>bootstrap<br/>[launch.py]"]
  node_shell["Interactive shell<br/>shell mode<br/>[shell.py]"]
  node_api["OpenAI-compatible API<br/>HTTP server<br/>[api_server.py]"]
  node_messages["Request message protocol<br/>frontend/backend messages<br/>[frontend.py]"]
  node_tokenizer["Tokenizer service<br/>CPU worker<br/>[server.py]"]
end

subgraph group_scheduling["Scheduling &amp; cache"]
  node_scheduler["Batch scheduler<br/>request coordinator<br/>[scheduler.py]"]
  node_prefill_decode["Prefill &amp; decode stages<br/>execution stages<br/>[prefill.py]"]
  node_cache_coordination["Scheduler cache coordination<br/>cache allocator<br/>[cache.py]"]
  node_kv_cache[("KV cache &amp; prefix reuse<br/>cache storage<br/>[radix_cache.py]")]
end

subgraph group_runtime["Model execution"]
  node_llm["LLM runtime interface<br/>model API<br/>[llm.py]"]
  node_engine["Inference engine<br/>GPU runtime coordinator<br/>[engine.py]"]
  node_execution_graph["Execution graph<br/>graph capture<br/>[graph.py]"]
  node_models["Model adapters &amp; weights<br/>model registry<br/>[register.py]"]
  node_layers["Neural layers<br/>GPU layer composition<br/>[attention.py]"]
end

subgraph group_acceleration["GPU acceleration"]
  node_attention["Attention backends<br/>attention abstraction<br/>[base.py]"]
  node_kernels{{"CUDA, Triton &amp; NCCL kernels<br/>native acceleration"}}
  node_tensor_parallel["Tensor parallel<br/>distributed coordination<br/>[impl.py]"]
end

node_model_hub[("Hugging Face / ModelScope<br/>model source<br/>[hf.py]")]
node_cuda_runtime{{"Linux + NVIDIA CUDA<br/>deployment runtime"}}

node_cli -->|"server mode"| node_launch
node_cli -->|"shell mode"| node_shell
node_launch -->|"starts"| node_api
node_api -->|"converts requests"| node_messages
node_messages -->|"tokenization messages"| node_tokenizer
node_tokenizer -->|"tokenized requests"| node_scheduler
node_scheduler -->|"dispatches batches"| node_prefill_decode
node_scheduler -->|"allocates cache"| node_cache_coordination
node_cache_coordination -->|"lookup and reuse"| node_kv_cache
node_prefill_decode -->|"engine-ready batches"| node_engine
node_scheduler -->|"model runtime"| node_llm
node_llm -->|"executes through"| node_engine
node_engine -->|"captures execution"| node_execution_graph
node_engine -->|"runs loaded model"| node_models
node_models -->|"loads weights"| node_model_hub
node_models -->|"composes"| node_layers
node_layers -->|"uses"| node_attention
node_attention -->|"accelerated by"| node_kernels
node_engine -->|"coordinates distributed execution"| node_tensor_parallel
node_tensor_parallel -->|"uses NCCL"| node_kernels
node_kernels -->|"runs on"| node_cuda_runtime

click node_cli "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/__main__.py"
click node_launch "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/server/launch.py"
click node_shell "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/shell.py"
click node_api "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/server/api_server.py"
click node_messages "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/message/frontend.py"
click node_tokenizer "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/tokenizer/server.py"
click node_scheduler "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/scheduler/scheduler.py"
click node_prefill_decode "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/scheduler/prefill.py"
click node_cache_coordination "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/scheduler/cache.py"
click node_kv_cache "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/kvcache/radix_cache.py"
click node_llm "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/llm/llm.py"
click node_engine "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/engine/engine.py"
click node_execution_graph "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/engine/graph.py"
click node_models "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/models/register.py"
click node_layers "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/layers/attention.py"
click node_attention "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/attention/base.py"
click node_tensor_parallel "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/distributed/impl.py"
click node_model_hub "https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/utils/hf.py"
click node_cuda_runtime "https://github.com/sgl-project/mini-sglang/blob/main/Dockerfile"

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_launch,node_shell,node_api,node_messages,node_tokenizer toneBlue
class node_scheduler,node_prefill_decode,node_cache_coordination,node_kv_cache toneAmber
class node_llm,node_engine,node_execution_graph,node_models,node_layers toneMint
class node_attention,node_kernels,node_tensor_parallel toneRose
class node_model_hub,node_cuda_runtime toneNeutral
```

## Components

### Serving path

- [CLI entry point](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/__main__.py): Python CLI · Selects server or interactive shell mode. · `python/minisgl/__main__.py`
- [Server launcher](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/server/launch.py): bootstrap · `python/minisgl/server/launch.py`
- [Interactive shell](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/shell.py): shell mode · `python/minisgl/shell.py`
- [OpenAI-compatible API](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/server/api_server.py): HTTP server · `python/minisgl/server/api_server.py`
- [Request message protocol](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/message/frontend.py): frontend/backend messages · Defines frontend, backend, and tokenizer message boundaries. · `python/minisgl/message/frontend.py`
- [Tokenizer service](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/tokenizer/server.py): CPU worker · Can run in a dedicated process separate from GPU inference. · `python/minisgl/tokenizer/server.py`

### Scheduling & cache

- [Batch scheduler](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/scheduler/scheduler.py): request coordinator · Coordinates batching, chunked prefill, overlap scheduling, and output I/O. · `python/minisgl/scheduler/scheduler.py`
- [Prefill & decode stages](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/scheduler/prefill.py): execution stages · Runs initial prompt processing and iterative token generation. · `python/minisgl/scheduler/prefill.py`
- [Scheduler cache coordination](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/scheduler/cache.py): cache allocator · `python/minisgl/scheduler/cache.py`
- [KV cache & prefix reuse](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/kvcache/radix_cache.py): cache storage · Radix prefix reuse and MHA cache storage back scheduled requests. · `python/minisgl/kvcache/radix_cache.py`

### Model execution

- [LLM runtime interface](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/llm/llm.py): model API · `python/minisgl/llm/llm.py`
- [Inference engine](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/engine/engine.py): GPU runtime coordinator · Converts scheduled batches into execution and sampling results. · `python/minisgl/engine/engine.py`
- [Execution graph](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/engine/graph.py): graph capture · `python/minisgl/engine/graph.py`
- [Model adapters & weights](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/models/register.py): model registry · Registers supported model families and shared weight/config handling. · `python/minisgl/models/register.py`
- [Neural layers](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/layers/attention.py): GPU layer composition · Attention, linear, and MoE layers compose model execution. · `python/minisgl/layers/attention.py`

### GPU acceleration

- [Attention backends](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/attention/base.py): attention abstraction · Selects optimized FlashAttention, FlashInfer, or TensorRT-LLM-style execution. · `python/minisgl/attention/base.py`
- **CUDA, Triton & NCCL kernels**: native acceleration · JIT-compiled CUDA/C++ extensions, Triton MoE kernels, and NCCL bindings.
- [Tensor parallel](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/distributed/impl.py): distributed coordination · `python/minisgl/distributed/impl.py`

### Other components

- [Hugging Face / ModelScope](https://github.com/sgl-project/mini-sglang/blob/main/python/minisgl/utils/hf.py): model source · Acquires model artifacts with optional ModelScope selection. · `python/minisgl/utils/hf.py`
- [Linux + NVIDIA CUDA](https://github.com/sgl-project/mini-sglang/blob/main/Dockerfile): deployment runtime · Requires NVIDIA GPUs, compatible CUDA drivers/toolkit, and kernel JIT support. · `Dockerfile`

## Connections

- CLI entry point → Server launcher: server mode
- CLI entry point → Interactive shell: shell mode
- Server launcher → OpenAI-compatible API: starts
- OpenAI-compatible API → Request message protocol: converts requests
- Request message protocol → Tokenizer service: tokenization messages
- Tokenizer service → Batch scheduler: tokenized requests
- Batch scheduler → Prefill & decode stages: dispatches batches
- Batch scheduler → Scheduler cache coordination: allocates cache
- Scheduler cache coordination → KV cache & prefix reuse: lookup and reuse
- Prefill & decode stages → Inference engine: engine-ready batches
- Batch scheduler → LLM runtime interface: model runtime
- LLM runtime interface → Inference engine: executes through
- Inference engine → Execution graph: captures execution
- Inference engine → Model adapters & weights: runs loaded model
- Model adapters & weights → Hugging Face / ModelScope: loads weights
- Model adapters & weights → Neural layers: composes
- Neural layers → Attention backends: uses
- Attention backends → CUDA, Triton & NCCL kernels: accelerated by
- Inference engine → Tensor parallel: coordinates distributed execution
- Tensor parallel → CUDA, Triton & NCCL kernels: uses NCCL
- CUDA, Triton & NCCL kernels → Linux + NVIDIA CUDA: runs on
