1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
6 * @brief A header file that provides class for describing precision of data
7 * @file ie_precision.hpp
10 #include <unordered_map>
12 #include "details/ie_exception.hpp"
14 namespace InferenceEngine {
17 * @brief This class holds precision value and provides precision related operations
21 /** Enum to specify of different */
22 enum ePrecision : uint8_t {
23 UNSPECIFIED = 255, /**< Unspecified value. Used by default */
24 MIXED = 0, /**< Mixed value. Can be received from network. No applicable for tensors */
25 FP32 = 10, /**< 32bit floating point value */
26 FP16 = 11, /**< 16bit floating point value */
27 Q78 = 20, /**< 16bit specific signed fixed point precision */
28 I16 = 30, /**< 16bit signed integer value */
29 U8 = 40, /**< 8bit unsigned integer value */
30 I8 = 50, /**< 8bit signed integer value */
31 U16 = 60, /**< 16bit unsigned integer value */
32 I32 = 70, /**< 32bit signed integer value */
33 BIN = 71, /**< 1bit integer value */
34 CUSTOM = 80 /**< custom precision has it's own name and size of elements */
38 struct PrecisionInfo {
39 /** @brief Size of underlined element */
42 /** @brief Null terminated string with precision name */
43 const char *name = "UNSPECIFIED";
46 ePrecision value = Precision::UNSPECIFIED;
48 PrecisionInfo precisionInfo;
51 /** @brief Default constructor */
52 Precision() = default;
54 /** @brief Constructor with specified precision */
55 Precision(const Precision::ePrecision value) { // NOLINT
56 precisionInfo = getPrecisionInfo(value);
60 * @brief Custom precision constructor
61 * @param byteSize size of elements
62 * @param name optional name string, used in serialisation
64 explicit Precision(size_t bitsSize, const char * name = nullptr) {
66 THROW_IE_EXCEPTION << "Precision with 0 elements size not supported";
68 precisionInfo.bitsSize = bitsSize;
69 if (name == nullptr) {
70 precisionInfo.name = "CUSTOM";
72 precisionInfo.name = name;
74 precisionInfo.value = CUSTOM;
77 /** @brief Creates custom precision with specific underlined type */
79 static Precision fromType(const char * typeName = nullptr) {
80 return Precision(8 * sizeof(T), typeName == nullptr ? typeid(T).name() : typeName);
83 /** @brief checks whether given storage class T can be used to store objects of current precision */
85 bool hasStorageType(const char * typeName = nullptr) const noexcept {
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);
110 /** @brief Equality operator with Precision object */
111 bool operator == (const Precision & p) const noexcept {
112 return precisionInfo.value == p &&
113 precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
114 areSameStrings(precisionInfo.name, p.precisionInfo.name);
117 /** @brief Equality operator with ePrecision enum value */
118 bool operator == (const ePrecision p) const noexcept {
119 return precisionInfo.value == p;
122 /** @brief Inequality operator with ePrecision enum value */
123 bool operator != (const ePrecision p) const noexcept {
124 return precisionInfo.value != p;
127 /** @brief Assignment operator with ePrecision enum value */
128 Precision & operator = (const ePrecision p) noexcept {
129 precisionInfo = getPrecisionInfo(p);
133 /** @brief Cast operator to a bool */
134 explicit operator bool() const noexcept {
135 return precisionInfo.value != UNSPECIFIED;
138 /** @brief Logical negation operator */
139 bool operator !() const noexcept {
140 return precisionInfo.value == UNSPECIFIED;
143 /** @brief Cast operator to a ePrecision */
144 operator Precision::ePrecision () const noexcept {
145 return precisionInfo.value;
148 /** @brief Getter of precision name */
149 const char *name() const noexcept {
150 return precisionInfo.name;
153 /** @brief Creates from string with precision name */
154 static Precision FromStr(const std::string &str) {
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);
170 return i == names.end() ? Precision() : Precision(i->second);
174 * @brief Returns size in bytes of single element of that precision
175 * @deprecated : size of precision will be report in bits in future releases
177 size_t size() const {
178 if (precisionInfo.bitsSize == 0) {
179 THROW_IE_EXCEPTION << " cannot estimate element if precision is " << precisionInfo.name;
181 return precisionInfo.bitsSize >> 3;
184 /** @brief Checks if it is a floating point */
185 bool is_float() const {
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");
226 * @brief Particular precision traits
228 template<Precision::ePrecision p>
229 struct PrecisionTrait {
232 /** @cond INTERNAL */
234 struct PrecisionTrait<Precision::FP32> {
235 using value_type = float;
239 struct PrecisionTrait<Precision::FP16> {
240 using value_type = int16_t;
243 struct PrecisionTrait<Precision::Q78> {
244 using value_type = uint16_t;
247 struct PrecisionTrait<Precision::I16> {
248 using value_type = int16_t;
251 struct PrecisionTrait<Precision::U16> {
252 using value_type = uint16_t;
255 struct PrecisionTrait<Precision::U8> {
256 using value_type = uint8_t;
259 struct PrecisionTrait<Precision::I8> {
260 using value_type = int8_t;
263 struct PrecisionTrait<Precision::I32> {
264 using value_type = int32_t;
267 struct PrecisionTrait<Precision::BIN> {
268 using value_type = int8_t;
272 inline uint8_t type_size_or_zero() {
277 struct PrecisionTrait<Precision::UNSPECIFIED> {
278 using value_type = void;
282 struct PrecisionTrait<Precision::MIXED> : PrecisionTrait<Precision::UNSPECIFIED>{
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;
316 inline std::ostream & operator << (std::ostream &out, const InferenceEngine::Precision & p) {
317 return out << p.name();
320 inline std::ostream & operator << (std::ostream &out, const InferenceEngine::Precision::ePrecision & p) {
321 return out << Precision(p).name();
326 } // namespace InferenceEngine