Skip to content

Commit 6e1befc

Browse files
[graph_trainer][aot_fx_trace][graph_pp] Add GraphPP runner integration
Add the GraphPP pipeline entry point, GraphPipelineStage wrapper, and GraphPPRunner runtime that executes prebuilt stage-local graph bundles for PyTorch runtime PP schedule actions. Graph construction uses eager PP metadata inference, GraphTrainer tracing, the metadata-preserving compile-time pass pipeline without terminal Inductor compilation, and the GraphPP partition/FSDP/dI-dW extraction passes. Reuse GraphTrainer tracer subclass wrapping for values crossing PP boundaries, keep internal GraphPP calling-convention values flat, preserve trace-time buffer state, and share explicit parameter-gradient accumulation helpers with the non-PP path. OVERLAP_F_B is registered but intentionally unsupported until the multiplex/DualPipeV follow-up commit. Add runner unit coverage for graph construction, forward/backward execution, FSDP unshard/reduce actions, metadata contracts, compile hooks, and supported runtime schedule validation. Add DeepSeek V3 GraphPP numerics and integration coverage for Interleaved1F1B and ZBVZeroBubble. stack-info: PR: #3089, branch: sanketpurandare/stack/8
1 parent 5e43112 commit 6e1befc

16 files changed

Lines changed: 3544 additions & 47 deletions

File tree

torchtitan/experiments/graph_trainer/common_utils.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
import fnmatch
88
import time
9-
from collections.abc import Callable
9+
from collections.abc import Callable, Iterable
1010
from contextlib import contextmanager
11+
from typing import Any
1112

1213
import torch
1314
import torch.nn as nn
@@ -97,6 +98,41 @@ def _maybe_materialize_grad_for_param_layout(
9798
_NOT_IN_LAYERS = -1
9899

99100

101+
def compute_annotated_loss(
102+
loss_fn: Callable[..., torch.Tensor],
103+
pred: Any,
104+
labels: Any,
105+
loss_kwargs: dict[str, Any] | None = None,
106+
) -> torch.Tensor:
107+
"""Compute the loss tensor with the same FX metadata convention as GraphTrainer."""
108+
annotated_loss_fn = annotate_fn({_MODULE_FQN: "loss"})(loss_fn)
109+
result = annotated_loss_fn(pred, labels, **(loss_kwargs or {}))
110+
if isinstance(result, tuple):
111+
if len(result) != 2:
112+
raise ValueError(
113+
"GraphTrainer loss functions must return a loss tensor or "
114+
"(loss tensor, metrics)."
115+
)
116+
loss, _metrics = result
117+
return loss
118+
return result
119+
120+
121+
def accumulate_param_grads_(
122+
params: Iterable[torch.Tensor],
123+
grads: Iterable[torch.Tensor | None],
124+
) -> None:
125+
"""Accumulate explicit graph-produced gradients into live parameters."""
126+
for param, grad in zip(params, grads, strict=True):
127+
if grad is None:
128+
continue
129+
grad = _maybe_materialize_grad_for_param_layout(param, grad)
130+
if param.grad is None:
131+
param.grad = grad
132+
else:
133+
param.grad += grad
134+
135+
100136
def _is_backward_node(node: torch.fx.Node) -> bool:
101137
return node.meta.get("autograd_backward", False)
102138

torchtitan/experiments/graph_trainer/configs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def to_graph_trainer_config(
251251
d["model_spec"] = replace(
252252
base_config.model_spec,
253253
parallelize_fn=graph_spec.parallelize_fn,
254+
pipelining_fn=graph_spec.pipelining_fn,
254255
model=graph_model,
255256
)
256257
d.pop("compile")

torchtitan/experiments/graph_trainer/deepseek_v3/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from dataclasses import fields
88

99
from torchtitan.components.optimizer import register_moe_load_balancing_hook
10-
from torchtitan.distributed.pipeline_parallel import pipeline_llm
10+
from torchtitan.experiments.graph_trainer.graph_pp.pipeline import graph_pp_llm
1111
from torchtitan.models.deepseek_v3 import deepseekv3_configs
1212
from torchtitan.models.deepseek_v3.state_dict_adapter import DeepSeekV3StateDictAdapter
1313
from torchtitan.protocols.model_spec import ModelSpec
@@ -47,7 +47,7 @@ def model_registry(
4747
flavor=flavor,
4848
model=config,
4949
parallelize_fn=_parallelize_fn,
50-
pipelining_fn=pipeline_llm,
50+
pipelining_fn=graph_pp_llm,
5151
post_optimizer_build_fn=register_moe_load_balancing_hook,
5252
state_dict_adapter=DeepSeekV3StateDictAdapter,
5353
)

torchtitan/experiments/graph_trainer/deepseek_v3/config_registry.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
from dataclasses import replace
8+
9+
from torchtitan.distributed.pipeline_parallel import pipeline_llm
710
from torchtitan.experiments.graph_trainer.configs import (
811
GraphTrainerCompileConfig,
912
to_graph_trainer_config,
@@ -26,6 +29,12 @@ def graph_trainer_deepseek_v3_debugmodel() -> GraphTrainer.Config:
2629
return config
2730

2831

32+
def graph_trainer_deepseek_v3_debugmodel_ep() -> GraphTrainer.Config:
33+
config = to_graph_trainer_config(deepseek_v3_debugmodel(), model_registry)
34+
config.compile = GraphTrainerCompileConfig(enable=True)
35+
return config
36+
37+
2938
def graph_trainer_deepseek_v3_debugmodel_hybridep() -> GraphTrainer.Config:
3039
config = to_graph_trainer_config(deepseek_v3_debugmodel(), model_registry)
3140
config.compile = GraphTrainerCompileConfig(enable=True)
@@ -46,6 +55,26 @@ def graph_trainer_deepseek_v3_debugmodel_minimal_async_ep() -> GraphTrainer.Conf
4655
return config
4756

4857

58+
def graph_trainer_deepseek_v3_debugmodel_flex_attn() -> GraphTrainer.Config:
59+
return graph_trainer_deepseek_v3_debugmodel()
60+
61+
62+
def graph_trainer_deepseek_v3_debugmodel_flex_attn_ep() -> GraphTrainer.Config:
63+
return graph_trainer_deepseek_v3_debugmodel_ep()
64+
65+
66+
def graph_trainer_deepseek_v3_debugmodel_eager_pp() -> GraphTrainer.Config:
67+
"""Test-only FlexAttention baseline that runs through eager pipeline parallelism."""
68+
config = graph_trainer_deepseek_v3_debugmodel()
69+
config.compile = GraphTrainerCompileConfig(
70+
enable=True,
71+
components=["loss"],
72+
mode=None,
73+
)
74+
config.model_spec = replace(config.model_spec, pipelining_fn=pipeline_llm)
75+
return config
76+
77+
4978
def graph_trainer_deepseek_v3_16b() -> GraphTrainer.Config:
5079
config = to_graph_trainer_config(deepseek_v3_16b(), model_registry)
5180
config.compile = GraphTrainerCompileConfig(enable=True)

0 commit comments

Comments
 (0)