blob: 3a3c98ff5ced2a85baf501edf30eccbd14df364a [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"
6#include <log/log.h>
7#include <boost/test/unit_test.hpp>
8
9namespace android
10{
11namespace hardware
12{
13namespace neuralnetworks
14{
15namespace V1_0
16{
17
18std::ostream& operator<<(std::ostream& os, ErrorStatus stat)
19{
20 return os << static_cast<int>(stat);
21}
22
23} // namespace android::hardware::neuralnetworks::V1_0
24} // namespace android::hardware::neuralnetworks
25} // namespace android::hardware
26} // namespace android
27
surmeh0149b9e102018-05-17 14:11:25 +010028namespace driverTestHelpers
29{
30
telsoa01ce3e84a2018-08-31 09:31:35 +010031using namespace android::hardware;
32using namespace armnn_driver;
33
surmeh0149b9e102018-05-17 14:11:25 +010034Return<void> ExecutionCallback::notify(ErrorStatus status)
35{
36 (void)status;
37 ALOGI("ExecutionCallback::notify invoked");
38 std::lock_guard<std::mutex> executionLock(mMutex);
39 mNotified = true;
40 mCondition.notify_one();
41 return Void();
42}
43
44Return<void> ExecutionCallback::wait()
45{
46 ALOGI("ExecutionCallback::wait invoked");
47 std::unique_lock<std::mutex> executionLock(mMutex);
48 while (!mNotified)
49 {
50 mCondition.wait(executionLock);
51 }
52 mNotified = false;
53 return Void();
54}
55
56Return<void> PreparedModelCallback::notify(ErrorStatus status,
Sadik Armagane6e54a82019-05-08 10:18:05 +010057 const android::sp<V1_0::IPreparedModel>& preparedModel)
surmeh0149b9e102018-05-17 14:11:25 +010058{
59 m_ErrorStatus = status;
60 m_PreparedModel = preparedModel;
61 return Void();
62}
63
Ferran Balaguerb2397fd2019-07-25 12:12:39 +010064#ifdef ARMNN_ANDROID_NN_V1_2
65
66Return<void> PreparedModelCallback_1_2::notify(ErrorStatus status,
67 const android::sp<V1_0::IPreparedModel>& preparedModel)
68{
69 m_ErrorStatus = status;
70 m_PreparedModel = preparedModel;
71 return Void();
72}
73
74Return<void> PreparedModelCallback_1_2::notify_1_2(ErrorStatus status,
75 const android::sp<V1_2::IPreparedModel>& preparedModel)
76{
77 m_ErrorStatus = status;
78 m_PreparedModel_1_2 = preparedModel;
79 return Void();
80}
81
82#endif
83
surmeh0149b9e102018-05-17 14:11:25 +010084// lifted from common/Utils.cpp
85hidl_memory allocateSharedMemory(int64_t size)
86{
87 hidl_memory memory;
88
89 const std::string& type = "ashmem";
90 android::sp<IAllocator> allocator = IAllocator::getService(type);
91 allocator->allocate(size, [&](bool success, const hidl_memory& mem) {
92 if (!success)
93 {
94 ALOGE("unable to allocate %li bytes of %s", size, type.c_str());
95 }
96 else
97 {
98 memory = mem;
99 }
100 });
101
102 return memory;
103}
104
Sadik Armagane6e54a82019-05-08 10:18:05 +0100105android::sp<V1_0::IPreparedModel> PrepareModelWithStatus(const V1_0::Model& model,
106 armnn_driver::ArmnnDriver& driver,
107 ErrorStatus& prepareStatus,
108 ErrorStatus expectedStatus)
surmeh0149b9e102018-05-17 14:11:25 +0100109{
surmeh0149b9e102018-05-17 14:11:25 +0100110 android::sp<PreparedModelCallback> cb(new PreparedModelCallback());
111 driver.prepareModel(model, cb);
112
113 prepareStatus = cb->GetErrorStatus();
114 BOOST_TEST(prepareStatus == expectedStatus);
115 if (expectedStatus == ErrorStatus::NONE)
116 {
117 BOOST_TEST((cb->GetPreparedModel() != nullptr));
118 }
119 return cb->GetPreparedModel();
120}
121
Matteo Martincigha5f9e762019-06-17 13:26:34 +0100122#if defined(ARMNN_ANDROID_NN_V1_1) || defined(ARMNN_ANDROID_NN_V1_2)
Nikhil Raj77605822018-09-03 11:25:56 +0100123
Sadik Armagane6e54a82019-05-08 10:18:05 +0100124android::sp<V1_0::IPreparedModel> PrepareModelWithStatus(const V1_1::Model& model,
125 armnn_driver::ArmnnDriver& driver,
126 ErrorStatus& prepareStatus,
127 ErrorStatus expectedStatus)
surmeh0149b9e102018-05-17 14:11:25 +0100128{
Nikhil Raj77605822018-09-03 11:25:56 +0100129 android::sp<PreparedModelCallback> cb(new PreparedModelCallback());
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100130 driver.prepareModel_1_1(model, V1_1::ExecutionPreference::LOW_POWER, cb);
Nikhil Raj77605822018-09-03 11:25:56 +0100131
132 prepareStatus = cb->GetErrorStatus();
133 BOOST_TEST(prepareStatus == expectedStatus);
134 if (expectedStatus == ErrorStatus::NONE)
135 {
136 BOOST_TEST((cb->GetPreparedModel() != nullptr));
137 }
138 return cb->GetPreparedModel();
surmeh0149b9e102018-05-17 14:11:25 +0100139}
140
Nikhil Raj77605822018-09-03 11:25:56 +0100141#endif
142
Ferran Balaguerb2397fd2019-07-25 12:12:39 +0100143#ifdef ARMNN_ANDROID_NN_V1_2
144
145android::sp<V1_2::IPreparedModel> PrepareModelWithStatus_1_2(const armnn_driver::hal_1_2::HalPolicy::Model& model,
146 armnn_driver::ArmnnDriver& driver,
147 ErrorStatus& prepareStatus,
148 ErrorStatus expectedStatus)
149{
150 android::sp<PreparedModelCallback_1_2> cb(new PreparedModelCallback_1_2());
151
152 android::hardware::hidl_vec<android::hardware::hidl_handle> emptyHandle1;
153 android::hardware::hidl_vec<android::hardware::hidl_handle> emptyHandle2;
154 armnn_driver::ArmnnDriver::HidlToken emptyToken;
155
156 driver.prepareModel_1_2(model, V1_1::ExecutionPreference::LOW_POWER, emptyHandle1, emptyHandle2, emptyToken, cb);
157
158 prepareStatus = cb->GetErrorStatus();
159 BOOST_TEST(prepareStatus == expectedStatus);
160 if (expectedStatus == ErrorStatus::NONE)
161 {
162 BOOST_TEST((cb->GetPreparedModel_1_2() != nullptr));
163 }
164 return cb->GetPreparedModel_1_2();
165}
166
167#endif
168
Sadik Armagane6e54a82019-05-08 10:18:05 +0100169ErrorStatus Execute(android::sp<V1_0::IPreparedModel> preparedModel,
surmeh0149b9e102018-05-17 14:11:25 +0100170 const Request& request,
171 ErrorStatus expectedStatus)
172{
telsoa01ce3e84a2018-08-31 09:31:35 +0100173 BOOST_TEST(preparedModel.get() != nullptr);
surmeh0149b9e102018-05-17 14:11:25 +0100174 android::sp<ExecutionCallback> cb(new ExecutionCallback());
175 ErrorStatus execStatus = preparedModel->execute(request, cb);
176 BOOST_TEST(execStatus == expectedStatus);
177 ALOGI("Execute: waiting for callback to be invoked");
178 cb->wait();
179 return execStatus;
180}
181
Sadik Armagane6e54a82019-05-08 10:18:05 +0100182android::sp<ExecutionCallback> ExecuteNoWait(android::sp<V1_0::IPreparedModel> preparedModel, const Request& request)
surmeh0149b9e102018-05-17 14:11:25 +0100183{
184 android::sp<ExecutionCallback> cb(new ExecutionCallback());
185 BOOST_TEST(preparedModel->execute(request, cb) == ErrorStatus::NONE);
186 ALOGI("ExecuteNoWait: returning callback object");
187 return cb;
188}
189
190template<>
191OperandType TypeToOperandType<float>()
192{
193 return OperandType::TENSOR_FLOAT32;
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100194}
surmeh0149b9e102018-05-17 14:11:25 +0100195
196template<>
197OperandType TypeToOperandType<int32_t>()
198{
199 return OperandType::TENSOR_INT32;
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100200}
surmeh0149b9e102018-05-17 14:11:25 +0100201
202} // namespace driverTestHelpers