blob: c04c8dfa8bbf4ffd0bef7213ed49dd24a4ef9b62 [file] [log] [blame]
Michael Butlera685c3d2020-02-22 22:37:59 -08001/*
2 * Copyright (C) 2020 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#include "CommonUtils.h"
18
Michael Butler3670c382020-08-06 23:22:35 -070019#include "HandleError.h"
20
Michael Butlera685c3d2020-02-22 22:37:59 -080021#include <android-base/logging.h>
Slava Shklyaevd4290b82020-10-27 18:44:01 +000022#include <android-base/unique_fd.h>
Michael Butlera685c3d2020-02-22 22:37:59 -080023#include <nnapi/Result.h>
24#include <nnapi/SharedMemory.h>
25#include <nnapi/TypeUtils.h>
26#include <nnapi/Types.h>
27#include <nnapi/Validation.h>
28
29#include <algorithm>
30#include <any>
Michael Butler3670c382020-08-06 23:22:35 -070031#include <functional>
Michael Butlera685c3d2020-02-22 22:37:59 -080032#include <optional>
33#include <variant>
34#include <vector>
35
36namespace android::hardware::neuralnetworks::utils {
37namespace {
38
39bool hasNoPointerData(const nn::Operand& operand);
40bool hasNoPointerData(const nn::Model::Subgraph& subgraph);
41bool hasNoPointerData(const nn::Request::Argument& argument);
42
43template <typename Type>
44bool hasNoPointerData(const std::vector<Type>& objects) {
45 return std::all_of(objects.begin(), objects.end(),
46 [](const auto& object) { return hasNoPointerData(object); });
47}
48
49bool hasNoPointerData(const nn::DataLocation& location) {
50 return std::visit([](auto ptr) { return ptr == nullptr; }, location.pointer);
51}
52
53bool hasNoPointerData(const nn::Operand& operand) {
54 return hasNoPointerData(operand.location);
55}
56
57bool hasNoPointerData(const nn::Model::Subgraph& subgraph) {
58 return hasNoPointerData(subgraph.operands);
59}
60
61bool hasNoPointerData(const nn::Request::Argument& argument) {
62 return hasNoPointerData(argument.location);
63}
64
65void copyPointersToSharedMemory(nn::Operand* operand, nn::ConstantMemoryBuilder* memoryBuilder) {
66 CHECK(operand != nullptr);
67 CHECK(memoryBuilder != nullptr);
68
69 if (operand->lifetime != nn::Operand::LifeTime::POINTER) {
70 return;
71 }
72
73 const void* data = std::visit([](auto ptr) { return static_cast<const void*>(ptr); },
74 operand->location.pointer);
75 CHECK(data != nullptr);
76 operand->lifetime = nn::Operand::LifeTime::CONSTANT_REFERENCE;
77 operand->location = memoryBuilder->append(data, operand->location.length);
78}
79
80void copyPointersToSharedMemory(nn::Model::Subgraph* subgraph,
81 nn::ConstantMemoryBuilder* memoryBuilder) {
82 CHECK(subgraph != nullptr);
83 std::for_each(subgraph->operands.begin(), subgraph->operands.end(),
84 [memoryBuilder](auto& operand) {
85 copyPointersToSharedMemory(&operand, memoryBuilder);
86 });
87}
88
89} // anonymous namespace
90
91nn::Capabilities::OperandPerformanceTable makeQuantized8PerformanceConsistentWithP(
92 const nn::Capabilities::PerformanceInfo& float32Performance,
93 const nn::Capabilities::PerformanceInfo& quantized8Performance) {
94 // In Android P, most data types are treated as having the same performance as
95 // TENSOR_QUANT8_ASYMM. This collection must be in sorted order.
96 std::vector<nn::Capabilities::OperandPerformance> operandPerformances = {
97 {.type = nn::OperandType::FLOAT32, .info = float32Performance},
98 {.type = nn::OperandType::INT32, .info = quantized8Performance},
99 {.type = nn::OperandType::UINT32, .info = quantized8Performance},
100 {.type = nn::OperandType::TENSOR_FLOAT32, .info = float32Performance},
101 {.type = nn::OperandType::TENSOR_INT32, .info = quantized8Performance},
102 {.type = nn::OperandType::TENSOR_QUANT8_ASYMM, .info = quantized8Performance},
103 {.type = nn::OperandType::OEM, .info = quantized8Performance},
104 {.type = nn::OperandType::TENSOR_OEM_BYTE, .info = quantized8Performance},
105 };
106 return nn::Capabilities::OperandPerformanceTable::create(std::move(operandPerformances))
107 .value();
108}
109
110bool hasNoPointerData(const nn::Model& model) {
111 return hasNoPointerData(model.main) && hasNoPointerData(model.referenced);
112}
113
114bool hasNoPointerData(const nn::Request& request) {
115 return hasNoPointerData(request.inputs) && hasNoPointerData(request.outputs);
116}
117
Michael Butler3670c382020-08-06 23:22:35 -0700118nn::GeneralResult<std::reference_wrapper<const nn::Model>> flushDataFromPointerToShared(
119 const nn::Model* model, std::optional<nn::Model>* maybeModelInSharedOut) {
120 CHECK(model != nullptr);
121 CHECK(maybeModelInSharedOut != nullptr);
122
123 if (hasNoPointerData(*model)) {
124 return *model;
125 }
126
127 // Make a copy of the model in order to make modifications. The modified model is returned to
128 // the caller through `maybeModelInSharedOut` if the function succeeds.
129 nn::Model modelInShared = *model;
Michael Butlera685c3d2020-02-22 22:37:59 -0800130
131 nn::ConstantMemoryBuilder memoryBuilder(modelInShared.pools.size());
132 copyPointersToSharedMemory(&modelInShared.main, &memoryBuilder);
133 std::for_each(modelInShared.referenced.begin(), modelInShared.referenced.end(),
134 [&memoryBuilder](auto& subgraph) {
135 copyPointersToSharedMemory(&subgraph, &memoryBuilder);
136 });
137
138 if (!memoryBuilder.empty()) {
139 auto memory = NN_TRY(memoryBuilder.finish());
140 modelInShared.pools.push_back(std::move(memory));
141 }
142
Michael Butler3670c382020-08-06 23:22:35 -0700143 *maybeModelInSharedOut = modelInShared;
144 return **maybeModelInSharedOut;
Michael Butlera685c3d2020-02-22 22:37:59 -0800145}
146
Michael Butler3670c382020-08-06 23:22:35 -0700147nn::GeneralResult<std::reference_wrapper<const nn::Request>> flushDataFromPointerToShared(
148 const nn::Request* request, std::optional<nn::Request>* maybeRequestInSharedOut) {
149 CHECK(request != nullptr);
150 CHECK(maybeRequestInSharedOut != nullptr);
151
152 if (hasNoPointerData(*request)) {
153 return *request;
154 }
155
156 // Make a copy of the request in order to make modifications. The modified request is returned
157 // to the caller through `maybeRequestInSharedOut` if the function succeeds.
158 nn::Request requestInShared = *request;
Michael Butlera685c3d2020-02-22 22:37:59 -0800159
160 // Change input pointers to shared memory.
161 nn::ConstantMemoryBuilder inputBuilder(requestInShared.pools.size());
162 for (auto& input : requestInShared.inputs) {
163 const auto& location = input.location;
164 if (input.lifetime != nn::Request::Argument::LifeTime::POINTER) {
165 continue;
166 }
167
168 input.lifetime = nn::Request::Argument::LifeTime::POOL;
169 const void* data = std::visit([](auto ptr) { return static_cast<const void*>(ptr); },
170 location.pointer);
171 CHECK(data != nullptr);
172 input.location = inputBuilder.append(data, location.length);
173 }
174
175 // Allocate input memory.
176 if (!inputBuilder.empty()) {
177 auto memory = NN_TRY(inputBuilder.finish());
178 requestInShared.pools.push_back(std::move(memory));
179 }
180
181 // Change output pointers to shared memory.
182 nn::MutableMemoryBuilder outputBuilder(requestInShared.pools.size());
183 for (auto& output : requestInShared.outputs) {
184 const auto& location = output.location;
185 if (output.lifetime != nn::Request::Argument::LifeTime::POINTER) {
186 continue;
187 }
188
189 output.lifetime = nn::Request::Argument::LifeTime::POOL;
190 output.location = outputBuilder.append(location.length);
191 }
192
193 // Allocate output memory.
194 if (!outputBuilder.empty()) {
195 auto memory = NN_TRY(outputBuilder.finish());
196 requestInShared.pools.push_back(std::move(memory));
197 }
198
Michael Butler3670c382020-08-06 23:22:35 -0700199 *maybeRequestInSharedOut = requestInShared;
200 return **maybeRequestInSharedOut;
Michael Butlera685c3d2020-02-22 22:37:59 -0800201}
202
Michael Butler3670c382020-08-06 23:22:35 -0700203nn::GeneralResult<void> unflushDataFromSharedToPointer(
204 const nn::Request& request, const std::optional<nn::Request>& maybeRequestInShared) {
205 if (!maybeRequestInShared.has_value() || maybeRequestInShared->pools.empty() ||
206 !std::holds_alternative<nn::Memory>(maybeRequestInShared->pools.back())) {
Michael Butlera685c3d2020-02-22 22:37:59 -0800207 return {};
208 }
Michael Butler3670c382020-08-06 23:22:35 -0700209 const auto& requestInShared = *maybeRequestInShared;
Michael Butlera685c3d2020-02-22 22:37:59 -0800210
211 // Map the memory.
212 const auto& outputMemory = std::get<nn::Memory>(requestInShared.pools.back());
213 const auto [pointer, size, context] = NN_TRY(map(outputMemory));
214 const uint8_t* constantPointer =
215 std::visit([](const auto& o) { return static_cast<const uint8_t*>(o); }, pointer);
216
217 // Flush each output pointer.
218 CHECK_EQ(request.outputs.size(), requestInShared.outputs.size());
219 for (size_t i = 0; i < request.outputs.size(); ++i) {
220 const auto& location = request.outputs[i].location;
221 const auto& locationInShared = requestInShared.outputs[i].location;
222 if (!std::holds_alternative<void*>(location.pointer)) {
223 continue;
224 }
225
226 // Get output pointer and size.
227 void* data = std::get<void*>(location.pointer);
228 CHECK(data != nullptr);
229 const size_t length = location.length;
230
231 // Get output pool location.
232 CHECK(requestInShared.outputs[i].lifetime == nn::Request::Argument::LifeTime::POOL);
233 const size_t index = locationInShared.poolIndex;
234 const size_t offset = locationInShared.offset;
235 const size_t outputPoolIndex = requestInShared.pools.size() - 1;
236 CHECK(locationInShared.length == length);
237 CHECK(index == outputPoolIndex);
238
239 // Flush memory.
240 std::memcpy(data, constantPointer + offset, length);
241 }
242
243 return {};
244}
245
246std::vector<uint32_t> countNumberOfConsumers(size_t numberOfOperands,
247 const std::vector<nn::Operation>& operations) {
248 return nn::countNumberOfConsumers(numberOfOperands, operations);
249}
250
Slava Shklyaevd4290b82020-10-27 18:44:01 +0000251nn::GeneralResult<hidl_handle> hidlHandleFromSharedHandle(const nn::SharedHandle& handle) {
252 if (handle == nullptr) {
253 return {};
254 }
255
256 std::vector<base::unique_fd> fds;
257 fds.reserve(handle->fds.size());
258 for (const auto& fd : handle->fds) {
259 int dupFd = dup(fd);
260 if (dupFd == -1) {
261 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Failed to dup the fd";
262 }
263 fds.emplace_back(dupFd);
264 }
265
266 native_handle_t* nativeHandle = native_handle_create(handle->fds.size(), handle->ints.size());
267 if (nativeHandle == nullptr) {
268 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Failed to create native_handle";
269 }
270 for (size_t i = 0; i < fds.size(); ++i) {
271 nativeHandle->data[i] = fds[i].release();
272 }
273 std::copy(handle->ints.begin(), handle->ints.end(), &nativeHandle->data[nativeHandle->numFds]);
274
275 hidl_handle hidlHandle;
276 hidlHandle.setTo(nativeHandle, /*shouldOwn=*/true);
277 return hidlHandle;
278}
279
280nn::GeneralResult<nn::SharedHandle> sharedHandleFromNativeHandle(const native_handle_t* handle) {
281 if (handle == nullptr) {
282 return nullptr;
283 }
284
285 std::vector<base::unique_fd> fds;
286 fds.reserve(handle->numFds);
287 for (int i = 0; i < handle->numFds; ++i) {
288 int dupFd = dup(handle->data[i]);
289 if (dupFd == -1) {
290 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Failed to dup the fd";
291 }
292 fds.emplace_back(dupFd);
293 }
294
295 std::vector<int> ints(&handle->data[handle->numFds],
296 &handle->data[handle->numFds + handle->numInts]);
297
298 return std::make_shared<const nn::Handle>(nn::Handle{
299 .fds = std::move(fds),
300 .ints = std::move(ints),
301 });
302}
303
304nn::GeneralResult<hidl_vec<hidl_handle>> convertSyncFences(
305 const std::vector<nn::SyncFence>& syncFences) {
306 hidl_vec<hidl_handle> handles(syncFences.size());
307 for (size_t i = 0; i < syncFences.size(); ++i) {
308 handles[i] =
309 NN_TRY(hal::utils::hidlHandleFromSharedHandle(syncFences[i].getSharedHandle()));
310 }
311 return handles;
312}
313
Michael Butlera685c3d2020-02-22 22:37:59 -0800314} // namespace android::hardware::neuralnetworks::utils