blob: d45922e1c4d6a4fc3234e873e628f3d882c8c69b [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;
48using ::test_helper::for_all;
49using ::test_helper::for_each;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010050using ::test_helper::MixedTyped;
51using ::test_helper::MixedTypedExample;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010052using ::test_helper::resize_accordingly;
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070053
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -070054template <typename T>
Xusong Wanga3165812018-11-19 18:26:08 -080055void copy_back_(std::map<int, std::vector<T>>* dst, const std::vector<RequestArgument>& ra,
56 char* src) {
57 for_each<T>(*dst, [&ra, src](int index, std::vector<T>& m) {
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -070058 ASSERT_EQ(m.size(), ra[index].location.length / sizeof(T));
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070059 char* begin = src + ra[index].location.offset;
60 memcpy(m.data(), begin, ra[index].location.length);
61 });
62}
63
64void copy_back(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) {
Xusong Wanga3165812018-11-19 18:26:08 -080065 copy_back_(&dst->float32Operands, ra, src);
66 copy_back_(&dst->int32Operands, ra, src);
Xusong Wangd49f6652019-01-16 18:32:24 -080067 copy_back_(&dst->quant8AsymmOperands, ra, src);
68 copy_back_(&dst->quant16SymmOperands, ra, src);
Xusong Wanga3165812018-11-19 18:26:08 -080069 copy_back_(&dst->float16Operands, ra, src);
70 copy_back_(&dst->bool8Operands, ra, src);
71 copy_back_(&dst->quant8ChannelOperands, ra, src);
Xusong Wangd49f6652019-01-16 18:32:24 -080072 copy_back_(&dst->quant16AsymmOperands, ra, src);
73 static_assert(8 == MixedTyped::kNumTypes,
Lev Proleev9b490f42018-11-02 12:44:11 +000074 "Number of types in MixedTyped changed, but copy_back function wasn't updated");
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070075}
76
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070077// Top level driver for models and examples generated by test_generator.py
78// Test driver for those generated from ml/nn/runtime/test/spec
Xusong Wangb5cb8f72018-10-31 08:43:12 -070079static Return<ErrorStatus> ExecutePreparedModel(sp<V1_0::IPreparedModel>& preparedModel,
80 const Request& request,
81 sp<ExecutionCallback>& callback) {
82 return preparedModel->execute(request, callback);
83}
84static Return<ErrorStatus> ExecutePreparedModel(sp<V1_2::IPreparedModel>& preparedModel,
85 const Request& request,
86 sp<ExecutionCallback>& callback) {
87 return preparedModel->execute_1_2(request, callback);
88}
Xusong Wang187c5972018-11-07 09:33:59 -080089static Return<ErrorStatus> ExecutePreparedModel(sp<V1_0::IPreparedModel>&, const Request&,
90 hidl_vec<OutputShape>*) {
David Gross49e41672018-12-21 11:20:26 -080091 ADD_FAILURE() << "asking for synchronous execution at V1_0";
92 return ErrorStatus::GENERAL_FAILURE;
93}
94static Return<ErrorStatus> ExecutePreparedModel(sp<V1_2::IPreparedModel>& preparedModel,
Xusong Wang187c5972018-11-07 09:33:59 -080095 const Request& request,
96 hidl_vec<OutputShape>* outputShapes) {
97 ErrorStatus result;
98 Return<void> ret = preparedModel->executeSynchronously(
99 request, [&result, &outputShapes](ErrorStatus error, const hidl_vec<OutputShape>& shapes) {
100 result = error;
101 *outputShapes = shapes;
102 });
103 if (!ret.isOk()) {
104 return ErrorStatus::GENERAL_FAILURE;
105 }
106 return result;
David Gross49e41672018-12-21 11:20:26 -0800107}
108enum class Synchronously { NO, YES };
109const float kDefaultAtol = 1e-5f;
110const float kDefaultRtol = 1e-5f;
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700111template <typename T_IPreparedModel>
112void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000113 const std::vector<MixedTypedExample>& examples,
David Gross49e41672018-12-21 11:20:26 -0800114 bool hasRelaxedFloat32Model = false, float fpAtol = kDefaultAtol,
Xusong Wanga3165812018-11-19 18:26:08 -0800115 float fpRtol = kDefaultRtol, Synchronously sync = Synchronously::NO,
116 bool testDynamicOutputShape = false) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700117 const uint32_t INPUT = 0;
118 const uint32_t OUTPUT = 1;
119
120 int example_no = 1;
121 for (auto& example : examples) {
122 SCOPED_TRACE(example_no++);
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100123 const MixedTyped& inputs = example.operands.first;
124 const MixedTyped& golden = example.operands.second;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700125
Xusong Wanga3165812018-11-19 18:26:08 -0800126 const bool hasFloat16Inputs = !inputs.float16Operands.empty();
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000127 if (hasRelaxedFloat32Model || hasFloat16Inputs) {
128 // TODO: Adjust the error limit based on testing.
129 // If in relaxed mode, set the absolute tolerance to be 5ULP of FP16.
130 fpAtol = 5.0f * 0.0009765625f;
131 // Set the relative tolerance to be 5ULP of the corresponding FP precision.
132 fpRtol = 5.0f * 0.0009765625f;
133 }
134
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700135 std::vector<RequestArgument> inputs_info, outputs_info;
136 uint32_t inputSize = 0, outputSize = 0;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700137 // This function only partially specifies the metadata (vector of RequestArguments).
138 // The contents are copied over below.
139 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
140 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
141 RequestArgument arg = {
142 .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
143 .dimensions = {},
144 };
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700145 RequestArgument arg_empty = {
146 .hasNoValue = true,
147 };
148 inputs_info[index] = s ? arg : arg_empty;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700149 inputSize += s;
150 });
151 // Compute offset for inputs 1 and so on
152 {
153 size_t offset = 0;
154 for (auto& i : inputs_info) {
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700155 if (!i.hasNoValue) i.location.offset = offset;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700156 offset += i.location.length;
157 }
158 }
159
160 MixedTyped test; // holding test results
161
162 // Go through all outputs, initialize RequestArgument descriptors
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700163 resize_accordingly(golden, test);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700164 for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) {
165 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
166 RequestArgument arg = {
167 .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
168 .dimensions = {},
169 };
170 outputs_info[index] = arg;
171 outputSize += s;
172 });
173 // Compute offset for outputs 1 and so on
174 {
175 size_t offset = 0;
176 for (auto& i : outputs_info) {
177 i.location.offset = offset;
178 offset += i.location.length;
179 }
180 }
Miao Wanga2d04c82018-02-05 17:26:54 -0800181 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
182 nn::allocateSharedMemory(outputSize)};
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700183 ASSERT_NE(0ull, pools[INPUT].size());
184 ASSERT_NE(0ull, pools[OUTPUT].size());
185
186 // load data
187 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
188 sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]);
189 ASSERT_NE(nullptr, inputMemory.get());
190 ASSERT_NE(nullptr, outputMemory.get());
191 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
192 char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer()));
193 ASSERT_NE(nullptr, inputPtr);
194 ASSERT_NE(nullptr, outputPtr);
195 inputMemory->update();
196 outputMemory->update();
197
198 // Go through all inputs, copy the values
199 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
200 char* begin = (char*)p;
201 char* end = begin + s;
202 // TODO: handle more than one input
203 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
204 });
205
206 inputMemory->commit();
207 outputMemory->commit();
Michael Butlercf22a572017-09-22 13:26:12 -0700208
Xusong Wang187c5972018-11-07 09:33:59 -0800209 ErrorStatus executionStatus;
210 hidl_vec<OutputShape> outputShapes;
David Gross49e41672018-12-21 11:20:26 -0800211 if (sync == Synchronously::NO) {
212 SCOPED_TRACE("asynchronous");
Michael Butlercf22a572017-09-22 13:26:12 -0700213
David Gross49e41672018-12-21 11:20:26 -0800214 // launch execution
215 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
216 ASSERT_NE(nullptr, executionCallback.get());
217 Return<ErrorStatus> executionLaunchStatus = ExecutePreparedModel(
218 preparedModel, {.inputs = inputs_info, .outputs = outputs_info, .pools = pools},
219 executionCallback);
220 ASSERT_TRUE(executionLaunchStatus.isOk());
221 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
222
223 // retrieve execution status
224 executionCallback->wait();
Xusong Wang187c5972018-11-07 09:33:59 -0800225 executionStatus = executionCallback->getStatus();
226 outputShapes = executionCallback->getOutputShapes();
David Gross49e41672018-12-21 11:20:26 -0800227 } else {
228 SCOPED_TRACE("synchronous");
229
230 // execute
Xusong Wang187c5972018-11-07 09:33:59 -0800231 Return<ErrorStatus> executionReturnStatus = ExecutePreparedModel(
232 preparedModel, {.inputs = inputs_info, .outputs = outputs_info, .pools = pools},
233 &outputShapes);
234 ASSERT_TRUE(executionReturnStatus.isOk());
235 executionStatus = static_cast<ErrorStatus>(executionReturnStatus);
David Gross49e41672018-12-21 11:20:26 -0800236 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700237
Xusong Wanga3165812018-11-19 18:26:08 -0800238 if (testDynamicOutputShape && executionStatus != ErrorStatus::NONE) {
239 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
240 "execute model that it does not support.";
241 std::cout << "[ ] Early termination of test because vendor service cannot "
242 "execute model that it does not support."
243 << std::endl;
244 return;
245 }
Xusong Wang187c5972018-11-07 09:33:59 -0800246 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
Xusong Wanga3165812018-11-19 18:26:08 -0800247
248 // Go through all outputs, overwrite output dimensions with returned output shapes
249 if (testDynamicOutputShape) {
250 ASSERT_NE(outputShapes.size(), 0);
251 for_each<uint32_t>(test.operandDimensions,
252 [&outputShapes](int idx, std::vector<uint32_t>& dim) {
253 dim = outputShapes[idx].dimensions;
254 });
255 }
Xusong Wang187c5972018-11-07 09:33:59 -0800256
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700257 // validate results
258 outputMemory->read();
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700259 copy_back(&test, outputs_info, outputPtr);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700260 outputMemory->commit();
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700261 // Filter out don't cares
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -0700262 MixedTyped filtered_golden = filter(golden, is_ignored);
263 MixedTyped filtered_test = filter(test, is_ignored);
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700264
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700265 // We want "close-enough" results for float
Xusong Wang10d77e42018-08-28 16:50:01 -0700266 compare(filtered_golden, filtered_test, fpAtol, fpRtol);
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100267
268 if (example.expectedMultinomialDistributionTolerance > 0) {
269 expectMultinomialDistributionWithinTolerance(test, example);
270 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700271 }
272}
David Gross49e41672018-12-21 11:20:26 -0800273template <typename T_IPreparedModel>
274void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
275 const std::vector<MixedTypedExample>& examples,
Xusong Wanga3165812018-11-19 18:26:08 -0800276 bool hasRelaxedFloat32Model, Synchronously sync,
277 bool testDynamicOutputShape) {
David Gross49e41672018-12-21 11:20:26 -0800278 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model, kDefaultAtol,
Xusong Wanga3165812018-11-19 18:26:08 -0800279 kDefaultRtol, sync, testDynamicOutputShape);
David Gross49e41672018-12-21 11:20:26 -0800280}
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700281
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700282static void getPreparedModel(sp<PreparedModelCallback> callback,
283 sp<V1_0::IPreparedModel>* preparedModel) {
284 *preparedModel = callback->getPreparedModel();
285}
286static void getPreparedModel(sp<PreparedModelCallback> callback,
287 sp<V1_2::IPreparedModel>* preparedModel) {
288 sp<V1_0::IPreparedModel> preparedModelV1_0 = callback->getPreparedModel();
289 *preparedModel = V1_2::IPreparedModel::castFrom(preparedModelV1_0).withDefault(nullptr);
290}
291
Michael Butlerf76acd02018-03-22 16:37:57 -0700292void Execute(const sp<V1_0::IDevice>& device, std::function<V1_0::Model(void)> create_model,
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100293 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800294 V1_0::Model model = create_model();
295
296 // see if service can handle model
297 bool fullySupportsModel = false;
Miao Wanga2d04c82018-02-05 17:26:54 -0800298 Return<void> supportedCall = device->getSupportedOperations(
Michael Butler4d5bb102018-02-26 15:24:46 -0800299 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
300 ASSERT_EQ(ErrorStatus::NONE, status);
Miao Wanga2d04c82018-02-05 17:26:54 -0800301 ASSERT_NE(0ul, supported.size());
302 fullySupportsModel =
303 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
304 });
305 ASSERT_TRUE(supportedCall.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800306
307 // launch prepare model
308 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
309 ASSERT_NE(nullptr, preparedModelCallback.get());
Miao Wanga2d04c82018-02-05 17:26:54 -0800310 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback);
311 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800312 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wanga2d04c82018-02-05 17:26:54 -0800313
314 // retrieve prepared model
315 preparedModelCallback->wait();
316 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700317 sp<V1_0::IPreparedModel> preparedModel;
318 getPreparedModel(preparedModelCallback, &preparedModel);
Miao Wanga2d04c82018-02-05 17:26:54 -0800319
320 // early termination if vendor service cannot fully prepare model
Michael Butler4d5bb102018-02-26 15:24:46 -0800321 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800322 ASSERT_EQ(nullptr, preparedModel.get());
323 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
324 "prepare model that it does not support.";
325 std::cout << "[ ] Early termination of test because vendor service cannot "
326 "prepare model that it does not support."
327 << std::endl;
328 return;
329 }
Michael Butler4d5bb102018-02-26 15:24:46 -0800330 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wanga2d04c82018-02-05 17:26:54 -0800331 ASSERT_NE(nullptr, preparedModel.get());
332
Xusong Wang10d77e42018-08-28 16:50:01 -0700333 float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f;
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000334 EvaluatePreparedModel(preparedModel, is_ignored, examples,
Xusong Wanga3165812018-11-19 18:26:08 -0800335 /*hasRelaxedFloat32Model=*/false, fpAtol, fpRtol, Synchronously::NO,
336 /*testDynamicOutputShape=*/false);
Miao Wanga2d04c82018-02-05 17:26:54 -0800337}
338
Michael Butlerf76acd02018-03-22 16:37:57 -0700339void Execute(const sp<V1_1::IDevice>& device, std::function<V1_1::Model(void)> create_model,
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100340 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800341 V1_1::Model model = create_model();
342
343 // see if service can handle model
344 bool fullySupportsModel = false;
Miao Wanga2d04c82018-02-05 17:26:54 -0800345 Return<void> supportedCall = device->getSupportedOperations_1_1(
Michael Butler4d5bb102018-02-26 15:24:46 -0800346 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
347 ASSERT_EQ(ErrorStatus::NONE, status);
Miao Wanga2d04c82018-02-05 17:26:54 -0800348 ASSERT_NE(0ul, supported.size());
349 fullySupportsModel =
350 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
351 });
352 ASSERT_TRUE(supportedCall.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800353
354 // launch prepare model
355 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
356 ASSERT_NE(nullptr, preparedModelCallback.get());
Michael Butler2504c2f2018-04-11 16:30:09 -0700357 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_1(
358 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
Miao Wanga2d04c82018-02-05 17:26:54 -0800359 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800360 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wanga2d04c82018-02-05 17:26:54 -0800361
362 // retrieve prepared model
363 preparedModelCallback->wait();
364 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700365 sp<V1_0::IPreparedModel> preparedModel;
366 getPreparedModel(preparedModelCallback, &preparedModel);
Miao Wanga2d04c82018-02-05 17:26:54 -0800367
368 // early termination if vendor service cannot fully prepare model
Michael Butler4d5bb102018-02-26 15:24:46 -0800369 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800370 ASSERT_EQ(nullptr, preparedModel.get());
371 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
372 "prepare model that it does not support.";
373 std::cout << "[ ] Early termination of test because vendor service cannot "
374 "prepare model that it does not support."
375 << std::endl;
376 return;
377 }
Michael Butler4d5bb102018-02-26 15:24:46 -0800378 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wanga2d04c82018-02-05 17:26:54 -0800379 ASSERT_NE(nullptr, preparedModel.get());
380
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000381 EvaluatePreparedModel(preparedModel, is_ignored, examples,
Xusong Wanga3165812018-11-19 18:26:08 -0800382 model.relaxComputationFloat32toFloat16, 1e-5f, 1e-5f, Synchronously::NO,
383 /*testDynamicOutputShape=*/false);
Miao Wanga2d04c82018-02-05 17:26:54 -0800384}
385
Slava Shklyaev871be942018-09-12 14:52:02 +0100386// TODO: Reduce code duplication.
387void Execute(const sp<V1_2::IDevice>& device, std::function<V1_2::Model(void)> create_model,
Xusong Wanga3165812018-11-19 18:26:08 -0800388 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples,
389 bool testDynamicOutputShape) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100390 V1_2::Model model = create_model();
391
392 // see if service can handle model
393 bool fullySupportsModel = false;
394 Return<void> supportedCall = device->getSupportedOperations_1_2(
395 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
396 ASSERT_EQ(ErrorStatus::NONE, status);
397 ASSERT_NE(0ul, supported.size());
398 fullySupportsModel =
399 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
400 });
401 ASSERT_TRUE(supportedCall.isOk());
402
403 // launch prepare model
404 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
405 ASSERT_NE(nullptr, preparedModelCallback.get());
406 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2(
407 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
408 ASSERT_TRUE(prepareLaunchStatus.isOk());
409 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
410
411 // retrieve prepared model
412 preparedModelCallback->wait();
413 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wangb5cb8f72018-10-31 08:43:12 -0700414 sp<V1_2::IPreparedModel> preparedModel;
415 getPreparedModel(preparedModelCallback, &preparedModel);
Slava Shklyaev871be942018-09-12 14:52:02 +0100416
417 // early termination if vendor service cannot fully prepare model
418 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
419 ASSERT_EQ(nullptr, preparedModel.get());
420 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
421 "prepare model that it does not support.";
422 std::cout << "[ ] Early termination of test because vendor service cannot "
423 "prepare model that it does not support."
424 << std::endl;
425 return;
426 }
427 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
428 ASSERT_NE(nullptr, preparedModel.get());
429
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000430 EvaluatePreparedModel(preparedModel, is_ignored, examples,
Xusong Wanga3165812018-11-19 18:26:08 -0800431 model.relaxComputationFloat32toFloat16, Synchronously::NO,
432 testDynamicOutputShape);
David Gross49e41672018-12-21 11:20:26 -0800433 EvaluatePreparedModel(preparedModel, is_ignored, examples,
Xusong Wanga3165812018-11-19 18:26:08 -0800434 model.relaxComputationFloat32toFloat16, Synchronously::YES,
435 testDynamicOutputShape);
Slava Shklyaev871be942018-09-12 14:52:02 +0100436}
437
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700438} // namespace generated_tests
439
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700440} // namespace neuralnetworks
441} // namespace hardware
442} // namespace android