forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackend.h
168 lines (153 loc) · 4.23 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
#pragma once
#include <c10/DeviceType.h>
#include <ATen/core/TensorTypeId.h>
#include <ATen/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, SparseCPU, SparseCUDA, Undefined, NumOptions };
static inline Backend toSparse(Backend b) {
switch (b) {
case Backend::CPU:
return Backend::SparseCPU;
case Backend::CUDA:
return Backend::SparseCUDA;
case Backend::SparseCPU:
return Backend::SparseCPU;
case Backend::SparseCUDA:
return Backend::SparseCUDA;
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::SparseCPU:
return Backend::CPU;
case Backend::SparseCUDA:
return Backend::CUDA;
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 == SparseCPUTensorId()) {
return Backend::SparseCPU;
} else if (t == SparseCUDATensorId()) {
return Backend::SparseCUDA;
} 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::SparseCPU:
return SparseCPUTensorId();
case Backend::SparseCUDA:
return SparseCUDATensorId();
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::SparseCPU:
return DeviceType::CPU;
case Backend::SparseCUDA:
return DeviceType::CUDA;
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;
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::SparseCPU:
return Backend::SparseCPU;
case Backend::SparseCUDA:
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:
return Backend::CUDA;
case Backend::CUDA:
return Backend::CUDA;
case Backend::SparseCPU:
return Backend::SparseCUDA;
case Backend::SparseCUDA:
return Backend::SparseCUDA;
case Backend::Undefined:
return Backend::Undefined;
default:
AT_ERROR("Unknown backend");
}
}
constexpr DeviceType kCPU = DeviceType::CPU;
constexpr DeviceType kCUDA = DeviceType::CUDA;
static inline const char* toString(Backend b) {
switch (b) {
case Backend::CPU:
return "CPU";
case Backend::CUDA:
return "CUDA";
case Backend::SparseCPU:
return "SparseCPU";
case Backend::SparseCUDA:
return "SparseCUDA";
default:
return "UNKNOWN_BACKEND";
}
}
} // namespace at