Skip to content

[CooR] Drop the device index from the fx graph cache key under compile_on_one_rank - #196153

Closed
aorenste wants to merge 23 commits into
gh/aorenste/382/basefrom
gh/aorenste/382/head
Closed

aorenste wants to merge 23 commits into
gh/aorenste/382/basefrom
gh/aorenste/382/head

Conversation

@aorenste

@aorenste aorenste commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Every rank compiled the same graph and then missed on every other rank's cache
entry, so no compiled artifact was ever shared. The index reaches the key through
exactly one component -- example_inputs' TensorMetadata:

[lfqb4cez...] example_inputs[0]: TensorMetadata(..., device=device(type='cuda', index=0), ...)

Nothing else contributes one. In particular the graph hashed alongside it does
not: the key is built in _compile_fx_inner before _recursive_post_grad_passes
runs, so it sees the AOT graph, where devices are still current_device() nodes.
Measured on a 16-GPU CooR job, 4730 device-bearing key lines per rank, all of
them example_inputs, and zero from gm.

Strip it in extract_tensor_metadata_for_cache_key, whose stated job is removing
fields not needed for caching, and which already does this for storage_offset
and storage_bytes. Gated on the accelerator being the current one, so cpu
tensors and non-CooR compiles keep their index -- outside CooR several devices
can legitimately be live and must not collide.

The full key is now byte-identical across devices under CooR, and still
correctly distinct without it.

test_inductor_compiled_on_one_device_runs_on_another asserted a cache miss as
a deliberate tripwire for this change -- its comment asked that landing a
device-agnostic key trip the test rather than silently widen it. It now asserts
the hit.

That comment also flagged CompiledFxGraph.device_idxs as a second requirement,
and it is one: cudagraphify takes its device_index from the loaded artifact, so a
cuda:1 rank hitting rank 0's entry warmed up and captured under cuda:0's device,
stream and pool, and crashed with CUDA driver error: invalid argument. Under
CooR the artifact now records no index, and CompiledFxGraph.device_index()
resolves it to the current device at load time -- the same single-accelerator
invariant the key change relies on. Outside CooR the recorded index is kept, since
the current device need not match the graph's there. The alternative, keeping
the stored index and overriding it on load, would leave a wrong value in the
artifact for the next reader to trip over.

extract_tensor_metadata_for_cache_key also feeds the AOTAutogradCache key (and
graph_region_tracker), so that key becomes rank-agnostic under CooR too, which is
what cross-rank reuse needs.

Test Plan:

TORCHINDUCTOR_COMPILE_THREADS=1 python test/distributed/tensor/test_compile_on_one_rank.py
python test/inductor/test_cudagraph_trees.py

test_cudagraphs_reuses_artifact_across_devices (both graph_partition settings)
fails with the invalid-argument error without the output_code.py change and
passes with it.

This PR was authored with the help of an AI assistant.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/196153

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 56d828d with merge base 13d3489 (image):
💚 Looks good so far! There are no failures yet. 💚

🟡 GREEN LIGHT - OUTDATED (earlier commit) - PR requires human review:

  • Triviality
    • Changes what the FX graph cache key encodes: extract_tensor_metadata_for_cache_key in torch/_inductor/codecache.py drops the device index under CooR
    • A cache key outlives the change and is shared across ranks, so this is outside every trivial class
  • Correctness needs a human
    • Lets one rank reuse another rank's compiled artifact, which needs a check that nothing in the cached CompiledFxGraph still holds the compiling rank's device index
    • The PR body says reuse was verified by hand; the diff has no code change to back that up
  • Tests
    • test_inductor_compiled_on_one_device_runs_on_another flips its deliberate cache-miss tripwire to assert a hit, which a reviewer should confirm is the intended landing
    • New tests in test/distributed/tensor/test_compile_on_one_rank.py cover the gating on CooR, device index, and cpu

reason: not_trivial
Reviewed commit: a080bf7 (NOT the current head 56d828d)

Inference job

🐛 Report a wrong verdict

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
@aorenste aorenste added the topic: not user facing topic category label Sep 6, 2026
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
aorenste added a commit that referenced this pull request Oct 29, 2026
…e_on_one_rank

Every rank compiled the same graph and then missed on every other rank's cache
entry, so no compiled artifact was ever shared. The index reaches the key through
exactly one component -- example_inputs' TensorMetadata:

  [lfqb4cez...] example_inputs[0]: TensorMetadata(..., device=device(type='cuda', index=0), ...)

Nothing else contributes one. In particular the graph hashed alongside it does
not: the key is built in _compile_fx_inner before _recursive_post_grad_passes
runs, so it sees the AOT graph, where devices are still current_device() nodes.
Measured on a 16-GPU CooR job, 4730 device-bearing key lines per rank, all of
them example_inputs, and zero from gm.

Strip it in extract_tensor_metadata_for_cache_key, whose stated job is removing
fields not needed for caching, and which already does this for storage_offset
and storage_bytes. Gated on the accelerator being the current one, so cpu
tensors and non-CooR compiles keep their index -- outside CooR several devices
can legitimately be live and must not collide.

The full key is now byte-identical across devices under CooR, and still
correctly distinct without it.

test_inductor_compiled_on_one_device_runs_on_another asserted a cache *miss* as
a deliberate tripwire for this change -- its comment asked that landing a
device-agnostic key trip the test rather than silently widen it. It now asserts
the hit.

That comment also flagged CompiledFxGraph.device_idxs as a second requirement,
and it is one: cudagraphify takes its device_index from the loaded artifact, so a
cuda:1 rank hitting rank 0's entry warmed up and captured under cuda:0's device,
stream and pool, and crashed with `CUDA driver error: invalid argument`. Under
CooR the artifact now records no index, and CompiledFxGraph.device_index()
resolves it to the current device at load time -- the same single-accelerator
invariant the key change relies on. Outside CooR the recorded index is kept, since
the current device need not match the graph's there. The alternative, keeping
the stored index and overriding it on load, would leave a wrong value in the
artifact for the next reader to trip over.

extract_tensor_metadata_for_cache_key also feeds the AOTAutogradCache key (and
graph_region_tracker), so that key becomes rank-agnostic under CooR too, which is
what cross-rank reuse needs.

Test Plan:

```
TORCHINDUCTOR_COMPILE_THREADS=1 python test/distributed/tensor/test_compile_on_one_rank.py
python test/inductor/test_cudagraph_trees.py
```

test_cudagraphs_reuses_artifact_across_devices (both graph_partition settings)
fails with the invalid-argument error without the output_code.py change and
passes with it.

This PR was authored with the help of an AI assistant.

ghstack-source-id: a521845
Pull-Request: #196153
@pytorchgreenlight pytorchgreenlight Bot added the GL/superseded GreenLight review is superseded by changes label Oct 29, 2026
@aorenste

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 STARTUP failures reported, please check workflows syntax! trunk

Details for Dev Infra team

Raised by workflow job

@aorenste

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 STARTUP failures reported, please check workflows syntax! trunk

Details for Dev Infra team

Raised by workflow job

[ghstack-poisoned]
@aorenste

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: HTTP Error 504: Gateway Timeout

Details for Dev Infra team

Raised by workflow job

@aorenste

aorenste commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

pytorchmergebot pushed a commit that referenced this pull request Nov 1, 2026
…compile_on_one_rank (#196154)

InputWriter records each input storage's device, so fx_graph_runnable comes out
different on every rank: 3568 of the 3716 lines differing between two ranks of a
16-GPU job were device(type='cuda', index=N). It also makes a repro produced by
rank 11 unrunnable anywhere with fewer than 12 GPUs.

Under compile_on_one_rank the inputs are on the current accelerator by invariant,
so writing the index records nothing that is true of any other rank. Drop it and
let the reader place the storage on whatever device is current: the repro replays
the same compile, matches across ranks, and runs on a box with fewer GPUs than
the rank that produced it. This extends the defaulting the writer already does --
there is a TODO there observing that defaulting to cpu is unhelpful when the
repros that matter are CUDA.

Gated, so a non-CooR repro still names its device: several accelerators can be
live at once there and the repro has to say which one it used.

Pull Request resolved: #196154
Approved by: https://github.com/bobrenjc93
ghstack dependencies: #196153
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…e_on_one_rank (pytorch#196153)

Every rank compiled the same graph and then missed on every other rank's cache
entry, so no compiled artifact was ever shared. The index reaches the key through
exactly one component -- example_inputs' TensorMetadata:

  [lfqb4cez...] example_inputs[0]: TensorMetadata(..., device=device(type='cuda', index=0), ...)

Nothing else contributes one. In particular the graph hashed alongside it does
not: the key is built in _compile_fx_inner before _recursive_post_grad_passes
runs, so it sees the AOT graph, where devices are still current_device() nodes.
Measured on a 16-GPU CooR job, 4730 device-bearing key lines per rank, all of
them example_inputs, and zero from gm.

Strip it in extract_tensor_metadata_for_cache_key, whose stated job is removing
fields not needed for caching, and which already does this for storage_offset
and storage_bytes. Gated on the accelerator being the current one, so cpu
tensors and non-CooR compiles keep their index -- outside CooR several devices
can legitimately be live and must not collide.

The full key is now byte-identical across devices under CooR, and still
correctly distinct without it.

test_inductor_compiled_on_one_device_runs_on_another asserted a cache *miss* as
a deliberate tripwire for this change -- its comment asked that landing a
device-agnostic key trip the test rather than silently widen it. It now asserts
the hit.

That comment also flagged CompiledFxGraph.device_idxs as a second requirement,
and it is one: cudagraphify takes its device_index from the loaded artifact, so a
cuda:1 rank hitting rank 0's entry warmed up and captured under cuda:0's device,
stream and pool, and crashed with `CUDA driver error: invalid argument`. Under
CooR the artifact now records no index, and CompiledFxGraph.device_index()
resolves it to the current device at load time -- the same single-accelerator
invariant the key change relies on. Outside CooR the recorded index is kept, since
the current device need not match the graph's there. The alternative, keeping
the stored index and overriding it on load, would leave a wrong value in the
artifact for the next reader to trip over.

extract_tensor_metadata_for_cache_key also feeds the AOTAutogradCache key (and
graph_region_tracker), so that key becomes rank-agnostic under CooR too, which is
what cross-rank reuse needs.

Test Plan:

```
TORCHINDUCTOR_COMPILE_THREADS=1 python test/distributed/tensor/test_compile_on_one_rank.py
python test/inductor/test_cudagraph_trees.py
```

test_cudagraphs_reuses_artifact_across_devices (both graph_partition settings)
fails with the invalid-argument error without the output_code.py change and
passes with it.

This PR was authored with the help of an AI assistant.

Pull Request resolved: pytorch#196153
Approved by: https://github.com/bobrenjc93
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…compile_on_one_rank (pytorch#196154)

InputWriter records each input storage's device, so fx_graph_runnable comes out
different on every rank: 3568 of the 3716 lines differing between two ranks of a
16-GPU job were device(type='cuda', index=N). It also makes a repro produced by
rank 11 unrunnable anywhere with fewer than 12 GPUs.

Under compile_on_one_rank the inputs are on the current accelerator by invariant,
so writing the index records nothing that is true of any other rank. Drop it and
let the reader place the storage on whatever device is current: the repro replays
the same compile, matches across ranks, and runs on a box with fewer GPUs than
the rank that produced it. This extends the defaulting the writer already does --
there is a TODO there observing that defaulting to cpu is unhelpful when the
repros that matter are CUDA.

Gated, so a non-CooR repro still names its device: several accelerators can be
live at once there and the repro has to say which one it used.

Pull Request resolved: pytorch#196154
Approved by: https://github.com/bobrenjc93
ghstack dependencies: pytorch#196153
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…e_on_one_rank (pytorch#196153)

Every rank compiled the same graph and then missed on every other rank's cache
entry, so no compiled artifact was ever shared. The index reaches the key through
exactly one component -- example_inputs' TensorMetadata:

  [lfqb4cez...] example_inputs[0]: TensorMetadata(..., device=device(type='cuda', index=0), ...)

Nothing else contributes one. In particular the graph hashed alongside it does
not: the key is built in _compile_fx_inner before _recursive_post_grad_passes
runs, so it sees the AOT graph, where devices are still current_device() nodes.
Measured on a 16-GPU CooR job, 4730 device-bearing key lines per rank, all of
them example_inputs, and zero from gm.

Strip it in extract_tensor_metadata_for_cache_key, whose stated job is removing
fields not needed for caching, and which already does this for storage_offset
and storage_bytes. Gated on the accelerator being the current one, so cpu
tensors and non-CooR compiles keep their index -- outside CooR several devices
can legitimately be live and must not collide.

The full key is now byte-identical across devices under CooR, and still
correctly distinct without it.

test_inductor_compiled_on_one_device_runs_on_another asserted a cache *miss* as
a deliberate tripwire for this change -- its comment asked that landing a
device-agnostic key trip the test rather than silently widen it. It now asserts
the hit.

That comment also flagged CompiledFxGraph.device_idxs as a second requirement,
and it is one: cudagraphify takes its device_index from the loaded artifact, so a
cuda:1 rank hitting rank 0's entry warmed up and captured under cuda:0's device,
stream and pool, and crashed with `CUDA driver error: invalid argument`. Under
CooR the artifact now records no index, and CompiledFxGraph.device_index()
resolves it to the current device at load time -- the same single-accelerator
invariant the key change relies on. Outside CooR the recorded index is kept, since
the current device need not match the graph's there. The alternative, keeping
the stored index and overriding it on load, would leave a wrong value in the
artifact for the next reader to trip over.

extract_tensor_metadata_for_cache_key also feeds the AOTAutogradCache key (and
graph_region_tracker), so that key becomes rank-agnostic under CooR too, which is
what cross-rank reuse needs.

Test Plan:

```
TORCHINDUCTOR_COMPILE_THREADS=1 python test/distributed/tensor/test_compile_on_one_rank.py
python test/inductor/test_cudagraph_trees.py
```

test_cudagraphs_reuses_artifact_across_devices (both graph_partition settings)
fails with the invalid-argument error without the output_code.py change and
passes with it.

This PR was authored with the help of an AI assistant.

Pull Request resolved: pytorch#196153
Approved by: https://github.com/bobrenjc93
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…compile_on_one_rank (pytorch#196154)

InputWriter records each input storage's device, so fx_graph_runnable comes out
different on every rank: 3568 of the 3716 lines differing between two ranks of a
16-GPU job were device(type='cuda', index=N). It also makes a repro produced by
rank 11 unrunnable anywhere with fewer than 12 GPUs.

Under compile_on_one_rank the inputs are on the current accelerator by invariant,
so writing the index records nothing that is true of any other rank. Drop it and
let the reader place the storage on whatever device is current: the repro replays
the same compile, matches across ranks, and runs on a box with fewer GPUs than
the rank that produced it. This extends the defaulting the writer already does --
there is a TODO there observing that defaulting to cpu is unhelpful when the
repros that matter are CUDA.

Gated, so a non-CooR repro still names its device: several accelerators can be
live at once there and the repro has to say which one it used.

Pull Request resolved: pytorch#196154
Approved by: https://github.com/bobrenjc93
ghstack dependencies: pytorch#196153
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/dtensor Run DTensor specific tests ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request GL/superseded GreenLight review is superseded by changes Merged module: inductor topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants