Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / gna_plugin / quantization / precision_ex.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include "ie_precision.hpp"
8
9 namespace InferenceEngine {
10
11 /**
12  * @brief reverse trait for getting some precision from it's underlined memory type
13  * this might not work for certain precisions : for Q78, U16
14  * @tparam T
15  */
16 template<class T>
17 struct precision_from_media {
18     static const Precision::ePrecision type = Precision::CUSTOM;
19 };
20
21 template<>
22 struct precision_from_media<float> {
23     static const Precision::ePrecision type = Precision::FP32;
24 };
25
26 template<>
27 struct precision_from_media<uint16_t> {
28     static const Precision::ePrecision type = Precision::FP16;
29 };
30
31 template<>
32 struct precision_from_media<int16_t> {
33     static const Precision::ePrecision type = Precision::I16;
34 };
35
36 template<>
37 struct precision_from_media<uint8_t> {
38     static const Precision::ePrecision type = Precision::U8;
39 };
40
41 template<>
42 struct precision_from_media<int8_t> {
43     static const Precision::ePrecision type = Precision::I8;
44 };
45
46 template<>
47 struct precision_from_media<int32_t> {
48     static const Precision::ePrecision type = Precision::I32;
49 };
50
51 /**
52  * @brief container for storing both precision and it's underlined media type
53  * @tparam TMedia
54  */
55 template <class TMedia>
56 class TPrecision : public Precision {
57  public:
58     typedef TMedia MediaType;
59     TPrecision() : Precision(precision_from_media<TMedia>::type) {}
60     explicit TPrecision(const Precision & that) : Precision(that) {}
61     TPrecision & operator = (const Precision & that) {
62         Precision::operator=(that);
63         return *this;
64     }
65     explicit TPrecision(const Precision::ePrecision  value) : Precision(value) {}
66 };
67
68 template <class T> TPrecision<T> createTPrecision() {
69     TPrecision<T> cnt(InferenceEngine::Precision::fromType<T>());
70     return cnt;
71 }
72
73 template <InferenceEngine::Precision::ePrecision T>
74 TPrecision<typename InferenceEngine::PrecisionTrait<T>::value_type> createTPrecision() {
75     TPrecision<typename InferenceEngine::PrecisionTrait<T>::value_type> cnt(T);
76     return cnt;
77 }
78
79
80 // special case for Mixed, or undefined precisions
81 template <>
82 class TPrecision<void> : public Precision {
83  public:
84     typedef void MediaType;
85     TPrecision() = default;
86     explicit TPrecision(const Precision & that) : Precision(that) {}
87     TPrecision & operator = (const Precision & that) {
88         Precision::operator=(that);
89         return *this;
90     }
91     explicit TPrecision(const Precision::ePrecision  value) : Precision(value) {}
92 };
93
94
95 }  // namespace InferenceEngine