13 #include <unordered_map>
48 struct PrecisionInfo {
53 const char*
name =
"UNSPECIFIED";
58 PrecisionInfo precisionInfo;
82 precisionInfo.bitsSize = bitsSize;
83 if (
name ==
nullptr) {
84 precisionInfo.name =
"CUSTOM";
86 precisionInfo.name =
name;
88 precisionInfo.value =
CUSTOM;
94 return Precision(8 *
sizeof(T), typeName ==
nullptr ?
typeid(T).
name() : typeName);
101 if (precisionInfo.value !=
BIN) {
102 if (
sizeof(T) !=
size()) {
108 return std::is_same<T, y>()
109 #define CASE2(x, y1, y2) \
111 return std::is_same<T, y1>() || std::is_same<T, y2>()
113 switch (precisionInfo.value) {
116 CASE2(
FP16, int16_t, uint16_t);
117 CASE2(
BF16, int16_t, uint16_t);
127 CASE2(
Q78, int16_t, uint16_t);
128 CASE2(
BIN, int8_t, uint8_t);
145 return precisionInfo.value == p && precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
155 return precisionInfo.value == p;
164 return precisionInfo.value != p;
181 explicit operator bool() const noexcept {
198 return precisionInfo.value;
206 return precisionInfo.value;
213 const char*
name() const noexcept {
214 return precisionInfo.name;
223 static const std::unordered_map<std::string, ePrecision> names = {
224 #define PRECISION_NAME(s) {#s, s}
225 PRECISION_NAME(
Q78), PRECISION_NAME(
BOOL), PRECISION_NAME(
BF16),
226 PRECISION_NAME(
I8), PRECISION_NAME(
I16), PRECISION_NAME(
I32), PRECISION_NAME(
I64),
227 PRECISION_NAME(
U8), PRECISION_NAME(
U16), PRECISION_NAME(
U32), PRECISION_NAME(
U64),
228 PRECISION_NAME(
FP32), PRECISION_NAME(
FP64), PRECISION_NAME(
FP16), PRECISION_NAME(
MIXED),
230 #undef PRECISION_NAME
232 auto i = names.find(str);
241 if (precisionInfo.bitsSize == 0) {
242 THROW_IE_EXCEPTION <<
" cannot estimate element if precision is " << precisionInfo.name;
244 return precisionInfo.bitsSize >> 3;
252 return precisionInfo.isFloat;
276 template <Precision::ePrecision precision>
287 if (l == r)
return true;
289 if (l ==
nullptr || r ==
nullptr)
return false;
291 for (; *l && *r; l++, r++) {
292 if (*l != *r)
return false;
305 return makePrecisionInfo<x>(#x);
324 return makePrecisionInfo<UNSPECIFIED>(
"UNSPECIFIED");
333 template <Precision::ePrecision p>
339 using value_type = float;
343 struct PrecisionTrait<Precision::
FP64> {
344 using value_type = double;
348 struct PrecisionTrait<Precision::
FP16> {
349 using value_type = int16_t;
352 struct PrecisionTrait<Precision::BF16> {
353 using value_type = int16_t;
356 struct PrecisionTrait<Precision::
Q78> {
357 using value_type = uint16_t;
360 struct PrecisionTrait<Precision::
I16> {
361 using value_type = int16_t;
364 struct PrecisionTrait<Precision::
U16> {
365 using value_type = uint16_t;
368 struct PrecisionTrait<Precision::
U8> {
369 using value_type = uint8_t;
372 struct PrecisionTrait<Precision::
I8> {
373 using value_type = int8_t;
376 struct PrecisionTrait<Precision::BOOL> {
377 using value_type = uint8_t;
380 struct PrecisionTrait<Precision::
I32> {
381 using value_type = int32_t;
384 struct PrecisionTrait<Precision::
U32> {
385 using value_type = uint32_t;
388 struct PrecisionTrait<Precision::
I64> {
389 using value_type = int64_t;
392 struct PrecisionTrait<Precision::
U64> {
393 using value_type = uint64_t;
396 struct PrecisionTrait<Precision::
BIN> {
397 using value_type = int8_t;
401 inline uint8_t type_size_or_zero() {
407 using value_type = void;
411 struct PrecisionTrait<Precision::
MIXED> : PrecisionTrait<Precision::UNSPECIFIED> {};
414 inline uint8_t type_size_or_zero<void>() {
418 template <Precision::ePrecision T>
419 inline typename std::enable_if<std::is_same<std::integral_constant<Precision::ePrecision, Precision::FP16>,
420 std::integral_constant<Precision::ePrecision, T>>::value,
426 template <Precision::ePrecision T>
427 inline typename std::enable_if<!std::is_same<std::integral_constant<Precision::ePrecision, Precision::FP16>,
428 std::integral_constant<Precision::ePrecision, T>>::value,
431 return std::is_floating_point<typename PrecisionTrait<T>::value_type>::value;
434 template <Precision::ePrecision precision>
436 Precision::PrecisionInfo info;
439 size_t nBits = precision ==
BIN ? 1 : 8;
440 info.bitsSize = nBits * type_size_or_zero<typename PrecisionTrait<precision>::value_type>();
441 info.isFloat = is_floating<precision>();
442 info.value = precision;
447 return out << p.name();
451 return out << Precision(p).name();
454 inline std::ostream&
operator<<(std::ostream& os,
const std::vector<Precision>& values) {
456 for (
size_t i = 0; i < values.size(); ++i) {
458 if (i != (values.size() - 1ul)) {
466 inline constexpr uint32_t getPrecisionMask(
470 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:240
bool operator==(const ePrecision p) const noexcept
Equality operator with ePrecision enum value.
Definition: ie_precision.hpp:154
static Precision FromStr(const std::string &str)
Creates Precision from string with precision name.
Definition: ie_precision.hpp:222
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:99
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:251
bool operator!() const noexcept
Logical negation operator.
Definition: ie_precision.hpp:189
static Precision fromType(const char *typeName=nullptr)
Creates custom precision with specific underlined type.
Definition: ie_precision.hpp:93
static bool areSameStrings(const char *l, const char *r) noexcept
Compare two c-strings.
Definition: ie_precision.hpp:286
constexpr uint8_t getPrecVal() const noexcept
Gets the precision value of type ePrecision.
Definition: ie_precision.hpp:205
bool operator!=(const ePrecision p) const noexcept
Inequality operator with ePrecision enum value.
Definition: ie_precision.hpp:163
Precision(size_t bitsSize, const char *name=nullptr)
Custom precision constructor.
Definition: ie_precision.hpp:78
Precision & operator=(const ePrecision p) noexcept
Assignment operator with ePrecision enum value.
Definition: ie_precision.hpp:172
static PrecisionInfo getPrecisionInfo(ePrecision v)
Creates PrecisionInfo based on ePrecision.
Definition: ie_precision.hpp:302
const char * name() const noexcept
Getter of precision name.
Definition: ie_precision.hpp:213
ePrecision
Definition: ie_precision.hpp:26
@ BIN
Definition: ie_precision.hpp:42
@ U8
Definition: ie_precision.hpp:35
@ FP64
Definition: ie_precision.hpp:32
@ FP16
Definition: ie_precision.hpp:30
@ I32
Definition: ie_precision.hpp:38
@ FP32
Definition: ie_precision.hpp:29
@ U64
Definition: ie_precision.hpp:41
@ U32
Definition: ie_precision.hpp:39
@ I64
Definition: ie_precision.hpp:40
@ I8
Definition: ie_precision.hpp:36
@ CUSTOM
Definition: ie_precision.hpp:44
@ MIXED
Definition: ie_precision.hpp:28
@ Q78
Definition: ie_precision.hpp:33
@ I16
Definition: ie_precision.hpp:34
@ BF16
Definition: ie_precision.hpp:31
@ UNSPECIFIED
Definition: ie_precision.hpp:27
@ U16
Definition: ie_precision.hpp:37
@ BOOL
Definition: ie_precision.hpp:43
Precision(const Precision::ePrecision value)
Constructor with specified precision.
Definition: ie_precision.hpp:68
bool operator==(const Precision &p) const noexcept
Equality operator with Precision object.
Definition: ie_precision.hpp:144
bool isSigned() const noexcept
Checks if it is a signed value.
Definition: ie_precision.hpp:259
A header file for the main Inference Engine exception.
#define THROW_IE_EXCEPTION
A macro used to throw general exception with a description.
Definition: ie_exception.hpp:25
Inference Engine C++ API.
Definition: cldnn_config.hpp:15
std::ostream & operator<<(std::ostream &out, const Layout &p)
Prints a string representation of InferenceEngine::Layout to a stream.
Definition: ie_common.h:102
Particular precision traits.
Definition: ie_precision.hpp:334