Skip to content

[config] Centralize built-in transform relations - #5007

Merged
jinsooihm merged 4 commits into
pytorch:mainfrom
jinsooihm:transform-graph
Oct 6, 2026
Merged

jinsooihm merged 4 commits into
pytorch:mainfrom
jinsooihm:transform-graph

Conversation

@jinsooihm

Copy link
Copy Markdown
Contributor

Summary

Consolidate run_after and conflicts_with relation into one file, rather than scattered for each transform.

This makes the import logic simpler and also less brain power to reason about composability.

run_after is modeled by a direct graph PRECEDES with an edge (a, b) meaning a must be applied before b.

conflicts_with is modeled by an undirected graph CONFLICTS with an edge {a, b} meaning a and b cannot be applied together.

PRECEDES does not necessarily have to be a DAG (although I would believe a set of reasonable transforms would keep it a DAG), but only the induced subgraph on the user chosen valid set of transforms without conflict should be a DAG.

Not sure about this part but Codex insists

External users who implement their own transform outside of torchtitan library should still be able to specify these relations,
so the class owned run_after and conflicts_with should be preserved?

@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 1, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 1, 2026
Comment thread torchtitan/config/transform/README.md Outdated
@dataclass(kw_only=True, slots=True)
class MyTransform(ModelConfigTransform):
run_after = (QuantizationTransform,)
run_after = (ExternalPrerequisiteTransform,)

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.

oh I didn't think of external transform before.

while I don't know if people outside repo would use transform, or if they use do we have to maintain this mechanism

  • I think having two different ways to check run_after / conflicts_with sounds confusing.
  • Does it make sense to have an API to let people register edges in the two graphs?

@fegin

fegin commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

I agree @tianyu-l 's point. We don't need to preserve the per-class run_after and conflict_with. The bare minimum is that we can allow users to modify the tuples by providing some helper.

@jinsooihm
jinsooihm marked this pull request as draft October 2, 2026 21:47
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 2, 2026
@jinsooihm
jinsooihm force-pushed the transform-graph branch 4 times, most recently from 6571b64 to d48c7b9 Compare October 2, 2026 22:19
@jinsooihm
jinsooihm marked this pull request as ready for review October 2, 2026 22:20
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 2, 2026
@jinsooihm

Copy link
Copy Markdown
Contributor Author

@fegin @tianyu-l I removed the class owned run_after and conflicts_with and added a capability for the user to modify the relation graph and pass it into apply_transform.

Comment thread torchtitan/config/transform/README.md Outdated
Comment on lines +151 to +154
default_relations.add_precedence(
before=ExternalPrerequisiteTransform,
after=MyTransform,
)

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 don't think we need this default_relations at all

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.

I agree. No one really needs to use it.

Comment thread torchtitan/config/transform/apply.py Outdated
transforms: list[ModelConfigTransform],
*,
context: ModelConfigTransformContext,
relations: TransformRelations = default_relations,

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.

default can just be

Suggested change
relations: TransformRelations = default_relations,
relations: TransformRelations = TransformRelations(),

or

Suggested change
relations: TransformRelations = default_relations,
relations: TransformRelations = None,

@jinsooihm jinsooihm mentioned this pull request Oct 3, 2026
@pytorch-bot pytorch-bot Bot added the ciflow/b200 Trigger B200 CI label Oct 5, 2026
@jinsooihm
jinsooihm force-pushed the transform-graph branch 2 times, most recently from 733a84e to adcc65b Compare October 5, 2026 05:51
@jinsooihm

Copy link
Copy Markdown
Contributor Author

@fegin @tianyu-l I made some change for the modules to be able to declare their own relations because

  • dist_moe is an optional package, so importing DistMoeTransform in relations.py would not work
  • But DistMoeTransform is a Torchtitan provided transform, so making the user to declare the relation every time it is used would be awkward.

@tianyu-l

tianyu-l commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

@jinsooihm I feel there must be a way that we could define the graph relation with DisMoETransform, without actually importing dist_moe library, until actual config / class is used?

@jinsooihm

Copy link
Copy Markdown
Contributor Author

@tianyu-l To also handle @4995, I will first try to make dist_moe import lazily so that it can be referred.

@fegin

fegin commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Manual lazy init is ugly and easy to forget. Let's create a mechanism in TorchTitan to handle this. One way to work is

try:
    from dist_moe import DistMoe
except ImportError:
    class DistMoE:
           def __init__(self):
                   raise NotSupportedError

jinsooihm added a commit that referenced this pull request Oct 5, 2026
The Dist-MoE integration imported the standalone backend while defining
its module and config types. This made otherwise generic TorchTitan
imports fail once another feature referenced the integration classes.
Especially problematic for defining relations for transforms #4995,
#5007.

Route backend symbol access through a cached lazy module. Postponed
annotations and deferred MXFP8 defaults keep the integration importable,
while selecting Dist-MoE still reports the focused installation error
and preserves failures from dependencies inside an installed backend.

Test Plan: taskset -c 0-7 pytest
tests/unit_tests/cpu/test_optional_imports.py
tests/unit_tests/cpu/test_transforms.py
tests/unit_tests/cpu/test_dist_moe.py -q -k 'not
runtime_passes_per_slot_capacity_to_annex and not
dist_moe_671b_recipe_topology'

Test Plan: env SKIP=pyrefly-check pre-commit run --files
tests/unit_tests/cpu/test_optional_imports.py
torchtitan/models/common/dist_moe/_optional.py
torchtitan/models/common/dist_moe/mxfp8.py
torchtitan/models/common/dist_moe/routed_experts.py
torchtitan/models/common/dist_moe/runtime.py
torchtitan/config/transform/dist_moe.py
jinsooihm added a commit that referenced this pull request Oct 5, 2026
Adds support for LoRA BF16 DistMoE.

`DistMoeRoutedExperts._weight_operands` returns the weights matrices
`w13_EFD, w2_EDF` used to make the DistMoE kernel call.
LoRA overrides `_weight_operands` to add the LoRA matrices to `w13_EFD,
w2_EDF` before returning.

This does not support MXFP8 DistMoE. It would be quite tricky to
implement.

## Some caveats
- `DistMoeTransform -> LoRATransform (for GroupedLinear w13, w2)` would
not error but result in no op. Would it be worth to error out by
checking the parent? It wouldn't be too clean.
- In `transform/lora.py`, `LoRATransform.run_after` registers
`DistMoeRoutedExpert` which requires importing `dist_moe` which is an
optional package dependency. Similar for `common/lora.py`
- Should we just set `LoRATransform.run_after` inside
`transform/dist_moe.py` and move the DistMoE LoRA to DistMoE files?
    - This also raises a question on how to do #5007 nicely.

## Test Plan
One node, 4 GB300 GPU DP shard 4, EP 2, LoRA rank 8, alpha 16
Deepseek v3 debug model on DistMoE LoRA compared to LoRA on
`GroupedLinear` on `w13, w2` with all to all backend.

| Metric | Step 1 | Step 100 | Mean absolute difference | RMSE | Maximum
absolute difference |
|---|---:|---:|---:|---:|---:|
| Loss, all-to-all | 8.217248 | 4.354145 | — | — | — |
| Loss, DistMoE | 8.217256 | 4.347911 | 0.002922 | 0.005632 | 0.030035 |
| Grad norm, all-to-all | 0.00892750 | 0.12869738 | — | — | — |
| Grad norm, DistMoE | 0.00892772 | 0.12781706 | 0.005116 | 0.013849 |
0.098999 |

<img width="1120" height="409" alt="Screenshot 2026-10-03 at 3 46 52 PM"
src="https://github.com/user-attachments/assets/3aa077ce-f666-424e-ba3e-6ef818aa8459"
/>
Summary: Consolidate built-in transform ordering and conflict policy in a central relation graph while retaining additive class-local hooks for external transforms.

Test Plan: Relevant CPU tests passed (100 tests); exact 8-GPU loss and grad-norm parity matched for 10 steps.
@jinsooihm
jinsooihm merged commit e194679 into pytorch:main Oct 6, 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.

3 participants