Skip to content

[dist_moe] Defer optional backend imports - #5066

Merged
jinsooihm merged 2 commits into
pytorch:mainfrom
jinsooihm:dist-moe-import
Oct 5, 2026
Merged

jinsooihm merged 2 commits into
pytorch:mainfrom
jinsooihm:dist-moe-import

Conversation

@jinsooihm

Copy link
Copy Markdown
Contributor

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

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.

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
@pytorch-bot pytorch-bot Bot added ciflow/b200 Trigger B200 CI ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Oct 5, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 5, 2026

@fegin fegin 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.

Will the type checking work on a machine without dist_moe? For example, the type checking for lora.py

@@ -0,0 +1,40 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

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.

Merge this file into dist_moe/__init__.py.

@jinsooihm

Copy link
Copy Markdown
Contributor Author

@fegin lora.py only refers to things exposed by common/dist_moe/__init__.py so it should work

Keep the optional backend proxy next to the Dist-MoE package exports. Define it before the intentionally late child-module imports so those modules can safely read the proxy from the partially initialized package.

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 torchtitan/models/common/dist_moe/__init__.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
jinsooihm merged commit 88759f1 into pytorch:main Oct 5, 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 ciflow/rl 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