pytorch/c10
Han Qi b5c4f46bb9 Add functions to setup PrivateUse1 as a python backend device. (#157859)
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.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/157859
Approved by: https://github.com/albanD
2025-10-01 21:32:59 +00:00
..
benchmark Set RUNPATH so installed tests can find the required shared libraries (#136627) 2024-10-25 09:38:08 +00:00
core Add functions to setup PrivateUse1 as a python backend device. (#157859) 2025-10-01 21:32:59 +00:00
cuda [PyTorch CCA] Add an API to get expandable segment sizes (#163771) 2025-10-01 02:16:58 +00:00
hip Use torch_compile_options for c10 libraries (#147821) 2025-03-18 01:54:23 +00:00
macros Move c10/macros/Macros.h to headeronly (#158365) 2025-07-16 18:46:52 +00:00
metal [MPS] Add igamma/igammac ops (#161927) 2025-09-02 20:52:02 +00:00
mobile [2/N] Fix extra warnings brought by clang-tidy-17 (#137459) 2024-10-08 19:05:02 +00:00
test Add -Wno-ctad-maybe-unsupported compiler flag (#162223) 2025-09-06 06:11:37 +00:00
util Combine strong and weak refcounts in intrusive_ptr in a single refcount (#163394) 2025-09-22 17:53:28 +00:00
xpu Add uuid to XPU device properties (#161392) 2025-09-02 06:41:32 +00:00
BUCK.oss Migrate c10/macros/cmake_macros.h.in to torch/headeronly (#158035) 2025-07-15 19:52:59 +00:00
BUILD.bazel
build.bzl Remove non-header-only dep from c10_headers target (#155858) 2025-06-16 21:41:25 +00:00
CMakeLists.txt Migrate c10/macros/cmake_macros.h.in to torch/headeronly (#158035) 2025-07-15 19:52:59 +00:00
ovrsource_defs.bzl Revert "Consistently use c10_ovrsource in arvr mode everywhere (#164128)" 2025-09-30 14:43:52 +00:00