Skip to content

[config] Remove the CP import cycle - #5744

Merged
fegin merged 1 commit into
mainfrom
fegin/stack/4
Nov 3, 2026
Merged

fegin merged 1 commit into
mainfrom
fegin/stack/4

Conversation

@fegin

@fegin fegin commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

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.

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.

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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 2, 2026
@fegin
fegin marked this pull request as ready for review November 2, 2026 20:36
@fegin
fegin requested a review from tianyu-l as a code owner November 2, 2026 20:36
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 2, 2026
@fegin

fegin commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@claude review the pr

@fegin
fegin requested a review from felipemello1 as a code owner November 2, 2026 20:36
@claude

claude Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

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.

@claude claude Bot 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.

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 -> OK

Should consider

  • tests/unit_tests/cpu/config/test_parallelism_config.py:23-27 -- the get_type_hints assertion 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/FSDPSymmMemScope or torchtitan.config.ParallelismConfig attribute access anywhere in torchtitan/, torchtitan_recipes/, tests/, scripts, or *.md docs. Recipes and most tests already imported from torchtitan.config.parallelism, so this PR also unifies on one import path.
  • Remaining config/__init__ imports. configs, configurable (-> observability.structured_logger), function, loader, and override don't import parallelism or distributed.context_parallel, so this cycle is fully gone.
  • Alternative. Importing Configurable from a different path in context_parallel.py wouldn't help, because any torchtitan.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).

Comment on lines +23 to +27
"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",

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.

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:

Suggested change
"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",

@fegin
fegin marked this pull request as draft November 2, 2026 20:44
@fegin
fegin changed the base branch from fegin/stack/3 to main November 2, 2026 20:44
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 2, 2026
@fegin
fegin changed the base branch from main to fegin/stack/3 November 2, 2026 20:45
@fegin
fegin marked this pull request as ready for review November 2, 2026 20:45
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 2, 2026
fegin added a commit that referenced this pull request Nov 3, 2026
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.
fegin added a commit that referenced this pull request Nov 3, 2026
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.
@fegin
fegin marked this pull request as draft November 3, 2026 08:07
@fegin
fegin changed the base branch from fegin/stack/3 to main November 3, 2026 08:07
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 3, 2026
@fegin
fegin force-pushed the fegin/stack/4 branch 2 times, most recently from 8b87d12 to ce691ca Compare November 3, 2026 08:07
@fegin
fegin changed the base branch from main to fegin/stack/7 November 3, 2026 08:07
@fegin
fegin marked this pull request as ready for review November 3, 2026 08:08
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 3, 2026
fegin added a commit that referenced this pull request Nov 3, 2026
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
@fegin
fegin marked this pull request as draft November 3, 2026 17:30
@fegin
fegin changed the base branch from fegin/stack/7 to main November 3, 2026 17:30
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 3, 2026
@fegin
fegin marked this pull request as ready for review November 3, 2026 17:30
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 3, 2026
@fegin
fegin merged commit e7cc98a into main Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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