Skip to content

server: one step's sequences sample at once on lanes (528.6 -> 123.2 us/step at six minds, same tokens); the server is C++20 - #125

Closed
joelteply wants to merge 1 commit into
fable/per-token-samplingfrom
fable/parallel-sampling
Closed

joelteply wants to merge 1 commit into
fable/per-token-samplingfrom
fable/parallel-sampling

Conversation

@joelteply

Copy link
Copy Markdown

Stacked on #122. It retargets to codex/s4-per-mind-integration when #122 merges. The commit message has the full design.

What changes

  • Sampling from a row. A sampler now samples from a gathered row (raw pointers into one decode's outputs) and never calls into the context.
  • presample(). It samples every slot of a view at once on the lanes: plain sampling and the MTP verify alike. The existing loops then emit in slot order, unchanged.
  • server_lanes. Claims are lock-free and tagged with a run id. Each lane has its own futex wake word, and a run wakes only as many lanes as it has jobs. The caller never sleeps, and on macOS lanes run at user-interactive QoS.
  • C++20. The server is now C++20, for atomic wait/notify; nothing forced C++17.

Measured (qwen35-0.8b, M5, 2026-11-16)

  • test-server-lanes: one decode of six sequences, 400 repetitions alternating which runs first. Serial 528.6 µs/step, laned 123.2 µs/step (4.29×), the same token every repetition. The lane runs themselves are checked too: every job runs once at 1 to 1,000 jobs, and a job's exception reaches the caller.
  • Six concurrent MTP streams at temperature 0: text identical to the serial build on every stream.
  • End-to-end tok/s is not a receipt on this node: the live residents share the GPU, and serial runs alone ranged 72 to 219 tok/s.
  • 0 warnings at C++20.

Measured after deploy

Owner: Fable, in the #122/#123 window. The host gap between GPU steps (/props step_budget where present) and acts/h at four or more residents.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

…us per step at six minds, same tokens); the server is C++20

Every sequence a decode step produced was sampled one after another on the main thread while the GPU
waited for the next submit. Each slot's sampler is its own state, so they need not wait on each other.

- common_sampler_row: one output row's pointers, gathered by the thread that owns the context (one
  synchronize). common_sampler_sample_row / _and_accept_rows read only the sampler and the row, never the
  context; common_sampler_sample(ctx, idx) is gather + sample_row, so there is one path.
- presample(): at the head of post_decode it finds every slot this view samples (plain or the MTP verify,
  the same predicate as the loops), gathers rows, and samples and accepts all of them at once on the
  lanes; the loops then emit in slot order exactly as before (an assert keeps the predicate in step). A
  sampling error is carried to its own slot's step, as the serial path raised it.
- server_lanes (server-lanes.h): claims are lock-free from one (run id << 32 | index) word, so a lane
  finishing an earlier run can never claim in a new one; each lane has its own wake word, and a run wakes
  only as many lanes as it has jobs beyond the caller's (atomic notify: a futex, not a mutex and
  condition variable); the caller runs jobs and never sleeps; idle lanes burn nothing; on macOS lanes
  run at user-interactive QoS (performance cores). Lanes = min(slots - 1, cores the OS reports - 1).
- The server targets C++20 (atomic wait/notify); u8path, deprecated in C++20, becomes a char8_t path.

Receipts (qwen35-0.8b, M5, 2026-11-16): test-server-lanes: every job runs once at 1..1000 jobs, a job's
exception reaches the caller; one decode of six sequences, 400 reps alternating order: serial 528.6
us/step, laned 123.2 us/step (4.29x), the same token every rep. Six concurrent MTP streams at
temperature 0: text identical to the serial build on every stream. End-to-end tok/s on the M5 is not a
receipt here: the live residents share the GPU, and serial runs alone ranged 72-219 tok/s. 0 warnings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
joelteply added a commit that referenced this pull request Nov 16, 2026
…en, the exact path); a row is sampled without the context

Codex's review of #122: `while (n < cap)` never ended when fewer than cap logits are not NaN (NaN never
compares at or above any threshold, even -inf), so an all-NaN or mostly-NaN model output wedged the slot.
Now, once the threshold reaches -inf with fewer than cap candidates, every non-NaN logit is already in:
coarse_candidates reports that and the step samples the full array, which is the exact path itself, so
NaN input behaves exactly as it does without the ballpark (never a different sampler). The strided seed
sample skips NaN (it breaks nth_element's ordering), and the miss margin is capped at the vocabulary
(an unbounded margin made the rank cast undefined).

The row seam (common_sampler_row, sample_row, sample_and_accept_rows) moves down from #125: sampling from
a gathered row reads only the sampler, which is what lets the test feed fabricated logits.

test-sampler-coarse "NaN logits": 0, 100 and 247,320 finite of 248,320 (distinct values, NaN for the
rest): the step ends and matches the full array over 8 steps each. With the bound removed the test hangs
until a 60 s alarm kills it, printing no NaN line. Every earlier case still identical over 400 steps;
penalties ok; 0 warnings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
@joelteply

Copy link
Copy Markdown
Author

Superseded by #129: rebuilt on #122's head after the row seam moved down into #122.

@joelteply

Copy link
Copy Markdown
Author

Independent source review of exact cc21b09: BLOCK in server-lanes.h.

  1. A worker can start after the destructor sets stop and increments wake. work() then initializes seen from that already-incremented wake and waits forever before checking stop; destructor hangs in join. Check shutdown before sleeping and add a deterministic delayed-worker-start/immediate-destruction test.

  2. done == n is job completion, not worker quiescence. An old worker can still be in take() when the caller begins the next run. run() overwrites job/n_jobs/done before publishing the new tagged next. If the next run has more jobs, the old worker can observe the new larger n_jobs with the old next word, successfully claim an old-run index, invoke the new function and increment the freshly reset done counter before the caller publishes new next. The tag does not prevent this interleaving because the old word remains current until the final store. Publish an immutable run descriptor with its bounds/function and give workers a run-lifetime/quiescence contract before reuse. Add a barrier-controlled consecutive-run test that pauses a helper after its last done increment while the next, larger run starts.

The measured parallel speedup is useful; these lifecycle races need fault replay before this can replace serial sampling. Parent #122 is also still under review; rebase its latest corrections before final exact-head review.

joelteply added a commit that referenced this pull request Nov 16, 2026
… stop checked before every sleep (Codex's review of #125)

- A lane holding an earlier run's claim word could read the next run's job count (stored before the new
  claim word) and claim against the old word, running one of the new run's jobs twice. Now each run's
  job, count and id are one record, published (release) BEFORE the run's claim word; a lane reads the
  claim word, then the record, and claims only when the word's run id equals the record's. Records
  alternate in two places with atomic fields: one is rewritten only two runs later, when a stale lane's
  claim word is two runs old and its compare-exchange fails.
- A lane started from a load of its wake word and never checked stop before its first wait, so a
  destructor that signalled before the lane ran was missed and the join hung. Now a lane starts from 0
  and checks stop before every sleep.

Receipts (M5, 2026-11-16): a ThreadSanitizer build of the lanes alone (500 construct/destroy, then 20,000
back-to-back runs of 2-7 jobs, each job checked exactly once): clean, every job once; the same harness
against the previous header hangs until a 120 s alarm. test-server-lanes adds the regression (2,000
construct/destroy; 50,000 back-to-back runs exactly once) and still picks the serial token every rep
(3.79x-4.89x per step across runs on the shared M5). 0 warnings.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
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.

1 participant