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);
87 if (precisionInfo.value !=
BIN) {
88 if (
sizeof(T) !=
size()) {
92 #define CASE(x, y) case x: return std::is_same<T, y>()
93 #define CASE2(x, y1, y2) case x: return std::is_same<T, y1>() || std::is_same<T, y2>()
95 switch (precisionInfo.value) {
97 CASE2(
FP16, int16_t, uint16_t);
103 CASE2(
Q78, int16_t, uint16_t);
104 CASE2(
BIN, int8_t, uint8_t);
106 return areSameStrings(
name(), typeName ==
nullptr ?
typeid(T).
name() : typeName);
117 return precisionInfo.value == p &&
118 precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
119 areSameStrings(precisionInfo.name, p.precisionInfo.name);
124 return precisionInfo.value == p;
129 return precisionInfo.value != p;
134 precisionInfo = getPrecisionInfo(p);
139 explicit operator bool() const noexcept {
150 return precisionInfo.value;
154 const char *
name() const noexcept {
155 return precisionInfo.name;
160 static std::unordered_map<std::string, ePrecision > names = {
161 #define PRECISION_NAME(s) {#s, s}
168 PRECISION_NAME(
FP32),
169 PRECISION_NAME(
FP16),
170 PRECISION_NAME(
MIXED),
172 #undef PRECISION_NAME
174 auto i = names.find(str);
183 if (precisionInfo.bitsSize == 0) {
184 THROW_IE_EXCEPTION <<
" cannot estimate element if precision is " << precisionInfo.name;
186 return precisionInfo.bitsSize >> 3;
191 return precisionInfo.isFloat;
195 template<Precision::ePrecision precision>
196 static PrecisionInfo makePrecisionInfo(
const char *
name);
198 static bool areSameStrings(
const char *l,
const char *r) noexcept {
202 if (l ==
nullptr || r ==
nullptr)
205 for (; *l && *r; l++, r++) {
206 if (*l != *r)
return false;
211 static PrecisionInfo getPrecisionInfo(
ePrecision v) {
212 #define CASE(x) case x: return makePrecisionInfo<x>(#x);
224 default :
return makePrecisionInfo<UNSPECIFIED>(
"UNSPECIFIED");
233 template<Precision::ePrecision p>
240 using value_type = float;
245 using value_type = int16_t;
249 using value_type = uint16_t;
253 using value_type = int16_t;
257 using value_type = uint16_t;
261 using value_type = uint8_t;
265 using value_type = int8_t;
269 using value_type = int32_t;
273 using value_type = int8_t;
277 inline uint8_t type_size_or_zero() {
283 using value_type = void;
291 inline uint8_t type_size_or_zero<void>() {
295 template<Precision::ePrecision T>
296 inline typename std::enable_if<std::is_same<
297 std::integral_constant<Precision::ePrecision, Precision::FP16>,
298 std::integral_constant<Precision::ePrecision, T>>::value,
bool>::type is_floating() {
302 template<Precision::ePrecision T>
303 inline typename std::enable_if<!std::is_same<
304 std::integral_constant<Precision::ePrecision, Precision::FP16>,
305 std::integral_constant<Precision::ePrecision, T>>::value,
bool>::type is_floating() {
306 return std::is_floating_point<typename PrecisionTrait<T>::value_type>::value;
309 template<Precision::ePrecision precision>
310 inline Precision::PrecisionInfo Precision::makePrecisionInfo(
const char *
name) {
311 Precision::PrecisionInfo info;
314 size_t nBits = precision ==
BIN ? 1 : 8;
315 info.bitsSize = nBits * type_size_or_zero<typename PrecisionTrait<precision>::value_type>();
316 info.isFloat = is_floating<precision>();
317 info.value = precision;
322 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:234
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:144
Precision & operator=(const ePrecision p) noexcept
Assignment operator with ePrecision enum value.
Definition: ie_precision.hpp:133
Definition: ie_precision.hpp:27
size_t size() const
Returns size in bytes of single element of that precision.
Definition: ie_precision.hpp:182
Definition: ie_precision.hpp:26
bool operator==(const Precision &p) const noexcept
Equality operator with Precision object.
Definition: ie_precision.hpp:116
Definition: ie_precision.hpp:33
bool is_float() const noexcept
Checks if it is a floating point.
Definition: ie_precision.hpp:190
Definition: ie_precision.hpp:34
static Precision FromStr(const std::string &str)
Creates from string with precision name.
Definition: ie_precision.hpp:159
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
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:128
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:154