Repository navigation
[AutoTP] Add support for vocab-parallel LM head and related configurations - #8309
Conversation
|
Hi @jinyouzhi when this PR is ready for review just ping me, thanks! |
Co-authored-by: Copilot <[email protected]> Signed-off-by: Jin, Youzhi <[email protected]>
ab4139a to
8cf638a
Compare
Co-authored-by: Copilot <[email protected]> Signed-off-by: Jin, Youzhi <[email protected]>
Co-authored-by: Copilot <[email protected]> Signed-off-by: Jin, Youzhi <[email protected]>
Require an explicit sequence-parallel group when the legacy wrapper gathers losses, and document the distinct backward conventions. Co-authored-by: Copilot <[email protected]> Signed-off-by: Jin, Youzhi <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8f03c27ea0
ℹ️ 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".
Replace the output head in heuristic AutoTP and preserve original tied-weight evidence before embedding traversal mutates parameters. Co-authored-by: Copilot <[email protected]> Signed-off-by: Jin, Youzhi <[email protected]>
Resolve and validate the heuristic output head before transformer partitioning, cache the candidate for replacement, and skip tied-weight scans when the feature is disabled. Co-authored-by: Copilot <[email protected]> Signed-off-by: Jin, Youzhi <[email protected]>
Hi, this PR is ready for review now. Thank you for your patience! @delock During development, I identified and fixed some issues related to SP in a separate PR: #8457. |
|
Hi @jinyouzhi Thanks for the PR — the loss kernel and backward-compat handling for 1. License header (new file) 2. Suggestion (non-blocking): a minimal save → load → forward-loss parity test for the vocab-sharded head would be good to have, since checkpoint paths have no coverage for the new |
New files carry only the SPDX identifier and team line. Co-authored-by: Copilot <[email protected]> Signed-off-by: Jin, Youzhi <[email protected]>
Fail when vocab_parallel_lm_head finds no supported head instead of degrading into an ordinary gathered head with no distributed loss, and warn when the flag supersedes an explicit lm_head spec from partition_config or a HuggingFace tp_plan. Also partition the embedding with a row spec in the tied-head regression so it exercises the captured tie identities; the previous embedding_rowwise plan converts to a skip spec, which left the embedding untouched and the guard unreached. Co-authored-by: Copilot <[email protected]> Signed-off-by: Jin, Youzhi <[email protected]>
Save and reload a vocab-parallel LM head and compare the causal-LM loss, since no existing test exercises checkpoint paths for the VocabParallelLinear layout. The reloaded model starts from a different initialization so an unrestored checkpoint cannot pass. Co-authored-by: Copilot <[email protected]> Signed-off-by: Jin, Youzhi <[email protected]>
|
@delock Thank you for your detailed review! I have updated the PR according to your comments and guidance. Please take another look when you have a chance. |
101 rows over two ranks gives shards of 51 and 50, so the map has to carry the per-rank extents rather than assume an even split, and restoring at TP1 merges two unequal shards into one tensor. The fixture asserts the emitted map records that split before saving, so the test fails if conversion silently falls back to the vocabulary category. Adapted from @jinyouzhi's cross-tp vocab test in deepspeedai#8309, using the column partition already in tree so it carries no dependency on that PR. Signed-off-by: Achyuthan Sivasankar <[email protected]>
101 rows over two ranks gives shards of 51 and 50, so the map has to carry the per-rank extents rather than assume an even split, and restoring at TP1 merges two unequal shards into one tensor. The fixture asserts the emitted map records that split before saving, so the test fails if conversion silently falls back to the vocabulary category. Adapted from @jinyouzhi's cross-tp vocab test in deepspeedai#8309, using the column partition already in tree so it carries no dependency on that PR. Signed-off-by: Achyuthan Sivasankar <[email protected]>
|
LM-head name detection is duplicated in several coupled places — should all reuse The |
|
|
Step 4 of the staging plan in deepspeedai#8252: AutoTP layers now emit an affine map, and the converter reads it on a path a real job takes rather than one a test supplies. Follows deepspeedai#8385 (the IR, the lowering constructors, the converter branch) and deepspeedai#8477 (the scale fix). Scope here is contiguous splits and replicated parameters; the fused QKV and Yuan shared-QK layouts are next and still carry `unsupported_reason`, so they emit no map and are unaffected. ### Where a map is built, and why it is not where you would expect The producer cannot build a map from what `collect_autotp_universal_checkpoint_info` sees. The conversion metadata carries eight fields and per-rank partition sizes are not among them — those were deliberately restore-only. But the layer does have them: `_freeze_partition_sizes` resolves them via `get_shard_size_list` while the layer is built, and they are not recoverable later from a shape alone. So the map is derived at mark time, in `_set_param_uc_meta`, which already receives `partition_sizes`, `sub_param_shard_widths`, `logical_shape` and `replicated`, and has `tp_world_size` in scope. That puts the derivation in one place rather than in each of the seven `_mark_uc_metadata` implementations, and `collect_...` then gathers what the layers produced. A layout that is not describable yet returns `None`, so conversion falls back to the categories. ### The map and the categories have to coexist A checkpoint carrying an affine map also carries the existing pattern lists, so a converter predating the map still reads it — that is the additivity §6.3 promises. The consequence is that the converter which prefers the map never consults those branches, which leaves their patterns looking unused and fails a strict conversion. They are superseded, not unused, so the affine branch now marks them consumed. This is the compatibility question raised as "one boundary for review" in deepspeedai#8385; it turns out both have to be present and the reader has to account for the other. This only surfaced end to end. The producer's output was correct in isolation, and the emitted maps matched a layout already verified by a passing resume — the failure was a strict-mode assert on rank 0 during conversion, which presented as rank 1 blocking on the following barrier. ### Tests `TestAffineMapProducer` builds a real AutoTP engine at TP2 and requires the producer to emit exactly the four maps the resume fixture previously supplied by hand. Those maps are not a guess: a full train → save → convert → resume cycle reproduces uninterrupted training through them, so matching them is evidence rather than self-consistency. `affine_resume_checkpoint` now takes its layout from the producer instead of injecting one, so the four save-convert-resume cases exercise the metadata a real job writes. All four pass — TP2 → TP1 and TP2 → TP2, through both the legacy and affine paths, compared against uninterrupted training on logits, losses, gradients, FP32 weights, both Adam moments and step counters. Validated on CPU/gloo (DS_ACCELERATOR=cpu LOCAL_SIZE=4): 109 passed across the resume matrix, the producer and coverage tests, the vocab cross-TP test, the affine unit suite and tests/unit/runtime/tensor_parallel/. The five remaining failures in that file are FusedAdam JIT-compile errors on this machine and are identical on upstream master. ### Review findings Three things came out of review, all fixed here: - affine_map was added to every conversion metadata dict, changing the schema even for layouts with no map. Now emitted only where there is one, so an existing layer publishes exactly what it did before. - The producer test checked that the expected maps were present but not that nothing extra was. It now asserts the pattern set exactly. - Parameters the TP machinery leaves untouched — embeddings, norm weights — were classified TP_REPLICATED with no map, so conversion still fell back to the category. TestAffineMapCoverage now asserts the general invariant: every pattern in any category must carry a map, with only AutoTP-unsupported layouts exempt. It fails without the fix. - TestUnevenVocabCrossTpResume covers a 101-row vocabulary head saved at TP2 and restored at TP1 and TP2. The 51/50 split is the point — restoring at TP1 merges two unequal shards, where an even-split assumption would surface. The fixture asserts the emitted map records that split before saving. Adapted from @jinyouzhi's cross-tp test in deepspeedai#8309, using the column partition already in tree so it carries no dependency on that PR. affine_ir_spec.md gains §6.4 on where a map is built, and a §6.3 rule on the map and categories coexisting. Related: deepspeedai#8252, deepspeedai#8230. cc @delock --------- Signed-off-by: Achyuthan Sivasankar <[email protected]>
…equence The compatibility wrapper stays available at deepspeed.sequence.cross_entropy.vocab_sequence_parallel_cross_entropy for existing callers, but new callers should use the general vocab_parallel_cross_entropy(tp_group=..., sp_group=...) API instead. Signed-off-by: iLeGend <[email protected]>
_vocab_metadata_cache was a module-level dict keyed on tp_group, so it held a strong reference to any process group ever passed in and never released it. Remove it: the bare vocab_parallel_cross_entropy function now always re-validates (no cache), and VocabParallelCausalLMLoss caches its own resolved (vocab_start_index, vocab_end_index, global_vocab_size) on self after validating once, since it already owns tp_group for its whole lifetime. Reusing one loss instance for a different shard now raises instead of silently reusing stale metadata. Signed-off-by: iLeGend <[email protected]>
Good catch. I removed the reference to the I think this is safe enough for the multi-model/multi-shard/multi-loss case, since each loss instance owns its own |
The ("lm_head", "embed_out") check was hardcoded separately in
_replace (legacy_lm_head), _create_row_parallel_layer,
_configure_gathered_column_tie_fallbacks, and _replace_last_linear_module,
coupled to _is_lm_head_name without actually reusing it. Route them all
through _is_lm_head_name, which now reads AutoTPConfig.lm_head_patterns
when a partition_config is supplied and falls back to the legacy pair
otherwise, so there is one place that defines what counts as an LM head.
Signed-off-by: iLeGend <[email protected]>
|
@jinyouzhi can you fix CI failure? Thanks! |
OK sure |
Signed-off-by: Jin, Youzhi <[email protected]>
Revert 53c4703 Keep _is_lm_head_name instance-bound so it can honor AutoTPConfig.lm_head_patterns, and update the stale static call in its regression test. Signed-off-by: Jin, Youzhi <[email protected]>
ef7d23f
…#8538) ## Summary Implements PR-E from deepspeedai#8173: eager training for explicitly row-parallel, untied `lm_head` / `embed_out` output heads. This PR is independent of the vocabulary-parallel CE work in PR-B deepspeedai#8309 and contains no PR-D changes. - Slice replicated activations along the last dimension using the frozen weight-shard geometry, including uneven hidden dimensions. - Reuse the existing row-parallel forward output reduction and replicated bias behavior. - Reconstruct and all-reduce the complete input gradient in backward so preceding replicated layers receive the correct gradient. - Preserve the original parameter identity, existing inference specialization, and default gathered-column training behavior. - Reject tied output weights at TP > 1, reshaped/non-input-dimension partitions, and deferred compiler collectives for this path. ## Validation Executed on jk01 with two NVIDIA L20 GPUs (46068 MiB each), in an isolated container: Python 3.12.3, PyTorch 2.13.0+cu130, CUDA 13.0, Transformers 5.14.1. - Partition/config regression: **17 passed**. - Real two-rank DeepSpeed-engine integration: **three configurations each completed five optimizer updates**, with two accumulated micro-batches per update (SGD LR 0.05, ZeRO stage 0, FP32, clipping explicitly disabled in both paths). - Configurations cover `lm_head` / `embed_out`, bias / no bias, hidden dimensions 32 / 35, and flattened / sequence-shaped inputs. - Compared logits, CE loss, input gradients, replicated projection gradients, local head gradients, and updated weights against an independent unsharded PyTorch model after every update. Also checked the exact SGD update and that weights actually changed. - Changed-file pre-commit checks and `git diff --check` passed. The integration method `TestRowParallelOutputHeadTraining.test_five_optimizer_steps_match_unsharded_reference` was executed through two-rank torchrun on the real devices. It can also be run through pytest: ```sh PYTHONPATH=.:tests python3 -m pytest tests/unit/module_inject/test_tp_partition_config_path.py -q PYTHONPATH=.:tests python3 -m pytest tests/unit/v1/autotp/test_autotp_training.py::TestRowParallelOutputHeadTraining -v -s ``` This validates the minimal eager training contract, not large-model convergence, mixed-precision/ZeRO checkpoint round trips, or DeepCompile. Coupled tied-embedding sharding remains outside this PR's scope. Signed-off-by: 0z5a <[email protected]> Co-authored-by: 0z5a <[email protected]> Co-authored-by: Ma, Guokai <[email protected]>
…eedai#8540) ## Dependency and review scope Depends on deepspeedai#8309 (PR-B from deepspeedai#8173), currently open. This branch is based on its head `1b4ba6b162e87b10bedbb7316466bd877e2cb278`; inherited PR-B commits retain their original authorship. The final commit is the optional Liger backend work. Please review that commit for this PR's independent changes. This is a draft until PR-B lands; the master comparison currently includes the dependency changes. This addresses the optional fused CE and memory-profiling direction in deepspeedai#8173. It does not include row-parallel output-head training (deepspeedai#8538) or coupled tied-embedding sharding. ## Changes - Add opt-in `tensor_parallel.vocab_parallel_ce_backend: "liger"` alongside `vocab_parallel_lm_head: true`; default `"torch"` remains dependency-free. - Lazy-load Liger's public vocabulary-parallel CE function (`liger-kernel>=0.8.1`) and retain the existing reduction / causal-label-shifting contract. - Use identical gathered shard metadata to select Liger on every TP rank only for equal vocabulary shards, nonempty CUDA tokens, and FP32/FP16/BF16 logits. Uneven shards or unsupported layouts uniformly fall back to the PyTorch reference. - Reject repeated retained-graph backward and higher-order gradients for Liger's destructive saved-buffer backward instead of silently producing incorrect gradients. - Add optional-backend numerical and real AutoTP training integration coverage, and document the compatibility boundaries. This fuses CE only, **not the linear projection**. Local vocabulary logits are still materialized. ## Real-device validation jk01: two NVIDIA L20 GPUs (46068 MiB each), isolated container, Python 3.12.3, PyTorch 2.13.0+cu130, CUDA 13.0, Transformers 5.14.1, Triton 3.7.1, liger-kernel 0.8.2. - Partition/config and non-distributed CE regressions: **42 passed**, 7 distributed cases deselected in that regression invocation. - Two-rank numerical tests: FP32/FP16/BF16, none/sum/mean, even/uneven vocab, ignored targets, unchanged input logits, retained-graph rejection, and higher-order rejection passed. - Real DeepSpeed-engine training: vocab 34 (Liger) and vocab 35 (uneven fallback), each **five optimizer updates**, with two accumulated micro-batches per update. SGD LR 0.05, FP32, ZeRO 0, clipping explicitly disabled. Logits, loss, gradients, updated sharded head weights/biases, and replicated projection weights/biases agree with an independent unsharded PyTorch model. - Changed-file pre-commit checks and `git diff --check` passed. The distributed integration methods in `TestLigerVocabParallelCE` were executed via two-rank torchrun on the actual devices. Reproduce with pytest: ```sh PYTHONPATH=.:tests python3 -m pytest tests/unit/v1/sequence_parallelism/test_liger_vocab_parallel_cross_entropy.py -v -s ``` ## Memory profile Compared local linear + vocabulary-parallel CE forward/backward + five SGD updates, two micro-batches each, after kernel warm-up and resetting weights/allocated peaks. BF16, TP=2, 1024 tokens per rank, hidden=128, global vocabulary=65536, local vocabulary=32768. Statistics are the maximum across ranks. | Backend | Peak allocated | Incremental peak over baseline | Short-run elapsed | | --- | ---: | ---: | ---: | | PyTorch | 512247296 bytes (488.52 MiB) | 478160384 bytes | 0.0606 s | | Liger | 310922240 bytes (296.52 MiB) | 276834304 bytes | 0.0779 s | Peak allocated decreases approximately 39.3%; incremental peak decreases approximately 42.1%. Losses and final local weights agree. This is a short single-sample memory comparison, **not evidence of throughput speedup**; allocated memory is not total device or reserved memory. Four-GPU TP x SP integration, mixed-precision/ZeRO checkpoint round trips, and large-model convergence are not validated by this two-GPU run. --------- Signed-off-by: 0z5a <[email protected]>
deepspeedai#8680) Follow-up to deepspeedai#8538 (part of deepspeedai#8173). ## Summary Row-parallel output-head training (deepspeedai#8538) rejected an `lm_head`/`embed_out` whose weight is tied to the input embedding, and that rejection could be bypassed silently (see *Bug* below). This PR fixes the bug and supports the tied case by sharding the tied `nn.Embedding` along the same hidden dimension as the row-parallel head, so both share one physical per-rank shard. ## Bug: tie silently broken when the embedding is visited first The tie was detected by scanning `self.module.modules()` *during* the replacement walk. The walk visits `embed_tokens` before `lm_head`, so when `partition_config` also matches the embedding, `_slice_embedding` swaps it for a new module with a new Parameter first. By the time `lm_head` was checked, no other module aliased its weight, the check passed, and the head was sharded while the embedding kept a separate copy — with no error. ```python AutoTPConfig(layer_specs=[ TPLayerSpec(patterns=[r".*embed_tokens\.weight$"], partition_type=PartitionType.ROW), TPLayerSpec(patterns=[r".*lm_head\.weight$"], partition_type=PartitionType.ROW), ]) # before: no error, model.lm_head.weight is model.embed_tokens.weight -> False # after: both modules share one hidden-dimension shard ``` Fixed by snapshotting weight ties from the original, pre-replacement model in `AutoTP.__init__`, following the approach the vocab-parallel path already takes for the same ordering issue (introduced in deepspeedai#8309, now `_tied_vocab_parallel_embedding_ids`). ## Changes - `HiddenParallelEmbedding` (`layers.py`): looks up each token's hidden-dimension slice and all-gathers the slices into the replicated activation (`GatherFromTensorParallelRegion`, so uneven hidden shards work). When tied, it reuses the row-parallel head's already-sharded weight `Parameter` and partition sizes; its universal-checkpoint metadata matches `LinearAllreduce`'s. `padding_idx`, `scale_grad_by_freq`, and Gemma3's scaled embedding are preserved; `max_norm`, `sparse`, and other custom forwards are rejected. - `AutoTP` (`auto_tp.py`): - Snapshots each module's weight-tie partners from the original model, and records embeddings tied to a row-parallel training head so `_slice_embedding` leaves them alone regardless of traversal order. - `_create_row_parallel_layer` validates all tie partners *before* partitioning the shared weight, then replaces every tied embedding alias with a `HiddenParallelEmbedding`. A tie to a non-`nn.Embedding` module still raises; a conflicting explicit embedding spec is superseded with a warning. - Docs: `autotp-training.md` describes the tied path and notes that `vocab_parallel_lm_head` is usually preferable for tied models, since a row-parallel head all-reduces full vocabulary logits. Not changed: untied embeddings that match a spec still go through `_slice_embedding`, whose un-gathered hidden slice is intentional for per-head embeddings such as T5's `relative_attention_bias`. ## Tests - Unit (`test_tp_partition_config_path.py`): tie is shared with/without an embedding spec visited first (the embedding-spec case reproduces the bug above on master); rejection for non-embedding ties and `max_norm` leaves the model untouched; Gemma3 scaled lookup matches the original module. - Integration (`TestTiedRowParallelOutputHeadTraining`, 2 GPUs): three optimizer steps against an unsharded reference comparing logits, the full tied gradient (lookup + projection contributions), and updated weights, covering even/uneven hidden (32/35), `padding_idx`, embedding-spec ordering, and ZeRO-0/2; universal-checkpoint affine map rebuilds the tied weight from uneven shards. Mutation check: detaching the embedding's weight in the lookup fails all four training cases. - `tests/unit/module_inject/` and `tests/unit/v1/autotp/`: 225 passed, 12 skipped on 3x NVIDIA GeForce RTX 5090 D. --------- Signed-off-by: Jin, Youzhi <[email protected]> Co-authored-by: Ma, Guokai <[email protected]>
deepspeedai#8859) # AutoTP: shard an untied input embedding along the vocabulary (`vocab_parallel_embedding`) **Part of:** deepspeedai#8672 (Q4 2026 roadmap), "Enable training of large scale models". ## Summary `vocab_parallel_lm_head` (deepspeedai#8309, deepspeedai#8582) shards an input embedding only when it is tied to the output layer. An untied input 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` (default `false`): AutoTP replaces the model's untied `nn.Embedding` by the existing `VocabParallelEmbedding` (1/tp of the rows per rank, masked lookup, sum over the TP group). ```json "tensor_parallel": { "autotp_size": 8, "vocab_parallel_embedding": true } ``` The option works on all three AutoTP partitioning paths (HuggingFace `tp_plan`, `partition_config` or `preset_model`, and the heuristic path), at ZeRO 0 to 3, and with models built under `deepspeed.zero.Init`. The vocabulary is split evenly over the ranks, like the vocabulary-parallel head. A tied embedding is sharded with its output head instead: | Tied embedding, output head | With `vocab_parallel_embedding: true` | |---|---| | vocabulary-parallel (`vocab_parallel_lm_head`) | no change: the embedding already shares the head's shard | | row-parallel (`partition_config` ROW spec, deepspeedai#8538, deepspeedai#8680) | no change: the embedding already shares the head's shard | | replicated | `NotImplementedError`, rather than untie the embedding from its head | The option raises a `ValueError` when a `partition_config` spec has already partitioned the embedding, and when the DeepCompile `autotp` pass 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): | | Without | With | |---|---|---| | Step time | 15.88 s | 15.77 s | | Peak memory (rank 0) | 114.1 GiB | 107.0 GiB | | Embedding parameters per GPU | 1.25B | 0.16B | | Gradient reduce after the last backward (`wall_clock_breakdown`) | 89 ms | 44 ms | 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", `VocabParallelEmbedding` and `embedding_rowwise`. deepspeedai#8309 and deepspeedai#8582 shard a tied embedding with a vocabulary-parallel head, deepspeedai#8538 and deepspeedai#8680 a tied embedding with a row-parallel head, deepspeedai#8294 converts the HuggingFace `embedding_rowwise` entry, and deepspeedai#8173 (closed RFC) discusses sharding output layers. None shards an untied input embedding. ## The change | File | Change | |---|---| | `deepspeed/module_inject/auto_tp.py` (+46) | `AutoTP._replace_untied_input_embedding()`: replaces every registration of `model.get_input_embeddings()` by a `VocabParallelEmbedding`, split evenly over the ranks. It gathers a `zero.Init` weight with `_gather_zero3_params`, which records the replacement for the optimizer remap. It returns `None` when 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 spec | | `deepspeed/runtime/engine.py` (+25, -1) | calls it in `finalize_autotp`, before the replicated-gradient hooks are registered, when the option is set; the heuristic path builds an `AutoTP` for it; rejects the DeepCompile `autotp` pass before any module is replaced | | `deepspeed/runtime/tensor_parallel/config.py` (+8) | the option | | `docs/_pages/config-json.md`, `docs/_tutorials/autotp-training.md` | the option, and an "Untied Input Embedding" section | | `tests/unit/v1/autotp/test_autotp_training.py` (+213) | `TestVocabParallelEmbedding` (16 tests) | ### Changes after the first review - A tied embedding that a row-parallel head has already sharded (`HiddenParallelEmbedding`) is left as it is, instead of raising. - An embedding already partitioned by a matching `partition_config` spec raises a `ValueError`, instead of being split a second time along the vocabulary. - The DeepCompile `autotp` pass raises a `ValueError` at initialize, instead of failing later in `engine.compile()`. - Models built under `deepspeed.zero.Init` work; before, the split of the empty ZeRO-3 placeholder raised. - The vocabulary is split evenly. Before, the HuggingFace plan and `partition_config` paths split it by key-value head 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. - Every name under which the model registers the embedding module gets the sharded module. - The sharding moved from a function in `layers.py` to an `AutoTP` method, so that it uses the model's `tp_meta` (including `tp_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: HF `tp_plan` at ZeRO 0, 1, 2, 3 and ZeRO-3 with `zero.Init`; HF `tp_plan` with `vocab_parallel_lm_head: true`; `preset_model: llama`; the heuristic path): the input embedding becomes a `VocabParallelEmbedding` with 33 of the 66 rows. With tokens from both halves of the vocabulary, the losses of 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): the embedding 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 embedding from the shards. - `test_save_load_round_trip_preserves_loss` (ZeRO-1): a checkpoint restores the loss in a model built with another seed. - `test_rejects_conflicting_request` (3 cases): a tied embedding with a replicated head, a `partition_config` spec that matches the embedding, and the DeepCompile `autotp` pass. Without the fixes (the first revision merged with master), 11 of the 16 tests fail: the 7 `test_matches_single_device_training` cases on the HF plan and `partition_config` paths (44 and 22 rows; with `zero.Init` the split raises), the row-parallel tied case, the alias case, and the spec and DeepCompile rejections. The changed code runs inside `deepspeed.initialize` with AutoTP, so every test file that initializes an engine with AutoTP 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. | Test files | Where, torch | Result | |---|---|---| | `unit/v1/autotp/test_autotp_training.py` (includes the 16 `TestVocabParallelEmbedding` tests) | GPU | 60 passed, 2 skipped (the existing BF16-optimizer overflow skips) | | the same file, `-m sequential` | GPU | 66 passed | | `TestVocabParallelEmbedding` on the first revision merged with master (control) | GPU | 11 failed, 5 passed, as listed above | | `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.py` | GPU | 96 passed | | `unit/compile/test_tp_compile.py`; the same with `-m sequential` | GPU | 19 passed; 9 passed | | `unit/checkpoint/test_autotp_uc_checkpoint.py`, `test_zero_affine.py` | GPU | 74 passed | | `unit/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.py` | GPU | 60 passed, 5 skipped (4 need `liger_kernel`, which is not installed here or in CI; 1 needs `DEEPSPEED_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.py` | GPU | 304 passed, 7 skipped (cases that run only with `DEEPSPEED_RUN_H100_TESTS=1`) | | `unit/v1/moe/test_autoep_autotp_grad_parity.py` with `DEEPSPEED_RUN_H100_TESTS=1` | GPU | 20 passed | | `unit/v1/moe/test_autoep_autotp_sharded_attention.py`; the same with `-m sequential` | GPU | 69 passed; 2 passed | | `unit/v1/moe/test_autoep_expert_activation.py` | GPU, torch 2.9.1+cu128 (on torch 2.8 the backward of `torch._grouped_mm` can stall with an empty expert, also on master) | 96 passed, 12 skipped (the file's own skip: "with Triton installed, this form's grouped path needs CUDA tensors") | | `test_autotp_training.py`, default set; `-m sequential` | CPU | 60 passed, 2 skipped; 66 passed | | `test_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.py` | CPU | 143 passed | | `test_zero_affine.py` | CPU | 27 passed | No test failed outside the control row. `pre-commit run --files` passes on every changed file. ## Compatibility | Cell | Validated | |---|---| | AutoTP 2, ZeRO 0, 1, 2, 3, fp32 (unit tests, HF `tp_plan` path) | yes | | ZeRO-3 with the model built under `deepspeed.zero.Init` | yes (unit test) | | `partition_config` (`preset_model: llama`) and heuristic paths, ZeRO-1 | yes (unit tests) | | Untied embedding with `vocab_parallel_lm_head: true` | yes (unit test, ZeRO-0 and heuristic ZeRO-1) | | Tied embedding with a vocabulary-parallel or a row-parallel head | yes, unchanged (unit tests) | | Checkpoint save / load (ZeRO-1) and universal checkpoint metadata | yes (unit tests) | | Universal checkpoint conversion end to end (`ds_to_universal`), HF export with `save_16bit_model` | not run | | AutoTP 8 + ZeRO-1, bf16, Qwen2.5-72B, 4 nodes | yes, first revision (see Results) | | bf16 / fp16 in the unit tests | not run (fp32 only) | | Optimizer or parameter offload | not run | | DeepCompile `autotp` pass | rejected with `ValueError` | | Models whose `get_input_embeddings()` is not an `nn.Embedding` | rejected with `ValueError` | | Accelerators other than CUDA | not run on device; the new tests also pass on CPU with 2 gloo processes | ## AI assistance This change, its tests and this description were prepared with AI assistance (Claude). --------- Signed-off-by: pengdurice <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
Motivation
Changes
This pull request adds support for vocabulary-parallel language modeling (LM) heads in DeepSpeed's tensor parallelism (TP) system. It introduces a new
VocabParallelLinearlayer that keeps logits sharded across TP ranks for untied LM heads and installs a compatible distributed loss automatically. The changes ensure correct partitioning, validation, and integration with the rest of the DeepSpeed engine and configuration system.Key changes include:
Vocabulary-parallel LM head support
vocab_parallel_lm_headconfiguration option inTPTrainingConfigand passed it through the relevant APIs to enable this feature. [1] [2] [3] [4] [5]auto_tp.pyto detect and correctly replace untied LM heads with the newVocabParallelLinearlayer, along with validation to ensure the vocab head is not tied to the embedding. [1] [2] [3] [4]New layer and loss integration
VocabParallelLinearlayer inlayers.py, which ensures logits remain sharded and provides partitioning metadata. [1] [2]Miscellaneous
deepspeed.sequencemodule's public API.These changes collectively enable efficient, distributed training of models with untied, sharded vocabulary projection heads and ensure compatibility with DeepSpeed's tensor parallelism ecosystem.
Limitations