Revert "[IE TESTS] dynavic batch for mvn layer (#1010)" (#2257)
[platform/upstream/dldt.git] / inference-engine / tests / ie_test_utils / functional_test_utils / layer_test_utils.cpp
1 // Copyright (C) 2019-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <transformations/convert_batch_to_space.hpp>
6 #include <transformations/convert_space_to_batch.hpp>
7
8 #include "layer_test_utils.hpp"
9
10 namespace LayerTestsUtils {
11
12 LayerTestsCommon::LayerTestsCommon() : threshold(1e-2f) {
13     core = PluginCache::get().ie(targetDevice);
14 }
15
16 void LayerTestsCommon::Run() {
17     SKIP_IF_CURRENT_TEST_IS_DISABLED()
18
19     ConfigurePlugin();
20     LoadNetwork();
21     Infer();
22     Validate();
23 }
24
25 LayerTestsCommon::~LayerTestsCommon() {
26     if (!configuration.empty()) {
27         PluginCache::get().reset();
28     }
29 }
30
31 InferenceEngine::Blob::Ptr LayerTestsCommon::GenerateInput(const InferenceEngine::InputInfo &info) const {
32     return FuncTestUtils::createAndFillBlob(info.getTensorDesc());
33 }
34
35 void LayerTestsCommon::Compare(const std::vector<std::uint8_t> &expected, const InferenceEngine::Blob::Ptr &actual) {
36     ASSERT_EQ(expected.size(), actual->byteSize());
37     const auto &expectedBuffer = expected.data();
38
39     auto memory = InferenceEngine::as<InferenceEngine::MemoryBlob>(actual);
40     IE_ASSERT(memory);
41     const auto lockedMemory = memory->wmap();
42     const auto actualBuffer = lockedMemory.as<const std::uint8_t *>();
43
44     const auto &precision = actual->getTensorDesc().getPrecision();
45     const auto &size = actual->size();
46     switch (precision) {
47         case InferenceEngine::Precision::FP32:
48             Compare(reinterpret_cast<const float *>(expectedBuffer), reinterpret_cast<const float *>(actualBuffer),
49                     size, threshold);
50             break;
51         case InferenceEngine::Precision::I32:
52             Compare(reinterpret_cast<const std::int32_t *>(expectedBuffer),
53                     reinterpret_cast<const std::int32_t *>(actualBuffer), size, 0);
54             break;
55         default:
56             FAIL() << "Comparator for " << precision << " precision isn't supported";
57     }
58 }
59
60 void LayerTestsCommon::Compare(const InferenceEngine::Blob::Ptr &expected, const InferenceEngine::Blob::Ptr &actual) {
61     auto get_raw_buffer = [] (const InferenceEngine::Blob::Ptr &blob) {
62         auto memory = InferenceEngine::as<InferenceEngine::MemoryBlob>(blob);
63         IE_ASSERT(memory);
64         const auto lockedMemory = memory->wmap();
65         return lockedMemory.as<const std::uint8_t *>();
66     };
67     const auto expectedBuffer = get_raw_buffer(expected);
68     const auto actualBuffer = get_raw_buffer(actual);
69
70     const auto &precision = actual->getTensorDesc().getPrecision();
71     const auto &size = actual->size();
72     switch (precision) {
73         case InferenceEngine::Precision::FP32:
74             Compare(reinterpret_cast<const float *>(expectedBuffer), reinterpret_cast<const float *>(actualBuffer),
75                     size, threshold);
76             break;
77         case InferenceEngine::Precision::I32:
78             Compare(reinterpret_cast<const std::int32_t *>(expectedBuffer),
79                     reinterpret_cast<const std::int32_t *>(actualBuffer), size, 0);
80             break;
81         default:
82             FAIL() << "Comparator for " << precision << " precision isn't supported";
83     }
84 }
85
86 void LayerTestsCommon::ConfigurePlugin() {
87     if (!configuration.empty()) {
88         core->SetConfig(configuration, targetDevice);
89     }
90 }
91
92 void LayerTestsCommon::ConfigureNetwork() const {
93     for (const auto &in : cnnNetwork.getInputsInfo()) {
94         if (inLayout != InferenceEngine::Layout::ANY) {
95             in.second->setLayout(inLayout);
96         }
97         if (inPrc != InferenceEngine::Precision::UNSPECIFIED) {
98             in.second->setPrecision(inPrc);
99         }
100     }
101
102     for (const auto &out : cnnNetwork.getOutputsInfo()) {
103         if (outLayout != InferenceEngine::Layout::ANY) {
104             out.second->setLayout(outLayout);
105         }
106         if (outPrc != InferenceEngine::Precision::UNSPECIFIED) {
107             out.second->setPrecision(outPrc);
108         }
109     }
110 }
111
112 void LayerTestsCommon::LoadNetwork() {
113     cnnNetwork = InferenceEngine::CNNNetwork{function};
114     ConfigureNetwork();
115     executableNetwork = core->LoadNetwork(cnnNetwork, targetDevice);
116 }
117
118 void LayerTestsCommon::Infer() {
119     inferRequest = executableNetwork.CreateInferRequest();
120     inputs.clear();
121
122     for (const auto &input : executableNetwork.GetInputsInfo()) {
123         const auto &info = input.second;
124         auto blob = GenerateInput(*info);
125         inferRequest.SetBlob(info->name(), blob);
126         inputs.push_back(blob);
127     }
128     if (configuration.count(InferenceEngine::PluginConfigParams::KEY_DYN_BATCH_ENABLED) &&
129         configuration.count(InferenceEngine::PluginConfigParams::YES)) {
130         auto batchSize = executableNetwork.GetInputsInfo().begin()->second->getTensorDesc().getDims()[0] / 2;
131         inferRequest.SetBatch(batchSize);
132     }
133     inferRequest.Infer();
134 }
135
136 std::vector<std::vector<std::uint8_t>> LayerTestsCommon::CalculateRefs() {
137     // nGraph interpreter does not support f16
138     // IE converts f16 to f32
139     ngraph::pass::ConvertPrecision<ngraph::element::Type_t::f16, ngraph::element::Type_t::f32>().run_on_function(function);
140     function->validate_nodes_and_infer_types();
141     auto referenceInputs = std::vector<std::vector<std::uint8_t>>(inputs.size());
142     for (std::size_t i = 0; i < inputs.size(); ++i) {
143         const auto& input = inputs[i];
144         const auto& inputSize = input->byteSize();
145
146         auto& referenceInput = referenceInputs[i];
147         referenceInput.resize(inputSize);
148
149         auto memory = InferenceEngine::as<InferenceEngine::MemoryBlob>(input);
150         IE_ASSERT(memory);
151         const auto lockedMemory = memory->wmap();
152         const auto buffer = lockedMemory.as<const std::uint8_t*>();
153         std::copy(buffer, buffer + inputSize, referenceInput.data());
154     }
155
156     auto ieOutPrc = outPrc;
157     if (outPrc == InferenceEngine::Precision::UNSPECIFIED) {
158         const auto &actualOutputs = GetOutputs();
159         ieOutPrc = actualOutputs[0]->getTensorDesc().getPrecision();
160     }
161
162     const auto &convertType = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(ieOutPrc);
163     std::vector<std::vector<std::uint8_t>> expectedOutputs;
164     switch (refMode) {
165         case INTERPRETER: {
166             expectedOutputs = ngraph::helpers::interpreterFunction(function, referenceInputs, convertType);
167             break;
168         }
169         case CONSTANT_FOLDING: {
170             const auto &foldedFunc = ngraph::helpers::foldFunction(function, referenceInputs);
171             expectedOutputs = ngraph::helpers::getConstData(foldedFunc, convertType);
172             break;
173         }
174         case IE: {
175             // reference inference on device with other options and nGraph function has to be implemented here
176             break;
177         }
178         case INTERPRETER_TRANSFORMATIONS: {
179             auto cloned_function = ngraph::clone_function(*function);
180
181             // todo: add functionality to configure the necessary transformations for each test separately
182             ngraph::pass::Manager m;
183             m.register_pass<ngraph::pass::ConvertSpaceToBatch>();
184             m.register_pass<ngraph::pass::ConvertBatchToSpace>();
185             m.run_passes(cloned_function);
186             expectedOutputs = ngraph::helpers::interpreterFunction(cloned_function, referenceInputs, convertType);
187             break;
188         }
189     }
190
191     return expectedOutputs;
192 }
193
194 std::vector<InferenceEngine::Blob::Ptr> LayerTestsCommon::GetOutputs() {
195     auto outputs = std::vector<InferenceEngine::Blob::Ptr>{};
196     for (const auto &output : executableNetwork.GetOutputsInfo()) {
197         const auto &name = output.first;
198         outputs.push_back(inferRequest.GetBlob(name));
199     }
200     return outputs;
201 }
202
203 void LayerTestsCommon::Compare(const std::vector<std::vector<std::uint8_t>>& expectedOutputs, const std::vector<InferenceEngine::Blob::Ptr>& actualOutputs) {
204     for (std::size_t outputIndex = 0; outputIndex < expectedOutputs.size(); ++outputIndex) {
205         const auto& expected = expectedOutputs[outputIndex];
206         const auto& actual = actualOutputs[outputIndex];
207         Compare(expected, actual);
208     }
209 }
210
211 void LayerTestsCommon::Validate() {
212     auto expectedOutputs = CalculateRefs();
213     const auto& actualOutputs = GetOutputs();
214
215     if (expectedOutputs.empty()) {
216         return;
217     }
218
219     IE_ASSERT(actualOutputs.size() == expectedOutputs.size())
220         << "nGraph interpreter has " << expectedOutputs.size() << " outputs, while IE " << actualOutputs.size();
221
222     Compare(expectedOutputs, actualOutputs);
223 }
224
225 void LayerTestsCommon::SetRefMode(RefMode mode) {
226     refMode = mode;
227 }
228 }  // namespace LayerTestsUtils