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> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 27 | #include <nnapi/Validation.h> |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 28 | #include <nnapi/hal/1.0/Conversions.h> |
| 29 | #include <nnapi/hal/1.2/Conversions.h> |
| 30 | #include <nnapi/hal/CommonUtils.h> |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 31 | #include <nnapi/hal/HandleError.h> |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 32 | |
| 33 | #include <algorithm> |
| 34 | #include <chrono> |
| 35 | #include <functional> |
| 36 | #include <iterator> |
| 37 | #include <limits> |
| 38 | #include <type_traits> |
| 39 | #include <utility> |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | template <typename Type> |
| 44 | constexpr std::underlying_type_t<Type> underlyingType(Type value) { |
| 45 | return static_cast<std::underlying_type_t<Type>>(value); |
| 46 | } |
| 47 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 48 | constexpr auto kVersion = android::nn::Version::ANDROID_R; |
| 49 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 50 | } // namespace |
| 51 | |
| 52 | namespace android::nn { |
| 53 | namespace { |
| 54 | |
| 55 | constexpr auto validOperandType(nn::OperandType operandType) { |
| 56 | switch (operandType) { |
| 57 | case nn::OperandType::FLOAT32: |
| 58 | case nn::OperandType::INT32: |
| 59 | case nn::OperandType::UINT32: |
| 60 | case nn::OperandType::TENSOR_FLOAT32: |
| 61 | case nn::OperandType::TENSOR_INT32: |
| 62 | case nn::OperandType::TENSOR_QUANT8_ASYMM: |
| 63 | case nn::OperandType::BOOL: |
| 64 | case nn::OperandType::TENSOR_QUANT16_SYMM: |
| 65 | case nn::OperandType::TENSOR_FLOAT16: |
| 66 | case nn::OperandType::TENSOR_BOOL8: |
| 67 | case nn::OperandType::FLOAT16: |
| 68 | case nn::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL: |
| 69 | case nn::OperandType::TENSOR_QUANT16_ASYMM: |
| 70 | case nn::OperandType::TENSOR_QUANT8_SYMM: |
| 71 | case nn::OperandType::TENSOR_QUANT8_ASYMM_SIGNED: |
| 72 | case nn::OperandType::SUBGRAPH: |
| 73 | case nn::OperandType::OEM: |
| 74 | case nn::OperandType::TENSOR_OEM_BYTE: |
| 75 | return true; |
| 76 | } |
| 77 | return nn::isExtension(operandType); |
| 78 | } |
| 79 | |
| 80 | using hardware::hidl_vec; |
| 81 | |
| 82 | template <typename Input> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 83 | using unvalidatedConvertOutput = |
| 84 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 85 | |
| 86 | template <typename Type> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 87 | GeneralResult<std::vector<unvalidatedConvertOutput<Type>>> unvalidatedConvertVec( |
| 88 | const hidl_vec<Type>& arguments) { |
| 89 | std::vector<unvalidatedConvertOutput<Type>> canonical; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 90 | canonical.reserve(arguments.size()); |
| 91 | for (const auto& argument : arguments) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 92 | canonical.push_back(NN_TRY(nn::unvalidatedConvert(argument))); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 93 | } |
| 94 | return canonical; |
| 95 | } |
| 96 | |
| 97 | template <typename Type> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 98 | GeneralResult<std::vector<unvalidatedConvertOutput<Type>>> unvalidatedConvert( |
| 99 | const hidl_vec<Type>& arguments) { |
| 100 | return unvalidatedConvertVec(arguments); |
| 101 | } |
| 102 | |
| 103 | template <typename Type> |
| 104 | decltype(nn::unvalidatedConvert(std::declval<Type>())) validatedConvert(const Type& halObject) { |
| 105 | auto canonical = NN_TRY(nn::unvalidatedConvert(halObject)); |
| 106 | const auto maybeVersion = validate(canonical); |
| 107 | if (!maybeVersion.has_value()) { |
| 108 | return error() << maybeVersion.error(); |
| 109 | } |
| 110 | const auto version = maybeVersion.value(); |
| 111 | if (version > kVersion) { |
| 112 | return NN_ERROR() << "Insufficient version: " << version << " vs required " << kVersion; |
| 113 | } |
| 114 | return canonical; |
| 115 | } |
| 116 | |
| 117 | template <typename Type> |
| 118 | GeneralResult<std::vector<unvalidatedConvertOutput<Type>>> validatedConvert( |
| 119 | const hidl_vec<Type>& arguments) { |
| 120 | std::vector<unvalidatedConvertOutput<Type>> canonical; |
| 121 | canonical.reserve(arguments.size()); |
| 122 | for (const auto& argument : arguments) { |
| 123 | canonical.push_back(NN_TRY(validatedConvert(argument))); |
| 124 | } |
| 125 | return canonical; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | } // anonymous namespace |
| 129 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 130 | GeneralResult<OperandType> unvalidatedConvert(const hal::V1_3::OperandType& operandType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 131 | return static_cast<OperandType>(operandType); |
| 132 | } |
| 133 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 134 | GeneralResult<OperationType> unvalidatedConvert(const hal::V1_3::OperationType& operationType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 135 | return static_cast<OperationType>(operationType); |
| 136 | } |
| 137 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 138 | GeneralResult<Priority> unvalidatedConvert(const hal::V1_3::Priority& priority) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 139 | return static_cast<Priority>(priority); |
| 140 | } |
| 141 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 142 | GeneralResult<Capabilities> unvalidatedConvert(const hal::V1_3::Capabilities& capabilities) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 143 | const bool validOperandTypes = std::all_of( |
| 144 | capabilities.operandPerformance.begin(), capabilities.operandPerformance.end(), |
| 145 | [](const hal::V1_3::Capabilities::OperandPerformance& operandPerformance) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 146 | const auto maybeType = unvalidatedConvert(operandPerformance.type); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 147 | return !maybeType.has_value() ? false : validOperandType(maybeType.value()); |
| 148 | }); |
| 149 | if (!validOperandTypes) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 150 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 151 | << "Invalid OperandType when unvalidatedConverting OperandPerformance in " |
| 152 | "Capabilities"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 155 | auto operandPerformance = NN_TRY(unvalidatedConvert(capabilities.operandPerformance)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 156 | auto table = NN_TRY(hal::utils::makeGeneralFailure( |
| 157 | Capabilities::OperandPerformanceTable::create(std::move(operandPerformance)), |
| 158 | nn::ErrorStatus::GENERAL_FAILURE)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 159 | |
| 160 | return Capabilities{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 161 | .relaxedFloat32toFloat16PerformanceScalar = NN_TRY( |
| 162 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceScalar)), |
| 163 | .relaxedFloat32toFloat16PerformanceTensor = NN_TRY( |
| 164 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceTensor)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 165 | .operandPerformance = std::move(table), |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 166 | .ifPerformance = NN_TRY(unvalidatedConvert(capabilities.ifPerformance)), |
| 167 | .whilePerformance = NN_TRY(unvalidatedConvert(capabilities.whilePerformance)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 168 | }; |
| 169 | } |
| 170 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 171 | GeneralResult<Capabilities::OperandPerformance> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 172 | const hal::V1_3::Capabilities::OperandPerformance& operandPerformance) { |
| 173 | return Capabilities::OperandPerformance{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 174 | .type = NN_TRY(unvalidatedConvert(operandPerformance.type)), |
| 175 | .info = NN_TRY(unvalidatedConvert(operandPerformance.info)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 176 | }; |
| 177 | } |
| 178 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 179 | GeneralResult<Operation> unvalidatedConvert(const hal::V1_3::Operation& operation) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 180 | return Operation{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 181 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 182 | .inputs = operation.inputs, |
| 183 | .outputs = operation.outputs, |
| 184 | }; |
| 185 | } |
| 186 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 187 | GeneralResult<Operand::LifeTime> unvalidatedConvert( |
| 188 | const hal::V1_3::OperandLifeTime& operandLifeTime) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 189 | return static_cast<Operand::LifeTime>(operandLifeTime); |
| 190 | } |
| 191 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 192 | GeneralResult<Operand> unvalidatedConvert(const hal::V1_3::Operand& operand) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 193 | return Operand{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 194 | .type = NN_TRY(unvalidatedConvert(operand.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 195 | .dimensions = operand.dimensions, |
| 196 | .scale = operand.scale, |
| 197 | .zeroPoint = operand.zeroPoint, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 198 | .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), |
| 199 | .location = NN_TRY(unvalidatedConvert(operand.location)), |
| 200 | .extraParams = NN_TRY(unvalidatedConvert(operand.extraParams)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 201 | }; |
| 202 | } |
| 203 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 204 | GeneralResult<Model> unvalidatedConvert(const hal::V1_3::Model& model) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 205 | return Model{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 206 | .main = NN_TRY(unvalidatedConvert(model.main)), |
| 207 | .referenced = NN_TRY(unvalidatedConvert(model.referenced)), |
| 208 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 209 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 210 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 211 | .extensionNameToPrefix = NN_TRY(unvalidatedConvert(model.extensionNameToPrefix)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 212 | }; |
| 213 | } |
| 214 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 215 | GeneralResult<Model::Subgraph> unvalidatedConvert(const hal::V1_3::Subgraph& subgraph) { |
| 216 | auto operations = NN_TRY(unvalidatedConvert(subgraph.operations)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 217 | |
| 218 | // Verify number of consumers. |
| 219 | const auto numberOfConsumers = |
| 220 | hal::utils::countNumberOfConsumers(subgraph.operands.size(), operations); |
| 221 | CHECK(subgraph.operands.size() == numberOfConsumers.size()); |
| 222 | for (size_t i = 0; i < subgraph.operands.size(); ++i) { |
| 223 | if (subgraph.operands[i].numberOfConsumers != numberOfConsumers[i]) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 224 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 225 | << "Invalid numberOfConsumers for operand " << i << ", expected " |
| 226 | << numberOfConsumers[i] << " but found " |
| 227 | << subgraph.operands[i].numberOfConsumers; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | return Model::Subgraph{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 232 | .operands = NN_TRY(unvalidatedConvert(subgraph.operands)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 233 | .operations = std::move(operations), |
| 234 | .inputIndexes = subgraph.inputIndexes, |
| 235 | .outputIndexes = subgraph.outputIndexes, |
| 236 | }; |
| 237 | } |
| 238 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 239 | GeneralResult<BufferDesc> unvalidatedConvert(const hal::V1_3::BufferDesc& bufferDesc) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 240 | return BufferDesc{.dimensions = bufferDesc.dimensions}; |
| 241 | } |
| 242 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 243 | GeneralResult<BufferRole> unvalidatedConvert(const hal::V1_3::BufferRole& bufferRole) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 244 | return BufferRole{ |
| 245 | .modelIndex = bufferRole.modelIndex, |
| 246 | .ioIndex = bufferRole.ioIndex, |
| 247 | .frequency = bufferRole.frequency, |
| 248 | }; |
| 249 | } |
| 250 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 251 | GeneralResult<Request> unvalidatedConvert(const hal::V1_3::Request& request) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 252 | return Request{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 253 | .inputs = NN_TRY(unvalidatedConvert(request.inputs)), |
| 254 | .outputs = NN_TRY(unvalidatedConvert(request.outputs)), |
| 255 | .pools = NN_TRY(unvalidatedConvert(request.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 256 | }; |
| 257 | } |
| 258 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 259 | GeneralResult<Request::MemoryPool> unvalidatedConvert( |
| 260 | const hal::V1_3::Request::MemoryPool& memoryPool) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 261 | using Discriminator = hal::V1_3::Request::MemoryPool::hidl_discriminator; |
| 262 | switch (memoryPool.getDiscriminator()) { |
| 263 | case Discriminator::hidlMemory: |
| 264 | return createSharedMemoryFromHidlMemory(memoryPool.hidlMemory()); |
| 265 | case Discriminator::token: |
| 266 | return static_cast<Request::MemoryDomainToken>(memoryPool.token()); |
| 267 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 268 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 269 | << "Invalid Request::MemoryPool discriminator " |
| 270 | << underlyingType(memoryPool.getDiscriminator()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 273 | GeneralResult<OptionalTimePoint> unvalidatedConvert( |
| 274 | const hal::V1_3::OptionalTimePoint& optionalTimePoint) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 275 | using Discriminator = hal::V1_3::OptionalTimePoint::hidl_discriminator; |
| 276 | switch (optionalTimePoint.getDiscriminator()) { |
| 277 | case Discriminator::none: |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 278 | return {}; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 279 | case Discriminator::nanosecondsSinceEpoch: |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 280 | return TimePoint{Duration{optionalTimePoint.nanosecondsSinceEpoch()}}; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 281 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 282 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 283 | << "Invalid OptionalTimePoint discriminator " |
| 284 | << underlyingType(optionalTimePoint.getDiscriminator()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 287 | GeneralResult<OptionalDuration> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 288 | const hal::V1_3::OptionalTimeoutDuration& optionalTimeoutDuration) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 289 | using Discriminator = hal::V1_3::OptionalTimeoutDuration::hidl_discriminator; |
| 290 | switch (optionalTimeoutDuration.getDiscriminator()) { |
| 291 | case Discriminator::none: |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 292 | return {}; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 293 | case Discriminator::nanoseconds: |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 294 | return Duration(optionalTimeoutDuration.nanoseconds()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 295 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 296 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 297 | << "Invalid OptionalTimeoutDuration discriminator " |
| 298 | << underlyingType(optionalTimeoutDuration.getDiscriminator()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 301 | GeneralResult<ErrorStatus> unvalidatedConvert(const hal::V1_3::ErrorStatus& status) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 302 | switch (status) { |
| 303 | case hal::V1_3::ErrorStatus::NONE: |
| 304 | case hal::V1_3::ErrorStatus::DEVICE_UNAVAILABLE: |
| 305 | case hal::V1_3::ErrorStatus::GENERAL_FAILURE: |
| 306 | case hal::V1_3::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 307 | case hal::V1_3::ErrorStatus::INVALID_ARGUMENT: |
| 308 | case hal::V1_3::ErrorStatus::MISSED_DEADLINE_TRANSIENT: |
| 309 | case hal::V1_3::ErrorStatus::MISSED_DEADLINE_PERSISTENT: |
| 310 | case hal::V1_3::ErrorStatus::RESOURCE_EXHAUSTED_TRANSIENT: |
| 311 | case hal::V1_3::ErrorStatus::RESOURCE_EXHAUSTED_PERSISTENT: |
| 312 | return static_cast<ErrorStatus>(status); |
| 313 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 314 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 315 | << "Invalid ErrorStatus " << underlyingType(status); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 318 | GeneralResult<Priority> convert(const hal::V1_3::Priority& priority) { |
| 319 | return validatedConvert(priority); |
| 320 | } |
| 321 | |
| 322 | GeneralResult<Capabilities> convert(const hal::V1_3::Capabilities& capabilities) { |
| 323 | return validatedConvert(capabilities); |
| 324 | } |
| 325 | |
| 326 | GeneralResult<Model> convert(const hal::V1_3::Model& model) { |
| 327 | return validatedConvert(model); |
| 328 | } |
| 329 | |
| 330 | GeneralResult<BufferDesc> convert(const hal::V1_3::BufferDesc& bufferDesc) { |
| 331 | return validatedConvert(bufferDesc); |
| 332 | } |
| 333 | |
| 334 | GeneralResult<Request> convert(const hal::V1_3::Request& request) { |
| 335 | return validatedConvert(request); |
| 336 | } |
| 337 | |
| 338 | GeneralResult<OptionalTimePoint> convert(const hal::V1_3::OptionalTimePoint& optionalTimePoint) { |
| 339 | return validatedConvert(optionalTimePoint); |
| 340 | } |
| 341 | |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 342 | GeneralResult<OptionalDuration> convert( |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 343 | const hal::V1_3::OptionalTimeoutDuration& optionalTimeoutDuration) { |
| 344 | return validatedConvert(optionalTimeoutDuration); |
| 345 | } |
| 346 | |
| 347 | GeneralResult<ErrorStatus> convert(const hal::V1_3::ErrorStatus& errorStatus) { |
| 348 | return validatedConvert(errorStatus); |
| 349 | } |
| 350 | |
| 351 | GeneralResult<SharedHandle> convert(const hardware::hidl_handle& handle) { |
| 352 | return validatedConvert(handle); |
| 353 | } |
| 354 | |
| 355 | GeneralResult<Memory> convert(const hardware::hidl_memory& memory) { |
| 356 | return validatedConvert(memory); |
| 357 | } |
| 358 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 359 | GeneralResult<std::vector<BufferRole>> convert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 360 | const hardware::hidl_vec<hal::V1_3::BufferRole>& bufferRoles) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 361 | return validatedConvert(bufferRoles); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | } // namespace android::nn |
| 365 | |
| 366 | namespace android::hardware::neuralnetworks::V1_3::utils { |
| 367 | namespace { |
| 368 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 369 | using utils::unvalidatedConvert; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 370 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 371 | nn::GeneralResult<V1_0::PerformanceInfo> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 372 | const nn::Capabilities::PerformanceInfo& performanceInfo) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 373 | return V1_0::utils::unvalidatedConvert(performanceInfo); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 376 | nn::GeneralResult<V1_0::DataLocation> unvalidatedConvert(const nn::DataLocation& dataLocation) { |
| 377 | return V1_0::utils::unvalidatedConvert(dataLocation); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 380 | nn::GeneralResult<hidl_vec<uint8_t>> unvalidatedConvert( |
| 381 | const nn::Model::OperandValues& operandValues) { |
| 382 | return V1_0::utils::unvalidatedConvert(operandValues); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 385 | nn::GeneralResult<hidl_handle> unvalidatedConvert(const nn::SharedHandle& handle) { |
| 386 | return V1_2::utils::unvalidatedConvert(handle); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 389 | nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::Memory& memory) { |
| 390 | return V1_0::utils::unvalidatedConvert(memory); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 393 | nn::GeneralResult<V1_0::RequestArgument> unvalidatedConvert(const nn::Request::Argument& argument) { |
| 394 | return V1_0::utils::unvalidatedConvert(argument); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 397 | nn::GeneralResult<V1_2::Operand::ExtraParams> unvalidatedConvert( |
| 398 | const nn::Operand::ExtraParams& extraParams) { |
| 399 | return V1_2::utils::unvalidatedConvert(extraParams); |
| 400 | } |
| 401 | |
| 402 | nn::GeneralResult<V1_2::Model::ExtensionNameAndPrefix> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 403 | const nn::Model::ExtensionNameAndPrefix& extensionNameAndPrefix) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 404 | return V1_2::utils::unvalidatedConvert(extensionNameAndPrefix); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | template <typename Input> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 408 | using unvalidatedConvertOutput = |
| 409 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 410 | |
| 411 | template <typename Type> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 412 | nn::GeneralResult<hidl_vec<unvalidatedConvertOutput<Type>>> unvalidatedConvertVec( |
| 413 | const std::vector<Type>& arguments) { |
| 414 | hidl_vec<unvalidatedConvertOutput<Type>> halObject(arguments.size()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 415 | for (size_t i = 0; i < arguments.size(); ++i) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 416 | halObject[i] = NN_TRY(unvalidatedConvert(arguments[i])); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 417 | } |
| 418 | return halObject; |
| 419 | } |
| 420 | |
| 421 | template <typename Type> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 422 | nn::GeneralResult<hidl_vec<unvalidatedConvertOutput<Type>>> unvalidatedConvert( |
| 423 | const std::vector<Type>& arguments) { |
| 424 | return unvalidatedConvertVec(arguments); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 427 | nn::GeneralResult<Request::MemoryPool> makeMemoryPool(const nn::Memory& memory) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 428 | Request::MemoryPool ret; |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 429 | ret.hidlMemory(NN_TRY(unvalidatedConvert(memory))); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 430 | return ret; |
| 431 | } |
| 432 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 433 | nn::GeneralResult<Request::MemoryPool> makeMemoryPool(const nn::Request::MemoryDomainToken& token) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 434 | Request::MemoryPool ret; |
| 435 | ret.token(underlyingType(token)); |
| 436 | return ret; |
| 437 | } |
| 438 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 439 | nn::GeneralResult<Request::MemoryPool> makeMemoryPool(const nn::SharedBuffer& /*buffer*/) { |
| 440 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Unable to make memory pool from IBuffer"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 443 | using utils::unvalidatedConvert; |
| 444 | |
| 445 | template <typename Type> |
| 446 | decltype(unvalidatedConvert(std::declval<Type>())) validatedConvert(const Type& canonical) { |
| 447 | const auto maybeVersion = nn::validate(canonical); |
| 448 | if (!maybeVersion.has_value()) { |
| 449 | return nn::error() << maybeVersion.error(); |
| 450 | } |
| 451 | const auto version = maybeVersion.value(); |
| 452 | if (version > kVersion) { |
| 453 | return NN_ERROR() << "Insufficient version: " << version << " vs required " << kVersion; |
| 454 | } |
| 455 | return unvalidatedConvert(canonical); |
| 456 | } |
| 457 | |
| 458 | template <typename Type> |
| 459 | nn::GeneralResult<hidl_vec<unvalidatedConvertOutput<Type>>> validatedConvert( |
| 460 | const std::vector<Type>& arguments) { |
| 461 | hidl_vec<unvalidatedConvertOutput<Type>> halObject(arguments.size()); |
| 462 | for (size_t i = 0; i < arguments.size(); ++i) { |
| 463 | halObject[i] = NN_TRY(validatedConvert(arguments[i])); |
| 464 | } |
| 465 | return halObject; |
| 466 | } |
| 467 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 468 | } // anonymous namespace |
| 469 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 470 | nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 471 | return static_cast<OperandType>(operandType); |
| 472 | } |
| 473 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 474 | nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 475 | return static_cast<OperationType>(operationType); |
| 476 | } |
| 477 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 478 | nn::GeneralResult<Priority> unvalidatedConvert(const nn::Priority& priority) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 479 | return static_cast<Priority>(priority); |
| 480 | } |
| 481 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 482 | nn::GeneralResult<Capabilities> unvalidatedConvert(const nn::Capabilities& capabilities) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 483 | std::vector<nn::Capabilities::OperandPerformance> operandPerformance; |
| 484 | operandPerformance.reserve(capabilities.operandPerformance.asVector().size()); |
| 485 | std::copy_if(capabilities.operandPerformance.asVector().begin(), |
| 486 | capabilities.operandPerformance.asVector().end(), |
| 487 | std::back_inserter(operandPerformance), |
| 488 | [](const nn::Capabilities::OperandPerformance& operandPerformance) { |
| 489 | return nn::validOperandType(operandPerformance.type); |
| 490 | }); |
| 491 | |
| 492 | return Capabilities{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 493 | .relaxedFloat32toFloat16PerformanceScalar = NN_TRY( |
| 494 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceScalar)), |
| 495 | .relaxedFloat32toFloat16PerformanceTensor = NN_TRY( |
| 496 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceTensor)), |
| 497 | .operandPerformance = NN_TRY(unvalidatedConvert(operandPerformance)), |
| 498 | .ifPerformance = NN_TRY(unvalidatedConvert(capabilities.ifPerformance)), |
| 499 | .whilePerformance = NN_TRY(unvalidatedConvert(capabilities.whilePerformance)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 500 | }; |
| 501 | } |
| 502 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 503 | nn::GeneralResult<Capabilities::OperandPerformance> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 504 | const nn::Capabilities::OperandPerformance& operandPerformance) { |
| 505 | return Capabilities::OperandPerformance{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 506 | .type = NN_TRY(unvalidatedConvert(operandPerformance.type)), |
| 507 | .info = NN_TRY(unvalidatedConvert(operandPerformance.info)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 508 | }; |
| 509 | } |
| 510 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 511 | nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 512 | return Operation{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 513 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 514 | .inputs = operation.inputs, |
| 515 | .outputs = operation.outputs, |
| 516 | }; |
| 517 | } |
| 518 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 519 | nn::GeneralResult<OperandLifeTime> unvalidatedConvert( |
| 520 | const nn::Operand::LifeTime& operandLifeTime) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 521 | if (operandLifeTime == nn::Operand::LifeTime::POINTER) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 522 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 523 | << "Model cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 524 | } |
| 525 | return static_cast<OperandLifeTime>(operandLifeTime); |
| 526 | } |
| 527 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 528 | nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 529 | return Operand{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 530 | .type = NN_TRY(unvalidatedConvert(operand.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 531 | .dimensions = operand.dimensions, |
| 532 | .numberOfConsumers = 0, |
| 533 | .scale = operand.scale, |
| 534 | .zeroPoint = operand.zeroPoint, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 535 | .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), |
| 536 | .location = NN_TRY(unvalidatedConvert(operand.location)), |
| 537 | .extraParams = NN_TRY(unvalidatedConvert(operand.extraParams)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 538 | }; |
| 539 | } |
| 540 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 541 | nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 542 | if (!hal::utils::hasNoPointerData(model)) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 543 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 544 | << "Model cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | return Model{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 548 | .main = NN_TRY(unvalidatedConvert(model.main)), |
| 549 | .referenced = NN_TRY(unvalidatedConvert(model.referenced)), |
| 550 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 551 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 552 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 553 | .extensionNameToPrefix = NN_TRY(unvalidatedConvert(model.extensionNameToPrefix)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 554 | }; |
| 555 | } |
| 556 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 557 | nn::GeneralResult<Subgraph> unvalidatedConvert(const nn::Model::Subgraph& subgraph) { |
| 558 | auto operands = NN_TRY(unvalidatedConvert(subgraph.operands)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 559 | |
| 560 | // Update number of consumers. |
| 561 | const auto numberOfConsumers = |
| 562 | hal::utils::countNumberOfConsumers(operands.size(), subgraph.operations); |
| 563 | CHECK(operands.size() == numberOfConsumers.size()); |
| 564 | for (size_t i = 0; i < operands.size(); ++i) { |
| 565 | operands[i].numberOfConsumers = numberOfConsumers[i]; |
| 566 | } |
| 567 | |
| 568 | return Subgraph{ |
| 569 | .operands = std::move(operands), |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 570 | .operations = NN_TRY(unvalidatedConvert(subgraph.operations)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 571 | .inputIndexes = subgraph.inputIndexes, |
| 572 | .outputIndexes = subgraph.outputIndexes, |
| 573 | }; |
| 574 | } |
| 575 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 576 | nn::GeneralResult<BufferDesc> unvalidatedConvert(const nn::BufferDesc& bufferDesc) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 577 | return BufferDesc{.dimensions = bufferDesc.dimensions}; |
| 578 | } |
| 579 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 580 | nn::GeneralResult<BufferRole> unvalidatedConvert(const nn::BufferRole& bufferRole) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 581 | return BufferRole{ |
| 582 | .modelIndex = bufferRole.modelIndex, |
| 583 | .ioIndex = bufferRole.ioIndex, |
| 584 | .frequency = bufferRole.frequency, |
| 585 | }; |
| 586 | } |
| 587 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 588 | nn::GeneralResult<Request> unvalidatedConvert(const nn::Request& request) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 589 | if (!hal::utils::hasNoPointerData(request)) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 590 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 591 | << "Request cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | return Request{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 595 | .inputs = NN_TRY(unvalidatedConvert(request.inputs)), |
| 596 | .outputs = NN_TRY(unvalidatedConvert(request.outputs)), |
| 597 | .pools = NN_TRY(unvalidatedConvert(request.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 598 | }; |
| 599 | } |
| 600 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 601 | nn::GeneralResult<Request::MemoryPool> unvalidatedConvert( |
| 602 | const nn::Request::MemoryPool& memoryPool) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 603 | return std::visit([](const auto& o) { return makeMemoryPool(o); }, memoryPool); |
| 604 | } |
| 605 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 606 | nn::GeneralResult<OptionalTimePoint> unvalidatedConvert( |
| 607 | const nn::OptionalTimePoint& optionalTimePoint) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 608 | OptionalTimePoint ret; |
| 609 | if (optionalTimePoint.has_value()) { |
| 610 | const auto count = optionalTimePoint.value().time_since_epoch().count(); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 611 | ret.nanosecondsSinceEpoch(count); |
| 612 | } |
| 613 | return ret; |
| 614 | } |
| 615 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 616 | nn::GeneralResult<OptionalTimeoutDuration> unvalidatedConvert( |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 617 | const nn::OptionalDuration& optionalTimeoutDuration) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 618 | OptionalTimeoutDuration ret; |
| 619 | if (optionalTimeoutDuration.has_value()) { |
| 620 | const auto count = optionalTimeoutDuration.value().count(); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 621 | ret.nanoseconds(count); |
| 622 | } |
| 623 | return ret; |
| 624 | } |
| 625 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 626 | nn::GeneralResult<ErrorStatus> unvalidatedConvert(const nn::ErrorStatus& errorStatus) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 627 | switch (errorStatus) { |
| 628 | case nn::ErrorStatus::NONE: |
| 629 | case nn::ErrorStatus::DEVICE_UNAVAILABLE: |
| 630 | case nn::ErrorStatus::GENERAL_FAILURE: |
| 631 | case nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 632 | case nn::ErrorStatus::INVALID_ARGUMENT: |
| 633 | case nn::ErrorStatus::MISSED_DEADLINE_TRANSIENT: |
| 634 | case nn::ErrorStatus::MISSED_DEADLINE_PERSISTENT: |
| 635 | case nn::ErrorStatus::RESOURCE_EXHAUSTED_TRANSIENT: |
| 636 | case nn::ErrorStatus::RESOURCE_EXHAUSTED_PERSISTENT: |
| 637 | return static_cast<ErrorStatus>(errorStatus); |
| 638 | default: |
| 639 | return ErrorStatus::GENERAL_FAILURE; |
| 640 | } |
| 641 | } |
| 642 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 643 | nn::GeneralResult<Priority> convert(const nn::Priority& priority) { |
| 644 | return validatedConvert(priority); |
| 645 | } |
| 646 | |
| 647 | nn::GeneralResult<Capabilities> convert(const nn::Capabilities& capabilities) { |
| 648 | return validatedConvert(capabilities); |
| 649 | } |
| 650 | |
| 651 | nn::GeneralResult<Model> convert(const nn::Model& model) { |
| 652 | return validatedConvert(model); |
| 653 | } |
| 654 | |
| 655 | nn::GeneralResult<BufferDesc> convert(const nn::BufferDesc& bufferDesc) { |
| 656 | return validatedConvert(bufferDesc); |
| 657 | } |
| 658 | |
| 659 | nn::GeneralResult<Request> convert(const nn::Request& request) { |
| 660 | return validatedConvert(request); |
| 661 | } |
| 662 | |
| 663 | nn::GeneralResult<OptionalTimePoint> convert(const nn::OptionalTimePoint& optionalTimePoint) { |
| 664 | return validatedConvert(optionalTimePoint); |
| 665 | } |
| 666 | |
| 667 | nn::GeneralResult<OptionalTimeoutDuration> convert( |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 668 | const nn::OptionalDuration& optionalTimeoutDuration) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 669 | return validatedConvert(optionalTimeoutDuration); |
| 670 | } |
| 671 | |
| 672 | nn::GeneralResult<ErrorStatus> convert(const nn::ErrorStatus& errorStatus) { |
| 673 | return validatedConvert(errorStatus); |
| 674 | } |
| 675 | |
| 676 | nn::GeneralResult<hidl_handle> convert(const nn::SharedHandle& handle) { |
| 677 | return validatedConvert(handle); |
| 678 | } |
| 679 | |
| 680 | nn::GeneralResult<hidl_memory> convert(const nn::Memory& memory) { |
| 681 | return validatedConvert(memory); |
| 682 | } |
| 683 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 684 | nn::GeneralResult<hidl_vec<BufferRole>> convert(const std::vector<nn::BufferRole>& bufferRoles) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 685 | return validatedConvert(bufferRoles); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 686 | } |
| 687 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 688 | nn::GeneralResult<V1_0::DeviceStatus> convert(const nn::DeviceStatus& deviceStatus) { |
| 689 | return V1_2::utils::convert(deviceStatus); |
| 690 | } |
| 691 | |
| 692 | nn::GeneralResult<V1_1::ExecutionPreference> convert( |
| 693 | const nn::ExecutionPreference& executionPreference) { |
| 694 | return V1_2::utils::convert(executionPreference); |
| 695 | } |
| 696 | |
| 697 | nn::GeneralResult<hidl_vec<V1_2::Extension>> convert(const std::vector<nn::Extension>& extensions) { |
| 698 | return V1_2::utils::convert(extensions); |
| 699 | } |
| 700 | |
| 701 | nn::GeneralResult<hidl_vec<hidl_handle>> convert(const std::vector<nn::SharedHandle>& handles) { |
| 702 | return V1_2::utils::convert(handles); |
| 703 | } |
| 704 | |
| 705 | nn::GeneralResult<hidl_vec<V1_2::OutputShape>> convert( |
| 706 | const std::vector<nn::OutputShape>& outputShapes) { |
| 707 | return V1_2::utils::convert(outputShapes); |
| 708 | } |
| 709 | |
| 710 | nn::GeneralResult<V1_2::DeviceType> convert(const nn::DeviceType& deviceType) { |
| 711 | return V1_2::utils::convert(deviceType); |
| 712 | } |
| 713 | |
| 714 | nn::GeneralResult<V1_2::MeasureTiming> convert(const nn::MeasureTiming& measureTiming) { |
| 715 | return V1_2::utils::convert(measureTiming); |
| 716 | } |
| 717 | |
| 718 | nn::GeneralResult<V1_2::Timing> convert(const nn::Timing& timing) { |
| 719 | return V1_2::utils::convert(timing); |
| 720 | } |
| 721 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 722 | } // namespace android::hardware::neuralnetworks::V1_3::utils |