Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / helpers / single_layer_common.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <ie_blob.h>
8 #include <ie_layers_property.hpp>
9 #include <inference_engine/precision_utils.h>
10 #include <inference_engine/parsers.h>
11 #include <xml_net_builder.hpp>
12 #include <xml_helper.hpp>
13
14 #ifndef USE_BOOST_RE
15
16 #include <regex>
17
18 #define REPLACE_WITH_STR(SRC, PATTERN, STR) SRC = std::regex_replace(SRC, std::regex(PATTERN), STR)
19 #define FIND_STR(SRC, PATTERN) std::regex_search(SRC, std::regex(PATTERN))
20 #else
21 #include <boost/regex.hpp>
22 #define REPLACE_WITH_STR(SRC, PATTERN, STR) SRC = boost::regex_replace(SRC, boost::regex(PATTERN), STR)
23 #define FIND_STR(SRC, PATTERN) boost::regex_search(SRC, boost::regex(PATTERN))
24 #endif
25
26 #define REPLACE_WITH_NUM(SRC, PATTERN, NUM) REPLACE_WITH_STR(SRC, PATTERN, std::to_string(NUM))
27 #define REPLACE_WITH_NUM_VECTOR(SRC, PATTERN, NUMS) \
28         { std::string result; \
29         if (NUMS.size() > 0) { \
30             result += std::to_string(NUMS[0]); \
31             for (int i = 1; i < NUMS.size(); i++) { \
32                     result += "," + std::to_string(NUMS[i]); \
33             } \
34         } \
35         REPLACE_WITH_STR(SRC, PATTERN, result); }
36 #define REPLACE_WITH_NUM_VECTOR_REVERSE(SRC, PATTERN, NUMS) \
37         { std::string result; \
38         auto nums_size = NUMS.size(); \
39         if (nums_size > 0) { \
40             result += std::to_string(NUMS[nums_size - 1]); \
41             for (int i = 2; i <= nums_size; i++) { \
42                     result += "," + std::to_string(NUMS[nums_size - i]); \
43             } \
44         } \
45         REPLACE_WITH_STR(SRC, PATTERN, result); }
46 #define REMOVE_LINE(SRC, PATTERN) REPLACE_WITH_STR(SRC, PATTERN, "")
47
48 struct conv_common_params {
49     InferenceEngine::PropertyVector<unsigned int> stride;
50     InferenceEngine::PropertyVector<unsigned int> kernel;
51     InferenceEngine::PropertyVector<unsigned int> pads_begin;
52     InferenceEngine::PropertyVector<unsigned int> pads_end;
53     InferenceEngine::PropertyVector<unsigned int> dilation;
54     std::string auto_pad;
55     size_t group;
56     size_t out_c;
57     bool with_bias;
58 };
59
60 struct pool_common_params {
61     InferenceEngine::PropertyVector<unsigned int> stride;
62     InferenceEngine::PropertyVector<unsigned int> kernel;
63     InferenceEngine::PropertyVector<unsigned int> pads_begin;
64     InferenceEngine::PropertyVector<unsigned int> pads_end;
65     std::string auto_pad;
66     bool avg;
67     bool exclude_pad;
68 };
69
70 struct eltwise_common_params {
71     std::string operation;
72     std::vector<float> coeff;
73 };
74
75 #define PRETTY_PARAM(name, type)                                                            \
76     class name                                                                              \
77     {                                                                                       \
78     public:                                                                                 \
79         typedef type param_type;                                                            \
80         name ( param_type arg = param_type ()) : val_(arg) {}                      \
81         operator param_type () const {return val_;}                                         \
82     private:                                                                                \
83         param_type val_;                                                                    \
84     };                                                                                      \
85     static inline void PrintTo(name param, ::std::ostream* os)                              \
86     {                                                                                       \
87         *os << #name ": " << ::testing::PrintToString((name::param_type)(param));           \
88     }
89
90 struct MapStrStr {
91     std::map<std::string, std::string> data{};
92
93     explicit MapStrStr(std::map<std::string, std::string> _data) : data(std::move(_data)) {}
94
95     MapStrStr() = default;
96 };
97
98 void get_common_dims(const InferenceEngine::Blob &blob,
99                      int32_t &dimx,
100                      int32_t &dimy,
101                      int32_t &dimz);
102
103 void get_common_dims(const InferenceEngine::Blob &blob,
104                      int32_t &dimx,
105                      int32_t &dimy,
106                      int32_t &dimz,
107                      int32_t &dimn);
108
109 template<int Version = 3>
110 inline InferenceEngine::details::CNNNetworkImplPtr
111 buildSingleLayerNetworkCommon(InferenceEngine::details::IFormatParser *parser,
112                               const std::string &layerType,
113                               const testing::InOutShapes &inOutShapes,
114                               std::map<std::string, std::string> *params,
115                               const std::string &layerDataName = "data",
116                               const InferenceEngine::Precision &precision = InferenceEngine::Precision::FP32,
117                               size_t weightsSize = 0,
118                               size_t biasesSize = 0,
119                               const InferenceEngine::TBlob<uint8_t>::Ptr &weights = nullptr) {
120     IE_ASSERT(parser);
121     testing::XMLHelper xmlHelper(parser);
122     std::string precisionStr = precision.name();
123     auto netBuilder = testing::XmlNetBuilder<Version>::buildNetworkWithOneInput("Mock", inOutShapes.inDims[0],
124                                                                                 precisionStr);
125     size_t inputsNumber = inOutShapes.inDims.size();
126     for (int i = 1; i < inputsNumber; i++) {
127         netBuilder.addInputLayer(precisionStr, inOutShapes.inDims[i]);
128     }
129     netBuilder.addLayer(layerType, precisionStr, params, inOutShapes, weightsSize, biasesSize, layerDataName);
130     std::string testContent;
131     if (inputsNumber > 1) {
132         auto edgeBuilder = netBuilder.havingEdges();
133         for (size_t i = 0; i < inputsNumber; i++) {
134             edgeBuilder.connect(i, inputsNumber);
135         }
136         testContent = edgeBuilder.finish();
137     } else {
138         testContent = netBuilder.finish();
139     }
140     xmlHelper.loadContent(testContent);
141     auto result = xmlHelper.parseWithReturningNetwork();
142     if (weights) xmlHelper.setWeights(weights);
143     return result;
144 }
145
146 void GenRandomDataCommon(InferenceEngine::Blob::Ptr blob);
147
148 class BufferWrapper {
149     InferenceEngine::Precision precision;
150     InferenceEngine::ie_fp16 *fp16_ptr;
151     float *fp32_ptr;
152 public:
153     explicit BufferWrapper(const InferenceEngine::Blob::Ptr &blob);
154
155     BufferWrapper(const InferenceEngine::Blob::Ptr &blob, InferenceEngine::Precision precision);
156
157     float operator[](size_t index);
158
159     void insert(size_t index, float value);
160 };
161
162 void
163 CompareCommon(const InferenceEngine::Blob::Ptr &actual, const InferenceEngine::Blob::Ptr &expected, float tolerance);
164
165 void fill_data_common(BufferWrapper &data, size_t size, size_t duty_ratio = 10);