12 #include <unordered_map> 43 struct PrecisionInfo {
48 const char*
name =
"UNSPECIFIED";
53 PrecisionInfo precisionInfo;
74 precisionInfo.bitsSize = bitsSize;
75 if (
name ==
nullptr) {
76 precisionInfo.name =
"CUSTOM";
78 precisionInfo.name =
name;
80 precisionInfo.value =
CUSTOM;
86 return Precision(8 *
sizeof(T), typeName ==
nullptr ?
typeid(T).
name() : typeName);
93 if (precisionInfo.value !=
BIN) {
94 if (
sizeof(T) !=
size()) {
100 return std::is_same<T, y>() 101 #define CASE2(x, y1, y2) \ 103 return std::is_same<T, y1>() || std::is_same<T, y2>() 105 switch (precisionInfo.value) {
107 CASE2(
FP16, int16_t, uint16_t);
115 CASE2(
Q78, int16_t, uint16_t);
116 CASE2(
BIN, int8_t, uint8_t);
129 return precisionInfo.value == p && precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
135 return precisionInfo.value == p;
140 return precisionInfo.value != p;
150 explicit operator bool() const noexcept {
161 return precisionInfo.value;
163 constexpr uint8_t getPrecVal()
const noexcept {
164 return precisionInfo.value;
168 const char*
name() const noexcept {
169 return precisionInfo.name;
174 static std::unordered_map<std::string, ePrecision> names = {
175 #define PRECISION_NAME(s) {#s, s} 176 PRECISION_NAME(
Q78), PRECISION_NAME(
U8), PRECISION_NAME(
I8), PRECISION_NAME(
I16),
177 PRECISION_NAME(
I32), PRECISION_NAME(
I64), PRECISION_NAME(
U16), PRECISION_NAME(
FP32),
178 PRECISION_NAME(
FP16), PRECISION_NAME(
MIXED), PRECISION_NAME(
BIN), PRECISION_NAME(
BOOL),
179 #undef PRECISION_NAME 181 auto i = names.find(str);
191 if (precisionInfo.bitsSize == 0) {
192 THROW_IE_EXCEPTION <<
" cannot estimate element if precision is " << precisionInfo.name;
194 return precisionInfo.bitsSize >> 3;
199 return precisionInfo.isFloat;
202 bool isSigned()
const noexcept {
217 template <Precision::ePrecision precision>
228 if (l == r)
return true;
230 if (l ==
nullptr || r ==
nullptr)
return false;
232 for (; *l && *r; l++, r++) {
233 if (*l != *r)
return false;
244 return makePrecisionInfo<x>(#x); 259 return makePrecisionInfo<UNSPECIFIED>(
"UNSPECIFIED");
268 template <Precision::ePrecision p>
274 using value_type = float;
279 using value_type = int16_t;
283 using value_type = uint16_t;
287 using value_type = int16_t;
291 using value_type = uint16_t;
295 using value_type = uint8_t;
299 using value_type = int8_t;
303 using value_type = uint8_t;
307 using value_type = int32_t;
311 using value_type = int64_t;
315 using value_type = int8_t;
319 inline uint8_t type_size_or_zero() {
325 using value_type = void;
332 inline uint8_t type_size_or_zero<void>() {
336 template <Precision::ePrecision T>
337 inline typename std::enable_if<std::is_same<std::integral_constant<Precision::ePrecision, Precision::FP16>,
338 std::integral_constant<Precision::ePrecision, T>>::value,
344 template <Precision::ePrecision T>
345 inline typename std::enable_if<!std::is_same<std::integral_constant<Precision::ePrecision, Precision::FP16>,
346 std::integral_constant<Precision::ePrecision, T>>::value,
349 return std::is_floating_point<typename PrecisionTrait<T>::value_type>::value;
352 template <Precision::ePrecision precision>
354 Precision::PrecisionInfo info;
357 size_t nBits = precision ==
BIN ? 1 : 8;
358 info.bitsSize = nBits * type_size_or_zero<typename PrecisionTrait<precision>::value_type>();
359 info.isFloat = is_floating<precision>();
360 info.value = precision;
365 return out << p.
name();
372 inline std::ostream& operator<<(std::ostream& os, const std::vector<Precision>& values) {
374 for (
size_t i = 0; i < values.size(); ++i) {
376 if (i != (values.size() - 1ul)) {
384 inline constexpr uint32_t getPrecisionMask(
388 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:25
Definition: ie_precision.hpp:34
Definition: ie_precision.hpp:28
ePrecision
Definition: ie_precision.hpp:25
Particular precision traits.
Definition: ie_precision.hpp:269
static Precision fromType(const char *typeName=nullptr)
Creates custom precision with specific underlined type.
Definition: ie_precision.hpp:85
Inference Engine API.
Definition: ie_argmax_layer.hpp:15
static PrecisionInfo getPrecisionInfo(ePrecision v)
Return PrecisionInfo.
Definition: ie_precision.hpp:241
Definition: ie_precision.hpp:33
bool operator!() const noexcept
Logical negation operator.
Definition: ie_precision.hpp:155
Precision & operator=(const ePrecision p) noexcept
Assignment operator with ePrecision enum value.
Definition: ie_precision.hpp:144
Definition: ie_precision.hpp:30
size_t size() const
Returns size of single element of that precision in bits.
Definition: ie_precision.hpp:190
Definition: ie_precision.hpp:29
bool operator==(const Precision &p) const noexcept
Equality operator with Precision object.
Definition: ie_precision.hpp:128
Definition: ie_precision.hpp:37
bool is_float() const noexcept
Checks if it is a floating point.
Definition: ie_precision.hpp:198
Definition: ie_precision.hpp:39
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:173
Precision(size_t bitsSize, const char *name=nullptr)
Custom precision constructor.
Definition: ie_precision.hpp:70
Definition: ie_precision.hpp:36
bool operator==(const ePrecision p) const noexcept
Equality operator with ePrecision enum value.
Definition: ie_precision.hpp:134
Definition: ie_precision.hpp:32
Definition: ie_precision.hpp:27
Definition: ie_precision.hpp:35
Definition: ie_precision.hpp:31
Precision(const Precision::ePrecision value)
Constructor with specified precision.
Definition: ie_precision.hpp:60
Definition: ie_precision.hpp:38
bool operator!=(const ePrecision p) const noexcept
Inequality operator with ePrecision enum value.
Definition: ie_precision.hpp:139
static bool areSameStrings(const char *l, const char *r) noexcept
Compare two c-strings.
Definition: ie_precision.hpp:227
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:22
Definition: ie_precision.hpp:26
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:91
const char * name() const noexcept
Getter of precision name.
Definition: ie_precision.hpp:168