blob: d2703cbb65b0c1fe67b9f932234ac406680f643c [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>
27#include <android/hidl/allocator/1.0/IAllocator.h>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070028#include <android/hidl/memory/1.0/IMemory.h>
29#include <hidlmemory/mapping.h>
Michael Butler0897ab32017-10-04 02:38:42 -070030#include <iostream>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070031
32namespace android {
33namespace hardware {
34namespace neuralnetworks {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070035
36namespace generated_tests {
Michael Butlercf22a572017-09-22 13:26:12 -070037using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback;
38using ::android::hardware::neuralnetworks::V1_0::implementation::PreparedModelCallback;
Slava Shklyaevdc98cb02018-11-30 17:55:12 +000039using ::test_helper::bool8;
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +010040using ::test_helper::compare;
41using ::test_helper::expectMultinomialDistributionWithinTolerance;
Mika Raentod534d322018-04-17 16:49:50 +010042using ::test_helper::filter;
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +010043using ::test_helper::Float32Operands;
Mika Raentod534d322018-04-17 16:49:50 +010044using ::test_helper::for_all;
45using ::test_helper::for_each;
Mika Raentod534d322018-04-17 16:49:50 +010046using ::test_helper::Int32Operands;
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +010047using ::test_helper::MixedTyped;
48using ::test_helper::MixedTypedExample;
Michael K. Sanders650fd182018-10-30 14:44:48 +000049using ::test_helper::MixedTypedIndex;
Mika Raentod534d322018-04-17 16:49:50 +010050using ::test_helper::Quant8Operands;
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +010051using ::test_helper::resize_accordingly;
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070052
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -070053template <typename T>
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070054void copy_back_(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) {
55 MixedTyped& test = *dst;
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -070056 for_each<T>(test, [&ra, src](int index, std::vector<T>& m) {
57 ASSERT_EQ(m.size(), ra[index].location.length / sizeof(T));
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070058 char* begin = src + ra[index].location.offset;
59 memcpy(m.data(), begin, ra[index].location.length);
60 });
61}
62
63void copy_back(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) {
64 copy_back_<float>(dst, ra, src);
65 copy_back_<int32_t>(dst, ra, src);
66 copy_back_<uint8_t>(dst, ra, src);
Lev Proleeved7ce7a2018-11-05 13:20:06 +000067 copy_back_<int16_t>(dst, ra, src);
Michael K. Sanders650fd182018-10-30 14:44:48 +000068 copy_back_<_Float16>(dst, ra, src);
Slava Shklyaevdc98cb02018-11-30 17:55:12 +000069 copy_back_<bool8>(dst, ra, src);
70 static_assert(6 == std::tuple_size<MixedTyped>::value,
Lev Proleevd36b7a82018-11-02 12:44:11 +000071 "Number of types in MixedTyped changed, but copy_back function wasn't updated");
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -070072}
73
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070074// Top level driver for models and examples generated by test_generator.py
75// Test driver for those generated from ml/nn/runtime/test/spec
Miao Wang4862d612018-02-05 17:26:54 -080076void EvaluatePreparedModel(sp<IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
Michael K. Sanders650fd182018-10-30 14:44:48 +000077 const std::vector<MixedTypedExample>& examples,
78 bool hasRelaxedFloat32Model = false, float fpAtol = 1e-5f,
Xusong Wangf6235f82018-08-28 16:50:01 -070079 float fpRtol = 1e-5f) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070080 const uint32_t INPUT = 0;
81 const uint32_t OUTPUT = 1;
82
83 int example_no = 1;
84 for (auto& example : examples) {
85 SCOPED_TRACE(example_no++);
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +010086 const MixedTyped& inputs = example.operands.first;
87 const MixedTyped& golden = example.operands.second;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070088
Michael K. Sanders650fd182018-10-30 14:44:48 +000089 const bool hasFloat16Inputs = !std::get<MixedTypedIndex<_Float16>::index>(inputs).empty();
90 if (hasRelaxedFloat32Model || hasFloat16Inputs) {
91 // TODO: Adjust the error limit based on testing.
92 // If in relaxed mode, set the absolute tolerance to be 5ULP of FP16.
93 fpAtol = 5.0f * 0.0009765625f;
94 // Set the relative tolerance to be 5ULP of the corresponding FP precision.
95 fpRtol = 5.0f * 0.0009765625f;
96 }
97
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070098 std::vector<RequestArgument> inputs_info, outputs_info;
99 uint32_t inputSize = 0, outputSize = 0;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700100 // This function only partially specifies the metadata (vector of RequestArguments).
101 // The contents are copied over below.
102 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
103 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
104 RequestArgument arg = {
105 .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
106 .dimensions = {},
107 };
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700108 RequestArgument arg_empty = {
109 .hasNoValue = true,
110 };
111 inputs_info[index] = s ? arg : arg_empty;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700112 inputSize += s;
113 });
114 // Compute offset for inputs 1 and so on
115 {
116 size_t offset = 0;
117 for (auto& i : inputs_info) {
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -0700118 if (!i.hasNoValue) i.location.offset = offset;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700119 offset += i.location.length;
120 }
121 }
122
123 MixedTyped test; // holding test results
124
125 // Go through all outputs, initialize RequestArgument descriptors
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700126 resize_accordingly(golden, test);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700127 for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) {
128 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
129 RequestArgument arg = {
130 .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
131 .dimensions = {},
132 };
133 outputs_info[index] = arg;
134 outputSize += s;
135 });
136 // Compute offset for outputs 1 and so on
137 {
138 size_t offset = 0;
139 for (auto& i : outputs_info) {
140 i.location.offset = offset;
141 offset += i.location.length;
142 }
143 }
Miao Wang4862d612018-02-05 17:26:54 -0800144 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
145 nn::allocateSharedMemory(outputSize)};
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700146 ASSERT_NE(0ull, pools[INPUT].size());
147 ASSERT_NE(0ull, pools[OUTPUT].size());
148
149 // load data
150 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
151 sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]);
152 ASSERT_NE(nullptr, inputMemory.get());
153 ASSERT_NE(nullptr, outputMemory.get());
154 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
155 char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer()));
156 ASSERT_NE(nullptr, inputPtr);
157 ASSERT_NE(nullptr, outputPtr);
158 inputMemory->update();
159 outputMemory->update();
160
161 // Go through all inputs, copy the values
162 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
163 char* begin = (char*)p;
164 char* end = begin + s;
165 // TODO: handle more than one input
166 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
167 });
168
169 inputMemory->commit();
170 outputMemory->commit();
Michael Butlercf22a572017-09-22 13:26:12 -0700171
172 // launch execution
173 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
174 ASSERT_NE(nullptr, executionCallback.get());
175 Return<ErrorStatus> executionLaunchStatus = preparedModel->execute(
176 {.inputs = inputs_info, .outputs = outputs_info, .pools = pools}, executionCallback);
177 ASSERT_TRUE(executionLaunchStatus.isOk());
178 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
179
180 // retrieve execution status
181 executionCallback->wait();
182 ErrorStatus executionReturnStatus = executionCallback->getStatus();
183 EXPECT_EQ(ErrorStatus::NONE, executionReturnStatus);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700184
185 // validate results
186 outputMemory->read();
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700187 copy_back(&test, outputs_info, outputPtr);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700188 outputMemory->commit();
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700189 // Filter out don't cares
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -0700190 MixedTyped filtered_golden = filter(golden, is_ignored);
191 MixedTyped filtered_test = filter(test, is_ignored);
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700192
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700193 // We want "close-enough" results for float
Xusong Wangf6235f82018-08-28 16:50:01 -0700194 compare(filtered_golden, filtered_test, fpAtol, fpRtol);
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100195
196 if (example.expectedMultinomialDistributionTolerance > 0) {
197 expectMultinomialDistributionWithinTolerance(test, example);
198 }
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700199 }
200}
201
Michael Butler7ed61352018-03-22 16:37:57 -0700202void Execute(const sp<V1_0::IDevice>& device, std::function<V1_0::Model(void)> create_model,
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100203 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Miao Wang4862d612018-02-05 17:26:54 -0800204 V1_0::Model model = create_model();
205
206 // see if service can handle model
207 bool fullySupportsModel = false;
Miao Wang4862d612018-02-05 17:26:54 -0800208 Return<void> supportedCall = device->getSupportedOperations(
Michael Butler1ae02d62018-02-26 15:24:46 -0800209 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
210 ASSERT_EQ(ErrorStatus::NONE, status);
Miao Wang4862d612018-02-05 17:26:54 -0800211 ASSERT_NE(0ul, supported.size());
212 fullySupportsModel =
213 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
214 });
215 ASSERT_TRUE(supportedCall.isOk());
Michael Butler1ae02d62018-02-26 15:24:46 -0800216
217 // launch prepare model
218 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
219 ASSERT_NE(nullptr, preparedModelCallback.get());
Miao Wang4862d612018-02-05 17:26:54 -0800220 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback);
221 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler1ae02d62018-02-26 15:24:46 -0800222 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wang4862d612018-02-05 17:26:54 -0800223
224 // retrieve prepared model
225 preparedModelCallback->wait();
226 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
227 sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel();
Miao Wang4862d612018-02-05 17:26:54 -0800228
229 // early termination if vendor service cannot fully prepare model
Michael Butler1ae02d62018-02-26 15:24:46 -0800230 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wang4862d612018-02-05 17:26:54 -0800231 ASSERT_EQ(nullptr, preparedModel.get());
232 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
233 "prepare model that it does not support.";
234 std::cout << "[ ] Early termination of test because vendor service cannot "
235 "prepare model that it does not support."
236 << std::endl;
237 return;
238 }
Michael Butler1ae02d62018-02-26 15:24:46 -0800239 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wang4862d612018-02-05 17:26:54 -0800240 ASSERT_NE(nullptr, preparedModel.get());
241
Xusong Wangf6235f82018-08-28 16:50:01 -0700242 float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f;
Michael K. Sanders650fd182018-10-30 14:44:48 +0000243 EvaluatePreparedModel(preparedModel, is_ignored, examples,
244 /*hasRelaxedFloat32Model=*/false, fpAtol, fpRtol);
Miao Wang4862d612018-02-05 17:26:54 -0800245}
246
Michael Butler7ed61352018-03-22 16:37:57 -0700247void Execute(const sp<V1_1::IDevice>& device, std::function<V1_1::Model(void)> create_model,
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100248 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Miao Wang4862d612018-02-05 17:26:54 -0800249 V1_1::Model model = create_model();
250
251 // see if service can handle model
252 bool fullySupportsModel = false;
Miao Wang4862d612018-02-05 17:26:54 -0800253 Return<void> supportedCall = device->getSupportedOperations_1_1(
Michael Butler1ae02d62018-02-26 15:24:46 -0800254 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
255 ASSERT_EQ(ErrorStatus::NONE, status);
Miao Wang4862d612018-02-05 17:26:54 -0800256 ASSERT_NE(0ul, supported.size());
257 fullySupportsModel =
258 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
259 });
260 ASSERT_TRUE(supportedCall.isOk());
Michael Butler1ae02d62018-02-26 15:24:46 -0800261
262 // launch prepare model
263 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
264 ASSERT_NE(nullptr, preparedModelCallback.get());
Michael Butlerf02692d2018-04-11 16:30:09 -0700265 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_1(
266 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
Miao Wang4862d612018-02-05 17:26:54 -0800267 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler1ae02d62018-02-26 15:24:46 -0800268 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wang4862d612018-02-05 17:26:54 -0800269
270 // retrieve prepared model
271 preparedModelCallback->wait();
272 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
273 sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel();
Miao Wang4862d612018-02-05 17:26:54 -0800274
275 // early termination if vendor service cannot fully prepare model
Michael Butler1ae02d62018-02-26 15:24:46 -0800276 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wang4862d612018-02-05 17:26:54 -0800277 ASSERT_EQ(nullptr, preparedModel.get());
278 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
279 "prepare model that it does not support.";
280 std::cout << "[ ] Early termination of test because vendor service cannot "
281 "prepare model that it does not support."
282 << std::endl;
283 return;
284 }
Michael Butler1ae02d62018-02-26 15:24:46 -0800285 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wang4862d612018-02-05 17:26:54 -0800286 ASSERT_NE(nullptr, preparedModel.get());
287
Michael K. Sanders650fd182018-10-30 14:44:48 +0000288 EvaluatePreparedModel(preparedModel, is_ignored, examples,
289 model.relaxComputationFloat32toFloat16);
Miao Wang4862d612018-02-05 17:26:54 -0800290}
291
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100292// TODO: Reduce code duplication.
293void Execute(const sp<V1_2::IDevice>& device, std::function<V1_2::Model(void)> create_model,
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100294 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100295 V1_2::Model model = create_model();
296
297 // see if service can handle model
298 bool fullySupportsModel = false;
299 Return<void> supportedCall = device->getSupportedOperations_1_2(
300 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
301 ASSERT_EQ(ErrorStatus::NONE, status);
302 ASSERT_NE(0ul, supported.size());
303 fullySupportsModel =
304 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
305 });
306 ASSERT_TRUE(supportedCall.isOk());
307
308 // launch prepare model
309 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
310 ASSERT_NE(nullptr, preparedModelCallback.get());
311 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2(
312 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
313 ASSERT_TRUE(prepareLaunchStatus.isOk());
314 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
315
316 // retrieve prepared model
317 preparedModelCallback->wait();
318 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
319 sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel();
320
321 // early termination if vendor service cannot fully prepare model
322 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
323 ASSERT_EQ(nullptr, preparedModel.get());
324 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
325 "prepare model that it does not support.";
326 std::cout << "[ ] Early termination of test because vendor service cannot "
327 "prepare model that it does not support."
328 << std::endl;
329 return;
330 }
331 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
332 ASSERT_NE(nullptr, preparedModel.get());
333
Michael K. Sanders650fd182018-10-30 14:44:48 +0000334 EvaluatePreparedModel(preparedModel, is_ignored, examples,
335 model.relaxComputationFloat32toFloat16);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100336}
337
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700338} // namespace generated_tests
339
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700340} // namespace neuralnetworks
341} // namespace hardware
342} // namespace android