blob: de0e494bf882a295c4c2ed1d4a2295608b13b170 [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>
Xusong Wangb5cb8f72018-10-31 08:43:12 -070027#include <android/hardware/neuralnetworks/1.1/IDevice.h>
28#include <android/hardware/neuralnetworks/1.2/IDevice.h>
29#include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h>
30#include <android/hardware/neuralnetworks/1.2/IPreparedModel.h>
31#include <android/hardware/neuralnetworks/1.2/IPreparedModelCallback.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080032#include <android/hidl/allocator/1.0/IAllocator.h>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070033#include <android/hidl/memory/1.0/IMemory.h>
34#include <hidlmemory/mapping.h>
Michael Butler0897ab32017-10-04 02:38:42 -070035#include <iostream>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070036
37namespace android {
38namespace hardware {
39namespace neuralnetworks {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070040
41namespace generated_tests {
Xusong Wangb5cb8f72018-10-31 08:43:12 -070042using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
43using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback;
Slava Shklyaev9e3fad12018-11-30 17:55:12 +000044using ::test_helper::bool8;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010045using ::test_helper::compare;
46using ::test_helper::expectMultinomialDistributionWithinTolerance;
Mika Raentode166942018-04-17 16:49:50 +010047using ::test_helper::filter;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010048using ::test_helper::Float32Operands;
Mika Raentode166942018-04-17 16:49:50 +010049using ::test_helper::for_all;
50using ::test_helper::for_each;
Mika Raentode166942018-04-17 16:49:50 +010051using ::test_helper::Int32Operands;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010052using ::test_helper::MixedTyped;
53using ::test_helper::MixedTypedExample;
Mika Raentode166942018-04-17 16:49:50 +010054using ::test_helper::Quant8Operands;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010055using ::test_helper::resize_accordingly;
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070056
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -070057template <typename T>
Xusong Wanga3165812018-11-19 18:26:08 -080058void copy_back_(std::map<int, std::vector<T>>* dst, const std::vector<RequestArgument>& ra,
59 char* src) {
60 for_each<T>(*dst, [&ra, src](int index, std::vector<T>& m) {
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -070061 ASSERT_EQ(m.size(), ra[index].location.length / sizeof(T));
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070062 char* begin = src + ra[index].location.offset;
63 memcpy(m.data(), begin, ra[index].location.length);
64 });
65}
66
67void copy_back(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) {
Xusong Wanga3165812018-11-19 18:26:08 -080068 copy_back_(&dst->float32Operands, ra, src);
69 copy_back_(&dst->int32Operands, ra, src);
70 copy_back_(&dst->quant8Operands, ra, src);
71 copy_back_(&dst->quant16Operands, ra, src);
72 copy_back_(&dst->float16Operands, ra, src);
73 copy_back_(&dst->bool8Operands, ra, src);
74 copy_back_(&dst->quant8ChannelOperands, ra, src);
75 static_assert(7 == 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,
82 const Request& request,
83 sp<ExecutionCallback>& callback) {
84 return preparedModel->execute(request, callback);
85}
86static Return<ErrorStatus> ExecutePreparedModel(sp<V1_2::IPreparedModel>& preparedModel,
87 const Request& request,
88 sp<ExecutionCallback>& callback) {
89 return preparedModel->execute_1_2(request, callback);
90}
Xusong Wang187c5972018-11-07 09:33:59 -080091static Return<ErrorStatus> ExecutePreparedModel(sp<V1_0::IPreparedModel>&, const Request&,
92 hidl_vec<OutputShape>*) {
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,
Xusong Wang187c5972018-11-07 09:33:59 -080097 const Request& request,
98 hidl_vec<OutputShape>* outputShapes) {
99 ErrorStatus result;
100 Return<void> ret = preparedModel->executeSynchronously(
101 request, [&result, &outputShapes](ErrorStatus error, const hidl_vec<OutputShape>& shapes) {
102 result = error;
103 *outputShapes = shapes;
104 });
105 if (!ret.isOk()) {
106 return ErrorStatus::GENERAL_FAILURE;
107 }
108 return result;
David Gross49e41672018-12-21 11:20:26 -0800109}
110enum class Synchronously { NO, YES };
111const float kDefaultAtol = 1e-5f;
112const float kDefaultRtol = 1e-5f;
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700113template <typename T_IPreparedModel>
114void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000115 const std::vector<MixedTypedExample>& examples,
David Gross49e41672018-12-21 11:20:26 -0800116 bool hasRelaxedFloat32Model = false, float fpAtol = kDefaultAtol,
Xusong Wanga3165812018-11-19 18:26:08 -0800117 float fpRtol = kDefaultRtol, Synchronously sync = Synchronously::NO,
118 bool testDynamicOutputShape = false) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700119 const uint32_t INPUT = 0;
120 const uint32_t OUTPUT = 1;
121
122 int example_no = 1;
123 for (auto& example : examples) {
124 SCOPED_TRACE(example_no++);
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100125 const MixedTyped& inputs = example.operands.first;
126 const MixedTyped& golden = example.operands.second;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700127
Xusong Wanga3165812018-11-19 18:26:08 -0800128 const bool hasFloat16Inputs = !inputs.float16Operands.empty();
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000129 if (hasRelaxedFloat32Model || hasFloat16Inputs) {
130 // TODO: Adjust the error limit based on testing.
131 // If in relaxed mode, set the absolute tolerance to be 5ULP of FP16.
132 fpAtol = 5.0f * 0.0009765625f;
133 // Set the relative tolerance to be 5ULP of the corresponding FP precision.
134 fpRtol = 5.0f * 0.0009765625f;
135 }
136
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700137 std::vector<RequestArgument> inputs_info, outputs_info;
138 uint32_t inputSize = 0, outputSize = 0;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700139 // This function only partially specifies the metadata (vector of RequestArguments).
140 // The contents are copied over below.
141 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
142 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
143 RequestArgument arg = {
144 .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
145 .dimensions = {},
146 };
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700147 RequestArgument arg_empty = {
148 .hasNoValue = true,
149 };
150 inputs_info[index] = s ? arg : arg_empty;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700151 inputSize += s;
152 });
153 // Compute offset for inputs 1 and so on
154 {
155 size_t offset = 0;
156 for (auto& i : inputs_info) {
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700157 if (!i.hasNoValue) i.location.offset = offset;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700158 offset += i.location.length;
159 }
160 }
161
162 MixedTyped test; // holding test results
163
164 // Go through all outputs, initialize RequestArgument descriptors
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700165 resize_accordingly(golden, test);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700166 for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) {
167 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
168 RequestArgument arg = {
169 .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
170 .dimensions = {},
171 };
172 outputs_info[index] = arg;
173 outputSize += s;
174 });
175 // Compute offset for outputs 1 and so on
176 {
177 size_t offset = 0;
178 for (auto& i : outputs_info) {
179 i.location.offset = offset;
180 offset += i.location.length;
181 }
182 }
Miao Wanga2d04c82018-02-05 17:26:54 -0800183 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
184 nn::allocateSharedMemory(outputSize)};
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700185 ASSERT_NE(0ull, pools[INPUT].size());
186 ASSERT_NE(0ull, pools[OUTPUT].size());
187
188 // load data
189 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
190 sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]);
191 ASSERT_NE(nullptr, inputMemory.get());
192 ASSERT_NE(nullptr, outputMemory.get());
193 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
194 char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer()));
195 ASSERT_NE(nullptr, inputPtr);
196 ASSERT_NE(nullptr, outputPtr);
197 inputMemory->update();
198 outputMemory->update();
199
200 // Go through all inputs, copy the values
201 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
202 char* begin = (char*)p;
203 char* end = begin + s;
204 // TODO: handle more than one input
205 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
206 });
207
208 inputMemory->commit();
209 outputMemory->commit();
Michael Butlercf22a572017-09-22 13:26:12 -0700210
Xusong Wang187c5972018-11-07 09:33:59 -0800211 ErrorStatus executionStatus;
212 hidl_vec<OutputShape> outputShapes;
David Gross49e41672018-12-21 11:20:26 -0800213 if (sync == Synchronously::NO) {
214 SCOPED_TRACE("asynchronous");
Michael Butlercf22a572017-09-22 13:26:12 -0700215
David Gross49e41672018-12-21 11:20:26 -0800216 // launch execution
217 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
218 ASSERT_NE(nullptr, executionCallback.get());
219 Return<ErrorStatus> executionLaunchStatus = ExecutePreparedModel(
220 preparedModel, {.inputs = inputs_info, .outputs = outputs_info, .pools = pools},
221 executionCallback);
222 ASSERT_TRUE(executionLaunchStatus.isOk());
223 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
224
225 // retrieve execution status
226 executionCallback->wait();
Xusong Wang187c5972018-11-07 09:33:59 -0800227 executionStatus = executionCallback->getStatus();
228 outputShapes = executionCallback->getOutputShapes();
David Gross49e41672018-12-21 11:20:26 -0800229 } else {
230 SCOPED_TRACE("synchronous");
231
232 // execute
Xusong Wang187c5972018-11-07 09:33:59 -0800233 Return<ErrorStatus> executionReturnStatus = ExecutePreparedModel(
234 preparedModel, {.inputs = inputs_info, .outputs = outputs_info, .pools = pools},
235 &outputShapes);
236 ASSERT_TRUE(executionReturnStatus.isOk());
237 executionStatus = static_cast<ErrorStatus>(executionReturnStatus);
David Gross49e41672018-12-21 11:20:26 -0800238 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700239
Xusong Wanga3165812018-11-19 18:26:08 -0800240 if (testDynamicOutputShape && executionStatus != ErrorStatus::NONE) {
241 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
242 "execute model that it does not support.";
243 std::cout << "[ ] Early termination of test because vendor service cannot "
244 "execute model that it does not support."
245 << std::endl;
246 return;
247 }
Xusong Wang187c5972018-11-07 09:33:59 -0800248 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
Xusong Wanga3165812018-11-19 18:26:08 -0800249
250 // Go through all outputs, overwrite output dimensions with returned output shapes
251 if (testDynamicOutputShape) {
252 ASSERT_NE(outputShapes.size(), 0);
253 for_each<uint32_t>(test.operandDimensions,
254 [&outputShapes](int idx, std::vector<uint32_t>& dim) {
255 dim = outputShapes[idx].dimensions;
256 });
257 }
Xusong Wang187c5972018-11-07 09:33:59 -0800258
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700259 // validate results
260 outputMemory->read();
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700261 copy_back(&test, outputs_info, outputPtr);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700262 outputMemory->commit();
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700263 // Filter out don't cares
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -0700264 MixedTyped filtered_golden = filter(golden, is_ignored);
265 MixedTyped filtered_test = filter(test, is_ignored);
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700266
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700267 // We want "close-enough" results for float
Xusong Wang10d77e42018-08-28 16:50:01 -0700268 compare(filtered_golden, filtered_test, fpAtol, fpRtol);
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100269
270 if (example.expectedMultinomialDistributionTolerance > 0) {
271 expectMultinomialDistributionWithinTolerance(test, example);
272 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700273 }
274}
David Gross49e41672018-12-21 11:20:26 -0800275template <typename T_IPreparedModel>
276void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
277 const std::vector<MixedTypedExample>& examples,
Xusong Wanga3165812018-11-19 18:26:08 -0800278 bool hasRelaxedFloat32Model, Synchronously sync,
279 bool testDynamicOutputShape) {
David Gross49e41672018-12-21 11:20:26 -0800280 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model, kDefaultAtol,
Xusong Wanga3165812018-11-19 18:26:08 -0800281 kDefaultRtol, sync, testDynamicOutputShape);
David Gross49e41672018-12-21 11:20:26 -0800282}
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700283
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700284static void getPreparedModel(sp<PreparedModelCallback> callback,
285 sp<V1_0::IPreparedModel>* preparedModel) {
286 *preparedModel = callback->getPreparedModel();
287}
288static void getPreparedModel(sp<PreparedModelCallback> callback,
289 sp<V1_2::IPreparedModel>* preparedModel) {
290 sp<V1_0::IPreparedModel> preparedModelV1_0 = callback->getPreparedModel();
291 *preparedModel = V1_2::IPreparedModel::castFrom(preparedModelV1_0).withDefault(nullptr);
292}
293
Michael Butlerf76acd02018-03-22 16:37:57 -0700294void Execute(const sp<V1_0::IDevice>& device, std::function<V1_0::Model(void)> create_model,
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100295 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800296 V1_0::Model model = create_model();
297
298 // see if service can handle model
299 bool fullySupportsModel = false;
Miao Wanga2d04c82018-02-05 17:26:54 -0800300 Return<void> supportedCall = device->getSupportedOperations(
Michael Butler4d5bb102018-02-26 15:24:46 -0800301 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
302 ASSERT_EQ(ErrorStatus::NONE, status);
Miao Wanga2d04c82018-02-05 17:26:54 -0800303 ASSERT_NE(0ul, supported.size());
304 fullySupportsModel =
305 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
306 });
307 ASSERT_TRUE(supportedCall.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800308
309 // launch prepare model
310 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
311 ASSERT_NE(nullptr, preparedModelCallback.get());
Miao Wanga2d04c82018-02-05 17:26:54 -0800312 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback);
313 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800314 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wanga2d04c82018-02-05 17:26:54 -0800315
316 // retrieve prepared model
317 preparedModelCallback->wait();
318 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700319 sp<V1_0::IPreparedModel> preparedModel;
320 getPreparedModel(preparedModelCallback, &preparedModel);
Miao Wanga2d04c82018-02-05 17:26:54 -0800321
322 // early termination if vendor service cannot fully prepare model
Michael Butler4d5bb102018-02-26 15:24:46 -0800323 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800324 ASSERT_EQ(nullptr, preparedModel.get());
325 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
326 "prepare model that it does not support.";
327 std::cout << "[ ] Early termination of test because vendor service cannot "
328 "prepare model that it does not support."
329 << std::endl;
330 return;
331 }
Michael Butler4d5bb102018-02-26 15:24:46 -0800332 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wanga2d04c82018-02-05 17:26:54 -0800333 ASSERT_NE(nullptr, preparedModel.get());
334
Xusong Wang10d77e42018-08-28 16:50:01 -0700335 float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f;
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000336 EvaluatePreparedModel(preparedModel, is_ignored, examples,
Xusong Wanga3165812018-11-19 18:26:08 -0800337 /*hasRelaxedFloat32Model=*/false, fpAtol, fpRtol, Synchronously::NO,
338 /*testDynamicOutputShape=*/false);
Miao Wanga2d04c82018-02-05 17:26:54 -0800339}
340
Michael Butlerf76acd02018-03-22 16:37:57 -0700341void Execute(const sp<V1_1::IDevice>& device, std::function<V1_1::Model(void)> create_model,
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100342 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800343 V1_1::Model model = create_model();
344
345 // see if service can handle model
346 bool fullySupportsModel = false;
Miao Wanga2d04c82018-02-05 17:26:54 -0800347 Return<void> supportedCall = device->getSupportedOperations_1_1(
Michael Butler4d5bb102018-02-26 15:24:46 -0800348 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
349 ASSERT_EQ(ErrorStatus::NONE, status);
Miao Wanga2d04c82018-02-05 17:26:54 -0800350 ASSERT_NE(0ul, supported.size());
351 fullySupportsModel =
352 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
353 });
354 ASSERT_TRUE(supportedCall.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800355
356 // launch prepare model
357 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
358 ASSERT_NE(nullptr, preparedModelCallback.get());
Michael Butler2504c2f2018-04-11 16:30:09 -0700359 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_1(
360 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
Miao Wanga2d04c82018-02-05 17:26:54 -0800361 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800362 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wanga2d04c82018-02-05 17:26:54 -0800363
364 // retrieve prepared model
365 preparedModelCallback->wait();
366 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700367 sp<V1_0::IPreparedModel> preparedModel;
368 getPreparedModel(preparedModelCallback, &preparedModel);
Miao Wanga2d04c82018-02-05 17:26:54 -0800369
370 // early termination if vendor service cannot fully prepare model
Michael Butler4d5bb102018-02-26 15:24:46 -0800371 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800372 ASSERT_EQ(nullptr, preparedModel.get());
373 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
374 "prepare model that it does not support.";
375 std::cout << "[ ] Early termination of test because vendor service cannot "
376 "prepare model that it does not support."
377 << std::endl;
378 return;
379 }
Michael Butler4d5bb102018-02-26 15:24:46 -0800380 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wanga2d04c82018-02-05 17:26:54 -0800381 ASSERT_NE(nullptr, preparedModel.get());
382
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000383 EvaluatePreparedModel(preparedModel, is_ignored, examples,
Xusong Wanga3165812018-11-19 18:26:08 -0800384 model.relaxComputationFloat32toFloat16, 1e-5f, 1e-5f, Synchronously::NO,
385 /*testDynamicOutputShape=*/false);
Miao Wanga2d04c82018-02-05 17:26:54 -0800386}
387
Slava Shklyaev871be942018-09-12 14:52:02 +0100388// TODO: Reduce code duplication.
389void Execute(const sp<V1_2::IDevice>& device, std::function<V1_2::Model(void)> create_model,
Xusong Wanga3165812018-11-19 18:26:08 -0800390 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples,
391 bool testDynamicOutputShape) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100392 V1_2::Model model = create_model();
393
394 // see if service can handle model
395 bool fullySupportsModel = false;
396 Return<void> supportedCall = device->getSupportedOperations_1_2(
397 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
398 ASSERT_EQ(ErrorStatus::NONE, status);
399 ASSERT_NE(0ul, supported.size());
400 fullySupportsModel =
401 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
402 });
403 ASSERT_TRUE(supportedCall.isOk());
404
405 // launch prepare model
406 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
407 ASSERT_NE(nullptr, preparedModelCallback.get());
408 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2(
409 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
410 ASSERT_TRUE(prepareLaunchStatus.isOk());
411 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
412
413 // retrieve prepared model
414 preparedModelCallback->wait();
415 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700416 sp<V1_2::IPreparedModel> preparedModel;
417 getPreparedModel(preparedModelCallback, &preparedModel);
Slava Shklyaev871be942018-09-12 14:52:02 +0100418
419 // early termination if vendor service cannot fully prepare model
420 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
421 ASSERT_EQ(nullptr, preparedModel.get());
422 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
423 "prepare model that it does not support.";
424 std::cout << "[ ] Early termination of test because vendor service cannot "
425 "prepare model that it does not support."
426 << std::endl;
427 return;
428 }
429 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
430 ASSERT_NE(nullptr, preparedModel.get());
431
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000432 EvaluatePreparedModel(preparedModel, is_ignored, examples,
Xusong Wanga3165812018-11-19 18:26:08 -0800433 model.relaxComputationFloat32toFloat16, Synchronously::NO,
434 testDynamicOutputShape);
David Gross49e41672018-12-21 11:20:26 -0800435 EvaluatePreparedModel(preparedModel, is_ignored, examples,
Xusong Wanga3165812018-11-19 18:26:08 -0800436 model.relaxComputationFloat32toFloat16, Synchronously::YES,
437 testDynamicOutputShape);
Slava Shklyaev871be942018-09-12 14:52:02 +0100438}
439
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700440} // namespace generated_tests
441
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700442} // namespace neuralnetworks
443} // namespace hardware
444} // namespace android