fix(rollout): gather vocabulary-parallel logits and share the random … #9162
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #9162"
general
weight: 0.35
delock
Fixes #9158. Implements R-1 of RFC #8790.
Summary
Under AutoTP,
HybridEngineRolloutchose 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 aDeepSpeedStaticCache. This PR fixes all three. The API does not change.Changes
generate()runs inside a context manager,_full_vocabulary_logits(module), that setsgather_output=Trueon every head markedis_vocab_parallel_lm_headand 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 passeslogits_to_keep=1, ornum_logits_to_keep=1on Transformers 4.45–4.48, which name the argument that way, andgenerate()does the same on these versions.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'sautotp_size(); for a wrapper that does not provide it, the size of DeepSpeed's tensor-parallel group is used, with a one-time warning.is_transformers_cachealso acceptsDeepSpeedStaticCache, which follows the cache interface without subclassing it and is a per-rank object. It looks the class up insys.modulesinstead of importing it, so AutoTP's input check does not loaddeepspeed/utils/static_cache.py, whose annotations need Python 3.10.requirements-inf.txtnow requirestransformers>=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_classdecode with Hugging Facegenerate(), 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. Withlogits_to_keep=1it gathers[batch, vocab]per step, as a hook would. R-2 (selection on the shards) still fits later: a vocabulary-parallel selector would leavegather_outputoff.Tests
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.TestAutoTPVocabularyParallelHeadRollouton a tied-embedding tiny Llama atautotp_size=2: greedy records matchgenerate()on an unsharded copy and agree across ranks on static, static graph, continuous and continuous graph, with the rollout as the first forward afterinitialize(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
TestAutoTPVocabularyParallelHeadRollouton 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 groupon 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.