Michael Butler | b98aa6d | 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 "Conversions.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android/hardware/neuralnetworks/1.3/types.h> |
| 21 | #include <nnapi/OperandTypes.h> |
| 22 | #include <nnapi/OperationTypes.h> |
| 23 | #include <nnapi/Result.h> |
| 24 | #include <nnapi/SharedMemory.h> |
| 25 | #include <nnapi/TypeUtils.h> |
| 26 | #include <nnapi/Types.h> |
| 27 | #include <nnapi/hal/1.0/Conversions.h> |
| 28 | #include <nnapi/hal/1.2/Conversions.h> |
| 29 | #include <nnapi/hal/CommonUtils.h> |
| 30 | |
| 31 | #include <algorithm> |
| 32 | #include <chrono> |
| 33 | #include <functional> |
| 34 | #include <iterator> |
| 35 | #include <limits> |
| 36 | #include <type_traits> |
| 37 | #include <utility> |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | template <typename Type> |
| 42 | constexpr std::underlying_type_t<Type> underlyingType(Type value) { |
| 43 | return static_cast<std::underlying_type_t<Type>>(value); |
| 44 | } |
| 45 | |
| 46 | } // namespace |
| 47 | |
| 48 | namespace android::nn { |
| 49 | namespace { |
| 50 | |
| 51 | constexpr auto validOperandType(nn::OperandType operandType) { |
| 52 | switch (operandType) { |
| 53 | case nn::OperandType::FLOAT32: |
| 54 | case nn::OperandType::INT32: |
| 55 | case nn::OperandType::UINT32: |
| 56 | case nn::OperandType::TENSOR_FLOAT32: |
| 57 | case nn::OperandType::TENSOR_INT32: |
| 58 | case nn::OperandType::TENSOR_QUANT8_ASYMM: |
| 59 | case nn::OperandType::BOOL: |
| 60 | case nn::OperandType::TENSOR_QUANT16_SYMM: |
| 61 | case nn::OperandType::TENSOR_FLOAT16: |
| 62 | case nn::OperandType::TENSOR_BOOL8: |
| 63 | case nn::OperandType::FLOAT16: |
| 64 | case nn::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: |
| 65 | case nn::OperandType::TENSOR_QUANT16_ASYMM: |
| 66 | case nn::OperandType::TENSOR_QUANT8_SYMM: |
| 67 | case nn::OperandType::TENSOR_QUANT8_ASYMM_SIGNED: |
| 68 | case nn::OperandType::SUBGRAPH: |
| 69 | case nn::OperandType::OEM: |
| 70 | case nn::OperandType::TENSOR_OEM_BYTE: |
| 71 | return true; |
| 72 | } |
| 73 | return nn::isExtension(operandType); |
| 74 | } |
| 75 | |
| 76 | using hardware::hidl_vec; |
| 77 | |
| 78 | template <typename Input> |
| 79 | using ConvertOutput = std::decay_t<decltype(convert(std::declval<Input>()).value())>; |
| 80 | |
| 81 | template <typename Type> |
| 82 | Result<std::vector<ConvertOutput<Type>>> convertVec(const hidl_vec<Type>& arguments) { |
| 83 | std::vector<ConvertOutput<Type>> canonical; |
| 84 | canonical.reserve(arguments.size()); |
| 85 | for (const auto& argument : arguments) { |
| 86 | canonical.push_back(NN_TRY(nn::convert(argument))); |
| 87 | } |
| 88 | return canonical; |
| 89 | } |
| 90 | |
| 91 | template <typename Type> |
| 92 | Result<std::vector<ConvertOutput<Type>>> convert(const hidl_vec<Type>& arguments) { |
| 93 | return convertVec(arguments); |
| 94 | } |
| 95 | |
| 96 | } // anonymous namespace |
| 97 | |
| 98 | Result<OperandType> convert(const hal::V1_3::OperandType& operandType) { |
| 99 | return static_cast<OperandType>(operandType); |
| 100 | } |
| 101 | |
| 102 | Result<OperationType> convert(const hal::V1_3::OperationType& operationType) { |
| 103 | return static_cast<OperationType>(operationType); |
| 104 | } |
| 105 | |
| 106 | Result<Priority> convert(const hal::V1_3::Priority& priority) { |
| 107 | return static_cast<Priority>(priority); |
| 108 | } |
| 109 | |
| 110 | Result<Capabilities> convert(const hal::V1_3::Capabilities& capabilities) { |
| 111 | const bool validOperandTypes = std::all_of( |
| 112 | capabilities.operandPerformance.begin(), capabilities.operandPerformance.end(), |
| 113 | [](const hal::V1_3::Capabilities::OperandPerformance& operandPerformance) { |
| 114 | const auto maybeType = convert(operandPerformance.type); |
| 115 | return !maybeType.has_value() ? false : validOperandType(maybeType.value()); |
| 116 | }); |
| 117 | if (!validOperandTypes) { |
| 118 | return NN_ERROR() |
| 119 | << "Invalid OperandType when converting OperandPerformance in Capabilities"; |
| 120 | } |
| 121 | |
| 122 | auto operandPerformance = NN_TRY(convert(capabilities.operandPerformance)); |
| 123 | auto table = |
| 124 | NN_TRY(Capabilities::OperandPerformanceTable::create(std::move(operandPerformance))); |
| 125 | |
| 126 | return Capabilities{ |
| 127 | .relaxedFloat32toFloat16PerformanceScalar = |
| 128 | NN_TRY(convert(capabilities.relaxedFloat32toFloat16PerformanceScalar)), |
| 129 | .relaxedFloat32toFloat16PerformanceTensor = |
| 130 | NN_TRY(convert(capabilities.relaxedFloat32toFloat16PerformanceTensor)), |
| 131 | .operandPerformance = std::move(table), |
| 132 | .ifPerformance = NN_TRY(convert(capabilities.ifPerformance)), |
| 133 | .whilePerformance = NN_TRY(convert(capabilities.whilePerformance)), |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | Result<Capabilities::OperandPerformance> convert( |
| 138 | const hal::V1_3::Capabilities::OperandPerformance& operandPerformance) { |
| 139 | return Capabilities::OperandPerformance{ |
| 140 | .type = NN_TRY(convert(operandPerformance.type)), |
| 141 | .info = NN_TRY(convert(operandPerformance.info)), |
| 142 | }; |
| 143 | } |
| 144 | |
| 145 | Result<Operation> convert(const hal::V1_3::Operation& operation) { |
| 146 | return Operation{ |
| 147 | .type = NN_TRY(convert(operation.type)), |
| 148 | .inputs = operation.inputs, |
| 149 | .outputs = operation.outputs, |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | Result<Operand::LifeTime> convert(const hal::V1_3::OperandLifeTime& operandLifeTime) { |
| 154 | return static_cast<Operand::LifeTime>(operandLifeTime); |
| 155 | } |
| 156 | |
| 157 | Result<Operand> convert(const hal::V1_3::Operand& operand) { |
| 158 | return Operand{ |
| 159 | .type = NN_TRY(convert(operand.type)), |
| 160 | .dimensions = operand.dimensions, |
| 161 | .scale = operand.scale, |
| 162 | .zeroPoint = operand.zeroPoint, |
| 163 | .lifetime = NN_TRY(convert(operand.lifetime)), |
| 164 | .location = NN_TRY(convert(operand.location)), |
| 165 | .extraParams = NN_TRY(convert(operand.extraParams)), |
| 166 | }; |
| 167 | } |
| 168 | |
| 169 | Result<Model> convert(const hal::V1_3::Model& model) { |
| 170 | return Model{ |
| 171 | .main = NN_TRY(convert(model.main)), |
| 172 | .referenced = NN_TRY(convert(model.referenced)), |
| 173 | .operandValues = NN_TRY(convert(model.operandValues)), |
| 174 | .pools = NN_TRY(convert(model.pools)), |
| 175 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
| 176 | .extensionNameToPrefix = NN_TRY(convert(model.extensionNameToPrefix)), |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | Result<Model::Subgraph> convert(const hal::V1_3::Subgraph& subgraph) { |
| 181 | auto operations = NN_TRY(convert(subgraph.operations)); |
| 182 | |
| 183 | // Verify number of consumers. |
| 184 | const auto numberOfConsumers = |
| 185 | hal::utils::countNumberOfConsumers(subgraph.operands.size(), operations); |
| 186 | CHECK(subgraph.operands.size() == numberOfConsumers.size()); |
| 187 | for (size_t i = 0; i < subgraph.operands.size(); ++i) { |
| 188 | if (subgraph.operands[i].numberOfConsumers != numberOfConsumers[i]) { |
| 189 | return NN_ERROR() << "Invalid numberOfConsumers for operand " << i << ", expected " |
| 190 | << numberOfConsumers[i] << " but found " |
| 191 | << subgraph.operands[i].numberOfConsumers; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return Model::Subgraph{ |
| 196 | .operands = NN_TRY(convert(subgraph.operands)), |
| 197 | .operations = std::move(operations), |
| 198 | .inputIndexes = subgraph.inputIndexes, |
| 199 | .outputIndexes = subgraph.outputIndexes, |
| 200 | }; |
| 201 | } |
| 202 | |
| 203 | Result<BufferDesc> convert(const hal::V1_3::BufferDesc& bufferDesc) { |
| 204 | return BufferDesc{.dimensions = bufferDesc.dimensions}; |
| 205 | } |
| 206 | |
| 207 | Result<BufferRole> convert(const hal::V1_3::BufferRole& bufferRole) { |
| 208 | return BufferRole{ |
| 209 | .modelIndex = bufferRole.modelIndex, |
| 210 | .ioIndex = bufferRole.ioIndex, |
| 211 | .frequency = bufferRole.frequency, |
| 212 | }; |
| 213 | } |
| 214 | |
| 215 | Result<Request> convert(const hal::V1_3::Request& request) { |
| 216 | return Request{ |
| 217 | .inputs = NN_TRY(convert(request.inputs)), |
| 218 | .outputs = NN_TRY(convert(request.outputs)), |
| 219 | .pools = NN_TRY(convert(request.pools)), |
| 220 | }; |
| 221 | } |
| 222 | |
| 223 | Result<Request::MemoryPool> convert(const hal::V1_3::Request::MemoryPool& memoryPool) { |
| 224 | using Discriminator = hal::V1_3::Request::MemoryPool::hidl_discriminator; |
| 225 | switch (memoryPool.getDiscriminator()) { |
| 226 | case Discriminator::hidlMemory: |
| 227 | return createSharedMemoryFromHidlMemory(memoryPool.hidlMemory()); |
| 228 | case Discriminator::token: |
| 229 | return static_cast<Request::MemoryDomainToken>(memoryPool.token()); |
| 230 | } |
| 231 | return NN_ERROR() << "Invalid Request::MemoryPool discriminator " |
| 232 | << underlyingType(memoryPool.getDiscriminator()); |
| 233 | } |
| 234 | |
| 235 | Result<OptionalTimePoint> convert(const hal::V1_3::OptionalTimePoint& optionalTimePoint) { |
| 236 | constexpr auto kTimePointMaxCount = TimePoint::max().time_since_epoch().count(); |
| 237 | const auto makeTimePoint = [](uint64_t count) -> Result<OptionalTimePoint> { |
| 238 | if (count > kTimePointMaxCount) { |
| 239 | return NN_ERROR() |
| 240 | << "Unable to convert OptionalTimePoint because the count exceeds the max"; |
| 241 | } |
| 242 | const auto nanoseconds = std::chrono::nanoseconds{count}; |
| 243 | return TimePoint{nanoseconds}; |
| 244 | }; |
| 245 | |
| 246 | using Discriminator = hal::V1_3::OptionalTimePoint::hidl_discriminator; |
| 247 | switch (optionalTimePoint.getDiscriminator()) { |
| 248 | case Discriminator::none: |
| 249 | return std::nullopt; |
| 250 | case Discriminator::nanosecondsSinceEpoch: |
| 251 | return makeTimePoint(optionalTimePoint.nanosecondsSinceEpoch()); |
| 252 | } |
| 253 | return NN_ERROR() << "Invalid OptionalTimePoint discriminator " |
| 254 | << underlyingType(optionalTimePoint.getDiscriminator()); |
| 255 | } |
| 256 | |
| 257 | Result<OptionalTimeoutDuration> convert( |
| 258 | const hal::V1_3::OptionalTimeoutDuration& optionalTimeoutDuration) { |
| 259 | constexpr auto kTimeoutDurationMaxCount = TimeoutDuration::max().count(); |
| 260 | const auto makeTimeoutDuration = [](uint64_t count) -> Result<OptionalTimeoutDuration> { |
| 261 | if (count > kTimeoutDurationMaxCount) { |
| 262 | return NN_ERROR() |
| 263 | << "Unable to convert OptionalTimeoutDuration because the count exceeds the max"; |
| 264 | } |
| 265 | return TimeoutDuration{count}; |
| 266 | }; |
| 267 | |
| 268 | using Discriminator = hal::V1_3::OptionalTimeoutDuration::hidl_discriminator; |
| 269 | switch (optionalTimeoutDuration.getDiscriminator()) { |
| 270 | case Discriminator::none: |
| 271 | return std::nullopt; |
| 272 | case Discriminator::nanoseconds: |
| 273 | return makeTimeoutDuration(optionalTimeoutDuration.nanoseconds()); |
| 274 | } |
| 275 | return NN_ERROR() << "Invalid OptionalTimeoutDuration discriminator " |
| 276 | << underlyingType(optionalTimeoutDuration.getDiscriminator()); |
| 277 | } |
| 278 | |
| 279 | Result<ErrorStatus> convert(const hal::V1_3::ErrorStatus& status) { |
| 280 | switch (status) { |
| 281 | case hal::V1_3::ErrorStatus::NONE: |
| 282 | case hal::V1_3::ErrorStatus::DEVICE_UNAVAILABLE: |
| 283 | case hal::V1_3::ErrorStatus::GENERAL_FAILURE: |
| 284 | case hal::V1_3::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 285 | case hal::V1_3::ErrorStatus::INVALID_ARGUMENT: |
| 286 | case hal::V1_3::ErrorStatus::MISSED_DEADLINE_TRANSIENT: |
| 287 | case hal::V1_3::ErrorStatus::MISSED_DEADLINE_PERSISTENT: |
| 288 | case hal::V1_3::ErrorStatus::RESOURCE_EXHAUSTED_TRANSIENT: |
| 289 | case hal::V1_3::ErrorStatus::RESOURCE_EXHAUSTED_PERSISTENT: |
| 290 | return static_cast<ErrorStatus>(status); |
| 291 | } |
| 292 | return NN_ERROR() << "Invalid ErrorStatus " << underlyingType(status); |
| 293 | } |
| 294 | |
| 295 | Result<std::vector<BufferRole>> convert( |
| 296 | const hardware::hidl_vec<hal::V1_3::BufferRole>& bufferRoles) { |
| 297 | return convertVec(bufferRoles); |
| 298 | } |
| 299 | |
| 300 | } // namespace android::nn |
| 301 | |
| 302 | namespace android::hardware::neuralnetworks::V1_3::utils { |
| 303 | namespace { |
| 304 | |
| 305 | using utils::convert; |
| 306 | |
| 307 | nn::Result<V1_0::PerformanceInfo> convert( |
| 308 | const nn::Capabilities::PerformanceInfo& performanceInfo) { |
| 309 | return V1_0::utils::convert(performanceInfo); |
| 310 | } |
| 311 | |
| 312 | nn::Result<V1_0::DataLocation> convert(const nn::DataLocation& dataLocation) { |
| 313 | return V1_0::utils::convert(dataLocation); |
| 314 | } |
| 315 | |
| 316 | nn::Result<hidl_vec<uint8_t>> convert(const nn::Model::OperandValues& operandValues) { |
| 317 | return V1_0::utils::convert(operandValues); |
| 318 | } |
| 319 | |
| 320 | nn::Result<hidl_memory> convert(const nn::Memory& memory) { |
| 321 | return V1_0::utils::convert(memory); |
| 322 | } |
| 323 | |
| 324 | nn::Result<V1_0::RequestArgument> convert(const nn::Request::Argument& argument) { |
| 325 | return V1_0::utils::convert(argument); |
| 326 | } |
| 327 | |
| 328 | nn::Result<V1_2::Operand::ExtraParams> convert(const nn::Operand::ExtraParams& extraParams) { |
| 329 | return V1_2::utils::convert(extraParams); |
| 330 | } |
| 331 | |
| 332 | nn::Result<V1_2::Model::ExtensionNameAndPrefix> convert( |
| 333 | const nn::Model::ExtensionNameAndPrefix& extensionNameAndPrefix) { |
| 334 | return V1_2::utils::convert(extensionNameAndPrefix); |
| 335 | } |
| 336 | |
| 337 | template <typename Input> |
| 338 | using ConvertOutput = std::decay_t<decltype(convert(std::declval<Input>()).value())>; |
| 339 | |
| 340 | template <typename Type> |
| 341 | nn::Result<hidl_vec<ConvertOutput<Type>>> convertVec(const std::vector<Type>& arguments) { |
| 342 | hidl_vec<ConvertOutput<Type>> halObject(arguments.size()); |
| 343 | for (size_t i = 0; i < arguments.size(); ++i) { |
| 344 | halObject[i] = NN_TRY(convert(arguments[i])); |
| 345 | } |
| 346 | return halObject; |
| 347 | } |
| 348 | |
| 349 | template <typename Type> |
| 350 | nn::Result<hidl_vec<ConvertOutput<Type>>> convert(const std::vector<Type>& arguments) { |
| 351 | return convertVec(arguments); |
| 352 | } |
| 353 | |
| 354 | nn::Result<Request::MemoryPool> makeMemoryPool(const nn::Memory& memory) { |
| 355 | Request::MemoryPool ret; |
| 356 | ret.hidlMemory(NN_TRY(convert(memory))); |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | nn::Result<Request::MemoryPool> makeMemoryPool(const nn::Request::MemoryDomainToken& token) { |
| 361 | Request::MemoryPool ret; |
| 362 | ret.token(underlyingType(token)); |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | nn::Result<Request::MemoryPool> makeMemoryPool( |
| 367 | const std::shared_ptr<const nn::IBuffer>& /*buffer*/) { |
| 368 | return NN_ERROR() << "Unable to make memory pool from IBuffer"; |
| 369 | } |
| 370 | |
| 371 | } // anonymous namespace |
| 372 | |
| 373 | nn::Result<OperandType> convert(const nn::OperandType& operandType) { |
| 374 | return static_cast<OperandType>(operandType); |
| 375 | } |
| 376 | |
| 377 | nn::Result<OperationType> convert(const nn::OperationType& operationType) { |
| 378 | return static_cast<OperationType>(operationType); |
| 379 | } |
| 380 | |
| 381 | nn::Result<Priority> convert(const nn::Priority& priority) { |
| 382 | return static_cast<Priority>(priority); |
| 383 | } |
| 384 | |
| 385 | nn::Result<Capabilities> convert(const nn::Capabilities& capabilities) { |
| 386 | std::vector<nn::Capabilities::OperandPerformance> operandPerformance; |
| 387 | operandPerformance.reserve(capabilities.operandPerformance.asVector().size()); |
| 388 | std::copy_if(capabilities.operandPerformance.asVector().begin(), |
| 389 | capabilities.operandPerformance.asVector().end(), |
| 390 | std::back_inserter(operandPerformance), |
| 391 | [](const nn::Capabilities::OperandPerformance& operandPerformance) { |
| 392 | return nn::validOperandType(operandPerformance.type); |
| 393 | }); |
| 394 | |
| 395 | return Capabilities{ |
| 396 | .relaxedFloat32toFloat16PerformanceScalar = |
| 397 | NN_TRY(convert(capabilities.relaxedFloat32toFloat16PerformanceScalar)), |
| 398 | .relaxedFloat32toFloat16PerformanceTensor = |
| 399 | NN_TRY(convert(capabilities.relaxedFloat32toFloat16PerformanceTensor)), |
| 400 | .operandPerformance = NN_TRY(convert(operandPerformance)), |
| 401 | .ifPerformance = NN_TRY(convert(capabilities.ifPerformance)), |
| 402 | .whilePerformance = NN_TRY(convert(capabilities.whilePerformance)), |
| 403 | }; |
| 404 | } |
| 405 | |
| 406 | nn::Result<Capabilities::OperandPerformance> convert( |
| 407 | const nn::Capabilities::OperandPerformance& operandPerformance) { |
| 408 | return Capabilities::OperandPerformance{ |
| 409 | .type = NN_TRY(convert(operandPerformance.type)), |
| 410 | .info = NN_TRY(convert(operandPerformance.info)), |
| 411 | }; |
| 412 | } |
| 413 | |
| 414 | nn::Result<Operation> convert(const nn::Operation& operation) { |
| 415 | return Operation{ |
| 416 | .type = NN_TRY(convert(operation.type)), |
| 417 | .inputs = operation.inputs, |
| 418 | .outputs = operation.outputs, |
| 419 | }; |
| 420 | } |
| 421 | |
| 422 | nn::Result<OperandLifeTime> convert(const nn::Operand::LifeTime& operandLifeTime) { |
| 423 | if (operandLifeTime == nn::Operand::LifeTime::POINTER) { |
| 424 | return NN_ERROR() << "Model cannot be converted because it contains pointer-based memory"; |
| 425 | } |
| 426 | return static_cast<OperandLifeTime>(operandLifeTime); |
| 427 | } |
| 428 | |
| 429 | nn::Result<Operand> convert(const nn::Operand& operand) { |
| 430 | return Operand{ |
| 431 | .type = NN_TRY(convert(operand.type)), |
| 432 | .dimensions = operand.dimensions, |
| 433 | .numberOfConsumers = 0, |
| 434 | .scale = operand.scale, |
| 435 | .zeroPoint = operand.zeroPoint, |
| 436 | .lifetime = NN_TRY(convert(operand.lifetime)), |
| 437 | .location = NN_TRY(convert(operand.location)), |
| 438 | .extraParams = NN_TRY(convert(operand.extraParams)), |
| 439 | }; |
| 440 | } |
| 441 | |
| 442 | nn::Result<Model> convert(const nn::Model& model) { |
| 443 | if (!hal::utils::hasNoPointerData(model)) { |
| 444 | return NN_ERROR() << "Model cannot be converted because it contains pointer-based memory"; |
| 445 | } |
| 446 | |
| 447 | return Model{ |
| 448 | .main = NN_TRY(convert(model.main)), |
| 449 | .referenced = NN_TRY(convert(model.referenced)), |
| 450 | .operandValues = NN_TRY(convert(model.operandValues)), |
| 451 | .pools = NN_TRY(convert(model.pools)), |
| 452 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
| 453 | .extensionNameToPrefix = NN_TRY(convert(model.extensionNameToPrefix)), |
| 454 | }; |
| 455 | } |
| 456 | |
| 457 | nn::Result<Subgraph> convert(const nn::Model::Subgraph& subgraph) { |
| 458 | auto operands = NN_TRY(convert(subgraph.operands)); |
| 459 | |
| 460 | // Update number of consumers. |
| 461 | const auto numberOfConsumers = |
| 462 | hal::utils::countNumberOfConsumers(operands.size(), subgraph.operations); |
| 463 | CHECK(operands.size() == numberOfConsumers.size()); |
| 464 | for (size_t i = 0; i < operands.size(); ++i) { |
| 465 | operands[i].numberOfConsumers = numberOfConsumers[i]; |
| 466 | } |
| 467 | |
| 468 | return Subgraph{ |
| 469 | .operands = std::move(operands), |
| 470 | .operations = NN_TRY(convert(subgraph.operations)), |
| 471 | .inputIndexes = subgraph.inputIndexes, |
| 472 | .outputIndexes = subgraph.outputIndexes, |
| 473 | }; |
| 474 | } |
| 475 | |
| 476 | nn::Result<BufferDesc> convert(const nn::BufferDesc& bufferDesc) { |
| 477 | return BufferDesc{.dimensions = bufferDesc.dimensions}; |
| 478 | } |
| 479 | |
| 480 | nn::Result<BufferRole> convert(const nn::BufferRole& bufferRole) { |
| 481 | return BufferRole{ |
| 482 | .modelIndex = bufferRole.modelIndex, |
| 483 | .ioIndex = bufferRole.ioIndex, |
| 484 | .frequency = bufferRole.frequency, |
| 485 | }; |
| 486 | } |
| 487 | |
| 488 | nn::Result<Request> convert(const nn::Request& request) { |
| 489 | if (!hal::utils::hasNoPointerData(request)) { |
| 490 | return NN_ERROR() << "Request cannot be converted because it contains pointer-based memory"; |
| 491 | } |
| 492 | |
| 493 | return Request{ |
| 494 | .inputs = NN_TRY(convert(request.inputs)), |
| 495 | .outputs = NN_TRY(convert(request.outputs)), |
| 496 | .pools = NN_TRY(convert(request.pools)), |
| 497 | }; |
| 498 | } |
| 499 | |
| 500 | nn::Result<Request::MemoryPool> convert(const nn::Request::MemoryPool& memoryPool) { |
| 501 | return std::visit([](const auto& o) { return makeMemoryPool(o); }, memoryPool); |
| 502 | } |
| 503 | |
| 504 | nn::Result<OptionalTimePoint> convert(const nn::OptionalTimePoint& optionalTimePoint) { |
| 505 | OptionalTimePoint ret; |
| 506 | if (optionalTimePoint.has_value()) { |
| 507 | const auto count = optionalTimePoint.value().time_since_epoch().count(); |
| 508 | if (count < 0) { |
| 509 | return NN_ERROR() << "Unable to convert OptionalTimePoint because time since epoch " |
| 510 | "count is negative"; |
| 511 | } |
| 512 | ret.nanosecondsSinceEpoch(count); |
| 513 | } |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | nn::Result<OptionalTimeoutDuration> convert( |
| 518 | const nn::OptionalTimeoutDuration& optionalTimeoutDuration) { |
| 519 | OptionalTimeoutDuration ret; |
| 520 | if (optionalTimeoutDuration.has_value()) { |
| 521 | const auto count = optionalTimeoutDuration.value().count(); |
| 522 | if (count < 0) { |
| 523 | return NN_ERROR() |
| 524 | << "Unable to convert OptionalTimeoutDuration because count is negative"; |
| 525 | } |
| 526 | ret.nanoseconds(count); |
| 527 | } |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | nn::Result<ErrorStatus> convert(const nn::ErrorStatus& errorStatus) { |
| 532 | switch (errorStatus) { |
| 533 | case nn::ErrorStatus::NONE: |
| 534 | case nn::ErrorStatus::DEVICE_UNAVAILABLE: |
| 535 | case nn::ErrorStatus::GENERAL_FAILURE: |
| 536 | case nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 537 | case nn::ErrorStatus::INVALID_ARGUMENT: |
| 538 | case nn::ErrorStatus::MISSED_DEADLINE_TRANSIENT: |
| 539 | case nn::ErrorStatus::MISSED_DEADLINE_PERSISTENT: |
| 540 | case nn::ErrorStatus::RESOURCE_EXHAUSTED_TRANSIENT: |
| 541 | case nn::ErrorStatus::RESOURCE_EXHAUSTED_PERSISTENT: |
| 542 | return static_cast<ErrorStatus>(errorStatus); |
| 543 | default: |
| 544 | return ErrorStatus::GENERAL_FAILURE; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | nn::Result<hidl_vec<BufferRole>> convert(const std::vector<nn::BufferRole>& bufferRoles) { |
| 549 | return convertVec(bufferRoles); |
| 550 | } |
| 551 | |
| 552 | } // namespace android::hardware::neuralnetworks::V1_3::utils |