How sgl-project/mini-sglang works
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: engine.py Inference engine · scheduler.py Batch scheduler · register.py Model adapters & weights · __main__.py CLI entry point
Components 20
Serving path
- CLI entry point · Selects server or interactive shell mode.
python/minisgl/__main__.py - Server launcher
python/minisgl/server/launch.py - Interactive shell
python/minisgl/shell.py - OpenAI-compatible API
python/minisgl/server/api_server.py - Request message protocol · Defines frontend, backend, and tokenizer message boundaries.
python/minisgl/message/frontend.py - Tokenizer service · Can run in a dedicated process separate from GPU inference.
python/minisgl/tokenizer/server.py
Scheduling & cache
- Batch scheduler · Coordinates batching, chunked prefill, overlap scheduling, and output I/O.
python/minisgl/scheduler/scheduler.py - Prefill & decode stages · Runs initial prompt processing and iterative token generation.
python/minisgl/scheduler/prefill.py - Scheduler cache coordination
python/minisgl/scheduler/cache.py - KV cache & prefix reuse · Radix prefix reuse and MHA cache storage back scheduled requests.
python/minisgl/kvcache/radix_cache.py
Model execution
- LLM runtime interface
python/minisgl/llm/llm.py - Inference engine · Converts scheduled batches into execution and sampling results.
python/minisgl/engine/engine.py - Execution graph
python/minisgl/engine/graph.py - Model adapters & weights · Registers supported model families and shared weight/config handling.
python/minisgl/models/register.py - Neural layers · Attention, linear, and MoE layers compose model execution.
python/minisgl/layers/attention.py
GPU acceleration
- Attention backends · Selects optimized FlashAttention, FlashInfer, or TensorRT-LLM-style execution.
python/minisgl/attention/base.py - CUDA, Triton & NCCL kernels · JIT-compiled CUDA/C++ extensions, Triton MoE kernels, and NCCL bindings.
- Tensor parallel
python/minisgl/distributed/impl.py
Other components
- Hugging Face / ModelScope · Acquires model artifacts with optional ModelScope selection.
python/minisgl/utils/hf.py - Linux + NVIDIA CUDA · Requires NVIDIA GPUs, compatible CUDA drivers/toolkit, and kernel JIT support.
Dockerfile
Connections 21
- 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