blob: c3578cdca7bc75a64ddc32c83a665cbec0138b35 [file] [log] [blame]
Slava Shklyaev73ee79d2019-05-14 14:15:14 +01001/*
2 * Copyright (C) 2019 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
17#include "GeneratedTestHarness.h"
18
19#include <android-base/logging.h>
20#include <android/hardware/neuralnetworks/1.0/IDevice.h>
21#include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h>
22#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
23#include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h>
24#include <android/hardware/neuralnetworks/1.0/types.h>
25#include <android/hardware/neuralnetworks/1.1/IDevice.h>
26#include <android/hardware/neuralnetworks/1.2/IDevice.h>
27#include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h>
28#include <android/hardware/neuralnetworks/1.2/IPreparedModel.h>
29#include <android/hardware/neuralnetworks/1.2/IPreparedModelCallback.h>
30#include <android/hidl/allocator/1.0/IAllocator.h>
31#include <android/hidl/memory/1.0/IMemory.h>
32#include <hidlmemory/mapping.h>
33
34#include <iostream>
35
36#include "1.0/Utils.h"
37#include "1.2/Callbacks.h"
38#include "ExecutionBurstController.h"
39#include "MemoryUtils.h"
40#include "TestHarness.h"
41#include "Utils.h"
42
43namespace android {
44namespace hardware {
45namespace neuralnetworks {
46namespace generated_tests {
47
48using ::android::hardware::neuralnetworks::V1_0::ErrorStatus;
49using ::android::hardware::neuralnetworks::V1_0::Request;
50using ::android::hardware::neuralnetworks::V1_0::RequestArgument;
51using ::android::hardware::neuralnetworks::V1_1::ExecutionPreference;
52using ::android::hardware::neuralnetworks::V1_2::IDevice;
53using ::android::hardware::neuralnetworks::V1_2::IPreparedModel;
54using ::android::hardware::neuralnetworks::V1_2::Model;
55using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
56using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback;
57using ::android::hidl::memory::V1_0::IMemory;
58using ::test_helper::compare;
59using ::test_helper::expectMultinomialDistributionWithinTolerance;
60using ::test_helper::filter;
61using ::test_helper::for_all;
62using ::test_helper::for_each;
63using ::test_helper::MixedTyped;
64using ::test_helper::MixedTypedExample;
65using ::test_helper::resize_accordingly;
66using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
67
68static bool isZeroSized(const MixedTyped& example, uint32_t index) {
69 for (auto i : example.operandDimensions.at(index)) {
70 if (i == 0) return true;
71 }
72 return false;
73}
74
75static Return<ErrorStatus> ExecutePreparedModel(sp<IPreparedModel>& preparedModel,
76 const Request& request, MeasureTiming measure,
77 sp<ExecutionCallback>& callback) {
78 return preparedModel->execute_1_2(request, measure, callback);
79}
80static Return<ErrorStatus> ExecutePreparedModel(sp<IPreparedModel>& preparedModel,
81 const Request& request, MeasureTiming measure,
82 hidl_vec<OutputShape>* outputShapes,
83 Timing* timing) {
84 ErrorStatus result;
85 Return<void> ret = preparedModel->executeSynchronously(
86 request, measure,
87 [&result, outputShapes, timing](ErrorStatus error, const hidl_vec<OutputShape>& shapes,
88 const Timing& time) {
89 result = error;
90 *outputShapes = shapes;
91 *timing = time;
92 });
93 if (!ret.isOk()) {
94 return ErrorStatus::GENERAL_FAILURE;
95 }
96 return result;
97}
98static std::shared_ptr<::android::nn::ExecutionBurstController> CreateBurst(
99 const sp<IPreparedModel>& preparedModel) {
100 return ::android::nn::ExecutionBurstController::create(preparedModel, /*blocking=*/true);
101}
102enum class Executor { ASYNC, SYNC, BURST };
103enum class OutputType { FULLY_SPECIFIED, UNSPECIFIED, INSUFFICIENT };
104const float kDefaultAtol = 1e-5f;
105const float kDefaultRtol = 1e-5f;
106void EvaluatePreparedModel(sp<IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
107 const std::vector<MixedTypedExample>& examples,
108 bool hasRelaxedFloat32Model, float fpAtol, float fpRtol,
109 Executor executor, MeasureTiming measure, OutputType outputType) {
110 const uint32_t INPUT = 0;
111 const uint32_t OUTPUT = 1;
112
113 int example_no = 1;
114 for (auto& example : examples) {
115 SCOPED_TRACE(example_no++);
116 const MixedTyped& inputs = example.operands.first;
117 const MixedTyped& golden = example.operands.second;
118
119 const bool hasFloat16Inputs = !inputs.float16Operands.empty();
120 if (hasRelaxedFloat32Model || hasFloat16Inputs) {
121 // TODO: Adjust the error limit based on testing.
122 // If in relaxed mode, set the absolute tolerance to be 5ULP of FP16.
123 fpAtol = 5.0f * 0.0009765625f;
124 // Set the relative tolerance to be 5ULP of the corresponding FP precision.
125 fpRtol = 5.0f * 0.0009765625f;
126 }
127
128 std::vector<RequestArgument> inputs_info, outputs_info;
129 uint32_t inputSize = 0, outputSize = 0;
130 // This function only partially specifies the metadata (vector of RequestArguments).
131 // The contents are copied over below.
132 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
133 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
134 RequestArgument arg = {
135 .location = {.poolIndex = INPUT,
136 .offset = 0,
137 .length = static_cast<uint32_t>(s)},
138 .dimensions = {},
139 };
140 RequestArgument arg_empty = {
141 .hasNoValue = true,
142 };
143 inputs_info[index] = s ? arg : arg_empty;
144 inputSize += s;
145 });
146 // Compute offset for inputs 1 and so on
147 {
148 size_t offset = 0;
149 for (auto& i : inputs_info) {
150 if (!i.hasNoValue) i.location.offset = offset;
151 offset += i.location.length;
152 }
153 }
154
155 MixedTyped test; // holding test results
156
157 // Go through all outputs, initialize RequestArgument descriptors
158 resize_accordingly(golden, test);
159 bool sizeLargerThanOne = true;
160 for_all(golden, [&golden, &outputs_info, &outputSize, &outputType, &sizeLargerThanOne](
161 int index, auto, auto s) {
162 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
163 if (index == 0) {
164 // On OutputType::INSUFFICIENT, set the output operand with index 0 with
165 // buffer size one byte less than needed.
166 if (outputType == OutputType::INSUFFICIENT) {
167 if (s > 1 && !isZeroSized(golden, index)) {
168 s -= 1;
169 } else {
170 sizeLargerThanOne = false;
171 }
172 }
173 }
174 RequestArgument arg = {
175 .location = {.poolIndex = OUTPUT,
176 .offset = 0,
177 .length = static_cast<uint32_t>(s)},
178 .dimensions = {},
179 };
180 outputs_info[index] = arg;
181 outputSize += s;
182 });
183 // If output0 does not have size larger than one byte,
184 // we can not provide an insufficient buffer
185 if (!sizeLargerThanOne && outputType == OutputType::INSUFFICIENT) return;
186 // Compute offset for outputs 1 and so on
187 {
188 size_t offset = 0;
189 for (auto& i : outputs_info) {
190 i.location.offset = offset;
191 offset += i.location.length;
192 }
193 }
194 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
195 nn::allocateSharedMemory(outputSize)};
196 ASSERT_NE(0ull, pools[INPUT].size());
197 ASSERT_NE(0ull, pools[OUTPUT].size());
198
199 // load data
200 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
201 sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]);
202 ASSERT_NE(nullptr, inputMemory.get());
203 ASSERT_NE(nullptr, outputMemory.get());
204 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
205 char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer()));
206 ASSERT_NE(nullptr, inputPtr);
207 ASSERT_NE(nullptr, outputPtr);
208 inputMemory->update();
209 outputMemory->update();
210
211 // Go through all inputs, copy the values
212 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
213 char* begin = (char*)p;
214 char* end = begin + s;
215 // TODO: handle more than one input
216 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
217 });
218
219 inputMemory->commit();
220 outputMemory->commit();
221
222 const Request request = {.inputs = inputs_info, .outputs = outputs_info, .pools = pools};
223
224 ErrorStatus executionStatus;
225 hidl_vec<OutputShape> outputShapes;
226 Timing timing;
227 switch (executor) {
228 case Executor::ASYNC: {
229 SCOPED_TRACE("asynchronous");
230
231 // launch execution
232 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
233 ASSERT_NE(nullptr, executionCallback.get());
234 Return<ErrorStatus> executionLaunchStatus =
235 ExecutePreparedModel(preparedModel, request, measure, executionCallback);
236 ASSERT_TRUE(executionLaunchStatus.isOk());
237 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
238
239 // retrieve execution status
240 executionCallback->wait();
241 executionStatus = executionCallback->getStatus();
242 outputShapes = executionCallback->getOutputShapes();
243 timing = executionCallback->getTiming();
244
245 break;
246 }
247 case Executor::SYNC: {
248 SCOPED_TRACE("synchronous");
249
250 // execute
251 Return<ErrorStatus> executionReturnStatus = ExecutePreparedModel(
252 preparedModel, request, measure, &outputShapes, &timing);
253 ASSERT_TRUE(executionReturnStatus.isOk());
254 executionStatus = static_cast<ErrorStatus>(executionReturnStatus);
255
256 break;
257 }
258 case Executor::BURST: {
259 SCOPED_TRACE("burst");
260
261 // create burst
262 const std::shared_ptr<::android::nn::ExecutionBurstController> controller =
263 CreateBurst(preparedModel);
264 ASSERT_NE(nullptr, controller.get());
265
266 // create memory keys
267 std::vector<intptr_t> keys(request.pools.size());
268 for (size_t i = 0; i < keys.size(); ++i) {
269 keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]);
270 }
271
272 // execute burst
273 std::tie(executionStatus, outputShapes, timing) =
274 controller->compute(request, measure, keys);
275
276 break;
277 }
278 }
279
280 if (outputType != OutputType::FULLY_SPECIFIED &&
281 executionStatus == ErrorStatus::GENERAL_FAILURE) {
282 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
283 "execute model that it does not support.";
284 std::cout << "[ ] Early termination of test because vendor service cannot "
285 "execute model that it does not support."
286 << std::endl;
287 GTEST_SKIP();
288 }
289 if (measure == MeasureTiming::NO) {
290 EXPECT_EQ(UINT64_MAX, timing.timeOnDevice);
291 EXPECT_EQ(UINT64_MAX, timing.timeInDriver);
292 } else {
293 if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) {
294 EXPECT_LE(timing.timeOnDevice, timing.timeInDriver);
295 }
296 }
297
298 switch (outputType) {
299 case OutputType::FULLY_SPECIFIED:
300 // If the model output operands are fully specified, outputShapes must be either
301 // either empty, or have the same number of elements as the number of outputs.
302 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
303 ASSERT_TRUE(outputShapes.size() == 0 ||
304 outputShapes.size() == test.operandDimensions.size());
305 break;
306 case OutputType::UNSPECIFIED:
307 // If the model output operands are not fully specified, outputShapes must have
308 // the same number of elements as the number of outputs.
309 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
310 ASSERT_EQ(outputShapes.size(), test.operandDimensions.size());
311 break;
312 case OutputType::INSUFFICIENT:
313 ASSERT_EQ(ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, executionStatus);
314 ASSERT_EQ(outputShapes.size(), test.operandDimensions.size());
315 ASSERT_FALSE(outputShapes[0].isSufficient);
316 return;
317 }
318 // Go through all outputs, overwrite output dimensions with returned output shapes
319 if (outputShapes.size() > 0) {
320 for_each<uint32_t>(test.operandDimensions,
321 [&outputShapes](int idx, std::vector<uint32_t>& dim) {
322 dim = outputShapes[idx].dimensions;
323 });
324 }
325
326 // validate results
327 outputMemory->read();
328 copy_back(&test, outputs_info, outputPtr);
329 outputMemory->commit();
330 // Filter out don't cares
331 MixedTyped filtered_golden = filter(golden, is_ignored);
332 MixedTyped filtered_test = filter(test, is_ignored);
333
334 // We want "close-enough" results for float
335 compare(filtered_golden, filtered_test, fpAtol, fpRtol);
336
337 if (example.expectedMultinomialDistributionTolerance > 0) {
338 expectMultinomialDistributionWithinTolerance(test, example);
339 }
340 }
341}
342void EvaluatePreparedModel(sp<IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
343 const std::vector<MixedTypedExample>& examples,
344 bool hasRelaxedFloat32Model, Executor executor, MeasureTiming measure,
345 OutputType outputType) {
346 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model, kDefaultAtol,
347 kDefaultRtol, executor, measure, outputType);
348}
349
350void EvaluatePreparedModel(sp<IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
351 const std::vector<MixedTypedExample>& examples,
352 bool hasRelaxedFloat32Model, bool testDynamicOutputShape) {
353 if (testDynamicOutputShape) {
354 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
355 Executor::ASYNC, MeasureTiming::NO, OutputType::UNSPECIFIED);
356 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
357 Executor::SYNC, MeasureTiming::NO, OutputType::UNSPECIFIED);
358 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
359 Executor::BURST, MeasureTiming::NO, OutputType::UNSPECIFIED);
360 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
361 Executor::ASYNC, MeasureTiming::YES, OutputType::UNSPECIFIED);
362 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
363 Executor::SYNC, MeasureTiming::YES, OutputType::UNSPECIFIED);
364 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
365 Executor::BURST, MeasureTiming::YES, OutputType::UNSPECIFIED);
366 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
367 Executor::ASYNC, MeasureTiming::NO, OutputType::INSUFFICIENT);
368 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
369 Executor::SYNC, MeasureTiming::NO, OutputType::INSUFFICIENT);
370 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
371 Executor::BURST, MeasureTiming::NO, OutputType::INSUFFICIENT);
372 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
373 Executor::ASYNC, MeasureTiming::YES, OutputType::INSUFFICIENT);
374 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
375 Executor::SYNC, MeasureTiming::YES, OutputType::INSUFFICIENT);
376 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
377 Executor::BURST, MeasureTiming::YES, OutputType::INSUFFICIENT);
378 } else {
379 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
380 Executor::ASYNC, MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
381 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
382 Executor::SYNC, MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
383 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
384 Executor::BURST, MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
385 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
386 Executor::ASYNC, MeasureTiming::YES, OutputType::FULLY_SPECIFIED);
387 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
388 Executor::SYNC, MeasureTiming::YES, OutputType::FULLY_SPECIFIED);
389 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
390 Executor::BURST, MeasureTiming::YES, OutputType::FULLY_SPECIFIED);
391 }
392}
393
394void PrepareModel(const sp<IDevice>& device, const Model& model,
395 sp<IPreparedModel>* preparedModel) {
396 // see if service can handle model
397 bool fullySupportsModel = false;
398 Return<void> supportedCall = device->getSupportedOperations_1_2(
399 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
400 ASSERT_EQ(ErrorStatus::NONE, status);
401 ASSERT_NE(0ul, supported.size());
402 fullySupportsModel = std::all_of(supported.begin(), supported.end(),
403 [](bool valid) { return valid; });
404 });
405 ASSERT_TRUE(supportedCall.isOk());
406
407 // launch prepare model
408 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
409 ASSERT_NE(nullptr, preparedModelCallback.get());
410 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2(
411 model, ExecutionPreference::FAST_SINGLE_ANSWER, hidl_vec<hidl_handle>(),
412 hidl_vec<hidl_handle>(), HidlToken(), preparedModelCallback);
413 ASSERT_TRUE(prepareLaunchStatus.isOk());
414 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
415
416 // retrieve prepared model
417 preparedModelCallback->wait();
418 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
419 sp<V1_0::IPreparedModel> preparedModelV1_0 = preparedModelCallback->getPreparedModel();
420 *preparedModel = IPreparedModel::castFrom(preparedModelV1_0).withDefault(nullptr);
421
422 // early termination if vendor service cannot fully prepare model
423 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
424 ASSERT_EQ(nullptr, preparedModel->get());
425 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
426 "prepare model that it does not support.";
427 std::cout << "[ ] Early termination of test because vendor service cannot "
428 "prepare model that it does not support."
429 << std::endl;
430 return;
431 }
432 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
433 ASSERT_NE(nullptr, preparedModel->get());
434}
435
436void Execute(const sp<IDevice>& device, std::function<Model(void)> create_model,
437 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples,
438 bool testDynamicOutputShape) {
439 Model model = create_model();
440 sp<IPreparedModel> preparedModel = nullptr;
441 PrepareModel(device, model, &preparedModel);
442 if (preparedModel == nullptr) {
443 GTEST_SKIP();
444 }
445 EvaluatePreparedModel(preparedModel, is_ignored, examples,
446 model.relaxComputationFloat32toFloat16, testDynamicOutputShape);
447}
448
449} // namespace generated_tests
450} // namespace neuralnetworks
451} // namespace hardware
452} // namespace android