Raise when a later engine asks for another tensor parallel layout (#9081 stage 1) (#9088)
Stage 1 of #9081: the guard (option 3), which @LiRunGuo and @Achyuthan-S
agreed should come first.
## Problem
The TP/DP groups in `deepspeed.utils.groups` are process-wide, and the
first engine creates them. When a second engine is built in the same
process:
- **Different `autotp_size`**: `_init_tp_mesh_device` returns early
because `_TENSOR_MODEL_PARALLEL_GROUP` is already set. An engine that
asks for TP4 after a TP2 engine silently becomes a TP2 model (reproducer
in #9081).
- **No AutoTP and no `mpu`** (pointed out by @Achyuthan-S on #9081):
this engine never reaches `_init_tp_mesh_device`. The earlier AutoTP
engine left `groups.mpu = groups`, so the new engine takes the TP2
engine's data parallel group, which covers only part of the ranks. On
master it then fails in `_configure_distributed_model` with
`AttributeError: 'NoneType' object has no attribute
'get_model_parallel_rank'`.
## Change
- `groups._check_existing_tp_groups()` compares the requested TP size
with the existing groups. It raises a `RuntimeError` that names both
layouts and points to #9081. It is a no-op when no TP groups exist yet.
- `_init_tp_mesh_device` calls it before its early return. This covers
the engine path, `tp_model_init` (`init_utils.py`) and
`TpTrainingManager` (`tp_manager.py`).
- `DeepSpeedEngine.__init__` also performs a preflight before publishing
`groups.mesh_device`, applying deterministic settings or configuring
AutoEP/AutoTP. It checks the requested AutoTP size, or size 1 for an
engine without AutoTP and without its own `mpu` when `groups.mpu` is
still the `groups` module set by an earlier AutoTP engine. This includes
data/sequence mesh-backed engines: a mesh does not replace `groups.mpu`.
- An AutoTP engine created after a data/sequence mesh engine is also
rejected in the preflight. The groups getters read `groups.mesh_device`
first, so without this check the AutoTP engine would silently use the
mesh engine's DP group.
- If an engine in between passed its own `mpu`, a later non-AutoTP
engine inherits that one and the stale-AutoTP check does not run.
Engines with the same layout keep sharing the groups, as before.
- The unused DP-size comparison was removed from the guard; production
callers only request a TP size.
- The AutoTP tutorial lists this as limitation 5.
The preflight avoids a rejected engine overwriting the existing engine's
global mesh before the caller catches the layout error. This is not a
general rollback mechanism for other initialization failures.
## Not covered
These are left for stage 2 of #9081, as the RFC lays out:
- **An explicit `mpu` replaces `groups.mpu`.** Every later reader of
`groups` then sees the newest engine's groups. #9074 relies on this: it
passes an `mpu` to every engine. Making this an error would break that
approach, and it is not a misconfiguration of the new engine.
- **An engine without an `mpu` after one with a user `mpu`** (the DP
case @LiRunGuo reported on #9081). The second engine takes the first
one's DP group. Megatron-style code may rely on passing the `mpu` once
("For subsequent use, just query groups"), so a check here would be a
behavior change. I would rather fix it with per-engine groups than turn
it into an error.
- **DeepSpeedExamples#1021**, which clears the TP/DP globals before each
`initialize`. The new engine then builds its own groups, so the check
does not fire.
## Duplicate check
I ran `gh pr list --search "9081 in:body"`, and searched for
`_init_tp_mesh_device` and AutoTP guard PRs. The only related open PR,
#8801, changes the `bwc.py` fallbacks and is unrelated. On #9081 nobody
else has taken the guard. @Achyuthan-S plans to move the `stage3.py`
universal-checkpoint reader in a later stage.
## Tests
### Latest preflight regression (`b66fba642`)
Hardware/software: Linux host with 2 x AMD EPYC 9575F CPUs; **4 CPU
processes, gloo**, Python 3.12.3, torch 2.14.0.dev20260804+xpu running
with `DS_ACCELERATOR=cpu`. No GPUs were used for this follow-up.
```bash
DS_ACCELERATOR=cpu LOCAL_SIZE=4 PYTHONPATH=tests:. OMP_NUM_THREADS=2 \
python -m pytest --forked \
tests/unit/v1/autotp/test_autotp_multiple_models.py \
tests/unit/utils/test_groups.py -q --color=no
# 17 passed in 65.18s
```
Scoped pre-commit and `git diff --check` also pass.
The mesh regression now covers a TP2 engine followed by either a
non-AutoTP engine or a TP4 engine with a data/sequence mesh. Both cases
fail before this follow-up because the rejected engine changes
`groups.mesh_device`. After the fix, the mesh remains unchanged and the
original engine can complete an SGD update matching an independent dense
reference. The test explicitly disables gradient clipping for that
comparison. Same-layout sharing and explicit-`mpu` inheritance continue
to pass.
Latest compatibility matrix: ZeRO stage 0, FP32, AutoTP 1/2/4, no
offload, CPU/gloo. This follow-up has not been rerun on GPU. The guard
is initialization-only; no throughput, peak-memory or convergence
improvement is claimed.
### Mesh-before-AutoTP follow-up (`4b5cc6d8a`)
`test_autotp_engine_after_mesh_raises` builds a data/sequence mesh
engine and then a TP2 engine; it fails without the new check.
- 4 x Intel Arc Pro B70 (XPU, oneCCL): `test_autotp_multiple_models.py`
cases pass.
- 4 x NVIDIA L20 (CUDA, NCCL): `pytest
tests/unit/v1/autotp/test_autotp_multiple_models.py
tests/unit/utils/test_groups.py` -> 18 passed.
### Earlier XPU validation (before the latest preflight follow-up)
Hardware: 4 x Intel Arc Pro B70 (XPU, oneCCL), torch
2.14.0.dev20260804+xpu, on upstream master 5dc44e8.
```
CUDA_VISIBLE_DEVICES=0,1,2,3 pytest -q tests/unit/v1/autotp/test_autotp_multiple_models.py tests/unit/utils/test_groups.py
15 passed
pytest -q tests/unit/v1/autotp tests/unit/utils/test_groups.py tests/unit/v1/moe/test_autoep_autotp_runtime.py
38 passed, 84 skipped (1d13628)
```
- The skips are the AutoTP tests that call `skip_on_device()` on XPU,
plus H100-only tests.
- On the first commit, one run failed in
`TestGroupCacheTeardown::test_nonmember_destroy_preserves_cached_groups`:
a worker was killed by signal 6 during teardown. That test does not
reach the changed code; it passed when rerun alone, and in the full run
on 1d13628.
- The original engine cases cover TP2 sharing, TP2-to-TP4 rejection,
TP2-to-non-AutoTP rejection, and inheritance of an intervening explicit
TP1 `mpu`. The latter initially raised; 1d13628 fixes that. The original
two rejection cases fail on master.
Earlier compatibility matrix: ZeRO stage 0, bf16 off, AutoTP 1/2/4, no
offload, on XPU. Not run on CUDA.
*This PR was prepared with AI assistance; the tests above were run on
the hardware listed.*
---------
Signed-off-by: Jin, Youzhi <youzhi.jin@intel.com>
Co-authored-by: Olatunji Ruwase <tunji.ruwase@snowflake.com>