Skip to content
SurogateSurogateDocs
↑↓ to move↵ to openesc to close

Serving examples

These scripts let you try each serving feature with one command. Run them from the repository root with Surogate installed (a source build needs make serve-build first). launch.sh starts one server in the foreground; stop it before you start another scenario on the same port. client.py sends requests to a running server and needs only the Python standard library.

bash
bash examples/serve/launch.sh chat
# In another terminal:
python examples/serve/client.py chat
python examples/serve/client.py stream
python examples/serve/client.py tools

MODEL picks a Hugging Face repo, a local safetensors directory or a local GGUF. For a split GGUF, pass the first shard and keep all the shards in one directory. The prepared cache points back at the source GGUF, so leave those files where they are. PORT changes the port, and generation scenarios serve the model as demo. Any other arguments go straight to surogate serve.

ScenarioDemonstratesInputs / hardware
chat#Chat, streaming, thinking controls, automatic function toolsQwen3-0.6B by default; one supported GPU
concurrent#Continuous batching, chunked prefill, FP8 KV, elastic allocation, prefix reuse, edited-turn checkpoints, queues and request logsQwen3.5-0.8B; one GPU
lora#Startup PEFT adapter and runtime load/unloadTrain examples/training/runtime-lora.yaml first
vision#Image/video input and media cachingQwen3-VL-2B safetensors; one GPU
offload#Whole decoder layers in host RAMGPU_LAYERS=8 by default; GPU still required
moe#Expert offload, automatic GPU expert cache, CPU/GPU expert computeSet MODEL to a supported MoE; sufficient host RAM
multi-gpu#Layer pipeline on two GPUsQwen3.5-4B by default; DEVICES=0,1
multi-model#Named models, priorities, shared spare KV memory, automatic sleep/wakeSet SECOND_MODEL to a prepared artifact; one GPU and host RAM
mtp#Multi-token prediction, concurrency cutoffSet MODEL to a supported MTP checkpoint
dflash#Separate speculative drafterPrepared target + drafter artifact; one GPU, BF16 KV, text only
generate#One-shot generation with answer on stdoutQwen3-0.6B; no HTTP server
embeddings#EmbeddingGemma on GPU or CPUSet MODEL to GGUF and FRONTEND to its tokenizer directory

Supported hardware and checkpoint formats differ by model family; see model coverage and the CLI reference. The launch defaults are example resource budgets. They don’t promise that a given workload fits.

bash
# Local BF16 or FP8 safetensors directory; precision is detected.
MODEL=./models/my-checkpoint bash examples/serve/launch.sh chat
# A prequantized NVFP4 export requires a supported Blackwell GPU.
MODEL=nvidia/Qwen3.6-27B-NVFP4 bash examples/serve/launch.sh concurrent
# GGUF, including automatic discovery of the remaining split shards.
MODEL=./models/model-00001-of-00006.gguf bash examples/serve/launch.sh moe
MODEL=./models/large-moe.gguf bash examples/serve/launch.sh moe --devices 0,1
bash examples/serve/launch.sh multi-gpu

Whole-layer offload keeps the weights in host RAM but still computes on the GPU. MoE CPU sharing applies only to offloaded experts. --host-expert-bank q4 saves RAM at the cost of host precision. --expert-slots N sets the expert cache size yourself; it must hold at least one layer’s experts.

bash
surogate sft examples/training/runtime-lora.yaml
bash examples/serve/launch.sh lora
# In another terminal, select the startup adapter by name:
python examples/serve/client.py chat --model tuned
python examples/serve/client.py unload-lora
python examples/serve/client.py load-lora --adapter "$(pwd)/outputs/training/runtime-lora"

Adapter paths are paths on the server’s filesystem. Let an adapter’s requests finish before you unload it. The runtime example trains attention projections; which other modules work depends on the family and weight format. To get a standalone model, merge any supported training adapter, and quantize it if you like:

bash
surogate merge --base-model Qwen/Qwen3-0.6B \
--checkpoint-dir ./outputs/training/runtime-lora --output ./outputs/serve/merged
surogate quantize --model ./outputs/serve/merged \
--output ./outputs/serve/merged-Q4_K_M.gguf --type q4_k_m
MODEL=./outputs/serve/merged-Q4_K_M.gguf bash examples/serve/launch.sh chat

quantize uses the bundled llama.cpp converter and quantizer. A source build needs the quantizer target, and you need disk space for an intermediate BF16 GGUF. You can also serve the merged safetensors directory as it is.

Prepare the second model once: start it with surogate serve Qwen/Qwen3.5-0.8B, stop that process, and copy the cache path it printed into SECOND_MODEL:

bash
SECOND_MODEL=/absolute/path/to/prepared-small.sinfer bash examples/serve/launch.sh multi-model
python examples/serve/client.py chat --model small
python examples/serve/client.py sleep --model small
python examples/serve/client.py wake --model small
python examples/serve/client.py status

For now, every model after the first must be a prepared .sinfer file (see serving several models). Both models share one GPU. --enable-sleep-mode keeps a sleeping model’s state in system RAM. A multi-model server wakes a model when a request arrives for it; a single-model server waits for an explicit wake. If you want a fixed KV reservation in place of shared elastic KV, use --no-elastic-kv and give every additional model kv-tokens=N.

bash
MODEL=./models/checkpoint-with-mtp bash examples/serve/launch.sh mtp
# Supported multi-GPU families also allow MTP:
MODEL=./models/checkpoint-with-mtp bash examples/serve/launch.sh mtp --devices 0,1

MTP needs the checkpoint’s draft weights, and community exports sometimes leave them out. If the prepared model also carries a reduced draft vocabulary, MTP drafts with it by default; --full-head-draft drafts with the full output head instead. Speculation doesn’t always pay off, so measure with and without it at the concurrency you expect to serve.

Preparing a DFlash pair is an advanced workflow that needs a source checkout. You supply a compatible Qwen3.5/3.6 MoE target and a DFlash drafter checkpoint, both already downloaded:

bash
python -m surogate.serve.convert.qwen3_5_moe.convert \
--model ./models/target --dflash-model ./models/compatible-drafter \
--out ./outputs/target-dflash.sinfer --no-vision
MODEL=./outputs/target-dflash.sinfer bash examples/serve/launch.sh dflash

The .sinfer file here is Surogate’s internal prepared artifact. This DFlash scenario needs a single GPU, BF16 KV and no vision. Ordinary model preparation never adds a drafter.

bash
bash examples/serve/launch.sh vision
python examples/serve/client.py image --media ./photo.jpg
python examples/serve/client.py video --media ./clip.mp4

The client sends local files as base64 data URIs; URLs work too. Very large media can go over the request or media budgets. Vision support depends on the family; Qwen3-VL GGUF and Qwen3-VL-MoE are not supported yet.

bash
MODEL=./models/embeddinggemma-300M-Q8_0.gguf FRONTEND=./models/embeddinggemma-300m \
bash examples/serve/launch.sh embeddings
python examples/serve/client.py embeddings --base-url http://127.0.0.1:8413 --model embeddinggemma-300m
# CPU alternative: requires AVX-512; choose the physical cores of one NUMA node.
OMP_WAIT_POLICY=ACTIVE OMP_NUM_THREADS=16 DEVICE=cpu \
MODEL=./models/embeddinggemma-300M-Q8_0.gguf FRONTEND=./models/embeddinggemma-300m \
bash examples/serve/launch.sh embeddings

The embeddings server is a separate process from generation. FRONTEND points at the directory with tokenizer.model and tokenizer_config.json, which preparation reads.

With launch.sh chat running, try any of these client commands:

CommandRequest behavior
chat#Sampling controls, generated-token logprobs and exact token IDs
stream#SSE deltas, reasoning fields when present, usage and completion marker
reasoning#Per-request thinking enabled; response separates reasoning and answer
tools#Auto tool selection, argument validation, local function and tool-result round trip
completions#Raw text without a chat template, greedy decoding and stop string
tokens#Tokenization with token strings, then generation from exact prompt IDs
responses#Conversation history supplied by the client, follow-up generation and input token counts
anthropic#Messages and message token counts
concurrent#Eight simultaneous requests with a common prefix; use the concurrent launch
status#Health, model names, KV statistics and Prometheus metrics
bash
python examples/serve/client.py responses
python examples/serve/client.py anthropic
python examples/serve/client.py tokens

Responses requests are stateless, so the client keeps the conversation history and sends it with each request. Background execution and compaction are not supported. The API guide covers structured output, token scores and the other options.

To turn on authentication, export the same SUROGATE_API_KEY in the server and client terminals. The launch script passes it as --api-key, and the client sends it as a bearer header. Every local user can read the server’s command line, so to keep the key off it, put the key in a file only you can read and export SUROGATE_API_KEY_FILE instead; the script then passes --api-key-file. --host 0.0.0.0 allows remote access and --cors allows cross-origin requests from browsers. /health never asks for the key. After the concurrent scenario, look at outputs/serve/requests.jsonl; /metrics and /kv_stats show throughput and memory use.

bash
bash examples/serve/launch.sh chat --enable-sleep-mode --preserve-thinking
python examples/serve/client.py sleep
python examples/serve/client.py wake
bash examples/serve/launch.sh generate > answer.txt

When you’re investigating CUDA graph behavior, use --enforce-eager (server) or --no-cuda-graph (one-shot). surogate serve --engine-help lists every flag, and its --generate / --embed variants list the flags for those modes.

Next: Serving models walks through real deployments (NVFP4, offload, several GPUs), and the CLI reference explains each flag these scripts pass.