blob: 40d2f4ceb7628b3ac4908fdeffda028cc273c88b [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"
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010018#include "1.0/Callbacks.h"
19#include "1.0/Utils.h"
20#include "MemoryUtils.h"
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070021#include "TestHarness.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>
Miao Wanga2d04c82018-02-05 17:26:54 -080025#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080026#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>
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010030
Michael Butler0897ab32017-10-04 02:38:42 -070031#include <iostream>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070032
33namespace android {
34namespace hardware {
35namespace neuralnetworks {
Slava Shklyaev2bcfdc82019-07-17 15:50:57 +010036namespace V1_0 {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070037namespace generated_tests {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010038
39using ::android::hardware::neuralnetworks::V1_0::ErrorStatus;
40using ::android::hardware::neuralnetworks::V1_0::IDevice;
41using ::android::hardware::neuralnetworks::V1_0::IPreparedModel;
42using ::android::hardware::neuralnetworks::V1_0::Model;
43using ::android::hardware::neuralnetworks::V1_0::Request;
44using ::android::hardware::neuralnetworks::V1_0::RequestArgument;
45using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback;
46using ::android::hardware::neuralnetworks::V1_0::implementation::PreparedModelCallback;
47using ::android::hidl::memory::V1_0::IMemory;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010048using ::test_helper::compare;
Mika Raentode166942018-04-17 16:49:50 +010049using ::test_helper::filter;
50using ::test_helper::for_all;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010051using ::test_helper::MixedTyped;
52using ::test_helper::MixedTypedExample;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010053using ::test_helper::resize_accordingly;
Xusong Wangd2933152019-03-12 14:40:32 -070054
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070055// Top level driver for models and examples generated by test_generator.py
56// Test driver for those generated from ml/nn/runtime/test/spec
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010057void EvaluatePreparedModel(sp<IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
58 const std::vector<MixedTypedExample>& examples, float fpAtol,
59 float fpRtol) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070060 const uint32_t INPUT = 0;
61 const uint32_t OUTPUT = 1;
62
63 int example_no = 1;
64 for (auto& example : examples) {
65 SCOPED_TRACE(example_no++);
Michael K. Sanders941d61a2018-10-19 14:39:09 +010066 const MixedTyped& inputs = example.operands.first;
67 const MixedTyped& golden = example.operands.second;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070068
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010069 CHECK(inputs.float16Operands.empty()) << "float16 is not supported in 1.0";
Michael K. Sandersefa4c812018-10-30 14:44:48 +000070
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070071 std::vector<RequestArgument> inputs_info, outputs_info;
72 uint32_t inputSize = 0, outputSize = 0;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070073 // This function only partially specifies the metadata (vector of RequestArguments).
74 // The contents are copied over below.
75 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
76 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
77 RequestArgument arg = {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010078 .location = {.poolIndex = INPUT,
79 .offset = 0,
80 .length = static_cast<uint32_t>(s)},
81 .dimensions = {},
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070082 };
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -070083 RequestArgument arg_empty = {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010084 .hasNoValue = true,
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -070085 };
86 inputs_info[index] = s ? arg : arg_empty;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070087 inputSize += s;
88 });
89 // Compute offset for inputs 1 and so on
90 {
91 size_t offset = 0;
92 for (auto& i : inputs_info) {
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -070093 if (!i.hasNoValue) i.location.offset = offset;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070094 offset += i.location.length;
95 }
96 }
97
98 MixedTyped test; // holding test results
99
100 // Go through all outputs, initialize RequestArgument descriptors
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700101 resize_accordingly(golden, test);
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100102 for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700103 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
104 RequestArgument arg = {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100105 .location = {.poolIndex = OUTPUT,
106 .offset = 0,
107 .length = static_cast<uint32_t>(s)},
108 .dimensions = {},
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700109 };
110 outputs_info[index] = arg;
111 outputSize += s;
112 });
113 // Compute offset for outputs 1 and so on
114 {
115 size_t offset = 0;
116 for (auto& i : outputs_info) {
117 i.location.offset = offset;
118 offset += i.location.length;
119 }
120 }
Miao Wanga2d04c82018-02-05 17:26:54 -0800121 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
122 nn::allocateSharedMemory(outputSize)};
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700123 ASSERT_NE(0ull, pools[INPUT].size());
124 ASSERT_NE(0ull, pools[OUTPUT].size());
125
126 // load data
127 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
128 sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]);
129 ASSERT_NE(nullptr, inputMemory.get());
130 ASSERT_NE(nullptr, outputMemory.get());
131 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
132 char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer()));
133 ASSERT_NE(nullptr, inputPtr);
134 ASSERT_NE(nullptr, outputPtr);
135 inputMemory->update();
136 outputMemory->update();
137
138 // Go through all inputs, copy the values
139 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
140 char* begin = (char*)p;
141 char* end = begin + s;
142 // TODO: handle more than one input
143 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
144 });
145
146 inputMemory->commit();
147 outputMemory->commit();
Michael Butlercf22a572017-09-22 13:26:12 -0700148
Michael Butler814d8372019-01-15 11:02:55 -0800149 const Request request = {.inputs = inputs_info, .outputs = outputs_info, .pools = pools};
150
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100151 // launch execution
152 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
153 ASSERT_NE(nullptr, executionCallback.get());
154 Return<ErrorStatus> executionLaunchStatus =
155 preparedModel->execute(request, executionCallback);
156 ASSERT_TRUE(executionLaunchStatus.isOk());
157 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
Michael Butlercf22a572017-09-22 13:26:12 -0700158
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100159 // retrieve execution status
160 executionCallback->wait();
161 ASSERT_EQ(ErrorStatus::NONE, executionCallback->getStatus());
Xusong Wang187c5972018-11-07 09:33:59 -0800162
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700163 // validate results
164 outputMemory->read();
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700165 copy_back(&test, outputs_info, outputPtr);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700166 outputMemory->commit();
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700167 // Filter out don't cares
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -0700168 MixedTyped filtered_golden = filter(golden, is_ignored);
169 MixedTyped filtered_test = filter(test, is_ignored);
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700170
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700171 // We want "close-enough" results for float
Xusong Wang10d77e42018-08-28 16:50:01 -0700172 compare(filtered_golden, filtered_test, fpAtol, fpRtol);
Xusong Wang34058782019-01-18 17:28:26 -0800173 }
174}
175
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100176void Execute(const sp<IDevice>& device, std::function<Model(void)> create_model,
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100177 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100178 Model model = create_model();
Miao Wanga2d04c82018-02-05 17:26:54 -0800179
180 // see if service can handle model
181 bool fullySupportsModel = false;
Miao Wanga2d04c82018-02-05 17:26:54 -0800182 Return<void> supportedCall = device->getSupportedOperations(
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100183 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
184 ASSERT_EQ(ErrorStatus::NONE, status);
185 ASSERT_NE(0ul, supported.size());
186 fullySupportsModel = std::all_of(supported.begin(), supported.end(),
187 [](bool valid) { return valid; });
188 });
Miao Wanga2d04c82018-02-05 17:26:54 -0800189 ASSERT_TRUE(supportedCall.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800190
191 // launch prepare model
192 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
193 ASSERT_NE(nullptr, preparedModelCallback.get());
Miao Wanga2d04c82018-02-05 17:26:54 -0800194 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback);
195 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800196 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wanga2d04c82018-02-05 17:26:54 -0800197
198 // retrieve prepared model
199 preparedModelCallback->wait();
200 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100201 sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel();
Miao Wanga2d04c82018-02-05 17:26:54 -0800202
203 // early termination if vendor service cannot fully prepare model
Michael Butler4d5bb102018-02-26 15:24:46 -0800204 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800205 ASSERT_EQ(nullptr, preparedModel.get());
206 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
207 "prepare model that it does not support.";
208 std::cout << "[ ] Early termination of test because vendor service cannot "
209 "prepare model that it does not support."
210 << std::endl;
Miao Wangbb685a42019-01-08 12:27:35 -0800211 GTEST_SKIP();
Miao Wanga2d04c82018-02-05 17:26:54 -0800212 }
Michael Butler4d5bb102018-02-26 15:24:46 -0800213 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wanga2d04c82018-02-05 17:26:54 -0800214 ASSERT_NE(nullptr, preparedModel.get());
215
Xusong Wang10d77e42018-08-28 16:50:01 -0700216 float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f;
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100217 EvaluatePreparedModel(preparedModel, is_ignored, examples, fpAtol, fpRtol);
Slava Shklyaev871be942018-09-12 14:52:02 +0100218}
219
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700220} // namespace generated_tests
Slava Shklyaev2bcfdc82019-07-17 15:50:57 +0100221} // namespace V1_0
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700222} // namespace neuralnetworks
223} // namespace hardware
224} // namespace android