ie_precision.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief A header file that provides class for describing precision of data
7  * @file ie_precision.hpp
8  */
9 #pragma once
10 #include <unordered_map>
11 #include <string>
12 #include "details/ie_exception.hpp"
13 
14 namespace InferenceEngine {
15 
16 /**
17  * @brief This class holds precision value and provides precision related operations
18  */
19 class Precision {
20 public:
21  /** Enum to specify of different */
22  enum ePrecision : uint8_t {
23  UNSPECIFIED = 255, /**< Unspecified value. Used by default */
24  MIXED = 0, /**< Mixed value. Can be received from network. No applicable for tensors */
25  FP32 = 10, /**< 32bit floating point value */
26  FP16 = 11, /**< 16bit floating point value */
27  Q78 = 20, /**< 16bit specific signed fixed point precision */
28  I16 = 30, /**< 16bit signed integer value */
29  U8 = 40, /**< 8bit unsigned integer value */
30  I8 = 50, /**< 8bit signed integer value */
31  U16 = 60, /**< 16bit unsigned integer value */
32  I32 = 70, /**< 32bit signed integer value */
33  I64 = 72, /**< 64bit signed integer value */
34  BIN = 71, /**< 1bit integer value */
35  CUSTOM = 80 /**< custom precision has it's own name and size of elements */
36  };
37 
38 private:
39  struct PrecisionInfo {
40  /** @brief Size of underlined element */
41  size_t bitsSize = 0;
42 
43  /** @brief Null terminated string with precision name */
44  const char *name = "UNSPECIFIED";
45 
46  bool isFloat = false;
48  };
49  PrecisionInfo precisionInfo;
50 
51 public:
52  /** @brief Default constructor */
53  Precision() = default;
54 
55  /** @brief Constructor with specified precision */
56  Precision(const Precision::ePrecision value) { // NOLINT
57  precisionInfo = getPrecisionInfo(value);
58  }
59 
60  /**
61  * @brief Custom precision constructor
62  * @param byteSize size of elements
63  * @param name optional name string, used in serialisation
64  */
65  explicit Precision(size_t bitsSize, const char * name = nullptr) {
66  if (bitsSize == 0) {
67  THROW_IE_EXCEPTION << "Precision with 0 elements size not supported";
68  }
69  precisionInfo.bitsSize = bitsSize;
70  if (name == nullptr) {
71  precisionInfo.name = "CUSTOM";
72  } else {
73  precisionInfo.name = name;
74  }
75  precisionInfo.value = CUSTOM;
76  }
77 
78  /** @brief Creates custom precision with specific underlined type */
79  template <class T>
80  static Precision fromType(const char * typeName = nullptr) {
81  return Precision(8 * sizeof(T), typeName == nullptr ? typeid(T).name() : typeName);
82  }
83 
84  /** @brief checks whether given storage class T can be used to store objects of current precision */
85  template <class T>
86  bool hasStorageType(const char * typeName = nullptr) const noexcept {
87  try {
88  if (precisionInfo.value != BIN) {
89  if (sizeof(T) != size()) {
90  return false;
91  }
92  }
93 #define CASE(x, y) case x: return std::is_same<T, y>()
94 #define CASE2(x, y1, y2) case x: return std::is_same<T, y1>() || std::is_same<T, y2>()
95 
96  switch (precisionInfo.value) {
97  CASE(FP32, float);
98  CASE2(FP16, int16_t, uint16_t);
99  CASE(I16, int16_t);
100  CASE(I32, int32_t);
101  CASE(I64, int64_t);
102  CASE(U16, uint16_t);
103  CASE(U8, uint8_t);
104  CASE(I8, int8_t);
105  CASE2(Q78, int16_t, uint16_t);
106  CASE2(BIN, int8_t, uint8_t);
107  default :
108  return areSameStrings(name(), typeName == nullptr ? typeid(T).name() : typeName);
109 #undef CASE
110 #undef CASE2
111  }
112  } catch (...) {
113  return false;
114  }
115  }
116 
117  /** @brief Equality operator with Precision object */
118  bool operator == (const Precision & p) const noexcept {
119  return precisionInfo.value == p &&
120  precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
121  areSameStrings(precisionInfo.name, p.precisionInfo.name);
122  }
123 
124  /** @brief Equality operator with ePrecision enum value */
125  bool operator == (const ePrecision p) const noexcept {
126  return precisionInfo.value == p;
127  }
128 
129  /** @brief Inequality operator with ePrecision enum value */
130  bool operator != (const ePrecision p) const noexcept {
131  return precisionInfo.value != p;
132  }
133 
134  /** @brief Assignment operator with ePrecision enum value */
135  Precision & operator = (const ePrecision p) noexcept {
136  precisionInfo = getPrecisionInfo(p);
137  return *this;
138  }
139 
140  /** @brief Cast operator to a bool */
141  explicit operator bool() const noexcept {
142  return precisionInfo.value != UNSPECIFIED;
143  }
144 
145  /** @brief Logical negation operator */
146  bool operator !() const noexcept {
147  return precisionInfo.value == UNSPECIFIED;
148  }
149 
150  /** @brief Cast operator to a ePrecision */
151  operator Precision::ePrecision () const noexcept {
152  return precisionInfo.value;
153  }
154 
155  /** @brief Getter of precision name */
156  const char *name() const noexcept {
157  return precisionInfo.name;
158  }
159 
160  /** @brief Creates from string with precision name */
161  static Precision FromStr(const std::string &str) {
162  static std::unordered_map<std::string, ePrecision > names = {
163 #define PRECISION_NAME(s) {#s, s}
164  PRECISION_NAME(Q78),
165  PRECISION_NAME(U8),
166  PRECISION_NAME(I8),
167  PRECISION_NAME(I16),
168  PRECISION_NAME(I32),
169  PRECISION_NAME(I64),
170  PRECISION_NAME(U16),
171  PRECISION_NAME(FP32),
172  PRECISION_NAME(FP16),
173  PRECISION_NAME(MIXED),
174  PRECISION_NAME(BIN),
175 #undef PRECISION_NAME
176  };
177  auto i = names.find(str);
178  return i == names.end() ? Precision() : Precision(i->second);
179  }
180 
181  /**
182  * @brief Returns size in bytes of single element of that precision
183  * @deprecated : size of precision will be reported in bits in future releases
184  */
185  size_t size() const {
186  if (precisionInfo.bitsSize == 0) {
187  THROW_IE_EXCEPTION << " cannot estimate element if precision is " << precisionInfo.name;
188  }
189  return precisionInfo.bitsSize >> 3;
190  }
191 
192  /** @brief Checks if it is a floating point */
193  bool is_float() const noexcept {
194  return precisionInfo.isFloat;
195  }
196 
197  protected:
198  template<Precision::ePrecision precision>
199  static PrecisionInfo makePrecisionInfo(const char * name);
200 
201  static bool areSameStrings(const char *l, const char *r) noexcept {
202  if (l == r)
203  return true;
204 
205  if (l == nullptr || r == nullptr)
206  return false;
207 
208  for (; *l && *r; l++, r++) {
209  if (*l != *r) return false;
210  }
211  return *l == *r;
212  }
213 
214  static PrecisionInfo getPrecisionInfo(ePrecision v) {
215 #define CASE(x) case x: return makePrecisionInfo<x>(#x);
216  switch (v) {
217  CASE(FP32);
218  CASE(FP16);
219  CASE(I16);
220  CASE(I32);
221  CASE(I64);
222  CASE(U16);
223  CASE(U8);
224  CASE(I8);
225  CASE(Q78);
226  CASE(MIXED);
227  CASE(BIN);
228  default : return makePrecisionInfo<UNSPECIFIED>("UNSPECIFIED");
229 #undef CASE
230  }
231  }
232 };
233 
234 /**
235  * @brief Particular precision traits
236  */
237 template<Precision::ePrecision p>
239 };
240 
241 /** @cond INTERNAL */
242 template<>
243 struct PrecisionTrait<Precision::FP32> {
244  using value_type = float;
245 };
246 
247 template<>
248 struct PrecisionTrait<Precision::FP16> {
249  using value_type = int16_t;
250 };
251 template<>
252 struct PrecisionTrait<Precision::Q78> {
253  using value_type = uint16_t;
254 };
255 template<>
256 struct PrecisionTrait<Precision::I16> {
257  using value_type = int16_t;
258 };
259 template<>
260 struct PrecisionTrait<Precision::U16> {
261  using value_type = uint16_t;
262 };
263 template<>
264 struct PrecisionTrait<Precision::U8> {
265  using value_type = uint8_t;
266 };
267 template<>
268 struct PrecisionTrait<Precision::I8> {
269  using value_type = int8_t;
270 };
271 template<>
272 struct PrecisionTrait<Precision::I32> {
273  using value_type = int32_t;
274 };
275 template<>
276 struct PrecisionTrait<Precision::I64> {
277  using value_type = int64_t;
278 };
279 template<>
280 struct PrecisionTrait<Precision::BIN> {
281  using value_type = int8_t;
282 };
283 
284 template<class T>
285 inline uint8_t type_size_or_zero() {
286  return sizeof(T);
287 }
288 
289 template<>
290 struct PrecisionTrait<Precision::UNSPECIFIED> {
291  using value_type = void;
292 };
293 
294 template<>
295 struct PrecisionTrait<Precision::MIXED> : PrecisionTrait<Precision::UNSPECIFIED>{
296 };
297 
298 template<>
299 inline uint8_t type_size_or_zero<void>() {
300  return 0;
301 }
302 
303 template<Precision::ePrecision T>
304 inline typename std::enable_if<std::is_same<
305  std::integral_constant<Precision::ePrecision, Precision::FP16>,
306  std::integral_constant<Precision::ePrecision, T>>::value, bool>::type is_floating() {
307  return true;
308 }
309 
310 template<Precision::ePrecision T>
311 inline typename std::enable_if<!std::is_same<
312  std::integral_constant<Precision::ePrecision, Precision::FP16>,
313  std::integral_constant<Precision::ePrecision, T>>::value, bool>::type is_floating() {
314  return std::is_floating_point<typename PrecisionTrait<T>::value_type>::value;
315 }
316 
317 template<Precision::ePrecision precision>
318 inline Precision::PrecisionInfo Precision::makePrecisionInfo(const char *name) {
319  Precision::PrecisionInfo info;
320  info.name = name;
321 
322  size_t nBits = precision == BIN ? 1 : 8;
323  info.bitsSize = nBits * type_size_or_zero<typename PrecisionTrait<precision>::value_type>();
324  info.isFloat = is_floating<precision>();
325  info.value = precision;
326  return info;
327 }
328 
329 inline std::ostream & operator << (std::ostream &out, const InferenceEngine::Precision & p) {
330  return out << p.name();
331 }
332 
333 inline std::ostream & operator << (std::ostream &out, const InferenceEngine::Precision::ePrecision & p) {
334  return out << Precision(p).name();
335 }
336 
337 /** @endcond */
338 
339 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:22
Definition: ie_precision.hpp:31
Definition: ie_precision.hpp:25
ePrecision
Definition: ie_precision.hpp:22
Particular precision traits.
Definition: ie_precision.hpp:238
static Precision fromType(const char *typeName=nullptr)
Creates custom precision with specific underlined type.
Definition: ie_precision.hpp:80
Definition: ie_argmax_layer.hpp:11
Definition: ie_precision.hpp:30
bool operator!() const noexcept
Logical negation operator.
Definition: ie_precision.hpp:146
Precision & operator=(const ePrecision p) noexcept
Assignment operator with ePrecision enum value.
Definition: ie_precision.hpp:135
Definition: ie_precision.hpp:27
size_t size() const
Returns size in bytes of single element of that precision.
Definition: ie_precision.hpp:185
Definition: ie_precision.hpp:26
bool operator==(const Precision &p) const noexcept
Equality operator with Precision object.
Definition: ie_precision.hpp:118
Definition: ie_precision.hpp:34
bool is_float() const noexcept
Checks if it is a floating point.
Definition: ie_precision.hpp:193
Definition: ie_precision.hpp:35
static Precision FromStr(const std::string &str)
Creates from string with precision name.
Definition: ie_precision.hpp:161
Precision(size_t bitsSize, const char *name=nullptr)
Custom precision constructor.
Definition: ie_precision.hpp:65
Definition: ie_precision.hpp:33
Definition: ie_precision.hpp:29
Definition: ie_precision.hpp:24
Definition: ie_precision.hpp:32
Definition: ie_precision.hpp:28
Precision(const Precision::ePrecision value)
Constructor with specified precision.
Definition: ie_precision.hpp:56
bool operator!=(const ePrecision p) const noexcept
Inequality operator with ePrecision enum value.
Definition: ie_precision.hpp:130
Precision()=default
Default constructor.
A header file for the main Inference Engine exception.
This class holds precision value and provides precision related operations.
Definition: ie_precision.hpp:19
Definition: ie_precision.hpp:23
bool hasStorageType(const char *typeName=nullptr) const noexcept
checks whether given storage class T can be used to store objects of current precision ...
Definition: ie_precision.hpp:86
const char * name() const noexcept
Getter of precision name.
Definition: ie_precision.hpp:156