blob: 3bc0a20ad3d08ecfa7d533c683cc48acfd6d0e2b [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,
57 const android::sp<IPreparedModel>& preparedModel)
58{
59 m_ErrorStatus = status;
60 m_PreparedModel = preparedModel;
61 return Void();
62}
63
64// lifted from common/Utils.cpp
65hidl_memory allocateSharedMemory(int64_t size)
66{
67 hidl_memory memory;
68
69 const std::string& type = "ashmem";
70 android::sp<IAllocator> allocator = IAllocator::getService(type);
71 allocator->allocate(size, [&](bool success, const hidl_memory& mem) {
72 if (!success)
73 {
74 ALOGE("unable to allocate %li bytes of %s", size, type.c_str());
75 }
76 else
77 {
78 memory = mem;
79 }
80 });
81
82 return memory;
83}
84
85android::sp<IMemory> AddPoolAndGetData(uint32_t size, Request& request)
86{
87 hidl_memory pool;
88
89 android::sp<IAllocator> allocator = IAllocator::getService("ashmem");
90 allocator->allocate(sizeof(float) * size, [&](bool success, const hidl_memory& mem) {
91 BOOST_TEST(success);
92 pool = mem;
93 });
94
95 request.pools.resize(request.pools.size() + 1);
96 request.pools[request.pools.size() - 1] = pool;
97
98 android::sp<IMemory> mapped = mapMemory(pool);
99 mapped->update();
100 return mapped;
101}
102
103void AddPoolAndSetData(uint32_t size, Request& request, const float* data)
104{
105 android::sp<IMemory> memory = AddPoolAndGetData(size, request);
106
107 float* dst = static_cast<float*>(static_cast<void*>(memory->getPointer()));
108
109 memcpy(dst, data, size * sizeof(float));
110}
111
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100112android::sp<IPreparedModel> PrepareModelWithStatus(const V1_0::Model& model,
surmeh0149b9e102018-05-17 14:11:25 +0100113 armnn_driver::ArmnnDriver& driver,
Nikhil Raj77605822018-09-03 11:25:56 +0100114 ErrorStatus& prepareStatus,
surmeh0149b9e102018-05-17 14:11:25 +0100115 ErrorStatus expectedStatus)
116{
surmeh0149b9e102018-05-17 14:11:25 +0100117 android::sp<PreparedModelCallback> cb(new PreparedModelCallback());
118 driver.prepareModel(model, cb);
119
120 prepareStatus = cb->GetErrorStatus();
121 BOOST_TEST(prepareStatus == expectedStatus);
122 if (expectedStatus == ErrorStatus::NONE)
123 {
124 BOOST_TEST((cb->GetPreparedModel() != nullptr));
125 }
126 return cb->GetPreparedModel();
127}
128
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100129#ifdef ARMNN_ANDROID_NN_V1_1
Nikhil Raj77605822018-09-03 11:25:56 +0100130
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100131android::sp<IPreparedModel> PrepareModelWithStatus(const V1_1::Model& model,
Nikhil Raj77605822018-09-03 11:25:56 +0100132 armnn_driver::ArmnnDriver& driver,
133 ErrorStatus& prepareStatus,
134 ErrorStatus expectedStatus)
surmeh0149b9e102018-05-17 14:11:25 +0100135{
Nikhil Raj77605822018-09-03 11:25:56 +0100136 android::sp<PreparedModelCallback> cb(new PreparedModelCallback());
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100137 driver.prepareModel_1_1(model, V1_1::ExecutionPreference::LOW_POWER, cb);
Nikhil Raj77605822018-09-03 11:25:56 +0100138
139 prepareStatus = cb->GetErrorStatus();
140 BOOST_TEST(prepareStatus == expectedStatus);
141 if (expectedStatus == ErrorStatus::NONE)
142 {
143 BOOST_TEST((cb->GetPreparedModel() != nullptr));
144 }
145 return cb->GetPreparedModel();
surmeh0149b9e102018-05-17 14:11:25 +0100146}
147
Nikhil Raj77605822018-09-03 11:25:56 +0100148#endif
149
surmeh0149b9e102018-05-17 14:11:25 +0100150ErrorStatus Execute(android::sp<IPreparedModel> preparedModel,
151 const Request& request,
152 ErrorStatus expectedStatus)
153{
telsoa01ce3e84a2018-08-31 09:31:35 +0100154 BOOST_TEST(preparedModel.get() != nullptr);
surmeh0149b9e102018-05-17 14:11:25 +0100155 android::sp<ExecutionCallback> cb(new ExecutionCallback());
156 ErrorStatus execStatus = preparedModel->execute(request, cb);
157 BOOST_TEST(execStatus == expectedStatus);
158 ALOGI("Execute: waiting for callback to be invoked");
159 cb->wait();
160 return execStatus;
161}
162
163android::sp<ExecutionCallback> ExecuteNoWait(android::sp<IPreparedModel> preparedModel, const Request& request)
164{
165 android::sp<ExecutionCallback> cb(new ExecutionCallback());
166 BOOST_TEST(preparedModel->execute(request, cb) == ErrorStatus::NONE);
167 ALOGI("ExecuteNoWait: returning callback object");
168 return cb;
169}
170
171template<>
172OperandType TypeToOperandType<float>()
173{
174 return OperandType::TENSOR_FLOAT32;
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100175}
surmeh0149b9e102018-05-17 14:11:25 +0100176
177template<>
178OperandType TypeToOperandType<int32_t>()
179{
180 return OperandType::TENSOR_INT32;
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100181}
surmeh0149b9e102018-05-17 14:11:25 +0100182
183} // namespace driverTestHelpers