Skip to content

[AutoTP] Convert HF embedding_rowwise tp_plan entries to SKIP specs - #8294

Merged
delock merged 4 commits into
deepspeedai:masterfrom
ebarkhordar:fix/8290-embedding-rowwise-tp-plan
Aug 23, 2026
Merged

delock merged 4 commits into
deepspeedai:masterfrom
ebarkhordar:fix/8290-embedding-rowwise-tp-plan

Conversation

@ebarkhordar

Copy link
Copy Markdown
Contributor

Problem

huggingface/transformers#47579 (on main since 861f4c41, 2026-08-21) makes PretrainedConfig.__init__ inject "embed_tokens": "embedding_rowwise" into base_model_tp_plan whenever tie_word_embeddings is true. SUPPORTED_STYLES is a strict allowlist and convert() raises on any style outside it, rejecting the whole plan, so AutoTP plan conversion now fails for every tied-embedding model (Qwen2/Qwen3, Llama, Gemma) built against transformers main.

Fix

Recognize embedding_rowwise and convert it to a SKIP spec, which is option 2 in #8290: the entry is understood and the embedding is deliberately left replicated.

What follows is the behaviour DeepSpeed already implements rather than a new policy. lm_head still converts to a gathered column spec, and _configure_gathered_column_tie_fallbacks then sees that lm_head.weight is embed_tokens.weight and leaves both modules replicated, logging that coupled vocabulary-parallel embedding is not supported yet. A tied model is therefore left in the shape it has on a transformers release without the injection, with both modules replicated and the tie intact.

The entry maps to SKIP with grad_allreduce left false, unlike replicated_with_grad_allreduce. The parameter is never split, so register_replicated_grad_hooks must not register an all-reduce for it.

Styles that are still unknown continue to reject the whole plan. test_unsupported_style_rejects_whole_plan is unchanged and still passes.

Verification

Run on CPU in a container at edaa7221, against transformers main (5.16.0.dev0) and torch 2.13.0+cpu.

  • The two added tests fail on master with the reported ValueError and pass with this change.
  • tests/unit/module_inject/ and tests/unit/runtime/test_tp_plan_extraction.py: 47 passed, on Python 3.11 and on 3.12.
  • pre-commit run --files on the three changed files passes yapf, check-torchdist, check-license and codespell; flake8 5.0.4 exits 0 on them under Python 3.11.
  • Not verified here: test_qwen2_tied_lm_head_falls_back_to_replicated, which needs 2 GPUs. That is the test HF transformers main injects 'embedding_rowwise' into tp_plan for tied-embedding models; AutoTP rejects the whole plan #8290 reports as failing and the one this change is meant to restore.

Two things worth deciding separately

Scoping this to embedding_rowwise leaves the next transformers-side style to fail the same way, since the injection is unconditional and the allowlist is deny-by-default against a vocabulary DeepSpeed does not own. A general rule for unknown styles looks like a maintainer call rather than something to settle here.

Related to that, the convert() docstring says entries with an unsupported style become SKIP specs instead of invalidating the plan, but no code path does that, and none does after this change either: an unsupported style still raises before the loop is reached. The docstring and the raise arrived together in #8204, so I have left both alone. Happy to follow up once you have picked the policy.

Refs #8290. This covers the conversion failure only, and does not implement vocabulary-parallel tied embeddings (option 1 or 3 in that issue), so I have not used a closing keyword.

transformers injects "embed_tokens": "embedding_rowwise" into
base_model_tp_plan for every tie_word_embeddings model since
huggingface/transformers#47579, and the converter's strict allowlist
rejected the whole plan, so AutoTP plan conversion failed for all
tied-embedding models against transformers main.

Recognize the style and map it to a SKIP spec, leaving the embedding
replicated. The gathered-column tie fallback already keeps a tied
lm_head replicated alongside it, so tied models behave as they do on a
transformers release without the injection. grad_allreduce stays false
because the parameter is never split.

Refs deepspeedai#8290

Signed-off-by: Ehsan Barkhordar <[email protected]>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 79ef659526

ℹ️ 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".


SUPPORTED_STYLES = {"colwise", "colwise_rep", "colwise_gather_output", "rowwise", "replicated_with_grad_allreduce"}
SUPPORTED_STYLES = {
"colwise", "colwise_rep", "colwise_gather_output", "rowwise", "replicated_with_grad_allreduce", "embedding_rowwise"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add the required Signed-off-by trailer

This is a one-parent, non-merge commit, but its commit message has no Signed-off-by: trailer, so it does not satisfy the repository's mandatory commit requirement; recreate the commit with --signoff using the configured Git identity.

AGENTS.md reference: AGENTS.md:L8-L8

Useful? React with 👍 / 👎.

@delock

delock commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

Hi @ebarkhordar , thanks for your timely fix to this issue. For your opens:

  1. Unknown entries should lead to an explicit fail, allowing concious decision made on each new entry. Let's see whether this strategy works.
  2. Yes, the docstring needs to be updated to reflect this strategy, can you update doc string in this PR? Thanks!

The docstring said unsupported styles become SKIP specs, which no code path
has ever done: an unknown style raises and invalidates the plan. Document that
as the intended strategy, per the maintainer decision in deepspeedai#8294, and name the
two supported styles that do map to SKIP. Also correct the return line, which
returns None only for an empty plan.

Signed-off-by: Ehsan Barkhordar <[email protected]>
@ebarkhordar

Copy link
Copy Markdown
Contributor Author

Thanks, both are settled in 0aa0c41.

  1. No change. An unknown style still raises and rejects the whole plan, and test_unsupported_style_rejects_whole_plan still pins that.

  2. The convert() docstring now describes that strategy instead of the SKIP-on-unsupported behaviour it claimed, which no code path has implemented since the raise landed in (1/2) Implementing Compiler Pass for AutoTP #8204. It also names the two supported styles that do become SKIP specs, embedding_rowwise and replicated_with_grad_allreduce. One more line in the same docstring was stale and I fixed it there rather than leaving it for later: the function returns None only for an empty plan, not when no entry is convertible.

The commit touches that docstring and nothing else. yapf, check-torchdist, check-license and codespell pass on the file, and flake8 5.0.4 exits 0 on it under Python 3.11.

@delock
delock enabled auto-merge August 23, 2026 08:53
@delock
delock added this pull request to the merge queue Aug 23, 2026
Merged via the queue into deepspeedai:master with commit 20ea454 Aug 23, 2026
13 checks passed
@ebarkhordar
ebarkhordar deleted the fix/8290-embedding-rowwise-tp-plan branch August 23, 2026 11:55
@delock

delock commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

Hi @ebarkhordar , thanks for your timely fix. Now we can move tied tests to modal GPU CI. I have a question for you, do you think we should persue option 1 in the long run? What is pros and cons for option 1? Thanks!

@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 14, 2026
banxingmjj pushed a commit to openanolis/DeepSpeed that referenced this pull request Nov 22, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants