diff --git a/core/engine.cpp b/core/engine.cpp index 2bfaa966..ee0478cd 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -409,6 +409,12 @@ int Engine::step_loop() { * pass issued ahead would overwrite them. */ pipelining_ = !cfg_.kld() && !embedding_ && (serial_drafting() || block_drafting() || cfg_.n_spec <= 0); + /* RADIANCE_IDS_EARLY=0 waits for the drafter's passes before reading the ids, as before, for + * an A/B. */ + early_ids_ = !cfg_.kld() && !embedding_ && [] { + const char* v = std::getenv("RADIANCE_IDS_EARLY"); + return !(v && *v == '0'); + }(); const bool t_on = (int)log_level() >= (int)Log::Debug; /* The next step's forward pass when already issued behind the previous step, before its * readback (Scheduler::step_next). */ @@ -759,6 +765,17 @@ int Engine::step_loop() { if (t_on) phase_mark(PH_COMMIT); if (st < 0) { RAD_ERR("commit: %s", rad_strerror(st)); return fail_loop(st); } + /* With the ids read ahead of the drafter's passes the commit has streamed the step's + * tokens while the card ran them. Their end now, before anything reads what they wrote + * (the slots below) or the next step reuses what the commit released: the commit issued + * no work of its own, so everything it freed is in use only by passes this wait covers. */ + if (so.early) { + st = rad_event_sync(step_end_); + if (st < 0) { RAD_ERR("step %d: waiting for its draft passes: %s", b->step, + rad_strerror(st)); return fail_loop(st); } + so.early = false; + } + /* The drafter for the next step already ran on the card; what remains is to check its * acceptance against the host's and publish its proposals. Each driver returns * immediately for a kind that is not its own. */ @@ -799,11 +816,17 @@ int Engine::step_loop() { } /* Issue the copy of the sampled ids into pinned memory behind the sampler, without waiting. - * Issued by the serial drafter before its passes (a head sampling through the sampler's buffer - * overwrites the ids), otherwise just before the wait. */ -int Engine::collect_issue(StepOut& so) { + * Issued by a drafter before its passes (a head sampling through the sampler's buffer overwrites + * the ids), otherwise just before the wait. + * + * With `fence`, from a drafter that queues its passes behind the step, the copy is followed by + * tok_end_, so collect_step can wait for the ids alone and the commit can stream the step's + * tokens while the card runs the passes (StepOut::early). Not at -vv: the logits row below is a + * pageable copy, read in collect_step. */ +int Engine::collect_issue(StepOut& so, bool fence) { so.rb = nullptr; so.lg.clear(); + so.early = false; if (so.n_rows <= 0) return RAD_OK; Rank& r0 = *ranks_[0]; void* src = r0.ctx.buf_ptr(r0.tok_buf); @@ -824,6 +847,11 @@ int Engine::collect_issue(StepOut& so) { so.lg.resize((size_t)nv); rad_memcpy_async(so.lg.data(), lp, nv * 4, r0.ctx.stream()); } + return RAD_OK; + } + if (fence && early_ids_) { + RAD_TRY(rad_event_record(tok_end_, r0.ctx.stream())); + so.early = true; } return RAD_OK; } @@ -922,7 +950,8 @@ int Engine::issue_scored_rows(Rank& r) { int Engine::collect_scored_rows() { if (score_rows_.empty()) return RAD_OK; - /* step_end_ fences rank 0's stream; every other rank's copies are fenced here. */ + /* The wait collect_step made (step_end_, or tok_end_ behind the copies issued ahead of the + * sampler) fences rank 0's stream; every other rank's copies are fenced here. */ for (size_t i = 1; i < ranks_.size(); ++i) { RAD_TRY(rad_dev_set(ranks_[i]->device)); RAD_TRY(rad_stream_sync(ranks_[i]->ctx.stream())); @@ -968,8 +997,10 @@ int Engine::collect_step(const RadBatch* b, StepOut& so) { if (so.n_rows <= 0) return RAD_OK; so.rows.resize((size_t)so.n_rows); - /* Wait on the step-end event, not the stream: a step issued ahead is queued behind it. */ - RAD_TRY(rad_event_sync(step_end_)); + /* Wait on an event, not the stream: a step issued ahead is queued behind it. The ids' own + * event when they were fenced ahead of the drafter's passes (StepOut::early), whose end the + * step loop waits for after the commit; else the step's end. */ + RAD_TRY(rad_event_sync(so.early ? tok_end_ : step_end_)); std::memcpy(so.rows.data(), so.rb, (size_t)so.n_rows * sizeof(int32_t)); so.rb = nullptr; RAD_TRY(collect_scored_rows()); diff --git a/core/engine.h b/core/engine.h index b9a5d88c..488dc5c5 100644 --- a/core/engine.h +++ b/core/engine.h @@ -254,6 +254,11 @@ private: /* [n_rows] each sampled row's logprobs, for the rows of requests that asked for them; * empty on a step with none. */ std::vector lp; + /* The ids' copy has its own event ahead of the drafter's passes (collect_issue's + * `fence`): collect_step waits on it, the commit streams the tokens, and the passes are + * waited for after that (step_end_), before anything reads what they wrote or reuses what + * the commit released. */ + bool early = false; }; /* The text frontend, loaded from the container. The sampler's grammars walk `vocab_view_`, @@ -323,16 +328,22 @@ private: void probe_arena_levels(); int step_loop(); /* Issue the copy of the step's sampled ids to the host, then wait for and interpret them. - * Split so the copy is issued before draft passes overwrite the ids, and the wait comes after - * them. */ - int collect_issue(StepOut& so); + * Split so the copy is issued before draft passes overwrite the ids. With `fence` (a drafter + * queueing its passes behind the step) the copy gets an event of its own, tok_end_, so the + * wait for the ids need not wait for the passes too (StepOut::early). */ + int collect_issue(StepOut& so, bool fence = false); int collect_step(const RadBatch* b, StepOut& so); int row_logprobs(StepOut& so, int32_t row, int32_t token); /* The step's end on the card: after its last draft pass and the host readbacks, before the - * step issued ahead. collect_step waits on this event, not the stream, so the card runs the + * step issued ahead. The engine waits on this event, not the stream, so the card runs the * next step while the host commits this one. */ int mark_step_end(const RadBatch* b, StepOut& so); RadEvent step_end_ = nullptr; + /* The sampled ids' copy, ahead of the drafter's passes (collect_issue's `fence`). */ + RadEvent tok_end_ = nullptr; + /* Whether the ids may be read before the drafter's passes end: not in KL mode, whose scored + * rows are read out of the logits after collection, nor in an embedding program. */ + bool early_ids_ = false; /* The draft head as declared (rad_declare_drafter), zeroed when no speculative window was * asked for. `kind` selects which of the two drivers below runs. `drafter_name_` owns the diff --git a/core/engine_draft.cpp b/core/engine_draft.cpp index 99d8a667..1ad7cadc 100644 --- a/core/engine_draft.cpp +++ b/core/engine_draft.cpp @@ -104,6 +104,7 @@ int Engine::step_state_init() { } RAD_TRY(rad_event_create_as(&step_end_, RAD_EVENT_LOCAL | RAD_EVENT_HOST_WAIT)); + RAD_TRY(rad_event_create_as(&tok_end_, RAD_EVENT_LOCAL | RAD_EVENT_HOST_WAIT)); return RAD_OK; } @@ -123,6 +124,8 @@ void Engine::step_state_fini() { couple_readback_ = nullptr; if (step_end_) rad_event_destroy(step_end_); step_end_ = nullptr; + if (tok_end_) rad_event_destroy(tok_end_); + tok_end_ = nullptr; } /* ------------------------------------------------------------------ the serial driver @@ -179,7 +182,9 @@ int Engine::advance_all(int mode, int round, bool from_proposal, int rows) { /* Issue the step's draft passes behind it, without waiting. After the trunk's ranks join: the * acceptance on every rank, the sampled-id readback (before a round can overwrite them), the * history pass and every round, then the final proposal into the slots and their readback. - * Nothing synchronises here; collect_step has the step's one wait. */ + * Nothing synchronises here. collect_step waits for the ids alone (their copy is fenced ahead of + * the passes), and the step loop for the passes after the commit, before finish_serial_drafter + * reads the slots. */ int Engine::issue_serial_drafter(int step, StepOut& so) { serial_rounds_ = 0; serial_rows_ = 0; @@ -191,7 +196,7 @@ int Engine::issue_serial_drafter(int step, StepOut& so) { } RAD_TRY(advance_all(ADV_ACCEPT, 0, false)); - RAD_TRY(collect_issue(so)); + RAD_TRY(collect_issue(so, /*fence=*/true)); int rounds = drafter_.depth; serial_row_.resize((size_t)cfg_.max_seqs); @@ -465,7 +470,9 @@ bool Engine::block_drafting() const { * The query pass's rows are device rows that the card positions at the committed end (ADV_BLOCK), * and its proposals go into the slots behind the anchor (ADV_BTAKE): exactly the token row the * next verify reads (ADV_TRUNK), so the step issued ahead verifies them with no host round trip. - * collect_step's wait covers both passes and the slots' readback. */ + * collect_step waits for the sampled ids alone, whose copy is fenced ahead of both passes, so the + * commit streams the step's tokens while the card runs them; the step loop waits for the passes + * and the slots' readback (step_end_) before finish_block_drafter reads the slots. */ void Engine::block_shape(int* block, int* steps) { *steps = drafter_.depth; /* The row count is the drafter's (defaulted and checked by declare_drafter). */ @@ -487,7 +494,7 @@ int Engine::issue_block_drafter(int step, StepOut& so) { RAD_TRY(advance_all(ADV_ACCEPT, 0, false)); /* The sampled ids before either pass, as the serial driver reads them. */ - RAD_TRY(collect_issue(so)); + RAD_TRY(collect_issue(so, /*fence=*/true)); /* The short block too (block_shape): its query pass is a shorter batch the card positions * the same way, its proposals the front of each row at the drafter's pitch (ADV_BTAKE), and diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index 7a8e4af5..17ee92da 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -1966,6 +1966,11 @@ void Scheduler::promote_next() { } /* ------------------------------------------------------------------ commit */ +/* With early ids (Engine::collect_step waits on tok_end_, not step_end_), this runs while the card + * may still be running the drafter's passes behind the step. So it issues no device work and + * allocates nothing: what it releases (KV blocks, StepSlot cards, prefix-cache publications) may + * still be read by those passes, and is reused only by work the engine issues after its wait for + * step_end_. Device work or an allocation added here must move behind that wait. */ int Scheduler::commit(const StepResult& res) { std::lock_guard lk(mu_); if (!step_open_) return RAD_E_STATE; @@ -2181,7 +2186,9 @@ int Scheduler::commit(const StepResult& res) { /* Every entry is retired, so anything deferred on this step, or on a drafter snapshot that * block_end() / serial_end() has since cleared, can be released: every pass naming them was - * issued before this step's readback, which waited for the compute stream. */ + * issued before this step's readback. That readback may return while the drafter's passes + * behind it still run (Engine::StepOut::early), and the engine then waits for them before it + * issues anything else, so nothing reuses what is released here before they end. */ sweep_deferred(); return RAD_OK; } diff --git a/docs/GUIDE.md b/docs/GUIDE.md index f3efa699..1b86aa92 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -3491,6 +3491,7 @@ The four slots are what the amdgpu kernel driver leaves to user queues. It keeps | `RADIANCE_PROFILE_EVERY` | with `--profile-ops`, dump the per-op table every N steps (default 32) | | `RADIANCE_AQL_STAMP`, `RADIANCE_AQL_STAMP_OUT` | `N`: every dispatch of one recorded pass in N on card 0 gets a completion signal of its own, which carries its start and end; the barrier bits and chains stay as unstamped, unlike `--profile-ops`, which plays no recorded pass. One CSV line a dispatch (pass, record, queue, kernel, whether it waited for the one before, whether it was chained, start and end in ns) is appended to `RADIANCE_AQL_STAMP_OUT` (default `/tmp/radiance-stamps-.csv`) once the card is done with it; `scripts/stampgaps.py` reads it per kind of pass (idle share, gaps before ordered starts, kernels by time). AQL backend only | | `RADIANCE_LOG_STEPS` | log one line per step: its phase, sequences, and each one's token count and context (ยง4.5) | +| `RADIANCE_IDS_EARLY=0` | read a step's sampled tokens only once the drafter's passes behind it have run. By default they are read, committed and streamed while the card runs those passes. For an A/B | Engine development has its own instruments: `RADIANCE_DEBUG_*` (residual probes, routing and issue dumps), `RADIANCE_OP_ORACLE*` (named ops checked against `libref` on the operands the model issued;