Skip to content

Statements

The kernel statements of a session or server block. Commands take no time: they run whenever a session is ready, in the order sessions became ready. Only run lets the clock move. Every expr below is evaluated at the Session moment unless the entry says otherwise.

The serving vocabulary provides shorthand for these statements. Resource amounts must have the matching Cost type: run work for its stage(s), and hold/grow/load amounts for their pools. Common reuse and cache expressions name all pools held by that hold. These conversions do not change evaluation moments. Lease durations, indices and predicates remain ordinary quantities.

Statement Does
Size / Cost declares request quantities or resource costs
set assigns a session attribute
observe records a sample
turn draws attributes and waits for the turn's response
hold takes units of pools for the scope of a block
grow enlarges the innermost hold, or the session's lease
shrink gives back part of the innermost hold, or of the session's lease
drop discards the session's cached prefix
release gives the innermost hold's units back now
load the KV of some tokens arrived from outside
run spends work at a stage
if a test or a draw
while repeats while a condition holds
loop repeats a block
choose picks an index by the smallest key
end ends the session

Typed declarations

Size items = 3;
Cost duration = cost(svc, 2 * items);
Cost processing = { mem: items, svc: 2 * items };

Size declarations belong to the workload. Cost declares a scalar resource cost or a record of independent resource costs. A bare number cannot initialize a scalar Cost because it does not identify a resource. Declaration computes and stores values; hold, run, grow and load apply them at their ordinary execution moments. See attribute types for ownership, field order and initialization rules.

Several pools can share one scope:

hold P(processing.P), Q(processing.Q) { run svc(processing.svc); }

Here P and Q stand for declared pools. Admission needs room in both and waits in P's queue; this differs from nested holds, where P can remain allocated while waiting for Q. Use one hold for a common admission and lifetime, and nesting for different lifetimes.

A queue entry's admission header can read its parameters and own resources, but cannot read Cost fields computed by its body. Keep those conversions in the header, for example hold kv(cost(kv, prompt)), or pass the ordinary quantity as an entry parameter. Cost fields remain available to the body.

set

set NAME = expr;

Assigns the session attribute NAME. Workload sizes are read-only in the server; use a separate server attribute for derived quantities. Every name assigned by set or choose in session code is an attribute of every session. In an iteration body, set instead assigns an engine register.

observe

observe NAME = expr;

Records a sample of expr after warm-up, with the time, session and turn. --dump DIR writes the samples; the report summarises them. An observation whose outermost operator is a comparison, &&, || or !, and that stays 0 for at least 40 samples, produces a note: observe … is constant 0 diagnostic. Check whether the condition was expected to occur before interpreting its summary.

turn

turn;

Loads the next trace turn (if present), then runs the workload's turn block. The server handles that turn; the next session statement waits for its response. Allowed only in a workload's session. The client's time between turns is tool Z;, which runs on a delay of the workload's own unless a stage tool is declared.

hold

hold POOL (units) [reserve (r)] [, POOL (units) [reserve (r)]]*
     [prefix (key: n, …)]
     [reuse (ρ)]
     block
     [cache (ℓ)]
     [lease POOL (t)];

Joins the queue of the first pool. When the head is admitted, units of each pool are allocated, the body runs, and at its end the units are released and min(ℓ, computed) stay cached (rounded down to blocks). The cache clause is what makes the hold take part in the prefix cache: with it the admission consumes the session's own cached prefix (cached); without it the hold leaves the session's cached blocks where they are and cached is 0 in its body (a body that reads it there, or a reuse there, does not link). cache (0) consumes the prefix and keeps nothing. The prefix is the cached part of the hold's chain: its prefix, or the session's own without one. Holds whose chains start with equal keys share those segments' cache, and a prefix several holds read is allocated once.

Argument Type Moment Default Description
POOL pool One or more. Admission needs room in every one; the hold waits in the first pool's queue.
units Cost(POOL) Admit Units to allocate at admission.
reserve Cost(POOL) Admit units Room required before admitting: max(units, r), rounded up to blocks. It does not change the allocation. See reserve held for outstanding reservations.
prefix key: n, … Admit (serial: inf) The sequence as segments, each a key and a length. Keys and lengths read attributes and numbers only (no state, clock, draw, cached or computed). A segment of length 0 is left out; only the last may be endless. The hold takes at least one pool with block (on one without, its chain is the session's own) and none that spills or is spilled to.
reuse Cost(all held pools) Admit no bound At most this many units of the cached prefix of the hold's chain are read (rounded down to blocks), and with prefix never more than the hold allocates; cached is set to what was. What the session's entry held past them stays as a dead entry until evicted. Without reuse, the whole cached prefix is read: with prefix up to the allocation, without it the session's whole entry. Only with cache: a hold without the clause consumes nothing.
block block Session The body.
cache Cost(all held pools) Session none Units kept cached at the end, at most what was computed. Read when the session releases. Its presence is what makes the hold consume the session's prefix at admission; cache (0) consumes and keeps nothing, no clause leaves the prefix where it is.
lease pool, expr Session none That pool's allocation outlives the scope: neither evictable nor a preemption victim (unless the pool says preempt leases by) until release of it, t clock units, or the session's end; cache applies then.

The header's moment

The header (units, reserve, reuse) is read at admission, not when the session queues, and cannot draw. A header cannot use a value captured from live pool or stage state by an earlier set: that value may be stale by admission. Write the expression in the header instead; in the body, read cached for the prefix actually reused.

If the hold is preempted (preempt lifo, or preempt by) it re-enters the head of the queue (or, under requeue tail, the queue as a newcomer) and the statement executes again with computed set to the position it had reached.

grow

grow POOL (d);
Argument Type Description
POOL pool A pool the session holds, or leases.
d Cost(POOL) Units added to the innermost hold on POOL, or outside every hold of it to the session's lease of it, rounded to blocks.

If it does not fit, the pool's preempt applies.

shrink

shrink POOL (d);
Argument Type Description
POOL pool A pool the session holds, or leases.
d Cost(POOL) Units given back by the innermost hold on POOL, or outside every hold of it by the session's lease of it, to the nearest block above what remains.

The inverse of grow. A hold keeps at least its position (and under reserve held what it gives back is no longer reserved); a lease keeps something, and a shrink that would leave it at nothing fails the run: release ends a lease. The units go back at once, and a hold or a growth waiting for them may be let in. Nothing held or leased there fails the run, as for grow. A negative grow is not this: an amount is not negative (Amounts). ThunderAgent counts a program that is calling a tool at its --acting-token-weight this way (examples/multi-turn/thunderagent.sq).

drop

drop POOL;

Discards the session's entry in POOL: what hangs under its serial's key, leaves first, and no shared segment. end keeps the session's cached prefixes (the cache does not know a session has left), so a program that models dropping them writes this in the server on the session's last request, which the workload sends as a parameter (send (…, final: more == 0), then if (final) { drop kv; }): the server holds the deployment's pools, the workload only its own.

release

release POOL;

The innermost enclosing hold on POOL gives its allocation back now, caching per its cache, and holds POOL no longer. Outside any hold on POOL it ends a lease of POOL. A session that holds and leases nothing there releases nothing.

load

load POOL (n);
Argument Type Description
POOL pool
n Cost(POOL) Tokens of KV that arrived from outside the engine.

The innermost enclosing hold's computed position on POOL advances by n, within its allocation; a program that needs more grows first. Afterwards cache and cached count them.

run

run STAGE [prefill | decode] (work) [growing POOL];
run STAGE, STAGE [, STAGE]* (work, share = POLICY);
run STAGE [, STAGE]* (work [, share = POLICY], latency = x);
Argument Type Description
STAGE stage Indexed when the stage is an array.
mode prefill | decode Required on a step stage, forbidden on any other.
work Cost(STAGE) Clock time at rate 1 (fifo, delay), or at φ(present)/present (ps), or tokens on a step stage. A run of zero work completes at once.
growing pool Only on a step stage. The hold on this pool grows block by block as the run advances, preempting if needed.
latency expr Seconds the job waits at its stages before work starts, present there and taking none of their capacity (a NIXL READ posted, then polled); the stage report counts it as the run's wait. Not on a step stage. A run of zero work and a latency completes when the wait ends.

Blocks the session until the work is done.

An engine runs the prefill and decode of the requests it keeps. The server, a gateway's route and a queue's entry call it, E (input, output);, and the session reaches it by a turn. Only a queue's entry writes a run on a step engine, on its own engine, for what a request cannot say; the server, a route and the session do not hold a pool an engine admits either (#539).

With several stages the run is one job, a flow, that holds all of them from its start to its end; its work goes down at one rate everywhere, which the run's share, a policy of std/share, sets from the stages' capacities; the stages runs join are one fabric, with one policy. Every stage is ps(φ) with a constant φ above 0, a run names each stage array once, and the work is in the unit the capacities share (tokens at tokens per second, say). A stage array some run holds with another is shared for the whole run: every job on it, a single-stage run included, is a flow, and the report's utilisation is the capacity its flows carry.

stage egress[2] : ps(BwP);     // a prefiller's NIC, tokens per second
stage ingress[2] : ps(BwD);    // a decoder's NIC
…
run egress[i], ingress[j] (cost(egress, ingress, tokens), share = share.maxmin);

if

if (test) block [else block]
if (test) block else if (test) block [else block]
if (~bernoulli(p)) block [else block]
Form Argument Takes the first block
if (test) expr, must be 0 or 1 when test is 1
if (~bernoulli(p)) a draw, 1 with probability p with probability p

else if (test) block is else { if (test) block }: the second test is read only when the first is 0.

Any other value of test (a fraction, a count, a negative, NaN) is a run-time error, and a constant strictly between 0 and 1 is refused at link time as a draw written as a test.

while

while (condition) { … }

Tests the condition before each pass. It must be exactly 0 or 1; any other value is a validation error when constant and a run-time error otherwise. Zero skips the body and continues after the loop. A draw is explicit, for example while (~bernoulli(p)) { … }. Every path through the body must let time pass or end the session, just as for loop. An inner conditional loop may execute zero times, so it cannot by itself establish progress for an outer loop.

loop

loop block

Repeats the block until an end. Every pass must let time pass; a body with a path that can repeat without doing so is a link error.

choose

choose NAME in n by (key, …);
Argument Type Description
NAME identifier Becomes a session attribute.
n Cost(POOL) Number of candidates, 0 … n-1 (rounded down; 0 or less leaves NAME at 0).
key, … expr, one or more Evaluated with NAME bound to each candidate. Several keys compare in order: the second decides among the first's ties, and so on.

NAME is set to the index with the smallest key (tuple), ties to the smallest index. Used with an array of stages: choose j in 2 by (work(prefill[j]));.

end

end;

The session leaves. It releases every hold but keeps its cached prefixes. In a server, end; refuses the request: the session ends and its turn is not answered, so the report's client section records nothing of it (what the client sees). It is refused there inside a hold, inside a fork and in a queue's entry.

Examples

A complete program, a deployment and its workload:

model.sq
fn main() {
  pool reqs { cap 1; }
  stage svc : fifo;
  server {
    set t0 = now;
    hold reqs (cost(reqs, 1)) { run svc (cost(svc, 2)); }
    observe response = now - t0;
  }
}
workload.sq
workload {
  arrive batch(2);
}

Save the deployment as model.sq and its workload as workload.sq, then run:

serq run model.sq --workload workload.sq --horizon 10

See also

Serving vocabulary, pools, stages.