pytorch - ✅(Solved) Fix [privateuse1] Update privateuse1 tutorial to show the Python backend approach [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#179010Fetched 2026-04-08 02:21:58
View on GitHub
Comments
0
Participants
1
Timeline
15
Reactions
0
Participants
Timeline (top)
mentioned ×7subscribed ×7labeled ×1

PR fix notes

PR #157859: Add functions to setup PrivateUse1 as a python backend device.

Description (problem / solution / changelog)

Fixes #156052 and #156444.

This PR setup the privateuseone key in Python to be used as a python backend for pytorch. Meaning that, after calling setup_privateuseone_for_python_backend('npy'), one can use a subclass to with that device to hold arbitrary python data as "device data" and use torch.library to register ops that takes that Tensor.

Changes done in this PR:

  1. Register an vanilla Device Guard: I extended NoOpDeviceGuard to have allow device index of 0 and to not raise errors when event related functions are accessed. If I don't do those, when calling backward I would get errors. (CPU backend uses NoOpDeviceGuard just fine, although there seems to be special treatment of CPU in the autograd engine.
  2. Tensor subclass allows not having __torch_dispatch__ if the device is not CUDA or CPU. The comment of the check suggests it was to avoid segfault when calling into ops that expects a storage. Here we have a different device so will not call into those ops.
  3. python function that invokes the other incantations to setup the privateusekey backend.

This took inspiration of https://github.com/bdhirsh/pytorch_open_registration_example and https://github.com/tinygrad/tinygrad/blob/master/extra/torch_backend/wrapped_tensor.cpp; great thanks to @bdhirsh and @geohot.

Changed files

  • build_variables.bzl (modified, +1/-0)
  • c10/core/impl/DeviceGuardImplInterface.cpp (modified, +8/-2)
  • c10/core/impl/DeviceGuardImplInterface.h (modified, +3/-0)
  • test/dynamo_skips/PrivateUse1BackendTest.test_backend_simple (added, +0/-0)
  • test/run_test.py (modified, +1/-0)
  • test/test_privateuseone_python_backend.py (added, +147/-0)
  • torch/_C/__init__.pyi.in (modified, +1/-0)
  • torch/_C/_acc/__init__.pyi (added, +15/-0)
  • torch/_subclasses/meta_utils.py (modified, +7/-5)
  • torch/csrc/Module.cpp (modified, +2/-0)
  • torch/csrc/acc/Module.cpp (added, +196/-0)
  • torch/csrc/acc/Module.h (added, +8/-0)
  • torch/utils/backend_registration.py (modified, +79/-1)
RAW_BUFFERClick to expand / collapse

📚 The doc issue

https://github.com/pytorch/pytorch/pull/157859 added support for Python backend support.

However, the utility function _setup_privateuseone_for_python_backend added in the PR isn't mentioned anywhere in the tutorial or docs. It would be good to mention it as it can help simplify adding a new backend.

Suggest a potential alternative/fix

NA

cc @NmomoN @mengpenghui @fwenguang @cdzhan @1274085042 @PHLens @albanD

extent analysis

TL;DR

Update the PyTorch documentation to include information about the _setup_privateuseone_for_python_backend utility function to help simplify adding a new backend.

Guidance

  • Review the PyTorch documentation to identify the most relevant sections where the _setup_privateuseone_for_python_backend function should be mentioned.
  • Consider adding a new section or subsection to the tutorial that explains how to use the _setup_privateuseone_for_python_backend function to add a new backend.
  • Check the PyTorch pull request #157859 to understand the context and purpose of the _setup_privateuseone_for_python_backend function.
  • Verify that the documentation updates are accurate and consistent with the existing documentation style.

Notes

The fix relies on updating the documentation to reflect the changes made in pull request #157859. The _setup_privateuseone_for_python_backend function is a specific utility function, and its documentation should be targeted at developers who are adding new backends to PyTorch.

Recommendation

Apply workaround: Update the PyTorch documentation to include information about the _setup_privateuseone_for_python_backend utility function, as this will provide developers with the necessary information to simplify adding a new backend.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - ✅(Solved) Fix [privateuse1] Update privateuse1 tutorial to show the Python backend approach [1 pull requests, 1 participants]