11 #include <unordered_map>
41 struct PrecisionInfo {
46 const char*
name =
"UNSPECIFIED";
51 PrecisionInfo precisionInfo;
72 precisionInfo.bitsSize = bitsSize;
73 if (
name ==
nullptr) {
74 precisionInfo.name =
"CUSTOM";
76 precisionInfo.name =
name;
78 precisionInfo.value =
CUSTOM;
84 return Precision(8 *
sizeof(T), typeName ==
nullptr ?
typeid(T).
name() : typeName);
91 if (precisionInfo.value !=
BIN) {
92 if (
sizeof(T) !=
size()) {
98 return std::is_same<T, y>()
99 #define CASE2(x, y1, y2) \
101 return std::is_same<T, y1>() || std::is_same<T, y2>()
103 switch (precisionInfo.value) {
105 CASE2(
FP16, int16_t, uint16_t);
112 CASE2(
Q78, int16_t, uint16_t);
113 CASE2(
BIN, int8_t, uint8_t);
126 return precisionInfo.value == p && precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
132 return precisionInfo.value == p;
137 return precisionInfo.value != p;
147 explicit operator bool() const noexcept {
158 return precisionInfo.value;
160 constexpr uint8_t getPrecVal()
const noexcept {
161 return precisionInfo.value;
165 const char*
name() const noexcept {
166 return precisionInfo.name;
171 static std::unordered_map<std::string, ePrecision> names = {
172 #define PRECISION_NAME(s) {#s, s}
173 PRECISION_NAME(
Q78), PRECISION_NAME(
U8), PRECISION_NAME(
I8), PRECISION_NAME(
I16),
174 PRECISION_NAME(
I32), PRECISION_NAME(
I64), PRECISION_NAME(
U16), PRECISION_NAME(
FP32),
175 PRECISION_NAME(
FP16), PRECISION_NAME(
MIXED), PRECISION_NAME(
BIN),
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;
199 bool isSigned()
const noexcept {
214 template <Precision::ePrecision precision>
225 if (l == r)
return true;
227 if (l ==
nullptr || r ==
nullptr)
return false;
229 for (; *l && *r; l++, r++) {
230 if (*l != *r)
return false;
241 return makePrecisionInfo<x>(#x);
255 return makePrecisionInfo<UNSPECIFIED>(
"UNSPECIFIED");
264 template <Precision::ePrecision p>
270 using value_type = float;
275 using value_type = int16_t;
279 using value_type = uint16_t;
283 using value_type = int16_t;
287 using value_type = uint16_t;
291 using value_type = uint8_t;
295 using value_type = int8_t;
299 using value_type = int32_t;
303 using value_type = int64_t;
307 using value_type = int8_t;
311 inline uint8_t type_size_or_zero() {
317 using value_type = void;
324 inline uint8_t type_size_or_zero<void>() {
328 template <Precision::ePrecision T>
329 inline typename std::enable_if<std::is_same<std::integral_constant<Precision::ePrecision, Precision::FP16>,
330 std::integral_constant<Precision::ePrecision, T>>::value,
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,
341 return std::is_floating_point<typename PrecisionTrait<T>::value_type>::value;
344 template <Precision::ePrecision precision>
346 Precision::PrecisionInfo info;
349 size_t nBits = precision ==
BIN ? 1 : 8;
350 info.bitsSize = nBits * type_size_or_zero<typename PrecisionTrait<precision>::value_type>();
351 info.isFloat = is_floating<precision>();
352 info.value = precision;
357 return out << p.
name();
364 inline std::ostream& operator<<(std::ostream& os, const std::vector<Precision>& values) {
366 for (
size_t i = 0; i < values.size(); ++i) {
368 if (i != (values.size() - 1ul)) {
376 inline constexpr uint32_t getPrecisionMask(
380 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:24
Definition: ie_precision.hpp:33
Definition: ie_precision.hpp:27
ePrecision
Definition: ie_precision.hpp:24
Particular precision traits.
Definition: ie_precision.hpp:265
static Precision fromType(const char *typeName=nullptr)
Creates custom precision with specific underlined type.
Definition: ie_precision.hpp:83
Inference Engine API.
Definition: ie_argmax_layer.hpp:11
static PrecisionInfo getPrecisionInfo(ePrecision v)
Return PrecisionInfo.
Definition: ie_precision.hpp:238
Definition: ie_precision.hpp:32
bool operator!() const noexcept
Logical negation operator.
Definition: ie_precision.hpp:152
Precision & operator=(const ePrecision p) noexcept
Assignment operator with ePrecision enum value.
Definition: ie_precision.hpp:141
Definition: ie_precision.hpp:29
size_t size() const
Returns size of single element of that precision in bits.
Definition: ie_precision.hpp:187
Definition: ie_precision.hpp:28
bool operator==(const Precision &p) const noexcept
Equality operator with Precision object.
Definition: ie_precision.hpp:125
Definition: ie_precision.hpp:36
bool is_float() const noexcept
Checks if it is a floating point.
Definition: ie_precision.hpp:195
Definition: ie_precision.hpp:37
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:170
Precision(size_t bitsSize, const char *name=nullptr)
Custom precision constructor.
Definition: ie_precision.hpp:68
Definition: ie_precision.hpp:35
bool operator==(const ePrecision p) const noexcept
Equality operator with ePrecision enum value.
Definition: ie_precision.hpp:131
Definition: ie_precision.hpp:31
Definition: ie_precision.hpp:26
Definition: ie_precision.hpp:34
Definition: ie_precision.hpp:30
Precision(const Precision::ePrecision value)
Constructor with specified precision.
Definition: ie_precision.hpp:58
bool operator!=(const ePrecision p) const noexcept
Inequality operator with ePrecision enum value.
Definition: ie_precision.hpp:136
static bool areSameStrings(const char *l, const char *r) noexcept
Compare two c-strings.
Definition: ie_precision.hpp:224
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:21
Definition: ie_precision.hpp:25
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:89
const char * name() const noexcept
Getter of precision name.
Definition: ie_precision.hpp:165