Repository navigation
perf: join admissions to the decode pipeline and stream off-thread - #3723
Merged
Merged
Conversation
On CUDA an admission no longer tears the pipelined decode-graph tick down. Before a prefill (Prefill arm, prefill chunk, or a budgeted step's prefill pieces) the step in flight is read to the host and kept with its speculative append; the next decode tick commits it to the old rows and primes the step after over the whole batch, so a new row joins at step n+2. Preemption, a row stopping at step n, and a cancellation still unwind it. MLXCEL_PIPELINE_ADMISSION_JOIN=0 restores the teardown. New counters: mlxcel_batch_decode_lookahead_admission_joins_total and _teardowns_total. Each CUDA worker also runs an output thread. A request with no stop strings, generation bound or token-only row is routed to it when it joins the decode batch: its detokenizer state and client sender move to the thread, the scheduler pushes each committed token through a bounded queue, and events keep their order through a private per-request channel. A slow client cannot stall the scheduler and a detokenization failure fails only its request. Preemption takes the request back. MLXCEL_OUTPUT_THREAD=0 keeps streaming inline. The result-building half of take_generation_result moves into ResultTemplate so the output thread can build the final result where the detokenizer lives. The remaining synchronous host reads on the decode path (step_rows.rs and speculative_finalize.rs included) are documented in CONTINUOUS_BATCHING.md.
With admissions joining the pipeline, `lookahead_safe` no longer refuses a prime while a chunked prefill is parked. The `MLXCEL_MIXED_STEP` arm still discarded the lookahead before its decode, so on CUDA every mixed tick primed a graph step, ran the prefill chunk while that step was in flight (a failed launch would surface against the chunked prompt), and threw the step away on the next tick. The arm now keeps the lookahead through the decode and settles it with `prepare_admission` before the chunk, as `execute_budgeted_step` does. With the join off it discards as before. On the output thread, a `Finish` record removed the row before flushing the tail and building the result, so a panic there left the client with a closed stream and no error, and kept the request in the failed set forever. The row now stays registered until `Done` is sent, and a failure on a terminal record (finish, detach, close) is not remembered. Validated with the output-thread unit tests and the decode-graph pipeline tests on GB10. Refs #3669
The pad-row release reads and keeps the lookahead rather than tearing it down, the join's teardown list now matches finalize_completed (any finish or cancellation), the host-read table names release_decode_pad_rows, the output thread docs say it starts only in the standard serving loop and skips token-only drafter rows, and the join paragraph says step n+2 forwards the first token the prefill sampled.
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Second half of #3669 (part 1 merged as #3719). On CUDA the decode pipeline now survives admissions, and detokenization and streaming move to a per-worker output thread. Metal, ROCm and CPU are unchanged: both paths are gated on pipelined decode graphs or the CUDA backend.
Closes #3669
Changes
Item 2: admissions join the pipeline
prepare_admission(newscheduler/admission_join.rs) replaces the unconditionaldiscard_lookaheadbefore a prefill in the run loop's Prefill arm and inexecute_budgeted_step(--max-num-batched-tokens). With the join on and a decode-graph lookahead stored, it reads the step in flight to the host and keeps it with its speculative append (the prefill would wait for that step on the shared stream anyway). Otherwise it is the old teardown.run_lookahead_tick,pipelined_join_decode. It applies whenseq_idsis the lookahead's rows plus newly admitted live rows. It commits step n+1 to the old rows and primes step n+2 over the whole batch, so a new row joins at step n+2. If a row stops on step n+1's token, the old rows unwind and the tick runs synchronously, as a stale lookahead does.lookahead_safeon an armed pipelined tick now only rejects a pending preemption.preempt_sequenceunwinds a kept lookahead before releasing the victim.release_decode_pad_rowswaits for a kept step before giving pad blocks back.MLXCEL_PIPELINE_ADMISSION_JOIN=0. New counters:mlxcel_batch_decode_lookahead_admission_joins_totalandmlxcel_batch_decode_lookahead_admission_teardowns_total.Item 3: output thread
batch/output_thread.rs: one thread per worker with a bounded queue (4096 records). A request is routed infinish_prefillwhen it has no stop strings, no generation bound and no token-only row, and the worker does not dispatch speculative decoding (output_thread::routable,route_output).StreamingDecodeStateand client sender move to the thread. Itsresponse_txbecomes a private channel that the routed link drains before each token, so scheduler-sent events keep their order with the routed tokens.SequenceFinishHooks::stop_textpushes the token instead of detokenizing.finalize_completedhands the thread aResultTemplate, which is the non-detokenizer half oftake_generation_result, now factored out insequence.rs. The thread streams the tail, builds the result, records the completion telemetry and sendsDone.catch_unwind. A panic fails only that request: the client gets an error and the request is marked cancelled for the scheduler. A client that never reads cannot stall the scheduler.unroute_output).spawn_model_worker_with_batch_configstarts the thread withenable_output_thread. The kill switch isMLXCEL_OUTPUT_THREAD=0.Item 4: audit of synchronous reads
step_rows.rsperforms no device reads: the row hooks read host state only.speculative_finalize.rsperforms none directly: speculative rounds verify on the host inside the drivers.docs/CONTINUOUS_BATCHING.md.Docs
docs/environment-variables.md: rows for both new variables.docs/CONTINUOUS_BATCHING.md: three paragraphs (the join, the output thread, the audit table), and the outdated "no pending admission or chunked prefill" condition is corrected.Tests (GB10, test-fast profile,
--test-threads=1undergpu-lock)scheduler_decode_graphs_tests::pipeline, all pass on Llama 3.2 1B 4bit and on Gemma 4 12B 4bit. New tests in that module:an_admission_during_the_pipeline_joins_at_the_step_after_next, with no budget and with--max-num-batched-tokens256. Both late rows join, no admission teardown, the KV invariant holds, slabs do not move, and tokens match the teardown arm row for row.a_natural_end_of_sequence_with_the_next_step_in_flight_matches_the_synchronous_pathfour_concurrent_multibyte_requests_stream_the_same_on_the_output_thread. It uses the Llama 3 chat format and skips on other tokenizers.cargo fmt --all --check: clean.--features cuda --lib --tests -D warnings): clean.cargo check -p mlxcel --lib --testsat default features: clean.make verify-*): all pass.Greedy parity (server)
mlxcel-server, Llama 3.2 1B 4bit,--parallel 8, 8 chat prompts x 64 tokens, temperature 0, streamed. Two traffic shapes: a burst and staggered arrivals (150 ms apart). Each run was done with no budget and with--max-num-batched-tokens 256.ac5716b8fReview
MLXCEL_MIXED_STEParm now keeps the joined lookahead through its decode and callsprepare_admissionbefore the chunk, as the budgeted step does. Before the fix it primed a graph step that the chunk then ran under, and the step was thrown away on the next tick.Doneis sent, so a panic while finishing still sends the client an error.output_workerfield's required drop order is now documented. LOW findings left as they are:DETACH_TIMEOUT) if the output thread is that far behind.record_admission_teardownalso counts eager teardowns.admission_join_appliesis quadratic in batch size, on mismatched ticks only.Deferred
Notes