Repository navigation
Conversation
…parallel_embedding`) Signed-off-by: pengdurice <pengduhit@gmail.com>
|
Thanks for picking this up. This fills the untied half of the tied/untied unification in RFC #8173 (#8582 handled tied + vocab-parallel head), so it fits well. A few points from tracing the paths on master:
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: pengdurice <pengduhit@gmail.com>
- Leave an embedding that AutoTP already sharded with its tied head (row- or vocabulary-parallel) as it is. - Raise for an embedding a partition_config spec already partitions, and for the DeepCompile autotp pass. - Gather a zero.Init weight before sharding, split the vocabulary evenly instead of by key-value heads, and replace every registration of the embedding module; the sharding moves from layers.py into AutoTP. - Tests: TestVocabParallelEmbedding now has 16 tests (HF plan, partition_config and heuristic paths, ZeRO 0-3, zero.Init, tied heads, checkpoints, rejected combinations). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: pengdurice <pengduhit@gmail.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ed43cd4db8
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| sharded = autotp._replace_untied_input_embedding() | ||
| if sharded is not None: | ||
| log_dist(f"AutoTP: vocab_parallel_embedding shards '{sharded}' along the vocabulary", ranks=[0]) | ||
| if autotp is not None: | ||
| self._autotp_frozen_replicated_params = autotp.register_replicated_grad_hooks(model) |
There was a problem hiding this comment.
Skip replicated-gradient hooks for vocabulary shards
When vocab_parallel_embedding is combined with a matching SKIP/grad_allreduce: true partition spec (including an HF replicated_with_grad_allreduce entry), _replace_untied_input_embedding() shards the weight here and the immediately following register_replicated_grad_hooks() still installs an all-reduce because it re-matches the old spec. Equal-sized ranks then sum gradients for different vocabulary rows (and uneven ranks can enter a mismatched collective), silently corrupting every embedding update; exclude tensor-parallel parameters from that hook or reject/clear grad_allreduce when applying this override.
Useful? React with 👍 / 👎.
| # The vocabulary is not made of attention heads, so it is split evenly like the vocabulary-parallel | ||
| # output head. A split by key-value head count leaves ranks beyond that count with no rows. | ||
| vocab_meta = dataclasses.replace(self.tp_meta, num_kv_heads=None) | ||
| sharded = VocabParallelEmbedding(embedding, self.mp_group, name=name, tp_meta=vocab_meta) |
There was a problem hiding this comment.
Reject zero-row embedding shards before construction
When autotp_size exceeds the number of vocabulary/grain chunks (for example vocab_size=4, tp=8, or a large tp_grain_size), get_shard_size_list() gives some ranks zero rows. This direct constructor omits the nonempty-shard validation used by _validate_vocab_parallel_layer(); on an empty rank VocabParallelEmbedding.forward() still indexes row 0 after masking, so that rank raises before the all-reduce while peers block. Validate min(partition_sizes) > 0 before creating the layer.
AGENTS.md reference: AGENTS.md:L19-L19
Useful? React with 👍 / 👎.
| # The vocabulary is not made of attention heads, so it is split evenly like the vocabulary-parallel | ||
| # output head. A split by key-value head count leaves ranks beyond that count with no rows. | ||
| vocab_meta = dataclasses.replace(self.tp_meta, num_kv_heads=None) | ||
| sharded = VocabParallelEmbedding(embedding, self.mp_group, name=name, tp_meta=vocab_meta) |
There was a problem hiding this comment.
Preserve vocabulary sizes per tensor in universal checkpoints
When an untied input vocabulary is smaller than, and registered before, a separately dim-0-sharded output vocabulary (for example an encoder-decoder model with distinct source/target vocabularies), this creates a second independent vocabulary tensor, but collect_autotp_universal_checkpoint_info() stores only the first tensor's size in the model-wide ORIGINAL_VOCAB_SIZE (deepspeed/module_inject/layers.py:778-779). merge_tp_slices() then slices every vocabulary pattern to that value (deepspeed/checkpoint/ds_to_universal.py:496-499), so universal conversion truncates the larger head and produces an unloadable or incorrect checkpoint; retain a size per pattern, or avoid the global vocabulary classification for affine-mapped tensors.
AGENTS.md reference: AGENTS.md:L40-L40
Useful? React with 👍 / 👎.
- Skip the replicated-gradient all-reduce for parameters AutoTP has sharded, so a skip spec with grad_allreduce that names the embedding (or a vocabulary-parallel head) does not sum the gradients of different rows. - Reject an embedding split that leaves a rank with no rows, as the vocabulary-parallel head does. - ds_to_universal: do not cut a vocabulary tensor that an affine map rebuilds to the model-wide original_vocab_size; an untied embedding and an output head may have different vocabularies. - Tests: a skip spec with grad_allreduce, an empty shard, and two vocabulary tensors of different sizes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: pengdurice <pengduhit@gmail.com>
AutoTP: shard an untied input embedding along the vocabulary (
vocab_parallel_embedding)Part of: #8672 (Q4 2026 roadmap), "Enable training of large scale models".
Summary
vocab_parallel_lm_head(#8309, #8582) shards an input embedding only when it is tied to the output layer. An untiedinput embedding stays replicated on every tensor-parallel rank: on Qwen2.5-72B at TP 8 it is 1.25B of each GPU's
10.18B parameters (12%), with its gradient, master copy and Adam states, and its gradient is the last one produced in
backward. This PR adds
tensor_parallel.vocab_parallel_embedding(defaultfalse): AutoTP replaces the model'suntied
nn.Embeddingby the existingVocabParallelEmbedding(1/tp of the rows per rank, masked lookup, sum overthe TP group).
The option works on all three AutoTP partitioning paths (HuggingFace
tp_plan,partition_configorpreset_model, and the heuristic path), at ZeRO 0 to 3, and with models built underdeepspeed.zero.Init. Thevocabulary is split evenly over the ranks, like the vocabulary-parallel head.
A tied embedding is sharded with its output head instead:
vocab_parallel_embedding: truevocab_parallel_lm_head)partition_configROW spec, #8538, #8680)NotImplementedError, rather than untie the embedding from its headThe option raises a
ValueErrorwhen apartition_configspec has already partitioned the embedding, and when theDeepCompile
autotppass is enabled (that pass rewrites only linear layers).Results
Qwen2.5-72B, 4 nodes x 8 H200 (EFA), AutoTP 8 x ZeRO-1 over 4, micro-batch 2, 64 x 8,192 tokens per step, bf16 with
fp32 master weights and Adam states, no recompute, Liger kernels for RMSNorm, rotary embedding and SwiGLU (applied by
our benchmark script); the two runs in one allocation, with this code in our study tree (together with the AutoTP
sequence-sharding changes, which are separate PRs):
wall_clock_breakdown)These runs used the first revision of this PR. The review fixes do not change this configuration: with 8 key-value
heads at TP 8, the old and the new split both give 19,008 rows per rank, and the run used ZeRO-1 without
zero.Init.Duplicate check
Searched issues and pull requests on 2026-11-18 for
vocab_parallel_embedding, "untied embedding AutoTP",VocabParallelEmbeddingandembedding_rowwise. #8309 and #8582 shard a tied embedding with a vocabulary-parallelhead, #8538 and #8680 a tied embedding with a row-parallel head, #8294 converts the HuggingFace
embedding_rowwiseentry, and #8173 (closed RFC) discusses sharding output layers. None shards an untied input embedding.
The change
deepspeed/module_inject/auto_tp.py(+46)AutoTP._replace_untied_input_embedding(): replaces every registration ofmodel.get_input_embeddings()by aVocabParallelEmbedding, split evenly over the ranks. It gathers azero.Initweight with_gather_zero3_params, which records the replacement for the optimizer remap. It returnsNonewhen AutoTP has already replaced the embedding with a tensor-parallel layer (tied to a sharded head). It raises when the embedding weight is shared with another module, or was already partitioned by a matching specdeepspeed/runtime/engine.py(+25, -1)finalize_autotp, before the replicated-gradient hooks are registered, when the option is set; the heuristic path builds anAutoTPfor it; rejects the DeepCompileautotppass before any module is replaceddeepspeed/runtime/tensor_parallel/config.py(+8)docs/_pages/config-json.md,docs/_tutorials/autotp-training.mdtests/unit/v1/autotp/test_autotp_training.py(+213)TestVocabParallelEmbedding(16 tests)Changes after the first review
HiddenParallelEmbedding) is left as it is,instead of raising.
partition_configspec raises aValueError, instead of being splita second time along the vocabulary.
autotppass raises aValueErrorat initialize, instead of failing later inengine.compile().deepspeed.zero.Initwork; before, the split of the empty ZeRO-3 placeholder raised.partition_configpaths split it by key-valuehead count: 44 and 22 rows for 3 key-value heads at TP 2, and 0 rows on ranks 8 to 15 for 8 key-value heads at TP 16.
layers.pyto anAutoTPmethod, so that it uses the model'stp_meta(includingtp_grain_size) and the ZeRO-3 gather on every path.Tests
TestVocabParallelEmbedding(2 ranks, AutoTP 2, a 2-layer Llama with 3 key-value heads and a 66-token vocabulary,built from a config):
test_matches_single_device_training(8 cases: HFtp_planat ZeRO 0, 1, 2, 3 and ZeRO-3 withzero.Init;HF
tp_planwithvocab_parallel_lm_head: true;preset_model: llama; the heuristic path): the input embeddingbecomes a
VocabParallelEmbeddingwith 33 of the 66 rows. With tokens from both halves of the vocabulary, the lossesof 2 SGD steps match the unsharded model, and the gathered embedding weight matches the unsharded model's after
them.
test_tied_embedding_keeps_the_shard_of_its_output_head(vocabulary-parallel and row-parallel head): theembedding keeps sharing the head's weight, and training matches the unsharded model.
test_replaces_every_registration_of_the_embedding: a second name for the embedding module gets the sharded module.test_universal_checkpoint_rebuilds_the_embedding: the universal checkpoint metadata rebuilds the full embeddingfrom the shards.
test_save_load_round_trip_preserves_loss(ZeRO-1): a checkpoint restores the loss in a model built with anotherseed.
test_rejects_conflicting_request(3 cases): a tied embedding with a replicated head, apartition_configspec thatmatches the embedding, and the DeepCompile
autotppass.Without the fixes (the first revision merged with master), 11 of the 16 tests fail: the 7
test_matches_single_device_trainingcases on the HF plan and
partition_configpaths (44 and 22 rows; withzero.Initthe split raises), therow-parallel tied case, the alias case, and the spec and DeepCompile rejections.
The changed code runs inside
deepspeed.initializewith AutoTP, so every test file that initializes an engine withAutoTP was run. All runs are on this branch with master 4a9efc1 merged. GPU: 1 node of 8 NVIDIA H200 (driver
595.112.07), pytest 8.3.5, transformers 5.15.1, torch 2.8.0+cu126 unless noted. CPU: the CPU accelerator with the gloo
backend and
LOCAL_SIZE=4, torch 2.8.0+cu126.unit/v1/autotp/test_autotp_training.py(includes the 16TestVocabParallelEmbeddingtests)-m sequentialTestVocabParallelEmbeddingon the first revision merged with master (control)unit/v1/autotp/test_tp_plan_e2e.py,test_tp_plan_real_models.py,test_autotp_custom_patterns.py,test_autotp_multiple_models.py,unit/module_inject/test_tp_partition_config_path.pyunit/compile/test_tp_compile.py; the same with-m sequentialunit/checkpoint/test_autotp_uc_checkpoint.py,test_zero_affine.pyunit/v1/ops/muon/test_per_head_muon_accelerator.py,unit/v1/sequence_parallelism/test_liger_vocab_parallel_cross_entropy.py,unit/v1/test_hybrid_engine_rollout_v1.pyliger_kernel, which is not installed here or in CI; 1 needsDEEPSPEED_PRETRAINED_ROLLOUT_MODEL)unit/v1/moe/test_autoep_unit.py,test_autoep_autotp_checkpoint.py,test_autoep_autotp_deepcompile_reject.py,test_autoep_autotp_folding_config.py,test_autoep_autotp_runtime.py,test_autoep_checkpoint.pyDEEPSPEED_RUN_H100_TESTS=1)unit/v1/moe/test_autoep_autotp_grad_parity.pywithDEEPSPEED_RUN_H100_TESTS=1unit/v1/moe/test_autoep_autotp_sharded_attention.py; the same with-m sequentialunit/v1/moe/test_autoep_expert_activation.pytorch._grouped_mmcan stall with an empty expert, also on master)test_autotp_training.py, default set;-m sequentialtest_tp_plan_e2e.py,test_tp_plan_real_models.py,test_autotp_custom_patterns.py,test_autotp_multiple_models.py,test_tp_partition_config_path.py,test_autotp_uc_checkpoint.pytest_zero_affine.pyNo test failed outside the control row.
pre-commit run --filespasses on every changed file.Compatibility
tp_planpath)deepspeed.zero.Initpartition_config(preset_model: llama) and heuristic paths, ZeRO-1vocab_parallel_lm_head: trueds_to_universal), HF export withsave_16bit_modelautotppassValueErrorget_input_embeddings()is not annn.EmbeddingValueErrorAI assistance
This change, its tests and this description were prepared with AI assistance (Claude).