Caffe2 - C++ API
A deep learning, cross platform ML framework
common_miopen.h
1 
16 #ifndef CAFFE2_CORE_COMMON_MIOPEN_H_
17 #define CAFFE2_CORE_COMMON_MIOPEN_H_
18 
19 #include <array>
20 #include <mutex>
21 #include "miopen/miopen.h"
22 #include "caffe2/core/common.h"
23 #include "caffe2/core/context.h"
24 #include "caffe2/core/logging.h"
25 #include "caffe2/core/types.h"
26 #include "caffe2/proto/caffe2_pb.h"
27 
28 #define MIOPEN_VERSION 1399
29 
30 namespace caffe2 {
31 
32 namespace internal {
36 inline const char* miopenGetErrorString(miopenStatus_t status)
37 {
38  switch(status)
39  {
40  case miopenStatusSuccess: return "MIOPEN_STATUS_SUCCESS";
41  case miopenStatusNotInitialized: return "MIOPEN_STATUS_NOT_INITIALIZED";
42  case miopenStatusAllocFailed: return "MIOPEN_STATUS_ALLOC_FAILED";
43  case miopenStatusBadParm: return "MIOPEN_STATUS_BAD_PARAM";
44  case miopenStatusInternalError: return "MIOPEN_STATUS_INTERNAL_ERROR";
45  case miopenStatusInvalidValue: return "MIOPEN_STATUS_INVALID_VALUE";
46  case miopenStatusNotImplemented: return "MIOPEN_STATUS_NOT_SUPPORTED";
47  case miopenStatusUnknownError: return "MIOPEN_STATUS_UNKNOWN_ERROR";
48  default: return "MIOPEN_STATUS_UNKNOWN_ERROR";
49  }
50 }
51 } // namespace internal
52 
53 // A macro that wraps around a miopen statement so we can check if the miopen
54 // execution finishes or not.
55 #define MIOPEN_ENFORCE(condition) \
56  do \
57  { \
58  miopenStatus_t status = condition; \
59  CAFFE_ENFORCE_EQ(status, \
60  miopenStatusSuccess, \
61  ", Error at: ", \
62  __FILE__, \
63  ":", \
64  __LINE__, \
65  ": ", \
66  ::caffe2::internal::miopenGetErrorString(status)); \
67  } while(0)
68 #define MIOPEN_CHECK(condition) \
69  do \
70  { \
71  miopenStatus_t status = condition; \
72  CHECK(status == miopenStatusSuccess) << ::caffe2::internal::miopenGetErrorString(status); \
73  } while(0)
74 
75 // report the version of miopen Caffe2 was compiled with
76 inline size_t miopenCompiledVersion() { return MIOPEN_VERSION; }
77 
78 // report the runtime version of miopen
79 inline size_t miopenRuntimeVersion() { return MIOPEN_VERSION; }
80 
81 // Check compatibility of compiled and runtime miopen versions
82 inline void CheckMIOPENVersions() {}
83 
89 template <typename T>
91 
92 template <>
93 class miopenTypeWrapper<float>
94 {
95  public:
96  static const miopenDataType_t type = miopenFloat;
97  typedef const float ScalingParamType;
98  typedef float BNParamType;
99  static ScalingParamType* kOne()
100  {
101  static ScalingParamType v = 1.0;
102  return &v;
103  }
104  static const ScalingParamType* kZero()
105  {
106  static ScalingParamType v = 0.0;
107  return &v;
108  }
109 };
110 
111 template <>
113 {
114  public:
115  static const miopenDataType_t type = miopenHalf;
116  typedef const float ScalingParamType;
117  typedef float BNParamType;
118  static ScalingParamType* kOne()
119  {
120  static ScalingParamType v = 1.0;
121  return &v;
122  }
123  static ScalingParamType* kZero()
124  {
125  static ScalingParamType v = 0.0;
126  return &v;
127  }
128 };
129 
136 {
137  public:
138  miopenTensorDescWrapper() { MIOPEN_ENFORCE(miopenCreateTensorDescriptor(&desc_)); }
139  ~miopenTensorDescWrapper() noexcept { MIOPEN_CHECK(miopenDestroyTensorDescriptor(desc_)); }
140 
141  inline miopenTensorDescriptor_t
142  Descriptor(const miopenDataType_t type, const vector<int>& dims, bool* changed)
143  {
144  if(type_ == type && dims_ == dims)
145  {
146  // if not changed, simply return the current descriptor.
147  if(changed)
148  *changed = false;
149  return desc_;
150  }
151  CAFFE_ENFORCE_EQ(
152  dims.size(), 4, "MIOPEN currently only support 4-dimensional tensor descriptor");
153 
154  type_ = type;
155  dims_ = dims;
156  MIOPEN_ENFORCE(
157  miopenSet4dTensorDescriptor(desc_, type, dims_[0], dims_[1], dims_[2], dims_[3]));
158  if(changed)
159  *changed = true;
160  return desc_;
161  }
162 
163  template <typename T>
164  inline miopenTensorDescriptor_t Descriptor(const StorageOrder& order, const vector<int>& dims)
165  {
166  return Descriptor(miopenTypeWrapper<T>::type, dims, nullptr);
167  }
168 
169  private:
170  miopenTensorDescriptor_t desc_;
171  miopenDataType_t type_;
172  vector<int> dims_;
173  C10_DISABLE_COPY_AND_ASSIGN(miopenTensorDescWrapper);
174 };
175 
176 } // namespace caffe2
177 
178 #endif // CAFFE2_CORE_COMMON_MIOPEN_H_
miopenTypeWrapper is a wrapper class that allows us to refer to the miopen type in a template functio...
Definition: common_miopen.h:90
miopenTensorDescWrapper is the placeholder that wraps around a miopenTensorDescriptor_t, allowing us to do descriptor change as-needed during runtime.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
Flush-To-Zero and Denormals-Are-Zero mode.