Skip to content

[CooR] Synchronize an index-less device on the caller's current device - #197384

Closed
aorenste wants to merge 11 commits into
gh/aorenste/396/basefrom
gh/aorenste/396/head
Closed

aorenste wants to merge 11 commits into
gh/aorenste/396/basefrom
gh/aorenste/396/head

Conversation

@aorenste

@aorenste aorenste commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with device.index or 0, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in with torch.cuda._DeviceGuard(<graph device>):,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The end-to-end two-device test lives in
test/distributed/tensor/test_compile_on_one_rank.py under requires_multigpu,
because that is where the multi-GPU CI job looks; the dynamo shards are
single-GPU. test_streams.py keeps a single-GPU test that patches
current_device_index, which drives both the trace-time resolution and the
guard that checks it. Since the patched indices needn't exist on the runner, it
also patches torch.accelerator.current_stream (Dynamo reads it before tracing)
and torch.accelerator.synchronize, which it records to check the runtime
synchronized the resolved devices.

Test Plan:

.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
CUDA_VISIBLE_DEVICES=0 .venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/distributed/tensor/test_compile_on_one_rank.py -k test_synchronize_indexless_device_uses_caller_device_without_coor
PATH="$PWD/.venv/bin:$PATH" lintrunner -a

Authored with the assistance of an AI assistant.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 4ac01b8 with merge base 2ea2e41 (image):
💚 Looks good so far! There are no failures yet. 💚

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

  • Behavior change in compiled semantics
    • handle_synchronize in torch/_dynamo/variables/torch.py now resolves an index-less device via torch.accelerator.current_device_index() instead of hardcoding 0
    • Compiled torch.cuda.synchronize() now syncs the caller's current device, which moves what users of compiled code observe
  • Correct but consequential
    • New EQUALS_MATCH guard on current_device_index specializes every frame with an index-less synchronize, one recompile per current device
    • PR body notes 8 GPUs exactly exhaust the default recompile_limit, after which the frame falls back to eager
    • Adds new graph break GB8974 in graph_break_registry.json where the call used to trace
  • Test coverage
    • Single-GPU test patches current_device_index; the two-GPU end-to-end test is stated by the PR body not to run in CI

reason: not_trivial
Reviewed commit: a67c93d (NOT the current head 4ac01b8)

Inference job

🐛 Report a wrong verdict

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

[ghstack-poisoned]
aorenste added a commit that referenced this pull request Sep 18, 2026
torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with `device.index or 0`, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in `with torch.cuda._DeviceGuard(<graph device>):`,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The dynamo shards are single-GPU and multigpu-test.sh runs an explicit
allowlist that does not include test_streams.py, so the two-device test
never executes in CI. It is kept as the end-to-end case and backed by a
single-GPU test that patches current_device_index, which drives both the
trace-time resolution and the guard that checks it.

Test Plan:

```shell
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_uses_caller_device
PATH="$PWD/.venv/bin:$PATH" lintrunner -a
```

Authored with the assistance of an AI assistant.


ghstack-source-id: faf6ea3
Pull-Request: #197384
@aorenste aorenste added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 19, 2026
[ghstack-poisoned]
aorenste added a commit that referenced this pull request Sep 19, 2026
torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with `device.index or 0`, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in `with torch.cuda._DeviceGuard(<graph device>):`,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The dynamo shards are single-GPU and multigpu-test.sh runs an explicit
allowlist that does not include test_streams.py, so the two-device test
never executes in CI. It is kept as the end-to-end case and backed by a
single-GPU test that patches current_device_index, which drives both the
trace-time resolution and the guard that checks it.

Test Plan:

```shell
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_uses_caller_device
PATH="$PWD/.venv/bin:$PATH" lintrunner -a
```

Authored with the assistance of an AI assistant.


ghstack-source-id: 18dc98c
Pull-Request: #197384
[ghstack-poisoned]
aorenste added a commit that referenced this pull request Sep 22, 2026
torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with `device.index or 0`, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in `with torch.cuda._DeviceGuard(<graph device>):`,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The dynamo shards are single-GPU and multigpu-test.sh runs an explicit
allowlist that does not include test_streams.py, so the two-device test
never executes in CI. It is kept as the end-to-end case and backed by a
single-GPU test that patches current_device_index, which drives both the
trace-time resolution and the guard that checks it.

Test Plan:

```shell
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_uses_caller_device
PATH="$PWD/.venv/bin:$PATH" lintrunner -a
```

Authored with the assistance of an AI assistant.


ghstack-source-id: 1298f7d
Pull-Request: #197384
[ghstack-poisoned]
aorenste added a commit that referenced this pull request Sep 27, 2026
torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with `device.index or 0`, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in `with torch.cuda._DeviceGuard(<graph device>):`,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The dynamo shards are single-GPU and multigpu-test.sh runs an explicit
allowlist that does not include test_streams.py, so the two-device test
never executes in CI. It is kept as the end-to-end case and backed by a
single-GPU test that patches current_device_index, which drives both the
trace-time resolution and the guard that checks it.

Test Plan:

```shell
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_uses_caller_device
PATH="$PWD/.venv/bin:$PATH" lintrunner -a
```

Authored with the assistance of an AI assistant.


ghstack-source-id: 396b8c9
Pull-Request: #197384
@aorenste aorenste added the topic: not user facing topic category label Sep 27, 2026
@aorenste
aorenste marked this pull request as ready for review September 27, 2026 18:08
@aorenste
aorenste requested a review from bobrenjc93 September 27, 2026 18:08
[ghstack-poisoned]
@aorenste aorenste changed the title Synchronize an index-less device on the caller's current device [CooR] Synchronize an index-less device on the caller's current device Sep 30, 2026
aorenste added a commit that referenced this pull request Sep 30, 2026
torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with `device.index or 0`, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in `with torch.cuda._DeviceGuard(<graph device>):`,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The dynamo shards are single-GPU and multigpu-test.sh runs an explicit
allowlist that does not include test_streams.py, so the two-device test
never executes in CI. It is kept as the end-to-end case and backed by a
single-GPU test that patches current_device_index, which drives both the
trace-time resolution and the guard that checks it.

Test Plan:

```shell
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_uses_caller_device
PATH="$PWD/.venv/bin:$PATH" lintrunner -a
```

Authored with the assistance of an AI assistant.

ghstack-source-id: e455bd0
Pull-Request: #197384
[ghstack-poisoned]
aorenste added a commit that referenced this pull request Oct 13, 2026
torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with `device.index or 0`, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in `with torch.cuda._DeviceGuard(<graph device>):`,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The dynamo shards are single-GPU and multigpu-test.sh runs an explicit
allowlist that does not include test_streams.py, so the two-device test
never executes in CI. It is kept as the end-to-end case and backed by a
single-GPU test that patches current_device_index, which drives both the
trace-time resolution and the guard that checks it.

Test Plan:

```shell
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_uses_caller_device
PATH="$PWD/.venv/bin:$PATH" lintrunner -a
```

Authored with the assistance of an AI assistant.

ghstack-source-id: e796aa7
Pull-Request: #197384
[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added the ciflow/dtensor Run DTensor specific tests label Oct 20, 2026
aorenste added a commit that referenced this pull request Oct 20, 2026
torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with `device.index or 0`, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in `with torch.cuda._DeviceGuard(<graph device>):`,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The end-to-end two-device test lives in
test/distributed/tensor/test_compile_on_one_rank.py under requires_multigpu,
because that is where the multi-GPU CI job looks; the dynamo shards are
single-GPU. test_streams.py keeps a single-GPU test that patches
current_device_index, which drives both the trace-time resolution and the
guard that checks it.

Test Plan:

```shell
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/distributed/tensor/test_compile_on_one_rank.py -k test_synchronize_indexless_device_uses_caller_device_without_coor
PATH="$PWD/.venv/bin:$PATH" lintrunner -a
```

Authored with the assistance of an AI assistant.

ghstack-source-id: 07442eb
Pull-Request: #197384
[ghstack-poisoned]
aorenste added a commit that referenced this pull request Oct 25, 2026
torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with `device.index or 0`, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in `with torch.cuda._DeviceGuard(<graph device>):`,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The end-to-end two-device test lives in
test/distributed/tensor/test_compile_on_one_rank.py under requires_multigpu,
because that is where the multi-GPU CI job looks; the dynamo shards are
single-GPU. test_streams.py keeps a single-GPU test that patches
current_device_index, which drives both the trace-time resolution and the
guard that checks it.

Test Plan:

```shell
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/distributed/tensor/test_compile_on_one_rank.py -k test_synchronize_indexless_device_uses_caller_device_without_coor
PATH="$PWD/.venv/bin:$PATH" lintrunner -a
```

Authored with the assistance of an AI assistant.

ghstack-source-id: 98aae3d
Pull-Request: #197384
[ghstack-poisoned]
aorenste added a commit that referenced this pull request Oct 30, 2026
torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with `device.index or 0`, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in `with torch.cuda._DeviceGuard(<graph device>):`,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The end-to-end two-device test lives in
test/distributed/tensor/test_compile_on_one_rank.py under requires_multigpu,
because that is where the multi-GPU CI job looks; the dynamo shards are
single-GPU. test_streams.py keeps a single-GPU test that patches
current_device_index, which drives both the trace-time resolution and the
guard that checks it.

Test Plan:

```shell
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/distributed/tensor/test_compile_on_one_rank.py -k test_synchronize_indexless_device_uses_caller_device_without_coor
PATH="$PWD/.venv/bin:$PATH" lintrunner -a
```

Authored with the assistance of an AI assistant.

ghstack-source-id: 936f594
Pull-Request: #197384
@pytorchgreenlight pytorchgreenlight Bot added the GL/superseded GreenLight review is superseded by changes label Oct 30, 2026
[ghstack-poisoned]
aorenste added a commit that referenced this pull request Nov 1, 2026
torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with `device.index or 0`, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in `with torch.cuda._DeviceGuard(<graph device>):`,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The end-to-end two-device test lives in
test/distributed/tensor/test_compile_on_one_rank.py under requires_multigpu,
because that is where the multi-GPU CI job looks; the dynamo shards are
single-GPU. test_streams.py keeps a single-GPU test that patches
current_device_index, which drives both the trace-time resolution and the
guard that checks it. Since the patched indices needn't exist on the runner, it
also patches torch.accelerator.current_stream (Dynamo reads it before tracing)
and torch.accelerator.synchronize, which it records to check the runtime
synchronized the resolved devices.

Test Plan:

```shell
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
CUDA_VISIBLE_DEVICES=0 .venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/distributed/tensor/test_compile_on_one_rank.py -k test_synchronize_indexless_device_uses_caller_device_without_coor
PATH="$PWD/.venv/bin:$PATH" lintrunner -a
```

Authored with the assistance of an AI assistant.

ghstack-source-id: fb77fcf
Pull-Request: #197384
@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

aorenste added a commit to aorenste/pytorch that referenced this pull request Nov 3, 2026
…ch#203047)

Summary:

test_torch_get_device_module fails on a CUDA build with no GPU once pytorch#197384 (D128968633) lands, as in test_dynamo here and OSS periodic's nogpu_AVX512 shard. Its last step compiles `torch.get_device_module().synchronize()`, which on that build is `torch.cuda.synchronize()`. pytorch#197384 resolves an index-less synchronize's device while tracing; with no driver, `torch.accelerator.current_device_index()` raises, so Dynamo graph-breaks and runs the call eagerly, and eager raises `RuntimeError: Found no NVIDIA driver on your system`. Before, the compiled op was `torch.accelerator.synchronize`, a silent no-op on uninitialized CUDA. Raising like eager is intended, so the test was relying on that no-op.

Move the synchronize step into its own test, gated on `requires_accelerator`, so the rest of test_torch_get_device_module keeps running on GPU-less machines. Decorating the whole test would also work, but would drop its get_device_module checks there.

This is the forward fix for D128968633, which is rebased on top of it so both land together.

Authored with the assistance of an AI assistant (Claude).

Test Plan:
In an OSS checkout containing pytorch#197384:
```
CUDA_VISIBLE_DEVICES= python test/dynamo/test_functions.py -k test_torch_get_device_module
CUDA_VISIBLE_DEVICES=0 python test/dynamo/test_functions.py -k test_torch_get_device_module
```
With no GPU visible, test_torch_get_device_module passes and test_torch_get_device_module_synchronize is skipped ("requires accelerator"); before this change test_torch_get_device_module failed in that configuration. With a GPU, both pass.

Reviewed By: jeanschmidt

Differential Revision: D129106076
pytorchmergebot pushed a commit that referenced this pull request Nov 4, 2026
…) (#203047)

Summary:

test_torch_get_device_module fails on a CUDA build with no GPU once #197384 (D128968633) lands, as in test_dynamo here and OSS periodic's nogpu_AVX512 shard. Its last step compiles `torch.get_device_module().synchronize()`, which on that build is `torch.cuda.synchronize()`. #197384 resolves an index-less synchronize's device while tracing; with no driver, `torch.accelerator.current_device_index()` raises, so Dynamo graph-breaks and runs the call eagerly, and eager raises `RuntimeError: Found no NVIDIA driver on your system`. Before, the compiled op was `torch.accelerator.synchronize`, a silent no-op on uninitialized CUDA. Raising like eager is intended, so the test was relying on that no-op.

Move the synchronize step into its own test, gated on `requires_accelerator`, so the rest of test_torch_get_device_module keeps running on GPU-less machines. Decorating the whole test would also work, but would drop its get_device_module checks there.

This is the forward fix for D128968633, which is rebased on top of it so both land together.

Authored with the assistance of an AI assistant (Claude).

Test Plan:
In an OSS checkout containing #197384:
```
CUDA_VISIBLE_DEVICES= python test/dynamo/test_functions.py -k test_torch_get_device_module
CUDA_VISIBLE_DEVICES=0 python test/dynamo/test_functions.py -k test_torch_get_device_module
```
With no GPU visible, test_torch_get_device_module passes and test_torch_get_device_module_synchronize is skipped ("requires accelerator"); before this change test_torch_get_device_module failed in that configuration. With a GPU, both pass.

Reviewed By: jeanschmidt

Differential Revision: D129106076

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

torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with `device.index or 0`, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in `with torch.cuda._DeviceGuard(<graph device>):`,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The end-to-end two-device test lives in
test/distributed/tensor/test_compile_on_one_rank.py under requires_multigpu,
because that is where the multi-GPU CI job looks; the dynamo shards are
single-GPU. test_streams.py keeps a single-GPU test that patches
current_device_index, which drives both the trace-time resolution and the
guard that checks it. Since the patched indices needn't exist on the runner, it
also patches torch.accelerator.current_stream (Dynamo reads it before tracing)
and torch.accelerator.synchronize, which it records to check the runtime
synchronized the resolved devices.

Test Plan:

```shell
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
CUDA_VISIBLE_DEVICES=0 .venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/distributed/tensor/test_compile_on_one_rank.py -k test_synchronize_indexless_device_uses_caller_device_without_coor
PATH="$PWD/.venv/bin:$PATH" lintrunner -a
```

Authored with the assistance of an AI assistant.

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

Summary:

test_torch_get_device_module fails on a CUDA build with no GPU once pytorch#197384 (D128968633) lands, as in test_dynamo here and OSS periodic's nogpu_AVX512 shard. Its last step compiles `torch.get_device_module().synchronize()`, which on that build is `torch.cuda.synchronize()`. pytorch#197384 resolves an index-less synchronize's device while tracing; with no driver, `torch.accelerator.current_device_index()` raises, so Dynamo graph-breaks and runs the call eagerly, and eager raises `RuntimeError: Found no NVIDIA driver on your system`. Before, the compiled op was `torch.accelerator.synchronize`, a silent no-op on uninitialized CUDA. Raising like eager is intended, so the test was relying on that no-op.

Move the synchronize step into its own test, gated on `requires_accelerator`, so the rest of test_torch_get_device_module keeps running on GPU-less machines. Decorating the whole test would also work, but would drop its get_device_module checks there.

This is the forward fix for D128968633, which is rebased on top of it so both land together.

Authored with the assistance of an AI assistant (Claude).

Test Plan:
In an OSS checkout containing pytorch#197384:
```
CUDA_VISIBLE_DEVICES= python test/dynamo/test_functions.py -k test_torch_get_device_module
CUDA_VISIBLE_DEVICES=0 python test/dynamo/test_functions.py -k test_torch_get_device_module
```
With no GPU visible, test_torch_get_device_module passes and test_torch_get_device_module_synchronize is skipped ("requires accelerator"); before this change test_torch_get_device_module failed in that configuration. With a GPU, both pass.

Reviewed By: jeanschmidt

Differential Revision: D129106076

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

torch.cuda.synchronize(torch.device("cuda")) -- and the bare
torch.cuda.synchronize() that builds the same device -- means "the current
device". Dynamo lowered it with `device.index or 0`, so compiled code
synchronized device 0 whichever device was current, while eager synchronized
the caller's. Resolve the index from
torch.accelerator.current_device_index() instead.

It has to be resolved while tracing rather than deferred to the operator:
Inductor wraps the call in `with torch.cuda._DeviceGuard(<graph device>):`,
so an index read at run time picks up the graph's device, not the caller's.
Resolving at trace time makes it a constant, which then has to be guarded --
an ID_MATCH on torch plus an EQUALS_MATCH on current_device_index().

That guard specializes every frame containing an index-less synchronize on
the ambient device index: one frame per current device, so a process cycling
through 8 GPUs compiles 8 frames and exactly consumes the default
recompile_limit of 8, after which the frame falls back to eager. Carrying
the index as a runtime value instead is what the Inductor device guard
rules out, so the recompiles are the price of synchronizing the right
device.

Resolving needs a visible accelerator. Where there is none, graph break
instead of letting the RuntimeError escape as an InternalTorchDynamoError
that asks the user to file a PyTorch bug -- the call then runs eagerly and
raises what eager raises.

compile_on_one_rank is untouched: there the index stays None so the runtime
resolves it per rank.

The end-to-end two-device test lives in
test/distributed/tensor/test_compile_on_one_rank.py under requires_multigpu,
because that is where the multi-GPU CI job looks; the dynamo shards are
single-GPU. test_streams.py keeps a single-GPU test that patches
current_device_index, which drives both the trace-time resolution and the
guard that checks it. Since the patched indices needn't exist on the runner, it
also patches torch.accelerator.current_stream (Dynamo reads it before tracing)
and torch.accelerator.synchronize, which it records to check the runtime
synchronized the resolved devices.

Test Plan:

```shell
.venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
CUDA_VISIBLE_DEVICES=0 .venv/bin/python test/dynamo/test_streams.py TestStreamsCUDASpecific.test_synchronize_indexless_device_resolves_current_device
.venv/bin/python test/distributed/tensor/test_compile_on_one_rank.py -k test_synchronize_indexless_device_uses_caller_device_without_coor
PATH="$PWD/.venv/bin:$PATH" lintrunner -a
```

Authored with the assistance of an AI assistant.

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

Summary:

test_torch_get_device_module fails on a CUDA build with no GPU once pytorch#197384 (D128968633) lands, as in test_dynamo here and OSS periodic's nogpu_AVX512 shard. Its last step compiles `torch.get_device_module().synchronize()`, which on that build is `torch.cuda.synchronize()`. pytorch#197384 resolves an index-less synchronize's device while tracing; with no driver, `torch.accelerator.current_device_index()` raises, so Dynamo graph-breaks and runs the call eagerly, and eager raises `RuntimeError: Found no NVIDIA driver on your system`. Before, the compiled op was `torch.accelerator.synchronize`, a silent no-op on uninitialized CUDA. Raising like eager is intended, so the test was relying on that no-op.

Move the synchronize step into its own test, gated on `requires_accelerator`, so the rest of test_torch_get_device_module keeps running on GPU-less machines. Decorating the whole test would also work, but would drop its get_device_module checks there.

This is the forward fix for D128968633, which is rebased on top of it so both land together.

Authored with the assistance of an AI assistant (Claude).

Test Plan:
In an OSS checkout containing pytorch#197384:
```
CUDA_VISIBLE_DEVICES= python test/dynamo/test_functions.py -k test_torch_get_device_module
CUDA_VISIBLE_DEVICES=0 python test/dynamo/test_functions.py -k test_torch_get_device_module
```
With no GPU visible, test_torch_get_device_module passes and test_torch_get_device_module_synchronize is skipped ("requires accelerator"); before this change test_torch_get_device_module failed in that configuration. With a GPU, both pass.

Reviewed By: jeanschmidt

Differential Revision: D129106076

Pull Request resolved: pytorch#203047
Approved by: https://github.com/bobrenjc93
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: dynamo topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants