blob: 3b11b726f2a56b0809b0c74eab2d3b85347ff74c [file] [log] [blame]
surmeh0149b9e102018-05-17 14:11:25 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
surmeh0149b9e102018-05-17 14:11:25 +01004//
5#include "DriverTestHelpers.hpp"
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +01006
7#include "../1.0/HalPolicy.hpp"
8
surmeh0149b9e102018-05-17 14:11:25 +01009#include <boost/test/unit_test.hpp>
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010010
surmeh0149b9e102018-05-17 14:11:25 +010011#include <log/log.h>
12
13BOOST_AUTO_TEST_SUITE(GenericLayerTests)
14
telsoa01ce3e84a2018-08-31 09:31:35 +010015using namespace android::hardware;
surmeh0149b9e102018-05-17 14:11:25 +010016using namespace driverTestHelpers;
telsoa01ce3e84a2018-08-31 09:31:35 +010017using namespace armnn_driver;
surmeh0149b9e102018-05-17 14:11:25 +010018
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010019using HalPolicy = hal_1_0::HalPolicy;
20
surmeh0149b9e102018-05-17 14:11:25 +010021BOOST_AUTO_TEST_CASE(GetSupportedOperations)
22{
23 auto driver = std::make_unique<ArmnnDriver>(DriverOptions(armnn::Compute::CpuRef));
24
telsoa01ce3e84a2018-08-31 09:31:35 +010025 ErrorStatus errorStatus;
26 std::vector<bool> supported;
surmeh0149b9e102018-05-17 14:11:25 +010027
telsoa01ce3e84a2018-08-31 09:31:35 +010028 auto cb = [&](ErrorStatus _errorStatus, const std::vector<bool>& _supported)
surmeh0149b9e102018-05-17 14:11:25 +010029 {
telsoa01ce3e84a2018-08-31 09:31:35 +010030 errorStatus = _errorStatus;
31 supported = _supported;
surmeh0149b9e102018-05-17 14:11:25 +010032 };
33
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010034 HalPolicy::Model model0 = {};
surmeh0149b9e102018-05-17 14:11:25 +010035
telsoa01ce3e84a2018-08-31 09:31:35 +010036 // Add operands
surmeh0149b9e102018-05-17 14:11:25 +010037 int32_t actValue = 0;
38 float weightValue[] = {2, 4, 1};
39 float biasValue[] = {4};
40
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010041 AddInputOperand<HalPolicy>(model0, hidl_vec<uint32_t>{1, 3});
42 AddTensorOperand<HalPolicy>(model0, hidl_vec<uint32_t>{1, 3}, weightValue);
43 AddTensorOperand<HalPolicy>(model0, hidl_vec<uint32_t>{1}, biasValue);
44 AddIntOperand<HalPolicy>(model0, actValue);
45 AddOutputOperand<HalPolicy>(model0, hidl_vec<uint32_t>{1, 1});
telsoa01ce3e84a2018-08-31 09:31:35 +010046
47 model0.operations.resize(1);
48
49 // Make a correct fully connected operation
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010050 model0.operations[0].type = HalPolicy::OperationType::FULLY_CONNECTED;
telsoa01ce3e84a2018-08-31 09:31:35 +010051 model0.operations[0].inputs = hidl_vec<uint32_t>{0, 1, 2, 3};
52 model0.operations[0].outputs = hidl_vec<uint32_t>{4};
53
54 driver->getSupportedOperations(model0, cb);
55 BOOST_TEST((int)errorStatus == (int)ErrorStatus::NONE);
56 BOOST_TEST(supported.size() == (size_t)1);
57 BOOST_TEST(supported[0] == true);
58
Matteo Martincigh8b287c22018-09-07 09:25:10 +010059 V1_0::Model model1 = {};
telsoa01ce3e84a2018-08-31 09:31:35 +010060
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010061 AddInputOperand<HalPolicy>(model1, hidl_vec<uint32_t>{1, 3});
62 AddTensorOperand<HalPolicy>(model1, hidl_vec<uint32_t>{1, 3}, weightValue);
63 AddTensorOperand<HalPolicy>(model1, hidl_vec<uint32_t>{1}, biasValue);
64 AddIntOperand<HalPolicy>(model1, actValue);
65 AddOutputOperand<HalPolicy>(model1, hidl_vec<uint32_t>{1, 1});
surmeh0149b9e102018-05-17 14:11:25 +010066
surmeh0149b9e102018-05-17 14:11:25 +010067 model1.operations.resize(2);
telsoa01ce3e84a2018-08-31 09:31:35 +010068
69 // Make a correct fully connected operation
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010070 model1.operations[0].type = HalPolicy::OperationType::FULLY_CONNECTED;
surmeh0149b9e102018-05-17 14:11:25 +010071 model1.operations[0].inputs = hidl_vec<uint32_t>{0, 1, 2, 3};
72 model1.operations[0].outputs = hidl_vec<uint32_t>{4};
73
telsoa01ce3e84a2018-08-31 09:31:35 +010074 // Add an incorrect fully connected operation
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010075 AddIntOperand<HalPolicy>(model1, actValue);
76 AddOutputOperand<HalPolicy>(model1, hidl_vec<uint32_t>{1, 1});
77
78 model1.operations[1].type = HalPolicy::OperationType::FULLY_CONNECTED;
telsoa01ce3e84a2018-08-31 09:31:35 +010079 model1.operations[1].inputs = hidl_vec<uint32_t>{4}; // Only 1 input operand, expected 4
surmeh0149b9e102018-05-17 14:11:25 +010080 model1.operations[1].outputs = hidl_vec<uint32_t>{5};
81
82 driver->getSupportedOperations(model1, cb);
surmeh0149b9e102018-05-17 14:11:25 +010083
Mike Kellyb5fdf382019-06-11 16:35:25 +010084#if defined(ARMNN_ANDROID_P) || defined(ARMNN_ANDROID_Q)
telsoa01ce3e84a2018-08-31 09:31:35 +010085 // In Android P, android::nn::validateModel returns INVALID_ARGUMENT, because of the wrong number of inputs for the
86 // fully connected layer (1 instead of 4)
87 BOOST_TEST((int)errorStatus == (int)ErrorStatus::INVALID_ARGUMENT);
88 BOOST_TEST(supported.empty());
89#else
90 // In Android O, android::nn::validateModel indicates that the second (wrong) fully connected layer in unsupported
91 // in the vector of flags returned by the callback
92 BOOST_TEST((int)errorStatus == (int)ErrorStatus::NONE);
93 BOOST_TEST(supported.size() == (size_t)2);
94 BOOST_TEST(supported[0] == true);
95 BOOST_TEST(supported[1] == false);
96#endif
surmeh0149b9e102018-05-17 14:11:25 +010097
telsoa01ce3e84a2018-08-31 09:31:35 +010098 // Test Broadcast on add/mul operators
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +010099 HalPolicy::Model model2 = {};
telsoa01ce3e84a2018-08-31 09:31:35 +0100100
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100101 AddInputOperand<HalPolicy>(model2, hidl_vec<uint32_t>{1, 1, 3, 4});
102 AddInputOperand<HalPolicy>(model2, hidl_vec<uint32_t>{4});
103 AddIntOperand<HalPolicy>(model2, actValue);
104 AddOutputOperand<HalPolicy>(model2, hidl_vec<uint32_t>{1, 1, 3, 4});
105 AddOutputOperand<HalPolicy>(model2, hidl_vec<uint32_t>{1, 1, 3, 4});
surmeh0149b9e102018-05-17 14:11:25 +0100106
107 model2.operations.resize(2);
108
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100109 model2.operations[0].type = HalPolicy::OperationType::ADD;
telsoa01ce3e84a2018-08-31 09:31:35 +0100110 model2.operations[0].inputs = hidl_vec<uint32_t>{0, 1, 2};
111 model2.operations[0].outputs = hidl_vec<uint32_t>{3};
surmeh0149b9e102018-05-17 14:11:25 +0100112
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100113 model2.operations[1].type = HalPolicy::OperationType::MUL;
telsoa01ce3e84a2018-08-31 09:31:35 +0100114 model2.operations[1].inputs = hidl_vec<uint32_t>{0, 1, 2};
115 model2.operations[1].outputs = hidl_vec<uint32_t>{4};
surmeh0149b9e102018-05-17 14:11:25 +0100116
117 driver->getSupportedOperations(model2, cb);
telsoa01ce3e84a2018-08-31 09:31:35 +0100118 BOOST_TEST((int)errorStatus == (int)ErrorStatus::NONE);
119 BOOST_TEST(supported.size() == (size_t)2);
120 BOOST_TEST(supported[0] == true);
121 BOOST_TEST(supported[1] == true);
surmeh0149b9e102018-05-17 14:11:25 +0100122
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100123 V1_0::Model model3 = {};
telsoa01ce3e84a2018-08-31 09:31:35 +0100124
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100125 AddInputOperand<HalPolicy>(model3, hidl_vec<uint32_t>{1, 1, 1, 8});
126 AddIntOperand<HalPolicy>(model3, 2);
127 AddOutputOperand<HalPolicy>(model3, hidl_vec<uint32_t>{1, 2, 2, 2});
telsoa01ce3e84a2018-08-31 09:31:35 +0100128
129 model3.operations.resize(1);
surmeh0149b9e102018-05-17 14:11:25 +0100130
131 // Add unsupported operation, should return no error but we don't support it
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100132 model3.operations[0].type = HalPolicy::OperationType::DEPTH_TO_SPACE;
telsoa01ce3e84a2018-08-31 09:31:35 +0100133 model3.operations[0].inputs = hidl_vec<uint32_t>{0, 1};
surmeh0149b9e102018-05-17 14:11:25 +0100134 model3.operations[0].outputs = hidl_vec<uint32_t>{2};
135
136 driver->getSupportedOperations(model3, cb);
telsoa01ce3e84a2018-08-31 09:31:35 +0100137 BOOST_TEST((int)errorStatus == (int)ErrorStatus::NONE);
138 BOOST_TEST(supported.size() == (size_t)1);
139 BOOST_TEST(supported[0] == false);
140
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100141 HalPolicy::Model model4 = {};
telsoa01ce3e84a2018-08-31 09:31:35 +0100142
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100143 AddIntOperand<HalPolicy>(model4, 0);
telsoa01ce3e84a2018-08-31 09:31:35 +0100144
145 model4.operations.resize(1);
surmeh0149b9e102018-05-17 14:11:25 +0100146
147 // Add invalid operation
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100148 model4.operations[0].type = static_cast<HalPolicy::OperationType>(100);
surmeh0149b9e102018-05-17 14:11:25 +0100149 model4.operations[0].outputs = hidl_vec<uint32_t>{0};
150
151 driver->getSupportedOperations(model4, cb);
telsoa01ce3e84a2018-08-31 09:31:35 +0100152 BOOST_TEST((int)errorStatus == (int)ErrorStatus::INVALID_ARGUMENT);
153 BOOST_TEST(supported.empty());
surmeh0149b9e102018-05-17 14:11:25 +0100154}
155
156// The purpose of this test is to ensure that when encountering an unsupported operation
telsoa01ce3e84a2018-08-31 09:31:35 +0100157// it is skipped and getSupportedOperations() continues (rather than failing and stopping).
158// As per IVGCVSW-710.
surmeh0149b9e102018-05-17 14:11:25 +0100159BOOST_AUTO_TEST_CASE(UnsupportedLayerContinueOnFailure)
160{
161 auto driver = std::make_unique<ArmnnDriver>(DriverOptions(armnn::Compute::CpuRef));
162
telsoa01ce3e84a2018-08-31 09:31:35 +0100163 ErrorStatus errorStatus;
164 std::vector<bool> supported;
surmeh0149b9e102018-05-17 14:11:25 +0100165
telsoa01ce3e84a2018-08-31 09:31:35 +0100166 auto cb = [&](ErrorStatus _errorStatus, const std::vector<bool>& _supported)
surmeh0149b9e102018-05-17 14:11:25 +0100167 {
telsoa01ce3e84a2018-08-31 09:31:35 +0100168 errorStatus = _errorStatus;
169 supported = _supported;
surmeh0149b9e102018-05-17 14:11:25 +0100170 };
171
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100172 HalPolicy::Model model = {};
surmeh0149b9e102018-05-17 14:11:25 +0100173
telsoa01ce3e84a2018-08-31 09:31:35 +0100174 // Operands
surmeh0149b9e102018-05-17 14:11:25 +0100175 int32_t actValue = 0;
176 float weightValue[] = {2, 4, 1};
177 float biasValue[] = {4};
178
telsoa01ce3e84a2018-08-31 09:31:35 +0100179 // HASHTABLE_LOOKUP is unsupported at the time of writing this test, but any unsupported layer will do
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100180 AddInputOperand<HalPolicy>(model,
181 hidl_vec<uint32_t>{1, 1, 3, 4},
182 HalPolicy::OperandType::TENSOR_INT32);
183 AddInputOperand<HalPolicy>(model,
184 hidl_vec<uint32_t>{4},
185 HalPolicy::OperandType::TENSOR_INT32);
186 AddInputOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 1, 3, 4});
187
188 AddOutputOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 1, 3, 4});
189 AddOutputOperand<HalPolicy>(model,
190 hidl_vec<uint32_t>{1, 1, 3, 4},
Ellen Norris-Thompson976ad3e2019-08-21 15:21:14 +0100191 HalPolicy::OperandType::TENSOR_QUANT8_ASYMM,
192 1.f / 225.f);
surmeh0149b9e102018-05-17 14:11:25 +0100193
telsoa01ce3e84a2018-08-31 09:31:35 +0100194 // Fully connected is supported
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100195 AddInputOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 3});
196
197 AddTensorOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 3}, weightValue);
198 AddTensorOperand<HalPolicy>(model, hidl_vec<uint32_t>{1}, biasValue);
199
200 AddIntOperand<HalPolicy>(model, actValue);
201
202 AddOutputOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 1});
surmeh0149b9e102018-05-17 14:11:25 +0100203
telsoa01ce3e84a2018-08-31 09:31:35 +0100204 // EMBEDDING_LOOKUP is unsupported
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100205 AddOutputOperand<HalPolicy>(model, hidl_vec<uint32_t>{1, 1, 3, 4});
surmeh0149b9e102018-05-17 14:11:25 +0100206
207 model.operations.resize(3);
208
telsoa01ce3e84a2018-08-31 09:31:35 +0100209 // Unsupported
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100210 model.operations[0].type = HalPolicy::OperationType::HASHTABLE_LOOKUP;
telsoa01ce3e84a2018-08-31 09:31:35 +0100211 model.operations[0].inputs = hidl_vec<uint32_t>{0, 1, 2};
212 model.operations[0].outputs = hidl_vec<uint32_t>{3, 4};
surmeh0149b9e102018-05-17 14:11:25 +0100213
telsoa01ce3e84a2018-08-31 09:31:35 +0100214 // Supported
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100215 model.operations[1].type = HalPolicy::OperationType::FULLY_CONNECTED;
telsoa01ce3e84a2018-08-31 09:31:35 +0100216 model.operations[1].inputs = hidl_vec<uint32_t>{5, 6, 7, 8};
217 model.operations[1].outputs = hidl_vec<uint32_t>{9};
surmeh0149b9e102018-05-17 14:11:25 +0100218
telsoa01ce3e84a2018-08-31 09:31:35 +0100219 // Unsupported
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100220 model.operations[2].type = HalPolicy::OperationType::EMBEDDING_LOOKUP;
telsoa01ce3e84a2018-08-31 09:31:35 +0100221 model.operations[2].inputs = hidl_vec<uint32_t>{1, 2};
222 model.operations[2].outputs = hidl_vec<uint32_t>{10};
surmeh0149b9e102018-05-17 14:11:25 +0100223
telsoa01ce3e84a2018-08-31 09:31:35 +0100224 // We are testing that the unsupported layers return false and the test continues rather than failing and stopping
surmeh0149b9e102018-05-17 14:11:25 +0100225 driver->getSupportedOperations(model, cb);
telsoa01ce3e84a2018-08-31 09:31:35 +0100226 BOOST_TEST((int)errorStatus == (int)ErrorStatus::NONE);
227 BOOST_TEST(supported.size() == (size_t)3);
228 BOOST_TEST(supported[0] == false);
229 BOOST_TEST(supported[1] == true);
230 BOOST_TEST(supported[2] == false);
surmeh0149b9e102018-05-17 14:11:25 +0100231}
232
233// The purpose of this test is to ensure that when encountering an failure
telsoa01ce3e84a2018-08-31 09:31:35 +0100234// during mem pool mapping we properly report an error to the framework via a callback
surmeh0149b9e102018-05-17 14:11:25 +0100235BOOST_AUTO_TEST_CASE(ModelToINetworkConverterMemPoolFail)
236{
Nikhil Raj77605822018-09-03 11:25:56 +0100237 auto driver = std::make_unique<ArmnnDriver>(DriverOptions(armnn::Compute::CpuRef));
surmeh0149b9e102018-05-17 14:11:25 +0100238
telsoa01ce3e84a2018-08-31 09:31:35 +0100239 ErrorStatus errorStatus;
240 std::vector<bool> supported;
surmeh0149b9e102018-05-17 14:11:25 +0100241
telsoa01ce3e84a2018-08-31 09:31:35 +0100242 auto cb = [&](ErrorStatus _errorStatus, const std::vector<bool>& _supported)
surmeh0149b9e102018-05-17 14:11:25 +0100243 {
telsoa01ce3e84a2018-08-31 09:31:35 +0100244 errorStatus = _errorStatus;
245 supported = _supported;
surmeh0149b9e102018-05-17 14:11:25 +0100246 };
247
Aron Virginas-Tar44cfd842019-06-14 15:45:03 +0100248 HalPolicy::Model model = {};
surmeh0149b9e102018-05-17 14:11:25 +0100249
250 model.pools = hidl_vec<hidl_memory>{hidl_memory("Unsuported hidl memory type", nullptr, 0)};
251
telsoa01ce3e84a2018-08-31 09:31:35 +0100252 // Memory pool mapping should fail, we should report an error
surmeh0149b9e102018-05-17 14:11:25 +0100253 driver->getSupportedOperations(model, cb);
telsoa01ce3e84a2018-08-31 09:31:35 +0100254 BOOST_TEST((int)errorStatus != (int)ErrorStatus::NONE);
255 BOOST_TEST(supported.empty());
surmeh0149b9e102018-05-17 14:11:25 +0100256}
257
258BOOST_AUTO_TEST_SUITE_END()