13 #include <unordered_map>
18 namespace InferenceEngine {
47 struct PrecisionInfo {
52 const char*
name =
"UNSPECIFIED";
57 PrecisionInfo precisionInfo;
81 precisionInfo.bitsSize = bitsSize;
82 if (
name ==
nullptr) {
83 precisionInfo.name =
"CUSTOM";
85 precisionInfo.name =
name;
87 precisionInfo.value =
CUSTOM;
93 return Precision(8 *
sizeof(T), typeName ==
nullptr ?
typeid(T).
name() : typeName);
100 if (precisionInfo.value !=
BIN) {
101 if (
sizeof(T) !=
size()) {
107 return std::is_same<T, y>()
108 #define CASE2(x, y1, y2) \
110 return std::is_same<T, y1>() || std::is_same<T, y2>()
112 switch (precisionInfo.value) {
114 CASE2(
FP16, int16_t, uint16_t);
115 CASE2(
BF16, int16_t, uint16_t);
125 CASE2(
Q78, int16_t, uint16_t);
126 CASE2(
BIN, int8_t, uint8_t);
143 return precisionInfo.value == p && precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
153 return precisionInfo.value == p;
162 return precisionInfo.value != p;
179 explicit operator bool() const noexcept {
196 return precisionInfo.value;
204 return precisionInfo.value;
211 const char*
name() const noexcept {
212 return precisionInfo.name;
221 static std::unordered_map<std::string, ePrecision> names = {
222 #define PRECISION_NAME(s) {#s, s}
223 PRECISION_NAME(
Q78), PRECISION_NAME(
BOOL), PRECISION_NAME(
BF16),
224 PRECISION_NAME(
I8), PRECISION_NAME(
I16), PRECISION_NAME(
I32), PRECISION_NAME(
I64),
225 PRECISION_NAME(
U8), PRECISION_NAME(
U16), PRECISION_NAME(
U32), PRECISION_NAME(
U64),
226 PRECISION_NAME(
FP32), PRECISION_NAME(
FP16), PRECISION_NAME(
MIXED), PRECISION_NAME(
BIN),
227 #undef PRECISION_NAME
229 auto i = names.find(str);
238 if (precisionInfo.bitsSize == 0) {
239 THROW_IE_EXCEPTION <<
" cannot estimate element if precision is " << precisionInfo.name;
241 return precisionInfo.bitsSize >> 3;
249 return precisionInfo.isFloat;
272 template <Precision::ePrecision precision>
283 if (l == r)
return true;
285 if (l ==
nullptr || r ==
nullptr)
return false;
287 for (; *l && *r; l++, r++) {
288 if (*l != *r)
return false;
301 return makePrecisionInfo<x>(#x);
319 return makePrecisionInfo<UNSPECIFIED>(
"UNSPECIFIED");
328 template <Precision::ePrecision p>
334 using value_type = float;
338 struct PrecisionTrait<Precision::
FP16> {
339 using value_type = int16_t;
342 struct PrecisionTrait<Precision::BF16> {
343 using value_type = int16_t;
346 struct PrecisionTrait<Precision::
Q78> {
347 using value_type = uint16_t;
350 struct PrecisionTrait<Precision::
I16> {
351 using value_type = int16_t;
354 struct PrecisionTrait<Precision::
U16> {
355 using value_type = uint16_t;
358 struct PrecisionTrait<Precision::
U8> {
359 using value_type = uint8_t;
362 struct PrecisionTrait<Precision::
I8> {
363 using value_type = int8_t;
366 struct PrecisionTrait<Precision::BOOL> {
367 using value_type = uint8_t;
370 struct PrecisionTrait<Precision::
I32> {
371 using value_type = int32_t;
374 struct PrecisionTrait<Precision::
U32> {
375 using value_type = uint32_t;
378 struct PrecisionTrait<Precision::
I64> {
379 using value_type = int64_t;
382 struct PrecisionTrait<Precision::
U64> {
383 using value_type = uint64_t;
386 struct PrecisionTrait<Precision::
BIN> {
387 using value_type = int8_t;
391 inline uint8_t type_size_or_zero() {
397 using value_type = void;
401 struct PrecisionTrait<Precision::
MIXED> : PrecisionTrait<Precision::UNSPECIFIED> {};
404 inline uint8_t type_size_or_zero<void>() {
408 template <Precision::ePrecision T>
409 inline typename std::enable_if<std::is_same<std::integral_constant<Precision::ePrecision, Precision::FP16>,
410 std::integral_constant<Precision::ePrecision, T>>::value,
416 template <Precision::ePrecision T>
417 inline typename std::enable_if<!std::is_same<std::integral_constant<Precision::ePrecision, Precision::FP16>,
418 std::integral_constant<Precision::ePrecision, T>>::value,
421 return std::is_floating_point<typename PrecisionTrait<T>::value_type>::value;
424 template <Precision::ePrecision precision>
426 Precision::PrecisionInfo info;
429 size_t nBits = precision ==
BIN ? 1 : 8;
430 info.bitsSize = nBits * type_size_or_zero<typename PrecisionTrait<precision>::value_type>();
431 info.isFloat = is_floating<precision>();
432 info.value = precision;
437 return out << p.name();
441 return out << Precision(p).name();
444 inline std::ostream&
operator<<(std::ostream& os,
const std::vector<Precision>& values) {
446 for (
size_t i = 0; i < values.size(); ++i) {
448 if (i != (values.size() - 1ul)) {
456 inline constexpr uint32_t getPrecisionMask(
460 return (precision1) | (precision2 << 8) | (precision3 << 16) | (precision4 << 24);
This class holds precision value and provides precision related operations.
Definition: ie_precision.hpp:23
size_t size() const
Returns size of single element of that precision in bits.
Definition: ie_precision.hpp:237
bool operator==(const ePrecision p) const noexcept
Equality operator with ePrecision enum value.
Definition: ie_precision.hpp:152
static Precision FromStr(const std::string &str)
Creates Precision from string with precision name.
Definition: ie_precision.hpp:220
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:98
static PrecisionInfo makePrecisionInfo(const char *name)
Creates PrecisionInfo by precision with a specified name.
Precision()=default
Default constructor.
bool is_float() const noexcept
Checks if it is a floating point value.
Definition: ie_precision.hpp:248
bool operator!() const noexcept
Logical negation operator.
Definition: ie_precision.hpp:187
static Precision fromType(const char *typeName=nullptr)
Creates custom precision with specific underlined type.
Definition: ie_precision.hpp:92
static bool areSameStrings(const char *l, const char *r) noexcept
Compare two c-strings.
Definition: ie_precision.hpp:282
constexpr uint8_t getPrecVal() const noexcept
Gets the precision value of type ePrecision.
Definition: ie_precision.hpp:203
bool operator!=(const ePrecision p) const noexcept
Inequality operator with ePrecision enum value.
Definition: ie_precision.hpp:161
Precision(size_t bitsSize, const char *name=nullptr)
Custom precision constructor.
Definition: ie_precision.hpp:77
Precision & operator=(const ePrecision p) noexcept
Assignment operator with ePrecision enum value.
Definition: ie_precision.hpp:170
static PrecisionInfo getPrecisionInfo(ePrecision v)
Creates PrecisionInfo based on ePrecision.
Definition: ie_precision.hpp:298
const char * name() const noexcept
Getter of precision name.
Definition: ie_precision.hpp:211
ePrecision
Definition: ie_precision.hpp:26
@ BIN
Definition: ie_precision.hpp:41
@ U8
Definition: ie_precision.hpp:34
@ FP16
Definition: ie_precision.hpp:30
@ I32
Definition: ie_precision.hpp:37
@ FP32
Definition: ie_precision.hpp:29
@ U64
Definition: ie_precision.hpp:40
@ U32
Definition: ie_precision.hpp:38
@ I64
Definition: ie_precision.hpp:39
@ I8
Definition: ie_precision.hpp:35
@ CUSTOM
Definition: ie_precision.hpp:43
@ MIXED
Definition: ie_precision.hpp:28
@ Q78
Definition: ie_precision.hpp:32
@ I16
Definition: ie_precision.hpp:33
@ BF16
Definition: ie_precision.hpp:31
@ UNSPECIFIED
Definition: ie_precision.hpp:27
@ U16
Definition: ie_precision.hpp:36
@ BOOL
Definition: ie_precision.hpp:42
Precision(const Precision::ePrecision value)
Constructor with specified precision.
Definition: ie_precision.hpp:67
bool operator==(const Precision &p) const noexcept
Equality operator with Precision object.
Definition: ie_precision.hpp:142
bool isSigned() const noexcept
Checks if it is a signed value.
Definition: ie_precision.hpp:256
@ U64
Definition: ie_c_api.h:184
@ U8
Definition: ie_c_api.h:179
@ FP32
Definition: ie_c_api.h:175
@ BIN
Definition: ie_c_api.h:186
@ FP16
Definition: ie_c_api.h:176
@ MIXED
Definition: ie_c_api.h:174
@ U32
Definition: ie_c_api.h:185
@ I32
Definition: ie_c_api.h:182
@ Q78
Definition: ie_c_api.h:177
@ I8
Definition: ie_c_api.h:180
@ I16
Definition: ie_c_api.h:178
@ I64
Definition: ie_c_api.h:183
@ UNSPECIFIED
Definition: ie_c_api.h:173
@ U16
Definition: ie_c_api.h:181
std::ostream & operator<<(std::ostream &out, const Layout &p)
Prints a string representation of InferenceEngine::Layout to a stream.
Definition: ie_common.h:101
A header file for the main Inference Engine exception.
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
Particular precision traits.
Definition: ie_precision.hpp:329