2 #include <c10/core/Device.h> 3 #include <c10/util/Exception.h> 4 #include <caffe2/proto/caffe2.pb.h> 8 using DeviceType = at::DeviceType;
9 constexpr DeviceType CPU = DeviceType::CPU;
10 constexpr DeviceType CUDA = DeviceType::CUDA;
11 constexpr DeviceType OPENGL = DeviceType::OPENGL;
12 constexpr DeviceType OPENCL = DeviceType::OPENCL;
13 constexpr DeviceType MKLDNN = DeviceType::MKLDNN;
14 constexpr DeviceType IDEEP = DeviceType::IDEEP;
15 constexpr DeviceType HIP = DeviceType::HIP;
16 constexpr DeviceType COMPILE_TIME_MAX_DEVICE_TYPES =
17 DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES;
18 constexpr DeviceType ONLY_FOR_TEST = DeviceType::ONLY_FOR_TEST;
20 inline CAFFE2_API DeviceType ProtoToType(
const caffe2::DeviceTypeProto p) {
22 case caffe2::PROTO_CPU:
23 return DeviceType::CPU;
24 case caffe2::PROTO_CUDA:
25 return DeviceType::CUDA;
26 case caffe2::PROTO_OPENGL:
27 return DeviceType::OPENGL;
28 case caffe2::PROTO_OPENCL:
29 return DeviceType::OPENCL;
30 case caffe2::PROTO_MKLDNN:
31 return DeviceType::MKLDNN;
32 case caffe2::PROTO_IDEEP:
33 return DeviceType::IDEEP;
34 case caffe2::PROTO_HIP:
35 return DeviceType::HIP;
36 case caffe2::PROTO_COMPILE_TIME_MAX_DEVICE_TYPES:
37 return DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES;
38 case caffe2::PROTO_ONLY_FOR_TEST:
39 return DeviceType::ONLY_FOR_TEST;
43 static_cast<int32_t>(p),
44 ". If you have recently updated the caffe2.proto file to add a new " 45 "device type, did you forget to update the ProtoToType() and TypeToProto" 46 "function to reflect such recent changes?");
50 inline CAFFE2_API DeviceType ProtoToType(
int p) {
51 return ProtoToType(static_cast<caffe2::DeviceTypeProto>(p));
54 inline CAFFE2_API DeviceTypeProto TypeToProto(
const DeviceType& t) {
57 return caffe2::PROTO_CPU;
58 case DeviceType::CUDA:
59 return caffe2::PROTO_CUDA;
60 case DeviceType::OPENGL:
61 return caffe2::PROTO_OPENGL;
62 case DeviceType::OPENCL:
63 return caffe2::PROTO_OPENCL;
64 case DeviceType::MKLDNN:
65 return caffe2::PROTO_MKLDNN;
66 case DeviceType::IDEEP:
67 return caffe2::PROTO_IDEEP;
69 return caffe2::PROTO_HIP;
70 case DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES:
71 return caffe2::PROTO_COMPILE_TIME_MAX_DEVICE_TYPES;
72 case DeviceType::ONLY_FOR_TEST:
73 return caffe2::PROTO_ONLY_FOR_TEST;
77 static_cast<int32_t>(t),
78 ". If you have recently updated the caffe2.proto file to add a new " 79 "device type, did you forget to update the ProtoToType() and TypeToProto" 80 "function to reflect such recent changes?");
84 inline CAFFE2_API caffe2::DeviceOption DeviceToOption(
86 caffe2::DeviceOption option;
87 auto type = device.
type();
88 option.set_device_type(TypeToProto(type));
92 if (device.
index() != -1) {
93 option.set_numa_node_id(device.
index());
96 case DeviceType::CUDA:
98 option.set_device_id(device.
index());
100 case DeviceType::OPENGL:
101 case DeviceType::OPENCL:
102 case DeviceType::MKLDNN:
103 case DeviceType::IDEEP:
104 case DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES:
105 case DeviceType::ONLY_FOR_TEST:
110 static_cast<int32_t>(type),
111 ". If you have recently updated the caffe2.proto file to add a new " 112 "device type, did you forget to update the ProtoToType() and TypeToProto" 113 "function to reflect such recent changes?");
118 inline CAFFE2_API
at::Device OptionToDevice(
const caffe2::DeviceOption option) {
119 auto type = option.device_type();
122 case caffe2::PROTO_CPU:
123 if (option.has_numa_node_id()) {
124 id = option.numa_node_id();
127 case caffe2::PROTO_CUDA:
128 case caffe2::PROTO_HIP:
129 id = option.device_id();
135 inline void ExtractDeviceOption(
136 DeviceOption* device_option,
138 AT_ASSERT(device_option);
139 device_option->CopyFrom(DeviceToOption(device));
Represents a a compute device on which a tensor is located.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
DeviceIndex index() const noexcept
Returns the optional index.
DeviceType type() const noexcept
Returns the type of device this is.