Skip to content

[config] Skip the parallel-axes check for configs that declare no layouts - #6359

Merged
felipemello1 merged 2 commits into
pytorch:mainfrom
felipemello1:felipemello1/ci/validate-axes-skip-undeclared
Nov 24, 2026
Merged

felipemello1 merged 2 commits into
pytorch:mainfrom
felipemello1:felipemello1/ci/validate-axes-skip-undeclared

Conversation

@felipemello1

@felipemello1 felipemello1 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Agent details

TLDR: Transformers Modeling Backend 8 GPU Integration Tests has been red on main since 2026-11-18: 15 of 15 completed runs. Its MoE test (fsdp+tp+ep+cp) fails at config load with HFTransformerModel.Config has no routed experts; set parallelism.expert_parallel_degree to 1 (got 2). It broke with #5995 ("[config] Reject unsupported TP/EP degrees for FLUX"). @bantianyue, could you please take a look? This PR skips the parallel-axes check when a model config declares no sharding layouts. Native TorchTitan models all declare layouts, so their checks don't change. A dense transformers-backend model with EP > 1 now gets a clear error at parallelize time.

On main the parallel-axes check stops every transformers-backend run at EP 2 at config load; with this PR configs with 0 layouts skip it, so the CI MoE recipe trains (loss 12.41, then 7.31, on 4 GB300s), a dense flavor at EP 2 fails at parallelize, and native models are still checked.

# torchtitan/config/validation.py, _validate_declared_parallel_axes
layouts = _collect_model_layouts(model)
if not layouts:
    return

Problem

  1. _validate_declared_parallel_axes checks the sharding layouts that the model config declares. It finds the routed experts by looking for MoE.Config in the config tree.
  2. When EP > 1 and the tree has no MoE.Config, it raises "has no routed experts". Commit fb1d2ba ("Reject unsupported TP/EP degrees for FLUX") added this.
  3. The transformers backend builds its MoE and its layouts later, in parallelize_hf_transformers (build_and_swap_native_moe, then set_hf_sharding_configs). When the check runs, its config declares 0 layouts and has no MoE.Config.
  4. So every transformers-backend MoE run with EP > 1 fails at config load. The workflow runs only when torchtitan/experiments/transformers_modeling_backend/** changes. So the commit that added the check never ran it. The 12-hour schedule caught it.

Solution

  1. Return early when the config declares no layouts. Nothing is declared yet, so nothing can be checked.
  2. Native models don't change. Layouts each one declares with every degree at 1 (_collect_model_layouts on each debugmodel): Flux 32, Llama3 195, DeepSeek-V3 311, Qwen3.5 379, Kimi K3 1406, Kimi K3.1 1416. All 15 models in torchtitan/models have at least 32. The transformers-backend MoE and dense debug configs have 0.
  3. New CPU test test_hf_moe_config_accepts_ep_gt1. It builds the transformers-backend MoE debug config from a local Qwen3MoeConfig (no network) and validates it at EP = 2. It runs in CPU Unit Test, so the next change to validation.py gets checked against this case.
  4. parallelize_hf_transformers raises when expert_parallel_degree > 1 and the flavor is dense (config.is_moe is False). Only MoE flavors build Titan MoE, so EP would go unused. It reads the config, not model.layers: under PP each stage keeps only its own layers, but a full copy of the config.

Results

CI recipe transformers_backend_moe_fsdp_tp_ep_cp, 2 steps
  main:     ValueError at config load (ConfigLoader re-runs __post_init__)
  this PR:  4x GB300, mesh pp=1 dp_shard=1 cp=2 tp=2 ep=2
            step 1 loss 12.41, step 2 loss 7.31, "Training completed"
Dense debug recipe, expert_parallel_degree = 2, 2x GB300
  main:     ValueError at config load: "has no routed experts" (the core check above)
  this PR:  ValueError at parallelize: "parallelism.expert_parallel_degree > 1 needs an MoE
            flavor (TitanMoeModelConfig), got 2 for a dense model."

You may ask:

  • Why change core and not the experiment? The check is in core. The experiment builds its MoE config at parallelize time on purpose, once EP and sequence parallelism are known (moe_replacement.py).
  • Why is the test in core? It guards a core function. The experiment's own unit tests run in no CI workflow.
  • Why not add torchtitan/config/** to the workflow's path filter? Many core files can break this backend. A CPU test on every PR is cheaper than an 8-GPU job.

NOTE: The GPU run above used 4 GB300s and transformers 5.23. CI uses 8 A10Gs (dp_shard = 2) and transformers 5.9.0. CI runs the 8-GPU workflow on this PR only once it is out of draft.
NOTE: fegin's open draft "[dist_moe] Keep the output postprocess weights on the dense FSDP mesh" edits the same lines of _validate_declared_parallel_axes. It adds the same layouts = _collect_model_layouts(model) line, so whichever lands second needs a small rebase.
NOTE: Two red checks here come from main. CPU Unit Test shows the profiler-close test's 3 failures until its fix lands. lint fails on a pyrefly error in torchtitan/models/kimi_k3/dsa.py until the Kimi K3.1 FLOPs fix lands.

Test plan

pytest tests/unit_tests/cpu/config/test_config_validation.py -k test_hf_moe_config_accepts_ep_gt1
  main:    1 failed (the ValueError above)
  this PR: 1 passed (also with HF_HUB_OFFLINE=1)
pytest tests/unit_tests/cpu/config/ tests/unit_tests/cpu/models/base/test_module.py \
  tests/unit_tests/cpu/distributed/test_expert_parallel.py: 279 passed
NGPU=4 MODULE=torchtitan_recipes.tests.transformers_modeling_backend \
  CONFIG=transformers_backend_moe_fsdp_tp_ep_cp ./run_train.sh --output-dir <dir>
NGPU=2, dense debug recipe with data_parallel_shard_degree = -1, expert_parallel_degree = 2:
  the ValueError above, raised in parallelize_hf_transformers

pre-commit run --files (SKIP=pyrefly-check): all hooks pass. pyrefly check torchtitan/config/validation.py: 0 errors.
Full core CPU suite (pytest tests/unit_tests/cpu) on main 34ac322 + both CI fixes, local CPU: 2099 passed, 7 failed. The same 7 also fail on plain main in this local venv (older torch nightly, no torchao). CI at 34ac322 passed 5 of them and skips the other 2's file.

…outs

The transformers backend builds its MoE and its sharding layouts at
parallelize time, so its config declares no layouts and no MoE.Config
when validate_model_training_config runs. The parallel-axes check added
in fb1d2ba then read EP > 1 on it as "no routed experts" and rejected
every transformers-backend MoE run with expert parallelism.

Return early when the config declares no layouts. Native TorchTitan
model configs all declare layouts, so their checks are unchanged.

A dense transformers-backend model with EP > 1 passes validation again,
as before fb1d2ba. A TODO in parallelize_hf_transformers records the
check that should reject it there.
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 24, 2026
@claude

claude Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 2m 15s —— View job


Verdict: Approve -- please address the items before merging (fill in the human-written summary; the dense-model EP TODO can likely become a 4-line raise). A maintainer still needs to review. See review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: Approve -- please address the items below before merging. The early return changes behavior only for configs that declare no layouts, and the only check it skips for them is the "no routed experts" raise. The PR touches core (torchtitan/config/validation.py), so a maintainer still needs to review it.

What this PR does. _validate_declared_parallel_axes now collects layouts once and returns early when there are none. When a config has no layouts, the per-layout loop was already a no-op. So the only behavior that changes is the EP > 1 "has no routed experts" raise, and that now runs only for configs that declare at least one layout. All native models declare layouts even with every degree at 1, so their checks don't change. The transformers backend declares its layouts at parallelize time, so it bypasses the check.

Example.

HFTransformerModel.Config("debugmodel_moe"), parallelism.expert_parallel_degree=2
  main:    ValueError "HFTransformerModel.Config has no routed experts"  (0 layouts, no MoE.Config)
  this PR: passes; MoE + layouts are built in parallelize_hf_transformers
Flux / llama3 debugmodel, EP=2
  main:    ValueError (32 / 195 layouts declared, no MoE.Config)
  this PR: same ValueError
deepseek_v3 debugmodel built at EP=1, validated at EP=2
  main/PR: ValueError "declares no 'ep' sharding" (test_ep_off_built_moe_rejects_ep_gt1)

Must fix

  1. PR description -- "Summary (human-written)" is empty. The author fills it in; agents leave it alone (CONTRIBUTING.md, AI-assisted contributions).

Should consider

  • torchtitan/experiments/transformers_modeling_backend/parallelize.py:138 -- the TODO says the dense-model EP check can't be done because of PP. But model.model.config.is_moe gives the full-model answer on every PP stage, so the roughly 4-line raise could replace the TODO (see the inline comment).
Details
  • Layer: the check lives in core, and the HF backend builds its MoE and its sharding on purpose once the parallelism context is known (moe_replacement.py). Gating on "nothing declared" is the smallest change that doesn't teach core about the experiment. A core branch on HFTransformerModel.Config would break principle 5.
  • Tradeoff: any future native model that declares no layouts at all also skips the EP check. Today that can't happen: every model in torchtitan/models declares at least 32 layouts with every degree at 1, according to the description.
  • Test: it builds only the config, not the model, and runs in the CPU suite, so it's cheap. It guards the exact regression that went unnoticed because the 8-GPU workflow's path filter left out torchtitan/config/**. Core CPU tests already import experiments (test_embedding.py, test_tokenizer.py), and transformers is in requirements-dev.txt, so the placement and the missing skip guard are fine.
  • Overlap: the description already mentions fegin's draft that edits the same lines.

Not verified: the GPU run (fsdp+tp+ep+cp on 8x A10G with transformers 5.9.0) and the is_moe suggestion under PP.

Comment on lines +138 to +139
# TODO: raise when expert_parallel_degree > 1 and no layer of the full model is MoE:
# a dense model trains with EP unused. Under PP, model.layers holds one stage only.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The reason this TODO gives for deferring the check doesn't seem to hold. model.layers is per stage under PP, but each stage is a deepcopy of the whole model (pipeline.py:198). So model.model.config is the full HFTransformerModel.Config on every stage, and its is_moe (set in Config.__init__, model.py:299) is the full-model answer. It is also the same flag that gates probe_native_moe_params.

Without the check, a dense HF model with EP > 1 trains while ignoring the EP degree. That is the silent fallback principle 8 rules out. The raise is about 4 lines, so it could replace the TODO here:

if parallelism.expert_parallel_degree > 1 and not model.model.config.is_moe:
    raise ValueError(
        "expert_parallel_degree > 1 requires an MoE flavor (TitanMoeModelConfig); "
        f"got {parallelism.expert_parallel_degree} for a dense model."
    )

Unverified: I haven't run this under PP. If is_moe can disagree with the per-layer moe_enabled detection (for example, an MoE HF checkpoint loaded with a dense flavor), please say so in the TODO instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reply from claude: Done in 3b0a828: the TODO is now this raise, reading model.model.config.is_moe. Checked:

  • PP: each stage is a deepcopy of the whole model (pipeline_parallel.py:664), so the config is the full one.
  • is_moe can't disagree in a way that matters: _native_moe_params is set only by probe_native_moe_params, which runs only when config.is_moe (model.py:885), and build_and_swap_native_moe skips layers without it (moe_replacement.py:127). So with is_moe False, no Titan MoE is built and EP would go unused.
  • Dense debug recipe at EP = 2 on 2 GPUs now raises the new ValueError at parallelize (it trained with EP unused before this commit).

@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude review

@claude

claude Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 57s —— View job


Verdict: Approve -- please address the items before merging (fill in the human-written summary; the dense-model EP raise from the last review is resolved). A maintainer still needs to review. See review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: Approve -- please address the item below before merging. The new commit replaces the TODO with a raise, and that raise is correct. The only item left is the empty human-written summary. The PR touches core (torchtitan/config/validation.py), so a maintainer still needs to review it.

Changes since the last review (3b0a828). parallelize_hf_transformers now raises when expert_parallel_degree > 1 and model.model.config.is_moe is False.

Example.

HF dense debugmodel, expert_parallel_degree=2
  main:              ValueError at config load ("has no routed experts", from the core check)
  a62d25c (commit 1): would train with EP unused (the core check is skipped, nothing else raises)
  3b0a828 (HEAD):    ValueError at parallelize ("needs an MoE flavor")
HF debugmodel_moe, expert_parallel_degree=2
  main: ValueError at config load    HEAD: passes; MoE + layouts built in parallelize

Earlier items

  • Resolved: the dense-model EP TODO in parallelize.py. is_moe is set from the flavor type (model.py:299), and it is the same flag that gates probe_native_moe_params (model.py:885). The HF model holds the same config object it was built from (model.py:853), and PP's deepcopy keeps the instance attribute. So every stage sees the full-model answer.
  • Still open: see Must fix.

Must fix

  1. PR description: "Summary (human-written)" is still the template placeholder. The author fills it in; agents leave it alone (CONTRIBUTING.md, AI-assisted contributions).
Details
  • Tradeoff: for HF configs, the "dense model + EP" error moves from config load to parallelize. Principle 8 prefers config time. But only the experiment knows is_moe, and its Config.__post_init__ doesn't see parallelism. Adding an HF-specific branch to core validation would break principle 5. Raising at parallelize, before any MoE swap or sharding, is the earliest point the experiment owns.
  • No new problems in the new hunk.

Not verified: the 8x A10G CI run with transformers 5.9.0.

@felipemello1
felipemello1 marked this pull request as ready for review November 24, 2026 20:12
…parallel_degree > 1

Only MoE flavors build Titan MoE: probe_native_moe_params runs only when
config.is_moe. A dense model with expert_parallel_degree > 1 trained with
EP unused. Raise in parallelize_hf_transformers instead.

The check reads model.model.config.is_moe, not the layers: under PP,
model.layers holds only one stage's layers, while each stage keeps a
deepcopy of the full config.
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 24, 2026
@felipemello1
felipemello1 force-pushed the felipemello1/ci/validate-axes-skip-undeclared branch from 3b0a828 to 0f17083 Compare November 24, 2026 20:12
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @bantianyue, the EP check from #5995 rejects every transformers-backend MoE run with EP > 1, because that backend builds its MoE at parallelize time. So the Transformers Modeling Backend 8 GPU job has been red since 11-18. This PR fixes it. Could you please take a look?

@felipemello1
felipemello1 merged commit bf549af into pytorch:main Nov 24, 2026
15 of 17 checks passed
@bantianyue

Copy link
Copy Markdown
Contributor

Thanks for catching this, and thanks for the fix.

#5995's config-time EP check assumed layouts and MoE.Config were already on the model config. I missed that the transformers modeling backend builds those at parallelize time, so every EP > 1 run on that path failed at config load. Skipping the check when no layouts are declared, and raising for dense HF flavors at parallelize time, looks right to me. Glad this is in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants