Serve a first model with SGLang-TorchTPU¶
Use this guide when you want SGLang's server, scheduler and RadixAttention prefix cache on a TPU7x (Ironwood) host, launched from one of the repository's recipes and verified with a request. SGLang-TorchTPU is an out-of-tree plugin under unmodified SGLang, much as vLLM-TorchTPU is under vLLM, and its recipes are Python files that render to sglang.launch_server flags. Chapter 19 explains the plugin, the two compile ladders and how it differs from vLLM-TorchTPU; Serve agents with SGLang-TorchTPU continues from here.
Two TPU backends for SGLang
SGLang also runs on TPU through SGLang-JAX, a separate JAX engine. The hardware-platforms/tpu page in the SGLang documentation tree describes SGLang-JAX (pip install sglang-jax), not SGLang-TorchTPU. Use the SGLang-TorchTPU README and recipes for the commands in this guide.
Prerequisites¶
- A TPU7x host: 4 chips and 8 TensorCores. Development and testing focus on TPU7x; treat other generations as untested. For provisioning, see Serve a first model with vLLM-TorchTPU, step 1, and its note on v7x provisioning.
- For the pip route: Linux x86_64 and Python 3.11 to 3.14.
- Optional: this book's
code/ch19_sglang/(source) andcode/ch18_vllm/client.py.
Steps¶
1. Install the engine and the plugin¶
Pick one route.
Docker. The image has the engine and plugin installed and starts in the repository root, /sgl-workspace/sglang:
docker pull lmsysorg/sglang:v0.1.0-tpu
docker run -it --rm --privileged --net=host --shm-size=64g \
-e SGLANG_PLATFORM=tpu_oot \
lmsysorg/sglang:v0.1.0-tpu
pip. Install both wheels of the v0.1.0 release together; sglang here is the repository's build, not the PyPI package:
pip install --extra-index-url https://download.pytorch.org/whl/cpu \
https://github.com/sgl-project/sglang-torchtpu/releases/download/tpu-v0.1.0/sglang-0.5.22-py3-none-any.whl \
https://github.com/sgl-project/sglang-torchtpu/releases/download/tpu-v0.1.0/sglang_tpu-0.1.0-py3-none-any.whl
From source. Install the engine with the TPU dependency manifest, then the plugin without re-resolving dependencies:
git clone https://github.com/sgl-project/sglang-torchtpu.git
cd sglang-torchtpu
cp python/pyproject_tpu.toml python/pyproject.toml
pip install --extra-index-url https://download.pytorch.org/whl/cpu -e python/
pip install --no-deps -e .
The recipes live in the repository, so clone it even after a pip install, and run everything below from its root.
Check: you are in the repository root (/sgl-workspace/sglang in the container), and benchmark/tpu/recipe/configs/gpt_oss_20b/server.py exists.
2. See what the recipe resolves to¶
A recipe module defines CONFIG with server_args (rendered to sglang.launch_server flags) and runtime_env (the server's environment). --dry-run prints the resolved command and environment as JSON and exits:
python -m benchmark.tpu.launch_server --config recipe/configs/gpt_oss_20b/server.py --dry-run
For GPT-OSS-20B the command is:
python -m sglang.launch_server --device tpu --dtype bfloat16 --host 127.0.0.1 --port 30000 \
--grammar-backend none --attention-backend pallas_tpu_rpa_v3 --page-size 64 \
--max-running-requests 64 --chunked-prefill-size 4096 --max-prefill-tokens 4096 \
--context-length 9236 --torch-compile-max-bs 64 --enable-torch-compile \
--kv-cache-dtype fp8_e4m3 --model-path openai/gpt-oss-20b --served-model-name gpt-oss-20b \
--tp-size 8 --ep-size 8 --mem-fraction-static 0.6 --tpu-compile-tracer direct
The recipe's comments explain the choices: EP 8 rather than TP for the 32 experts; "quantization": None to keep the checkpoint's MXFP4; page 64 as "the granularity a radix prefix match is floored to"; direct tracing; and "disable_radix_cache": False to serve with prefix caching, which the base config turns off for throughput benchmarks. The environment adds SGLANG_TPU_FUSED_MOE_SC_GATHER=0. The book's builder prints the same flags without a TPU:
python code/ch19_sglang/launch_profiles.py --profile gpt-oss-20b-server
Check: the resolved command has no --disable-radix-cache if you want prefix caching, and --tp-size equals the TensorCores you have (8 per host).
3. Launch the server¶
python -m benchmark.tpu.launch_server --config recipe/configs/gpt_oss_20b/server.py
--runtime-envs '{"NAME":"value"}' overrides runtime_env entries (null removes one). Warmup compiles one program per prefill token bucket and per decode batch-size bucket; SUPPORT.md says "Cold start is dominated by per-bucket compilation; use the compile cache", so persist TORCH_TPU_TIER3_COMPILATION_CACHE_ROOT on a disk that survives restarts. The builder adds it:
python code/ch19_sglang/launch_profiles.py --profile gpt-oss-20b-server --cache-root "$HOME/tpu-cache"
Check: the server reports it is ready and listens on port 30000. A restart with the cache root set starts faster than the first run.
4. Send a request¶
curl http://localhost:30000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model": "gpt-oss-20b", "messages": [{"role": "user", "content": "Hello"}]}'
Or with chapter 18's client, pointed at port 30000:
python code/ch18_vllm/client.py smoke --base-url http://localhost:30000 --model gpt-oss-20b
Check: a coherent reply. The model name is the recipe's --served-model-name, gpt-oss-20b. Health probes use GET /health.
5. Catch compiles that warmup missed¶
After warmup the platform freezes the compiler (torch.compiler.set_stance("fail_on_recompile")), so a shape outside the ladders is an error, not a silent stall. In CI, also set SGLANG_TPU_CHECK_RECOMPILATION=1 ("Logs every XLA program compiled or loaded after warmup") and SGLANG_TPU_CHECK_RECOMPILATION_SITES=1 to attribute each compile to a source line. The builder's --ci flag sets the first.
Check: a CI run under realistic traffic logs no compiles after warmup. If a shape fails, end --tpu-compile-tokens at --chunked-prefill-size and --tpu-compile-bs at --max-running-requests, as the recipes do.
Troubleshooting¶
| Symptom | Cause | Fix |
|---|---|---|
| Commands from SGLang's TPU docs page fail | That page describes SGLang-JAX | Use the SGLang-TorchTPU README and recipes |
The wrong sglang is installed |
The PyPI package instead of the repository's build | Install both release wheels together, or from source |
--config cannot find the recipe |
Run outside the repository root; module paths resolve only under recipe/, recipe. or benchmark.tpu.recipe. |
Run from the repository root |
| TTFT grows with every turn of a conversation | Radix cache off: a recipe inherited --disable-radix-cache from the base config |
Use server.py, or set "disable_radix_cache": False in your recipe |
| Server errors on a request shape after warmup | fail_on_recompile: a token count or batch size outside the ladders |
Extend --tpu-compile-tokens and --tpu-compile-bs to their limits |
| Startup takes many minutes every time | Per-bucket compilation, no persistent cache | Persist TORCH_TPU_TIER3_COMPILATION_CACHE_ROOT; fewer ladder entries |
| Startup error about the page size | Batched RPA needs --page-size of at least 256 |
Use 256 with pallas_tpu_rpa_batched |
response_format ignored, tool calls malformed |
Recipes set --grammar-backend none; structured output and function calling are Experimental |
Check --grammar-backend and --tool-call-parser; validate on the client |
References¶
- SGLang-TorchTPU: README
- SGLang-TorchTPU: TPU serving recipes
- SGLang-TorchTPU: Supported models and features
- SGLang-TorchTPU: PyTorch-Native Production Serving on TPUs
- llm-d: SGLang in llm-d
- SGLang-TorchTPU repository
- Companion code: code/ch19_sglang/
- Companion code: code/ch18_vllm/
- Chapter 19: SGLang-TorchTPU