10 #include <unordered_map>
39 struct PrecisionInfo {
44 const char *
name =
"UNSPECIFIED";
49 PrecisionInfo precisionInfo;
70 precisionInfo.bitsSize = bitsSize;
71 if (
name ==
nullptr) {
72 precisionInfo.name =
"CUSTOM";
74 precisionInfo.name =
name;
76 precisionInfo.value =
CUSTOM;
82 return Precision(8 *
sizeof(T), typeName ==
nullptr ?
typeid(T).
name() : typeName);
89 if (precisionInfo.value !=
BIN) {
90 if (
sizeof(T) !=
size()) {
94 #define CASE(x, y) case x: return std::is_same<T, y>()
95 #define CASE2(x, y1, y2) case x: return std::is_same<T, y1>() || std::is_same<T, y2>()
97 switch (precisionInfo.value) {
99 CASE2(
FP16, int16_t, uint16_t);
106 CASE2(
Q78, int16_t, uint16_t);
107 CASE2(
BIN, int8_t, uint8_t);
120 return precisionInfo.value == p &&
121 precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
127 return precisionInfo.value == p;
132 return precisionInfo.value != p;
142 explicit operator bool() const noexcept {
153 return precisionInfo.value;
157 const char *
name() const noexcept {
158 return precisionInfo.name;
163 static std::unordered_map<std::string, ePrecision > names = {
164 #define PRECISION_NAME(s) {#s, s}
172 PRECISION_NAME(
FP32),
173 PRECISION_NAME(
FP16),
174 PRECISION_NAME(
MIXED),
176 #undef PRECISION_NAME
178 auto i = names.find(str);
188 if (precisionInfo.bitsSize == 0) {
189 THROW_IE_EXCEPTION <<
" cannot estimate element if precision is " << precisionInfo.name;
191 return precisionInfo.bitsSize >> 3;
196 return precisionInfo.isFloat;
205 template<Precision::ePrecision precision>
219 if (l ==
nullptr || r ==
nullptr)
222 for (; *l && *r; l++, r++) {
223 if (*l != *r)
return false;
232 #define CASE(x) case x: return makePrecisionInfo<x>(#x);
245 default :
return makePrecisionInfo<UNSPECIFIED>(
"UNSPECIFIED");
254 template<Precision::ePrecision p>
261 using value_type = float;
266 using value_type = int16_t;
270 using value_type = uint16_t;
274 using value_type = int16_t;
278 using value_type = uint16_t;
282 using value_type = uint8_t;
286 using value_type = int8_t;
290 using value_type = int32_t;
294 using value_type = int64_t;
298 using value_type = int8_t;
302 inline uint8_t type_size_or_zero() {
308 using value_type = void;
316 inline uint8_t type_size_or_zero<void>() {
320 template<Precision::ePrecision T>
321 inline typename std::enable_if<std::is_same<
322 std::integral_constant<Precision::ePrecision, Precision::FP16>,
323 std::integral_constant<Precision::ePrecision, T>>::value,
bool>::type is_floating() {
327 template<Precision::ePrecision T>
328 inline typename std::enable_if<!std::is_same<
329 std::integral_constant<Precision::ePrecision, Precision::FP16>,
330 std::integral_constant<Precision::ePrecision, T>>::value,
bool>::type is_floating() {
331 return std::is_floating_point<typename PrecisionTrait<T>::value_type>::value;
334 template<Precision::ePrecision precision>
336 Precision::PrecisionInfo info;
339 size_t nBits = precision ==
BIN ? 1 : 8;
340 info.bitsSize = nBits * type_size_or_zero<typename PrecisionTrait<precision>::value_type>();
341 info.isFloat = is_floating<precision>();
342 info.value = precision;
347 return out << p.
name();
358 return (precision1) | (precision2 << 8) | (precision3 << 16) | (precision4 << 24);
#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:255
static Precision fromType(const char *typeName=nullptr)
Creates custom precision with specific underlined type.
Definition: ie_precision.hpp:81
Inference Engine API.
Definition: ie_argmax_layer.hpp:11
static PrecisionInfo getPrecisionInfo(ePrecision v)
Return PrecisionInfo.
Definition: ie_precision.hpp:231
Definition: ie_precision.hpp:30
bool operator!() const noexcept
Logical negation operator.
Definition: ie_precision.hpp:147
Precision & operator=(const ePrecision p) noexcept
Assignment operator with ePrecision enum value.
Definition: ie_precision.hpp:136
Definition: ie_precision.hpp:27
size_t size() const
Returns size of single element of that precision in bits.
Definition: ie_precision.hpp:187
Definition: ie_precision.hpp:26
bool operator==(const Precision &p) const noexcept
Equality operator with Precision object.
Definition: ie_precision.hpp:119
Definition: ie_precision.hpp:34
bool is_float() const noexcept
Checks if it is a floating point.
Definition: ie_precision.hpp:195
Definition: ie_precision.hpp:35
static PrecisionInfo makePrecisionInfo(const char *name)
Returns PrecisionInfo by its name.
static Precision FromStr(const std::string &str)
Creates from string with precision name.
Definition: ie_precision.hpp:162
Precision(size_t bitsSize, const char *name=nullptr)
Custom precision constructor.
Definition: ie_precision.hpp:66
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:131
static bool areSameStrings(const char *l, const char *r) noexcept
Compare two c-strings.
Definition: ie_precision.hpp:215
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:87
const char * name() const noexcept
Getter of precision name.
Definition: ie_precision.hpp:157