Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 268 additions & 0 deletions TECHNICAL_REPORTS/0.9/3723-admission-join-output-thread-20261203.en.md

Large diffs are not rendered by default.

268 changes: 268 additions & 0 deletions TECHNICAL_REPORTS/0.9/3723-admission-join-output-thread-20261203.ko.md

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion docs/CONTINUOUS_BATCHING.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,22 @@ On CUDA most of a decode step's host time is the evaluation of its lazy MLX grap
- **Feedback.** Step n+1's input tokens are step n's sampled tokens. They are copied on the device into the token slot of the persistent step-input buffer (`step_inputs::stage_i32_from_device`), after the step's host bytes and before the slot's event, so the staged layout is the one a host-staged step has: the pipelined step keys like the synchronous one and replays the same graph.
- **Collect.** Step n's tokens are read from its pinned copy after its event (`PendingTokens::wait`), while step n+1 runs. Two slots per graph are enough because at most two steps are in flight; a read whose slot a third launch reused is refused rather than answered with later tokens.

The host therefore builds step n+1 while step n runs, and commits step n (stop checks, detokenization, streaming) while step n+1 runs. The rest is the #632 state machine unchanged: a row that finishes at step n (length, end of sequence, a stop string, a cancellation), an admission, a preemption or a changed id set waits for the step in flight, unwinds the speculative appends (`apply_lookahead_trim`) and re-runs the tick synchronously, which for a graph tick is a synchronous replay. Rows whose every layer is pool-backed layer state (Gemma 4 with its default paged sliding state) join the pipeline on these ticks too: the pool's block tables unwind their speculative appends exactly, which the eager pipeline cannot assume for scheduler-owned state, so on the eager path they stay synchronous as before. `verify` and `eager` modes never pipeline. `MLXCEL_DECODE_GRAPH_PIPELINE=0` restores #3668's routing: a replayed step is synchronous, by default a tick of more than one row that the lookahead pipeline can run stays with the eager pipeline, and `MLXCEL_DECODE_GRAPHS=always` replays the multi-row ticks synchronously too. The pipeline engages only where it engages for eager steps: no `--max-kv-size` bound (which a single-slot `--ctx-size` launch also sets), no pending admission, chunked prefill or preemption, and no loop detection.
The host therefore builds step n+1 while step n runs, and commits step n (stop checks, detokenization, streaming) while step n+1 runs. The rest is the #632 state machine unchanged: a row that finishes at step n (length, end of sequence, a stop string, a cancellation), an admission, a preemption or a changed id set waits for the step in flight, unwinds the speculative appends (`apply_lookahead_trim`) and re-runs the tick synchronously, which for a graph tick is a synchronous replay. Rows whose every layer is pool-backed layer state (Gemma 4 with its default paged sliding state) join the pipeline on these ticks too: the pool's block tables unwind their speculative appends exactly, which the eager pipeline cannot assume for scheduler-owned state, so on the eager path they stay synchronous as before. `verify` and `eager` modes never pipeline. `MLXCEL_DECODE_GRAPH_PIPELINE=0` restores #3668's routing: a replayed step is synchronous, by default a tick of more than one row that the lookahead pipeline can run stays with the eager pipeline, and `MLXCEL_DECODE_GRAPHS=always` replays the multi-row ticks synchronously too. The pipeline engages only where it engages for eager steps: no `--max-kv-size` bound (which a single-slot `--ctx-size` launch also sets), no pending preemption (and, with `MLXCEL_PIPELINE_ADMISSION_JOIN=0`, no pending admission or chunked prefill), and no loop detection.

**Admissions join the pipeline (issue #3669).** An admission no longer tears a pipelined graph tick down. Before a queued prompt's prefill, a parked prefill's next chunk, or the prefill pieces of a budgeted step (`--max-num-batched-tokens`), the stored lookahead's in-flight step is read to the host (the prefill's forward would wait for it anyway, since both run on the generation stream) and kept with its speculative append. Step n+1 has then run with the old membership. On the next decode tick the old rows commit step n+1's tokens and step n+2 is launched over the whole batch, so a newly admitted row joins at the step after next, which forwards the first token its prefill sampled; from the tick after, the steady pipeline runs over the joined batch. Nothing is unwound and no step is run twice. Because a queued prompt or a parked chunked prefill no longer ends the pipeline, a pipelined graph tick keeps priming while requests wait; only a pending preemption stops it. Teardown remains for a preemption (the lookahead is torn down before the victim's caches are released), any finish or cancellation (`finalize_completed`), a lookahead built on the eager path, and a row that stops on step n+1's token (its tick unwinds the old rows' append and runs synchronously). Releasing a decode pad row does not tear the lookahead down: the step in flight read that row's block, so it is read to the host first (`settle_lookahead`) and kept. `MLXCEL_PIPELINE_ADMISSION_JOIN=0` restores the teardown. `/metrics` counts `mlxcel_batch_decode_lookahead_admission_joins_total` and `mlxcel_batch_decode_lookahead_admission_teardowns_total`.

**Streaming on an output thread (issue #3669).** On CUDA each worker of the standard serving loop runs one output thread for detokenization and streaming, unless it dispatches speculative decoding; the disaggregated prefill and decode roles (`--node-role`) keep streaming on the scheduler thread. A request is routed to it when it joins the decode batch, if its text cannot decide its next step: no stop strings, no `n_indent` or `t_max_predict_ms` bound, and no token-only drafter (prompt lookup or a full-model `--draft-model`). Its first token is streamed by the prefill as before; from then on the scheduler thread only pushes token ids onto a bounded queue (4096 records), and the output thread runs the incremental detokenizer, sends the pieces, streams the held tail at the end, builds the final result, records its completion telemetry and sends `Done`. Finishes that depend on token ids only (end of sequence, `max_tokens`, the context bound) stay on the scheduler thread. Every other event the scheduler sends for a routed request (an error, a failed forward) goes through the same queue, in the order it was sent, so the client sees exactly the stream the scheduler thread would have sent. A slow client cannot stall the scheduler: the client channel is unbounded, so the output thread never waits on a client, and the queue only fills if detokenization falls behind decode by 4096 tokens. A panic while handling one request answers that request with an error and cancels it; other requests continue. A preempted request takes its client sender back from the output thread before it is re-queued. Requests with stop strings or generation bounds keep the synchronous path, because their text can end the request before the next step. `MLXCEL_OUTPUT_THREAD=0` keeps all streaming on the scheduler thread. Metal, ROCm and CPU builds never start the thread.

**Synchronous host reads left on the decode path (issue #3669).** A pipelined graph tick reads only the pinned copy of the step before (after its event) and, on a teardown, waits for the step in flight without reading it. The reads that remain, and why:

| Where | Read | Why it stays |
|---|---|---|
| `read_lookahead_tokens` (eager lookahead pipeline) | `try_tokens_to_host` on the step's lazy token array | An eager step is not a captured graph, so there is no pinned copy; the read waits for that step only, with the next one already scheduled |
| `Engine::step_padded` (synchronous step, `run_engine_step`) | Evaluation and host read of the sampled tokens | The synchronous step is the fallback for everything the pipeline does not run (stochastic draws, penalties, structured output, logprobs, a teardown re-run); its next step needs the token |
| Synchronous graph replay (`dispatch_sync_decode` armed) | The replay's own readback after a stream synchronize | A tick the pipeline cannot run has no step to overlap with |
| `step_rows.rs` | None | The row hooks read host state only (stop matcher, bounds, structured matcher); the token reaches them already on the host |
| `speculative_finalize.rs` | None directly | Speculative rounds (MTP, DFlash, prompt lookup) verify proposals against the target's tokens on the host inside the speculative drivers; acceptance needs the comparison before the next round, and those ticks are never pipelined |
| `prepare_admission`, `release_decode_pad_rows` (both through `settle_lookahead`) | The pinned copy of the step in flight | Taken before an admission's prefill, which would wait for that step on the shared stream anyway, and before a pad row's block goes back to the pool, since the step in flight reads it |

**Controls and failure.** `MLXCEL_DECODE_GRAPHS=0` switches the feature off and restores the previous step unchanged; `always` replays every eligible step whatever the lookahead pipeline could do; `eager` keeps the step structure but evaluates every step, and `verify` replays and evaluates each step and fails it on a token mismatch (both are diagnostics). `MLXCEL_DECODE_GRAPH_MAX_BS` caps the bucket. A capture that fails before any node could have been marked evaluated leaves the step to be evaluated; a capture that fails after that point is fatal for the step, which fails, and graphs are disabled. Three non-fatal capture failures, or 48 captures with fewer replays than captures, also disable graphs. A CUDA error on a launch or readback fails that step's requests with the error string and disables graphs for that engine (each model engine has its own `DecodeGraphs`) with one error log. Misses, parameter mismatches, evictions, suspensions, capture failures and verify mismatches appear only in the log. Each capture logs `Captured <n> decode graphs (buckets [...]) in <s> s, arena <GiB>`, and `/metrics` carries `mlxcel_batch_decode_graph_replays_total`, `mlxcel_batch_decode_graph_captures_total`, `mlxcel_batch_decode_graphs`, `mlxcel_batch_decode_graph_bytes` and `mlxcel_batch_decode_graph_pipelined_replays_total` (the replays launched before the previous step's tokens were read). A CUDA error while waiting for a pipelined replay's tokens fails that step's rows like a failed synchronous readback and disables graphs for the engine. Metal, ROCm and CPU builds never construct any of this: `mlxcel_core::step_graph::mode_from` returns off for every backend but CUDA whatever the environment says.

Expand Down
Loading
Loading