Skip to content

Offline inference with vLLM-TorchTPU

Use this guide when you want outputs from a model without running a server: a batch of prompts, a LoRA adapter, or an image question to a vision-language model, through vLLM's LLM class on a TPU VM. Offline mode is also how you produce reference outputs for a parity gate and how you profile one shape without scheduler noise. The vLLM-TorchTPU repository ships three example scripts, two of which accept every vLLM engine argument on the command line; this guide runs each and says what to look for. Chapter 18 explains the engine underneath.

Prerequisites

Steps

1. Run a text batch

python examples/offline_inference.py \
  --model meta-llama/Llama-3.2-1B-Instruct \
  --tensor-parallel-size 1 \
  --max-model-len 1024

The script adds all EngineArgs to its parser (so --enable-expert-parallel, --pipeline-parallel-size and the rest work), defaults to meta-llama/Llama-3.2-1B-Instruct with --max-model-len 1024, and adds its own options:

Option Effect
--prompt P [P ...] Your prompts instead of the built-in list of 35 questions
--max-tokens, --temperature (default 0.0), --top-p, --top-k Sampling parameters
--use-chat-template / --no-use-chat-template LLM.chat with the model's chat template, or raw prompts to LLM.generate; by default the template is used when the tokenizer defines one
--chat-template-kwargs JSON Passed to the template; Qwen3.5 accepts --chat-template-kwargs='{"enable_thinking": false}' to turn thinking off

Check: the output is a series of Prompt: ... and Generated text: ... blocks separated by dashed lines, one per prompt. Keep the outputs of a greedy run (the default temperature 0) as reference outputs for the parity gate in chapter 18.

In your own code the same run is a few lines:

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")

2. Run with a LoRA adapter

python examples/offline_lora_inference.py \
  --model Qwen/Qwen2.5-3B-Instruct \
  --enable-lora \
  --max-lora-rank 8

The script's defaults are Qwen/Qwen2.5-3B-Instruct, --max-model-len 256, --max-num-seqs 8, --enable-lora, --max-lora-rank 8 and --max-tokens 16. It sends one prompt, What is 1+1?, with the adapter attached per request:

from vllm.lora.request import LoRARequest

lora_request = LoRARequest(
    "lora_adapter_3",
    3,
    "Username6568/Qwen2.5-3B-Instruct-1_plus_1_equals_3_adapter",
)
outputs = llm.generate(prompt, sampling_params=sampling_params, lora_request=lora_request)

Check: one Prompt: and Generated text: block, then total time: ... [secs].. The adapter's name says what it was trained to answer; an answer that matches it shows the adapter was applied, and running the same prompt without lora_request gives the base model's answer to compare.

3. Ask a vision-language model about an image

python examples/multi_modal_inference.py \
  --model Qwen/Qwen2.5-VL-3B-Instruct \
  --tensor-parallel-size 1 \
  --num-prompts 1

The script supports Qwen2.5-VL only and image input only. It loads vLLM's cherry_blossom image asset, asks one of four questions about it per prompt (with --num-prompts N it batches N prompts over the same image), and samples at temperature 0.2 so identical prompts can differ. Its defaults are --gpu-memory-utilization 0.5 (a comment in the script says it "needs much more space for intermediate tensors"), --max-model-len 4096 and --num-prompts 4; the engine uses max_num_seqs=5, at most one image per prompt, and Qwen2.5-VL pixel limits from 28 * 28 to 1280 * 28 * 28. Add --time-generate to print the generate() time.

Check: one generated description per prompt between dashed lines, describing the image.

4. Profile an offline run

examples/offline_inference.py and examples/offline_lora_inference.py call llm.start_profile() and llm.stop_profile() around generation when the engine's torch profiler directory is configured. For a clean trace of one prefill or decode shape, use examples/tpu_profiling.py instead: Profile vLLM-TorchTPU serving, section "Offline: one shape".

Troubleshooting

Symptom Cause Fix
The first generate takes minutes Warmup compiles one program per token size Expected once; persist the caches (Keep compiled programs across restarts)
Outputs look like continuations, not answers Raw prompts sent to an instruct model Leave --use-chat-template at its default, or pass it explicitly
Qwen3.5 output starts with long reasoning Thinking mode on in the chat template --chat-template-kwargs='{"enable_thinking": false}'
ValueError: Modality video is not supported. The multimodal script supports images only --modality image (the default)
Out of HBM in the multimodal script Intermediate tensors of the vision path Keep --gpu-memory-utilization low (the script's default is 0.5); lower --max-model-len
Another vision-language model gives poor answers in the multimodal script The script builds Qwen2.5-VL prompts only Use a Qwen2.5-VL checkpoint, or write the prompt format from the model's Hugging Face examples
LoRA answer identical to the base model No lora_request on the call, or LoRA not enabled Pass lora_request=; --enable-lora with --max-lora-rank at least the adapter's rank

References