forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeviceType.cpp
67 lines (62 loc) · 2.22 KB
/
DeviceType.cpp
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
#include <c10/DeviceType.h>
#include <c10/util/Exception.h>
namespace c10 {
std::string DeviceTypeName(DeviceType d, bool lower_case) {
switch (d) {
// I considered instead using ctype::tolower to lower-case the strings
// on the fly, but this seemed a bit much.
case DeviceType::CPU:
return lower_case ? "cpu" : "CPU";
case DeviceType::CUDA:
return lower_case ? "cuda" : "CUDA";
case DeviceType::OPENGL:
return lower_case ? "opengl" : "OPENGL";
case DeviceType::OPENCL:
return lower_case ? "opencl" : "OPENCL";
case DeviceType::MKLDNN:
return lower_case ? "mkldnn" : "MKLDNN";
case DeviceType::IDEEP:
return lower_case ? "ideep" : "IDEEP";
case DeviceType::HIP:
return lower_case ? "hip" : "HIP";
case DeviceType::FPGA:
return lower_case ? "fpga" : "FPGA";
default:
AT_ERROR(
"Unknown device: ",
static_cast<int16_t>(d),
". If you have recently updated the caffe2.proto file to add a new "
"device type, did you forget to update the DeviceTypeName() "
"function to reflect such recent changes?");
// The below code won't run but is needed to suppress some compiler
// warnings.
return "";
}
}
// NB: Per the C++ standard (e.g.,
// https://stackoverflow.com/questions/18195312/what-happens-if-you-static-cast-invalid-value-to-enum-class)
// as long as you cast from the same underlying type, it is always valid to cast
// into an enum class (even if the value would be invalid by the enum.) Thus,
// the caller is allowed to cast a possibly invalid int16_t to DeviceType and
// then pass it to this function. (I considered making this function take an
// int16_t directly, but that just seemed weird.)
bool isValidDeviceType(DeviceType d) {
switch (d) {
case DeviceType::CPU:
case DeviceType::CUDA:
case DeviceType::OPENGL:
case DeviceType::OPENCL:
case DeviceType::MKLDNN:
case DeviceType::IDEEP:
case DeviceType::HIP:
case DeviceType::FPGA:
return true;
default:
return false;
}
}
std::ostream& operator<<(std::ostream& stream, DeviceType type) {
stream << DeviceTypeName(type, /* lower case */ true);
return stream;
}
} // namespace c10