Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / shape_infer / adult_test_utils.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <gtest/gtest.h>
6 #include <tuple>
7 #include "adult_test.hpp"
8 #include "adult_test_utils.hpp"
9
10
11 using namespace InferenceEngine;
12 using namespace details;
13 using namespace ShapeInfer;
14
15 void BaseMatcher::compareWithRef(const std::vector<InferenceEngine::Blob::Ptr>& outBlobs,
16                                  const std::vector<std::vector<float>>& refData,
17                                  float tolerance) {
18     for (int outIdx = 0; outIdx < outBlobs.size(); outIdx++) {
19         auto* data = outBlobs[outIdx]->buffer().as<float*>();
20         for (int elemIdx = 0; elemIdx < refData[outIdx].size(); elemIdx++) {
21             ASSERT_NEAR(data[elemIdx], refData[outIdx][elemIdx], tolerance);
22         }
23     }
24 }
25
26 std::vector<IE::Blob::Ptr>
27 BaseMatcher::createBlobs(const std::vector<IE::SizeVector>& shapes, const std::vector<IE::Precision>& precisions) {
28     if (shapes.size() != precisions.size())
29         THROW_IE_EXCEPTION << "Vectors of shapes and precisions can't have different sizes";
30     std::vector<Blob::Ptr> blobs;
31     int i = 0;
32     for (const auto& dims : shapes) {
33         // it's assumed that empty dims = empty data = no blob
34         if (!dims.empty()) {
35             TensorDesc inDesc(precisions[i++], dims, TensorDesc::getLayoutByDims(dims));
36             auto blob = make_blob_with_precision(inDesc);
37             blob->allocate();
38             blobs.push_back(blob);
39         }
40     }
41     return blobs;
42 }
43
44 void BaseMatcher::fillBlobs(const std::vector<IE::Blob::Ptr>& blobs, const std::vector<std::vector<float>>& data) {
45     if (!data.empty()) {
46         for (int blobIdx = 0; blobIdx < blobs.size(); blobIdx++) {
47             auto blob = blobs[blobIdx];
48             // it's assumed that empty dims = empty data = no blob
49             if (!data[blobIdx].empty()) {
50                 switch (blob->precision()) {
51                     case Precision::FP32: {
52                         auto* buffer = blob->buffer().as<float*>();
53                         for (int dataIdx = 0; dataIdx < blob->size(); dataIdx++) {
54                             buffer[dataIdx] = data[blobIdx][dataIdx];
55                         }
56                     }
57                         break;
58                     case Precision::I32: {
59                         auto* buffer = blob->buffer().as<int32_t*>();
60                         for (int dataIdx = 0; dataIdx < blob->size(); dataIdx++) {
61                             buffer[dataIdx] = static_cast<int32_t>(data[blobIdx][dataIdx]);
62                         }
63                     }
64                         break;
65                     default:
66                         THROW_IE_EXCEPTION << "Unsupported precision " << blob->precision() << " to fill blobs";
67                 }
68             }
69         }
70     }
71 }
72
73 void ConstInferMatcher::toData(const std::vector<std::vector<float>>& refData) {
74     auto impl = holder->getConstInferImpl(config.type);
75     ASSERT_NE(nullptr, impl);
76     auto outBlobs = createBlobs(config.inOutData.inOutShapes.outDims, config.outPrecisions);
77     auto inBlobs = createBlobs(config.inOutData.inOutShapes.inDims, config.inPrecisions);
78     fillBlobs(inBlobs, config.inOutData.inData);
79     auto blobs = config.initBlobs(config.floatBlobData);
80     std::vector<Blob::CPtr> inCBlobs;
81     std::copy(inBlobs.begin(), inBlobs.end(), back_inserter(inCBlobs));
82     ASSERT_NO_THROW(impl->infer(inCBlobs, config.strParams, blobs, outBlobs));
83     compareWithRef(outBlobs, refData);
84 }
85
86 void ShapeInferMatcher::toShapes(const std::vector<IE::SizeVector>& refShape) {
87     siHolder.reset(new IE::ShapeInfer::BuiltInShapeInferHolder());
88     IE::IShapeInferImpl::Ptr impl;
89     std::vector<IE::SizeVector> outShapes;
90     sts = siHolder->getShapeInferImpl(impl, config.type.c_str(), &desc);
91     ASSERT_NE(nullptr, impl);
92     auto inBlobs = createBlobs(config.inOutData.inOutShapes.inDims, config.inPrecisions);
93     fillBlobs(inBlobs, config.inOutData.inData);
94     std::vector<Blob::CPtr> inCBlobs;
95     std::copy(inBlobs.begin(), inBlobs.end(), back_inserter(inCBlobs));
96     auto blobs = config.initBlobs(config.floatBlobData);
97     sts = impl->inferShapes(inCBlobs, config.strParams, blobs, outShapes, &desc);
98     ASSERT_EQ(sts, IE::OK) << desc.msg;
99     ASSERT_EQ(config.inOutData.inOutShapes.outDims, outShapes);
100 }
101
102 InitBlobsFunc ASITestBuilder::defaultBlobInit() {
103     return [](const FloatMap& blobDataMap) -> BlobMap {
104         BlobMap blobs;
105         for (const auto& it : blobDataMap) {
106             std::string blobName;
107             std::vector<float> data;
108             std::tie(blobName, data) = it;
109             SizeVector blobDims = {data.size()};
110             auto blob = make_shared_blob<float>(Precision::FP32, TensorDesc::getLayoutByDims(blobDims), blobDims,
111                                                 data);
112             blobs[blobName] = blob;
113         }
114         return blobs;
115     };
116 }
117
118 MatcherConfigurator<ConstInferMatcher> ASITestBuilder::constInferResultFor() {
119     return MatcherConfigurator<ConstInferMatcher>(config);
120 }
121
122 MatcherConfigurator<ShapeInferMatcher> ASITestBuilder::shapeInferResultFor() {
123     return MatcherConfigurator<ShapeInferMatcher>(config);
124 }