Go to the documentation of this file.
13 #include <unordered_map>
18 namespace InferenceEngine {
47 struct PrecisionInfo {
52 const char*
name =
"UNSPECIFIED";
57 PrecisionInfo precisionInfo;
78 precisionInfo.bitsSize = bitsSize;
79 if (
name ==
nullptr) {
80 precisionInfo.name =
"CUSTOM";
82 precisionInfo.name =
name;
84 precisionInfo.value =
CUSTOM;
90 return Precision(8 *
sizeof(T), typeName ==
nullptr ?
typeid(T).
name() : typeName);
97 if (precisionInfo.value !=
BIN) {
98 if (
sizeof(T) !=
size()) {
104 return std::is_same<T, y>()
105 #define CASE2(x, y1, y2) \
107 return std::is_same<T, y1>() || std::is_same<T, y2>()
109 switch (precisionInfo.value) {
111 CASE2(
FP16, int16_t, uint16_t);
112 CASE2(
BF16, int16_t, uint16_t);
122 CASE2(
Q78, int16_t, uint16_t);
123 CASE2(
BIN, int8_t, uint8_t);
136 return precisionInfo.value == p && precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
142 return precisionInfo.value == p;
147 return precisionInfo.value != p;
157 explicit operator bool() const noexcept {
168 return precisionInfo.value;
176 return precisionInfo.value;
180 const char*
name() const noexcept {
181 return precisionInfo.name;
186 static std::unordered_map<std::string, ePrecision> names = {
187 #define PRECISION_NAME(s) {#s, s}
188 PRECISION_NAME(
Q78), PRECISION_NAME(
BOOL), PRECISION_NAME(
BF16),
189 PRECISION_NAME(
I8), PRECISION_NAME(
I16), PRECISION_NAME(
I32), PRECISION_NAME(
I64),
190 PRECISION_NAME(
U8), PRECISION_NAME(
U16), PRECISION_NAME(
U32), PRECISION_NAME(
U64),
191 PRECISION_NAME(
FP32), PRECISION_NAME(
FP16), PRECISION_NAME(
MIXED), PRECISION_NAME(
BIN),
192 #undef PRECISION_NAME
194 auto i = names.find(str);
203 if (precisionInfo.bitsSize == 0) {
204 THROW_IE_EXCEPTION <<
" cannot estimate element if precision is " << precisionInfo.name;
206 return precisionInfo.bitsSize >> 3;
214 return precisionInfo.isFloat;
237 template <Precision::ePrecision precision>
248 if (l == r)
return true;
250 if (l ==
nullptr || r ==
nullptr)
return false;
252 for (; *l && *r; l++, r++) {
253 if (*l != *r)
return false;
264 return makePrecisionInfo<x>(#x);
282 return makePrecisionInfo<UNSPECIFIED>(
"UNSPECIFIED");
291 template <Precision::ePrecision p>
297 using value_type = float;
301 struct PrecisionTrait<Precision::
FP16> {
302 using value_type = int16_t;
305 struct PrecisionTrait<Precision::BF16> {
306 using value_type = int16_t;
309 struct PrecisionTrait<Precision::
Q78> {
310 using value_type = uint16_t;
313 struct PrecisionTrait<Precision::
I16> {
314 using value_type = int16_t;
317 struct PrecisionTrait<Precision::
U16> {
318 using value_type = uint16_t;
321 struct PrecisionTrait<Precision::
U8> {
322 using value_type = uint8_t;
325 struct PrecisionTrait<Precision::
I8> {
326 using value_type = int8_t;
329 struct PrecisionTrait<Precision::BOOL> {
330 using value_type = uint8_t;
333 struct PrecisionTrait<Precision::
I32> {
334 using value_type = int32_t;
337 struct PrecisionTrait<Precision::
U32> {
338 using value_type = uint32_t;
341 struct PrecisionTrait<Precision::
I64> {
342 using value_type = int64_t;
345 struct PrecisionTrait<Precision::
U64> {
346 using value_type = uint64_t;
349 struct PrecisionTrait<Precision::
BIN> {
350 using value_type = int8_t;
354 inline uint8_t type_size_or_zero() {
360 using value_type = void;
364 struct PrecisionTrait<Precision::
MIXED> : PrecisionTrait<Precision::UNSPECIFIED> {};
367 inline uint8_t type_size_or_zero<void>() {
371 template <Precision::ePrecision T>
372 inline typename std::enable_if<std::is_same<std::integral_constant<Precision::ePrecision, Precision::FP16>,
373 std::integral_constant<Precision::ePrecision, T>>::value,
379 template <Precision::ePrecision T>
380 inline typename std::enable_if<!std::is_same<std::integral_constant<Precision::ePrecision, Precision::FP16>,
381 std::integral_constant<Precision::ePrecision, T>>::value,
384 return std::is_floating_point<typename PrecisionTrait<T>::value_type>::value;
387 template <Precision::ePrecision precision>
389 Precision::PrecisionInfo info;
392 size_t nBits = precision ==
BIN ? 1 : 8;
393 info.bitsSize = nBits * type_size_or_zero<typename PrecisionTrait<precision>::value_type>();
394 info.isFloat = is_floating<precision>();
395 info.value = precision;
400 return out << p.name();
404 return out << Precision(p).name();
407 inline std::ostream& operator<<(std::ostream& os,
const std::vector<Precision>& values) {
409 for (
size_t i = 0; i < values.size(); ++i) {
411 if (i != (values.size() - 1ul)) {
419 inline constexpr uint32_t getPrecisionMask(
423 return (precision1) | (precision2 << 8) | (precision3 << 16) | (precision4 << 24);
@ BIN
Definition: ie_c_api.h:186
@ FP16
Definition: ie_c_api.h:176
Particular precision traits.
Definition: ie_precision.hpp:292
constexpr uint8_t getPrecVal() const noexcept
Gets the precision value of type ePrecision.
Definition: ie_precision.hpp:175
@ MIXED
Definition: ie_precision.hpp:28
This class holds precision value and provides precision related operations.
Definition: ie_precision.hpp:23
@ I16
Definition: ie_precision.hpp:33
@ I64
Definition: ie_precision.hpp:39
@ Q78
Definition: ie_c_api.h:177
Precision(const Precision::ePrecision value)
Constructor with specified precision.
Definition: ie_precision.hpp:64
@ I32
Definition: ie_precision.hpp:37
@ FP32
Definition: ie_precision.hpp:29
bool operator!=(const ePrecision p) const noexcept
Inequality operator with ePrecision enum value.
Definition: ie_precision.hpp:146
@ UNSPECIFIED
Definition: ie_c_api.h:173
const char * name() const noexcept
Getter of precision name.
Definition: ie_precision.hpp:180
static bool areSameStrings(const char *l, const char *r) noexcept
Compare two c-strings.
Definition: ie_precision.hpp:247
@ U64
Definition: ie_c_api.h:184
ePrecision
Definition: ie_precision.hpp:26
@ I32
Definition: ie_c_api.h:182
@ BF16
Definition: ie_precision.hpp:31
@ BIN
Definition: ie_precision.hpp:41
@ BOOL
Definition: ie_precision.hpp:42
Precision()=default
Default constructor.
A header file for the main Inference Engine exception.
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:95
@ MIXED
Definition: ie_c_api.h:174
@ Q78
Definition: ie_precision.hpp:32
@ FP32
Definition: ie_c_api.h:175
@ I64
Definition: ie_c_api.h:183
static Precision fromType(const char *typeName=nullptr)
Creates custom precision with specific underlined type.
Definition: ie_precision.hpp:89
@ FP16
Definition: ie_precision.hpp:30
@ U16
Definition: ie_c_api.h:181
@ UNSPECIFIED
Definition: ie_precision.hpp:27
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
@ U32
Definition: ie_c_api.h:185
Precision & operator=(const ePrecision p) noexcept
Assignment operator with ePrecision enum value.
Definition: ie_precision.hpp:151
@ U16
Definition: ie_precision.hpp:36
static PrecisionInfo getPrecisionInfo(ePrecision v)
Return PrecisionInfo.
Definition: ie_precision.hpp:261
@ CUSTOM
Definition: ie_precision.hpp:43
bool operator!() const noexcept
Logical negation operator.
Definition: ie_precision.hpp:162
bool operator==(const Precision &p) const noexcept
Equality operator with Precision object.
Definition: ie_precision.hpp:135
@ I16
Definition: ie_c_api.h:178
@ I8
Definition: ie_c_api.h:180
size_t size() const
Returns size of single element of that precision in bits.
Definition: ie_precision.hpp:202
@ U8
Definition: ie_c_api.h:179
@ U8
Definition: ie_precision.hpp:34
static Precision FromStr(const std::string &str)
Creates from string with precision name.
Definition: ie_precision.hpp:185
@ I8
Definition: ie_precision.hpp:35
Precision(size_t bitsSize, const char *name=nullptr)
Custom precision constructor.
Definition: ie_precision.hpp:74
@ U64
Definition: ie_precision.hpp:40
@ U32
Definition: ie_precision.hpp:38
bool isSigned() const noexcept
Checks if it is a signed value.
Definition: ie_precision.hpp:221
bool is_float() const noexcept
Checks if it is a floating point value.
Definition: ie_precision.hpp:213
bool operator==(const ePrecision p) const noexcept
Equality operator with ePrecision enum value.
Definition: ie_precision.hpp:141
static PrecisionInfo makePrecisionInfo(const char *name)
Creates PrecisionInfo by precision with a specified name.