forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackend.h
216 lines (200 loc) · 5.49 KB
/
Backend.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#pragma once
#include <c10/DeviceType.h>
#include <c10/core/TensorTypeId.h>
#include <c10/core/TensorTypeIdRegistration.h>
#include <c10/util/Exception.h>
#include <stdexcept>
namespace at {
/**
* This legacy enum class defines the set of backends supported by
* old school, code generated Type-based ATen. The reason we are
* sunsetting this enum class is because it doesn't allow for
* open registration of backends. TensorTypeId is the replacement
* for Backend which supports open registration.
*
* ARE YOU SURE YOU WANT TO USE THIS TYPE? Think about if SparseCPU/SparseCUDA
* would make sense in your use case. If it doesn't make sense, maybe
* you want DeviceType.
*/
enum class Backend { CPU, CUDA, HIP, SparseCPU, SparseCUDA, SparseHIP, Undefined, NumOptions };
static inline Backend toSparse(Backend b) {
switch (b) {
case Backend::CPU:
return Backend::SparseCPU;
case Backend::CUDA:
return Backend::SparseCUDA;
case Backend::HIP:
return Backend::SparseHIP;
case Backend::SparseCPU:
return Backend::SparseCPU;
case Backend::SparseCUDA:
return Backend::SparseCUDA;
case Backend::SparseHIP:
return Backend::SparseHIP;
default:
throw std::runtime_error("Unknown backend");
}
}
static inline Backend toDense(Backend b) {
switch (b) {
case Backend::CPU:
return Backend::CPU;
case Backend::CUDA:
return Backend::CUDA;
case Backend::HIP:
return Backend::HIP;
case Backend::SparseCPU:
return Backend::CPU;
case Backend::SparseCUDA:
return Backend::CUDA;
case Backend::SparseHIP:
return Backend::HIP;
default:
throw std::runtime_error("Unknown backend");
}
}
static inline Backend tensorTypeIdToBackend(TensorTypeId t) {
if (t == CPUTensorId()) {
return Backend::CPU;
} else if (t == CUDATensorId()) {
return Backend::CUDA;
} else if (t == HIPTensorId()) {
return Backend::HIP;
} else if (t == SparseCPUTensorId()) {
return Backend::SparseCPU;
} else if (t == SparseCUDATensorId()) {
return Backend::SparseCUDA;
} else if (t == SparseHIPTensorId()) {
return Backend::SparseHIP;
} else if (t == UndefinedTensorId()) {
return Backend::Undefined;
} else {
AT_ERROR("Unrecognized tensor type ID: ", t);
}
}
static inline TensorTypeId backendToTensorTypeId(Backend b) {
switch (b) {
case Backend::CPU:
return CPUTensorId();
case Backend::CUDA:
return CUDATensorId();
case Backend::HIP:
return HIPTensorId();
case Backend::SparseCPU:
return SparseCPUTensorId();
case Backend::SparseCUDA:
return SparseCUDATensorId();
case Backend::SparseHIP:
return SparseHIPTensorId();
case Backend::Undefined:
return UndefinedTensorId();
default:
throw std::runtime_error("Unknown backend");
}
}
static inline DeviceType backendToDeviceType(Backend b) {
switch (b) {
case Backend::CPU:
return DeviceType::CPU;
case Backend::CUDA:
return DeviceType::CUDA;
case Backend::HIP:
return DeviceType::HIP;
case Backend::SparseCPU:
return DeviceType::CPU;
case Backend::SparseCUDA:
return DeviceType::CUDA;
case Backend::SparseHIP:
return DeviceType::HIP;
case Backend::Undefined:
AT_ERROR("Undefined backend is not a valid device type");
default:
AT_ERROR("Unknown backend");
}
}
static inline Backend deviceTypeToBackend(DeviceType d) {
switch (d) {
case DeviceType::CPU:
return Backend::CPU;
case DeviceType::CUDA:
return Backend::CUDA;
case DeviceType::HIP:
return Backend::HIP;
default:
AT_ERROR("Unknown device type ", d);
}
}
static inline Backend backendToCPU(Backend b) {
switch (b) {
case Backend::CPU:
return Backend::CPU;
case Backend::CUDA:
return Backend::CPU;
case Backend::HIP:
return Backend::CPU;
case Backend::SparseCPU:
return Backend::SparseCPU;
case Backend::SparseCUDA:
return Backend::SparseCPU;
case Backend::SparseHIP:
return Backend::SparseCPU;
case Backend::Undefined:
return Backend::Undefined;
default:
AT_ERROR("Unknown backend");
}
}
static inline Backend backendToCUDA(Backend b) {
switch (b) {
case Backend::CPU:
case Backend::CUDA:
case Backend::HIP:
return Backend::CUDA;
case Backend::SparseCPU:
case Backend::SparseCUDA:
case Backend::SparseHIP:
return Backend::SparseCUDA;
case Backend::Undefined:
return Backend::Undefined;
default:
AT_ERROR("Unknown backend");
}
}
static inline Backend backendToHIP(Backend b) {
switch (b) {
case Backend::CPU:
case Backend::CUDA:
case Backend::HIP:
return Backend::HIP;
case Backend::SparseCPU:
case Backend::SparseCUDA:
case Backend::SparseHIP:
return Backend::SparseHIP;
case Backend::Undefined:
return Backend::Undefined;
default:
AT_ERROR("Unknown backend");
}
}
constexpr DeviceType kCPU = DeviceType::CPU;
constexpr DeviceType kCUDA = DeviceType::CUDA;
constexpr DeviceType kHIP = DeviceType::HIP;
static inline const char* toString(Backend b) {
switch (b) {
case Backend::CPU:
return "CPU";
case Backend::CUDA:
return "CUDA";
case Backend::HIP:
return "HIP";
case Backend::SparseCPU:
return "SparseCPU";
case Backend::SparseCUDA:
return "SparseCUDA";
case Backend::SparseHIP:
return "SparseHIP";
default:
return "UNKNOWN_BACKEND";
}
}
} // namespace c10