10 #include <unordered_map>
38 struct PrecisionInfo {
43 const char *
name =
"UNSPECIFIED";
48 PrecisionInfo precisionInfo;
56 precisionInfo = getPrecisionInfo(value);
68 precisionInfo.bitsSize = bitsSize;
69 if (
name ==
nullptr) {
70 precisionInfo.name =
"CUSTOM";
72 precisionInfo.name =
name;
74 precisionInfo.value =
CUSTOM;
80 return Precision(8 *
sizeof(T), typeName ==
nullptr ?
typeid(T).
name() : typeName);
86 if (precisionInfo.value !=
BIN) {
87 if (
sizeof(T) !=
size()) {
91 #define CASE(x, y) case x: return std::is_same<T, y>()
92 #define CASE2(x, y1, y2) case x: return std::is_same<T, y1>() || std::is_same<T, y2>()
94 switch (precisionInfo.value) {
96 CASE2(
FP16, int16_t, uint16_t);
102 CASE2(
Q78, int16_t, uint16_t);
103 CASE2(
BIN, int8_t, uint8_t);
104 default :
return areSameStrings(
name(), typeName ==
nullptr ?
typeid(T).
name() : typeName);
112 return precisionInfo.value == p &&
113 precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
114 areSameStrings(precisionInfo.name, p.precisionInfo.name);
119 return precisionInfo.value == p;
124 return precisionInfo.value != p;
129 precisionInfo = getPrecisionInfo(p);
134 explicit operator bool() const noexcept {
145 return precisionInfo.value;
149 const char *
name() const noexcept {
150 return precisionInfo.name;
155 static std::unordered_map<std::string, ePrecision > names = {
156 #define PRECISION_NAME(s) {#s, s}
163 PRECISION_NAME(
FP32),
164 PRECISION_NAME(
FP16),
165 PRECISION_NAME(
MIXED),
167 #undef PRECISION_NAME
169 auto i = names.find(str);
178 if (precisionInfo.bitsSize == 0) {
179 THROW_IE_EXCEPTION <<
" cannot estimate element if precision is " << precisionInfo.name;
181 return precisionInfo.bitsSize >> 3;
186 return precisionInfo.isFloat;
190 template<Precision::ePrecision precision>
191 static PrecisionInfo makePrecisionInfo(
const char *
name);
193 static bool areSameStrings(
const char *l,
const char *r) noexcept {
197 if (l ==
nullptr || r ==
nullptr)
200 for (; *l && *r; l++, r++) {
201 if (*l != *r)
return false;
206 static PrecisionInfo getPrecisionInfo(
ePrecision v) {
207 #define CASE(x) case x: return makePrecisionInfo<x>(#x);
219 default :
return makePrecisionInfo<UNSPECIFIED>(
"UNSPECIFIED");
228 template<Precision::ePrecision p>
235 using value_type = float;
240 using value_type = int16_t;
244 using value_type = uint16_t;
248 using value_type = int16_t;
252 using value_type = uint16_t;
256 using value_type = uint8_t;
260 using value_type = int8_t;
264 using value_type = int32_t;
268 using value_type = int8_t;
272 inline uint8_t type_size_or_zero() {
278 using value_type = void;
286 inline uint8_t type_size_or_zero<void>() {
290 template<Precision::ePrecision T>
291 inline typename std::enable_if<std::is_same<
292 std::integral_constant<Precision::ePrecision, Precision::FP16>,
293 std::integral_constant<Precision::ePrecision, T>>::value,
bool>::type is_floating() {
297 template<Precision::ePrecision T>
298 inline typename std::enable_if<!std::is_same<
299 std::integral_constant<Precision::ePrecision, Precision::FP16>,
300 std::integral_constant<Precision::ePrecision, T>>::value,
bool>::type is_floating() {
301 return std::is_floating_point<typename PrecisionTrait<T>::value_type>::value;
304 template<Precision::ePrecision precision>
305 inline Precision::PrecisionInfo Precision::makePrecisionInfo(
const char *
name) {
306 Precision::PrecisionInfo info;
309 int nBits = precision ==
BIN ? 1 : 8;
310 info.bitsSize = nBits * type_size_or_zero<typename PrecisionTrait<precision>::value_type>();
311 info.isFloat = is_floating<precision>();
312 info.value = precision;
317 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:229
static Precision fromType(const char *typeName=nullptr)
Creates custom precision with specific underlined type.
Definition: ie_precision.hpp:79
Definition: ie_argmax_layer.hpp:11
Definition: ie_precision.hpp:30
bool operator!() const noexcept
Logical negation operator.
Definition: ie_precision.hpp:139
Precision & operator=(const ePrecision p) noexcept
Assignment operator with ePrecision enum value.
Definition: ie_precision.hpp:128
Definition: ie_precision.hpp:27
size_t size() const
Returns size in bytes of single element of that precision.
Definition: ie_precision.hpp:177
Definition: ie_precision.hpp:26
bool operator==(const Precision &p) const noexcept
Equality operator with Precision object.
Definition: ie_precision.hpp:111
Definition: ie_precision.hpp:33
Definition: ie_precision.hpp:34
static Precision FromStr(const std::string &str)
Creates from string with precision name.
Definition: ie_precision.hpp:154
Precision(size_t bitsSize, const char *name=nullptr)
Custom precision constructor.
Definition: ie_precision.hpp:64
Definition: ie_precision.hpp:29
Definition: ie_precision.hpp:24
Definition: ie_precision.hpp:32
bool is_float() const
Checks if it is a floating point.
Definition: ie_precision.hpp:185
Definition: ie_precision.hpp:28
Precision(const Precision::ePrecision value)
Constructor with specified precision.
Definition: ie_precision.hpp:55
bool operator!=(const ePrecision p) const noexcept
Inequality operator with ePrecision enum value.
Definition: ie_precision.hpp:123
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:85
const char * name() const noexcept
Getter of precision name.
Definition: ie_precision.hpp:149