blob: 5fe8415147592e819761d321c15c8983a2d29e70 [file] [log] [blame]
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Michael Butlercf22a572017-09-22 13:26:12 -070017#include "Callbacks.h"
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070018#include "TestHarness.h"
Miao Wanga2d04c82018-02-05 17:26:54 -080019#include "Utils.h"
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070020
21#include <android-base/logging.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080022#include <android/hardware/neuralnetworks/1.0/IDevice.h>
23#include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h>
24#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
25#include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h>
26#include <android/hardware/neuralnetworks/1.0/types.h>
27#include <android/hidl/allocator/1.0/IAllocator.h>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070028#include <android/hidl/memory/1.0/IMemory.h>
29#include <hidlmemory/mapping.h>
Michael Butler0897ab32017-10-04 02:38:42 -070030#include <iostream>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070031
32namespace android {
33namespace hardware {
34namespace neuralnetworks {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070035
36namespace generated_tests {
Michael Butlercf22a572017-09-22 13:26:12 -070037using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback;
38using ::android::hardware::neuralnetworks::V1_0::implementation::PreparedModelCallback;
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -070039using ::generated_tests::filter;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070040using ::generated_tests::for_all;
41using ::generated_tests::for_each;
42using ::generated_tests::resize_accordingly;
43using ::generated_tests::MixedTyped;
44using ::generated_tests::MixedTypedExampleType;
45using ::generated_tests::Float32Operands;
46using ::generated_tests::Int32Operands;
47using ::generated_tests::Quant8Operands;
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070048using ::generated_tests::compare;
49
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -070050template <typename T>
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070051void copy_back_(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) {
52 MixedTyped& test = *dst;
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -070053 for_each<T>(test, [&ra, src](int index, std::vector<T>& m) {
54 ASSERT_EQ(m.size(), ra[index].location.length / sizeof(T));
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070055 char* begin = src + ra[index].location.offset;
56 memcpy(m.data(), begin, ra[index].location.length);
57 });
58}
59
60void copy_back(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) {
61 copy_back_<float>(dst, ra, src);
62 copy_back_<int32_t>(dst, ra, src);
63 copy_back_<uint8_t>(dst, ra, src);
64}
65
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070066// Top level driver for models and examples generated by test_generator.py
67// Test driver for those generated from ml/nn/runtime/test/spec
Miao Wanga2d04c82018-02-05 17:26:54 -080068void EvaluatePreparedModel(sp<IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
69 const std::vector<MixedTypedExampleType>& examples) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070070 const uint32_t INPUT = 0;
71 const uint32_t OUTPUT = 1;
72
73 int example_no = 1;
74 for (auto& example : examples) {
75 SCOPED_TRACE(example_no++);
76
77 const MixedTyped& inputs = example.first;
78 const MixedTyped& golden = example.second;
79
80 std::vector<RequestArgument> inputs_info, outputs_info;
81 uint32_t inputSize = 0, outputSize = 0;
82
83 // This function only partially specifies the metadata (vector of RequestArguments).
84 // The contents are copied over below.
85 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
86 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
87 RequestArgument arg = {
88 .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
89 .dimensions = {},
90 };
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -070091 RequestArgument arg_empty = {
92 .hasNoValue = true,
93 };
94 inputs_info[index] = s ? arg : arg_empty;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070095 inputSize += s;
96 });
97 // Compute offset for inputs 1 and so on
98 {
99 size_t offset = 0;
100 for (auto& i : inputs_info) {
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700101 if (!i.hasNoValue) i.location.offset = offset;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700102 offset += i.location.length;
103 }
104 }
105
106 MixedTyped test; // holding test results
107
108 // Go through all outputs, initialize RequestArgument descriptors
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700109 resize_accordingly(golden, test);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700110 for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) {
111 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
112 RequestArgument arg = {
113 .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
114 .dimensions = {},
115 };
116 outputs_info[index] = arg;
117 outputSize += s;
118 });
119 // Compute offset for outputs 1 and so on
120 {
121 size_t offset = 0;
122 for (auto& i : outputs_info) {
123 i.location.offset = offset;
124 offset += i.location.length;
125 }
126 }
Miao Wanga2d04c82018-02-05 17:26:54 -0800127 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
128 nn::allocateSharedMemory(outputSize)};
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700129 ASSERT_NE(0ull, pools[INPUT].size());
130 ASSERT_NE(0ull, pools[OUTPUT].size());
131
132 // load data
133 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
134 sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]);
135 ASSERT_NE(nullptr, inputMemory.get());
136 ASSERT_NE(nullptr, outputMemory.get());
137 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
138 char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer()));
139 ASSERT_NE(nullptr, inputPtr);
140 ASSERT_NE(nullptr, outputPtr);
141 inputMemory->update();
142 outputMemory->update();
143
144 // Go through all inputs, copy the values
145 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
146 char* begin = (char*)p;
147 char* end = begin + s;
148 // TODO: handle more than one input
149 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
150 });
151
152 inputMemory->commit();
153 outputMemory->commit();
Michael Butlercf22a572017-09-22 13:26:12 -0700154
155 // launch execution
156 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
157 ASSERT_NE(nullptr, executionCallback.get());
158 Return<ErrorStatus> executionLaunchStatus = preparedModel->execute(
159 {.inputs = inputs_info, .outputs = outputs_info, .pools = pools}, executionCallback);
160 ASSERT_TRUE(executionLaunchStatus.isOk());
161 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
162
163 // retrieve execution status
164 executionCallback->wait();
165 ErrorStatus executionReturnStatus = executionCallback->getStatus();
166 EXPECT_EQ(ErrorStatus::NONE, executionReturnStatus);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700167
168 // validate results
169 outputMemory->read();
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700170 copy_back(&test, outputs_info, outputPtr);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700171 outputMemory->commit();
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700172 // Filter out don't cares
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -0700173 MixedTyped filtered_golden = filter(golden, is_ignored);
174 MixedTyped filtered_test = filter(test, is_ignored);
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700175
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700176 // We want "close-enough" results for float
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700177 compare(filtered_golden, filtered_test);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700178 }
179}
180
Miao Wanga2d04c82018-02-05 17:26:54 -0800181void Execute(sp<V1_0::IDevice>& device, std::function<V1_0::Model(void)> create_model,
182 std::function<bool(int)> is_ignored,
183 const std::vector<MixedTypedExampleType>& examples) {
184 V1_0::Model model = create_model();
185
186 // see if service can handle model
187 bool fullySupportsModel = false;
188 ErrorStatus supportedStatus;
189 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
190 ASSERT_NE(nullptr, preparedModelCallback.get());
191
192 Return<void> supportedCall = device->getSupportedOperations(
193 model, [&](ErrorStatus status, const hidl_vec<bool>& supported) {
194 supportedStatus = status;
195 ASSERT_NE(0ul, supported.size());
196 fullySupportsModel =
197 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
198 });
199 ASSERT_TRUE(supportedCall.isOk());
200 ASSERT_EQ(ErrorStatus::NONE, supportedStatus);
201 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback);
202 ASSERT_TRUE(prepareLaunchStatus.isOk());
203
204 // retrieve prepared model
205 preparedModelCallback->wait();
206 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
207 sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel();
208 if (fullySupportsModel) {
209 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
210 } else {
211 EXPECT_TRUE(prepareReturnStatus == ErrorStatus::NONE ||
212 prepareReturnStatus == ErrorStatus::GENERAL_FAILURE);
213 }
214
215 // early termination if vendor service cannot fully prepare model
216 if (!fullySupportsModel && prepareReturnStatus == ErrorStatus::GENERAL_FAILURE) {
217 ASSERT_EQ(nullptr, preparedModel.get());
218 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
219 "prepare model that it does not support.";
220 std::cout << "[ ] Early termination of test because vendor service cannot "
221 "prepare model that it does not support."
222 << std::endl;
223 return;
224 }
225 ASSERT_NE(nullptr, preparedModel.get());
226
227 EvaluatePreparedModel(preparedModel, is_ignored, examples);
228}
229
230void Execute(sp<V1_1::IDevice>& device, std::function<V1_1::Model(void)> create_model,
231 std::function<bool(int)> is_ignored,
232 const std::vector<MixedTypedExampleType>& examples) {
233 V1_1::Model model = create_model();
234
235 // see if service can handle model
236 bool fullySupportsModel = false;
237 ErrorStatus supportedStatus;
238 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
239 ASSERT_NE(nullptr, preparedModelCallback.get());
240
241 Return<void> supportedCall = device->getSupportedOperations_1_1(
242 model, [&](ErrorStatus status, const hidl_vec<bool>& supported) {
243 supportedStatus = status;
244 ASSERT_NE(0ul, supported.size());
245 fullySupportsModel =
246 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
247 });
248 ASSERT_TRUE(supportedCall.isOk());
249 ASSERT_EQ(ErrorStatus::NONE, supportedStatus);
250 Return<ErrorStatus> prepareLaunchStatus =
251 device->prepareModel_1_1(model, preparedModelCallback);
252 ASSERT_TRUE(prepareLaunchStatus.isOk());
253
254 // retrieve prepared model
255 preparedModelCallback->wait();
256 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
257 sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel();
258 if (fullySupportsModel) {
259 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
260 } else {
261 EXPECT_TRUE(prepareReturnStatus == ErrorStatus::NONE ||
262 prepareReturnStatus == ErrorStatus::GENERAL_FAILURE);
263 }
264
265 // early termination if vendor service cannot fully prepare model
266 if (!fullySupportsModel && prepareReturnStatus == ErrorStatus::GENERAL_FAILURE) {
267 ASSERT_EQ(nullptr, preparedModel.get());
268 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
269 "prepare model that it does not support.";
270 std::cout << "[ ] Early termination of test because vendor service cannot "
271 "prepare model that it does not support."
272 << std::endl;
273 return;
274 }
275 ASSERT_NE(nullptr, preparedModel.get());
276
277 EvaluatePreparedModel(preparedModel, is_ignored, examples);
278}
279
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700280} // namespace generated_tests
281
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700282} // namespace neuralnetworks
283} // namespace hardware
284} // namespace android