Skip to content

Commit 809660f

Browse files
ezyangfacebook-github-bot
authored andcommitted
ATen DerivedType is dead, long live ATen RegisterDispatchKey (#47011)
Summary: Pull Request resolved: #47011 smessmer has complained about how it is difficult to find generated code. Well hopefully this diffs helps a bit with that. There are three components to this refactor: - Rename TypeDerived (CPUType) to RegisterDispatchKey (RegisterCPU). The 'Type' nomenclature is vestigial and I think Register says what these files do a lot more clearly. I also got rid of the CPUType namespace; everything just goes in anonymous namespace now, less moving parts this way. - Give Math and DefaultBackend their own files (RegisterMath and RegisterDefaultBackend) - Restructure code generation so that schema definition is done completely separately from RegisterDispatchKey I decided to name the files RegisterCPU rather than the old convention BackendSelectRegister, because it seems better to me if these files clump together in an alphabetical listing rather than being spread out everywhere. There are a few manual registration files which should probably get similar renaming. I also did a little garden cleaning about how we identify if a dispatch key is a cuda key or a generic key (previously called KEYWORD_ALL_BACKENDS but I like my naming better). Signed-off-by: Edward Z. Yang <[email protected]> Differential Revision: D24600806 Test Plan: Imported from OSS Reviewed By: smessmer Pulled By: ezyang fbshipit-source-id: c1b510dd7515bd95e3ad25b8edf961b2fb30a25a
1 parent 00a3add commit 809660f

File tree

6 files changed

+90
-135
lines changed

6 files changed

+90
-135
lines changed

BUILD.bazel

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,17 @@ genrule(
125125
] + glob(["aten/src/ATen/templates/**"]),
126126
outs = [
127127
"aten/src/ATen/Declarations.yaml",
128-
"aten/src/ATen/BackendSelectRegister.cpp",
129-
"aten/src/ATen/CPUType.cpp",
128+
"aten/src/ATen/RegisterBackendSelect.cpp",
129+
"aten/src/ATen/RegisterCPU.cpp",
130+
"aten/src/ATen/RegisterMkldnnCPU.cpp",
131+
"aten/src/ATen/RegisterQuantizedCPU.cpp",
132+
"aten/src/ATen/RegisterSparseCPU.cpp",
133+
"aten/src/ATen/RegisterMath.cpp",
134+
"aten/src/ATen/RegisterDefaultBackend.cpp",
135+
"aten/src/ATen/RegisterSchema.cpp",
130136
"aten/src/ATen/Functions.h",
131137
"aten/src/ATen/Functions.cpp",
132138
"aten/src/ATen/NativeFunctions.h",
133-
"aten/src/ATen/MkldnnCPUType.cpp",
134-
"aten/src/ATen/QuantizedCPUType.cpp",
135-
"aten/src/ATen/SparseCPUType.cpp",
136-
"aten/src/ATen/TypeDefault.cpp",
137139
"aten/src/ATen/core/TensorBody.h",
138140
"aten/src/ATen/core/TensorMethods.cpp",
139141
"aten/src/ATen/core/ATenOpList.cpp",
File renamed without changes.

aten/src/ATen/templates/TypeDerived.cpp renamed to aten/src/ATen/templates/RegisterDispatchKey.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,14 @@
3333

3434
namespace at {
3535

36-
/* example
37-
Tensor * ${Type}::add(Tensor & a, Tensor & b) {
38-
std::cout << "add Tensor with backend ${Backend}\n";
39-
return &a;
40-
}
41-
*/
42-
43-
namespace ${Type} {
36+
namespace {
4437

45-
${type_derived_method_definitions}
38+
${dispatch_definitions}
4639

47-
} // namespace ${Type}
48-
49-
TORCH_LIBRARY_IMPL(aten, ${Backend}, m) {
50-
${function_registrations}
40+
TORCH_LIBRARY_IMPL(aten, ${DispatchKey}, m) {
41+
${dispatch_registrations}
5142
}
5243

44+
} // anonymous namespace
45+
5346
} // namespace at

aten/src/ATen/templates/TypeDefault.cpp renamed to aten/src/ATen/templates/RegisterSchema.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,8 @@
1717
#include <torch/library.h>
1818

1919
namespace at {
20-
namespace TypeDefault {
21-
22-
${type_method_definitions}
23-
24-
} // namespace TypeDefault
25-
2620
TORCH_LIBRARY(aten, m) {
27-
${function_registrations};
21+
${schema_registrations};
2822

2923
// String Ops
3024
// Implementations located in torch/csrc/jit/runtime/register_prim_ops.cpp
@@ -63,12 +57,4 @@ TORCH_LIBRARY(aten, m) {
6357
// Implementations located in torch/csrc/jit/runtime/register_distributed_ops.cpp
6458
m.def("get_gradients(int context_id) -> Dict(Tensor, Tensor)");
6559
}
66-
67-
TORCH_LIBRARY_IMPL(aten, Math, m) {
68-
${math_function_registrations};
69-
}
70-
71-
TORCH_LIBRARY_IMPL(aten, DefaultBackend, m) {
72-
${default_backend_function_registrations};
73-
}
7460
} // namespace at

c10/core/DispatchKey.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ enum class DispatchKey : uint8_t {
5151

5252
// Here are backends which you think of as traditionally specifying
5353
// how to implement operations on some device.
54-
CPU, // registered at build/aten/src/ATen/CPUType.cpp
55-
CUDA, // registered at build/aten/src/ATen/CUDAType.cpp
54+
CPU, // registered at build/aten/src/ATen/RegisterCPU.cpp
55+
CUDA, // registered at build/aten/src/ATen/RegisterCUDA.cpp
5656
HIP, // NB: I think this is not actually used, due to Note [Masquerading as
5757
// CUDA]
5858
FPGA, // Xilinx support lives out of tree at https://gitlab.com/pytorch-complex/vitis_kernels
@@ -73,8 +73,8 @@ enum class DispatchKey : uint8_t {
7373

7474
// Here are backends which specify more specialized operators
7575
// based on the dtype of the tensor.
76-
QuantizedCPU, // registered at build/aten/src/ATen/QuantizedCPUType.cpp
77-
QuantizedCUDA, // registered at build/aten/src/ATen/QuantizedCUDAType.cpp
76+
QuantizedCPU, // registered at build/aten/src/ATen/RegisterQuantizedCPU.cpp
77+
QuantizedCUDA, // registered at build/aten/src/ATen/RegisterQuantizedCUDA.cpp
7878
ComplexCPU, // lives out of tree at
7979
// https://gitlab.com/pytorch-complex/pytorch-cpu-strided-complex
8080
ComplexCUDA, // and
@@ -97,10 +97,10 @@ enum class DispatchKey : uint8_t {
9797
// based on the layout of the tensor. Note that the sparse backends
9898
// are one case where ordering matters: sparse multi-dispatches with
9999
// the corresponding dense tensors, and must be handled before them.
100-
MkldnnCPU, // registered at build/aten/src/ATen/MkldnnCPUType.cpp
100+
MkldnnCPU, // registered at build/aten/src/ATen/RegisterMkldnnCPU.cpp
101101
// NB: not to be confused with MKLDNN, which is Caffe2 only
102-
SparseCPU, // registered at build/aten/src/ATen/SparseCPUType.cpp
103-
SparseCUDA, // registered at build/aten/src/ATen/SparseCUDAType.cpp
102+
SparseCPU, // registered at build/aten/src/ATen/RegisterSparseCPU.cpp
103+
SparseCUDA, // registered at build/aten/src/ATen/RegisterSparseCUDA.cpp
104104
SparseHIP, // TODO: I think this is not actually used, due to Note
105105
// [Masquerading as CUDA]
106106

@@ -276,8 +276,8 @@ enum class DispatchKey : uint8_t {
276276

277277
// See Note [Alias Dispatch Key : Autograd]
278278
Autograd,
279-
Math,
280-
DefaultBackend,
279+
Math, // registered at build/aten/src/ATen/RegisterMath.cpp
280+
DefaultBackend, // registered at build/aten/src/ATen/RegisterDefaultBackend.cpp
281281

282282
// Define an alias key to represent end of alias dispatch keys.
283283
// If you add new alias keys after Autograd, please also update it here.

0 commit comments

Comments
 (0)