Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 1 | /* |
| 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 Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 19 | #include "HandleError.h" |
| 20 | |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Slava Shklyaev | d4290b8 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 22 | #include <android-base/unique_fd.h> |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 23 | #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 Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 31 | #include <functional> |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 32 | #include <optional> |
| 33 | #include <variant> |
| 34 | #include <vector> |
| 35 | |
| 36 | namespace android::hardware::neuralnetworks::utils { |
| 37 | namespace { |
| 38 | |
| 39 | bool hasNoPointerData(const nn::Operand& operand); |
| 40 | bool hasNoPointerData(const nn::Model::Subgraph& subgraph); |
| 41 | bool hasNoPointerData(const nn::Request::Argument& argument); |
| 42 | |
| 43 | template <typename Type> |
| 44 | bool hasNoPointerData(const std::vector<Type>& objects) { |
| 45 | return std::all_of(objects.begin(), objects.end(), |
| 46 | [](const auto& object) { return hasNoPointerData(object); }); |
| 47 | } |
| 48 | |
| 49 | bool hasNoPointerData(const nn::DataLocation& location) { |
| 50 | return std::visit([](auto ptr) { return ptr == nullptr; }, location.pointer); |
| 51 | } |
| 52 | |
| 53 | bool hasNoPointerData(const nn::Operand& operand) { |
| 54 | return hasNoPointerData(operand.location); |
| 55 | } |
| 56 | |
| 57 | bool hasNoPointerData(const nn::Model::Subgraph& subgraph) { |
| 58 | return hasNoPointerData(subgraph.operands); |
| 59 | } |
| 60 | |
| 61 | bool hasNoPointerData(const nn::Request::Argument& argument) { |
| 62 | return hasNoPointerData(argument.location); |
| 63 | } |
| 64 | |
| 65 | void 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 | |
| 80 | void 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 | |
| 91 | nn::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 | |
| 110 | bool hasNoPointerData(const nn::Model& model) { |
| 111 | return hasNoPointerData(model.main) && hasNoPointerData(model.referenced); |
| 112 | } |
| 113 | |
| 114 | bool hasNoPointerData(const nn::Request& request) { |
| 115 | return hasNoPointerData(request.inputs) && hasNoPointerData(request.outputs); |
| 116 | } |
| 117 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 118 | nn::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 Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 130 | |
| 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 Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 143 | *maybeModelInSharedOut = modelInShared; |
| 144 | return **maybeModelInSharedOut; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 147 | nn::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 Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 159 | |
| 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 Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 199 | *maybeRequestInSharedOut = requestInShared; |
| 200 | return **maybeRequestInSharedOut; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 203 | nn::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 Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 207 | return {}; |
| 208 | } |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 209 | const auto& requestInShared = *maybeRequestInShared; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 210 | |
| 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 | |
| 246 | std::vector<uint32_t> countNumberOfConsumers(size_t numberOfOperands, |
| 247 | const std::vector<nn::Operation>& operations) { |
| 248 | return nn::countNumberOfConsumers(numberOfOperands, operations); |
| 249 | } |
| 250 | |
Slava Shklyaev | d4290b8 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 251 | nn::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 | |
| 280 | nn::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 | |
| 304 | nn::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 Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 314 | } // namespace android::hardware::neuralnetworks::utils |