Skip to content

[CI Fix][GraphTrainer] Avoid materializing parameters during FSDP inspection - #5560

Merged
acisseJZhong merged 2 commits into
mainfrom
codex/fix-dist-moe-mxfp8-contiguous
Oct 20, 2026
Merged

acisseJZhong merged 2 commits into
mainfrom
codex/fix-dist-moe-mxfp8-contiguous

Conversation

@acisseJZhong

@acisseJZhong acisseJZhong commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Inspect parameter shard placements with SimpleFSDP parametrizations disabled.
  • Add a regression test for reapplying data parallel to an already parametrized quantized weight.

Root cause

The GraphTrainer B200 job fails in graph_trainer_dist_moe_mxfp8_fsdp_ep_pp while applying pipeline-stage parallelism: https://github.com/pytorch/torchtitan/actions/runs/39351458272/job/118396337526

data_parallel() computes linear_param_shard_placements(model) before its module loop skips modules that already have SimpleFSDP. On a reused/shared module that GraphPP has already parametrized, reading child.weight during that inspection invokes ReplicateComputation and tries to build the MXFP8 compute operands.

This happens during model construction, while the model is still on the meta device and before to_empty(device=...). Dist-MoE checks both weight.is_cuda and weight.is_contiguous() but reports either failure as ValueError: weight must be contiguous CUDA storage. Here the failed condition is weight.is_cuda == False, not contiguity.

Regression provenance

#4867 introduced the unconditional linear_param_shard_placements(model) scan that reads parametrized child.weight. #4949 later allowed callers to supply placements explicitly, but retained that scan on the default path.

The workflow installs dist_moe@main rather than a pinned revision. The last passing run on October 15 installed dist_moe@6f0c3d4216:

https://github.com/pytorch/torchtitan/actions/runs/38663506034

The first failing run on October 16 installed dist_moe@e3d21b8d2a:

https://github.com/pytorch/torchtitan/actions/runs/38832949153

Between those revisions, meta-pytorch/dist_moe@fa32967a85 added the valid CUDA/contiguous input contract and exposed the latent GraphTrainer bug. That commit has no associated public GitHub PR.

The Dist-MoE check should not be weakened and the dependency should not be pinned back: parameter-layout inspection should never execute the parameter's compute representation in the first place.

Fix

Use GraphTrainer's existing disable_active_parametrization() context while inspecting shard placements. Inspection now sees the raw sharded parameter instead of materializing its compute representation; actual forward parametrization is unchanged.

An initial attempt to call .contiguous() at the Dist-MoE boundary failed with the same error, confirming that the meta device, not storage stride, was the violated contract:

https://github.com/pytorch/torchtitan/actions/runs/39374680382/job/118470042165

Test plan

The shared local environment has an older PyTorch/Pyrefly API set than current main, so the Pyrefly hook is left to CI.

The regular B200 failure on this PR is the independent Kimi K3 golden drift fixed in #5559. Its reported values exactly match #5559's updated golden, and that PR passes the regular B200 workflow: https://github.com/pytorch/torchtitan/actions/runs/39374678479

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 20, 2026
@acisseJZhong acisseJZhong added ciflow/b200 Trigger B200 CI and removed ciflow/rl labels Oct 20, 2026
@acisseJZhong
acisseJZhong force-pushed the codex/fix-dist-moe-mxfp8-contiguous branch from 1372455 to ffb5a79 Compare October 20, 2026 05:37
@acisseJZhong acisseJZhong changed the title [Dist-MoE] Prepare MXFP8 weights from contiguous storage [GraphTrainer] Avoid materializing parameters during FSDP inspection Oct 20, 2026
@acisseJZhong

Copy link
Copy Markdown
Contributor Author

The corrected root-cause fix passes the full GraphTrainer B200 workflow: https://github.com/pytorch/torchtitan/actions/runs/39375868492

In particular, TestDistMoePipelineNumerics no longer tries to materialize the MXFP8 weight while the model is on meta.

@acisseJZhong
acisseJZhong marked this pull request as ready for review October 20, 2026 05:59
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 20, 2026
@acisseJZhong

Copy link
Copy Markdown
Contributor Author

The regular B200 failure on this PR is the independent Kimi K3 golden drift fixed in #5559. Its reported baseline values exactly match #5559's updated golden, and that PR passes the full regular B200 workflow: https://github.com/pytorch/torchtitan/actions/runs/39374678479

The relevant GraphTrainer B200 workflow for this PR passes: https://github.com/pytorch/torchtitan/actions/runs/39375868492

@acisseJZhong
acisseJZhong requested a review from pianpwk October 20, 2026 06:04
@acisseJZhong acisseJZhong changed the title [GraphTrainer] Avoid materializing parameters during FSDP inspection [CI Fix][GraphTrainer] Avoid materializing parameters during FSDP inspection Oct 20, 2026
@tianyu-l

Copy link
Copy Markdown
Contributor

@claude
linear_param_shard_placements is called before parametrization is even applied. Why do we need to wrap with disable_active_parametrization context?

@claude

claude Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @tianyu-l's task in 54s —— View job


Why disable_active_parametrization is needed here

  • Read the PR diff and data_parallel() call flow
  • Find out when parametrization is applied versus when the inspection runs
  • Answer the question

Short answer: You're right about the modules the current data_parallel() call is about to wrap. But model can already contain submodules that an earlier data_parallel() call parametrized. linear_param_shard_placements(model) walks model.modules(), so it reaches those submodules too. It does this before the "SimpleFSDP" in mod.__class__.__name__ skip in the loop at simple_fsdp.py:551.

Where an earlier call wraps a submodule first

apply_simple_fsdp in common_utils.py:609-659 calls data_parallel() more than once on the same tree when EP is on:

  1. If experts_shard_dim == 1, it calls data_parallel(routed_experts.w13, ...) and data_parallel(routed_experts.w2, ...) (L636-651). Neither call passes param_shard_placements. Each call:
    • swaps the module's class for SimpleFSDPGroupedLinear_N;
    • turns weight into a property that returns ReplicateComputation(self._parameters["weight"]) (_register_parametrization, L211-236).
  2. Then it calls data_parallel(model, ...) (L653), again with no param_shard_placements. This runs linear_param_shard_placements(model) (torchtitan/distributed/fsdp.py:71-100), which visits every GroupedLinear, including the w13 that was just wrapped. w13 is stacked (num_linears == 2), so the scan reaches weight = child.weight at L96.

On a wrapped module, reading child.weight doesn't return the stored parameter. It runs ReplicateComputation._forward, which does replicate_compute (the all-gather) and then _BuildUnshardedTensorFunction (L482-506). For an MXFP8 _ShardedFSDPTensor, that builds the quantized compute operands. All of this happens during model construction, while the weights are still on meta. That's where Dist-MoE's weight is not CUDA check fires.

The GraphPP case in the PR description has the same shape: a module that already went through data_parallel gets visited again by a later call over an enclosing tree. The unit test you added triggers it the simplest way, by calling data_parallel(linear, ...) twice on one module.

What the context manager changes

With _active_parametrization = False, ReplicateComputation._forward returns x as is (L479-480). So child.weight gives back the stored sharded parameter, and no collective or quantization runs. The placements recorded for those already-wrapped modules are never used, because the loop skips them anyway. Modules that aren't wrapped yet have no parametrization, so the context changes nothing for them.

A possible cleanup (not a correctness issue)

The fix would read more directly if the scan just left out already-wrapped modules, for example by filtering "SimpleFSDP" in type(child).__name__. However, linear_param_shard_placements lives in core torchtitan/distributed/fsdp.py, and the root CLAUDE.md says not to add experiment-specific logic to core. Reusing GraphTrainer's existing disable_active_parametrization() keeps the fix inside the experiment, so I think the current approach is reasonable. It would help to add a one-line comment above L536, along the lines of "model may contain submodules parametrized by an earlier data_parallel call (e.g. EP experts); read raw params instead of running ReplicateComputation". Without it, the next reader will likely ask the same question.

@tianyu-l tianyu-l 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.

thanks


if param_shard_placements is None:
param_shard_placements = linear_param_shard_placements(model)
with disable_active_parametrization():

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.

please add comments around

It would help to add a one-line comment above L536, along the lines of "model may contain submodules parametrized by an earlier data_parallel call (e.g. EP experts); read raw params instead of running ReplicateComputation".

@acisseJZhong
acisseJZhong merged commit 3b1714a into main Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/b200 Trigger B200 CI 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.

2 participants