Serving examples
EngineSurogate EngineThese 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 examples/serve/launch.sh chat# In another terminal:python examples/serve/client.py chatpython examples/serve/client.py streampython examples/serve/client.py toolsMODEL 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.
Launch recipes
Section titled “Launch recipes”| Scenario | Demonstrates | Inputs / hardware |
|---|---|---|
chat# | Chat, streaming, thinking controls, automatic function tools | Qwen3-0.6B by default; one supported GPU |
concurrent# | Continuous batching, chunked prefill, FP8 KV, elastic allocation, prefix reuse, edited-turn checkpoints, queues and request logs | Qwen3.5-0.8B; one GPU |
lora# | Startup PEFT adapter and runtime load/unload | Train examples/training/runtime-lora.yaml first |
vision# | Image/video input and media caching | Qwen3-VL-2B safetensors; one GPU |
offload# | Whole decoder layers in host RAM | GPU_LAYERS=8 by default; GPU still required |
moe# | Expert offload, automatic GPU expert cache, CPU/GPU expert compute | Set MODEL to a supported MoE; sufficient host RAM |
multi-gpu# | Layer pipeline on two GPUs | Qwen3.5-4B by default; DEVICES=0,1 |
multi-model# | Named models, priorities, shared spare KV memory, automatic sleep/wake | Set SECOND_MODEL to a prepared artifact; one GPU and host RAM |
mtp# | Multi-token prediction, concurrency cutoff | Set MODEL to a supported MTP checkpoint |
dflash# | Separate speculative drafter | Prepared target + drafter artifact; one GPU, BF16 KV, text only |
generate# | One-shot generation with answer on stdout | Qwen3-0.6B; no HTTP server |
embeddings# | EmbeddingGemma on GPU or CPU | Set 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.
Checkpoint formats and placement
Section titled “Checkpoint formats and placement”# 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 moeMODEL=./models/large-moe.gguf bash examples/serve/launch.sh moe --devices 0,1bash examples/serve/launch.sh multi-gpuWhole-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.
Runtime adapters and merged models
Section titled “Runtime adapters and merged models”surogate sft examples/training/runtime-lora.yamlbash examples/serve/launch.sh lora# In another terminal, select the startup adapter by name:python examples/serve/client.py chat --model tunedpython examples/serve/client.py unload-lorapython 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:
surogate merge --base-model Qwen/Qwen3-0.6B \ --checkpoint-dir ./outputs/training/runtime-lora --output ./outputs/serve/mergedsurogate quantize --model ./outputs/serve/merged \ --output ./outputs/serve/merged-Q4_K_M.gguf --type q4_k_mMODEL=./outputs/serve/merged-Q4_K_M.gguf bash examples/serve/launch.sh chatquantize 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.
Multiple models and sleep
Section titled “Multiple models and sleep”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:
SECOND_MODEL=/absolute/path/to/prepared-small.sinfer bash examples/serve/launch.sh multi-modelpython examples/serve/client.py chat --model smallpython examples/serve/client.py sleep --model smallpython examples/serve/client.py wake --model smallpython examples/serve/client.py statusFor 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.
Speculation
Section titled “Speculation”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,1MTP 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:
python -m surogate.serve.convert.qwen3_5_moe.convert \ --model ./models/target --dflash-model ./models/compatible-drafter \ --out ./outputs/target-dflash.sinfer --no-visionMODEL=./outputs/target-dflash.sinfer bash examples/serve/launch.sh dflashThe .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.
Images, video, and embeddings
Section titled “Images, video, and embeddings”bash examples/serve/launch.sh visionpython examples/serve/client.py image --media ./photo.jpgpython examples/serve/client.py video --media ./clip.mp4The 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.
MODEL=./models/embeddinggemma-300M-Q8_0.gguf FRONTEND=./models/embeddinggemma-300m \ bash examples/serve/launch.sh embeddingspython 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 embeddingsThe embeddings server is a separate process from generation. FRONTEND points at the
directory with tokenizer.model and tokenizer_config.json, which preparation reads.
API examples
Section titled “API examples”With launch.sh chat running, try any of these client commands:
| Command | Request 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 |
python examples/serve/client.py responsespython examples/serve/client.py anthropicpython examples/serve/client.py tokensResponses 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 examples/serve/launch.sh chat --enable-sleep-mode --preserve-thinkingpython examples/serve/client.py sleeppython examples/serve/client.py wakebash examples/serve/launch.sh generate > answer.txtWhen 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.