10 #include <unordered_map> 39 struct PrecisionInfo {
44 const char *
name =
"UNSPECIFIED";
49 PrecisionInfo precisionInfo;
57 precisionInfo = getPrecisionInfo(value);
69 precisionInfo.bitsSize = bitsSize;
70 if (
name ==
nullptr) {
71 precisionInfo.name =
"CUSTOM";
73 precisionInfo.name =
name;
75 precisionInfo.value =
CUSTOM;
81 return Precision(8 *
sizeof(T), typeName ==
nullptr ?
typeid(T).
name() : typeName);
88 if (precisionInfo.value !=
BIN) {
89 if (
sizeof(T) !=
size()) {
93 #define CASE(x, y) case x: return std::is_same<T, y>() 94 #define CASE2(x, y1, y2) case x: return std::is_same<T, y1>() || std::is_same<T, y2>() 96 switch (precisionInfo.value) {
98 CASE2(
FP16, int16_t, uint16_t);
105 CASE2(
Q78, int16_t, uint16_t);
106 CASE2(
BIN, int8_t, uint8_t);
108 return areSameStrings(
name(), typeName ==
nullptr ?
typeid(T).
name() : typeName);
119 return precisionInfo.value == p &&
120 precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
121 areSameStrings(precisionInfo.name, p.precisionInfo.name);
126 return precisionInfo.value == p;
131 return precisionInfo.value != p;
136 precisionInfo = getPrecisionInfo(p);
141 explicit operator bool() const noexcept {
152 return precisionInfo.value;
156 const char *
name() const noexcept {
157 return precisionInfo.name;
162 static std::unordered_map<std::string, ePrecision > names = {
163 #define PRECISION_NAME(s) {#s, s} 171 PRECISION_NAME(
FP32),
172 PRECISION_NAME(
FP16),
173 PRECISION_NAME(
MIXED),
175 #undef PRECISION_NAME 177 auto i = names.find(str);
186 if (precisionInfo.bitsSize == 0) {
187 THROW_IE_EXCEPTION <<
" cannot estimate element if precision is " << precisionInfo.name;
189 return precisionInfo.bitsSize >> 3;
194 return precisionInfo.isFloat;
198 template<Precision::ePrecision precision>
199 static PrecisionInfo makePrecisionInfo(
const char *
name);
201 static bool areSameStrings(
const char *l,
const char *r) noexcept {
205 if (l ==
nullptr || r ==
nullptr)
208 for (; *l && *r; l++, r++) {
209 if (*l != *r)
return false;
214 static PrecisionInfo getPrecisionInfo(
ePrecision v) {
215 #define CASE(x) case x: return makePrecisionInfo<x>(#x); 228 default :
return makePrecisionInfo<UNSPECIFIED>(
"UNSPECIFIED");
237 template<Precision::ePrecision p>
244 using value_type = float;
249 using value_type = int16_t;
253 using value_type = uint16_t;
257 using value_type = int16_t;
261 using value_type = uint16_t;
265 using value_type = uint8_t;
269 using value_type = int8_t;
273 using value_type = int32_t;
277 using value_type = int64_t;
281 using value_type = int8_t;
285 inline uint8_t type_size_or_zero() {
291 using value_type = void;
299 inline uint8_t type_size_or_zero<void>() {
303 template<Precision::ePrecision T>
304 inline typename std::enable_if<std::is_same<
305 std::integral_constant<Precision::ePrecision, Precision::FP16>,
306 std::integral_constant<Precision::ePrecision, T>>::value,
bool>::type is_floating() {
310 template<Precision::ePrecision T>
311 inline typename std::enable_if<!std::is_same<
312 std::integral_constant<Precision::ePrecision, Precision::FP16>,
313 std::integral_constant<Precision::ePrecision, T>>::value,
bool>::type is_floating() {
314 return std::is_floating_point<typename PrecisionTrait<T>::value_type>::value;
317 template<Precision::ePrecision precision>
318 inline Precision::PrecisionInfo Precision::makePrecisionInfo(
const char *
name) {
319 Precision::PrecisionInfo info;
322 size_t nBits = precision ==
BIN ? 1 : 8;
323 info.bitsSize = nBits * type_size_or_zero<typename PrecisionTrait<precision>::value_type>();
324 info.isFloat = is_floating<precision>();
325 info.value = precision;
330 return out << p.
name();
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:22
Definition: ie_precision.hpp:31
Definition: ie_precision.hpp:25
ePrecision
Definition: ie_precision.hpp:22
Particular precision traits.
Definition: ie_precision.hpp:238
static Precision fromType(const char *typeName=nullptr)
Creates custom precision with specific underlined type.
Definition: ie_precision.hpp:80
Definition: ie_argmax_layer.hpp:11
Definition: ie_precision.hpp:30
bool operator!() const noexcept
Logical negation operator.
Definition: ie_precision.hpp:146
Precision & operator=(const ePrecision p) noexcept
Assignment operator with ePrecision enum value.
Definition: ie_precision.hpp:135
Definition: ie_precision.hpp:27
size_t size() const
Returns size in bytes of single element of that precision.
Definition: ie_precision.hpp:185
Definition: ie_precision.hpp:26
bool operator==(const Precision &p) const noexcept
Equality operator with Precision object.
Definition: ie_precision.hpp:118
Definition: ie_precision.hpp:34
bool is_float() const noexcept
Checks if it is a floating point.
Definition: ie_precision.hpp:193
Definition: ie_precision.hpp:35
static Precision FromStr(const std::string &str)
Creates from string with precision name.
Definition: ie_precision.hpp:161
Precision(size_t bitsSize, const char *name=nullptr)
Custom precision constructor.
Definition: ie_precision.hpp:65
Definition: ie_precision.hpp:33
Definition: ie_precision.hpp:29
Definition: ie_precision.hpp:24
Definition: ie_precision.hpp:32
Definition: ie_precision.hpp:28
Precision(const Precision::ePrecision value)
Constructor with specified precision.
Definition: ie_precision.hpp:56
bool operator!=(const ePrecision p) const noexcept
Inequality operator with ePrecision enum value.
Definition: ie_precision.hpp:130
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:19
Definition: ie_precision.hpp:23
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:86
const char * name() const noexcept
Getter of precision name.
Definition: ie_precision.hpp:156