Skip to content

Test and document parallelism.storage=FlexShardConfig() - #6211

Open
anshul-si wants to merge 5 commits into
gh/anshul-si/29/basefrom
gh/anshul-si/29/head
Open

anshul-si wants to merge 5 commits into
gh/anshul-si/29/basefrom
gh/anshul-si/29/head

Conversation

@anshul-si

@anshul-si anshul-si commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Stacked on #5842, the expert-parallelism PR, the chunked-loss PR, the
checkpointing PR, the Hugging Face load PR, the tensor- and context-parallelism PR,
the DeepSeek V3 PR and the accelerator-graphs PR.

Summary (human-written)

Description

No integration test trains with parallelism.storage=FlexShardConfig(). The
stack's checks are the multi-GPU unit tests in test_flex_shard_fsdp.py, which build
models with model.parallelize, and one-off loss_compare runs. Nothing in CI trains
through torchtitan.train with FlexShard, so a change that breaks it there (config
validation, the training engine, CheckpointManager, the accelerator-graph wrappers),
or breaks its parity with FSDP2 at a recipe's layout, would land unnoticed. FlexShard
also has no user docs beyond the storage docstring. This PR adds three
integration entries and a doc. It changes no training code.

Two entries in the models suite are FlexShard twins of FSDP2 entries that have goldens:
llama3_flex_shard+tp+cp (Llama 3 debug, dp_shard=2, tp=2, cp=2, sequence parallel)
and deepseek_v3_flex_shard+ep (DeepSeek V3 debug without MTP layers, dp_shard=8, ep=8), both with ChunkedLossWrapper and 8 GPUs. Each config is its counterpart's
(llama3_fsdp+tp+cp, deepseek_v3_fsdp+ep) with
storage=FlexShardConfig(), and each entry checks loss and grad norm
against the counterpart's golden, bitwise. So the guarantee the stack makes,
bitwise-identical training from the same weights, is what CI checks at a recipe's
layout, and a numerics change that regenerates an FSDP2 golden has to keep the twin
matching it.

The twins match only if both backends start from the same weights. Since #5842's
rework, FlexShard initializes each shard as its slice of one device's initialization,
while FSDP2's DTensor init draws each shard on its own, so from the same seed they
start from different weights. The runner gives both the same weights only under Real
PG: there, an entry with a golden runs through loss_compare.py, which creates a seed
checkpoint on one device, without sharding, from loss_compare_seed_config, and loads
it before step 1. Each twin's seed config is its counterpart's (llama3_debugmodel
for Llama 3, as llama3_fsdp+tp+cp sets; the FSDP2 config itself for DeepSeek V3,
which sets none), so the twin loads the same seed checkpoint as its counterpart's
golden run. Under Fake PG, the runner passes --no-seed-checkpoint and rejects
checkpointing (validate_fake_pg_compatibility), so each backend would train from its
own initialization and the twin couldn't match the Fake PG golden. The twins
are therefore Real PG only (use_real_pg=True, which marks the tests that need a
checkpoint load): they run in the 8-GPU Real PG job on every ready PR, in its
real_pg_required subset, and in the full suite on main. Real PG also performs the
reductions, which the Fake PG backend skips (it returns each rank's own chunk), so a
reduction bug in FlexShard can't pass there. The earlier revision of this PR ran the
twins under Fake PG too, which relied on FlexShard initializing through DTensor views
of its parameters (as_fsdp2_dtensor), which #5842 deleted. The 1-GPU Fake PG job now
runs no FlexShard test, so it doesn't install flex_shard.

The features suite gets flex_shard_checkpoint (2 GPUs, Real PG), three runs in one
output directory:

  • FSDP2 saves a full checkpoint at step 5 and a float32 Hugging Face export at step
    10. FSDP2 writes both because FlexShard can't save Hugging Face checkpoints yet.
  • FlexShard resumes the step-5 checkpoint and saves a full checkpoint at step 10.
  • A fresh FlexShard run loads the export with initial_load_in_hf, from a checkpoint
    folder of its own, so that it doesn't resume the full checkpoints.
    The configs are the Llama 3 debug recipe's: varlen attention, ChunkedLossWrapper,
    local compile, activation remat, and accelerator graphs, which are on without EP. The
    entry checks that each run completes. The multi-GPU unit tests compare checkpoints and
    Hugging Face loads with FSDP2's bitwise; this entry covers the trainer around them.
    Both FlexShard runs start from checkpoints, so initialization doesn't matter here.
    The runs are an entry of their own, not appended to full_checkpoint and
    model_only_hf_checkpoint, which run on ROCm and in release validation, where
    flex_shard isn't installed.

All three entries skip ROCm, where FlexShard is unvalidated, through
hardware_skips/skipped_tests.toml. Every config passes FlexShard's validation
(FlexShardConfig.validate_model and FlexShardConfig.validate_trainer) at this
commit: none has MTP layers, uses HSDP or PP, or captures graphs under EP.

CI installs flex_shard through a new .github/scripts/install_flex_shard.sh, which
holds the pin, d6bb546, and the --no-deps git install that the multi-GPU unit-test
job had inline (#5842). That job and the 8-GPU Real PG script
(integration_test_real_pg.sh, for CUDA only, since ROCm shares the script) call it.
Release validation (validate_release_gpu.sh) runs the features and models suites
against the RC's torch, 2.15.0, and flex_shard needs ops from 2.16 nightlies
(pytorch#197204). So it doesn't install flex_shard and --excludes the three entries,
with a TODO to drop that once the release torch is 2.16 or later. The 8-GPU Real PG
job's timeout goes from 60 to 70 minutes: its models job took 50 to 58 minutes in the
last five runs on main (November 13 to 15), and the twins add two 8-GPU runs.
FlexShardConfig.validate_model's ImportError for a missing flex_shard now names the
script for the pinned commit, in place of unit_test_gpu.yaml.

The new torchtitan/distributed/FLEX_SHARD_FSDP.md, next to flex_shard_backend.py,
covers how to enable and install FlexShard, how it shards, and the contract: outside
forward and backward, parameters, grads and optimizer states are plain local shards,
whose state dicts declare their layouts through DCP's CheckpointableTensor protocol,
with no DTensor. It explains the initialization difference and how to compare the
backends from the same seed checkpoint, where memory and timing differ from FSDP2,
what's supported, and every rejection in FlexShard's validation with its reason, plus
the two raised elsewhere (graphs under EP, and ChunkedLossWrapper with a frozen
decoder). The name keeps it apart from distributed/flex_shard/README.md, which
documents DistMuon's in-tree package. README.md, distributed/README.md (which also
gains a row for #5842's storage.py) and the storage docstring link
to it, and area_reviewers.yml adds it to the fsdp area. It replaces the README edits
of the FlexShard fork (weifengpy#7, #16, #43), which the
upstreaming plan left for this PR.

The twins haven't run on this stack: they need 8 GPUs, and this machine has 2. The
earlier revision ran them on 8 GB300 GPUs, against their FSDP2 counterparts from the
same seed checkpoint, bitwise, but #5842 and the TP/CP and EP PRs have since reworked
initialization and the grad norm. CI's Real PG job will be their first run. What ran
here, on 2 GB200 GPUs:

  • The doc's example, as written: scripts/loss_compare.py . . --baseline-config llama3_fsdp2 --test-config llama3_flex_shard --assert-equal --metrics loss,grad_norm (Llama 3 debug with flex attention, dp_shard=2, 100 steps,
    accelerator graphs, both runs loading the seed checkpoint that loss_compare
    creates) reports equal loss and grad norm at every step (step-100 loss 2.76104).
    This is the mechanism the twins rely on, through torchtitan.train. With
    --no-seed-checkpoint, as under Fake PG, the step-1 loss already differs (8.11912
    for FSDP2, 8.21909 for FlexShard), and so does the grad norm.
  • flex_shard_checkpoint through the runner's run_single_test, with flex attention
    in place of varlen, because the environment has no FA4: all three runs pass. FSDP2
    saves step-5 and exports/step-10. FlexShard loads step-5, records its
    forward-backward graph and saves step-10. The Hugging Face run loads
    exports/step-10 (step-1 loss 4.08220, against 8.04280 from scratch) and records its
    graph.
  • On CPU, every config of the three entries builds and passes __post_init__, which
    runs FlexShard's validation for the FlexShard ones, and
    validate_fake_pg_compatibility, and both twins' seed configs are FSDP2 configs.
    test_integration_test_definitions.py and test_context_parallel_validation.py pass
    (52 passed, 39 subtests); the latter validates every CP recipe in the suites, the
    Llama 3 twin included.
  • The doc's rejections were checked against FlexShard's validation, and its
    rule for supported models against which model classes resolve _apply_fsdp to
    Decoder._apply_fsdp. pre-commit passes on the changed files.

Not covered end to end: FlexShard's expert buckets on more than one rank (the DeepSeek
V3 twin has edp_shard=1; a twin of gpt_oss_fsdp+tp+ep would cover it for another
8-GPU entry), resuming a FlexShard checkpoint, and FlexShard's own initialization,
since every entry loads a checkpoint. The multi-GPU unit tests check the first and
the last at CI's 4 GPUs, and that FlexShard saves the same checkpoints as FSDP2,
whose checkpoints FlexShard loads.

[ghstack-poisoned]
[ghstack-poisoned]
@anshul-si

Copy link
Copy Markdown
Contributor Author

@claude review this pr

@claude

claude Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @anshul-si's task in 4m 13s —— View job


Request changes -- all three new entries pass the removed skip_rocm_test field, which breaks the features and models suites; see the 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: Request changes -- at HEAD, all three new entries pass skip_rocm_test, a field that #5735 removed in the base. So features.py and models.py fail to build their test lists, and every integration job is red before any FlexShard test runs.

What this PR does. It adds three integration entries:

  • two Real PG FlexShard twins of FSDP2 entries that have goldens (llama3_flex_shard+tp+cp and deepseek_v3_flex_shard+ep), which must match the FSDP2 golden bitwise by loading the same seed checkpoint;
  • a 3-run 2-GPU entry, flex_shard_checkpoint, in which FlexShard resumes an FSDP2 checkpoint and loads FSDP2's HF export.

It moves the flex_shard pin into .github/scripts/install_flex_shard.sh, installs flex_shard in the CUDA Real PG job, excludes the entries from release validation, and adds a user doc, FLEX_SHARD_FSDP.md. It changes no training code.

Example. The PR CI's "required subset" run (--test_scope real_pg_required) picks up the twin because it has use_real_pg=True:

loss_compare.py --baseline-config=llama3_debugmodel_fsdp2_tp2_cp2_flex_shard \
  --seed-config=llama3_debugmodel --import-result=tests/assets/losses/real_pg/a10g/llama3.txt --assert-equal

The seed checkpoint comes from a single device, as for llama3_fsdp+tp+cp, so FlexShard has to reproduce FSDP2's golden bitwise. A regression in FlexShard's reductions or in clip-grad ordering fails here.

Must fix

  1. PR description. The "Summary (human-written)" section is empty, and the author has to write it. The description also says test_integration_test_definitions.py passes, but that run predates the base update that removed skip_rocm_test.
  2. tests/integration_tests/models.py:688, :742 and tests/integration_tests/features.py:970 -- skip_rocm_test no longer exists, so each kwarg raises a TypeError. Move the three names to rocm in tests/integration_tests/hardware_skips/skipped_tests.toml (inline comments).
  3. torchtitan/distributed/flex_shard_fsdp.py:668 (not in the diff) -- the ImportError still points users to .github/workflows/unit_test_gpu.yaml for the pinned commit. This PR moves the pin to .github/scripts/install_flex_shard.sh, so change the message to name that script, as the doc does.

Should consider

  • tests/integration_tests/features.py:284 -- fold the FlexShard resume and HF load into the existing full_checkpoint and model_only_hf_checkpoint entries instead of adding a third checkpoint entry with its own FSDP2 save run (inline).
  • torchtitan/distributed/FLEX_SHARD_FSDP.md:136 -- "mesh dim" should be "mesh axis" (inline).
Details

Gate.

  • Problem: nothing in CI trains with FlexShardConfig() through torchtitan.train. The gap is real.
  • Layer: tests, CI and docs only.
  • Dependency: flex_shard is a meta-pytorch repo, which .claude/CLAUDE.md allows.
  • Smallest alternative: the twins are the cheapest way to check bitwise parity at a recipe layout. Turning the FSDP2 entries over to FlexShard would lose FSDP2's golden coverage.
  • Precedent: sharing a golden between two entries already exists (llama3_fsdp+tp+cp+region_ac shares llama3.txt).

Twins.

  • The seed config is right for both twins. loss_compare.py:1171 defaults the seed config to the baseline config, so deepseek_v3_fsdp+ep's seed is deepseek_v3_debugmodel_fsdp8_ep8, which the twin passes explicitly.
  • _filter_tests skips use_real_pg entries under Fake PG (run_tests.py:403), so the 1-GPU job doesn't need flex_shard.
  • The Real PG matrix is nightly-only (set-matrix.yaml:75), which meets flex_shard's requirement of a 2.16 nightly.

flex_shard_checkpoint.

  • Run 1's last save goes to checkpoint/exports/step-10 (model-only, HF), so run 2 resumes the full checkpoint checkpoint/step-5.
  • Run 3 gets a folder of its own, so it doesn't resume those checkpoints. That's correct.

Not verified: that the twins match the FSDP2 goldens at HEAD. They haven't run on this stack, and CI's Real PG job will be their first run.

Comment thread tests/integration_tests/models.py Resolved
Comment thread tests/integration_tests/models.py Outdated
),
loss_compare_seed_config=deepseek_v3_debugmodel_fsdp8_ep8,
use_real_pg=True,
skip_rocm_test=True,

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.

This is the same removed skip_rocm_test field as at line 688, and it raises a TypeError the same way. Move it to hardware_skips/skipped_tests.toml.

Suggested change
skip_rocm_test=True,

Comment thread tests/integration_tests/features.py Resolved
Comment thread tests/integration_tests/features.py Resolved
[ghstack-poisoned]
@anshul-si

Copy link
Copy Markdown
Contributor Author

@claude review this pr

@claude

claude Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @anshul-si's task in 2m 25s —— View job


Approve -- the earlier blockers are fixed at HEAD; only the empty human-written summary remains; see the 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 earlier blockers are fixed at HEAD. The three entries now skip ROCm through skipped_tests.toml, and the ImportError names install_flex_shard.sh. What's left is the author's summary. A maintainer (weifengpy, fsdp area) still needs to review.

What this PR does. It adds three Real PG integration entries for fsdp_backend=FlexShardConfig():

  • llama3_flex_shard+tp+cp and deepseek_v3_flex_shard+ep are twins of FSDP2 entries. Each loads its counterpart's seed checkpoint and has to reproduce that counterpart's golden bitwise.
  • flex_shard_checkpoint runs on 2 GPUs. FlexShard resumes an FSDP2 checkpoint and then loads FSDP2's HF export.

It also moves the flex_shard pin into one script, which the multi-GPU unit job and the CUDA Real PG job call, excludes the entries from release validation (torch 2.15), and adds FLEX_SHARD_FSDP.md.

Example. With TEST_SCOPE=real_pg_required on a ready PR, the runner picks up the twin and runs:

loss_compare.py --baseline-config=llama3_debugmodel_fsdp2_tp2_cp2_flex_shard \
  --seed-config=llama3_debugmodel \
  --import-result=tests/assets/losses/real_pg/a10g/llama3.txt --assert-equal

Before this PR, a FlexShard regression in reductions, grad clipping or CheckpointManager had no torchtitan.train coverage. Now that run fails on it.

Must fix

  1. PR description. The "Summary (human-written)" section is still empty, and only the author can write it. The CPU check it reports (test_integration_test_definitions.py, 52 passed) also ran before the skip_rocm_test fix, so rerun it and update the numbers.

Status of the earlier review

  • skip_rocm_test kwargs (models.py, features.py): resolved. The names are now under rocm in skipped_tests.toml.
  • The ImportError pointing at unit_test_gpu.yaml: resolved (flex_shard_fsdp.py:668).
  • "mesh dim" in the doc: resolved.
  • Folding into full_checkpoint / model_only_hf_checkpoint: the author's reason holds. Those entries run on ROCm and in release validation, and skipped_tests.toml and --exclude work on whole entries, so a separate entry is the only way to skip just the FlexShard runs.
Details
  • Install coverage. integration_test_real_pg.sh installs flex_shard only for cuda. ROCm skips all three entries through the toml, and the XPU job isn't wired up yet. Fake PG skips use_real_pg entries (run_tests.py:403). The H100 and B200 workflows run their own suites, not features or models. The CUDA Real PG matrix uses the nightly cu132 index with no pinned torch (set-matrix.yaml:75-76), which meets flex_shard's 2.16-nightly requirement.
  • Validation. None of the FlexShard configs sets spmd_typechecking, HSDP, PP, MTP or last_save_in_hf. llama3_debugmodel_checkpoint_and_hf_save inherits typechecking=True and last_save_in_hf, but it's the FSDP2 run, so validate_flex_shard_training doesn't apply to it.
  • flex_shard_checkpoint. Run 1's last save is model-only HF, written to checkpoint/exports/step-10, so run 2 resumes the full checkpoint/step-5. Run 3 uses folder="hf_load" with load_only=True. initial_load_model_only defaults to True (checkpointer/base.py:650), so run 3 loads only the model from the export.
  • Doc. The rejection table matches validate_flex_shard_training (flex_shard_fsdp.py:536-656) row by row, and the links (FSDP.md, ACCELERATOR_GRAPH.md, flex_shard/README.md) resolve.
  • Shared golden. In export mode, a twin writes to the same result path as its counterpart, so whichever runs last wins. That matches the existing precedent (llama3_fsdp+tp+cp+region_ac shares llama3.txt), and since the twins must be bitwise identical, it's harmless.

Not verified: that the twins match the FSDP2 goldens at HEAD. They haven't run on this stack, so CI's 8-GPU Real PG job is their first run. I couldn't run the CPU definition tests in this environment either.

@anshul-si
anshul-si marked this pull request as ready for review November 19, 2026 07:39
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 19, 2026
[ghstack-poisoned]
@anshul-si anshul-si changed the title Test and document parallelism.fsdp_backend=FlexShardConfig() Test and document parallelism.fsdp_backend=FlexShardBackend.Config() Nov 25, 2026
[ghstack-poisoned]
@anshul-si anshul-si changed the title Test and document parallelism.fsdp_backend=FlexShardBackend.Config() Test and document parallelism.storage=FlexShardConfig() Nov 30, 2026

This branch has not been deployed

No deployments
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.

1 participant