1 // Copyright (C) 2019-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
5 #include <transformations/convert_batch_to_space.hpp>
6 #include <transformations/convert_space_to_batch.hpp>
8 #include "layer_test_utils.hpp"
10 namespace LayerTestsUtils {
12 LayerTestsCommon::LayerTestsCommon() : threshold(1e-2f) {
13 core = PluginCache::get().ie(targetDevice);
16 void LayerTestsCommon::Run() {
17 SKIP_IF_CURRENT_TEST_IS_DISABLED()
25 LayerTestsCommon::~LayerTestsCommon() {
26 if (!configuration.empty()) {
27 PluginCache::get().reset();
31 InferenceEngine::Blob::Ptr LayerTestsCommon::GenerateInput(const InferenceEngine::InputInfo &info) const {
32 return FuncTestUtils::createAndFillBlob(info.getTensorDesc());
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();
39 auto memory = InferenceEngine::as<InferenceEngine::MemoryBlob>(actual);
41 const auto lockedMemory = memory->wmap();
42 const auto actualBuffer = lockedMemory.as<const std::uint8_t *>();
44 const auto &precision = actual->getTensorDesc().getPrecision();
45 const auto &size = actual->size();
47 case InferenceEngine::Precision::FP32:
48 Compare(reinterpret_cast<const float *>(expectedBuffer), reinterpret_cast<const float *>(actualBuffer),
51 case InferenceEngine::Precision::I32:
52 Compare(reinterpret_cast<const std::int32_t *>(expectedBuffer),
53 reinterpret_cast<const std::int32_t *>(actualBuffer), size, 0);
56 FAIL() << "Comparator for " << precision << " precision isn't supported";
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);
64 const auto lockedMemory = memory->wmap();
65 return lockedMemory.as<const std::uint8_t *>();
67 const auto expectedBuffer = get_raw_buffer(expected);
68 const auto actualBuffer = get_raw_buffer(actual);
70 const auto &precision = actual->getTensorDesc().getPrecision();
71 const auto &size = actual->size();
73 case InferenceEngine::Precision::FP32:
74 Compare(reinterpret_cast<const float *>(expectedBuffer), reinterpret_cast<const float *>(actualBuffer),
77 case InferenceEngine::Precision::I32:
78 Compare(reinterpret_cast<const std::int32_t *>(expectedBuffer),
79 reinterpret_cast<const std::int32_t *>(actualBuffer), size, 0);
82 FAIL() << "Comparator for " << precision << " precision isn't supported";
86 void LayerTestsCommon::ConfigurePlugin() {
87 if (!configuration.empty()) {
88 core->SetConfig(configuration, targetDevice);
92 void LayerTestsCommon::ConfigureNetwork() const {
93 for (const auto &in : cnnNetwork.getInputsInfo()) {
94 if (inLayout != InferenceEngine::Layout::ANY) {
95 in.second->setLayout(inLayout);
97 if (inPrc != InferenceEngine::Precision::UNSPECIFIED) {
98 in.second->setPrecision(inPrc);
102 for (const auto &out : cnnNetwork.getOutputsInfo()) {
103 if (outLayout != InferenceEngine::Layout::ANY) {
104 out.second->setLayout(outLayout);
106 if (outPrc != InferenceEngine::Precision::UNSPECIFIED) {
107 out.second->setPrecision(outPrc);
112 void LayerTestsCommon::LoadNetwork() {
113 cnnNetwork = InferenceEngine::CNNNetwork{function};
115 executableNetwork = core->LoadNetwork(cnnNetwork, targetDevice);
118 void LayerTestsCommon::Infer() {
119 inferRequest = executableNetwork.CreateInferRequest();
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);
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);
133 inferRequest.Infer();
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();
146 auto& referenceInput = referenceInputs[i];
147 referenceInput.resize(inputSize);
149 auto memory = InferenceEngine::as<InferenceEngine::MemoryBlob>(input);
151 const auto lockedMemory = memory->wmap();
152 const auto buffer = lockedMemory.as<const std::uint8_t*>();
153 std::copy(buffer, buffer + inputSize, referenceInput.data());
156 auto ieOutPrc = outPrc;
157 if (outPrc == InferenceEngine::Precision::UNSPECIFIED) {
158 const auto &actualOutputs = GetOutputs();
159 ieOutPrc = actualOutputs[0]->getTensorDesc().getPrecision();
162 const auto &convertType = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(ieOutPrc);
163 std::vector<std::vector<std::uint8_t>> expectedOutputs;
166 expectedOutputs = ngraph::helpers::interpreterFunction(function, referenceInputs, convertType);
169 case CONSTANT_FOLDING: {
170 const auto &foldedFunc = ngraph::helpers::foldFunction(function, referenceInputs);
171 expectedOutputs = ngraph::helpers::getConstData(foldedFunc, convertType);
175 // reference inference on device with other options and nGraph function has to be implemented here
178 case INTERPRETER_TRANSFORMATIONS: {
179 auto cloned_function = ngraph::clone_function(*function);
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);
191 return expectedOutputs;
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));
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);
211 void LayerTestsCommon::Validate() {
212 auto expectedOutputs = CalculateRefs();
213 const auto& actualOutputs = GetOutputs();
215 if (expectedOutputs.empty()) {
219 IE_ASSERT(actualOutputs.size() == expectedOutputs.size())
220 << "nGraph interpreter has " << expectedOutputs.size() << " outputs, while IE " << actualOutputs.size();
222 Compare(expectedOutputs, actualOutputs);
225 void LayerTestsCommon::SetRefMode(RefMode mode) {
228 } // namespace LayerTestsUtils