Skip to content

Log multi-device backward passes via C10_LOG_API_USAGE_ONCE - #192081

Closed
ezyang wants to merge 2 commits into
gh/ezyang/3540/basefrom
gh/ezyang/3540/head
Closed

ezyang wants to merge 2 commits into
gh/ezyang/3540/basefrom
gh/ezyang/3540/head

Conversation

@ezyang

@ezyang ezyang commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Add once-per-process API usage logs for backward passes, split by the set
of devices they span, so we can assess how much real workloads rely on
multithreaded autograd before changing its default. The check walks the
graph roots in compute_dependencies and emits two independent signals that
may both fire for the same pass:

  • torch.autograd.multidevice_backward when two distinct non-CPU devices
    are observed.
  • torch.autograd.gpu_cpu_backward when a single non-CPU device runs
    alongside CPU nodes.

Multi-device (multiple accelerators in one backward) is the case that
genuinely benefits from multithreaded autograd and is expected to be rare;
the GPU + CPU case is expected to be common. Each log fires at most once
per process, and the per-node device inspection is skipped entirely once
both have fired.

Test Plan: Built, then ran each case in a fresh process with
PYTORCH_API_USAGE_STDERR=1 and grepped the emitted events:

a = torch.randn(4, requires_grad=True, device="cuda")
b = torch.randn(4, requires_grad=True, device="cpu")
(a.sum() + b.sum()).backward()

a = torch.randn(4, requires_grad=True, device="cuda")
(a * 2).sum().backward()

a = torch.randn(4, requires_grad=True, device="cpu")
(a * 2).sum().backward()

The GPU + CPU, single-accelerator, and CPU-only cases behaved as
described. The multi-device path was not exercised at runtime (the test
host has a single GPU); its logic mirrors the GPU + CPU path and was
verified by inspection.

Authored with assistance from an AI coding assistant.

Co-Authored-By: Claude Opus 4.8 [email protected]

Implement the multi-device detection in Engine::compute_dependencies. As the
dependency walk visits each node in the queue, look at the node's device and
skip anything that runs on the CPU ready queue. Record the first non-CPU
device seen; once a second, distinct non-CPU device shows up, fire
C10_LOG_API_USAGE_ONCE("torch.autograd.multidevice_backward").

The emission is gated by a process-wide static std::atomic<bool> flag
(relaxed ordering) so the log fires at most once per process and the per-node
check short-circuits cheaply on every subsequent backward. A CPU plus a
single accelerator still does not count, matching the intent of measuring
reliance on multithreaded autograd across multiple accelerators.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure, 1 Unrelated Failure

As of commit a8dd0af with merge base d34e364 (image):

NEW FAILURE - The following job has failed:

  • trunk / linux-jammy-rocm-py3.10-mi350 / test (default, 1, 8, linux.rocm.gpu.gfx950.1) (gh)
    test/inductor/test_max_autotune.py::TestMaxAutotune::test_max_autotune_decompose_k_dynamic_input_bwd

    AI verdict: AI verdict: not related

    The commit only adds API usage logging (C10_LOG_API_USAGE_ONCE) in the autograd engine's compute_dependencies. The failing test checks that inductor generates extern_kernels.bmm_dtype in compiled code — completely unrelated code path. The same test method also failed at the merge base (under a different test class variant), indicating a pre-existing issue on mi350.

    Full reasoning on HUD →

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

@ezyang
ezyang marked this pull request as ready for review August 4, 2026 20:24
@ezyang
ezyang requested review from albanD and soulitzer as code owners August 4, 2026 20:24
Comment thread torch/csrc/autograd/engine.cpp Outdated
bool will_use_accelerator = false;
// Log (once per process) backward passes that span multiple non-CPU
// devices, to assess reliance on multithreaded autograd before changing
// its default. CPU + a single accelerator does not count.

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.

Why shouldn't CPU + single accelerator count?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are in fact right and I should distinguish these two cases.

Extends the logging to cover a second case: a backward pass that spans a
non-CPU device together with CPU nodes now emits its own
`torch.autograd.gpu_cpu_backward` marker, alongside the existing
`torch.autograd.multidevice_backward` for passes spanning two distinct
non-CPU devices. The two cases are independent and may both fire for the
same pass.

To support this, the per-node scan now also tracks whether any CPU node
was seen (`saw_cpu`) in addition to the first non-CPU device. Each marker
fires at most once per process, guarded by its own atomic flag.

Other changes:

- Switch the guard flags from load/store to `exchange`, closing the race
  where concurrent backward threads could both log the same marker.
- Add an `inspect_devices` short-circuit computed once up front, so the
  per-node device inspection is skipped entirely after both markers have
  already fired — avoiding needless work on the common path.

[ghstack-poisoned]
ezyang added a commit that referenced this pull request Aug 5, 2026
Add once-per-process API usage logs for backward passes, split by the set
of devices they span, so we can assess how much real workloads rely on
multithreaded autograd before changing its default. The check walks the
graph roots in compute_dependencies and emits two independent signals that
may both fire for the same pass:

- torch.autograd.multidevice_backward when two distinct non-CPU devices
  are observed.
- torch.autograd.gpu_cpu_backward when a single non-CPU device runs
  alongside CPU nodes.

Multi-device (multiple accelerators in one backward) is the case that
genuinely benefits from multithreaded autograd and is expected to be rare;
the GPU + CPU case is expected to be common. Each log fires at most once
per process, and the per-node device inspection is skipped entirely once
both have fired.

Test Plan: Built, then ran each case in a fresh process with
PYTORCH_API_USAGE_STDERR=1 and grepped the emitted events:

```
a = torch.randn(4, requires_grad=True, device="cuda")
b = torch.randn(4, requires_grad=True, device="cpu")
(a.sum() + b.sum()).backward()

a = torch.randn(4, requires_grad=True, device="cuda")
(a * 2).sum().backward()

a = torch.randn(4, requires_grad=True, device="cpu")
(a * 2).sum().backward()
```

The GPU + CPU, single-accelerator, and CPU-only cases behaved as
described. The multi-device path was not exercised at runtime (the test
host has a single GPU); its logic mirrors the GPU + CPU path and was
verified by inspection.

Authored with assistance from an AI coding assistant.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
ghstack-source-id: 1db815b
Pull-Request: #192081
@ezyang ezyang added the topic: not user facing topic category label Aug 5, 2026
ezyang added a commit that referenced this pull request Aug 5, 2026
Add a once-per-process API usage log for backward passes that span more
than one non-CPU device, so we can assess how much real workloads rely on
multithreaded autograd before changing its default. The check walks the
graph roots in compute_dependencies and fires only when it observes two
distinct non-CPU devices; a CPU plus a single accelerator does not count.

Test Plan: Built and confirmed the log fires under a multi-device backward
and stays silent for single-device backward.

Authored with assistance from an AI coding assistant.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
ghstack-source-id: 72cfa0a
Pull-Request: #192081
ezyang added a commit that referenced this pull request Aug 5, 2026
Add a once-per-process API usage log for backward passes that span more
than one non-CPU device, so we can assess how much real workloads rely on
multithreaded autograd before changing its default. The check walks the
graph roots in compute_dependencies and fires only when it observes two
distinct non-CPU devices; a CPU plus a single accelerator does not count.

Test Plan: Built and confirmed the log fires under a multi-device backward
and stays silent for single-device backward.

Authored with assistance from an AI coding assistant.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
ghstack-source-id: 72cfa0a
Pull-Request: #192081
ezyang added a commit that referenced this pull request Aug 6, 2026
Add a once-per-process API usage log for backward passes that span more
than one non-CPU device, so we can assess how much real workloads rely on
multithreaded autograd before changing its default. The check walks the
graph roots in compute_dependencies and fires only when it observes two
distinct non-CPU devices; a CPU plus a single accelerator does not count.

Test Plan: Built and confirmed the log fires under a multi-device backward
and stays silent for single-device backward.

Authored with assistance from an AI coding assistant.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
ghstack-source-id: 72cfa0a
Pull-Request: #192081
ezyang added a commit that referenced this pull request Aug 6, 2026
Add a once-per-process API usage log for backward passes that span more
than one non-CPU device, so we can assess how much real workloads rely on
multithreaded autograd before changing its default. The check walks the
graph roots in compute_dependencies and fires only when it observes two
distinct non-CPU devices; a CPU plus a single accelerator does not count.

Test Plan: Built and confirmed the log fires under a multi-device backward
and stays silent for single-device backward.

Authored with assistance from an AI coding assistant.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
ghstack-source-id: 72cfa0a
Pull-Request: #192081
@ezyang

ezyang commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 8, 2026
@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

@ezyang ezyang added the mergedog mergedog is watching this PR label Aug 8, 2026
ezyang added a commit that referenced this pull request Aug 8, 2026
Add once-per-process API usage logs for backward passes, split by the set
of devices they span, so we can assess how much real workloads rely on
multithreaded autograd before changing its default. The check walks the
graph roots in compute_dependencies and emits two independent signals that
may both fire for the same pass:

- torch.autograd.multidevice_backward when two distinct non-CPU devices
  are observed.
- torch.autograd.gpu_cpu_backward when a single non-CPU device runs
  alongside CPU nodes.

Multi-device (multiple accelerators in one backward) is the case that
genuinely benefits from multithreaded autograd and is expected to be rare;
the GPU + CPU case is expected to be common. Each log fires at most once
per process, and the per-node device inspection is skipped entirely once
both have fired.

Test Plan: Built, then ran each case in a fresh process with
PYTORCH_API_USAGE_STDERR=1 and grepped the emitted events:

```
a = torch.randn(4, requires_grad=True, device="cuda")
b = torch.randn(4, requires_grad=True, device="cpu")
(a.sum() + b.sum()).backward()

a = torch.randn(4, requires_grad=True, device="cuda")
(a * 2).sum().backward()

a = torch.randn(4, requires_grad=True, device="cpu")
(a * 2).sum().backward()
```

The GPU + CPU, single-accelerator, and CPU-only cases behaved as
described. The multi-device path was not exercised at runtime (the test
host has a single GPU); its logic mirrors the GPU + CPU path and was
verified by inspection.

Authored with assistance from an AI coding assistant.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
ghstack-source-id: 1db815b
Pull-Request: #192081
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / linux-jammy-rocm-py3.10-mi350 / test (default, 1, 8, linux.rocm.gpu.gfx950.1)

Details for Dev Infra team Raised by workflow job

@ezyang

ezyang commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

mergedog handoff

Note

This PR is being shepherded by mergedog, an automation tool run by a maintainer. It watches CI and may push fixes; the PR author does not need to act unless a maintainer asks.

All CI is green (or skipped). Ready for human review and @pytorchbot merge.

Current PR head: a8dd0af1d1eac00099f6530c0ac2e7b3b792bafb.

Autonomous changes pushed

  • None.

CI notes at handoff

mergedog is treating these still-failing checks as unrelated/spurious. Please verify before merging:

  • linux-jammy-rocm-py3.10-mi350 / test (default, 1, 8, linux.rocm.gpu.gfx950.1)
  • linux-jammy-rocm-py3.10-mi350 / test (default, 4, 8, linux.rocm.gpu.gfx950.1)

Warning: mergedog's suppressed check list differs from the latest Dr. CI summary.

Suppressed by mergedog but not marked unrelated by Dr. CI:

  • linux-jammy-rocm-py3.10-mi350 / test (default, 1, 8, linux.rocm.gpu.gfx950.1)
Latest Dr. CI summary
## :link: Helpful Links
### :test_tube: See artifacts and rendered test results at [hud.pytorch.org/pr/192081](https://hud.pytorch.org/pr/192081)
* :page_facing_up: Preview [Python docs built from this PR](https://docs-preview.pytorch.org/pytorch/pytorch/192081/index.html)
* :page_facing_up: Preview [C++ docs built from this PR](https://docs-preview.pytorch.org/pytorch/pytorch/192081/cppdocs/index.html)
* :question: Need help or want to give feedback on the CI? Visit the [bot commands wiki](https://github.com/pytorch/pytorch/wiki/Bot-commands)

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


## :x: 1 New Failure, 2 Pending, 1 Unrelated Failure
As of commit a8dd0af1d1eac00099f6530c0ac2e7b3b792bafb with merge base d34e3640083f5f9bea117e37b7cfc9a49d7d943d (<sub><sub><img alt="image" width=70 src="https://img.shields.io/date/1785384900?label=&color=FFFFFF&style=flat-square"></sub></sub>):
<details open><summary><b>NEW FAILURE</b> - The following job has failed:</summary><p>

* [trunk / linux-jammy-rocm-py3.10-mi350 / test (default, 1, 8, linux.rocm.gpu.gfx950.1)](https://hud.pytorch.org/pr/pytorch/pytorch/192081#93059516974) ([gh](https://github.com/pytorch/pytorch/actions/runs/31237333440/job/93059516974))
    `test/inductor/test_max_autotune.py::TestMaxAutotune::test_max_autotune_decompose_k_dynamic_input_bwd`
  <details><summary>AI verdict: <a href="https://hud.pytorch.org/pr/pytorch/pytorch/192081#93059516974"><img alt="AI verdict: not related" src="https://hud.pytorch.org/api/drci/advisorBadge?owner=pytorch&repo=pytorch&sha=a8dd0af1d1eac00099f6530c0ac2e7b3b792bafb&job=trunk+%2F+linux-jammy-rocm-py3.10-mi350+%2F+test+%28default%2C+1%2C+8%2C+linux.rocm.gpu.gfx950.1%29"></a></summary><blockquote>

  The commit only adds API usage logging (`C10_LOG_API_USAGE_ONCE`) in the autograd engine&#39;s `compute_dependencies`. The failing test checks that inductor generates `extern_kernels.bmm_dtype` in compiled code — completely unrelated code path. The same test method also failed at the merge base (under a different test class variant), indicating a pre-existing issue on mi350.

  <a href="https://hud.pytorch.org/pr/pytorch/pytorch/192081#93059516974">Full reasoning on HUD &rarr;</a>
  </blockquote></details>
</p></details>
<details ><summary><b>BROKEN TRUNK</b> - The following job failed but were present on the merge base:</summary><p>👉 <b>Rebase onto the `viable/strict` branch to avoid these failures</b></p><p>

* [trunk / linux-jammy-rocm-py3.10-mi350 / test (default, 4, 8, linux.rocm.gpu.gfx950.1)](https://hud.pytorch.org/pr/pytorch/pytorch/192081#93059516977) ([gh](https://github.com/pytorch/pytorch/actions/runs/31237333440/job/93059516977)) ([trunk failure](https://hud.pytorch.org/pytorch/pytorch/commit/d34e3640083f5f9bea117e37b7cfc9a49d7d943d#90780378556))
    `test/inductor/test_max_autotune.py::TestMaxAutotuneAsyncPipelined::test_max_autotune_decompose_k_dynamic_input_bwd`
</p></details>


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

During shepherding, the configured LLM was invoked 2 times. If any CI shows red, it judged that failure unrelated to this PR's changes — please verify below before merging.

Session 1 — fix-CI (2026-08-08T05:53:44+00:00)

  • Before: 77f527fd8ae2
  • Verdict: judged failures spurious (no commit) — failing jobs: linux-jammy-rocm-py3.10-mi350 / test (default, 1, 8, linux.rocm.gpu.gfx950.1); rationale: linux-jammy-rocm-py3.10-mi350 / test (default, 1, 8, linux.rocm.gpu.gfx950.1): test/inductor/test_max_autotune.py::TestMaxAutotune::test_max_autotune_decompose_k_dynamic_input_bwd expected extern_kernels.bmm_dtype, but ROCm autotuning selected a Triton kernel. This is an unrelated, performance-dependent autotuning result: the PR only adds API-usage logging while walking autograd graph devices in torch/csrc/autograd/engine.cpp and does not change Inductor, max-autotune, BMM lowering, or ROCm kernels.
LLM transcript

Session 2 — fix-CI (2026-08-08T05:56:37+00:00)

  • Before: 77f527fd8ae2
  • Verdict: judged failures spurious (no commit) — failing jobs: linux-jammy-rocm-py3.10-mi350 / test (default, 4, 8, linux.rocm.gpu.gfx950.1); rationale: linux-jammy-rocm-py3.10-mi350 / test (default, 4, 8, linux.rocm.gpu.gfx950.1): test/inductor/test_max_autotune.py::TestMaxAutotuneAsyncPipelined::test_max_autotune_decompose_k_dynamic_input_bwd repeatedly failed a ROCm Inductor generated-code FileCheck because extern_kernels.bmm_dtype was absent. This is unrelated to the PR's C++ autograd-engine API-usage logging, which does not alter Inductor lowering or generated backward code.
LLM transcript

@ezyang

ezyang commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 2 checks: trunk / linux-jammy-rocm-py3.10-mi350 / test (default, 1, 8, linux.rocm.gpu.gfx950.1), trunk / linux-jammy-rocm-py3.10-mi350 / test (default, 4, 8, linux.rocm.gpu.gfx950.1)

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

ankitbhrdwj pushed a commit to ankitbhrdwj/pytorch that referenced this pull request Aug 12, 2026
…192081)

Add once-per-process API usage logs for backward passes, split by the set
of devices they span, so we can assess how much real workloads rely on
multithreaded autograd before changing its default. The check walks the
graph roots in compute_dependencies and emits two independent signals that
may both fire for the same pass:

- torch.autograd.multidevice_backward when two distinct non-CPU devices
  are observed.
- torch.autograd.gpu_cpu_backward when a single non-CPU device runs
  alongside CPU nodes.

Multi-device (multiple accelerators in one backward) is the case that
genuinely benefits from multithreaded autograd and is expected to be rare;
the GPU + CPU case is expected to be common. Each log fires at most once
per process, and the per-node device inspection is skipped entirely once
both have fired.

Test Plan: Built, then ran each case in a fresh process with
PYTORCH_API_USAGE_STDERR=1 and grepped the emitted events:

```
a = torch.randn(4, requires_grad=True, device="cuda")
b = torch.randn(4, requires_grad=True, device="cpu")
(a.sum() + b.sum()).backward()

a = torch.randn(4, requires_grad=True, device="cuda")
(a * 2).sum().backward()

a = torch.randn(4, requires_grad=True, device="cpu")
(a * 2).sum().backward()
```

The GPU + CPU, single-accelerator, and CPU-only cases behaved as
described. The multi-device path was not exercised at runtime (the test
host has a single GPU); its logic mirrors the GPU + CPU path and was
verified by inspection.

Authored with assistance from an AI coding assistant.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pull Request resolved: pytorch#192081
Approved by: https://github.com/soulitzer
@github-actions
github-actions Bot deleted the gh/ezyang/3540/head branch October 30, 2026 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged mergedog mergedog is watching this PR topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants