Skip to content

Serve a first model with vLLM-TorchTPU

Use this guide when you have a Cloud TPU VM (or are about to create one) and want an OpenAI-compatible vllm serve endpoint and an offline batch running on it, each verified with a request. vLLM-TorchTPU is a platform plugin: you install it next to vLLM and run the ordinary vllm serve command. Chapter 18 explains the plugin, the warmup compiles and the flags; this guide is the shortest path to a server that answers.

Prerequisites

  • A Google Cloud project with billing enabled, the gcloud CLI authenticated, the Cloud TPU and Compute Engine APIs enabled, and TPU quota in your zone (IAM & Admin, Quotas & System Limits).
  • A TPU generation. The vLLM-TorchTPU setup guide calls TPU v7x (Ironwood) recommended and validated in CI, TPU v6e (Trillium) supported but not covered by CI, and v5e, v5p and v4 experimental.
  • Python 3.12 on the VM (>=3.12,<3.13): the tpu-sync-torch and torch_tpu wheels target CPython 3.12.
  • Optional: this book's code/ch18_vllm/ (source), which builds the commands below and times requests.

v7x provisioning in the public documentation

The public documents differ on how TPU v7x is provisioned. The vLLM-TorchTPU Cloud TPU Setup page generates queued-resource commands for v7x. Google Cloud's TPU queued resources documentation says the Cloud TPU API does not support Ironwood (TPU7x) or later generations, and TorchTPU's VM guide says GKE "is necessary to access recent TPU hardware". Plan on GKE for v7x; chapter 8 explains the choice.

Steps

1. Provision the TPU VM

The two generations use different APIs: Compute Engine (gcloud compute instances create) for v6e and v5p, and queued resources through the Cloud TPU API (gcloud alpha compute tpus queued-resources create) for v7x, v5e and v4. The setup page generates the create command for your generation, chip count and capacity model; for v6e, the full command with flex-start, spot and on-demand variants is in Install TorchTPU on a TPU VM, step 2.

gcloud auth login
gcloud config set project PROJECT_ID
gcloud services enable tpu.googleapis.com compute.googleapis.com

Check the state, then connect:

# Compute Engine (v6e, v5p)
gcloud compute instances describe my-tpu-vm --project=PROJECT_ID --zone=ZONE
gcloud compute ssh my-tpu-vm --project=PROJECT_ID --zone=ZONE

# Queued resources (v7x, v5e, v4)
gcloud alpha compute tpus queued-resources describe my-queued-resource --project=PROJECT_ID --zone=ZONE
gcloud compute tpus tpu-vm ssh my-tpu-name --project=PROJECT_ID --zone=ZONE --worker=0

Check: the instance is RUNNING or the queued resource is ACTIVE before you try to connect. A v7x host (tpu7x-8) has 4 chips, each exposing 2 logical devices: 8 devices and 768 GB of HBM.

2. Confirm the devices are visible

ls /dev/vfio/

Check: the directory lists devices. If it is empty or missing, no TPU is attached, and nothing you install will fix it.

3. Install vLLM-TorchTPU

The recommended install uses uv and routes torch and torchvision to the PyTorch CPU index; without that, default PyPI serves CUDA builds that pull gigabytes of nvidia-* packages onto the TPU host.

uv venv --python 3.12 ~/uv_venv
source ~/uv_venv/bin/activate

# Route torch and torchvision to the PyTorch CPU index
export UV_TORCH_BACKEND=cpu

uv pip install vllm-torchtpu

With pip instead:

python3.12 -m venv ~/pip_venv --symlinks
source ~/pip_venv/bin/activate
pip install --upgrade pip

pip install \
  --extra-index-url https://download.pytorch.org/whl/cpu \
  vllm-torchtpu

vllm-torchtpu installs upstream vllm (built with VLLM_TARGET_DEVICE="empty", which skips the CUDA extensions) and pulls vllm-torchtpu-core, the plugin, with torch-tpu, libtpu and tpu-sync-torch. Source and cross-repository installs, which currently pin vllm==0.32.0, are on the installation page.

Check: vLLM finds the plugin.

python -c "from vllm.platforms import current_platform; print(current_platform.device_name)"

The command prints the platform's device name. The install also provides tpu-info, which shows chip utilization and HBM in use; run it now to see the idle chips.

Or use the container. The release image has everything installed; :nightly adds the test and benchmark tools. Pass --privileged, --net=host and --shm-size 150gb so the container reaches /dev/vfio:

sudo docker run -it --rm \
  --name $USER-vllm \
  --privileged \
  --net=host \
  -v /dev/shm:/dev/shm \
  --shm-size 150gb \
  -p 8000:8000 \
  --entrypoint /bin/bash \
  vllm/vllm-torchtpu:latest

4. Start the server

One device, the quickstart's first model:

vllm serve meta-llama/Llama-3.2-1B-Instruct \
  --tensor-parallel-size 1 \
  --max-model-len 4096 \
  --port 8000

A whole 8-device host (v7x) with an fp8 MoE checkpoint and expert parallelism:

vllm serve Qwen/Qwen3-Coder-30B-A3B-Instruct-FP8 \
  --tensor-parallel-size 8 \
  --enable-expert-parallel \
  --kv-cache-dtype fp8 \
  --max-model-len 16384 \
  --max-num-batched-tokens 8192 \
  --async-scheduling \
  --port 8000

The first start compiles a TPU program for each configured token size, which takes minutes. To keep the compiled programs across restarts, set the cache roots first (Keep compiled programs across restarts); the book's builder prints both commands with the caches set:

python code/ch18_vllm/launch_profiles.py --profile quickstart-tp1 --cache-root "$HOME/tpu-cache"
python code/ch18_vllm/launch_profiles.py --profile quickstart-tp8-ep --cache-root "$HOME/tpu-cache"

Check: the server logs Application startup complete. Until then, requests are refused.

5. Send a request

From a second terminal:

curl http://localhost:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "meta-llama/Llama-3.2-1B-Instruct",
    "messages": [
      {"role": "user", "content": "Explain how Tensor Processing Units accelerate matrix multiplication."}
    ],
    "max_tokens": 128,
    "temperature": 0.0
  }'

Or with the book's client, which sends the same request and prints the reply:

python code/ch18_vllm/client.py smoke --model meta-llama/Llama-3.2-1B-Instruct

Check: a coherent answer. It proves the plugin loaded, the weights are right, and one prefill and decode ran. On a laptop, python code/ch18_vllm/client.py --cpu-smoke runs the client against a local fake endpoint and ends cpu-smoke: OK.

6. Run an offline batch

The same engine runs offline through vllm.LLM, with engine flags as keyword arguments:

from vllm import LLM, SamplingParams

prompts = [
    "Hello, my name is",
    "The capital of France is",
    "The future of AI hardware is",
]

sampling_params = SamplingParams(temperature=0.0, max_tokens=64)
llm = LLM(
    model="meta-llama/Llama-3.2-1B-Instruct",
    tensor_parallel_size=1,
    max_model_len=1024,
)

outputs = llm.generate(prompts, sampling_params)
for output in outputs:
    print(f"Prompt: {output.prompt!r}")
    print(f"Generated: {output.outputs[0].text!r}\n")

Check: one Prompt: and Generated: pair per prompt. Offline inference with vLLM-TorchTPU covers the repository's example scripts, LoRA and vision-language models.

7. Clean up

# Compute Engine (v6e, v5p)
gcloud compute instances delete my-tpu-vm --project=PROJECT_ID --zone=ZONE

# Queued resources (v7x, v5e, v4)
gcloud alpha compute tpus queued-resources delete my-queued-resource --project=PROJECT_ID --zone=ZONE --force

Troubleshooting

Symptom Cause Fix
The install downloads nvidia-* packages torch resolved from default PyPI, which serves CUDA builds export UV_TORCH_BACKEND=cpu with uv, or --extra-index-url https://download.pytorch.org/whl/cpu with pip; reinstall in a fresh venv
No wheel found for torch_tpu or tpu-sync-torch Python other than 3.12 Create the venv with Python 3.12
/dev/vfio/ empty No TPU attached to the VM Check the instance or queued resource; re-provision
The container cannot see the TPU Missing --privileged, --net=host or --shm-size Use the docker run command in step 3
pytest or benchmark tools missing in the container Release images (:latest, :vX.Y.Z) carry the runtime only Use vllm/vllm-torchtpu:nightly
Startup takes minutes on every restart Programs recompiled each time Persist the cache roots: Keep compiled programs across restarts
curl is refused The server is still compiling Wait for Application startup complete
Out of HBM, or requests preempted under load Weights plus KV exceed the reservation Lower --max-model-len or --max-num-seqs; --kv-cache-dtype fp8; see chapter 18's KV arithmetic

References