10 #include <unordered_map>
37 struct PrecisionInfo {
42 const char *
name =
"UNSPECIFIED";
47 PrecisionInfo precisionInfo;
55 precisionInfo = getPrecisionInfo(value);
67 precisionInfo.bitsSize = bitsSize;
68 if (
name ==
nullptr) {
69 precisionInfo.name =
"CUSTOM";
71 precisionInfo.name =
name;
73 precisionInfo.value =
CUSTOM;
79 return Precision(8 *
sizeof(T), typeName ==
nullptr ?
typeid(T).
name() : typeName);
85 if (
sizeof(T) !=
size()) {
88 #define CASE(x, y) case x: return std::is_same<T, y>()
89 #define CASE2(x, y1, y2) case x: return std::is_same<T, y1>() || std::is_same<T, y2>()
91 switch (precisionInfo.value) {
93 CASE2(
FP16, int16_t, uint16_t);
99 CASE2(
Q78, int16_t, uint16_t);
100 default :
return areSameStrings(
name(), typeName ==
nullptr ?
typeid(T).
name() : typeName);
108 return precisionInfo.value == p &&
109 precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
110 areSameStrings(precisionInfo.name, p.precisionInfo.name);
115 return precisionInfo.value == p;
120 return precisionInfo.value != p;
125 precisionInfo = getPrecisionInfo(p);
130 explicit operator bool() const noexcept {
141 return precisionInfo.value;
145 const char *
name() const noexcept {
146 return precisionInfo.name;
151 static std::unordered_map<std::string, ePrecision > names = {
152 #define PRECISION_NAME(s) {#s, s}
159 PRECISION_NAME(
FP32),
160 PRECISION_NAME(
FP16),
161 PRECISION_NAME(
MIXED),
162 #undef PRECISION_NAME
164 auto i = names.find(str);
173 if (precisionInfo.bitsSize == 0) {
174 THROW_IE_EXCEPTION <<
" cannot estimate element if precision is " << precisionInfo.name;
176 return precisionInfo.bitsSize >> 3;
181 return precisionInfo.isFloat;
185 template<Precision::ePrecision precision>
186 static PrecisionInfo makePrecisionInfo(
const char *
name);
188 static bool areSameStrings(
const char *l,
const char *r) noexcept {
192 if (l ==
nullptr || r ==
nullptr)
195 for (; *l && *r; l++, r++) {
196 if (*l != *r)
return false;
201 static PrecisionInfo getPrecisionInfo(
ePrecision v) {
202 #define CASE(x) case x: return makePrecisionInfo<x>(#x);
213 default :
return makePrecisionInfo<UNSPECIFIED>(
"UNSPECIFIED");
222 template<Precision::ePrecision p>
229 using value_type = float;
234 using value_type = int16_t;
238 using value_type = uint16_t;
242 using value_type = int16_t;
246 using value_type = uint16_t;
250 using value_type = uint8_t;
254 using value_type = int8_t;
258 using value_type = int32_t;
262 inline uint8_t type_size_or_zero() {
268 using value_type = void;
276 inline uint8_t type_size_or_zero<void>() {
280 template<Precision::ePrecision T>
281 inline typename std::enable_if<std::is_same<
282 std::integral_constant<Precision::ePrecision, Precision::FP16>,
283 std::integral_constant<Precision::ePrecision, T>>::value,
bool>::type is_floating() {
287 template<Precision::ePrecision T>
288 inline typename std::enable_if<!std::is_same<
289 std::integral_constant<Precision::ePrecision, Precision::FP16>,
290 std::integral_constant<Precision::ePrecision, T>>::value,
bool>::type is_floating() {
291 return std::is_floating_point<typename PrecisionTrait<T>::value_type>::value;
294 template<Precision::ePrecision precision>
295 inline Precision::PrecisionInfo Precision::makePrecisionInfo(
const char *
name) {
296 Precision::PrecisionInfo info;
298 info.bitsSize = 8 * type_size_or_zero<typename PrecisionTrait<precision>::value_type>();
299 info.isFloat = is_floating<precision>();
300 info.value = precision;
305 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:223
static Precision fromType(const char *typeName=nullptr)
Creates custom precision with specific underlined type.
Definition: ie_precision.hpp:78
Definition: ie_argmax_layer.hpp:11
Definition: ie_precision.hpp:30
bool operator!() const noexcept
Logical negation operator.
Definition: ie_precision.hpp:135
Precision & operator=(const ePrecision p) noexcept
Assignment operator with ePrecision enum value.
Definition: ie_precision.hpp:124
Definition: ie_precision.hpp:27
size_t size() const
Returns size in bytes of single element of that precision.
Definition: ie_precision.hpp:172
Definition: ie_precision.hpp:26
bool operator==(const Precision &p) const noexcept
Equality operator with Precision object.
Definition: ie_precision.hpp:107
Definition: ie_precision.hpp:33
static Precision FromStr(const std::string &str)
Creates from string with precision name.
Definition: ie_precision.hpp:150
Precision(size_t bitsSize, const char *name=nullptr)
Custom precision constructor.
Definition: ie_precision.hpp:63
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:180
Definition: ie_precision.hpp:28
Precision(const Precision::ePrecision value)
Constructor with specified precision.
Definition: ie_precision.hpp:54
bool operator!=(const ePrecision p) const noexcept
Inequality operator with ePrecision enum value.
Definition: ie_precision.hpp:119
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 for store objects of current precision ...
Definition: ie_precision.hpp:84
const char * name() const noexcept
Getter of precision name.
Definition: ie_precision.hpp:145