Repository navigation
[config] Remove the CP import cycle - #5744
Conversation
|
@claude review the pr |
|
Claude finished @fegin's task in 1m 39s —— View job Verdict: Approve (a maintainer still needs to review): the full review is in the PR conversation, with one optional inline comment on the test. |
There was a problem hiding this comment.
Verdict: Approve -- the cycle is real, the fix removes it at its source (the eager re-export in torchtitan/config/__init__.py), and every in-repo caller is updated. A maintainer still needs to review, since this touches core config.
What this PR does. torchtitan/distributed/context_parallel.py imports torchtitan.config.configurable.Configurable. Importing that submodule runs torchtitan/config/__init__.py first, and before this PR that file eagerly imported .parallelism, which in turn imports ContextParallelLoadBalancer from the half-initialized CP module. The PR removes the ParallelismConfig / FSDPSymmMemScope re-exports and switches 10 test files to torchtitan.config.parallelism. It also adds one fresh-process regression test.
Example.
# fresh interpreter
from torchtitan.distributed.context_parallel import ContextParallelLoadBalancer
# before: context_parallel -> config/__init__ -> config/parallelism -> context_parallel
# -> ImportError (ContextParallelLoadBalancer not yet defined)
# after: config/__init__ only loads configs/configurable/function/loader/override -> OKShould consider
tests/unit_tests/cpu/config/test_parallelism_config.py:23-27-- theget_type_hintsassertion has no in-repo consumer, and the annotation is evaluated eagerly anyway. The two imports alone catch the regression (see inline).
Details
- Callsite audit. No remaining
from torchtitan.config import ParallelismConfig/FSDPSymmMemScopeortorchtitan.config.ParallelismConfigattribute access anywhere intorchtitan/,torchtitan_recipes/,tests/, scripts, or*.mddocs. Recipes and most tests already imported fromtorchtitan.config.parallelism, so this PR also unifies on one import path. - Remaining
config/__init__imports.configs,configurable(->observability.structured_logger),function,loader, andoverridedon't importparallelismordistributed.context_parallel, so this cycle is fully gone. - Alternative. Importing
Configurablefrom a different path incontext_parallel.pywouldn't help, because anytorchtitan.config.*import runs the package__init__. Removing the re-export is the smallest correct fix. - Test cost. One subprocess that imports torch. This guards an import-order invariant that in-process pytest runs can't detect once some other module has already imported
torchtitan.config, so it earns its place.
Not verified: I couldn't run the regression test in this environment (shell execution was not permitted).
| "from torchtitan.config.parallelism import ParallelismConfig\n" | ||
| "from typing import get_type_hints\n" | ||
| "load_balancer_type = get_type_hints(ParallelismConfig)[" | ||
| "'context_parallel_load_balancer']\n" | ||
| "assert load_balancer_type == ContextParallelLoadBalancer.Config | None\n", |
There was a problem hiding this comment.
Optional: nothing in torchtitan/ calls get_type_hints (I grepped for it), and parallelism.py has no from __future__ import annotations. So the field annotation is evaluated when the class is defined, and the get_type_hints assertion adds nothing the import doesn't already check. The regression this test exists to catch is the re-added eager export, and the CP-first import alone catches that. You could cut the subprocess script down to the two imports:
| "from torchtitan.config.parallelism import ParallelismConfig\n" | |
| "from typing import get_type_hints\n" | |
| "load_balancer_type = get_type_hints(ParallelismConfig)[" | |
| "'context_parallel_load_balancer']\n" | |
| "assert load_balancer_type == ContextParallelLoadBalancer.Config | None\n", | |
| "from torchtitan.config.parallelism import ParallelismConfig\n", |
Stacked PRs: * #5746 * #5745 * #5744 * #5743 * __->__#5742 --- --- --- [docs] Clarify PyTorch library ownership ## Summary (human-written) Agents are confused by what "PyTorch libraries" means. They may treat meta-pytorch repos as non-pytorch repo and request changes if we use repos in the core parallelisms, which violates one of the core principal. ## Why The dependency rule does not define PyTorch libraries. This can incorrectly exclude libraries such as meta-pytorch/attention-gym. ## What changed - Count pytorch/pytorch and meta-pytorch repos as PyTorch libraries. - Keep complex techniques upstream, in pytorch/pytorch or a suitable meta-pytorch repo. ## Testing Documentation only. `pre-commit run --files .claude/CLAUDE.md` passes.
Stacked PRs: * #5746 * #5745 * #5744 * __->__#5743 * #5742 --- --- --- [tools] Fix CUDA Graph dependency arrows ## Summary (human-written) Fix a bug of cudagraph dependency arrow. The AI summary shows an example that can cause errors. This PR fixes the issue. ## Why The trace compactor matches GPU dependency arrows to kernel start times. An arrow's source is at the source kernel's end. This can attach the arrow to the next kernel or make compaction fail. ```text Kernel A: [10, 12] Kernel B: [12, 14] At time 12, an arrow source belongs to A and a destination belongs to B. ``` ## What changed - Match GPU arrow sources to slice ends and destinations to slice starts. - Accept one adjacent floating-point value when matching a source timestamp. This handles rounding in timestamp + duration. - Keep errors for endpoints that cannot be matched. ## Numerics / performance This tool processes saved profiler traces. Kernel timestamps and durations are preserved. Training code is outside the change. ## Testing - Six tests pass: `python -m unittest discover -s .claude/skills/cuda_graph_trace_compaction/scripts -p test_compact_cuda_graph_trace.py`. - The regression test checks adjacent kernels, a rounded source timestamp, and preservation of the arrow timestamp. - Saved Muse Glimmer trace validation maps all 4228 and 3372 GPU flow endpoints.
8b87d12 to
ce691ca
Compare
Stacked PRs: * #5746 * #5745 * #5744 * __->__#5771 --- --- --- [tools] Fix CUDA Graph dependency arrows Reland #5743, accidentally land #5743 to an incorrect branch. ## Summary (human-written) Fix a bug of cudagraph dependency arrow. The AI summary shows an example that can cause errors. This PR fixes the issue. ## Why The trace compactor matches GPU dependency arrows to kernel start times. An arrow's source is at the source kernel's end. This can attach the arrow to the next kernel or make compaction fail. ```text Kernel A: [10, 12] Kernel B: [12, 14] At time 12, an arrow source belongs to A and a destination belongs to B. ``` ## What changed - Match GPU arrow sources to slice ends and destinations to slice starts. - Accept one adjacent floating-point value when matching a source timestamp. This handles rounding in timestamp + duration. - Keep errors for endpoints that cannot be matched. ## Numerics / performance This tool processes saved profiler traces. Kernel timestamps and durations are preserved. Training code is outside the change. ## Testing - Six tests pass: `python -m unittest discover -s .claude/skills/cuda_graph_trace_compaction/scripts -p test_compact_cuda_graph_trace.py`. - The regression test checks adjacent kernels, a rounded source timestamp, and preservation of the arrow timestamp. - Saved Muse Glimmer trace validation maps all 4228 and 3372 GPU flow endpoints.
## Summary (human-written) config/__init__.py shouldn't import parallelism, this causes a circular import and this PR remove it. ``` context_parallel -> config/__init__.py -> config/parallelism -> context_parallel ``` ## Why A CP-first import fails on main. CP imports Configurable, but config/__init__.py eagerly imports ParallelismConfig, which reads the unfinished CP module. ```python from torchtitan.distributed.context_parallel import ContextParallelLoadBalancer ``` ## What changed - Remove the eager parallelism exports from config/__init__.py. Keep the direct load-balancer type annotation. - Mechanical: update ten test files to import from config.parallelism. - Add one fresh-process regression test for CP-first imports and runtime type hints. ## User-facing changes | Before | After | | --- | --- | | Import ParallelismConfig or FSDPSymmMemScope from torchtitan.config | Import from torchtitan.config.parallelism | ## Alternatives considered - A type-only CP import avoids the cycle but breaks get_type_hints(ParallelismConfig). - A lazy export adds a compatibility layer that the repo does not require. ## Numerics Imports only. No tensor math or training behavior changes. GPU numerics were not rerun. ## Testing - `python -m pytest -q tests/unit_tests/cpu/config -k 'not test_every_cp_recipe_passes_the_gate'`: 206 pass. - The regression test fails on unmodified main at ff96670 and passes with this fix. - Eight affected test files collect 155 tests. LoRA collection fails on both main and this change because torchvision::nms is missing. - Targeted Pyrefly reports zero errors. All non-typechecking pre-commit hooks pass. - Whole-worktree Pyrefly reports the same 179 errors as before this change. stack-info: PR: #5744, branch: fegin/stack/4
Stacked PRs:
[config] Remove the CP import cycle
Summary (human-written)
config/init.py shouldn't import parallelism, this causes a circular
import and this PR remove it.
Why
A CP-first import fails on main. CP imports Configurable, but config/init.py eagerly imports ParallelismConfig, which reads the unfinished CP module.
What changed
User-facing changes
Alternatives considered
Numerics
Imports only. No tensor math or training behavior changes. GPU numerics were not rerun.
Testing
python -m pytest -q tests/unit_tests/cpu/config -k 'not test_every_cp_recipe_passes_the_gate': 206 pass.