Skip to content

preparing pipeline runtime for MTP+PP - #4486

Open
acisseJZhong wants to merge 2 commits into
mainfrom
mtp-pp-runtime
Open

acisseJZhong wants to merge 2 commits into
mainfrom
mtp-pp-runtime

Conversation

@acisseJZhong

@acisseJZhong acisseJZhong commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR introduces model-owned lifecycle hooks for parameters replicated across
eager pipeline stages. The initial use case is MTP: the first stage owns the
input embedding weight, while the final stage keeps a replica so it can embed
shifted tokens locally. The runtime keeps the two copies synchronized, combines
their gradient contributions, and counts the shared parameter only once when
computing the gradient norm.

This PR contains only the generic infrastructure from D118152394. MTP + PP and
its model-specific stage metadata will follow in a separate stacked PR.

Design

Pipeline builders now return a PipelineResult containing the schedule, local
model parts, global virtual-stage indices, stage ownership flags, and a
PipelineRuntime.

PipelineRuntime follows the null-object pattern. The default implementation
is a no-op, so the Trainer invokes the same hooks unconditionally for non-PP
and ordinary PP jobs. A model-specific pipeline builder can replace it with a
specialized runtime without adding model checks to the Trainer.

The Trainer invokes runtime hooks at four lifecycle boundaries:

after model initialization or checkpoint load
    -> synchronize_parameters()

before scheduling each PP microbatch
    -> prepare_microbatch()

after all backward passes, before clipping and optimization
    -> finalize_gradients()
    -> parameters_for_grad_norm()

Shared Parameters Across Pipeline Stages

PipelineSharedParameterSpec identifies a parameter by FQN and records its
canonical and replica virtual stages. The canonical stage owns the authoritative
value after initialization or checkpoint load; the replica stage holds an
additional copy for its local computation.

SharedParameterPipelineRuntime then:

  • copies the canonical value to the replica after initialization or checkpoint
    load;
  • sums both gradient contributions and stores the result on both copies before
    optimization; and
  • excludes the replica when computing the logical grad norm, while still
    applying the clipping coefficient to both copies.

Same-rank copies use local operations. Cross-rank copies communicate through
an owner-only group, reusing the full PP group when it has exactly two ranks.
DTensor communication operates on corresponding local shards.

Compatibility

  • Existing LLM, VLM, Graph PP, and Hugging Face pipeline builders return the
    structured PipelineResult with the no-op runtime.
  • TorchFT continues to use the no-op runtime and explicitly rejects specialized
    model-owned pipeline hooks that it does not yet execute.
  • Existing non-PP and ordinary PP behavior is unchanged.

Tests

pytest -q \
  tests/unit_tests/cpu/test_pipeline_parallel.py \
  tests/unit_tests/cpu/test_distributed_utils.py \
  tests/unit_tests/cpu/test_trainer.py \
  tests/unit_tests/cpu/test_train_spec.py \
  tests/unit_tests/cpu/test_override.py \
  torchtitan/experiments/graph_trainer/tests/test_graph_pp_runner.py

The tests cover the no-op runtime, same-rank and cross-rank shared parameters,
canonical-source broadcast, gradient summation, owner-group setup, logical
grad-norm filtering, Trainer lifecycle calls, and updated pipeline providers.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 5, 2026
@acisseJZhong acisseJZhong changed the title refactor pipeline runtime for MTP+PP preparing pipeline runtime for MTP+PP Sep 5, 2026
Comment thread torchtitan/trainer.py
num_tokens_per_dp_rank * dp_degree
)
# apply parallelisms and initialization
self.pipeline_runtime = PipelineRuntime()

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.

why initing here if later it's going to be assigned to something else

Comment thread torchtitan/trainer.py

self.model_parts = [model]

self.pipeline_runtime.synchronize_parameters()

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.

confusing


Args:
fqn: Parameter FQN shared by the canonical and replica stage modules.
stage_indices: Stage indices ordered as ``(canonical, replica)``.

@tianyu-l tianyu-l Sep 7, 2026 •

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.

I think this idea of one fqn can be put on multiple PP ranks is legit. But who's canonical, who's replica?

I feel we can generalize the FlexShard Owned placement here, to allow a param to be owned by multiple ranks.

  • if owned by every rank, it's Replicate
  • the gradient should be Partial as they are seeing different data

And the grad norm computation should be handled automatically by the placement itself. I suppose it could also properly handle DCP resharding, which seems not considered in this PR?

return tensor._local_tensor if isinstance(tensor, DTensor) else tensor


class SharedParameterPipelineRuntime(PipelineRuntime):

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.

As mentioned above, I feel this should be handled by tensor placement, rather than pipeline runtime. In particular, I'm not convinced that PipelineRuntime needs to be introduced.

@acisseJZhong
acisseJZhong force-pushed the mtp-pp-runtime branch 2 times, most recently from 5a10154 to 4a6fdec Compare September 10, 2026 04:34
@acisseJZhong
acisseJZhong removed this pull request from stack #4545 September 10, 2026 04:38
@acisseJZhong
acisseJZhong changed the base branch from enable_mtp_cp to labels_into_batch September 10, 2026 04:39

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

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