For new accelerators, one of the most important and fundamental aspects of integration is supporting high-performance operators. To facilitate operator adaptation for users and accelerator developers, PyTorch provides multiple methods for developing and registering operators in both Python and C++. The following sections detail some of PyTorch’s fundamental capabilities for operator registration.
PyTorch currently has over 3500 built-in operators (including related operator variants). This represents a significant workload from any perspective, and supporting this massive number of operators in a short period of time is no easy task. Therefore, as the first step in developing new backend operators, our goal should be to focus on the essential operators. For other operators, we can first use the community’s fallback mechanism to support the feature as the first priority. After that, we can gradually complete other operators to improve the performance of the new backend.
The required operator set is listed below, primarily consisting of low-level operators required by factory functions and fallback operators:
Operator Name
Dispatch Key
Description
empty.memory_format
PrivateUse1
Create an uninitialized Tensor with the specified shape and memory layout (the stride is automatically calculated)
empty_strided
PrivateUse1
Create an uninitialized Tensor of the specified shape and stride (more degrees of freedom)
as_strided
PrivateUse1
Create a shared view of the input Tensor with new shape, stride, and offset (without allocating new memory)
view
PrivateUse1
Create a shared view of the input Tensor with new shape, but the original Tensor must be memory-contiguous
_reshape_alias
PrivateUse1
Creates a shared view without safety checks(Internal version of reshape)
resize_
PrivateUse1
Modify the shape of the Tensor in place and reallocate memory if capacity is insufficient
_copy_from
PrivateUse1
The underlying core function of Tensor.copy_ is responsible for the actual cross-device data copying
_copy_from_and_resize
PrivateUse1
Combine resize_ and _copy_from to resize first and then copy
_local_scalar_dense
PrivateUse1
The underlying implementation of .item(), extracting values from Tensor to CPU scalars
set_.source_Tensor
PrivateUse1
Set the current Tensor using the specified Tensor
set_.source_Storage
PrivateUse1
Set the current Tensor using the specified Storage
set_.source_Storage_storage_offset
PrivateUse1
Set the current Tensor using the specified Storage with the storage offset
fallback
PrivateUse1
Fallback to CPU
Basics
Now that we have defined the initial scope of operator support, we can begin developing operator adaptations. This section will explain these implementations in Python and C++ based on actual scenarios.
Step 1
The operators mentioned above share a common characteristic: They are built-in PyTorch operators with defined namespaces and Schemas, and these operators’ built-in accelerators (CPU, CUDA, etc.) have been implemented. What we have to do next is to implement these operators for the new accelerators.
C++
1at::Tensorempty_memory_format( 2c10::IntArrayRefsize, 3std::optional<c10::ScalarType>dtype_opt, 4std::optional<c10::Layout>layout_opt, 5std::optional<c10::Device>device_opt, 6std::optional<bool>pin_memory_opt, 7std::optional<c10::MemoryFormat>memory_format_opt){ 8constautodevice=c10::device_or_default(device_opt); 9constautodtype=c10::dtype_or_default(dtype_opt);10TORCH_CHECK(device.is_privateuseone());11TORCH_CHECK(12c10::layout_or_default(layout_opt)==c10::Layout::Strided,13"Non strided layout not supported");14TORCH_CHECK(15!c10::pinned_memory_or_default(pin_memory_opt),16"Pin memory can only be on CPU");17constc10::DeviceGuarddevice_guard(device);18constexprc10::DispatchKeySetpu1_dks(c10::DispatchKey::PrivateUse1);19autoallocator=at::GetAllocator(at::kPrivateUse1);20returnat::detail::empty_generic(21size,allocator,pu1_dks,dtype,memory_format_opt);22}
Taking the empty.memory_format operator as an example, we first need to query the operator’s schema information in native_functions.yaml, which contains detailed signature information. Then, we can implement the operator based on the capabilities of the new accelerator.
After completing the wrapper_empty_memory_format, we can register aten::empty.memory_format for PrivateUse1 through TORCH_LIBRARY_IMPL.
Step 2
By following Step 1, we can complete the development and registration of all operators except fallback. Next, to support operators related to operations (such as mathematical operations and convolution operations), we need to implement the registration of fallback semantics. This is a built-in capability provided by the PyTorch framework that can fallback some operations that are not supported by new accelerators to the CPU for execution. For new backends in development, this is an extremely effective way to ensure functionality at the expense of performance.
Enabling the fallback mechanism only for certain operators, while following PyTorch’s default behavior for other operators (an error will be reported if the accelerator does not have a corresponding operator implementation), this is a very reasonable scenario as well.
Per-operator fallbacks are very similar to global fallbacks, the only difference being the registration method: calling m.impl registers an implementation for a specific operator, while m.fallback registers a default implementation for all operators.
Of course, global fallbacks can also be combined with a blacklist of fallbacks, which is a common approach, especially when only a few operators do not support fallbacks.
PyTorch STUB
PyTorch also provides another approach for built-in operators: STUB. This method is essentially based on the Step 1 approach, but adds secondary scheduling capabilities (for example, scheduling based on CPU characteristics).
pushd${
findaten-typef-a-namexargs-Igrep-wl
Custom Operators
In addition to PyTorch’s built-in operators, custom accelerator operators are also very common to improve performance in specific scenarios. These can be categorized into three main approaches:
Forward-only
Forward and backward: Separate registration
Forward and backward: Implemented using torch.autograd.Function
Forward Only
Here, we’ll briefly introduce the implementation process of custom operators, focusing on the forward-only approach. The implementation can be summarized into the following three points:
Define Schema:
C++
Tools
Operator registration in PyTorch is complex, with diverse registration methods and numerous scenarios. Therefore, the PyTorch community has provided a number of tools to help developers quickly understand the underlying principles and assist in troubleshooting. Here we briefly introduce several commonly used tools:
Commands
PyTorch provides a set of commands prefixed with torch._C._dispatch_ around its Dispatch feature. You can query all related interfaces using the following command:
python-c'import torch; print("\n".join([x for x in dir(torch._C) if x.startswith("_dispatch_")]))'
...
_dispatch_dump
_dispatch_dump_table
_dispatch_has_kernel
_dispatch_has_kernel_for_any_dispatch_key
_dispatch_has_kernel_for_dispatch_key
_dispatch_isTensorSubclassLike
_dispatch_is_alias_key
_dispatch_is_included_in_alias
_dispatch_is_main_interpreter
_dispatch_kernel_for_dispatch_key_is_fallthrough
_dispatch_key_for_device
_dispatch_key_name
_dispatch_key_parse
_dispatch_key_set
...
Here are explanations for several commonly used commands:
torch._C._dispatch_key_set:
Displays the DispatchKey of the current Tensor, with priority increasing from left to right.
wrapper_cpu_fallback wraps the at::native::cpu_fallback method provided by PyTorch and is registered with PrivateUse1 in PyTorch via TORCH_LIBRARY_IMPL. Subsequent operations not supported by the new backend will automatically fall back to the CPU for execution, and the results will be passed back to the new backend after execution.
::
OperatorName
>
cpu_fallback_blocklist
=
{
3c10::OperatorName("aten::abs",""),
4c10::OperatorName("aten::abs","out"),
5};
6
7
const
auto
&
op_name
=
op
.
schema
().
operator_name
();
8
if
(
cpu_fallback_blocklist
.
count
(
op_name
))
{
9
TORCH_CHECK
(
10
false
,
11
"Operator '"
,
12
op_name
,
13
"' is not implemented for device openreg."
);
14
}
else
{
15
at
::
native
::
cpu_fallback
(
op
,
stack
);
16
}
17
}
TORCH_ROOT
}
"*.h"
|
{}
"^DECLARE_DISPATCH"
{}
popd
DECLARE_DISPATCH is a macro used to explicitly declare STUB. It is currently distributed in the aten directory. Based on this macro, you can find all operators that can be integrated using the STUB method.
The above listing contains the file that declares the STUB operator, where you can clearly see the STUB name and the associated function signature. Next, we will take abs_stub as an example to briefly introduce the path to support operators through STUB.
C++
1voidabs_kernel(at::TensorIteratorBase&iter){ 2TORCH_CHECK(iter.ntensors()==2,"Abs kernel expects 2 tensors"); 3TORCH_CHECK( 4iter.common_dtype()==at::ScalarType::Float, 5"Abs kernel only supports float type"); 6 7auto&output_tensor=iter.tensor(0); 8auto&input_tensor=iter.tensor(1); 910TORCH_CHECK(11input_tensor.sizes()==output_tensor.sizes(),12"Input and output tensor sizes must match.");1314autoabs_loop=[](float*out_ptr,constfloat*in_ptr,int64_tn){15for(int64_ti=0;i<n;++i){16out_ptr[i]=std::abs(in_ptr[i]);17}18};1920MemoryGuardguard(input_tensor,output_tensor);2122if(iter.is_contiguous()){23abs_loop(24static_cast<float*>(iter.data_ptr(0)),25static_cast<float*>(iter.data_ptr(1)),26iter.numel());27}else{28TORCH_CHECK(29input_tensor.is_contiguous(),"Input tensor must be contiguous.")3031autooutput=at::empty(32input_tensor.sizes(),33input_tensor.options().memory_format(34input_tensor.suggest_memory_format()));3536MemoryGuardguard(output);3738abs_loop(39static_cast<float*>(output.data_ptr()),40static_cast<float*>(iter.data_ptr(1)),41iter.numel());4243output_tensor.copy_(output);44}45}
From the signature, we can see that the input of abs_stub is TensorIteratorBase, a powerful helper class provided by PyTorch that contains all input and output operators, as well as some other auxiliary methods. Based on it, we can develop the abs_kernel operator and then call REGISTER_PRIVATEUSE1_DISPATCH to specify abs_stub to complete the registration.
Use TORCH_LIBRARY_IMPL to register the wrapper_custom_abs implementation for the custom_abs operator in PrivateUse1. Because Autograd is always enabled in PyTorch, PyTorch defaults to finding and executing the corresponding backward implementation even if only forward computation is required(will fallthrough in backward implementation). Fortunately, PyTorch have implemented a general AutogradFallback for PrivateUse1 as well, if only forward computation is involved, it is equivalent to a fallthrough operation, selecting the next DispatchKey for computation; if backward computation is involved, an error is thrown.
Register Metadata(optional, but required by the graph mode, etc.):
PyTorch supports registering Meta in both C++ and Python. Since Python registration is simpler, Python is used as an example here. Similar to the TORCH_LIBRARY_IMPL function in C++, Python provides the more user-friendly torch.library.impl decorator.
ATen
/
RegisterCPU_0
.
cpp
:
1309
[
kernel
]
CUDA
:
registered
at
./
build
/
aten
/
src
/
ATen
/
RegisterCUDA_0
.
cpp
:
2420
[
kernel
]
HIP
:
registered
at
./
build
/
aten
/
src
/
ATen
/
RegisterCompositeExplicitAutogradNonFunctional_0
.
cpp
:
1373
[
default
backend
kernel
]
MPS
:
registered
at
./
build
/
aten
/
src
/
ATen
/
RegisterCompositeExplicitAutogradNonFunctional_0
.
cpp
:
1373
[
default
backend
kernel
]
IPU
:
registered
at
./
build
/
aten
/
src
/
ATen
/
RegisterCompositeExplicitAutogradNonFunctional_0
.
cpp
:
1373
[
default
backend
kernel
]
XPU
:
registered
at
./
build
/
aten
/
src
/
ATen
/
RegisterCompositeExplicitAutogradNonFunctional_0
.
cpp
:
1373
[
default
backend
kernel
]
HPU
:
registered
at
./
build
/
aten
/
src
/
ATen
/
RegisterCompositeExplicitAutogradNonFunctional_0
.
cpp
:
1373
[
default
backend
kernel
]
VE
:
registered
at
./
build
/
aten
/
src
/
ATen
/
RegisterCompositeExplicitAutogradNonFunctional_0
.
cpp
:
1373
[
default
backend
kernel
]
MTIA
:
registered
at
./
build
/
aten
/
src
/
ATen
/
RegisterCompositeExplicitAutogradNonFunctional_0
.
cpp
:
1373
[
default
backend
kernel
]
MAIA
:
registered
at
./
build
/
aten
/
src
/
ATen
/
RegisterCompositeExplicitAutogradNonFunctional_0
.
cpp
:
1373
[
default
backend
kernel
]
PrivateUse1
:
registered
at
./
build
/
aten
/
src
/
ATen
/
RegisterCompositeExplicitAutogradNonFunctional_0
.
cpp
:
1373
[
default
backend
kernel
]
...
You can easily query the corresponding implementation of the aten::add.Tensor operator on other platforms, so that you can track the entire operator calling process from the source code level.
)
[
call
]
op
=
[
aten
::
randn
],
key
=
[
BackendSelect
]
[
redispatch
]
op
=
[
aten
::
randn
],
key
=
[
CPU
]
[
call
]
op
=
[
aten
::
empty
.
memory_format
],
key
=
[
BackendSelect
]
[
redispatch
]
op
=
[
aten
::
empty
.
memory_format
],
key
=
[
CPU
]
[
call
]
op
=
[
aten
::
normal_
],
key
=
[
CPU
]
You can clearly see all the underlying operators called by Python-level operators within PyTorch: including the operator name, calling hierarchy, and corresponding DispatchKey.