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 auto bufferSize = actual->size();
46 // With dynamic batch, you need to size
47 if (configuration.count(InferenceEngine::PluginConfigParams::KEY_DYN_BATCH_ENABLED)) {
48 auto batchSize = actual->getTensorDesc().getDims()[0];
49 auto halfBatchSize = batchSize > 1 ? batchSize/ 2 : 1;
50 bufferSize = (actual->size() * halfBatchSize / batchSize);
52 const auto &size = bufferSize;
54 case InferenceEngine::Precision::FP32:
55 Compare(reinterpret_cast<const float *>(expectedBuffer), reinterpret_cast<const float *>(actualBuffer),
58 case InferenceEngine::Precision::I32:
59 Compare(reinterpret_cast<const std::int32_t *>(expectedBuffer),
60 reinterpret_cast<const std::int32_t *>(actualBuffer), size, 0);
63 FAIL() << "Comparator for " << precision << " precision isn't supported";
67 void LayerTestsCommon::ConfigurePlugin() {
68 if (!configuration.empty()) {
69 core->SetConfig(configuration, targetDevice);
73 void LayerTestsCommon::ConfigureNetwork() const {
74 for (const auto &in : cnnNetwork.getInputsInfo()) {
75 if (inLayout != InferenceEngine::Layout::ANY) {
76 in.second->setLayout(inLayout);
78 if (inPrc != InferenceEngine::Precision::UNSPECIFIED) {
79 in.second->setPrecision(inPrc);
83 for (const auto &out : cnnNetwork.getOutputsInfo()) {
84 if (outLayout != InferenceEngine::Layout::ANY) {
85 out.second->setLayout(outLayout);
87 if (outPrc != InferenceEngine::Precision::UNSPECIFIED) {
88 out.second->setPrecision(outPrc);
93 void LayerTestsCommon::LoadNetwork() {
94 cnnNetwork = InferenceEngine::CNNNetwork{function};
96 executableNetwork = core->LoadNetwork(cnnNetwork, targetDevice);
99 void LayerTestsCommon::Infer() {
100 inferRequest = executableNetwork.CreateInferRequest();
103 for (const auto &input : cnnNetwork.getInputsInfo()) {
104 const auto &info = input.second;
105 auto blob = GenerateInput(*info);
106 inferRequest.SetBlob(info->name(), blob);
107 inputs.push_back(blob);
109 if (configuration.count(InferenceEngine::PluginConfigParams::KEY_DYN_BATCH_ENABLED) &&
110 configuration.count(InferenceEngine::PluginConfigParams::YES)) {
111 auto batchSize = cnnNetwork.getInputsInfo().begin()->second->getTensorDesc().getDims()[0] / 2;
112 inferRequest.SetBatch(batchSize);
114 inferRequest.Infer();
117 std::vector<std::vector<std::uint8_t>> LayerTestsCommon::CalculateRefs() {
118 // nGraph interpreter does not support f16
119 // IE converts f16 to f32
120 ngraph::pass::ConvertPrecision<ngraph::element::Type_t::f16, ngraph::element::Type_t::f32>().run_on_function(function);
121 function->validate_nodes_and_infer_types();
122 auto referenceInputs = std::vector<std::vector<std::uint8_t>>(inputs.size());
123 for (std::size_t i = 0; i < inputs.size(); ++i) {
124 const auto& input = inputs[i];
125 const auto& inputSize = input->byteSize();
127 auto& referenceInput = referenceInputs[i];
128 referenceInput.resize(inputSize);
130 auto memory = InferenceEngine::as<InferenceEngine::MemoryBlob>(input);
132 const auto lockedMemory = memory->wmap();
133 const auto buffer = lockedMemory.as<const std::uint8_t*>();
134 std::copy(buffer, buffer + inputSize, referenceInput.data());
137 const auto &actualOutputs = GetOutputs();
138 const auto &convertType = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(actualOutputs[0]->getTensorDesc().getPrecision());
139 std::vector<std::vector<std::uint8_t>> expectedOutputs;
142 expectedOutputs = ngraph::helpers::interpreterFunction(function, referenceInputs, convertType);
145 case CONSTANT_FOLDING: {
146 const auto &foldedFunc = ngraph::helpers::foldFunction(function, referenceInputs);
147 expectedOutputs = ngraph::helpers::getConstData(foldedFunc, convertType);
151 // reference inference on device with other options and nGraph function has to be implemented here
154 case INTERPRETER_TRANSFORMATIONS: {
155 auto cloned_function = ngraph::clone_function(*function);
157 // todo: add functionality to configure the necessary transformations for each test separately
158 ngraph::pass::Manager m;
159 m.register_pass<ngraph::pass::ConvertSpaceToBatch>();
160 m.register_pass<ngraph::pass::ConvertBatchToSpace>();
161 m.run_passes(cloned_function);
162 expectedOutputs = ngraph::helpers::interpreterFunction(cloned_function, referenceInputs, convertType);
167 return expectedOutputs;
170 std::vector<InferenceEngine::Blob::Ptr> LayerTestsCommon::GetOutputs() {
171 auto outputs = std::vector<InferenceEngine::Blob::Ptr>{};
172 for (const auto &output : cnnNetwork.getOutputsInfo()) {
173 const auto &name = output.first;
174 outputs.push_back(inferRequest.GetBlob(name));
179 void LayerTestsCommon::Compare(const std::vector<std::vector<std::uint8_t>>& expectedOutputs, const std::vector<InferenceEngine::Blob::Ptr>& actualOutputs) {
180 for (std::size_t outputIndex = 0; outputIndex < expectedOutputs.size(); ++outputIndex) {
181 const auto& expected = expectedOutputs[outputIndex];
182 const auto& actual = actualOutputs[outputIndex];
183 Compare(expected, actual);
187 void LayerTestsCommon::Validate() {
188 auto expectedOutputs = CalculateRefs();
189 const auto& actualOutputs = GetOutputs();
191 if (expectedOutputs.empty()) {
195 IE_ASSERT(actualOutputs.size() == expectedOutputs.size())
196 << "nGraph interpreter has " << expectedOutputs.size() << " outputs, while IE " << actualOutputs.size();
198 Compare(expectedOutputs, actualOutputs);
201 void LayerTestsCommon::SetRefMode(RefMode mode) {
204 } // namespace LayerTestsUtils