Repository navigation
[rl][easy] vLLM priority scheduling: serve rollout groups in the order they started - #5257
Conversation
791ba1e to
d703ee3
Compare
d703ee3 to
8099764
Compare
| request.min_policy_version = self._group_min_policy_versions.setdefault( | ||
| request.group_id, self.policy_version | ||
| ) | ||
| request.arrival_time = time.time() |
There was a problem hiding this comment.
question: why timing is set here in the engine loop's _decide_next_action, instead of when entering the generate method where the request is constructed?
I don't know if it matters in practice, but feels more natural there.
There was a problem hiding this comment.
reply from claude: Good point, moved it. generate now builds the request with arrival_time=time.time(), so it's a plain constructor field (no field(init=False), no stamp in _decide_next_action).
- Still rank 0's clock:
generateonly runs on rank 0, and the value is broadcast insideEngineRequest, so TP ranks still agree. - Same tie order: the engine-loop queue is FIFO and drained in order.
- One metric change: vLLM measures TTFT from
arrival_time, sotime_to_first_token_msnow also counts the wait for the next engine-loop decision (up to one step burst, longer behind a weight pull).queue_time_msstill measures only vLLM's queue. The PR description's NOTE says this now.
| engine_request=EngineRequest( | ||
| request_id=request_id, | ||
| prompt_token_ids=prompt_token_ids, | ||
| sampling=sampling, | ||
| group_id=group_id, | ||
| routing_session_id=routing_session_id, | ||
| ), |
There was a problem hiding this comment.
I mean setting the arrival_time here.
There was a problem hiding this comment.
reply from claude: Done, it's set here now (arrival_time=time.time() in the EngineRequest(...) call). Details in the other thread.
|
@claude review this PR and my review |
|
Claude finished @tianyu-l's task in 1m 45s —— View job Review of #5257 and @tianyu-l's question
Your question: set
|
The generator passes each request's min_policy_version as its vLLM priority and switches the engine to scheduling_policy="priority". Queued requests from older policies are admitted first, and running ones are preempted last when the KV cache is full. Rank 0 stamps each request's arrival_time and broadcasts it, since vLLM breaks priority ties by arrival time and every TP rank must schedule the same batch.
Each request's vLLM priority is now its group's start order instead of its min_policy_version. Training groups use their id, a dataloader index that grows as groups start, so every turn of an older group goes ahead of a newer group's turns, also within one policy version. A validation group (negative id) takes the newest training group id the generator had seen at its first admission, kept until release_groups, so it runs after the training groups that started before it. min_policy_version still salts the prefix cache and measures policy age.
Every request already carries its group_id, so the engine loop passes it as the vLLM priority. Validation ids are negative, so validation now goes before training. On main, validation runs only before and after training, so no training request waits behind it. Drops EngineRequest.priority, the validation priority map and its release.
generate (rank 0 only) now builds each EngineRequest with arrival_time=time.time(), so it is a plain constructor field instead of a field(init=False) stamped in _decide_next_action. Tie order and TP agreement are unchanged; vLLM's TTFT now also counts the wait in the engine-loop queue. Also rewords the scheduling_policy comment and the generate docstring, and renames the admission test.
Validation group ids count down (-1, -2, ...) and restart every validation run, so passing them raw would serve the newest validation request first. The engine loop now passes max(group_id, -1): all validation runs before training, in arrival order among itself. Training priorities are unchanged.
8099764 to
8819125
Compare
|
reply from claude: Thanks. On the minor points:
New since the approval: validation ids count down and restart every run, so all validation now shares priority -1 ( |
…gine; add GenerationEngine (#5317) Stacked on #5316. ## Why `torchtitan/rl/generator/generator.py` (1860 lines) mixed three concerns in one class, `VLLMGenerator`: - calls into vLLM (`EngineArgs`, `render_cmpl` + `add_request`, `step`, `SamplingParams`, `RequestOutput` parsing, prefix-cache reset, teardown); - request dispatch across data-parallel replicas and fan-in of results to rank 0; - the engine thread and its SPMD loop (queue, rank 0's decision, broadcast, step bursts, load coalescing). That makes each part hard to read in isolation and ties the generator to vLLM. This PR splits the file along those lines and puts the inference engine behind an interface, so a second engine (e.g. SGLang) can be added as another `GenerationEngine` without touching the generator, the engine loop or the dispatcher. ## What changed **Generator split** | File | Contents | | --- | --- | | `generator/generator.py` | `Generator` (was `VLLMGenerator`), the service: config, setup and endpoints | | `generator/engine_loop.py` | `EngineLoopThread` (the thread and event loop the engine lives on) and `EngineLoop` (queue, decide -> broadcast -> apply, load coalescing, policy version, per-group cache-salt pins) | | `generator/dispatcher.py` | `RequestDispatcher` and the rank-0 completion metrics | | `generator/types.py` | `SamplingConfig`, `EngineRequest`, `FinishedRequest`, `RequestMetricsInputs` | | `generator/engine.py` | `GenerationEngine`, the engine interface | | `generator/vllm/engine.py` | `VLLMEngine`, holding every vLLM call | | `generator/vllm/config.py` | `VLLMCudaGraphConfig` | The dispatcher now receives engine-agnostic `FinishedRequest`s instead of vLLM `RequestOutput`s, and rank 0 builds each `Completion`. Peer replicas used to send a `Completion` with a placeholder `min_policy_version` that rank 0 overwrote; they now send their `FinishedRequest`s plus the policy version installed when those finished. **Engine interface** - `VLLMGenerator` -> `Generator`, `VLLMGeneratorActor` -> `GeneratorActor`. - `GenerationEngine` (`generator/engine.py`) is the interface the engine loop and the generator call: `add_requests`, `step`, `has_unfinished_requests`, `load_prefetched_model_state_dict`, `shutdown`, `prefetched_model_state_dict`, and the data-parallel layout. Its docstring lists the build arguments every engine accepts. - `VLLMEngine` implements it. vLLM setup, `EngineArgs` and the stat logger are private `VLLMEngine` methods, and the engine is built from its config on the engine loop thread. - Only `generator/vllm/` imports vLLM; `generator.py` imports the default engine config lazily. **Monarch README** `actors/monarch/README.md` lists the Monarch adapters and the Monarch use that still lives outside them (provisioning in `train.py`, spawning in the controller and rollouter, `InterGeneratorRouter` as an `Actor`, and the RDMA microbatch), with the planned path to confine it to `actors/monarch/`. ## User-facing changes - `VLLMGenerator` -> `Generator`, `VLLMGeneratorActor` -> `GeneratorActor`. - vLLM-only settings move from `Generator.Config` to `VLLMEngine.Config`, selected by the new `Generator.Config.engine` (default `VLLMEngine.Config()`): | Before | After | | --- | --- | | `generator.cuda_graph` | `generator.engine.cuda_graph` | | `generator.extra_vllm_engine_args` | `generator.engine.extra_engine_args` | | `generator.vllm_stat_logger` | `generator.engine.stat_logger` | | `generator.session_kv_holder` | `generator.engine.session_kv_holder` | Engine-agnostic settings stay on `Generator.Config` and are passed to the engine's build: `model_dtype`, `gpu_memory_limit`, `max_num_batched_tokens`, `checkpointer`, `dist_moe_runtime`, `debug`. Every recipe and test is updated. Only Python configs change: the CLI has no per-field overrides, and `--override` targets override functions, not config paths. - `SamplingConfig` moves to `torchtitan.rl.generator.types`, `VLLMCudaGraphConfig` to `torchtitan.rl.generator.vllm.config`, and `add_extra_vllm_engine_args` to `torchtitan.rl.generator.vllm.engine`; every caller is updated. ## Execution order The per-iteration sequence is unchanged: decide, broadcast, stamp versions, admit, step burst, fan-in, load. The differences: - The engine's process-wide setup (`register_to_vllm`, the model runner env var, the batch-invariant logprob patch) now runs on the engine loop thread inside the engine build, after `set_batch_invariance` and the determinism flags. vLLM reads neither at import time. - `RequestOutput` -> `FinishedRequest` conversion happens in `VLLMEngine.step` on every rank, not only TP rank 0 (a small list copy on the other ranks; the n=1 check now fires on every rank instead of only rank 0). - A model state dict load sets the policy version after the optional prefix-cache reset rather than before; both run synchronously on the engine loop thread, and nothing reads the version in between. - `generate`'s `stop_token_ids` assert runs before the closed/not-started guard (error path only). - Session KV release (#5220) runs through the engine loop as a `SessionReleaseMessage`, applied on every rank in the next decision; vLLM priority scheduling (#5257) passes `group_id` as the priority and rank 0's `arrival_time` to every rank, as on main. Because `session_kv_holder` is vLLM-only, its check against `reset_kv_cache_on_model_state_dict_load` now fails when the engine is built rather than when `Generator.Config` is constructed. ## Testing - RL CPU unit tests: 492 passed, 8 skipped (rebased on `dc9ea005e`). `test_engine_loop.py` drives `EngineLoop` directly with a fake engine; `VLLMEngine`'s admission, salting, rejection, step, shutdown and KV-reset tests use a fake `LLMEngine`; the engine kwargs test builds `VLLMEngine` through `Config.build(...)` with vLLM mocked; the `generate` endpoint and the DP fan-in's min/max versions have their own tests. - `pre-commit run --all-files`: clean for the changed files (see #5316 for the unrelated local `kda.py` pyrefly errors). - Every RL recipe builds, except a hardware-gated MXFP8 recipe. - GPU A/B against main (2x GB300, Qwen3 debug model with random init and a deterministic varying reward): - on-policy, batch-invariant: all 65 non-timing metrics are bitwise identical to main, and identical run to run; - async (`target_offpolicy_steps=3`): within main's own run-to-run variation. - After the rebase onto `dc9ea005e`, CI's multi-GPU RL suites pass locally under torchrun: bitwise parity Varlen 3/3, Flex 3/3, Qwen3.5 4/4, and the 4-GPU uneven decode test 1/1.
TLDR: vLLM now serves requests in the order their rollout groups started, instead of first-come-first-served: every turn of an older group is admitted first from the queue and preempted last. On main, a multi-turn rollout's next turn waits behind groups that started after it. Not measured yet; our multi-turn runs rarely queue requests, so for them this is headroom.
Problem
scheduling_policy="fcfs", so vLLM admits queued requests in arrival order.Solution
scheduling_policy="priority". Lower runs first.group_id. Every request already carries it. Training group ids are dataloader indices, so they grow as groups start. Every rollout and turn of a group shares it.max(group_id, -1)): it goes before training, in arrival order.arrival_time. Rank 0 stamps it ingenerateand broadcasts it onEngineRequest, so a TP rank whose clock jumps back cannot schedule a different batch.min_policy_versionstill salts the prefix cache and measures policy age. No new config field.You may ask:
priorityandarrival_time, both set by rank 0, so the ranks still agree (ties: see NOTE).max(group_id, -1). On main, validation runs only before and after training, so no training request waits behind it. Once validation runs beside training, it would delay training's next batch, and that change can pick validation's priority.NOTE:
time_to_first_token_ms(and vLLM's e2e latency) now start whengenerateis called. So they also count the wait for the next engine-loop decision (up to one burst of steps, longer behind a weight pull), the decision broadcast and prompt rendering. On a DP replica on another host, they also include the clock skew between hosts.queue_time_msstill measures only vLLM's queue.-..., which our ids never are.Test plan
pytest tests/rl/unit_tests/cpu/test_engine_loop.py tests/rl/unit_tests/cpu/test_generator.py pytest tests/rl/unit_tests/cpu # 459 passed, 5 skipped (main: 458 passed); both have the same 3 failures: 2 in test_verifiers_terminal_bench.py, 1 in test_rollout_worker.pyNew:
test_admission_passes_group_id_as_priority_and_arrival_time. It fails withpriority = min_policy_versionor without the -1 clamp.