Skip to content

5. The engine

Replace the per-turn service time with an engine. Each iteration allocates a token budget across the requests it is running and then admits waiting requests with the remaining budget. This model allows prefill and decode to share an iteration.

The program

docs/tutorial/programs/05-engine.sq
// Chapter 5: the engine.
// The `fifo` stage of chapters 3 and 4 served one turn at a time. A real
// LLM engine runs *iterations*: every iteration hands a token budget to the
// requests it is already running — one token to each decoding request, a
// chunk to each prefilling one — and then admits waiting requests with
// whatever budget is left. Prefill and decode share the same iteration.
// Its workload: workloads/05-engine.sq, chapter 4's sessions.
use "std/args";

let B = 2048;          // token budget per iteration (max_num_batched_tokens)
let bs = 16;           // tokens per block
let alpha = 2e-5;      // compute per scheduled token (s)
let omega = 2e-4;      // weights read per iteration (s)
let beta = 2e-9;       // KV read per iteration per resident token (s)

fn main() {
  let max_seqs = args.number("max_seqs", 16);     // how many requests may run at once
  let blocks = args.number("blocks", 4000);     // KV blocks

  device gpu {
    compute (t) = t * alpha;
    hbm (k) = omega + beta * k;
    kv cap blocks * bs;
  }
  pool kv on gpu { block bs; evict lru; preempt lifo; }
  engine llm on gpu {
    reqs cap max_seqs;
    tokens cap B;
    scheduler {
      advance running;
      // a running slot, and the blocks of the reused prefix plus the chunk
      // the budget left allows; only if every token the request has fits,
      // as vLLM's default scheduler_reserve_full_isl (config/scheduler.py:191).
      // It reuses what the cache holds of its tokens in whole blocks, never
      // the last, and leaves them all cached when it ends.
      admit waiting while (running.preempted == 0)
        taking reqs (1), kv (min(request.known, request.hit + budget_left(llm)))
          reserve (request.known)
        reuse (floor(request.known - 1, bs))
        cache (request.input + request.output);
    }
    // an iteration costs whichever is larger: reading the weights and the
    // running requests' KV, or computing the tokens it scheduled
    worker {
      forward (max(hbm(batch.kv_decode + batch.kv_prefill), compute(batch.tokens)));
    }
  }
  pool reqs on llm { queue fifo; }

  // a turn is one request to the engine
  server (input, output, final) {
    llm (input, output);
    // a session's last turn leaves nothing worth keeping
    if (final) { drop kv; }
  }
}
docs/tutorial/programs/workloads/05-engine.sq
// Chapter 5's workload: chapter 4's sessions, each turn one request with its
// context and its output, and whether it is the session's last (05-engine.sq
// runs it).
use "std/args";

let p = 0.8;
let Lambda = args.number("Lambda", 0.5);
let Z = args.number("Z", 3.0);

workload {
  arrive poisson(Lambda);
  init { set K = 0; }
  turn {
    set n = ceil(K == 0 ? ~uniform(1000, 3000) : ~exp(500));
    set o = floor(~exp(200)) + 1;
    set more = ~bernoulli(p);
  }
  send (input: K + n, output: o, final: more == 0);
  // the conversation: a request per turn, then a tool call and the next
  // turn, or the end
  session {
    loop {
      turn;
      set K = K + n + o;
      if (more) { tool (~exp(Z)); } else { end; }
    }
  }
}

Its deployment, drawn by serq draw:

The engine as a queueing network

The engine on its device

The stage engine of chapters 3 and 4 is now an engine, and engine is the word that declares one, so the engine is named llm. Leaving its admission aside for now:

device gpu {
  compute (t) = t * alpha;
  hbm (k) = omega + beta * k;
  kv cap blocks * bs;
}
pool kv on gpu { block bs; evict lru; preempt lifo; }
engine llm on gpu {
  reqs cap max_seqs;
  tokens cap B;
  scheduler {
    advance running;
    admit waiting while (running.preempted == 0)
      taking …;
  }
  worker {
    forward (max(hbm(batch.kv_decode + batch.kv_prefill), compute(batch.tokens)));
  }
}
pool reqs on llm { queue fifo; }

The device says what the hardware offers: the time to compute t tokens, the time to read k tokens of KV, and the KV capacity. pool kv on gpu is the device's memory, with its rules: blocks, eviction, preemption. The engine says what runs on it. reqs cap is how many requests may run at once (max_num_seqs), tokens cap the tokens one iteration may schedule (max_num_batched_tokens). pool reqs on llm holds the engine's reqs cap: its running slots, and the queue of requests waiting for one, served fifo.

scheduler is what each iteration does: give the running requests their tokens, then admit waiting ones while nothing was preempted. taking says what an admission takes of a request, below. worker is how long the iteration takes, as an expression in what it scheduled:

Value Meaning
batch.tokens tokens scheduled this iteration
batch.decoding decoding requests scheduled
batch.prefilled prefill tokens scheduled
batch.kv_decode, batch.kv_prefill memory held by the scheduled decode / prefill requests
batch.attention attention work of the prefill chunks, \(\sum n(K + n/2)\)

The cost is the larger of the time to read weights and KV memory and the time to compute the scheduled tokens. Fit these parameters to measurements of the engine you want to model. each at most (c) limits each request's prefill in an iteration, and advance running takes an order and an only; see Engines on devices.

What the engine does with one request

The engine keeps each request's state, as vLLM's scheduler does: the tokens it has, request.known (its prompt, request.input, and what it has generated), and how many of them have KV. Each iteration it computes what has no KV yet, a chunk of the prompt as the budget allows, and when they all have it samples a token; after that it decodes one token per iteration until it has generated request.output. Prefill and decode are that one rule with many tokens left or one.

What an admission takes

admit waiting while (running.preempted == 0)
  taking reqs (1), kv (min(request.known, request.hit + budget_left(llm)))
    reserve (request.known)
  reuse (floor(request.known - 1, bs))
  cache (request.input + request.output);

taking sits on admit waiting because that is when the scheduler sizes the request: it reads the request at the moment the iteration admits it, not when it arrived. A prefix may be evicted while the request waits.

reuse (floor(request.known - 1, bs)) is how much of its tokens the cache may supply: whole blocks, never the last token, which is computed again for its logits. request.hit is what the cache does supply, min(cachedin(kv), reuse), read when the request is admitted; the engine computes the rest.

reqs (1) is one running slot.

kv (min(request.known, request.hit + budget_left(llm))) is the blocks of the reused prefix plus the prefill chunk the iteration's budget still allows, capped by the tokens the request has. budget_left(llm) is the token budget left after serving the running requests. The KV pool grows with the request as it computes, block by block.

reserve (request.known) admits the request only if every token it has fits beside the memory the running requests hold, although only the first chunk is allocated. This is vLLM's default (scheduler_reserve_full_isl). Without it, a long prompt is admitted on its first chunk and its later chunks preempt other requests; at the loads of the next chapter, that raises the preemption count about tenfold.

cache (request.input + request.output) leaves the request's sequence cached when it ends, for the session's next turn, as in chapter 4.

With preempt lifo, a growth that does not fit preempts the most recently admitted running request. It goes back to the head of the queue keeping the tokens it has, and when it is admitted again computes their KV again and generates only the rest (vLLM's _preempt_request).

Waiting requests are admitted only by admit waiting, in an iteration, with the budget left, and never in an iteration that preempted: pool reqs on llm hands the pool's queue to the engine's scheduler.

The engine records what it did with each admission: llm.reused, the tokens it read from the cache, and llm.prefill, the tokens it computed. The report's engine table sums them: the admissions, those of a request preempted before, and the share of the admitted tokens the cache gave (engine statistics). The time to the first token needs no observe: the report's client section gives it for every turn, from the turn; to the token the prefill samples, once however many times the request is preempted, and the time per output token after it (what the client sees).

The workload and the server

The workload file is chapter 4's sessions. Its session describes the conversation: each turn; waits for a response before the client decides whether to continue. The workload sends each request its prompt, the session's context and new tokens, its output, and whether it is the session's last, send (input: K + n, output: o, final: more == 0);, and the server says what a turn is:

server (input, output, final) {
  llm (input, output);
  // a session's last turn leaves nothing worth keeping
  if (final) { drop kv; }
}

one request to the engine, with input as its prompt and output as its output: request.input and request.output in taking. After the session's last turn the server drops its cached KV, which is the deployment's, as in chapter 4. The server reads what the request carries, not the client's K; the client keeps its context itself, set K = K + n + o;. The deployment figure shows the server side.

Running it

serq run docs/tutorial/programs/05-engine.sq --workload docs/tutorial/programs/workloads/05-engine.sq --horizon 20000 --warmup 2000 --seed 1
run: horizon 20000 end 20000 warmup 2000 seed 1 events 9310920 arrivals 9905 ended 8902 turns 44889 mean live 6.146

observe      count       mean    95% CI    cv2         p99
-----------  -----  ---------  --------  -----  ----------
llm.reused   44889  3988.7417  ±79.2073  0.861  16640.0000
llm.prefill  44889   876.4606  ±33.4152  1.471   3492.0000

client    count       mean      95% CI    cv2        p99
--------  -----  ---------  ----------  -----  ---------
ttft      44889    0.01906    ±0.00111  1.870    0.09567
tpot      44660  0.0002516  ±0.0000146  7.313  0.0007231
response  44889    0.06450    ±0.00217  0.817    0.26212

status   stays    mean     p50     p99  number
-------  -----  ------  ------  ------  ------
waiting  44889  0.0011  0.0000  0.0271   0.003
running  44889  0.0635  0.0503  0.2571   0.158

engine  admits  readmits  token hit
------  ------  --------  ---------
llm      44889         0      0.820

stage  number   util   done    thru    wait  service    iters
-----  ------  -----  -----  ------  ------  -------  -------
llm     0.158  0.141  89549  4.9749  0.0000   0.0318  9261013
tool    5.985  0.998  35986  1.9992  0.0000   2.9932        0

step  prefill only  decode only  mixed   idle  decodes  decode batch  decode step   itl p50   itl p99
----  ------------  -----------  -----  -----  -------  ------------  -----------  --------  --------
llm          0.037        0.097  0.007  0.859    0.113         1.080     0.000225  0.000209  0.000245

pool   used   cached  queue  holders    wait  admits  evict(n)  evict(u)  preempt  spill  rej  stuck
----  -----  -------  -----  -------  ------  ------  --------  --------  -------  -----  ---  -----
kv    783.2  29643.4  0.000    0.158     NaN   49906       435   3514144        0      0    0      0
reqs    0.2      0.0  0.003    0.158  0.0011   49906         0         0        0      0    0      0

done 89549 at llm against 44 889 turns — two runs per turn, prefill and decode, but for the 229 turns whose prefill samples their one output token (o is 1, which floor(~exp(200)) + 1 draws with probability 0.005: 224 ± 15 turns expected), which never decode: tpot counts the 44 660 turns that do. The output is a whole number of tokens, 200.5 on average. Before token counts were whole (#661) the workload drew ~exp(200) + 1, whose fractional last token still cost a whole decode iteration: rounding down gives every turn one decode step fewer, so the response is shorter by about one decode step (0.000225 s) than it was, 0.06472 s before, on seeds 1–5 by 0.00021 to 0.00025 s, and the iterations fell from 9 305 275 to 9 261 013.

TTFT is now separately observable, and at 19 ms it is a different quantity from the 64 ms response. Iteration-level simulation gives both.

Measured 2026-10-15 on serQ afee627 with whole token counts (#661, #700), by the command above; the engine table, which moves no other number, since

594.

wait NaN on the kv pool

An admission that takes several pools joins the queue of the first one taking names, reqs, so kv never has a queue of its own and has no queue-wait samples. Read the wait on reqs.


Next: turn the load up. → The cliff