Accelerator hooks are the mechanism for integrating custom accelerator devices into PyTorch’s runtime.
Design
The tables below list the hooks accelerator vendors should implement when integrating a new device backend. These hooks are categorized into two priority levels:
High‑priority hooks: Core APIs that the PyTorch runtime directly depends on. Vendors should implement all high‑priority hooks to ensure core compatibility and basic device functionality.
Low‑priority hooks: Device‑management and utility APIs that PyTorch does not directly depend on. These hooks enhance user experience and multi‑device support and are optional. Vendors can implement them based on specific requirements and use cases.
High‑priority hooks
Hook method
Description
Application scenarios
init()
Initializes the accelerator runtime and device contexts
Set up necessary state when PyTorch first accesses the device
hasPrimaryContext(DeviceIndex)
Checks if a primary context exists for the device
Determine whether device initialization has occurred
getDefaultGenerator(DeviceIndex)
Returns the default random number generator for a device
Access the device’s primary RNG for reproducible random operations
getNewGenerator(DeviceIndex)
Creates a new independent random number generator
Create isolated RNG instances for parallel operations
getDeviceFromPtr(void*)
Determines which device a memory pointer belongs to
Identify the accelerator device associated with a memory allocation
getPinnedMemoryAllocator()
Returns an allocator for pinned (page-locked) host memory
Allocate host memory that can be efficiently transferred to/from the accelerator
isPinnedPtr(void*)
Checks if a pointer points to pinned memory
Validate memory types before performing operations
Low‑priority hooks
Hook method
Description
Application scenarios
isBuilt()
Returns whether the accelerator backend is built/compiled into the extension
Check whether the accelerator library is available at compile time
isAvailable()
Returns whether the accelerator hardware is available at runtime
Verify whether accelerator devices can be detected and initialized
deviceCount()
Returns the number of available accelerator devices
Enumerate all available accelerator devices for device selection
setCurrentDevice(DeviceIndex)
Sets the active device for the current thread
Switch the current thread’s context to a specific accelerator device
getCurrentDevice()
Returns the currently active device index
Query which accelerator device is active in the current thread
exchangeDevice(DeviceIndex)
Atomically exchanges the current device and returns the previous one
Temporarily switch devices and restore the previous device afterward
maybeExchangeDevice(DeviceIndex)
Conditionally exchanges device only if the index is valid
Safely attempt device switching with validation
Implementation
For illustration, OpenReg (Open Registration) is a PyTorch integration example that fills the gap for out‑of‑tree accelerator backend integration. It demonstrates how vendors can register custom device backends—without modifying PyTorch core—by implementing the hooks interface (see at::PrivateUse1HooksInterface).
Override the base interface: The getDefaultGenerator method overrides the virtual method from at::PrivateUse1HooksInterface.
Delegate to the device‑specific implementation: Call getDefaultOpenRegGenerator(device_index), which manages a per‑device generator instance.
Return a device‑specific generator: The returned at::Generator wraps an OpenRegGeneratorImpl that implements device‑specific random number generation.
This pattern applies to all hooks: override the interface method, validate inputs, delegate to your device‑specific API, and return results in PyTorch’s expected format.
Integration Example
The following sections demonstrate how PyTorch integrates with accelerator hooks when accessing the default random number generator. The example traces the complete flow from user-facing Python code down to the device-specific implementation.
Layer 1: User Code
User code sets a deterministic seed by calling manual_seed:
importtorchtorch.openreg.manual_seed(42)
Layer 2: Extension Python API
The Python API layer manages device selection and calls into the C++ extension (defined in torch_openreg/openreg/random.py):
The manual_seed function obtains the current device index, calls torch_openreg._C._get_default_generator(idx) to get the device‑specific generator, and sets its seed.
Layer 3: Python/C++ Bridge
The C++ extension exposes _getDefaultGenerator to Python, which bridges into PyTorch core:
This function unpacks the device index from Python, creates a PrivateUse1 device object, and calls at::globalContext().defaultGenerator(). PyTorch’s context then dispatches to the registered hooks.
&
name
);
3
TORCH_API
std
::
string
cudnn_depthwise2str
(
CuDNNDepthwiseKernel
k
);
4
5
class
TORCH_API
Context
{
6
public
:
7
Context
();
8
9constGenerator&defaultGenerator(Devicedevice){
10
c10
::
DeviceType
device_type
=
device
.
type
();
11
lazyInitDevice
(
device_type
);
12
13
if
(
device_type
==
at
::
kCPU
)
{
14
return
at
::
detail
::
getDefaultCPUGenerator
();
15
}
else
{
16
return
getAcceleratorHooksInterface
(
device_type
)
17
.
getDefaultGenerator
(
device
.
index
());
18
}
19
}
20
21
const
AcceleratorHooksInterface
&
getAcceleratorHooksInterface
(
22
std
::
optional
<
c10
::
DeviceType
>
opt_device_type
=
std
::
nullopt
)
{
23
if
(
!
opt_device_type
.
has_value
())
{
24opt_device_type=at::getAccelerator(true);
25}
26
if
(
opt_device_type
==
at
::
kCUDA
)
{
27
return
at
::
detail
::
getCUDAHooks
();
28
}
else
if
(
opt_device_type
==
at
::
kXPU
)
{
29
return
at
::
detail
::
getXPUHooks
();
30
}
else
if
(
opt_device_type
==
at
::
kMPS
)
{
31
return
at
::
detail
::
getMPSHooks
();
32
}
else
if
(
opt_device_type
==
at
::
kPrivateUse1
)
{
33
return
at
::
detail
::
getPrivateUse1Hooks
();
34
}
else
if
(
opt_device_type
==
at
::
kMTIA
)
{
35
return
at
::
detail
::
getMTIAHooks
();
36
}
else
if
(
opt_device_type
==
at
::
kHIP
)
{
37
return
at
::
detail
::
getHIPHooks
();
38
}
else
if
(
opt_device_type
==
at
::
kHPU
)
{
39
return
at
::
detail
::
getHPUHooks
();
40
}
else
if
(
opt_device_type
==
at
::
kXLA
)
{
41
return
at
::
detail
::
getXLAHooks
();
42
}
else
{
43
TORCH_CHECK
(
44
false
,
This layered architecture keeps PyTorch device‑agnostic while delegating hardware‑specific operations to accelerator implementations. Hooks are registered once at module load time:
The getDefaultGenerator hook method overrides the base interface and delegates to getDefaultOpenRegGenerator, which manages the actual generator instances.
auto
deivce_nums
=
device_count
();
4
default_generators
.
resize
(
deivce_nums
);
5
for
(
auto
i
=
0
;
i
<
deivce_nums
;
i
++
)
{
6
default_generators
[
i
]
=
at
::
make_generator
<
OpenRegGeneratorImpl
>
(
i
);
7
default_generators
[
i
].
seed
();
8
}
9
return
true
;
10
}();
11
12
c10
::
DeviceIndex
idx
=
device_index
;
13
if
(
idx
==
-1
)
{
14
idx
=
current_device
();
15
}
else
{
16
TORCH_CHECK
(
idx
>=
0
&&
idx
<
device_count
());
17
}
18
return
default_generators
[
idx
];
19
}
This function maintains a static vector of generators (one per device), initializes them on first access, validates the device index, and returns the appropriate generator instance.