blob: 7c5b0bc0c4650553c4ffa1643808372fbcff9777 [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 Wang4862d612018-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 Wang4862d612018-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 Wang1a06e772018-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 Wang4862d612018-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 Wang1a06e772018-10-31 08:43:12 -070042using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
43using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback;
Slava Shklyaevdc98cb02018-11-30 17:55:12 +000044using ::test_helper::bool8;
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +010045using ::test_helper::compare;
46using ::test_helper::expectMultinomialDistributionWithinTolerance;
Mika Raentod534d322018-04-17 16:49:50 +010047using ::test_helper::filter;
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +010048using ::test_helper::Float32Operands;
Mika Raentod534d322018-04-17 16:49:50 +010049using ::test_helper::for_all;
50using ::test_helper::for_each;
Mika Raentod534d322018-04-17 16:49:50 +010051using ::test_helper::Int32Operands;
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +010052using ::test_helper::MixedTyped;
53using ::test_helper::MixedTypedExample;
Michael K. Sanders650fd182018-10-30 14:44:48 +000054using ::test_helper::MixedTypedIndex;
Mika Raentod534d322018-04-17 16:49:50 +010055using ::test_helper::Quant8Operands;
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +010056using ::test_helper::resize_accordingly;
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070057
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -070058template <typename T>
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070059void copy_back_(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) {
60 MixedTyped& test = *dst;
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -070061 for_each<T>(test, [&ra, src](int index, std::vector<T>& m) {
62 ASSERT_EQ(m.size(), ra[index].location.length / sizeof(T));
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070063 char* begin = src + ra[index].location.offset;
64 memcpy(m.data(), begin, ra[index].location.length);
65 });
66}
67
68void copy_back(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) {
69 copy_back_<float>(dst, ra, src);
70 copy_back_<int32_t>(dst, ra, src);
71 copy_back_<uint8_t>(dst, ra, src);
Lev Proleeved7ce7a2018-11-05 13:20:06 +000072 copy_back_<int16_t>(dst, ra, src);
Michael K. Sanders650fd182018-10-30 14:44:48 +000073 copy_back_<_Float16>(dst, ra, src);
Slava Shklyaevdc98cb02018-11-30 17:55:12 +000074 copy_back_<bool8>(dst, ra, src);
75 static_assert(6 == std::tuple_size<MixedTyped>::value,
Lev Proleevd36b7a82018-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 Wang1a06e772018-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}
David Gross4592ed12018-12-21 11:20:26 -080091static Return<ErrorStatus> ExecutePreparedModel(sp<V1_0::IPreparedModel>&, const Request&) {
92 ADD_FAILURE() << "asking for synchronous execution at V1_0";
93 return ErrorStatus::GENERAL_FAILURE;
94}
95static Return<ErrorStatus> ExecutePreparedModel(sp<V1_2::IPreparedModel>& preparedModel,
96 const Request& request) {
97 return preparedModel->executeSynchronously(request);
98}
99enum class Synchronously { NO, YES };
100const float kDefaultAtol = 1e-5f;
101const float kDefaultRtol = 1e-5f;
Xusong Wang1a06e772018-10-31 08:43:12 -0700102template <typename T_IPreparedModel>
103void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
Michael K. Sanders650fd182018-10-30 14:44:48 +0000104 const std::vector<MixedTypedExample>& examples,
David Gross4592ed12018-12-21 11:20:26 -0800105 bool hasRelaxedFloat32Model = false, float fpAtol = kDefaultAtol,
106 float fpRtol = kDefaultRtol, Synchronously sync = Synchronously::NO) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700107 const uint32_t INPUT = 0;
108 const uint32_t OUTPUT = 1;
109
110 int example_no = 1;
111 for (auto& example : examples) {
112 SCOPED_TRACE(example_no++);
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100113 const MixedTyped& inputs = example.operands.first;
114 const MixedTyped& golden = example.operands.second;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700115
Michael K. Sanders650fd182018-10-30 14:44:48 +0000116 const bool hasFloat16Inputs = !std::get<MixedTypedIndex<_Float16>::index>(inputs).empty();
117 if (hasRelaxedFloat32Model || hasFloat16Inputs) {
118 // TODO: Adjust the error limit based on testing.
119 // If in relaxed mode, set the absolute tolerance to be 5ULP of FP16.
120 fpAtol = 5.0f * 0.0009765625f;
121 // Set the relative tolerance to be 5ULP of the corresponding FP precision.
122 fpRtol = 5.0f * 0.0009765625f;
123 }
124
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700125 std::vector<RequestArgument> inputs_info, outputs_info;
126 uint32_t inputSize = 0, outputSize = 0;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700127 // This function only partially specifies the metadata (vector of RequestArguments).
128 // The contents are copied over below.
129 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
130 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
131 RequestArgument arg = {
132 .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
133 .dimensions = {},
134 };
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700135 RequestArgument arg_empty = {
136 .hasNoValue = true,
137 };
138 inputs_info[index] = s ? arg : arg_empty;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700139 inputSize += s;
140 });
141 // Compute offset for inputs 1 and so on
142 {
143 size_t offset = 0;
144 for (auto& i : inputs_info) {
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700145 if (!i.hasNoValue) i.location.offset = offset;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700146 offset += i.location.length;
147 }
148 }
149
150 MixedTyped test; // holding test results
151
152 // Go through all outputs, initialize RequestArgument descriptors
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700153 resize_accordingly(golden, test);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700154 for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) {
155 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
156 RequestArgument arg = {
157 .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
158 .dimensions = {},
159 };
160 outputs_info[index] = arg;
161 outputSize += s;
162 });
163 // Compute offset for outputs 1 and so on
164 {
165 size_t offset = 0;
166 for (auto& i : outputs_info) {
167 i.location.offset = offset;
168 offset += i.location.length;
169 }
170 }
Miao Wang4862d612018-02-05 17:26:54 -0800171 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
172 nn::allocateSharedMemory(outputSize)};
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700173 ASSERT_NE(0ull, pools[INPUT].size());
174 ASSERT_NE(0ull, pools[OUTPUT].size());
175
176 // load data
177 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
178 sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]);
179 ASSERT_NE(nullptr, inputMemory.get());
180 ASSERT_NE(nullptr, outputMemory.get());
181 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
182 char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer()));
183 ASSERT_NE(nullptr, inputPtr);
184 ASSERT_NE(nullptr, outputPtr);
185 inputMemory->update();
186 outputMemory->update();
187
188 // Go through all inputs, copy the values
189 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
190 char* begin = (char*)p;
191 char* end = begin + s;
192 // TODO: handle more than one input
193 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
194 });
195
196 inputMemory->commit();
197 outputMemory->commit();
Michael Butlercf22a572017-09-22 13:26:12 -0700198
David Gross4592ed12018-12-21 11:20:26 -0800199 if (sync == Synchronously::NO) {
200 SCOPED_TRACE("asynchronous");
Michael Butlercf22a572017-09-22 13:26:12 -0700201
David Gross4592ed12018-12-21 11:20:26 -0800202 // launch execution
203 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
204 ASSERT_NE(nullptr, executionCallback.get());
205 Return<ErrorStatus> executionLaunchStatus = ExecutePreparedModel(
206 preparedModel, {.inputs = inputs_info, .outputs = outputs_info, .pools = pools},
207 executionCallback);
208 ASSERT_TRUE(executionLaunchStatus.isOk());
209 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
210
211 // retrieve execution status
212 executionCallback->wait();
213 ErrorStatus executionReturnStatus = executionCallback->getStatus();
214 EXPECT_EQ(ErrorStatus::NONE, executionReturnStatus);
215 } else {
216 SCOPED_TRACE("synchronous");
217
218 // execute
219 Return<ErrorStatus> executionStatus = ExecutePreparedModel(
220 preparedModel, {.inputs = inputs_info, .outputs = outputs_info, .pools = pools});
221 ASSERT_TRUE(executionStatus.isOk());
222 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionStatus));
223 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700224
225 // validate results
226 outputMemory->read();
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700227 copy_back(&test, outputs_info, outputPtr);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700228 outputMemory->commit();
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700229 // Filter out don't cares
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -0700230 MixedTyped filtered_golden = filter(golden, is_ignored);
231 MixedTyped filtered_test = filter(test, is_ignored);
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700232
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700233 // We want "close-enough" results for float
Xusong Wangf6235f82018-08-28 16:50:01 -0700234 compare(filtered_golden, filtered_test, fpAtol, fpRtol);
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100235
236 if (example.expectedMultinomialDistributionTolerance > 0) {
237 expectMultinomialDistributionWithinTolerance(test, example);
238 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700239 }
240}
David Gross4592ed12018-12-21 11:20:26 -0800241template <typename T_IPreparedModel>
242void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
243 const std::vector<MixedTypedExample>& examples,
244 bool hasRelaxedFloat32Model, Synchronously sync) {
245 EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model, kDefaultAtol,
246 kDefaultRtol, sync);
247}
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700248
Xusong Wang1a06e772018-10-31 08:43:12 -0700249static void getPreparedModel(sp<PreparedModelCallback> callback,
250 sp<V1_0::IPreparedModel>* preparedModel) {
251 *preparedModel = callback->getPreparedModel();
252}
253static void getPreparedModel(sp<PreparedModelCallback> callback,
254 sp<V1_2::IPreparedModel>* preparedModel) {
255 sp<V1_0::IPreparedModel> preparedModelV1_0 = callback->getPreparedModel();
256 *preparedModel = V1_2::IPreparedModel::castFrom(preparedModelV1_0).withDefault(nullptr);
257}
258
Michael Butler7ed61352018-03-22 16:37:57 -0700259void Execute(const sp<V1_0::IDevice>& device, std::function<V1_0::Model(void)> create_model,
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100260 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Miao Wang4862d612018-02-05 17:26:54 -0800261 V1_0::Model model = create_model();
262
263 // see if service can handle model
264 bool fullySupportsModel = false;
Miao Wang4862d612018-02-05 17:26:54 -0800265 Return<void> supportedCall = device->getSupportedOperations(
Michael Butler1ae02d62018-02-26 15:24:46 -0800266 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
267 ASSERT_EQ(ErrorStatus::NONE, status);
Miao Wang4862d612018-02-05 17:26:54 -0800268 ASSERT_NE(0ul, supported.size());
269 fullySupportsModel =
270 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
271 });
272 ASSERT_TRUE(supportedCall.isOk());
Michael Butler1ae02d62018-02-26 15:24:46 -0800273
274 // launch prepare model
275 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
276 ASSERT_NE(nullptr, preparedModelCallback.get());
Miao Wang4862d612018-02-05 17:26:54 -0800277 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback);
278 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler1ae02d62018-02-26 15:24:46 -0800279 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wang4862d612018-02-05 17:26:54 -0800280
281 // retrieve prepared model
282 preparedModelCallback->wait();
283 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wang1a06e772018-10-31 08:43:12 -0700284 sp<V1_0::IPreparedModel> preparedModel;
285 getPreparedModel(preparedModelCallback, &preparedModel);
Miao Wang4862d612018-02-05 17:26:54 -0800286
287 // early termination if vendor service cannot fully prepare model
Michael Butler1ae02d62018-02-26 15:24:46 -0800288 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wang4862d612018-02-05 17:26:54 -0800289 ASSERT_EQ(nullptr, preparedModel.get());
290 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
291 "prepare model that it does not support.";
292 std::cout << "[ ] Early termination of test because vendor service cannot "
293 "prepare model that it does not support."
294 << std::endl;
295 return;
296 }
Michael Butler1ae02d62018-02-26 15:24:46 -0800297 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wang4862d612018-02-05 17:26:54 -0800298 ASSERT_NE(nullptr, preparedModel.get());
299
Xusong Wangf6235f82018-08-28 16:50:01 -0700300 float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f;
Michael K. Sanders650fd182018-10-30 14:44:48 +0000301 EvaluatePreparedModel(preparedModel, is_ignored, examples,
302 /*hasRelaxedFloat32Model=*/false, fpAtol, fpRtol);
Miao Wang4862d612018-02-05 17:26:54 -0800303}
304
Michael Butler7ed61352018-03-22 16:37:57 -0700305void Execute(const sp<V1_1::IDevice>& device, std::function<V1_1::Model(void)> create_model,
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100306 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Miao Wang4862d612018-02-05 17:26:54 -0800307 V1_1::Model model = create_model();
308
309 // see if service can handle model
310 bool fullySupportsModel = false;
Miao Wang4862d612018-02-05 17:26:54 -0800311 Return<void> supportedCall = device->getSupportedOperations_1_1(
Michael Butler1ae02d62018-02-26 15:24:46 -0800312 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
313 ASSERT_EQ(ErrorStatus::NONE, status);
Miao Wang4862d612018-02-05 17:26:54 -0800314 ASSERT_NE(0ul, supported.size());
315 fullySupportsModel =
316 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
317 });
318 ASSERT_TRUE(supportedCall.isOk());
Michael Butler1ae02d62018-02-26 15:24:46 -0800319
320 // launch prepare model
321 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
322 ASSERT_NE(nullptr, preparedModelCallback.get());
Michael Butlerf02692d2018-04-11 16:30:09 -0700323 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_1(
324 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
Miao Wang4862d612018-02-05 17:26:54 -0800325 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler1ae02d62018-02-26 15:24:46 -0800326 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wang4862d612018-02-05 17:26:54 -0800327
328 // retrieve prepared model
329 preparedModelCallback->wait();
330 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wang1a06e772018-10-31 08:43:12 -0700331 sp<V1_0::IPreparedModel> preparedModel;
332 getPreparedModel(preparedModelCallback, &preparedModel);
Miao Wang4862d612018-02-05 17:26:54 -0800333
334 // early termination if vendor service cannot fully prepare model
Michael Butler1ae02d62018-02-26 15:24:46 -0800335 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wang4862d612018-02-05 17:26:54 -0800336 ASSERT_EQ(nullptr, preparedModel.get());
337 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
338 "prepare model that it does not support.";
339 std::cout << "[ ] Early termination of test because vendor service cannot "
340 "prepare model that it does not support."
341 << std::endl;
342 return;
343 }
Michael Butler1ae02d62018-02-26 15:24:46 -0800344 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wang4862d612018-02-05 17:26:54 -0800345 ASSERT_NE(nullptr, preparedModel.get());
346
Michael K. Sanders650fd182018-10-30 14:44:48 +0000347 EvaluatePreparedModel(preparedModel, is_ignored, examples,
348 model.relaxComputationFloat32toFloat16);
Miao Wang4862d612018-02-05 17:26:54 -0800349}
350
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100351// TODO: Reduce code duplication.
352void Execute(const sp<V1_2::IDevice>& device, std::function<V1_2::Model(void)> create_model,
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100353 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100354 V1_2::Model model = create_model();
355
356 // see if service can handle model
357 bool fullySupportsModel = false;
358 Return<void> supportedCall = device->getSupportedOperations_1_2(
359 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
360 ASSERT_EQ(ErrorStatus::NONE, status);
361 ASSERT_NE(0ul, supported.size());
362 fullySupportsModel =
363 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
364 });
365 ASSERT_TRUE(supportedCall.isOk());
366
367 // launch prepare model
368 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
369 ASSERT_NE(nullptr, preparedModelCallback.get());
370 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2(
371 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
372 ASSERT_TRUE(prepareLaunchStatus.isOk());
373 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
374
375 // retrieve prepared model
376 preparedModelCallback->wait();
377 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wang1a06e772018-10-31 08:43:12 -0700378 sp<V1_2::IPreparedModel> preparedModel;
379 getPreparedModel(preparedModelCallback, &preparedModel);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100380
381 // early termination if vendor service cannot fully prepare model
382 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
383 ASSERT_EQ(nullptr, preparedModel.get());
384 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
385 "prepare model that it does not support.";
386 std::cout << "[ ] Early termination of test because vendor service cannot "
387 "prepare model that it does not support."
388 << std::endl;
389 return;
390 }
391 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
392 ASSERT_NE(nullptr, preparedModel.get());
393
Michael K. Sanders650fd182018-10-30 14:44:48 +0000394 EvaluatePreparedModel(preparedModel, is_ignored, examples,
David Gross4592ed12018-12-21 11:20:26 -0800395 model.relaxComputationFloat32toFloat16, Synchronously::NO);
396 EvaluatePreparedModel(preparedModel, is_ignored, examples,
397 model.relaxComputationFloat32toFloat16, Synchronously::YES);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100398}
399
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700400} // namespace generated_tests
401
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700402} // namespace neuralnetworks
403} // namespace hardware
404} // namespace android