blob: 2e1385497b0b7fec48b5b2e8020924eef930cb97 [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);
75 static_assert(8 == MixedTyped::kNumTypes,
Lev Proleev9b490f42018-11-02 12:44:11 +000076 "Number of types in MixedTyped changed, but copy_back function wasn't updated");
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070077}
78
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070079// Top level driver for models and examples generated by test_generator.py
80// Test driver for those generated from ml/nn/runtime/test/spec
Xusong Wangb5cb8f72018-10-31 08:43:12 -070081static Return<ErrorStatus> ExecutePreparedModel(sp<V1_0::IPreparedModel>& preparedModel,
David Grosse3013492019-01-23 14:01:52 -080082 const Request& request, MeasureTiming,
Xusong Wangb5cb8f72018-10-31 08:43:12 -070083 sp<ExecutionCallback>& callback) {
84 return preparedModel->execute(request, callback);
85}
86static Return<ErrorStatus> ExecutePreparedModel(sp<V1_2::IPreparedModel>& preparedModel,
David Grosse3013492019-01-23 14:01:52 -080087 const Request& request, MeasureTiming measure,
Xusong Wangb5cb8f72018-10-31 08:43:12 -070088 sp<ExecutionCallback>& callback) {
David Grosse3013492019-01-23 14:01:52 -080089 return preparedModel->execute_1_2(request, measure, callback);
Xusong Wangb5cb8f72018-10-31 08:43:12 -070090}
Xusong Wang187c5972018-11-07 09:33:59 -080091static Return<ErrorStatus> ExecutePreparedModel(sp<V1_0::IPreparedModel>&, const Request&,
David Grosse3013492019-01-23 14:01:52 -080092 MeasureTiming, hidl_vec<OutputShape>*, Timing*) {
David Gross49e41672018-12-21 11:20:26 -080093 ADD_FAILURE() << "asking for synchronous execution at V1_0";
94 return ErrorStatus::GENERAL_FAILURE;
95}
96static Return<ErrorStatus> ExecutePreparedModel(sp<V1_2::IPreparedModel>& preparedModel,
David Grosse3013492019-01-23 14:01:52 -080097 const Request& request, MeasureTiming measure,
98 hidl_vec<OutputShape>* outputShapes,
99 Timing* timing) {
Xusong Wang187c5972018-11-07 09:33:59 -0800100 ErrorStatus result;
101 Return<void> ret = preparedModel->executeSynchronously(
David Grosse3013492019-01-23 14:01:52 -0800102 request, measure,
103 [&result, outputShapes, timing](ErrorStatus error, const hidl_vec<OutputShape>& shapes,
104 const Timing& time) {
105 result = error;
106 *outputShapes = shapes;
107 *timing = time;
108 });
Xusong Wang187c5972018-11-07 09:33:59 -0800109 if (!ret.isOk()) {
110 return ErrorStatus::GENERAL_FAILURE;
111 }
112 return result;
David Gross49e41672018-12-21 11:20:26 -0800113}
Michael Butler814d8372019-01-15 11:02:55 -0800114static std::unique_ptr<::android::nn::ExecutionBurstController> CreateBurst(
115 const sp<V1_0::IPreparedModel>&) {
116 ADD_FAILURE() << "asking for burst execution at V1_0";
117 return nullptr;
118}
119static std::unique_ptr<::android::nn::ExecutionBurstController> CreateBurst(
120 const sp<V1_2::IPreparedModel>& preparedModel) {
121 return ::android::nn::createExecutionBurstController(preparedModel, /*blocking=*/true);
122}
123enum class Executor { ASYNC, SYNC, BURST };
Xusong Wang929fd212019-01-27 23:08:12 -0800124enum class OutputType { FULLY_SPECIFIED, UNSPECIFIED, INSUFFICIENT };
David Gross49e41672018-12-21 11:20:26 -0800125const float kDefaultAtol = 1e-5f;
126const float kDefaultRtol = 1e-5f;
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700127template <typename T_IPreparedModel>
128void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000129 const std::vector<MixedTypedExample>& examples,
David Grosse3013492019-01-23 14:01:52 -0800130 bool hasRelaxedFloat32Model, float fpAtol, float fpRtol,
Xusong Wang929fd212019-01-27 23:08:12 -0800131 Executor executor, MeasureTiming measure, OutputType outputType) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700132 const uint32_t INPUT = 0;
133 const uint32_t OUTPUT = 1;
134
135 int example_no = 1;
136 for (auto& example : examples) {
137 SCOPED_TRACE(example_no++);
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100138 const MixedTyped& inputs = example.operands.first;
139 const MixedTyped& golden = example.operands.second;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700140
Xusong Wanga3165812018-11-19 18:26:08 -0800141 const bool hasFloat16Inputs = !inputs.float16Operands.empty();
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000142 if (hasRelaxedFloat32Model || hasFloat16Inputs) {
143 // TODO: Adjust the error limit based on testing.
144 // If in relaxed mode, set the absolute tolerance to be 5ULP of FP16.
145 fpAtol = 5.0f * 0.0009765625f;
146 // Set the relative tolerance to be 5ULP of the corresponding FP precision.
147 fpRtol = 5.0f * 0.0009765625f;
148 }
149
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700150 std::vector<RequestArgument> inputs_info, outputs_info;
151 uint32_t inputSize = 0, outputSize = 0;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700152 // This function only partially specifies the metadata (vector of RequestArguments).
153 // The contents are copied over below.
154 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
155 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
156 RequestArgument arg = {
157 .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
158 .dimensions = {},
159 };
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700160 RequestArgument arg_empty = {
161 .hasNoValue = true,
162 };
163 inputs_info[index] = s ? arg : arg_empty;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700164 inputSize += s;
165 });
166 // Compute offset for inputs 1 and so on
167 {
168 size_t offset = 0;
169 for (auto& i : inputs_info) {
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700170 if (!i.hasNoValue) i.location.offset = offset;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700171 offset += i.location.length;
172 }
173 }
174
175 MixedTyped test; // holding test results
176
177 // Go through all outputs, initialize RequestArgument descriptors
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700178 resize_accordingly(golden, test);
Xusong Wang929fd212019-01-27 23:08:12 -0800179 bool sizeLargerThanOne = true;
180 for_all(golden, [&outputs_info, &outputSize, &outputType, &sizeLargerThanOne](
181 int index, auto, auto s) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700182 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
Xusong Wang929fd212019-01-27 23:08:12 -0800183 if (index == 0) {
184 // On OutputType::INSUFFICIENT, set the output operand with index 0 with
185 // buffer size one byte less than needed.
186 if (outputType == OutputType::INSUFFICIENT) {
187 if (s > 1)
188 s -= 1;
189 else
190 sizeLargerThanOne = false;
191 }
192 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700193 RequestArgument arg = {
194 .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
195 .dimensions = {},
196 };
197 outputs_info[index] = arg;
198 outputSize += s;
199 });
Xusong Wang929fd212019-01-27 23:08:12 -0800200 // If output0 does not have size larger than one byte,
201 // we can not provide an insufficient buffer
202 if (!sizeLargerThanOne && outputType == OutputType::INSUFFICIENT) return;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700203 // Compute offset for outputs 1 and so on
204 {
205 size_t offset = 0;
206 for (auto& i : outputs_info) {
207 i.location.offset = offset;
208 offset += i.location.length;
209 }
210 }
Miao Wanga2d04c82018-02-05 17:26:54 -0800211 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
212 nn::allocateSharedMemory(outputSize)};
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700213 ASSERT_NE(0ull, pools[INPUT].size());
214 ASSERT_NE(0ull, pools[OUTPUT].size());
215
216 // load data
217 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
218 sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]);
219 ASSERT_NE(nullptr, inputMemory.get());
220 ASSERT_NE(nullptr, outputMemory.get());
221 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
222 char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer()));
223 ASSERT_NE(nullptr, inputPtr);
224 ASSERT_NE(nullptr, outputPtr);
225 inputMemory->update();
226 outputMemory->update();
227
228 // Go through all inputs, copy the values
229 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
230 char* begin = (char*)p;
231 char* end = begin + s;
232 // TODO: handle more than one input
233 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
234 });
235
236 inputMemory->commit();
237 outputMemory->commit();
Michael Butlercf22a572017-09-22 13:26:12 -0700238
Michael Butler814d8372019-01-15 11:02:55 -0800239 const Request request = {.inputs = inputs_info, .outputs = outputs_info, .pools = pools};
240
Xusong Wang187c5972018-11-07 09:33:59 -0800241 ErrorStatus executionStatus;
242 hidl_vec<OutputShape> outputShapes;
David Grosse3013492019-01-23 14:01:52 -0800243 Timing timing;
Michael Butler814d8372019-01-15 11:02:55 -0800244 switch (executor) {
245 case Executor::ASYNC: {
246 SCOPED_TRACE("asynchronous");
Michael Butlercf22a572017-09-22 13:26:12 -0700247
Michael Butler814d8372019-01-15 11:02:55 -0800248 // launch execution
249 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
250 ASSERT_NE(nullptr, executionCallback.get());
251 Return<ErrorStatus> executionLaunchStatus =
252 ExecutePreparedModel(preparedModel, request, measure, executionCallback);
253 ASSERT_TRUE(executionLaunchStatus.isOk());
254 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
David Gross49e41672018-12-21 11:20:26 -0800255
Michael Butler814d8372019-01-15 11:02:55 -0800256 // retrieve execution status
257 executionCallback->wait();
258 executionStatus = executionCallback->getStatus();
259 outputShapes = executionCallback->getOutputShapes();
260 timing = executionCallback->getTiming();
David Gross49e41672018-12-21 11:20:26 -0800261
Michael Butler814d8372019-01-15 11:02:55 -0800262 break;
263 }
264 case Executor::SYNC: {
265 SCOPED_TRACE("synchronous");
266
267 // execute
268 Return<ErrorStatus> executionReturnStatus = ExecutePreparedModel(
269 preparedModel, request, measure, &outputShapes, &timing);
270 ASSERT_TRUE(executionReturnStatus.isOk());
271 executionStatus = static_cast<ErrorStatus>(executionReturnStatus);
272
273 break;
274 }
275 case Executor::BURST: {
276 SCOPED_TRACE("burst");
277
278 // create burst
279 const std::unique_ptr<::android::nn::ExecutionBurstController> controller =
280 CreateBurst(preparedModel);
281 ASSERT_NE(nullptr, controller.get());
282
283 // create memory keys
284 std::vector<intptr_t> keys(request.pools.size());
285 for (size_t i = 0; i < keys.size(); ++i) {
286 keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]);
287 }
288
289 // execute burst
290 std::tie(executionStatus, outputShapes, timing) =
291 controller->compute(request, measure, keys);
292
293 break;
294 }
David Gross49e41672018-12-21 11:20:26 -0800295 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700296
Xusong Wang929fd212019-01-27 23:08:12 -0800297 if (outputType != OutputType::FULLY_SPECIFIED &&
298 executionStatus == ErrorStatus::GENERAL_FAILURE) {
Xusong Wanga3165812018-11-19 18:26:08 -0800299 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
300 "execute model that it does not support.";
301 std::cout << "[ ] Early termination of test because vendor service cannot "
302 "execute model that it does not support."
303 << std::endl;
Xusong Wang929fd212019-01-27 23:08:12 -0800304 GTEST_SKIP();
Xusong Wanga3165812018-11-19 18:26:08 -0800305 }
David Grosse3013492019-01-23 14:01:52 -0800306 if (measure == MeasureTiming::NO) {
307 EXPECT_EQ(UINT64_MAX, timing.timeOnDevice);
308 EXPECT_EQ(UINT64_MAX, timing.timeInDriver);
309 } else {
310 if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) {
311 EXPECT_LE(timing.timeOnDevice, timing.timeInDriver);
312 }
313 }
Xusong Wanga3165812018-11-19 18:26:08 -0800314
Xusong Wang929fd212019-01-27 23:08:12 -0800315 switch (outputType) {
316 case OutputType::FULLY_SPECIFIED:
317 // If the model output operands are fully specified, outputShapes must be either
318 // either empty, or have the same number of elements as the number of outputs.
319 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
320 ASSERT_TRUE(outputShapes.size() == 0 ||
321 outputShapes.size() == test.operandDimensions.size());
322 break;
323 case OutputType::UNSPECIFIED:
324 // If the model output operands are not fully specified, outputShapes must have
325 // the same number of elements as the number of outputs.
326 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
327 ASSERT_EQ(outputShapes.size(), test.operandDimensions.size());
328 break;
329 case OutputType::INSUFFICIENT:
330 ASSERT_EQ(ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, executionStatus);
331 ASSERT_EQ(outputShapes.size(), test.operandDimensions.size());
332 ASSERT_FALSE(outputShapes[0].isSufficient);
333 return;
334 }
Xusong Wanga3165812018-11-19 18:26:08 -0800335 // Go through all outputs, overwrite output dimensions with returned output shapes
Xusong Wang929fd212019-01-27 23:08:12 -0800336 if (outputShapes.size() > 0) {
Xusong Wanga3165812018-11-19 18:26:08 -0800337 for_each<uint32_t>(test.operandDimensions,
338 [&outputShapes](int idx, std::vector<uint32_t>& dim) {
339 dim = outputShapes[idx].dimensions;
340 });
341 }
Xusong Wang187c5972018-11-07 09:33:59 -0800342
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700343 // validate results
344 outputMemory->read();
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700345 copy_back(&test, outputs_info, outputPtr);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700346 outputMemory->commit();
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700347 // Filter out don't cares
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -0700348 MixedTyped filtered_golden = filter(golden, is_ignored);
349 MixedTyped filtered_test = filter(test, is_ignored);
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700350
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700351 // We want "close-enough" results for float
Xusong Wang10d77e42018-08-28 16:50:01 -0700352 compare(filtered_golden, filtered_test, fpAtol, fpRtol);
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100353
354 if (example.expectedMultinomialDistributionTolerance > 0) {
355 expectMultinomialDistributionWithinTolerance(test, example);
356 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700357 }
358}
David Gross49e41672018-12-21 11:20:26 -0800359template <typename T_IPreparedModel>
360void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
361 const std::vector<MixedTypedExample>& examples,
Michael Butler814d8372019-01-15 11:02:55 -0800362 bool hasRelaxedFloat32Model, Executor executor, MeasureTiming measure,
Xusong Wang929fd212019-01-27 23:08:12 -0800363 OutputType outputType) {
David Gross49e41672018-12-21 11:20:26 -0800364 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model, kDefaultAtol,
Xusong Wang929fd212019-01-27 23:08:12 -0800365 kDefaultRtol, executor, measure, outputType);
David Gross49e41672018-12-21 11:20:26 -0800366}
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700367
Xusong Wang34058782019-01-18 17:28:26 -0800368void EvaluatePreparedModel(sp<V1_2::IPreparedModel>& preparedModel,
369 std::function<bool(int)> is_ignored,
370 const std::vector<MixedTypedExample>& examples,
371 bool hasRelaxedFloat32Model, bool testDynamicOutputShape) {
372 if (testDynamicOutputShape) {
373 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
374 Executor::ASYNC, MeasureTiming::NO, OutputType::UNSPECIFIED);
375 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
376 Executor::SYNC, MeasureTiming::NO, OutputType::UNSPECIFIED);
377 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
378 Executor::BURST, MeasureTiming::NO, OutputType::UNSPECIFIED);
379 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
380 Executor::ASYNC, MeasureTiming::YES, OutputType::UNSPECIFIED);
381 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
382 Executor::SYNC, MeasureTiming::YES, OutputType::UNSPECIFIED);
383 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
384 Executor::BURST, MeasureTiming::YES, OutputType::UNSPECIFIED);
385 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
386 Executor::ASYNC, MeasureTiming::NO, OutputType::INSUFFICIENT);
387 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
388 Executor::SYNC, MeasureTiming::NO, OutputType::INSUFFICIENT);
389 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
390 Executor::BURST, MeasureTiming::NO, OutputType::INSUFFICIENT);
391 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
392 Executor::ASYNC, MeasureTiming::YES, OutputType::INSUFFICIENT);
393 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
394 Executor::SYNC, MeasureTiming::YES, OutputType::INSUFFICIENT);
395 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
396 Executor::BURST, MeasureTiming::YES, OutputType::INSUFFICIENT);
397 } else {
398 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
399 Executor::ASYNC, MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
400 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
401 Executor::SYNC, MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
402 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
403 Executor::BURST, MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
404 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
405 Executor::ASYNC, MeasureTiming::YES, OutputType::FULLY_SPECIFIED);
406 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
407 Executor::SYNC, MeasureTiming::YES, OutputType::FULLY_SPECIFIED);
408 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model,
409 Executor::BURST, MeasureTiming::YES, OutputType::FULLY_SPECIFIED);
410 }
411}
412
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700413static void getPreparedModel(sp<PreparedModelCallback> callback,
414 sp<V1_0::IPreparedModel>* preparedModel) {
415 *preparedModel = callback->getPreparedModel();
416}
417static void getPreparedModel(sp<PreparedModelCallback> callback,
418 sp<V1_2::IPreparedModel>* preparedModel) {
419 sp<V1_0::IPreparedModel> preparedModelV1_0 = callback->getPreparedModel();
420 *preparedModel = V1_2::IPreparedModel::castFrom(preparedModelV1_0).withDefault(nullptr);
421}
422
Michael Butlerf76acd02018-03-22 16:37:57 -0700423void Execute(const sp<V1_0::IDevice>& device, std::function<V1_0::Model(void)> create_model,
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100424 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800425 V1_0::Model model = create_model();
426
427 // see if service can handle model
428 bool fullySupportsModel = false;
Miao Wanga2d04c82018-02-05 17:26:54 -0800429 Return<void> supportedCall = device->getSupportedOperations(
Michael Butler4d5bb102018-02-26 15:24:46 -0800430 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
431 ASSERT_EQ(ErrorStatus::NONE, status);
Miao Wanga2d04c82018-02-05 17:26:54 -0800432 ASSERT_NE(0ul, supported.size());
433 fullySupportsModel =
434 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
435 });
436 ASSERT_TRUE(supportedCall.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800437
438 // launch prepare model
439 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
440 ASSERT_NE(nullptr, preparedModelCallback.get());
Miao Wanga2d04c82018-02-05 17:26:54 -0800441 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback);
442 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800443 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wanga2d04c82018-02-05 17:26:54 -0800444
445 // retrieve prepared model
446 preparedModelCallback->wait();
447 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700448 sp<V1_0::IPreparedModel> preparedModel;
449 getPreparedModel(preparedModelCallback, &preparedModel);
Miao Wanga2d04c82018-02-05 17:26:54 -0800450
451 // early termination if vendor service cannot fully prepare model
Michael Butler4d5bb102018-02-26 15:24:46 -0800452 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800453 ASSERT_EQ(nullptr, preparedModel.get());
454 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
455 "prepare model that it does not support.";
456 std::cout << "[ ] Early termination of test because vendor service cannot "
457 "prepare model that it does not support."
458 << std::endl;
Miao Wangbb685a42019-01-08 12:27:35 -0800459 GTEST_SKIP();
Miao Wanga2d04c82018-02-05 17:26:54 -0800460 }
Michael Butler4d5bb102018-02-26 15:24:46 -0800461 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wanga2d04c82018-02-05 17:26:54 -0800462 ASSERT_NE(nullptr, preparedModel.get());
463
Xusong Wang10d77e42018-08-28 16:50:01 -0700464 float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f;
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000465 EvaluatePreparedModel(preparedModel, is_ignored, examples,
Michael Butler814d8372019-01-15 11:02:55 -0800466 /*hasRelaxedFloat32Model=*/false, fpAtol, fpRtol, Executor::ASYNC,
Xusong Wang929fd212019-01-27 23:08:12 -0800467 MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
Miao Wanga2d04c82018-02-05 17:26:54 -0800468}
469
Michael Butlerf76acd02018-03-22 16:37:57 -0700470void Execute(const sp<V1_1::IDevice>& device, std::function<V1_1::Model(void)> create_model,
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100471 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800472 V1_1::Model model = create_model();
473
474 // see if service can handle model
475 bool fullySupportsModel = false;
Miao Wanga2d04c82018-02-05 17:26:54 -0800476 Return<void> supportedCall = device->getSupportedOperations_1_1(
Michael Butler4d5bb102018-02-26 15:24:46 -0800477 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
478 ASSERT_EQ(ErrorStatus::NONE, status);
Miao Wanga2d04c82018-02-05 17:26:54 -0800479 ASSERT_NE(0ul, supported.size());
480 fullySupportsModel =
481 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
482 });
483 ASSERT_TRUE(supportedCall.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800484
485 // launch prepare model
486 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
487 ASSERT_NE(nullptr, preparedModelCallback.get());
Michael Butler2504c2f2018-04-11 16:30:09 -0700488 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_1(
489 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
Miao Wanga2d04c82018-02-05 17:26:54 -0800490 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800491 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wanga2d04c82018-02-05 17:26:54 -0800492
493 // retrieve prepared model
494 preparedModelCallback->wait();
495 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700496 sp<V1_0::IPreparedModel> preparedModel;
497 getPreparedModel(preparedModelCallback, &preparedModel);
Miao Wanga2d04c82018-02-05 17:26:54 -0800498
499 // early termination if vendor service cannot fully prepare model
Michael Butler4d5bb102018-02-26 15:24:46 -0800500 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800501 ASSERT_EQ(nullptr, preparedModel.get());
502 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
503 "prepare model that it does not support.";
504 std::cout << "[ ] Early termination of test because vendor service cannot "
505 "prepare model that it does not support."
506 << std::endl;
Miao Wangbb685a42019-01-08 12:27:35 -0800507 GTEST_SKIP();
Miao Wanga2d04c82018-02-05 17:26:54 -0800508 }
Michael Butler4d5bb102018-02-26 15:24:46 -0800509 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wanga2d04c82018-02-05 17:26:54 -0800510 ASSERT_NE(nullptr, preparedModel.get());
511
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000512 EvaluatePreparedModel(preparedModel, is_ignored, examples,
Michael Butler814d8372019-01-15 11:02:55 -0800513 model.relaxComputationFloat32toFloat16, 1e-5f, 1e-5f, Executor::ASYNC,
Xusong Wang929fd212019-01-27 23:08:12 -0800514 MeasureTiming::NO, OutputType::FULLY_SPECIFIED);
Miao Wanga2d04c82018-02-05 17:26:54 -0800515}
516
Xusong Wang34058782019-01-18 17:28:26 -0800517void PrepareModel(const sp<V1_2::IDevice>& device, const V1_2::Model& model,
518 sp<V1_2::IPreparedModel>* preparedModel) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100519 // see if service can handle model
520 bool fullySupportsModel = false;
521 Return<void> supportedCall = device->getSupportedOperations_1_2(
522 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
523 ASSERT_EQ(ErrorStatus::NONE, status);
524 ASSERT_NE(0ul, supported.size());
525 fullySupportsModel =
526 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
527 });
528 ASSERT_TRUE(supportedCall.isOk());
529
530 // launch prepare model
531 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
532 ASSERT_NE(nullptr, preparedModelCallback.get());
533 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2(
534 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
535 ASSERT_TRUE(prepareLaunchStatus.isOk());
536 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
537
538 // retrieve prepared model
539 preparedModelCallback->wait();
540 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wang34058782019-01-18 17:28:26 -0800541 getPreparedModel(preparedModelCallback, preparedModel);
Slava Shklyaev871be942018-09-12 14:52:02 +0100542
543 // early termination if vendor service cannot fully prepare model
544 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Xusong Wang34058782019-01-18 17:28:26 -0800545 ASSERT_EQ(nullptr, preparedModel->get());
Slava Shklyaev871be942018-09-12 14:52:02 +0100546 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
547 "prepare model that it does not support.";
548 std::cout << "[ ] Early termination of test because vendor service cannot "
549 "prepare model that it does not support."
550 << std::endl;
Miao Wangbb685a42019-01-08 12:27:35 -0800551 GTEST_SKIP();
Slava Shklyaev871be942018-09-12 14:52:02 +0100552 }
553 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Xusong Wang34058782019-01-18 17:28:26 -0800554 ASSERT_NE(nullptr, preparedModel->get());
555}
Slava Shklyaev871be942018-09-12 14:52:02 +0100556
Xusong Wang34058782019-01-18 17:28:26 -0800557// TODO: Reduce code duplication.
558void Execute(const sp<V1_2::IDevice>& device, std::function<V1_2::Model(void)> create_model,
559 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples,
560 bool testDynamicOutputShape) {
561 V1_2::Model model = create_model();
562 sp<V1_2::IPreparedModel> preparedModel = nullptr;
563 PrepareModel(device, model, &preparedModel);
564 EvaluatePreparedModel(preparedModel, is_ignored, examples,
565 model.relaxComputationFloat32toFloat16, testDynamicOutputShape);
Slava Shklyaev871be942018-09-12 14:52:02 +0100566}
567
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700568} // namespace generated_tests
569
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700570} // namespace neuralnetworks
571} // namespace hardware
572} // namespace android