Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / engines / gna / configuration_test.cpp
1 //
2 // Copyright 2016-2018 Intel Corporation.
3 //
4 // This software and the related documents are Intel copyrighted materials,
5 // and your use of them is governed by the express license under which they
6 // were provided to you (End User License Agreement for the Intel(R) Software
7 // Development Products (Version May 2017)). Unless the License provides
8 // otherwise, you may not use, modify, copy, publish, distribute, disclose or
9 // transmit this software or the related documents without Intel's prior
10 // written permission.
11 //
12 // This software and the related documents are provided as is, with no
13 // express or implied warranties, other than those that are expressly
14 // stated in the License.
15 //
16
17 #include <vector>
18
19 #include <gtest/gtest.h>
20 #include <mock_icnn_network.hpp>
21 #include "gna_plugin/gna_plugin_config.hpp"
22 #include "gna_matcher.hpp"
23 #include "test_irs.hpp"
24
25 using namespace InferenceEngine;
26 using namespace GNAPluginNS;
27 using namespace ::testing;
28
29 class GNAConfigTest : public GNATest {
30
31  protected:
32     MockICNNNetwork net;
33
34     void SetUp() override  {
35     }
36 };
37
38 TEST_F(GNAConfigTest, reportAnErrorIfConfigNotFound) {
39
40     Config c ({{TargetDevice :: eGNA, Precision::I16},
41                {TargetDevice :: eCPU, Precision::FP32}});
42
43     EXPECT_CALL(net, getPrecision()).WillRepeatedly(Return(Precision::FP32));
44     EXPECT_CALL(net, getTargetDevice()).WillRepeatedly(Return(TargetDevice::eGNA));
45
46     ASSERT_ANY_THROW(c.find_configuration(net));
47 }
48
49 TEST_F(GNAConfigTest, canFindConfiguration) {
50
51     Config c ({{TargetDevice :: eGNA, Precision::I16},
52                {TargetDevice :: eCPU, Precision::FP32}});
53
54     EXPECT_CALL(net, getPrecision()).WillRepeatedly(Return(Precision::FP32));
55     EXPECT_CALL(net, getTargetDevice()).WillRepeatedly(Return(TargetDevice::eCPU));
56
57     auto match = c.find_configuration(net);
58
59     EXPECT_EQ(match.device, TargetDevice::eCPU);
60     EXPECT_EQ(match.networkPrec, Precision::FP32);
61 }
62
63 TEST_F(GNAConfigTest, canPassTroughNetworkAfterFindConfiguration) {
64
65     Config c ({{TargetDevice :: eGNA, Precision::I16},
66                {TargetDevice :: eCPU, Precision::FP32}});
67
68     EXPECT_CALL(net, getPrecision()).WillRepeatedly(Return(Precision::FP32));
69     EXPECT_CALL(net, getTargetDevice()).WillRepeatedly(Return(TargetDevice::eCPU));
70
71     auto match = c.find_configuration(net);
72
73     auto net2 = match.convert(net);
74
75     EXPECT_EQ(net2->getTargetDevice(), TargetDevice::eCPU);
76     EXPECT_EQ(net2->getPrecision(), Precision::FP32);
77 }
78
79 TEST_F(GNAConfigTest, canNotMatchWithDefaultDevice) {
80
81     Config c ({{TargetDevice :: eGNA, Precision::I16},
82                {TargetDevice :: eCPU, Precision::FP32}});
83
84     c.setDefaultDevice(TargetDevice::eGNA);
85
86     EXPECT_CALL(net, getPrecision()).WillRepeatedly(Return(Precision::FP32));
87     EXPECT_CALL(net, getTargetDevice()).WillRepeatedly(Return(TargetDevice::eDefault));
88
89     EXPECT_ANY_THROW(c.find_configuration(net).convert(net));
90 }
91
92 TEST_F(GNAConfigTest, canMatchWithDefaultDevice) {
93
94     Config c ({{TargetDevice :: eGNA, Precision::I16},
95                {TargetDevice :: eCPU, Precision::FP32}});
96
97     c.setDefaultDevice(TargetDevice::eGNA);
98
99     EXPECT_CALL(net, getPrecision()).WillRepeatedly(Return(Precision::I16));
100     EXPECT_CALL(net, getTargetDevice()).WillRepeatedly(Return(TargetDevice::eDefault));
101
102     auto net2 = c.find_configuration(net).convert(net);
103
104     EXPECT_EQ(net2->getTargetDevice(), TargetDevice::eDefault);
105     EXPECT_EQ(net2->getPrecision(), Precision::I16);
106 }
107
108 TEST_F(GNAConfigTest, canMatchWith1AsyncThread) {
109     assert_that()
110         .onInferModel(GNATestIRs::Fc2DOutputModel())
111         .inNotCompactMode()
112         .withAcceleratorThreadsNumber("1")
113         .gna().propagate_forward().called_without().pwl_inserted_into_nnet();
114 }
115
116 TEST_F(GNAConfigTest, canMatchWith4AsyncThreads) {
117     assert_that()
118         .onInferModel(GNATestIRs::Fc2DOutputModel())
119         .inNotCompactMode()
120         .withAcceleratorThreadsNumber("4")
121         .gna().propagate_forward().called_without().pwl_inserted_into_nnet();
122 }
123
124 TEST_F(GNAConfigTest, canNOTMatchWith0AsyncThreads) {
125     assert_that()
126         .onInferModel(GNATestIRs::Fc2DOutputModel())
127         .inNotCompactMode()
128         .withAcceleratorThreadsNumber("0")
129         .gna().propagate_forward().called_without().pwl_inserted_into_nnet()
130         .throws();
131 }
132
133 TEST_F(GNAConfigTest, canNOTMatchWith128AsyncThreads) {
134     assert_that()
135         .onInferModel(GNATestIRs::Fc2DOutputModel())
136         .inNotCompactMode()
137         .withAcceleratorThreadsNumber("128")
138         .gna().propagate_forward().called_without().pwl_inserted_into_nnet()
139         .throws();
140 }
141
142 TEST_F(GNAConfigTest, canMatchWithSingleMultipleOMPThreads) {
143     assert_that()
144         .onInferModel(GNATestIRs::Fc2DOutputModel())
145         .inNotCompactMode()
146         .enable_omp_multithreading()
147         .gna().propagate_forward().called_without().pwl_inserted_into_nnet();
148 }
149
150 TEST_F(GNAConfigTest, failToCreatePluginWithDifferentInputScaleFactors) {
151     assert_that().creating().gna_plugin()
152         .withGNAConfig(std::string(GNA_CONFIG_KEY(SCALE_FACTOR))+"_1", 1000)
153         .withGNAConfig(std::string(GNA_CONFIG_KEY(SCALE_FACTOR))+"_2", 2000).throws();
154 }