sglang-torchtpu¶
What it is. SGLang on Cloud TPU through TorchTPU. The repository holds two things: a fork of the SGLang engine in python/ (its scheduler, RadixAttention prefix cache and API server, synced with upstream), and the out-of-tree plugin sglang_tpu/, which supplies the TPU platform, attention backends, KV cache pools, MoE kernels, megakernels and the graph runner. Development and testing focus on TPU7x (Ironwood), and every recipe targets a tpu7x host. SGLang-JAX, which the hardware-platforms/tpu page of the SGLang documentation describes, is a separate JAX engine; its commands do not apply here. Upstream: https://github.com/sgl-project/sglang-torchtpu. The book teaches it in chapter 19 and the SGLang quickstart.
Layout¶
| Path | What lives there |
|---|---|
sglang_tpu/ |
The plugin, where all new TPU code goes: platform.py (TpuOOTPlatform), tpu_platform.py (TpuSRTPlatform), envs.py, patches.py, kv_pool.py, allocator_tpu.py |
sglang_tpu/graph_runner/, compilation/ |
TpuGraphRunner (precompiles the token and batch ladders), compile policies, the recompilation checker |
sglang_tpu/attention_pallas_*.py, sglang_tpu/attention/, kernels/ |
Attention backends (RPA v3, batched RPA, MLA, sparse MLA, GDN, KDA) and Pallas kernels |
sglang_tpu/moe/, k3_megakernel/, glm_flash_megakernel/, muse_megakernel/ |
MoE kernels (moe/pallas/gmm_v2.py and fused variants); whole-decoder megakernels |
sglang_tpu/disaggregation/ |
Prefill and decode transfer: raiden/ (TPU Sync DMA) and tpu/ (host ZMQ push) |
sglang_tpu/hooks/ |
HookRegistry hooks carried until an upstream seam merges |
python/sglang/ |
The engine fork: srt/server_args.py, srt/managers/ (scheduler), srt/mem_cache/, srt/plugins/hook_registry.py, srt/models/ |
benchmark/tpu/ |
launch_server.py and the serving recipes in recipe/configs/<model>/ |
sgl-model-gateway/, rust/ |
Rust router and gateway; Rust server components |
tools/plugin/ |
The migration ratchet: check_tpu_branches.py, inventory.py, scoreboard.py, gate_audit.py |
test/ |
run_suite.py; TPU tests under registered/tpu/ (unit/, kernels/, e2e/, nightly accuracy and performance) |
SUPPORT.md, SUPPORT_MATRIX.md |
Feature tiers; the matrix generated from registered tests |
Entry points¶
| Command, flag or variable | What it does | Source | Taught in |
|---|---|---|---|
pip install --extra-index-url https://download.pytorch.org/whl/cpu -e python/ then pip install --no-deps -e . |
Source install: the engine fork (after cp python/pyproject_tpu.toml python/pyproject.toml), then the plugin |
README.md |
Ch 19 |
docker run ... -e SGLANG_PLATFORM=tpu_oot lmsysorg/sglang:v0.1.0-tpu |
Prebuilt image with engine and plugin | README.md |
quickstart |
SGLANG_PLATFORM=tpu_oot or tpu (or unset) |
Selects TpuOOTPlatform through sglang_tpu:activate, or the in-tree TpuSRTPlatform |
pyproject.toml, sglang_tpu/__init__.py |
Ch 19, Ch 25 |
python -m benchmark.tpu.launch_server --config recipe/configs/gpt_oss_20b/server.py |
Starts a server from a recipe (server_args and runtime_env); the recipe file is benchmark/tpu/recipe/configs/gpt_oss_20b/server.py, and --config resolves only under recipe/; --dry-run prints the command |
benchmark/tpu/launch_server.py |
Ch 19 |
python -m sglang.launch_server --model-path MODEL --tp-size 8 ... |
The server on port 30000 | python/sglang/srt/server_args.py |
Ch 19, serving flags |
--attention-backend pallas_tpu_rpa_v3 (default) or pallas_tpu_rpa_batched |
Attention kernel and KV pool; batched RPA needs --page-size of at least 256 |
sglang_tpu/attention_pallas_rpa_v3.py |
Ch 19 |
--enable-torch-compile, --tpu-compile-tokens, --tpu-compile-bs, --tpu-compile-tracer {dynamo,direct} |
The graph runner and its prefill and decode ladders | graph_runner/tpu_graph_runner.py |
Ch 19 |
torch.compiler.set_stance("fail_on_recompile") after warmup |
A shape you did not warm becomes an error | sglang_tpu/tpu_platform.py |
Ch 19 |
SGLANG_TPU_CHECK_RECOMPILATION=1, SGLANG_TPU_CHECK_RECOMPILATION_SITES=1 |
Log programs compiled after warmup, with source lines | sglang_tpu/envs.py |
Ch 19 |
--attn-dp-size N |
Data-parallel attention within the --tp-size group; --dp-size N --enable-dp-attention is the deprecated spelling |
server_args.py |
Ch 19 |
--kv-cache-dtype fp8_e4m3, --mem-fraction-static, --page-size, --chunked-prefill-size, --max-running-requests |
KV dtype, HBM share, page, prefill chunk and decode batch | server_args.py, tpu_platform.py defaults |
Ch 19 |
SGLANG_TPU_USE_SEQ_ALONG_LANE_KV=1, SGLANG_TPU_USE_BLOCK_MAJOR_KV=1 |
KV pool layouts: tokens on the lane axis; all layers of a page contiguous | sglang_tpu/envs.py, kv_pool.py |
Ch 19 |
--disable-radix-cache, --enable-hierarchical-cache --hicache-io-backend raiden, --enable-cache-report, --enable-metrics |
Prefix cache off; host KV tier; cached-token counts and Prometheus metrics | server_args.py |
Ch 19, agentic guide |
--speculative-algorithm {EAGLE3,NEXTN,DFLASH,DSPARK} |
Speculative decoding on the TPU spec worker (--speculative-eagle-topk 1) |
server_args.py |
Ch 19 |
--disaggregation-mode {prefill,decode} --disaggregation-transfer-backend raiden |
Prefill and decode on separate servers over TPU Sync | sglang_tpu/disaggregation/raiden/ |
Ch 19 |
TORCH_TPU_TIER3_COMPILATION_CACHE_ROOT |
Persist compiled buckets across restarts; cold start is per-bucket compilation | SUPPORT.md |
Ch 19 |
cpu_export_jax_op(name, function, ...) |
The plugin's bridge from a JAX or Pallas kernel to a PyTorch custom op | sglang_tpu/pallas_jax_op.py |
Ch 12 |
Extension points¶
In order of preference, from the contributing guide (chapter 25):
- An existing
SRTPlatformmethod or capability flag. - A registry filled from
init_backend()(register_attention_backend,register_oot_forward,get_quantization_config). - Subclass and override through a factory.
- A small upstream seam PR, carried meanwhile as a hook in
sglang_tpu/hooks/__init__.pythroughHookRegistry(BEFORE,AFTER,AROUND,REPLACE).
tools/plugin/check_tpu_branches.py rejects a change that raises the per-file count of is_tpu() branches or sglang_tpu imports in core code against tools/plugin/tpu_debt_baseline.json.
Tests¶
python3 test/run_suite.py --hw cpu --suite base-a-test-cpu # CPU unit tests
python3 test/run_suite.py --hw tpu --suite stage-a-test-tpu-unit # kernel correctness, TPU
python3 test/run_suite.py --hw tpu --suite stage-b-test-tpu-4-v7 # end-to-end PR gate, 4-chip TPU7x host
python3 scripts/ci/tpu/support_matrix.py --strict --check SUPPORT_MATRIX.md
Every TPU test file starts with a register_tpu_ci(...) marker naming its suite; SUPPORT_MATRIX.md is generated from those markers, so a feature counts as supported only when a registered test backs it.