|
1 | 1 | #pragma once
|
2 |
| - |
3 |
| -#include <ATen/core/TensorTypeId.h> |
4 |
| -#include <ATen/core/TensorTypeIdRegistration.h> |
5 |
| -#include <ATen/core/Error.h> |
6 |
| -#include <ATen/core/DeviceType.h> |
7 |
| - |
8 |
| -#include <stdexcept> |
9 |
| - |
10 |
| -namespace at { |
11 |
| - |
12 |
| -/** |
13 |
| - * This legacy enum class defines the set of backends supported by |
14 |
| - * old school, code generated Type-based ATen. The reason we are |
15 |
| - * sunsetting this enum class is because it doesn't allow for |
16 |
| - * open registration of backends. TensorTypeId is the replacement |
17 |
| - * for Backend which supports open registration. |
18 |
| - * |
19 |
| - * ARE YOU SURE YOU WANT TO USE THIS TYPE? Think about if SparseCPU/SparseCUDA |
20 |
| - * would make sense in your use case. If it doesn't make sense, maybe |
21 |
| - * you want DeviceType. |
22 |
| - */ |
23 |
| -enum class Backend { CPU, CUDA, SparseCPU, SparseCUDA, Undefined, NumOptions }; |
24 |
| - |
25 |
| -static inline Backend toSparse(Backend b) { |
26 |
| - switch (b) { |
27 |
| - case Backend::CPU: |
28 |
| - return Backend::SparseCPU; |
29 |
| - case Backend::CUDA: |
30 |
| - return Backend::SparseCUDA; |
31 |
| - case Backend::SparseCPU: |
32 |
| - return Backend::SparseCPU; |
33 |
| - case Backend::SparseCUDA: |
34 |
| - return Backend::SparseCUDA; |
35 |
| - default: |
36 |
| - throw std::runtime_error("Unknown backend"); |
37 |
| - } |
38 |
| -} |
39 |
| - |
40 |
| -static inline Backend toDense(Backend b) { |
41 |
| - switch (b) { |
42 |
| - case Backend::CPU: |
43 |
| - return Backend::CPU; |
44 |
| - case Backend::CUDA: |
45 |
| - return Backend::CUDA; |
46 |
| - case Backend::SparseCPU: |
47 |
| - return Backend::CPU; |
48 |
| - case Backend::SparseCUDA: |
49 |
| - return Backend::CUDA; |
50 |
| - default: |
51 |
| - throw std::runtime_error("Unknown backend"); |
52 |
| - } |
53 |
| -} |
54 |
| - |
55 |
| -static inline Backend tensorTypeIdToBackend(TensorTypeId t) { |
56 |
| - if (t == CPUTensorId()) { |
57 |
| - return Backend::CPU; |
58 |
| - } else if (t == CUDATensorId()) { |
59 |
| - return Backend::CUDA; |
60 |
| - } else if (t == SparseCPUTensorId()) { |
61 |
| - return Backend::SparseCPU; |
62 |
| - } else if (t == SparseCUDATensorId()) { |
63 |
| - return Backend::SparseCUDA; |
64 |
| - } else if (t == UndefinedTensorId()) { |
65 |
| - return Backend::Undefined; |
66 |
| - } else { |
67 |
| - AT_ERROR("Unrecognized tensor type ID: ", t); |
68 |
| - } |
69 |
| -} |
70 |
| - |
71 |
| -static inline TensorTypeId backendToTensorTypeId(Backend b) { |
72 |
| - switch (b) { |
73 |
| - case Backend::CPU: |
74 |
| - return CPUTensorId(); |
75 |
| - case Backend::CUDA: |
76 |
| - return CUDATensorId(); |
77 |
| - case Backend::SparseCPU: |
78 |
| - return SparseCPUTensorId(); |
79 |
| - case Backend::SparseCUDA: |
80 |
| - return SparseCUDATensorId(); |
81 |
| - case Backend::Undefined: |
82 |
| - return UndefinedTensorId(); |
83 |
| - default: |
84 |
| - throw std::runtime_error("Unknown backend"); |
85 |
| - } |
86 |
| -} |
87 |
| - |
88 |
| -static inline DeviceType backendToDeviceType(Backend b) { |
89 |
| - switch (b) { |
90 |
| - case Backend::CPU: |
91 |
| - return DeviceType::CPU; |
92 |
| - case Backend::CUDA: |
93 |
| - return DeviceType::CUDA; |
94 |
| - case Backend::SparseCPU: |
95 |
| - return DeviceType::CPU; |
96 |
| - case Backend::SparseCUDA: |
97 |
| - return DeviceType::CUDA; |
98 |
| - case Backend::Undefined: |
99 |
| - AT_ERROR("Undefined backend is not a valid device type"); |
100 |
| - default: |
101 |
| - AT_ERROR("Unknown backend"); |
102 |
| - } |
103 |
| -} |
104 |
| - |
105 |
| -static inline Backend deviceTypeToBackend(DeviceType d) { |
106 |
| - switch (d) { |
107 |
| - case DeviceType::CPU: |
108 |
| - return Backend::CPU; |
109 |
| - case DeviceType::CUDA: |
110 |
| - return Backend::CUDA; |
111 |
| - default: |
112 |
| - AT_ERROR("Unknown device type ", d); |
113 |
| - } |
114 |
| -} |
115 |
| - |
116 |
| -static inline Backend backendToCPU(Backend b) { |
117 |
| - switch (b) { |
118 |
| - case Backend::CPU: |
119 |
| - return Backend::CPU; |
120 |
| - case Backend::CUDA: |
121 |
| - return Backend::CPU; |
122 |
| - case Backend::SparseCPU: |
123 |
| - return Backend::SparseCPU; |
124 |
| - case Backend::SparseCUDA: |
125 |
| - return Backend::SparseCPU; |
126 |
| - case Backend::Undefined: |
127 |
| - return Backend::Undefined; |
128 |
| - default: |
129 |
| - AT_ERROR("Unknown backend"); |
130 |
| - } |
131 |
| -} |
132 |
| - |
133 |
| -static inline Backend backendToCUDA(Backend b) { |
134 |
| - switch (b) { |
135 |
| - case Backend::CPU: |
136 |
| - return Backend::CUDA; |
137 |
| - case Backend::CUDA: |
138 |
| - return Backend::CUDA; |
139 |
| - case Backend::SparseCPU: |
140 |
| - return Backend::SparseCUDA; |
141 |
| - case Backend::SparseCUDA: |
142 |
| - return Backend::SparseCUDA; |
143 |
| - case Backend::Undefined: |
144 |
| - return Backend::Undefined; |
145 |
| - default: |
146 |
| - AT_ERROR("Unknown backend"); |
147 |
| - } |
148 |
| -} |
149 |
| - |
150 |
| -constexpr DeviceType kCPU = DeviceType::CPU; |
151 |
| -constexpr DeviceType kCUDA = DeviceType::CUDA; |
152 |
| - |
153 |
| -static inline const char* toString(Backend b) { |
154 |
| - switch (b) { |
155 |
| - case Backend::CPU: |
156 |
| - return "CPU"; |
157 |
| - case Backend::CUDA: |
158 |
| - return "CUDA"; |
159 |
| - case Backend::SparseCPU: |
160 |
| - return "SparseCPU"; |
161 |
| - case Backend::SparseCUDA: |
162 |
| - return "SparseCUDA"; |
163 |
| - default: |
164 |
| - return "UNKNOWN_BACKEND"; |
165 |
| - } |
166 |
| -} |
167 |
| - |
168 |
| -} // namespace at |
| 2 | +#include <ATen/core/Backend.h> |
0 commit comments