Caffe2 - C++ API
A deep learning, cross platform ML framework
caffe2_pb.h
1 #pragma once
2 #include <c10/core/Device.h>
3 #include <c10/util/Exception.h>
4 #include <caffe2/proto/caffe2.pb.h>
5 
6 namespace caffe2 {
7 
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;
19 
20 inline CAFFE2_API DeviceType ProtoToType(const caffe2::DeviceTypeProto p) {
21  switch (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;
40  default:
41  AT_ERROR(
42  "Unknown device:",
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?");
47  }
48 }
49 
50 inline CAFFE2_API DeviceType ProtoToType(int p) {
51  return ProtoToType(static_cast<caffe2::DeviceTypeProto>(p));
52 }
53 
54 inline CAFFE2_API DeviceTypeProto TypeToProto(const DeviceType& t) {
55  switch (t) {
56  case DeviceType::CPU:
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;
68  case DeviceType::HIP:
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;
74  default:
75  AT_ERROR(
76  "Unknown device:",
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?");
81  }
82 }
83 
84 inline CAFFE2_API caffe2::DeviceOption DeviceToOption(
85  const at::Device& device) {
86  caffe2::DeviceOption option;
87  auto type = device.type();
88  option.set_device_type(TypeToProto(type));
89 
90  switch (type) {
91  case DeviceType::CPU:
92  if (device.index() != -1) {
93  option.set_numa_node_id(device.index());
94  }
95  break;
96  case DeviceType::CUDA:
97  case DeviceType::HIP:
98  option.set_device_id(device.index());
99  break;
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:
106  break;
107  default:
108  AT_ERROR(
109  "Unknown device:",
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?");
114  }
115  return option;
116 }
117 
118 inline CAFFE2_API at::Device OptionToDevice(const caffe2::DeviceOption option) {
119  auto type = option.device_type();
120  int32_t id = -1;
121  switch (type) {
122  case caffe2::PROTO_CPU:
123  if (option.has_numa_node_id()) {
124  id = option.numa_node_id();
125  }
126  break;
127  case caffe2::PROTO_CUDA:
128  case caffe2::PROTO_HIP:
129  id = option.device_id();
130  break;
131  }
132  return at::Device(ProtoToType(type), id);
133 }
134 
135 inline void ExtractDeviceOption(
136  DeviceOption* device_option,
137  const at::Device& device) {
138  AT_ASSERT(device_option);
139  device_option->CopyFrom(DeviceToOption(device));
140 }
141 
142 } // namespace caffe2
Represents a a compute device on which a tensor is located.
Definition: Device.h:30
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
DeviceIndex index() const noexcept
Returns the optional index.
Definition: Device.h:70
DeviceType type() const noexcept
Returns the type of device this is.
Definition: Device.h:65