summaryrefslogtreecommitdiff
path: root/inference-engine/include/ie_precision.hpp
blob: d50fe5cd6e790f8aa078b97ed0346afbade7fedf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (C) 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

/**
 * @brief A header file that provides class for describing precision of data
 * @file ie_precision.hpp
 */
#pragma once
#include <unordered_map>
#include <string>
#include "details/ie_exception.hpp"

namespace InferenceEngine {

/**
 * @brief This class holds precision value and provides precision related operations
 */
class Precision {
public:
    /** Enum to specify of different  */
    enum ePrecision : uint8_t {
        UNSPECIFIED = 255, /**< Unspecified value. Used by default */
        MIXED = 0,  /**< Mixed value. Can be received from network. No applicable for tensors */
        FP32 = 10,  /**< 32bit floating point value */
        FP16 = 11,  /**< 16bit floating point value */
        Q78 = 20,   /**< 16bit specific signed fixed point precision */
        I16 = 30,   /**< 16bit signed integer value */
        U8 = 40,    /**< 8bit unsigned integer value */
        I8 = 50,    /**< 8bit signed integer value */
        U16 = 60,   /**< 16bit unsigned integer value */
        I32 = 70,   /**< 32bit signed integer value */
        CUSTOM = 80 /**< custom precision has it's own name and size of elements */
    };

private:
    struct PrecisionInfo {
        /** @brief Size of underlined element */
        size_t bitsSize = 0;

        /** @brief Null terminated string with precision name */
        const char *name = "UNSPECIFIED";

        bool isFloat     = false;
        ePrecision value = Precision::UNSPECIFIED;
    };
    PrecisionInfo precisionInfo;

public:
    /** @brief Default constructor */
    Precision()  = default;

    /** @brief Constructor with specified precision */
    Precision(const Precision::ePrecision  value) {  // NOLINT
        precisionInfo = getPrecisionInfo(value);
    }

    /**
     * @brief Custom precision constructor
     * @param byteSize size of elements
     * @param name optional name string, used in serialisation
     */
    explicit Precision(size_t bitsSize, const char * name = nullptr) {
        if (bitsSize == 0) {
            THROW_IE_EXCEPTION << "Precision with 0 elements size not supported";
        }
        precisionInfo.bitsSize = bitsSize;
        if (name == nullptr) {
            precisionInfo.name = "CUSTOM";
        } else {
            precisionInfo.name = name;
        }
        precisionInfo.value = CUSTOM;
    }

    /** @brief Creates custom precision with specific underlined type */
    template <class T>
    static Precision fromType(const char * typeName = nullptr) {
        return Precision(8 * sizeof(T), typeName == nullptr ? typeid(T).name() : typeName);
    }

    /** @brief checks whether given storage class T can be used for store objects of current precision */
    template <class T>
    bool hasStorageType(const char * typeName = nullptr) const noexcept {
        if (sizeof(T) != size()) {
            return false;
        }
#define CASE(x, y) case x: return std::is_same<T, y>()
#define CASE2(x, y1, y2) case x: return std::is_same<T, y1>() || std::is_same<T, y2>()

        switch (precisionInfo.value) {
            CASE(FP32, float);
            CASE2(FP16, int16_t, uint16_t);
            CASE(I16, int16_t);
            CASE(I32, int32_t);
            CASE(U16, uint16_t);
            CASE(U8, uint8_t);
            CASE(I8, int8_t);
            CASE2(Q78, int16_t, uint16_t);
            default : return areSameStrings(name(), typeName == nullptr ? typeid(T).name() : typeName);
#undef CASE
#undef CASE2
        }
    }

    /** @brief Equality operator with Precision object */
    bool operator == (const Precision  & p) const noexcept {
        return precisionInfo.value == p &&
            precisionInfo.bitsSize == p.precisionInfo.bitsSize &&
            areSameStrings(precisionInfo.name, p.precisionInfo.name);
    }

    /** @brief Equality operator with ePrecision enum value */
    bool operator == (const ePrecision  p) const noexcept {
        return precisionInfo.value == p;
    }

    /** @brief Inequality operator with ePrecision enum value */
    bool operator != (const ePrecision   p) const noexcept {
        return precisionInfo.value != p;
    }

    /** @brief Assignment operator with ePrecision enum value */
    Precision & operator = (const ePrecision p) noexcept {
        precisionInfo = getPrecisionInfo(p);
        return *this;
    }

    /** @brief Cast operator to a bool */
    explicit operator bool() const noexcept {
        return precisionInfo.value != UNSPECIFIED;
    }

    /** @brief Logical negation operator */
    bool operator !() const noexcept {
        return precisionInfo.value == UNSPECIFIED;
    }

    /** @brief Cast operator to a ePrecision */
    operator Precision::ePrecision  () const noexcept {
        return precisionInfo.value;
    }

    /** @brief Getter of precision name */
    const char *name() const noexcept {
        return precisionInfo.name;
    }

    /** @brief Creates from string with precision name */
    static Precision FromStr(const std::string &str) {
        static std::unordered_map<std::string, ePrecision > names = {
#define     PRECISION_NAME(s) {#s, s}
            PRECISION_NAME(Q78),
            PRECISION_NAME(U8),
            PRECISION_NAME(I8),
            PRECISION_NAME(I16),
            PRECISION_NAME(I32),
            PRECISION_NAME(U16),
            PRECISION_NAME(FP32),
            PRECISION_NAME(FP16),
            PRECISION_NAME(MIXED),
#undef      PRECISION_NAME
        };
        auto i = names.find(str);
        return i == names.end() ? Precision() : Precision(i->second);
    }

    /**
     * @brief Returns size in bytes of single element of that precision
     * @deprecated : size of precision will be report in bits in future releases
     */
    size_t size() const {
        if (precisionInfo.bitsSize == 0) {
            THROW_IE_EXCEPTION << " cannot estimate element if precision is " << precisionInfo.name;
        }
        return precisionInfo.bitsSize >> 3;
    }

    /** @brief Checks if it is a floating point */
    bool is_float() const {
        return precisionInfo.isFloat;
    }

 protected:
    template<Precision::ePrecision precision>
    static PrecisionInfo makePrecisionInfo(const char * name);

    static bool areSameStrings(const char *l, const char *r) noexcept {
        if (l == r)
            return true;

        if (l == nullptr || r == nullptr)
            return false;

        for (; *l && *r; l++, r++) {
            if (*l != *r) return false;
        }
        return *l == *r;
    }

    static PrecisionInfo getPrecisionInfo(ePrecision v) {
#define CASE(x) case x: return makePrecisionInfo<x>(#x);
        switch (v) {
            CASE(FP32);
            CASE(FP16);
            CASE(I16);
            CASE(I32);
            CASE(U16);
            CASE(U8);
            CASE(I8);
            CASE(Q78);
            CASE(MIXED);
            default : return makePrecisionInfo<UNSPECIFIED>("UNSPECIFIED");
#undef CASE
        }
    }
};

/**
 * @brief Particular precision traits
 */
template<Precision::ePrecision p>
struct PrecisionTrait {
};

/** @cond INTERNAL */
template<>
struct PrecisionTrait<Precision::FP32> {
    using value_type = float;
};

template<>
struct PrecisionTrait<Precision::FP16> {
    using value_type = int16_t;
};
template<>
struct PrecisionTrait<Precision::Q78> {
    using value_type = uint16_t;
};
template<>
struct PrecisionTrait<Precision::I16> {
    using value_type = int16_t;
};
template<>
struct PrecisionTrait<Precision::U16> {
    using value_type = uint16_t;
};
template<>
struct PrecisionTrait<Precision::U8> {
    using value_type = uint8_t;
};
template<>
struct PrecisionTrait<Precision::I8> {
    using value_type = int8_t;
};
template<>
struct PrecisionTrait<Precision::I32> {
    using value_type = int32_t;
};

template<class T>
inline uint8_t type_size_or_zero() {
    return sizeof(T);
}

template<>
struct PrecisionTrait<Precision::UNSPECIFIED> {
    using value_type = void;
};

template<>
struct PrecisionTrait<Precision::MIXED> : PrecisionTrait<Precision::UNSPECIFIED>{
};

template<>
inline uint8_t type_size_or_zero<void>() {
    return 0;
}

template<Precision::ePrecision T>
inline typename std::enable_if<std::is_same<
    std::integral_constant<Precision::ePrecision, Precision::FP16>,
    std::integral_constant<Precision::ePrecision, T>>::value, bool>::type is_floating() {
    return true;
}

template<Precision::ePrecision T>
inline typename std::enable_if<!std::is_same<
    std::integral_constant<Precision::ePrecision, Precision::FP16>,
    std::integral_constant<Precision::ePrecision, T>>::value, bool>::type is_floating() {
    return std::is_floating_point<typename PrecisionTrait<T>::value_type>::value;
}

template<Precision::ePrecision precision>
inline Precision::PrecisionInfo Precision::makePrecisionInfo(const char *name) {
    Precision::PrecisionInfo info;
    info.name = name;
    info.bitsSize = 8 * type_size_or_zero<typename PrecisionTrait<precision>::value_type>();
    info.isFloat = is_floating<precision>();
    info.value = precision;
    return info;
}

inline std::ostream & operator << (std::ostream &out, const InferenceEngine::Precision & p) {
    return out << p.name();
}

inline std::ostream & operator << (std::ostream &out, const InferenceEngine::Precision::ePrecision & p) {
    return out << Precision(p).name();
}

/** @endcond */

}  // namespace InferenceEngine