fix(rollout): gather vocabulary-parallel logits and share the random … (#9162)
Fixes #9158. Implements R-1 of RFC #8790.
## Summary
Under AutoTP, `HybridEngineRollout` chose tokens from each rank's
vocabulary shard when the model ties its output head, could sample
different tokens on the ranks of a tensor-parallel group, and failed
AutoTP's input check when its first forward passed a
`DeepSpeedStaticCache`. This PR fixes all three. The API does not
change.
## Changes
- **Full-vocabulary logits.** `generate()` runs inside a context
manager, `_full_vocabulary_logits(module)`, that sets
`gather_output=True` on every head marked `is_vocab_parallel_lm_head`
and restores the previous value on exit, also on error. Selection then
sees `[batch, vocab]` logits on every rank, and training keeps
rank-local logits for the vocabulary-parallel loss. Every path keeps
only the last position's logits, so the gather is one position per row:
about 10 MB at 32 rows and a 151k vocabulary in bf16. Prefill passes
`logits_to_keep=1`, or `num_logits_to_keep=1` on Transformers 4.45–4.48,
which name the argument that way, and `generate()` does the same on
these versions.
- **Shared random state.** At the start of `generate()`, with AutoTP,
the group's source rank broadcasts its accelerator random state to the
group. The ranks then run the same forwards and draw the same samples
for the rest of the call. The AutoTP size comes from the engine's
`autotp_size()`; for a wrapper that does not provide it, the size of
DeepSpeed's tensor-parallel group is used, with a one-time warning.
- **Input check.** `is_transformers_cache` also accepts
`DeepSpeedStaticCache`, which follows the cache interface without
subclassing it and is a per-rank object. It looks the class up in
`sys.modules` instead of importing it, so AutoTP's input check does not
load `deepspeed/utils/static_cache.py`, whose annotations need Python
3.10.
- **Transformers floor.** `requirements-inf.txt` now requires
`transformers>=4.45`. Versions before 4.45 have neither argument, so the
prefill would compute logits for every prompt position and the gather
would hold `[batch, prompt_len, vocab]` on every rank. 4.45 keeps Python
3.8, which 4.47 and later drop.
Why not a hook in `_token_selector`, as RFC #8790 sketches: the static
path and the continuous prefill for models without
`_supports_cache_class` decode with Hugging Face `generate()`, which
bypasses the selector; the repetition penalty and the recorded log-probs
need the full vocabulary too; and a gather inside the head is captured
in the CUDA graph. With `logits_to_keep=1` it gathers `[batch, vocab]`
per step, as a hook would. R-2 (selection on the shards) still fits
later: a vocabulary-parallel selector would leave `gather_output` off.
## Tests
- CPU:
`test_full_vocabulary_logits_gathers_only_vocabulary_parallel_heads_and_restores_them`
(also after an exception),
`test_generate_decodes_with_full_vocabulary_logits`,
`test_full_vocabulary_logits_accepts_a_head_without_gather_output`,
`test_generate_shares_the_random_state_across_the_autotp_group` (with TP
2 the rank sets the source rank's broadcast state before it generates;
TP 1 does not broadcast),
`test_generate_shares_the_random_state_when_the_engine_is_wrapped`,
`test_last_logits_kwargs_follow_the_transformers_argument_name`,
`test_tp_input_check_treats_deepspeed_static_cache_as_a_cache`.
- GPU, 2 ranks, `TestAutoTPVocabularyParallelHeadRollout` on a
tied-embedding tiny Llama at `autotp_size=2`: greedy records match
`generate()` on an unsharded copy and agree across ranks on static,
static graph, continuous and continuous graph, with the rollout as the
first forward after `initialize` (so the graph paths also meet the input
check); with ranks seeded differently, sampled records agree across
ranks.
## Validation
**Hardware and software:** 2× NVIDIA A100-SXM4-40GB (driver 595.71.05),
PyTorch 2.14.1+cu130, CUDA 13.0, Python 3.12.15, Transformers 5.19.0.
| Command | Result |
|---|---|
| `pytest tests/unit/v1/test_hybrid_engine_rollout_v1.py` (2 GPUs) | 51
passed, 1 skipped (opt-in pretrained test) |
| `pytest tests/unit/runtime/rollout` | 193 passed, 3 skipped |
| New `TestAutoTPVocabularyParallelHeadRollout` on master's code | 5 of
5 fail, as expected |
| Existing AutoTP tests (`test_tp_plan_real_models`, `test_tp_plan_e2e`,
`test_autotp_training`, `test_vocab_parallel_ops`) | 149 passed, 52
skipped |
Reproduction from the issue, fp32, TP2 against one GPU, on Qwen3-0.6B
and Qwen3-4B-Instruct-2507 (both tie their embeddings, so the head is
`VocabParallelLinear`):
| Check | Master | This PR |
|---|---|---|
| Greedy, static / static graph / continuous / continuous graph | Rank 1
differs from one GPU on every path | Both ranks match one GPU on every
path |
| Sampling, ranks seeded `1234 + rank` | Ranks differ | Ranks agree |
| Static graph capture as the first forward | `Data inconsistency within
the TP group` on both ranks | Passes |
Throughput and peak memory, Qwen3-4B-Instruct-2507 bf16, TP2, 32 rows,
128 new tokens, graph capture, three runs each, this PR's gathered head
against `vocab_parallel_lm_head: false`:
| Path | Gathered head (this PR) | Replicated head |
|---|---|---|
| Static graph | 1,689 tok/s, 4.51 GiB | 1,702 tok/s, 4.87 GiB |
| Continuous graph | 1,479 tok/s, 4.22 GiB | 1,486 tok/s, 4.58 GiB |
The gather costs 0.5–0.8% of throughput and the vocabulary-parallel head
saves 0.36 GiB of peak memory per GPU.
**Compatibility matrix:** AutoTP 2 with ZeRO 0, fp32 (correctness) and
bf16 (throughput), on all four decode paths. AutoTP with ZeRO 1–3, fp16,
and hybrid engine inference tensor parallelism are not covered.
## AI assistance
This PR was written with AI assistance (Claude Code). The submitter
reviewed every changed line and ran the tests above.
---------
Signed-off-by: Puyuan Yao <williamyao034@gmail.com>
Co-authored-by: Ma, Guokai <guokai.ma@gmail.com>