AutoEP + AutoTP folding: Universal Checkpointing under ZeRO-3 (#8848)
# AutoEP + AutoTP folding: Universal Checkpointing under ZeRO-3
**Part of:** #8672 (Q4 2026 roadmap), "Mixture-of-Expert (MoE) Training
Scaling and Kernel Development": "AutoEP Performance Optimization &
Parallel Folding (AutoEP + AutoTP)" (PoC @tohtana @pengdurice). Follows
#8670.
## Problem
#8670 makes `ds_to_universal` refuse a folded ZeRO-3 checkpoint:
"Universal checkpoint conversion of an AutoEP +
AutoTP folded ZeRO-3 checkpoint is not supported yet. Load it with the
same topology." So a folded run can only resume
with the same TP size, EP size and number of GPUs.
Four things stood in the way: three in the conversion, one in the load.
1. **File keys.** The AutoEP expert step of the ZeRO-3 conversion keyed
checkpoint files by the ZeRO data-parallel rank
alone (`_zero3_rank_from_file`). ZeRO-3 files are named
`zero_pp_rank_{dp}_mp_rank_{tp}`. Under folding the files of
different TP ranks share a data-parallel rank, so they collided. That is
why #8670 refused.
2. **Experts in the AutoTP universal info.**
`collect_autotp_universal_checkpoint_info` publishes every parameter
AutoTP
left untouched as TP-replicated, with an affine map built from its local
shape. Under folding that includes the AutoEP
expert weights (`experts.w1/w2/w3`), but the TP peers hold different
experts, so this layout is wrong. Any reader
that plans from the published patterns or affine maps would trust it.
3. **The TP merge's strict check.** `_merge_tp_slice_files` asserts that
every pattern in AutoTP's universal info was
used. The expert patterns of item 2 are never used by the TP merge,
because the AutoEP step merges those weights,
so the assertion fails.
4. **The layout check on load.** `_load_checkpoint` compares the folding
layout recorded in the model-states file with
the runtime's (`_validate_autoep_folding_checkpoint_metadata`). This
rejects any other TP size, EP size or an
unfolded runtime, although a universal load re-cuts every tensor for the
runtime's layout.
## Change
- `deepspeed/checkpoint/ds_to_universal.py`:
- The AutoEP ZeRO-3 files are keyed by (TP rank, DP rank), with
`_zero3_tp_dp_ranks_from_file`, which the dense
merge already uses. `_zero3_rank_from_file` is no longer used and is
removed, along with the folding refusal. The
docstring of `_autoep_expert_param_names_by_rank` states the key.
- `_merge_tp_slice_files` no longer counts a pattern as unused when it
only matches parameters excluded from this
merge. The ZeRO-3 path now passes the AutoEP expert names as excluded;
the ZeRO-1/2 path already did. With the
change to `layers.py` below, new checkpoints no longer publish these
patterns; this keeps checkpoints saved before
it convertible.
- When an optimizer file has no `ds_zero_partition_groups` entry for an
expert sub-group, the converter raises
instead of falling back to the dense data-parallel size and rank. Expert
sub-groups are partitioned over the
expert-data-parallel group, so that fallback would be wrong. Every
partition-native AutoEP ZeRO-3 checkpoint
records the entry.
- The expert reassembly itself is unchanged. It already uses each file's
EP rank and ZeRO partition rank. Under
folding each expert slice exists on exactly one rank, and the existing
duplicate and completeness checks still
apply.
- `deepspeed/module_inject/layers.py`
`collect_autotp_universal_checkpoint_info`: skip the expert parameters
of AutoEP
layers (the parameters with `allreduce=False` in a module marked
`_is_autoep_layer`). AutoEP records their placement
in its own checkpoint metadata. The router and the shared expert are
still published as TP-replicated: they are the
same on every TP peer. Experts of the legacy `deepspeed.moe` layer are
not affected.
- `deepspeed/runtime/engine.py` `_load_checkpoint`: skip the
folding-layout check when `checkpoint.load_universal` is
set. Rigid loads keep it. The other folding-metadata checks are on the
ZeRO-1/2 per-expert files and the per-rank
ZeRO files, which a universal load does not read.
- Loading tensors needs no change. For each parameter,
`load_hp_checkpoint_state` in `stage3.py` cuts the EP slice,
then the TP shard, then the ZeRO partition in the parameter's own group
(dense data-parallel or expert
data-parallel). That is what folding needs. A module-only load
(`load_module_only=True`) goes through
`load_module_checkpoint_state_from_checkpoint_dir_stage3`, which uses
the same function.
- Docs: `docs/code-docs/source/autoep.rst` (universal loads, including
module-only loads, can change
`tensor_parallel.autotp_size`; regular loads need the same `autoep_size`
and `autotp_size`) and
`docs/_pages/config-json.md`.
- Tests, in `tests/unit/v1/moe/test_autoep_autotp_sharded_attention.py`:
- `TestZero3FoldedCheckpoint` no longer expects the conversion to be
refused.
- New `TestZero3FoldedUniversalCheckpoint`. A distributed fixture trains
a folded TP 2 x EP 2 run on 4 ranks for one
AdamW step, saves, converts, and records every parameter and both Adam
states at full size, then the losses and
parameters of the next step. Each target layout loads the universal
checkpoint and must restore every recorded
tensor exactly. Targets with the source's 2 data-parallel groups read
the same batches, so they must also repeat
the next step. Targets, each in fp32 and bf16:
| Target (ranks, TP, EP) | What changes from the source (4 ranks, TP 2,
EP 2) |
|---|---|
| 4, 2, 2 | nothing |
| 4, 2, 4 | EP 4: each EP group spans both TP groups |
| 4, 2, 4, CPU optimizer offload (#8829) | the same, with the optimizer
on the CPU |
| 4, 4, 2 | TP 4: one dense data-parallel group |
| 8, 4, 2 | more ranks, the same 2 dense data-parallel groups |
| 2, 1, 2 | AutoEP without AutoTP |
| 4, 1, 4 | AutoEP without AutoTP: dense data parallel 2 → 4, expert
data parallel 2 → 1, TP 2 → 1 |
- The next-step comparison leaves out only the pass-through half of each
attention key head of `k_proj.bias`
(`[..., 8:]` with `head_dim=16`, `partial_rotary_factor=0.5`). Those
entries add the same amount to every score of
a query, which the softmax cancels, so their true gradient is zero;
their computed gradient is rounding noise that
another layout adds up in another order, and AdamW turns that noise into
steps of about lr. RoPE rotates the other
half by position, so it has a real gradient and is compared.
- New `TestZero3FoldedUniversalModuleOnlyLoad`: the same converted
checkpoint loaded with `load_module_only=True`
into (4, 2, 4) and (4, 1, 4), in fp32 and bf16. Every fp32 weight must
equal the recorded state. In fp32, (4, 2, 4)
must also give the source's next losses, which checks the weights the
model computes with.
- New `TestFoldedUniversalSchemaLeavesOutExperts`: a folded TP 2 x EP 2
run saves a checkpoint; in every model-state
file, no expert weight matches a `tp_replicated_parameter_patterns`
entry or has an affine map.
- `test_autoep_unit.py`: the converter's AutoEP detection test reads the
metadata under the new
(TP rank, DP rank) key.
Not covered:
- ZeRO-1/2 conversion of folded checkpoints. It stores experts in
per-expert files, one per TP rank, and is still
refused in `autoep_universal.py` (`resolve_expert_ckpt_path`).
- Folded layouts with AutoTP's default specifications (MLP column and
row, row-parallel bias, vocabulary-parallel
embedding and head). The test shards only the attention projections. The
routed experts are the only part folding
adds to the converter; the dense AutoTP categories go through the same
ZeRO-3 TP merge as an unfolded AutoTP run.
## Tests
On master 4a9efc110 with this change, 1 node of 8 NVIDIA H200 (driver
595.112.07), torch 2.8.0+cu126, pytest 8.3.5:
| File | Result |
|---|---|
| `tests/unit/v1/moe/test_autoep_autotp_sharded_attention.py` (includes
the 14 universal-checkpoint, 4 module-only and 1 schema cases) | 88
passed |
| the same file, `-m sequential` | 2 passed |
| `tests/unit/v1/moe/test_autoep_checkpoint.py` | 47 passed, 2 skipped
(ZeRO-1/2 universal-checkpoint cases that run only with
`DEEPSPEED_RUN_H100_TESTS=1`) |
| `tests/unit/v1/moe/test_autoep_autotp_checkpoint.py` | 6 passed, 2
skipped (resume cases that run only with `DEEPSPEED_RUN_H100_TESTS=1`) |
| `tests/unit/v1/moe/test_autoep_unit.py` (includes the converter's
AutoEP detection test, updated for the (TP rank, DP rank) keys) | 214
passed |
| `tests/unit/checkpoint/test_autoep_affine_universal.py` | 21 passed |
|
`tests/unit/runtime/tensor_parallel/test_autotp_universal_checkpoint.py`
| 33 passed |
| `tests/unit/checkpoint/test_autotp_uc_checkpoint.py` | 47 passed |
| `tests/unit/checkpoint/test_affine_shard_map.py` | 286 passed |
| `tests/unit/checkpoint/test_fp32_master_only.py` | 8 passed |
| `tests/unit/checkpoint/test_reshape_checkpoint.py` | 8 passed |
| `tests/unit/v1/zero/test_zero2_gradient_safety.py` | 58 passed |
| `tests/unit/v1/autotp/test_autotp_custom_patterns.py` | 28 passed |
| `tests/unit/v1/autotp/test_tp_plan_e2e.py` | 10 passed |
| `tests/unit/checkpoint/test_universal_checkpoint.py` | 150 passed |
| `tests/unit/v1/autotp/test_autotp_training.py` | 44 passed, 2 skipped
(the existing BF16-optimizer overflow skips); `-m sequential`: 66 passed
|
These are the test files that use a function this PR changes
(`ds_to_universal`, `collect_autotp_universal_checkpoint_info`,
the universal-load branch of the AutoEP folding check). No test failed.
The new schema test fails, as it should, when
`deepspeed/module_inject/layers.py` is taken from master 4a9efc110 and
everything else is kept: `model.layers.1.mlp.experts.w1 is TP-replicated
in .../zero_pp_rank_0_mp_rank_00_model_states.pt`.
On CPU (`DS_ACCELERATOR=cpu`, torch 2.8.0): `test_autoep_unit.py`,
`test_autoep_affine_universal.py`,
`test_autoep_affine.py`,
`tests/unit/runtime/tensor_parallel/test_autotp_universal_checkpoint.py`,
`test_autotp_uc_checkpoint.py`, `test_affine_shard_map.py`: 614 passed,
19 skipped (cases that need an accelerator).
`pre-commit run --files` passes on the changed files.
## Further checks (not part of the PR's tests)
Earlier, on master 5f266e26d with the folding optimizer-offload change
(#8829, merged since) applied: a wider version
of the universal test with 7 target layouts. Besides the same layout, EP
4, TP 4, 8 ranks with TP 4 and AutoEP without
AutoTP: 8 ranks with TP 2 x EP 4, and AutoEP EP 4 without AutoTP. Run in
fp32 and bf16, and in 3 offload combinations:
saved and loaded without offload, saved and loaded with offload, saved
without and loaded with offload. All 42 loads
restore every parameter and both Adam states exactly. Every target with
the source's 2 data-parallel groups repeats
the next step: losses equal, and parameters within 1e-4 relative,
leaving out the whole attention key bias (that run
predates the narrower exclusion). In bf16 only the same layout repeats
it.
## Duplicate check
Searched open pull requests on 2026-11-18 ("ds_to_universal", "universal
checkpoint AutoEP", "folding universal",
"AutoEP AutoTP folding"): none converts folded ZeRO-3 checkpoints. #9080
(universal loads across ZeRO stages) edits
`ds_to_universal.py` and `engine.py` in other functions
(`_save_optimizer_state_stage3`, checkpoint file names).
#8844 (mine, merged) also edited
`_consolidate_zero3_autoep_expert_states`; this branch merges it.
## AI assistance
This change, its tests and this description were prepared with AI
assistance (Claude).
---------
Signed-off-by: pengdurice <pengduhit@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>