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 {
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);
115 /** @brief Equality operator with Precision object */
116 bool operator == (const Precision & p) const noexcept {
117 return precisionInfo.value == p &&
118 precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
119 areSameStrings(precisionInfo.name, p.precisionInfo.name);
122 /** @brief Equality operator with ePrecision enum value */
123 bool operator == (const ePrecision p) const noexcept {
124 return precisionInfo.value == p;
127 /** @brief Inequality operator with ePrecision enum value */
128 bool operator != (const ePrecision p) const noexcept {
129 return precisionInfo.value != p;
132 /** @brief Assignment operator with ePrecision enum value */
133 Precision & operator = (const ePrecision p) noexcept {
134 precisionInfo = getPrecisionInfo(p);
138 /** @brief Cast operator to a bool */
139 explicit operator bool() const noexcept {
140 return precisionInfo.value != UNSPECIFIED;
143 /** @brief Logical negation operator */
144 bool operator !() const noexcept {
145 return precisionInfo.value == UNSPECIFIED;
148 /** @brief Cast operator to a ePrecision */
149 operator Precision::ePrecision () const noexcept {
150 return precisionInfo.value;
153 /** @brief Getter of precision name */
154 const char *name() const noexcept {
155 return precisionInfo.name;
158 /** @brief Creates from string with precision name */
159 static Precision FromStr(const std::string &str) {
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);
175 return i == names.end() ? Precision() : Precision(i->second);
179 * @brief Returns size in bytes of single element of that precision
180 * @deprecated : size of precision will be reported in bits in future releases
182 size_t size() const {
183 if (precisionInfo.bitsSize == 0) {
184 THROW_IE_EXCEPTION << " cannot estimate element if precision is " << precisionInfo.name;
186 return precisionInfo.bitsSize >> 3;
189 /** @brief Checks if it is a floating point */
190 bool is_float() const noexcept {
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");
231 * @brief Particular precision traits
233 template<Precision::ePrecision p>
234 struct PrecisionTrait {
237 /** @cond INTERNAL */
239 struct PrecisionTrait<Precision::FP32> {
240 using value_type = float;
244 struct PrecisionTrait<Precision::FP16> {
245 using value_type = int16_t;
248 struct PrecisionTrait<Precision::Q78> {
249 using value_type = uint16_t;
252 struct PrecisionTrait<Precision::I16> {
253 using value_type = int16_t;
256 struct PrecisionTrait<Precision::U16> {
257 using value_type = uint16_t;
260 struct PrecisionTrait<Precision::U8> {
261 using value_type = uint8_t;
264 struct PrecisionTrait<Precision::I8> {
265 using value_type = int8_t;
268 struct PrecisionTrait<Precision::I32> {
269 using value_type = int32_t;
272 struct PrecisionTrait<Precision::BIN> {
273 using value_type = int8_t;
277 inline uint8_t type_size_or_zero() {
282 struct PrecisionTrait<Precision::UNSPECIFIED> {
283 using value_type = void;
287 struct PrecisionTrait<Precision::MIXED> : PrecisionTrait<Precision::UNSPECIFIED>{
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;
321 inline std::ostream & operator << (std::ostream &out, const InferenceEngine::Precision & p) {
322 return out << p.name();
325 inline std::ostream & operator << (std::ostream &out, const InferenceEngine::Precision::ePrecision & p) {
326 return out << Precision(p).name();
331 } // namespace InferenceEngine