blob: 058eb25002fab3622140bfd288aeb51f0ee4aa59 [file] [log] [blame]
Michael Butlerf76acd02018-03-22 16:37:57 -07001/*
2 * Copyright (C) 2018 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
17#define LOG_TAG "neuralnetworks_hidl_hal_test"
18
Michael Butlerf76acd02018-03-22 16:37:57 -070019#include <android-base/logging.h>
20#include <android/hidl/memory/1.0/IMemory.h>
21#include <hidlmemory/mapping.h>
22
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010023#include "1.0/Callbacks.h"
24#include "MemoryUtils.h"
25#include "TestHarness.h"
26#include "VtsHalNeuralnetworks.h"
27
Michael Butlerf76acd02018-03-22 16:37:57 -070028namespace android {
29namespace hardware {
30namespace neuralnetworks {
31namespace V1_0 {
32namespace vts {
33namespace functional {
34
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010035using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback;
Michael Butlerf76acd02018-03-22 16:37:57 -070036using ::android::hidl::memory::V1_0::IMemory;
Mika Raentode166942018-04-17 16:49:50 +010037using test_helper::for_all;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010038using test_helper::MixedTyped;
39using test_helper::MixedTypedExample;
Michael Butlerf76acd02018-03-22 16:37:57 -070040
41///////////////////////// UTILITY FUNCTIONS /////////////////////////
42
Michael Butlerf76acd02018-03-22 16:37:57 -070043// Primary validation function. This function will take a valid request, apply a
44// mutation to it to invalidate the request, then pass it to interface calls
45// that use the request. Note that the request here is passed by value, and any
46// mutation to the request does not leave this function.
47static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
48 Request request, const std::function<void(Request*)>& mutation) {
49 mutation(&request);
50 SCOPED_TRACE(message + " [execute]");
51
52 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
53 ASSERT_NE(nullptr, executionCallback.get());
54 Return<ErrorStatus> executeLaunchStatus = preparedModel->execute(request, executionCallback);
55 ASSERT_TRUE(executeLaunchStatus.isOk());
56 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
57
58 executionCallback->wait();
59 ErrorStatus executionReturnStatus = executionCallback->getStatus();
60 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
61}
62
63// Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation,
64// so this is efficiently accomplished by moving the element to the end and
65// resizing the hidl_vec to one less.
66template <typename Type>
67static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) {
68 if (vec) {
69 std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end());
70 vec->resize(vec->size() - 1);
71 }
72}
73
74template <typename Type>
75static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) {
76 // assume vec is valid
77 const uint32_t index = vec->size();
78 vec->resize(index + 1);
79 (*vec)[index] = value;
80 return index;
81}
82
83///////////////////////// REMOVE INPUT ////////////////////////////////////
84
85static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
86 for (size_t input = 0; input < request.inputs.size(); ++input) {
87 const std::string message = "removeInput: removed input " + std::to_string(input);
88 validate(preparedModel, message, request,
89 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
90 }
91}
92
93///////////////////////// REMOVE OUTPUT ////////////////////////////////////
94
95static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
96 for (size_t output = 0; output < request.outputs.size(); ++output) {
97 const std::string message = "removeOutput: removed Output " + std::to_string(output);
98 validate(preparedModel, message, request,
99 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
100 }
101}
102
103///////////////////////////// ENTRY POINT //////////////////////////////////
104
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100105std::vector<Request> createRequests(const std::vector<MixedTypedExample>& examples) {
Michael Butlerf76acd02018-03-22 16:37:57 -0700106 const uint32_t INPUT = 0;
107 const uint32_t OUTPUT = 1;
108
109 std::vector<Request> requests;
110
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100111 for (const MixedTypedExample& example : examples) {
112 const MixedTyped& inputs = example.operands.first;
113 const MixedTyped& outputs = example.operands.second;
Michael Butlerf76acd02018-03-22 16:37:57 -0700114
115 std::vector<RequestArgument> inputs_info, outputs_info;
116 uint32_t inputSize = 0, outputSize = 0;
117
118 // This function only partially specifies the metadata (vector of RequestArguments).
119 // The contents are copied over below.
120 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
121 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
122 RequestArgument arg = {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100123 .location = {.poolIndex = INPUT,
124 .offset = 0,
125 .length = static_cast<uint32_t>(s)},
126 .dimensions = {},
Michael Butlerf76acd02018-03-22 16:37:57 -0700127 };
128 RequestArgument arg_empty = {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100129 .hasNoValue = true,
Michael Butlerf76acd02018-03-22 16:37:57 -0700130 };
131 inputs_info[index] = s ? arg : arg_empty;
132 inputSize += s;
133 });
134 // Compute offset for inputs 1 and so on
135 {
136 size_t offset = 0;
137 for (auto& i : inputs_info) {
138 if (!i.hasNoValue) i.location.offset = offset;
139 offset += i.location.length;
140 }
141 }
142
143 // Go through all outputs, initialize RequestArgument descriptors
144 for_all(outputs, [&outputs_info, &outputSize](int index, auto, auto s) {
145 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
146 RequestArgument arg = {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100147 .location = {.poolIndex = OUTPUT,
148 .offset = 0,
149 .length = static_cast<uint32_t>(s)},
150 .dimensions = {},
Michael Butlerf76acd02018-03-22 16:37:57 -0700151 };
152 outputs_info[index] = arg;
153 outputSize += s;
154 });
155 // Compute offset for outputs 1 and so on
156 {
157 size_t offset = 0;
158 for (auto& i : outputs_info) {
159 i.location.offset = offset;
160 offset += i.location.length;
161 }
162 }
163 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
164 nn::allocateSharedMemory(outputSize)};
165 if (pools[INPUT].size() == 0 || pools[OUTPUT].size() == 0) {
166 return {};
167 }
168
169 // map pool
170 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
171 if (inputMemory == nullptr) {
172 return {};
173 }
174 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
175 if (inputPtr == nullptr) {
176 return {};
177 }
178
179 // initialize pool
180 inputMemory->update();
181 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
182 char* begin = (char*)p;
183 char* end = begin + s;
184 // TODO: handle more than one input
185 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
186 });
187 inputMemory->commit();
188
189 requests.push_back({.inputs = inputs_info, .outputs = outputs_info, .pools = pools});
190 }
191
192 return requests;
193}
194
Michael Butler0a1ad962019-04-30 13:51:24 -0700195void ValidationTest::validateRequests(const sp<IPreparedModel>& preparedModel,
Michael Butlerf76acd02018-03-22 16:37:57 -0700196 const std::vector<Request>& requests) {
Michael Butlerf76acd02018-03-22 16:37:57 -0700197 // validate each request
198 for (const Request& request : requests) {
199 removeInputTest(preparedModel, request);
200 removeOutputTest(preparedModel, request);
201 }
202}
203
204} // namespace functional
205} // namespace vts
206} // namespace V1_0
207} // namespace neuralnetworks
208} // namespace hardware
209} // namespace android