How lightseekorg/tokenspeed works
1. Repository shape
This is a monorepo around a GPU LLM inference engine, split into three main implementation surfaces: the Python runtime (python/tokenspeed), the kernel library (tokenspeed-kernel), and the scheduler/control plane (tokenspeed-scheduler). There is also a standalone MLA package (tokenspeed-mla) for specialized attention kernels and benchmarking.
2. Top-level runtime path
The primary request path starts in the Python entrypoints/CLI, then enters the async serving engine (runtime/engine). From there, requests are normalized, scheduled, batched, executed, and postprocessed before token outputs are returned. The main flow is: client/API → AsyncLLM / request handler → scheduler control client → execution/model runner → sampling/detokenization/output processors.
3. Scheduler as the control plane
The scheduler is a separate C++ subsystem with Python bindings. It owns request lifecycle state, batch formation, KV-cache admission/eviction, and overlap timing. Its FSM-like design is visible in the scheduler, fsm, resource, and outside_events directories. This is the boundary where request state transitions are decided before GPU execution.
4. KV cache and memory management
Cache management is a first-class subsystem, not an implementation detail. The Python runtime has cache controllers, prefix cache abstractions, transfer pools, and storage backends; the scheduler has KV/paged cache allocators and hybrid prefix cache logic. This boundary is important for diagrams because cache ownership moves across scheduler, host memory, and GPU memory, with explicit reuse/eviction policies.
5. Model execution stack
Model execution is layered: model definitions in runtime/models, model loading in runtime/model_loader, then execution orchestration in runtime/execution, which drives the model runner, weight loading, CUDA graph capture, and distributed initialization. The model layer is built around a common base with model-specific variants for Llama, Qwen, DeepSeek, Kimi, MiniMax, multimodal, MoE, and next-generation architectures.
6. Kernel abstraction and dispatch
The tokenspeed-kernel package is the low-level portable kernel surface. It exposes a registry/selection system that chooses among backend implementations such as CUDA, Triton, FlashInfer, TRT-LLM, and vendor-specific paths. This package contains the actual attention, GEMM, MoE, quantization, sampling, routing, KV-cache, embedding, and normalization kernels used by the runtime.
7. Attention and model-specific acceleration
Attention is a major subsystem with multiple backends and model-specific variants: MHA, MLA, DeepSeek V4 attention, FlashMLA, TRT-LLM attention, and specialized KV-cache kernels. The repo also includes dedicated support for DeepSeek-style attention metadata and MLA FP8 utilities, which makes attention a distinct architectural boundary rather than a single operator.
8. Mixture-of-Experts and quantization
MoE is handled as its own subsystem spanning model code, dispatch logic, expert location, and several backend families. Quantization is similarly modular, with support for FP8, NVFP4, MXFP4, W8A8, and compressed-tensor schemes. In an architecture diagram, these sit between model execution and kernel invocation, because they affect both weight format and routing/execution behavior.
9. Parallelism and distributed communication
Distributed execution is coordinated through runtime/distributed, which includes process-group management, device communicators, and multiple communication backends. The modeling layer uses static placement annotations to generate communication rather than requiring manual parallelism code. The system supports tensor/data/expert-style coordination through NCCL-like and custom all-reduce paths, making communication a separate subsystem that the model layer targets explicitly.
10. Special runtime features
The runtime includes grammar-constrained generation, speculative decoding, multimodal input handling, sampling backends, and logprob/output processing. These are all downstream of the model forward pass but upstream of the final text/token output. They are important because they shape the request pipeline after logits are produced.
11. Disaggregated prefill/decode path
There is a dedicated runtime/pd subtree for prefill/decode disaggregation. It includes executors, decode/prefill connectors, transfer engines, and Mooncake-related transport code. This is a separate data-flow branch from the standard single-process engine and is relevant if the diagram needs to show split-phase serving.
12. Standalone MLA package
tokenspeed-mla is a focused package for MLA kernels and helpers, likely used for standalone benchmarking and specialization. It mirrors the attention-heavy part of the larger system but is intentionally separated, which suggests the repository treats MLA performance work as an independently testable surface.
13. Validation and tooling boundaries
Tests are extensive and mirror the architecture: runtime tests, kernel tests, scheduler C++/Python tests, CI suites, and agentic benchmark harnesses. Documentation and GitHub workflows show the project is organized around reproducible GPU performance, model-specific correctness, and release pipelines for Python packages and Docker images.
14. What to show in a diagram
A good architecture diagram should emphasize four boxes and their interfaces: client/CLI entrypoint, Python async runtime, C++ scheduler/control plane, and kernel/backend layer. Around those, show supporting subsystems for model loading/execution, cache management, distributed communication, sampling/output processing, and optional PD disaggregation. The main architectural theme is a control-plane/data-plane split with a pluggable, backend-driven kernel stack underneath.
Main entry points: model_runner.py Model exec · registry.py Kernel registry · request_handler.py Request flow · transformer_model.py Model defs
Components 22
Python runtime
Async serving engine and model execution surface
- CLI entrypoint · Launches serving and CLI workflows
python/tokenspeed/cli/__main__.py - Async LLM · Primary request-facing runtime orchestrator
python/tokenspeed/runtime/engine/async_llm.py - Request flow · Normalizes requests and drives scheduling/output stages
python/tokenspeed/runtime/engine/request_handler.py - Scheduler client · Runtime bridge into the external scheduler
python/tokenspeed/runtime/engine/scheduler_control_client.py - Model exec · Coordinates model loading, runners, CUDA graphs, and state
python/tokenspeed/runtime/execution/model_runner.py - Model defs · Base model abstractions and model-specific variants
python/tokenspeed/runtime/models/base/transformer_model.py - Model loader · Loads checkpoints and prepares weights
python/tokenspeed/runtime/model_loader/loader.py - Cache runtime · Host-side cache controllers, prefix cache, and transfer pools
python/tokenspeed/runtime/cache/kvstore_controller.py - Distributed comm · Process-group and collective communication layer
python/tokenspeed/runtime/distributed/process_group_manager.py - Sampling output · Sampling, detokenization, logits, and output assembly
python/tokenspeed/runtime/sampling/registry.py - Grammar/spec decode · Grammar-constrained generation and speculative decoding
python/tokenspeed/runtime/grammar/grammar_manager.py - PD path · Prefill/decode split execution and transfer path
python/tokenspeed/runtime/pd/factory.py
Kernel layer
Portable low-level operator package and backend dispatch
- Kernel registry · Selects backend implementations for operators
tokenspeed-kernel/python/tokenspeed_kernel/registry.py - Attention ops · MHA, MLA, DeepSeek, FlashInfer, Triton, and TRT-LLM attention paths
tokenspeed-kernel/python/tokenspeed_kernel/ops/attention/__init__.py - MoE ops · Expert routing and fused MoE execution backends
tokenspeed-kernel/python/tokenspeed_kernel/ops/moe/__init__.py - Quant ops · FP8, NVFP4, MXFP4, W8A8, and compressed-tensor paths
tokenspeed-kernel/python/tokenspeed_kernel/ops/quantization/__init__.py - KV-cache ops · Low-level paged/KV cache kernels used by runtime and scheduler
tokenspeed-kernel/python/tokenspeed_kernel/ops/kvcache/__init__.py - Comm ops · Collective communication backends and fused transfer paths
tokenspeed-kernel/python/tokenspeed_kernel/ops/communication/__init__.py
Scheduler control plane
C++ request lifecycle and cache admission subsystem
- Scheduler FSM · Request state transitions and batch planning core
tokenspeed-scheduler/csrc/scheduler/scheduler.cpp - Resource mgmt · KV/page allocators, prefix cache, and eviction resources
tokenspeed-scheduler/csrc/resource/allocator/kv_allocator.cpp - Scheduler events · Forward, cache, and PD event/state handling
tokenspeed-scheduler/csrc/fsm/forward_events.cpp
MLA package
Standalone MLA attention specialization package
- MLA kernels · Standalone MLA prefill/decode kernel package
tokenspeed-mla/python/tokenspeed_mla/mla_prefill.py
Connections 26
- CLI entrypoint → Async LLM: launches · CLI starts the async serving engine
- Async LLM → Request flow: routes · Engine hands requests into the internal request pipeline
- Request flow → Scheduler client: plans · Requests are sent to the scheduler control plane
- Scheduler client → Scheduler FSM: controls · Python runtime delegates lifecycle and batching decisions
- Request flow → Model exec: executes · Pipeline drives model execution after scheduling
- Model exec → Model loader: loads · Execution stack loads model weights and checkpoints
- Model exec → Model defs: instantiates · Model runner uses base and model-specific definitions
- Model exec → Cache runtime: uses · Execution depends on host-side cache control and transfers
- Model exec → Distributed comm: communicates · Distributed init and collectives support parallel execution
- Model exec → Sampling output: postprocesses · Logits are sampled, detokenized, and assembled into outputs
- Model exec → Grammar/spec decode: constrains · Generation controls shape decoding after forward passes
- Request flow → PD path: splits · Optional prefill/decode disaggregation branch
- Kernel registry → Attention ops: dispatches · Registry selects attention backend implementations
- Kernel registry → MoE ops: dispatches · Registry selects MoE backend implementations
- Kernel registry → Quant ops: dispatches · Registry selects quantization backends
- Kernel registry → KV-cache ops: dispatches · Registry selects KV-cache kernels
- Kernel registry → Comm ops: dispatches · Registry selects communication kernels
- Scheduler FSM → Resource mgmt: allocates · Scheduler owns page and KV admission resources
- Scheduler FSM → Scheduler events: transitions · FSM reacts to forward/cache/PD events
- Resource mgmt → KV-cache ops: admits · Scheduler resource policy maps to cache kernel usage
- Attention ops → MLA kernels: specializes · MLA package provides specialized attention kernels
- PD path → Comm ops: transfers · Split-phase serving relies on transfer and transport ops
- Model defs → Attention ops: targets · Model layers bind to attention backend variants
- Model defs → MoE ops: targets · MoE models bind to expert-routing kernels
- Model defs → Quant ops: targets · Model variants target quantized weight paths
- Sampling output → Kernel registry: invokes · Sampling and logits processing can call kernel-backed ops