blob: 7eea7fcedc3e8b7d7d3fd617d889896812139dee [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
Xusong Wang34058782019-01-18 17:28:26 -080017#include "GeneratedTestHarness.h"
Michael Butlercf22a572017-09-22 13:26:12 -070018#include "Callbacks.h"
Michael Butler814d8372019-01-15 11:02:55 -080019#include "ExecutionBurstController.h"
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070020#include "TestHarness.h"
Miao Wanga2d04c82018-02-05 17:26:54 -080021#include "Utils.h"
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070022
23#include <android-base/logging.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080024#include <android/hardware/neuralnetworks/1.0/IDevice.h>
25#include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h>
26#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
27#include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h>
28#include <android/hardware/neuralnetworks/1.0/types.h>
Xusong Wangb5cb8f72018-10-31 08:43:12 -070029#include <android/hardware/neuralnetworks/1.1/IDevice.h>
30#include <android/hardware/neuralnetworks/1.2/IDevice.h>
31#include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h>
32#include <android/hardware/neuralnetworks/1.2/IPreparedModel.h>
33#include <android/hardware/neuralnetworks/1.2/IPreparedModelCallback.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080034#include <android/hidl/allocator/1.0/IAllocator.h>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070035#include <android/hidl/memory/1.0/IMemory.h>
36#include <hidlmemory/mapping.h>
Michael Butler0897ab32017-10-04 02:38:42 -070037#include <iostream>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070038
39namespace android {
40namespace hardware {
41namespace neuralnetworks {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070042
43namespace generated_tests {
Xusong Wangb5cb8f72018-10-31 08:43:12 -070044using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
45using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback;
Slava Shklyaev9e3fad12018-11-30 17:55:12 +000046using ::test_helper::bool8;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010047using ::test_helper::compare;
48using ::test_helper::expectMultinomialDistributionWithinTolerance;
Mika Raentode166942018-04-17 16:49:50 +010049using ::test_helper::filter;
50using ::test_helper::for_all;
51using ::test_helper::for_each;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010052using ::test_helper::MixedTyped;
53using ::test_helper::MixedTypedExample;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010054using ::test_helper::resize_accordingly;
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070055
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -070056template <typename T>
Xusong Wanga3165812018-11-19 18:26:08 -080057void copy_back_(std::map<int, std::vector<T>>* dst, const std::vector<RequestArgument>& ra,
58 char* src) {
59 for_each<T>(*dst, [&ra, src](int index, std::vector<T>& m) {
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -070060 ASSERT_EQ(m.size(), ra[index].location.length / sizeof(T));
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070061 char* begin = src + ra[index].location.offset;
62 memcpy(m.data(), begin, ra[index].location.length);
63 });
64}
65
66void copy_back(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) {
Xusong Wanga3165812018-11-19 18:26:08 -080067 copy_back_(&dst->float32Operands, ra, src);
68 copy_back_(&dst->int32Operands, ra, src);
Xusong Wangd49f6652019-01-16 18:32:24 -080069 copy_back_(&dst->quant8AsymmOperands, ra, src);
70 copy_back_(&dst->quant16SymmOperands, ra, src);
Xusong Wanga3165812018-11-19 18:26:08 -080071 copy_back_(&dst->float16Operands, ra, src);
72 copy_back_(&dst->bool8Operands, ra, src);
73 copy_back_(&dst->quant8ChannelOperands, ra, src);
Xusong Wangd49f6652019-01-16 18:32:24 -080074 copy_back_(&dst->quant16AsymmOperands, ra, src);
Lev Proleevbf26a9e2019-02-20 12:49:14 +000075 copy_back_(&dst->quant8SymmOperands, ra, src);
76 static_assert(9 == MixedTyped::kNumTypes,
Lev Proleev9b490f42018-11-02 12:44:11 +000077 "Number of types in MixedTyped changed, but copy_back function wasn't updated");
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070078}
79
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070080// Top level driver for models and examples generated by test_generator.py
81// Test driver for those generated from ml/nn/runtime/test/spec
Xusong Wangb5cb8f72018-10-31 08:43:12 -070082static Return<ErrorStatus> ExecutePreparedModel(sp<V1_0::IPreparedModel>& preparedModel,
David Grosse3013492019-01-23 14:01:52 -080083 const Request& request, MeasureTiming,
Xusong Wangb5cb8f72018-10-31 08:43:12 -070084 sp<ExecutionCallback>& callback) {
85 return preparedModel->execute(request, callback);
86}
87static Return<ErrorStatus> ExecutePreparedModel(sp<V1_2::IPreparedModel>& preparedModel,
David Grosse3013492019-01-23 14:01:52 -080088 const Request& request, MeasureTiming measure,
Xusong Wangb5cb8f72018-10-31 08:43:12 -070089 sp<ExecutionCallback>& callback) {
David Grosse3013492019-01-23 14:01:52 -080090 return preparedModel->execute_1_2(request, measure, callback);
Xusong Wangb5cb8f72018-10-31 08:43:12 -070091}
Xusong Wang187c5972018-11-07 09:33:59 -080092static Return<ErrorStatus> ExecutePreparedModel(sp<V1_0::IPreparedModel>&, const Request&,
David Grosse3013492019-01-23 14:01:52 -080093 MeasureTiming, hidl_vec<OutputShape>*, Timing*) {
David Gross49e41672018-12-21 11:20:26 -080094 ADD_FAILURE() << "asking for synchronous execution at V1_0";
95 return ErrorStatus::GENERAL_FAILURE;
96}
97static Return<ErrorStatus> ExecutePreparedModel(sp<V1_2::IPreparedModel>& preparedModel,
David Grosse3013492019-01-23 14:01:52 -080098 const Request& request, MeasureTiming measure,
99 hidl_vec<OutputShape>* outputShapes,
100 Timing* timing) {
Xusong Wang187c5972018-11-07 09:33:59 -0800101 ErrorStatus result;
102 Return<void> ret = preparedModel->executeSynchronously(
David Grosse3013492019-01-23 14:01:52 -0800103 request, measure,
104 [&result, outputShapes, timing](ErrorStatus error, const hidl_vec<OutputShape>& shapes,
105 const Timing& time) {
106 result = error;
107 *outputShapes = shapes;
108 *timing = time;
109 });
Xusong Wang187c5972018-11-07 09:33:59 -0800110 if (!ret.isOk()) {
111 return ErrorStatus::GENERAL_FAILURE;
112 }
113 return result;
David Gross49e41672018-12-21 11:20:26 -0800114}
Michael Butler814d8372019-01-15 11:02:55 -0800115static std::unique_ptr<::android::nn::ExecutionBurstController> CreateBurst(
116 const sp<V1_0::IPreparedModel>&) {
117 ADD_FAILURE() << "asking for burst execution at V1_0";
118 return nullptr;
119}
120static std::unique_ptr<::android::nn::ExecutionBurstController> CreateBurst(
121 const sp<V1_2::IPreparedModel>& preparedModel) {
122 return ::android::nn::createExecutionBurstController(preparedModel, /*blocking=*/true);
123}
124enum class Executor { ASYNC, SYNC, BURST };
Xusong Wang929fd212019-01-27 23:08:12 -0800125enum class OutputType { FULLY_SPECIFIED, UNSPECIFIED, INSUFFICIENT };
David Gross49e41672018-12-21 11:20:26 -0800126const float kDefaultAtol = 1e-5f;
127const float kDefaultRtol = 1e-5f;
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700128template <typename T_IPreparedModel>
129void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000130 const std::vector<MixedTypedExample>& examples,
David Grosse3013492019-01-23 14:01:52 -0800131 bool hasRelaxedFloat32Model, float fpAtol, float fpRtol,
Xusong Wang929fd212019-01-27 23:08:12 -0800132 Executor executor, MeasureTiming measure, OutputType outputType) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700133 const uint32_t INPUT = 0;
134 const uint32_t OUTPUT = 1;
135
136 int example_no = 1;
137 for (auto& example : examples) {
138 SCOPED_TRACE(example_no++);
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100139 const MixedTyped& inputs = example.operands.first;
140 const MixedTyped& golden = example.operands.second;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700141
Xusong Wanga3165812018-11-19 18:26:08 -0800142 const bool hasFloat16Inputs = !inputs.float16Operands.empty();
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000143 if (hasRelaxedFloat32Model || hasFloat16Inputs) {
144 // TODO: Adjust the error limit based on testing.
145 // If in relaxed mode, set the absolute tolerance to be 5ULP of FP16.
146 fpAtol = 5.0f * 0.0009765625f;
147 // Set the relative tolerance to be 5ULP of the corresponding FP precision.
148 fpRtol = 5.0f * 0.0009765625f;
149 }
150
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700151 std::vector<RequestArgument> inputs_info, outputs_info;
152 uint32_t inputSize = 0, outputSize = 0;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700153 // This function only partially specifies the metadata (vector of RequestArguments).
154 // The contents are copied over below.
155 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
156 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
157 RequestArgument arg = {
158 .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
159 .dimensions = {},
160 };
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700161 RequestArgument arg_empty = {
162 .hasNoValue = true,
163 };
164 inputs_info[index] = s ? arg : arg_empty;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700165 inputSize += s;
166 });
167 // Compute offset for inputs 1 and so on
168 {
169 size_t offset = 0;
170 for (auto& i : inputs_info) {
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700171 if (!i.hasNoValue) i.location.offset = offset;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700172 offset += i.location.length;
173 }
174 }
175
176 MixedTyped test; // holding test results
177
178 // Go through all outputs, initialize RequestArgument descriptors
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700179 resize_accordingly(golden, test);
Xusong Wang929fd212019-01-27 23:08:12 -0800180 bool sizeLargerThanOne = true;
181 for_all(golden, [&outputs_info, &outputSize, &outputType, &sizeLargerThanOne](
182 int index, auto, auto s) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700183 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
Xusong Wang929fd212019-01-27 23:08:12 -0800184 if (index == 0) {
185 // On OutputType::INSUFFICIENT, set the output operand with index 0 with
186 // buffer size one byte less than needed.
187 if (outputType == OutputType::INSUFFICIENT) {
188 if (s > 1)
189 s -= 1;
190 else
191 sizeLargerThanOne = false;
192 }
193 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700194 RequestArgument arg = {
195 .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
196 .dimensions = {},
197 };
198 outputs_info[index] = arg;
199 outputSize += s;
200 });
Xusong Wang929fd212019-01-27 23:08:12 -0800201 // If output0 does not have size larger than one byte,
202 // we can not provide an insufficient buffer
203 if (!sizeLargerThanOne && outputType == OutputType::INSUFFICIENT) return;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700204 // Compute offset for outputs 1 and so on
205 {
206 size_t offset = 0;
207 for (auto& i : outputs_info) {
208 i.location.offset = offset;
209 offset += i.location.length;
210 }
211 }
Miao Wanga2d04c82018-02-05 17:26:54 -0800212 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
213 nn::allocateSharedMemory(outputSize)};
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700214 ASSERT_NE(0ull, pools[INPUT].size());
215 ASSERT_NE(0ull, pools[OUTPUT].size());
216
217 // load data
218 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
219 sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]);
220 ASSERT_NE(nullptr, inputMemory.get());
221 ASSERT_NE(nullptr, outputMemory.get());
222 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
223 char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer()));
224 ASSERT_NE(nullptr, inputPtr);
225 ASSERT_NE(nullptr, outputPtr);
226 inputMemory->update();
227 outputMemory->update();
228
229 // Go through all inputs, copy the values
230 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
231 char* begin = (char*)p;
232 char* end = begin + s;
233 // TODO: handle more than one input
234 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
235 });
236
237 inputMemory->commit();
238 outputMemory->commit();
Michael Butlercf22a572017-09-22 13:26:12 -0700239
Michael Butler814d8372019-01-15 11:02:55 -0800240 const Request request = {.inputs = inputs_info, .outputs = outputs_info, .pools = pools};
241
Xusong Wang187c5972018-11-07 09:33:59 -0800242 ErrorStatus executionStatus;
243 hidl_vec<OutputShape> outputShapes;
David Grosse3013492019-01-23 14:01:52 -0800244 Timing timing;
Michael Butler814d8372019-01-15 11:02:55 -0800245 switch (executor) {
246 case Executor::ASYNC: {
247 SCOPED_TRACE("asynchronous");
Michael Butlercf22a572017-09-22 13:26:12 -0700248
Michael Butler814d8372019-01-15 11:02:55 -0800249 // launch execution
250 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
251 ASSERT_NE(nullptr, executionCallback.get());
252 Return<ErrorStatus> executionLaunchStatus =
253 ExecutePreparedModel(preparedModel, request, measure, executionCallback);
254 ASSERT_TRUE(executionLaunchStatus.isOk());
255 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
David Gross49e41672018-12-21 11:20:26 -0800256
Michael Butler814d8372019-01-15 11:02:55 -0800257 // retrieve execution status
258 executionCallback->wait();
259 executionStatus = executionCallback->getStatus();
260 outputShapes = executionCallback->getOutputShapes();
261 timing = executionCallback->getTiming();
David Gross49e41672018-12-21 11:20:26 -0800262
Michael Butler814d8372019-01-15 11:02:55 -0800263 break;
264 }
265 case Executor::SYNC: {
266 SCOPED_TRACE("synchronous");
267
268 // execute
269 Return<ErrorStatus> executionReturnStatus = ExecutePreparedModel(
270 preparedModel, request, measure, &outputShapes, &timing);
271 ASSERT_TRUE(executionReturnStatus.isOk());
272 executionStatus = static_cast<ErrorStatus>(executionReturnStatus);
273
274 break;
275 }
276 case Executor::BURST: {
277 SCOPED_TRACE("burst");
278
279 // create burst
280 const std::unique_ptr<::android::nn::ExecutionBurstController> controller =
281 CreateBurst(preparedModel);
282 ASSERT_NE(nullptr, controller.get());
283
284 // create memory keys
285 std::vector<intptr_t> keys(request.pools.size());
286 for (size_t i = 0; i < keys.size(); ++i) {
287 keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]);
288 }
289
290 // execute burst
291 std::tie(executionStatus, outputShapes, timing) =
292 controller->compute(request, measure, keys);
293
294 break;
295 }
David Gross49e41672018-12-21 11:20:26 -0800296 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700297
Xusong Wang929fd212019-01-27 23:08:12 -0800298 if (outputType != OutputType::FULLY_SPECIFIED &&
299 executionStatus == ErrorStatus::GENERAL_FAILURE) {
Xusong Wanga3165812018-11-19 18:26:08 -0800300 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
301 "execute model that it does not support.";
302 std::cout << "[ ] Early termination of test because vendor service cannot "
303 "execute model that it does not support."
304 << std::endl;
Xusong Wang929fd212019-01-27 23:08:12 -0800305 GTEST_SKIP();
Xusong Wanga3165812018-11-19 18:26:08 -0800306 }
David Grosse3013492019-01-23 14:01:52 -0800307 if (measure == MeasureTiming::NO) {
308 EXPECT_EQ(UINT64_MAX, timing.timeOnDevice);
309 EXPECT_EQ(UINT64_MAX, timing.timeInDriver);
310 } else {
311 if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) {
312 EXPECT_LE(timing.timeOnDevice, timing.timeInDriver);
313 }
314 }
Xusong Wanga3165812018-11-19 18:26:08 -0800315
Xusong Wang929fd212019-01-27 23:08:12 -0800316 switch (outputType) {
317 case OutputType::FULLY_SPECIFIED:
318 // If the model output operands are fully specified, outputShapes must be either
319 // either empty, or have the same number of elements as the number of outputs.
320 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
321 ASSERT_TRUE(outputShapes.size() == 0 ||
322 outputShapes.size() == test.operandDimensions.size());
323 break;
324 case OutputType::UNSPECIFIED:
325 // If the model output operands are not fully specified, outputShapes must have
326 // the same number of elements as the number of outputs.
327 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
328 ASSERT_EQ(outputShapes.size(), test.operandDimensions.size());
329 break;
330 case OutputType::INSUFFICIENT:
331 ASSERT_EQ(ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, executionStatus);
332 ASSERT_EQ(outputShapes.size(), test.operandDimensions.size());
333 ASSERT_FALSE(outputShapes[0].isSufficient);
334 return;
335 }
Xusong Wanga3165812018-11-19 18:26:08 -0800336 // Go through all outputs, overwrite output dimensions with returned output shapes
Xusong Wang929fd212019-01-27 23:08:12 -0800337 if (outputShapes.size() > 0) {
Xusong Wanga3165812018-11-19 18:26:08 -0800338 for_each<uint32_t>(test.operandDimensions,
339 [&outputShapes](int idx, std::vector<uint32_t>& dim) {
340 dim = outputShapes[idx].dimensions;
341 });
342 }
Xusong Wang187c5972018-11-07 09:33:59 -0800343
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700344 // validate results
345 outputMemory->read();
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700346 copy_back(&test, outputs_info, outputPtr);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700347 outputMemory->commit();
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700348 // Filter out don't cares
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -0700349 MixedTyped filtered_golden = filter(golden, is_ignored);
350 MixedTyped filtered_test = filter(test, is_ignored);
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700351
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700352 // We want "close-enough" results for float
Xusong Wang10d77e42018-08-28 16:50:01 -0700353 compare(filtered_golden, filtered_test, fpAtol, fpRtol);
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100354
355 if (example.expectedMultinomialDistributionTolerance > 0) {
356 expectMultinomialDistributionWithinTolerance(test, example);
357 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700358 }
359}
David Gross49e41672018-12-21 11:20:26 -0800360template <typename T_IPreparedModel>
361void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
362 const std::vector<MixedTypedExample>& examples,
Michael Butler814d8372019-01-15 11:02:55 -0800363 bool hasRelaxedFloat32Model, Executor executor, MeasureTiming measure,
Xusong Wang929fd212019-01-27 23:08:12 -0800364 OutputType outputType) {
David Gross49e41672018-12-21 11:20:26 -0800365 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model, kDefaultAtol,
Xusong Wang929fd212019-01-27 23:08:12 -0800366 kDefaultRtol, executor, measure, outputType);
David Gross49e41672018-12-21 11:20:26 -0800367}
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700368
Xusong Wang34058782019-01-18 17:28:26 -0800369void EvaluatePreparedModel(sp<V1_2::IPreparedModel>& preparedModel,
370 std::function<bool(int)> is_ignored,
371 const std::vector<MixedTypedExample>& examples,
372 bool hasRelaxedFloat32Model, bool testDynamicOutputShape) {
373 if (testDynamicOutputShape) {
374 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
375 Executor::ASYNC, MeasureTiming::NO, OutputType::UNSPECIFIED);
376 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
377 Executor::SYNC, MeasureTiming::NO, OutputType::UNSPECIFIED);
378 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
379 Executor::BURST, MeasureTiming::NO, OutputType::UNSPECIFIED);
380 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
381 Executor::ASYNC, MeasureTiming::YES, OutputType::UNSPECIFIED);
382 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
383 Executor::SYNC, MeasureTiming::YES, OutputType::UNSPECIFIED);
384 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
385 Executor::BURST, MeasureTiming::YES, OutputType::UNSPECIFIED);
386 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
387 Executor::ASYNC, MeasureTiming::NO, OutputType::INSUFFICIENT);
388 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
389 Executor::SYNC, MeasureTiming::NO, OutputType::INSUFFICIENT);
390 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
391 Executor::BURST, MeasureTiming::NO, OutputType::INSUFFICIENT);
392 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
393 Executor::ASYNC, MeasureTiming::YES, OutputType::INSUFFICIENT);
394 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
395 Executor::SYNC, MeasureTiming::YES, OutputType::INSUFFICIENT);
396 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
397 Executor::BURST, MeasureTiming::YES, OutputType::INSUFFICIENT);
398 } else {
399 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
400 Executor::ASYNC, MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
401 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
402 Executor::SYNC, MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
403 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
404 Executor::BURST, MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
405 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
406 Executor::ASYNC, MeasureTiming::YES, OutputType::FULLY_SPECIFIED);
407 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
408 Executor::SYNC, MeasureTiming::YES, OutputType::FULLY_SPECIFIED);
409 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
410 Executor::BURST, MeasureTiming::YES, OutputType::FULLY_SPECIFIED);
411 }
412}
413
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700414static void getPreparedModel(sp<PreparedModelCallback> callback,
415 sp<V1_0::IPreparedModel>* preparedModel) {
416 *preparedModel = callback->getPreparedModel();
417}
418static void getPreparedModel(sp<PreparedModelCallback> callback,
419 sp<V1_2::IPreparedModel>* preparedModel) {
420 sp<V1_0::IPreparedModel> preparedModelV1_0 = callback->getPreparedModel();
421 *preparedModel = V1_2::IPreparedModel::castFrom(preparedModelV1_0).withDefault(nullptr);
422}
423
Michael Butlerf76acd02018-03-22 16:37:57 -0700424void Execute(const sp<V1_0::IDevice>& device, std::function<V1_0::Model(void)> create_model,
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100425 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800426 V1_0::Model model = create_model();
427
428 // see if service can handle model
429 bool fullySupportsModel = false;
Miao Wanga2d04c82018-02-05 17:26:54 -0800430 Return<void> supportedCall = device->getSupportedOperations(
Michael Butler4d5bb102018-02-26 15:24:46 -0800431 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
432 ASSERT_EQ(ErrorStatus::NONE, status);
Miao Wanga2d04c82018-02-05 17:26:54 -0800433 ASSERT_NE(0ul, supported.size());
434 fullySupportsModel =
435 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
436 });
437 ASSERT_TRUE(supportedCall.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800438
439 // launch prepare model
440 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
441 ASSERT_NE(nullptr, preparedModelCallback.get());
Miao Wanga2d04c82018-02-05 17:26:54 -0800442 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback);
443 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800444 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wanga2d04c82018-02-05 17:26:54 -0800445
446 // retrieve prepared model
447 preparedModelCallback->wait();
448 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700449 sp<V1_0::IPreparedModel> preparedModel;
450 getPreparedModel(preparedModelCallback, &preparedModel);
Miao Wanga2d04c82018-02-05 17:26:54 -0800451
452 // early termination if vendor service cannot fully prepare model
Michael Butler4d5bb102018-02-26 15:24:46 -0800453 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800454 ASSERT_EQ(nullptr, preparedModel.get());
455 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
456 "prepare model that it does not support.";
457 std::cout << "[ ] Early termination of test because vendor service cannot "
458 "prepare model that it does not support."
459 << std::endl;
Miao Wangbb685a42019-01-08 12:27:35 -0800460 GTEST_SKIP();
Miao Wanga2d04c82018-02-05 17:26:54 -0800461 }
Michael Butler4d5bb102018-02-26 15:24:46 -0800462 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wanga2d04c82018-02-05 17:26:54 -0800463 ASSERT_NE(nullptr, preparedModel.get());
464
Xusong Wang10d77e42018-08-28 16:50:01 -0700465 float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f;
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000466 EvaluatePreparedModel(preparedModel, is_ignored, examples,
Michael Butler814d8372019-01-15 11:02:55 -0800467 /*hasRelaxedFloat32Model=*/false, fpAtol, fpRtol, Executor::ASYNC,
Xusong Wang929fd212019-01-27 23:08:12 -0800468 MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
Miao Wanga2d04c82018-02-05 17:26:54 -0800469}
470
Michael Butlerf76acd02018-03-22 16:37:57 -0700471void Execute(const sp<V1_1::IDevice>& device, std::function<V1_1::Model(void)> create_model,
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100472 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800473 V1_1::Model model = create_model();
474
475 // see if service can handle model
476 bool fullySupportsModel = false;
Miao Wanga2d04c82018-02-05 17:26:54 -0800477 Return<void> supportedCall = device->getSupportedOperations_1_1(
Michael Butler4d5bb102018-02-26 15:24:46 -0800478 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
479 ASSERT_EQ(ErrorStatus::NONE, status);
Miao Wanga2d04c82018-02-05 17:26:54 -0800480 ASSERT_NE(0ul, supported.size());
481 fullySupportsModel =
482 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
483 });
484 ASSERT_TRUE(supportedCall.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800485
486 // launch prepare model
487 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
488 ASSERT_NE(nullptr, preparedModelCallback.get());
Michael Butler2504c2f2018-04-11 16:30:09 -0700489 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_1(
490 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
Miao Wanga2d04c82018-02-05 17:26:54 -0800491 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800492 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wanga2d04c82018-02-05 17:26:54 -0800493
494 // retrieve prepared model
495 preparedModelCallback->wait();
496 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700497 sp<V1_0::IPreparedModel> preparedModel;
498 getPreparedModel(preparedModelCallback, &preparedModel);
Miao Wanga2d04c82018-02-05 17:26:54 -0800499
500 // early termination if vendor service cannot fully prepare model
Michael Butler4d5bb102018-02-26 15:24:46 -0800501 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800502 ASSERT_EQ(nullptr, preparedModel.get());
503 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
504 "prepare model that it does not support.";
505 std::cout << "[ ] Early termination of test because vendor service cannot "
506 "prepare model that it does not support."
507 << std::endl;
Miao Wangbb685a42019-01-08 12:27:35 -0800508 GTEST_SKIP();
Miao Wanga2d04c82018-02-05 17:26:54 -0800509 }
Michael Butler4d5bb102018-02-26 15:24:46 -0800510 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wanga2d04c82018-02-05 17:26:54 -0800511 ASSERT_NE(nullptr, preparedModel.get());
512
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000513 EvaluatePreparedModel(preparedModel, is_ignored, examples,
Michael Butler814d8372019-01-15 11:02:55 -0800514 model.relaxComputationFloat32toFloat16, 1e-5f, 1e-5f, Executor::ASYNC,
Xusong Wang929fd212019-01-27 23:08:12 -0800515 MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
Miao Wanga2d04c82018-02-05 17:26:54 -0800516}
517
Xusong Wang34058782019-01-18 17:28:26 -0800518void PrepareModel(const sp<V1_2::IDevice>& device, const V1_2::Model& model,
519 sp<V1_2::IPreparedModel>* preparedModel) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100520 // see if service can handle model
521 bool fullySupportsModel = false;
522 Return<void> supportedCall = device->getSupportedOperations_1_2(
523 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
524 ASSERT_EQ(ErrorStatus::NONE, status);
525 ASSERT_NE(0ul, supported.size());
526 fullySupportsModel =
527 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
528 });
529 ASSERT_TRUE(supportedCall.isOk());
530
531 // launch prepare model
532 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
533 ASSERT_NE(nullptr, preparedModelCallback.get());
534 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2(
535 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
536 ASSERT_TRUE(prepareLaunchStatus.isOk());
537 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
538
539 // retrieve prepared model
540 preparedModelCallback->wait();
541 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wang34058782019-01-18 17:28:26 -0800542 getPreparedModel(preparedModelCallback, preparedModel);
Slava Shklyaev871be942018-09-12 14:52:02 +0100543
544 // early termination if vendor service cannot fully prepare model
545 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Xusong Wang34058782019-01-18 17:28:26 -0800546 ASSERT_EQ(nullptr, preparedModel->get());
Slava Shklyaev871be942018-09-12 14:52:02 +0100547 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
548 "prepare model that it does not support.";
549 std::cout << "[ ] Early termination of test because vendor service cannot "
550 "prepare model that it does not support."
551 << std::endl;
Miao Wang4135a8e2019-02-01 14:00:08 -0800552 return;
Slava Shklyaev871be942018-09-12 14:52:02 +0100553 }
554 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Xusong Wang34058782019-01-18 17:28:26 -0800555 ASSERT_NE(nullptr, preparedModel->get());
556}
Slava Shklyaev871be942018-09-12 14:52:02 +0100557
Xusong Wang34058782019-01-18 17:28:26 -0800558// TODO: Reduce code duplication.
559void Execute(const sp<V1_2::IDevice>& device, std::function<V1_2::Model(void)> create_model,
560 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples,
561 bool testDynamicOutputShape) {
562 V1_2::Model model = create_model();
563 sp<V1_2::IPreparedModel> preparedModel = nullptr;
564 PrepareModel(device, model, &preparedModel);
Miao Wang4135a8e2019-02-01 14:00:08 -0800565 if (preparedModel == nullptr) {
566 GTEST_SKIP();
567 }
Xusong Wang34058782019-01-18 17:28:26 -0800568 EvaluatePreparedModel(preparedModel, is_ignored, examples,
569 model.relaxComputationFloat32toFloat16, testDynamicOutputShape);
Slava Shklyaev871be942018-09-12 14:52:02 +0100570}
571
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700572} // namespace generated_tests
573
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700574} // namespace neuralnetworks
575} // namespace hardware
576} // namespace android