Skip to content
Athrael.net logoAthrael.net

Introduction to GPU InferenceChapter 2: Serving inference

Your first inference server

Lesson 4 of 4 · about 7 min

What this lesson covers

Previously we looked at how Narwhal’s router sends a request’s prefill and decode to different engines, and how its role controller moves an engine from one role to the other while the model stays loaded. So far the course has shown inference in diagrams. In this lesson you run both yourself, with four small engines on your own computer.

You’ll see parts of earlier lessons at work: an engine’s slots, the KV cache passing from a prefill engine to a decode engine, time to first token (TTFT) and time per output token (TPOT) measured on your own requests, and a hot swap. The consoles on this page replay recorded output, so you can follow along here before running the commands yourself.

Installing Narwhal

You’ll need Linux, or Windows with WSL2 and an Ubuntu distribution, and Python 3.11 or newer. You can use either a GPU or a CPU.

Install Narwhal in a Python virtual environment.

python3 -m venv .venv
source .venv/bin/activate
python -m pip install narwhal-inference
Install commands
Terminal
$

Choose a command, then press Enter in the console to run it.

Run the commands in the rest of this lesson from the same folder, with the virtual environment active.

The Narwhal CLI

Narwhal is operated through a command-line interface (CLI). The narwhal-inference package installs six commands, which you can test using the interactive console below.

Narwhal commands
Terminal
(.venv) $

Choose a command, then press Enter in the console to run it.

Your own fleet

narwhal dev starts a fleet on your computer, made of a router and four llama.cpp engines that serve ERNIE-4.5-0.3B at 4-bit precision. Two engines start in the prefill role and two in decode, and --engine-count sets a different number, from 2 to 8.

setup downloads a llama.cpp build and the model, about 1 GB when it finds an NVIDIA GPU and 250 MB for the CPU. To run on the CPU, add --accelerator none.

Run the commands below in order. Before sending the 2k prompts, start narwhal dev watch in a second terminal, with the same folder and virtual environment.

narwhal dev setup
narwhal dev up
narwhal dev ask "What is the capital of France?"
narwhal dev ask --repeat 2000 --concurrency 8 --max-tokens 200
narwhal dev downTerminal 1
narwhal dev watchTerminal 2
Your own fleet
narwhal dev
Terminal 1
(.venv) $
narwhal dev
Terminal 2
(.venv) $

Choose a command, then press Enter in the console to run it.

In the first reply, n1 ran the prefill and saved the slot’s KV cache to a file, which n3 restored to generate the reply.

Prefilling a short question takes one step, while its reply takes dozens of decode steps, so under 2k prompts the decode engines fill up and the prefill engines sit idle. In the recording, the router moves n1 to decode after 9 seconds, and its model stays loaded.

When the 2k prompts finish, ask prints a summary of the run. The median TTFT was 92 ms and the median TPOT 4.9 ms, about 200 tokens per second, and the fleet served 22.5 requests per second.

watch measures at the router, where TPOT also covers the KV cache transfer and the wait for a decode slot, so it reads higher.

OpenAI-compatible API

The router serves the OpenAI-compatible API, so supported clients can send their requests to the fleet via curl.

curl -s http://127.0.0.1:18000/v1/chat/completions \
  -H 'content-type: application/json' \
  -d '{"model": "ERNIE-4.5-0.3B", "messages": [{"role": "user", "content": "What is the capital of France?"}]}'

The reply is JSON, shown here formatted, with the answer in choices[0].message.content.

{
  "created": 1795543895,
  "id": "chatcmpl-bHZZYN3zRAqXWPdf9UIdCt2PlXT8TViG",
  "model": "ERNIE-4.5-0.3B",
  "system_fingerprint": "b12365-ae93877c8",
  "timings": { ⋮ },
  "object": "chat.completion",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "The capital of France is Paris."
      }
    }
  ],
  "usage": { "prompt_tokens": 15, "completion_tokens": 8, "total_tokens": 23 }
}Reply

Note: ERNIE-4.5-0.3B is small enough to run four copies on one computer, and at that size its answers are sometimes wrong or come back in Chinese.

Recap

In this lesson we set up narwhal-inference and ran a disaggregated fleet of four llama.cpp engines on one computer, and used CLI commands to view our deployment, send requests and see what a disaggregated, hot-swapping inference framework looks like. We also saw the KV cache pass from a prefill engine to a decode engine, read TTFT and TPOT for our own requests, and reached the fleet through the OpenAI-compatible API.