# lightseekorg/tokenspeed architecture

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

- Interactive diagram: https://gitdiagram.com/lightseekorg/tokenspeed
- Repository: https://github.com/lightseekorg/tokenspeed

## Overview

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: [`python/tokenspeed/runtime/execution/model_runner.py`](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/execution/model_runner.py) (Model exec), [`tokenspeed-kernel/python/tokenspeed_kernel/registry.py`](https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/registry.py) (Kernel registry), [`python/tokenspeed/runtime/engine/request_handler.py`](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/engine/request_handler.py) (Request flow), [`python/tokenspeed/runtime/models/base/transformer_model.py`](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/models/base/transformer_model.py) (Model defs)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_python_runtime["Python runtime"]
  node_cli_entry["CLI entrypoint<br/>python entrypoints<br/>[__main__.py]"]
  node_async_llm["Async LLM<br/>async serving<br/>[async_llm.py]"]
  node_request_flow["Request flow<br/>request pipeline<br/>[request_handler.py]"]
  node_scheduler_client["Scheduler client<br/>control client"]
  node_model_exec["Model exec<br/>execution stack<br/>[model_runner.py]"]
  node_model_defs["Model defs<br/>model zoo"]
  node_model_loader["Model loader<br/>weights loader<br/>[loader.py]"]
  node_cache_runtime[("Cache runtime<br/>cache mgmt")]
  node_dist_comm["Distributed comm<br/>parallel comms"]
  node_sampling_output["Sampling output<br/>postprocessing<br/>[registry.py]"]
  node_grammar_spec["Grammar/spec decode<br/>generation controls<br/>[grammar_manager.py]"]
  node_pd_path["PD path<br/>disaggregated serving<br/>[factory.py]"]
end

subgraph group_kernel_layer["Kernel layer"]
  node_kernel_registry["Kernel registry<br/>op dispatch<br/>[registry.py]"]
  node_attention_ops["Attention ops<br/>attention kernels<br/>[__init__.py]"]
  node_moe_ops["MoE ops<br/>expert kernels<br/>[__init__.py]"]
  node_quant_ops["Quant ops<br/>quantization kernels<br/>[__init__.py]"]
  node_kvcache_ops["KV-cache ops<br/>cache kernels<br/>[__init__.py]"]
  node_comm_ops["Comm ops<br/>communication kernels<br/>[__init__.py]"]
end

subgraph group_scheduler_cp["Scheduler control plane"]
  node_scheduler_fsm["Scheduler FSM<br/>request lifecycle<br/>[scheduler.cpp]"]
  node_resource_mgmt[("Resource mgmt<br/>cache allocator<br/>[kv_allocator.cpp]")]
  node_scheduler_events["Scheduler events<br/>event model<br/>[forward_events.cpp]"]
end

subgraph group_mla_pkg["MLA package"]
  node_mla_lib["MLA kernels<br/>mla attention<br/>[mla_prefill.py]"]
end

node_cli_entry -->|"launches"| node_async_llm
node_async_llm -->|"routes"| node_request_flow
node_request_flow -->|"plans"| node_scheduler_client
node_scheduler_client -->|"controls"| node_scheduler_fsm
node_request_flow -->|"executes"| node_model_exec
node_model_exec -->|"loads"| node_model_loader
node_model_exec -->|"instantiates"| node_model_defs
node_model_exec -->|"uses"| node_cache_runtime
node_model_exec -->|"communicates"| node_dist_comm
node_model_exec -->|"postprocesses"| node_sampling_output
node_model_exec -->|"constrains"| node_grammar_spec
node_request_flow -.->|"splits"| node_pd_path
node_kernel_registry -->|"dispatches"| node_attention_ops
node_kernel_registry -->|"dispatches"| node_moe_ops
node_kernel_registry -->|"dispatches"| node_quant_ops
node_kernel_registry -->|"dispatches"| node_kvcache_ops
node_kernel_registry -->|"dispatches"| node_comm_ops
node_scheduler_fsm -->|"allocates"| node_resource_mgmt
node_scheduler_fsm -->|"transitions"| node_scheduler_events
node_resource_mgmt -.->|"admits"| node_kvcache_ops
node_attention_ops -.->|"specializes"| node_mla_lib
node_pd_path -->|"transfers"| node_comm_ops
node_model_defs -->|"targets"| node_attention_ops
node_model_defs -->|"targets"| node_moe_ops
node_model_defs -->|"targets"| node_quant_ops
node_sampling_output -.->|"invokes"| node_kernel_registry

click node_cli_entry "https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/cli/__main__.py"
click node_async_llm "https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/engine/async_llm.py"
click node_request_flow "https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/engine/request_handler.py"
click node_scheduler_client "https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/engine/scheduler_control_client.py"
click node_model_exec "https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/execution/model_runner.py"
click node_model_defs "https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/models/base/transformer_model.py"
click node_model_loader "https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/model_loader/loader.py"
click node_cache_runtime "https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/cache/kvstore_controller.py"
click node_dist_comm "https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/distributed/process_group_manager.py"
click node_sampling_output "https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/sampling/registry.py"
click node_grammar_spec "https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/grammar/grammar_manager.py"
click node_pd_path "https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/pd/factory.py"
click node_kernel_registry "https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/registry.py"
click node_attention_ops "https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/ops/attention/__init__.py"
click node_moe_ops "https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/ops/moe/__init__.py"
click node_quant_ops "https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/ops/quantization/__init__.py"
click node_kvcache_ops "https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/ops/kvcache/__init__.py"
click node_comm_ops "https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/ops/communication/__init__.py"
click node_scheduler_fsm "https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-scheduler/csrc/scheduler/scheduler.cpp"
click node_resource_mgmt "https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-scheduler/csrc/resource/allocator/kv_allocator.cpp"
click node_scheduler_events "https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-scheduler/csrc/fsm/forward_events.cpp"
click node_mla_lib "https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-mla/python/tokenspeed_mla/mla_prefill.py"

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_entry,node_async_llm,node_request_flow,node_scheduler_client,node_model_exec,node_model_defs,node_model_loader,node_cache_runtime,node_dist_comm,node_sampling_output,node_grammar_spec,node_pd_path toneBlue
class node_kernel_registry,node_attention_ops,node_moe_ops,node_quant_ops,node_kvcache_ops,node_comm_ops toneAmber
class node_scheduler_fsm,node_resource_mgmt,node_scheduler_events toneMint
class node_mla_lib toneRose
```

## Components

### Python runtime

Async serving engine and model execution surface

- [CLI entrypoint](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/cli/__main__.py): python entrypoints · Launches serving and CLI workflows · `python/tokenspeed/cli/__main__.py`
- [Async LLM](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/engine/async_llm.py): async serving · Primary request-facing runtime orchestrator · `python/tokenspeed/runtime/engine/async_llm.py`
- [Request flow](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/engine/request_handler.py): request pipeline · Normalizes requests and drives scheduling/output stages · `python/tokenspeed/runtime/engine/request_handler.py`
- [Scheduler client](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/engine/scheduler_control_client.py): control client · Runtime bridge into the external scheduler · `python/tokenspeed/runtime/engine/scheduler_control_client.py`
- [Model exec](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/execution/model_runner.py): execution stack · Coordinates model loading, runners, CUDA graphs, and state · `python/tokenspeed/runtime/execution/model_runner.py`
- [Model defs](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/models/base/transformer_model.py): model zoo · Base model abstractions and model-specific variants · `python/tokenspeed/runtime/models/base/transformer_model.py`
- [Model loader](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/model_loader/loader.py): weights loader · Loads checkpoints and prepares weights · `python/tokenspeed/runtime/model_loader/loader.py`
- [Cache runtime](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/cache/kvstore_controller.py): cache mgmt · Host-side cache controllers, prefix cache, and transfer pools · `python/tokenspeed/runtime/cache/kvstore_controller.py`
- [Distributed comm](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/distributed/process_group_manager.py): parallel comms · Process-group and collective communication layer · `python/tokenspeed/runtime/distributed/process_group_manager.py`
- [Sampling output](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/sampling/registry.py): postprocessing · Sampling, detokenization, logits, and output assembly · `python/tokenspeed/runtime/sampling/registry.py`
- [Grammar/spec decode](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/grammar/grammar_manager.py): generation controls · Grammar-constrained generation and speculative decoding · `python/tokenspeed/runtime/grammar/grammar_manager.py`
- [PD path](https://github.com/lightseekorg/tokenspeed/blob/main/python/tokenspeed/runtime/pd/factory.py): disaggregated serving · 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](https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/registry.py): op dispatch · Selects backend implementations for operators · `tokenspeed-kernel/python/tokenspeed_kernel/registry.py`
- [Attention ops](https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/ops/attention/__init__.py): attention kernels · MHA, MLA, DeepSeek, FlashInfer, Triton, and TRT-LLM attention paths · `tokenspeed-kernel/python/tokenspeed_kernel/ops/attention/__init__.py`
- [MoE ops](https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/ops/moe/__init__.py): expert kernels · Expert routing and fused MoE execution backends · `tokenspeed-kernel/python/tokenspeed_kernel/ops/moe/__init__.py`
- [Quant ops](https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/ops/quantization/__init__.py): quantization kernels · FP8, NVFP4, MXFP4, W8A8, and compressed-tensor paths · `tokenspeed-kernel/python/tokenspeed_kernel/ops/quantization/__init__.py`
- [KV-cache ops](https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/ops/kvcache/__init__.py): cache kernels · Low-level paged/KV cache kernels used by runtime and scheduler · `tokenspeed-kernel/python/tokenspeed_kernel/ops/kvcache/__init__.py`
- [Comm ops](https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-kernel/python/tokenspeed_kernel/ops/communication/__init__.py): communication kernels · 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](https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-scheduler/csrc/scheduler/scheduler.cpp): request lifecycle · Request state transitions and batch planning core · `tokenspeed-scheduler/csrc/scheduler/scheduler.cpp`
- [Resource mgmt](https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-scheduler/csrc/resource/allocator/kv_allocator.cpp): cache allocator · KV/page allocators, prefix cache, and eviction resources · `tokenspeed-scheduler/csrc/resource/allocator/kv_allocator.cpp`
- [Scheduler events](https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-scheduler/csrc/fsm/forward_events.cpp): event model · Forward, cache, and PD event/state handling · `tokenspeed-scheduler/csrc/fsm/forward_events.cpp`

### MLA package

Standalone MLA attention specialization package

- [MLA kernels](https://github.com/lightseekorg/tokenspeed/blob/main/tokenspeed-mla/python/tokenspeed_mla/mla_prefill.py): mla attention · Standalone MLA prefill/decode kernel package · `tokenspeed-mla/python/tokenspeed_mla/mla_prefill.py`

## Connections

- 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
