Skip to content

Lane host: a request whose prefix another prompt is filling waits for its cut - #1321

Closed
wojo wants to merge 1 commit into
ashhart:mainfrom
wojo:up-prefix-wait
Closed

wojo wants to merge 1 commit into
ashhart:mainfrom
wojo:up-prefix-wait

Conversation

@wojo

@wojo wojo commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

What this changes

When prompts fill a chunk a step, the lane host holds back a queued request whose prompt shares a cut that another
admitted prompt is still filling. The request waits, holding no lane, until that fill stands at the cut, then resumes
the kept state. So requests that arrive together on a new system prompt compute it once, not once each.
TENSORFOLD_PREFIX_WAIT_MS bounds the wait: 60000 by default, and 0 turns it off.

Fixes #1310

What is wrong on main (67cc8fb)

  • takeJob (zig/src/core/lane_host.zig:298-320) admits the queue's head whenever a lane is free, and
    lane_stream.open (lane_stream.zig:27) looks it up. A lookup can only return states that are already kept
    (prompt_cache.zig:243, :288).
  • With prefill_step the pass goes on the filling list (lane_host.zig:345-348) and advances a chunk between rounds
    (lane_host_fill.zig:80). It keeps the shared cut only when it stands there (Job.kept, lane_job.zig:51).
  • Every request admitted before that point has nothing to resume, so it fills the prefix itself, side by side with
    the first. GLM-5.3-Flash on CUDA fills in steps by default (families/glm/cuda_lanes.zig:93-100).
  • On two GB10s at TP=2 (674fb9e, Concurrent requests on a new shared system prompt each prefill it when prompts fill a chunk a step #1310), eight requests on a new ~26,600-token system prompt computed 24,576 / 73,728 /
    172,032 duplicate prefill tokens at 2 / 4 / 8 at once: every request that started while the first was still
    filling. Whole passes do not do this, because each pass ends before the next lookup.

The change

  • lane_host_prefix.zig (new, 84 lines) holds the wait, which runs at admission:
    • waits runs when admit.pick has chosen a request. The request waits for the furthest mark of an admitted,
      unfinished, foreground fill when three things hold. The mark is past where that fill has stood. It adds at least
      the store's min_gap past what find holds for this request now. And it would serve this request's own lookup.
      A waiting request stays in the queue in its place and is passed over.
    • release runs at the top of each pick. It lets a request go on once its fill has stood at the mark (kept or
      refused), once the fill is no longer on the filling list (prompt in, cancelled, failed, requeued: matched by
      pointer and id), or once the bound has passed (timeouts counts those). Its ordinary lookup then resumes the cut,
      or finds nothing and fills.
  • No deadlock: a request waits once and only for an admitted fill, and admitted fills never wait. So there is no
    cycle, and the oldest fill always runs.
  • Scope: only with prefill_step fills and a prompt cache. Never for background fills, pictures, embeddings,
    prompt logprobs or untaught requests. Whole passes never wait (core.fills() is false). Family round planners
    (LaneHost.rounds) admit on their own path and never wait either.
  • prompt_cache.zig: Store.wouldServe, which judges a state another pass will keep the way find judges a kept
    one. The state must be eligible on this request's starts, have the same tokens through the lookahead, and have the
    same decoded spans.
  • lane_host_admit.zig: pick releases waiters, passes over the ones that still wait, and asks about the one it
    picks. lane_job.zig: Job.kept records the furthest mark (reached), and a requeue resets it.
  • lane_host.zig carries only the setting (prefix, read in init the way share is). It goes from 584 to 586 lines.
  • RUNBOOK.md: one paragraph on the wait and its setting.

Why on by default. A waiter holds no lane and no memory. It waits only for rows that would otherwise be computed
twice, and the fill it waits for runs without that competition, so it reaches the cut sooner. The wait ends as soon as
the cut is reached or cannot come, and the bound is a ceiling on top of that. Replies do not change: the waiter resumes
a state kept by the same arithmetic it would have computed, which is the cache's existing resume contract. That is
checked here by token hash against the wait-off replies. Off, every request computes its own prefix, as on main.

Tests

zig/src/core/lane_host_prefix_test.zig uses the stepped fake backend with the store's rules set as GLM's CUDA rules
are: a planned 16-row grid, lookahead 1, min_gap 256. Requests are queued before the host starts, so they arrive
together.

  • Four requests on one 1,030-token system prompt plus an unrelated one, at the default bound. Request 1 computes the
    prompt, and requests 2-4 wait and resume at the 1,024-token cut (cached 0, 1024, 1024, 1024, 0). The unrelated
    request neither waits nor is waited for. Every reply equals its run with the wait off, by tokens. waits is 3,
    there are no timeouts, and nothing is left queued.
  • The same with the wait off (0): every request computes the prompt (cached all 0). This is main's behaviour.
  • A leader cancelled mid-fill releases its waiter, which computes the prefix itself (cached 0) and finishes.
  • A waiter cancelled while it waits leaves the queue once. The fill runs on.
  • At a 1 ms bound, with 3 ms chunks, the waiter is released and counted as a timeout, then fills.
  • Whole passes never wait, and the kept cut still serves the later requests (cached 0, 1024, 1024, 1024, 0).
  • A shared cut under min_gap (128 tokens) is not waited for.
  • TENSORFOLD_PREFIX_WAIT_MS parsing: unset, blank, signed, fractional or garbage values give the default, 0 turns
    the wait off, and 250 is 250 ms.

Before the decisions were implemented, five of the seven failed. That run had the fields, the pick plumbing and the
tests in place, with waits returning false, release doing nothing and the parser returning the default:

lane_host_prefix_test.test.prefix wait: requests on a system prompt another prompt is filling wait for its cut and resume it...slices differ. first difference occurs at index 1 (0x1)
  expected: 0, 1024, 1024, 1024, 0
  found:    0, 0, 0, 0, 0
lane_host_prefix_test.test.prefix wait: a cancelled leader releases its waiter, which computes the prefix itself...expected 1, found 0
lane_host_prefix_test.test.prefix wait: a waiter cancelled while it waits leaves the queue once; the fill runs on...expected 1, found 0
lane_host_prefix_test.test.prefix wait: at the bound a waiter is released, counted, and fills beside the leader...expected 1, found 0
lane_host_prefix_test.test.prefix wait: TENSORFOLD_PREFIX_WAIT_MS is whole milliseconds, 0 off, anything else the default...expected 0, found 60000000000
16 passed; 0 skipped; 5 failed.

Receipt

  • Environment, host tests: base 67cc8fbc, branch commit 4aa0a98d; Zig 0.17.0; Apple M4 Mac, macOS 27.0.1. The
    Xcode Metal Toolchain component is not installed, so the Metal library builds in zig build test fail on both base
    and branch.
  • Environment, served run: base 67cc8fbc ("before") against branch 4aa0a98d ("after"), both tensorfold-native
    built alike (Zig 0.17.0, aarch64-linux-gnu, -Doptimize=fast) with the same 97 embedded CUDA kernel images (one
    fatbin set, nvcc 13.0 V13.0.88, sm_121). Two NVIDIA DGX Spark (GB10, 121 GiB unified memory), driver 580.178.04
    (CUDA 13.0), cuBLASLt 13.1.1.3, NCCL 2.30.7, glibc 2.39, Ubuntu 24.04.5 LTS, kernel 6.17.0-1032-nvidia, aarch64;
    TP=2 over RoCE, one model process on each. Checkpoint Mia-AiLab/GLM-5.3-Flash-EXL3-4bpw-TensorFold@078455ff, MTP
    drafts. The same serve arguments on every start: serve <checkpoint> --backend cuda --parallel 8 --context 131072 --no-update-check, with the default prompt cache and TENSORFOLD_CUDA_KERNELS=native. Each case ran on a freshly
    started server. The after cases ran as the server's first traffic; the before case followed two unrelated 32-token
    requests on its start (other prompts; the fan-out's are salted and new at each level). Both start-up lines:
    8 streams at once, 1.77 GiB each at a 131072-token window.
  • Workload: a stdlib client,
    prefix_fanout.py,
    sends 8 chat requests per level. They share a system prompt of ~26,600 tokens (26,588 to 26,615; the client's
    --system-tokens 28000 target) that is new at each level, each asks its own short question, and they go 1, 2, 4
    and 8 at a time, greedy, 32 tokens each. Duplicate prefill is the sum of computed prompt tokens (prompt less
    cached_tokens) minus the ideal: the first prompt whole, plus each other prompt less the shared cut on the
    2,048-row grid (24,576 tokens at every level). The after runs reuse the before run's exact prompts and compare every
    reply by token hash.
requests at once before: duplicate tokens after: duplicate tokens after, TENSORFOLD_PREFIX_WAIT_MS=0 before: level wall after: level wall after, wait off: level wall
1 0 0 0 38.0 s 38.0 s 38.0 s
2 24,576 0 24,576 57.6 s 37.7 s 57.3 s
4 73,728 0 73,728 99.2 s 38.4 s 99.3 s
8 172,032 0 172,032 179.9 s 38.9 s 180.2 s

After, every follower at every level reports cached_tokens 24,576. With the wait off, the c8 level reports 0 on all
eight, as before.

  • Exactness: every after reply equals its before reply by token hash: 32/32 with the wait on and 32/32 with it off.
    bench_concurrent.py --levels 1,4 --alone --serial: before, alone 30/30 equal and serial 4/4 equal; after, the same
    (0 unequal, 0 failed on both).
  • Decode speed, bench_openai.py --tokens 256 --reps 3 --temperatures 0 (median of the 3 reps): before 48.37 tok/s
    (fibonacci-raw) and 27.31 tok/s (gpu-chat-no-think), after 47.21 and 26.56. One serve per tree, not repeated or
    alternated, so this is a single sample each. Decode does not reach the change: it acts only at admission.
  • Prompt speed: n/a for a cold single prompt, which never waits. The fan-out walls above are the prompt-side effect.
  • Tests run: zig build test-cuda-host, base 253/253 steps and 3253/3267 tests passed (14 skipped), branch 253/253 and
    3260/3274 (14 skipped; the 7 new tests). zig build test: base
    393/490 steps, 2400/2414 tests passed (13 skipped, 1 failed); branch 393/490 steps,
    2407/2421 tests passed (13 skipped, 1 failed; the 7 new tests). The steps that fail on both
    are the 55 Metal library builds (no Metal toolchain here) and cortheon fuzz: every reader takes linear time on long inputs, a timing test that fails on this machine on base too. The engine seam's own module (engine_api.zig, which
    macOS runs only beside the Metal libraries) passes 279/279 on base and 286/286 on the branch.
    python3 tools/zig/lean_check.py: 434 problems on base and 434 on the branch (all inherited; none in the files this
    touches).
  • Not run, and why: Metal devices (no Metal toolchain here, and no Mac device run). Metal families with stepped fills
    take the same host path, which the fake backend tests. TP=1 and TP=3 or more, other CUDA families, HIP and XPU served
    runs, fuzz-cortheon and bench_cache.py were not run either: a cold single prompt never waits. One served run per
    tree (and one for the wait off), so the walls and the decode figures are single samples.

Limits and follow-ups

  • The counters (LaneHost.prefix.waits, .timeouts) are not exported to /metrics. The fan-out effect already shows
    in cached_tokens and the prompt-cache counters. I can add them in a follow-up if you want them there.
  • A waiter waits for the furthest qualifying mark of any one fill. It does not chain to a second fill that started
    after the one it waits for.
  • The bound is wall time, not tokens. A fill throttled by TENSORFOLD_PREFILL_SHARE reaches its cut later, and the
    waiter's own fill would have been throttled the same way.

Checklist

  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off. (Admission
    only. The waiter's pass is an ordinary resumed fill.)
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.
    (Resumed equals fresh: 32/32 token hashes, wait on and off. Concurrent equals solo: bench_concurrent alone 30/30
    and serial 4/4 on both trees. "draft": false was not run, because the change does not touch drafting.)
  • No precision traded for speed. If the bits change, the description says which sums change and why. (No
    arithmetic changes.)
  • A new or changed GPU kernel is registered with the kernel contract (docs/kernel-contract.md); each backend's
    SUMMARY line is in the receipt. (No kernel is added or changed.)
  • Prompt processing is no slower than the last release. (Not compared with a release. A cold single prompt does
    not reach the change, and the fan-out walls are above.)
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not. (Host tests on an
    Apple M4 without the Metal toolchain, and a served CUDA run on two GB10s. No Metal device run.)
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (Five of seven
    fail with the decisions stubbed. They are host tests with nothing to skip on.)
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

By Robert Wojciechowski (wojo on GitHub, @rwojo on X)

… its cut (TENSORFOLD_PREFIX_WAIT_MS)

With prompts filling a chunk a step between decode rounds (prefill_step,
GLM-5.3-Flash on CUDA by default), requests that arrive together all start
filling at once, and each computes a system prompt none of them has kept yet.
Whole passes never did: each ends before the next lookup, which finds the cut.

- lane_host_prefix: before a queued request takes a lane, if an admitted
  fill still has a mark ahead that the request's prompt shares, that its own
  lookup would resume and that adds at least the store's min_gap past what is
  kept for it now, the request stays in the queue, passed over, holding no
  lane. It goes on in queue order once that fill has stood at the mark (kept
  or not), the fill has left the filling list (prompt in, cancelled, failed,
  requeued), or TENSORFOLD_PREFIX_WAIT_MS passed (60000 by default, 0 off).
  Its ordinary lookup then resumes the cut. A request waits once and only for
  an admitted fill, which never waits, so there is no cycle and the oldest
  fill always runs. Never for background fills, pictures, embeddings, prompt
  logprobs or untaught requests; whole passes and rounds planners never wait.
- prompt_cache: Store.wouldServe, a state another pass will keep judged as
  find judges a kept one (eligible, same tokens through the lookahead, same
  decoded spans).
- lane_host_admit.pick releases waiters, passes over the ones still waiting
  and asks the wait of the one it picks; Job.kept records the furthest mark
  a pass has stood at. lane_host.zig only carries the setting.

Tests (lane_host_prefix_test.zig, the stepped fake backend on a 16-row grid
with lookahead 1, as GLM's CUDA rules): four requests on one system prompt
and an unrelated one, all queued together, compute it once (three resume the
cut, replies equal to the wait off), the unrelated one neither waits nor is
waited for; with the wait off every one computes it (main's behaviour); a
cancelled leader releases its waiter, which computes the prefix itself; a
cancelled waiter leaves the queue once; the bound releases and counts; whole
passes never wait; a cut under min_gap is not waited for; the setting's
parsing. Five of the seven failed with the plumbing and stubbed decisions.
@ashhart

ashhart commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Landed on main in 6588b5a (rebased onto current main), with a changelog line. Thanks @wojo.

Device checks, main vs this PR:

  • GB10, Qwen3.8-27B 4-bit, CUDA: eight chat requests arriving together on a new 28k-token system prompt. The first wave's last first token went from 201 s to 31 s in alternating runs (base new new base), and every request after the first resumed the cut. Prefill (2k to 64k) and the 1/2/4/8-stream sweep are flat; an alternating prefill recheck at 2k/8k is within 0.2%. Chat resume equals fresh.
  • GB10, Flash Next FP4: the same wave's last first token went from 146 s to 25 s.
  • M3 Ultra, Qwen3.8-27B: prefill and sweep flat (within 0.3%), 0 of 20 prefill token rows differ from main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent requests on a new shared system prompt each prefill it when prompts fill a chunk a step

2 participants