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 | constexpr auto kTimePointMaxCount = TimePoint::max().time_since_epoch().count(); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 276 | const auto makeTimePoint = [](uint64_t count) -> GeneralResult<OptionalTimePoint> { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 277 | if (count > kTimePointMaxCount) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 278 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 279 | << "Unable to unvalidatedConvert OptionalTimePoint because the count exceeds " |
| 280 | "the max"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 281 | } |
| 282 | const auto nanoseconds = std::chrono::nanoseconds{count}; |
| 283 | return TimePoint{nanoseconds}; |
| 284 | }; |
| 285 | |
| 286 | using Discriminator = hal::V1_3::OptionalTimePoint::hidl_discriminator; |
| 287 | switch (optionalTimePoint.getDiscriminator()) { |
| 288 | case Discriminator::none: |
| 289 | return std::nullopt; |
| 290 | case Discriminator::nanosecondsSinceEpoch: |
| 291 | return makeTimePoint(optionalTimePoint.nanosecondsSinceEpoch()); |
| 292 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 293 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 294 | << "Invalid OptionalTimePoint discriminator " |
| 295 | << underlyingType(optionalTimePoint.getDiscriminator()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 298 | GeneralResult<OptionalTimeoutDuration> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 299 | const hal::V1_3::OptionalTimeoutDuration& optionalTimeoutDuration) { |
| 300 | constexpr auto kTimeoutDurationMaxCount = TimeoutDuration::max().count(); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 301 | const auto makeTimeoutDuration = [](uint64_t count) -> GeneralResult<OptionalTimeoutDuration> { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 302 | if (count > kTimeoutDurationMaxCount) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 303 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 304 | << "Unable to unvalidatedConvert OptionalTimeoutDuration because the count " |
| 305 | "exceeds the max"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 306 | } |
| 307 | return TimeoutDuration{count}; |
| 308 | }; |
| 309 | |
| 310 | using Discriminator = hal::V1_3::OptionalTimeoutDuration::hidl_discriminator; |
| 311 | switch (optionalTimeoutDuration.getDiscriminator()) { |
| 312 | case Discriminator::none: |
| 313 | return std::nullopt; |
| 314 | case Discriminator::nanoseconds: |
| 315 | return makeTimeoutDuration(optionalTimeoutDuration.nanoseconds()); |
| 316 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 317 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 318 | << "Invalid OptionalTimeoutDuration discriminator " |
| 319 | << underlyingType(optionalTimeoutDuration.getDiscriminator()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 322 | GeneralResult<ErrorStatus> unvalidatedConvert(const hal::V1_3::ErrorStatus& status) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 323 | switch (status) { |
| 324 | case hal::V1_3::ErrorStatus::NONE: |
| 325 | case hal::V1_3::ErrorStatus::DEVICE_UNAVAILABLE: |
| 326 | case hal::V1_3::ErrorStatus::GENERAL_FAILURE: |
| 327 | case hal::V1_3::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 328 | case hal::V1_3::ErrorStatus::INVALID_ARGUMENT: |
| 329 | case hal::V1_3::ErrorStatus::MISSED_DEADLINE_TRANSIENT: |
| 330 | case hal::V1_3::ErrorStatus::MISSED_DEADLINE_PERSISTENT: |
| 331 | case hal::V1_3::ErrorStatus::RESOURCE_EXHAUSTED_TRANSIENT: |
| 332 | case hal::V1_3::ErrorStatus::RESOURCE_EXHAUSTED_PERSISTENT: |
| 333 | return static_cast<ErrorStatus>(status); |
| 334 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 335 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 336 | << "Invalid ErrorStatus " << underlyingType(status); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 339 | GeneralResult<Priority> convert(const hal::V1_3::Priority& priority) { |
| 340 | return validatedConvert(priority); |
| 341 | } |
| 342 | |
| 343 | GeneralResult<Capabilities> convert(const hal::V1_3::Capabilities& capabilities) { |
| 344 | return validatedConvert(capabilities); |
| 345 | } |
| 346 | |
| 347 | GeneralResult<Model> convert(const hal::V1_3::Model& model) { |
| 348 | return validatedConvert(model); |
| 349 | } |
| 350 | |
| 351 | GeneralResult<BufferDesc> convert(const hal::V1_3::BufferDesc& bufferDesc) { |
| 352 | return validatedConvert(bufferDesc); |
| 353 | } |
| 354 | |
| 355 | GeneralResult<Request> convert(const hal::V1_3::Request& request) { |
| 356 | return validatedConvert(request); |
| 357 | } |
| 358 | |
| 359 | GeneralResult<OptionalTimePoint> convert(const hal::V1_3::OptionalTimePoint& optionalTimePoint) { |
| 360 | return validatedConvert(optionalTimePoint); |
| 361 | } |
| 362 | |
| 363 | GeneralResult<OptionalTimeoutDuration> convert( |
| 364 | const hal::V1_3::OptionalTimeoutDuration& optionalTimeoutDuration) { |
| 365 | return validatedConvert(optionalTimeoutDuration); |
| 366 | } |
| 367 | |
| 368 | GeneralResult<ErrorStatus> convert(const hal::V1_3::ErrorStatus& errorStatus) { |
| 369 | return validatedConvert(errorStatus); |
| 370 | } |
| 371 | |
| 372 | GeneralResult<SharedHandle> convert(const hardware::hidl_handle& handle) { |
| 373 | return validatedConvert(handle); |
| 374 | } |
| 375 | |
| 376 | GeneralResult<Memory> convert(const hardware::hidl_memory& memory) { |
| 377 | return validatedConvert(memory); |
| 378 | } |
| 379 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 380 | GeneralResult<std::vector<BufferRole>> convert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 381 | const hardware::hidl_vec<hal::V1_3::BufferRole>& bufferRoles) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 382 | return validatedConvert(bufferRoles); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | } // namespace android::nn |
| 386 | |
| 387 | namespace android::hardware::neuralnetworks::V1_3::utils { |
| 388 | namespace { |
| 389 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 390 | using utils::unvalidatedConvert; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 391 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 392 | nn::GeneralResult<V1_0::PerformanceInfo> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 393 | const nn::Capabilities::PerformanceInfo& performanceInfo) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 394 | return V1_0::utils::unvalidatedConvert(performanceInfo); |
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_0::DataLocation> unvalidatedConvert(const nn::DataLocation& dataLocation) { |
| 398 | return V1_0::utils::unvalidatedConvert(dataLocation); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 401 | nn::GeneralResult<hidl_vec<uint8_t>> unvalidatedConvert( |
| 402 | const nn::Model::OperandValues& operandValues) { |
| 403 | return V1_0::utils::unvalidatedConvert(operandValues); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 406 | nn::GeneralResult<hidl_handle> unvalidatedConvert(const nn::SharedHandle& handle) { |
| 407 | return V1_2::utils::unvalidatedConvert(handle); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 410 | nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::Memory& memory) { |
| 411 | return V1_0::utils::unvalidatedConvert(memory); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 414 | nn::GeneralResult<V1_0::RequestArgument> unvalidatedConvert(const nn::Request::Argument& argument) { |
| 415 | return V1_0::utils::unvalidatedConvert(argument); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 418 | nn::GeneralResult<V1_2::Operand::ExtraParams> unvalidatedConvert( |
| 419 | const nn::Operand::ExtraParams& extraParams) { |
| 420 | return V1_2::utils::unvalidatedConvert(extraParams); |
| 421 | } |
| 422 | |
| 423 | nn::GeneralResult<V1_2::Model::ExtensionNameAndPrefix> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 424 | const nn::Model::ExtensionNameAndPrefix& extensionNameAndPrefix) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 425 | return V1_2::utils::unvalidatedConvert(extensionNameAndPrefix); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | template <typename Input> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 429 | using unvalidatedConvertOutput = |
| 430 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 431 | |
| 432 | template <typename Type> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 433 | nn::GeneralResult<hidl_vec<unvalidatedConvertOutput<Type>>> unvalidatedConvertVec( |
| 434 | const std::vector<Type>& arguments) { |
| 435 | hidl_vec<unvalidatedConvertOutput<Type>> halObject(arguments.size()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 436 | for (size_t i = 0; i < arguments.size(); ++i) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 437 | halObject[i] = NN_TRY(unvalidatedConvert(arguments[i])); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 438 | } |
| 439 | return halObject; |
| 440 | } |
| 441 | |
| 442 | template <typename Type> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 443 | nn::GeneralResult<hidl_vec<unvalidatedConvertOutput<Type>>> unvalidatedConvert( |
| 444 | const std::vector<Type>& arguments) { |
| 445 | return unvalidatedConvertVec(arguments); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 448 | nn::GeneralResult<Request::MemoryPool> makeMemoryPool(const nn::Memory& memory) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 449 | Request::MemoryPool ret; |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 450 | ret.hidlMemory(NN_TRY(unvalidatedConvert(memory))); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 451 | return ret; |
| 452 | } |
| 453 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 454 | nn::GeneralResult<Request::MemoryPool> makeMemoryPool(const nn::Request::MemoryDomainToken& token) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 455 | Request::MemoryPool ret; |
| 456 | ret.token(underlyingType(token)); |
| 457 | return ret; |
| 458 | } |
| 459 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 460 | nn::GeneralResult<Request::MemoryPool> makeMemoryPool(const nn::SharedBuffer& /*buffer*/) { |
| 461 | 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] | 462 | } |
| 463 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 464 | using utils::unvalidatedConvert; |
| 465 | |
| 466 | template <typename Type> |
| 467 | decltype(unvalidatedConvert(std::declval<Type>())) validatedConvert(const Type& canonical) { |
| 468 | const auto maybeVersion = nn::validate(canonical); |
| 469 | if (!maybeVersion.has_value()) { |
| 470 | return nn::error() << maybeVersion.error(); |
| 471 | } |
| 472 | const auto version = maybeVersion.value(); |
| 473 | if (version > kVersion) { |
| 474 | return NN_ERROR() << "Insufficient version: " << version << " vs required " << kVersion; |
| 475 | } |
| 476 | return unvalidatedConvert(canonical); |
| 477 | } |
| 478 | |
| 479 | template <typename Type> |
| 480 | nn::GeneralResult<hidl_vec<unvalidatedConvertOutput<Type>>> validatedConvert( |
| 481 | const std::vector<Type>& arguments) { |
| 482 | hidl_vec<unvalidatedConvertOutput<Type>> halObject(arguments.size()); |
| 483 | for (size_t i = 0; i < arguments.size(); ++i) { |
| 484 | halObject[i] = NN_TRY(validatedConvert(arguments[i])); |
| 485 | } |
| 486 | return halObject; |
| 487 | } |
| 488 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 489 | } // anonymous namespace |
| 490 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 491 | nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 492 | return static_cast<OperandType>(operandType); |
| 493 | } |
| 494 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 495 | nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 496 | return static_cast<OperationType>(operationType); |
| 497 | } |
| 498 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 499 | nn::GeneralResult<Priority> unvalidatedConvert(const nn::Priority& priority) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 500 | return static_cast<Priority>(priority); |
| 501 | } |
| 502 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 503 | nn::GeneralResult<Capabilities> unvalidatedConvert(const nn::Capabilities& capabilities) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 504 | std::vector<nn::Capabilities::OperandPerformance> operandPerformance; |
| 505 | operandPerformance.reserve(capabilities.operandPerformance.asVector().size()); |
| 506 | std::copy_if(capabilities.operandPerformance.asVector().begin(), |
| 507 | capabilities.operandPerformance.asVector().end(), |
| 508 | std::back_inserter(operandPerformance), |
| 509 | [](const nn::Capabilities::OperandPerformance& operandPerformance) { |
| 510 | return nn::validOperandType(operandPerformance.type); |
| 511 | }); |
| 512 | |
| 513 | return Capabilities{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 514 | .relaxedFloat32toFloat16PerformanceScalar = NN_TRY( |
| 515 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceScalar)), |
| 516 | .relaxedFloat32toFloat16PerformanceTensor = NN_TRY( |
| 517 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceTensor)), |
| 518 | .operandPerformance = NN_TRY(unvalidatedConvert(operandPerformance)), |
| 519 | .ifPerformance = NN_TRY(unvalidatedConvert(capabilities.ifPerformance)), |
| 520 | .whilePerformance = NN_TRY(unvalidatedConvert(capabilities.whilePerformance)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 521 | }; |
| 522 | } |
| 523 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 524 | nn::GeneralResult<Capabilities::OperandPerformance> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 525 | const nn::Capabilities::OperandPerformance& operandPerformance) { |
| 526 | return Capabilities::OperandPerformance{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 527 | .type = NN_TRY(unvalidatedConvert(operandPerformance.type)), |
| 528 | .info = NN_TRY(unvalidatedConvert(operandPerformance.info)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 529 | }; |
| 530 | } |
| 531 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 532 | nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 533 | return Operation{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 534 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 535 | .inputs = operation.inputs, |
| 536 | .outputs = operation.outputs, |
| 537 | }; |
| 538 | } |
| 539 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 540 | nn::GeneralResult<OperandLifeTime> unvalidatedConvert( |
| 541 | const nn::Operand::LifeTime& operandLifeTime) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 542 | if (operandLifeTime == nn::Operand::LifeTime::POINTER) { |
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 | return static_cast<OperandLifeTime>(operandLifeTime); |
| 547 | } |
| 548 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 549 | nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 550 | return Operand{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 551 | .type = NN_TRY(unvalidatedConvert(operand.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 552 | .dimensions = operand.dimensions, |
| 553 | .numberOfConsumers = 0, |
| 554 | .scale = operand.scale, |
| 555 | .zeroPoint = operand.zeroPoint, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 556 | .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), |
| 557 | .location = NN_TRY(unvalidatedConvert(operand.location)), |
| 558 | .extraParams = NN_TRY(unvalidatedConvert(operand.extraParams)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 559 | }; |
| 560 | } |
| 561 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 562 | nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 563 | if (!hal::utils::hasNoPointerData(model)) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 564 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 565 | << "Model cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | return Model{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 569 | .main = NN_TRY(unvalidatedConvert(model.main)), |
| 570 | .referenced = NN_TRY(unvalidatedConvert(model.referenced)), |
| 571 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 572 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 573 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 574 | .extensionNameToPrefix = NN_TRY(unvalidatedConvert(model.extensionNameToPrefix)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 575 | }; |
| 576 | } |
| 577 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 578 | nn::GeneralResult<Subgraph> unvalidatedConvert(const nn::Model::Subgraph& subgraph) { |
| 579 | auto operands = NN_TRY(unvalidatedConvert(subgraph.operands)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 580 | |
| 581 | // Update number of consumers. |
| 582 | const auto numberOfConsumers = |
| 583 | hal::utils::countNumberOfConsumers(operands.size(), subgraph.operations); |
| 584 | CHECK(operands.size() == numberOfConsumers.size()); |
| 585 | for (size_t i = 0; i < operands.size(); ++i) { |
| 586 | operands[i].numberOfConsumers = numberOfConsumers[i]; |
| 587 | } |
| 588 | |
| 589 | return Subgraph{ |
| 590 | .operands = std::move(operands), |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 591 | .operations = NN_TRY(unvalidatedConvert(subgraph.operations)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 592 | .inputIndexes = subgraph.inputIndexes, |
| 593 | .outputIndexes = subgraph.outputIndexes, |
| 594 | }; |
| 595 | } |
| 596 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 597 | nn::GeneralResult<BufferDesc> unvalidatedConvert(const nn::BufferDesc& bufferDesc) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 598 | return BufferDesc{.dimensions = bufferDesc.dimensions}; |
| 599 | } |
| 600 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 601 | nn::GeneralResult<BufferRole> unvalidatedConvert(const nn::BufferRole& bufferRole) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 602 | return BufferRole{ |
| 603 | .modelIndex = bufferRole.modelIndex, |
| 604 | .ioIndex = bufferRole.ioIndex, |
| 605 | .frequency = bufferRole.frequency, |
| 606 | }; |
| 607 | } |
| 608 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 609 | nn::GeneralResult<Request> unvalidatedConvert(const nn::Request& request) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 610 | if (!hal::utils::hasNoPointerData(request)) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 611 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 612 | << "Request cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | return Request{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 616 | .inputs = NN_TRY(unvalidatedConvert(request.inputs)), |
| 617 | .outputs = NN_TRY(unvalidatedConvert(request.outputs)), |
| 618 | .pools = NN_TRY(unvalidatedConvert(request.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 619 | }; |
| 620 | } |
| 621 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 622 | nn::GeneralResult<Request::MemoryPool> unvalidatedConvert( |
| 623 | const nn::Request::MemoryPool& memoryPool) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 624 | return std::visit([](const auto& o) { return makeMemoryPool(o); }, memoryPool); |
| 625 | } |
| 626 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 627 | nn::GeneralResult<OptionalTimePoint> unvalidatedConvert( |
| 628 | const nn::OptionalTimePoint& optionalTimePoint) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 629 | OptionalTimePoint ret; |
| 630 | if (optionalTimePoint.has_value()) { |
| 631 | const auto count = optionalTimePoint.value().time_since_epoch().count(); |
| 632 | if (count < 0) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 633 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 634 | << "Unable to unvalidatedConvert OptionalTimePoint because time since epoch " |
| 635 | "count is " |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 636 | "negative"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 637 | } |
| 638 | ret.nanosecondsSinceEpoch(count); |
| 639 | } |
| 640 | return ret; |
| 641 | } |
| 642 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 643 | nn::GeneralResult<OptionalTimeoutDuration> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 644 | const nn::OptionalTimeoutDuration& optionalTimeoutDuration) { |
| 645 | OptionalTimeoutDuration ret; |
| 646 | if (optionalTimeoutDuration.has_value()) { |
| 647 | const auto count = optionalTimeoutDuration.value().count(); |
| 648 | if (count < 0) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 649 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 650 | << "Unable to unvalidatedConvert OptionalTimeoutDuration because count is " |
| 651 | "negative"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 652 | } |
| 653 | ret.nanoseconds(count); |
| 654 | } |
| 655 | return ret; |
| 656 | } |
| 657 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 658 | nn::GeneralResult<ErrorStatus> unvalidatedConvert(const nn::ErrorStatus& errorStatus) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 659 | switch (errorStatus) { |
| 660 | case nn::ErrorStatus::NONE: |
| 661 | case nn::ErrorStatus::DEVICE_UNAVAILABLE: |
| 662 | case nn::ErrorStatus::GENERAL_FAILURE: |
| 663 | case nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 664 | case nn::ErrorStatus::INVALID_ARGUMENT: |
| 665 | case nn::ErrorStatus::MISSED_DEADLINE_TRANSIENT: |
| 666 | case nn::ErrorStatus::MISSED_DEADLINE_PERSISTENT: |
| 667 | case nn::ErrorStatus::RESOURCE_EXHAUSTED_TRANSIENT: |
| 668 | case nn::ErrorStatus::RESOURCE_EXHAUSTED_PERSISTENT: |
| 669 | return static_cast<ErrorStatus>(errorStatus); |
| 670 | default: |
| 671 | return ErrorStatus::GENERAL_FAILURE; |
| 672 | } |
| 673 | } |
| 674 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 675 | nn::GeneralResult<Priority> convert(const nn::Priority& priority) { |
| 676 | return validatedConvert(priority); |
| 677 | } |
| 678 | |
| 679 | nn::GeneralResult<Capabilities> convert(const nn::Capabilities& capabilities) { |
| 680 | return validatedConvert(capabilities); |
| 681 | } |
| 682 | |
| 683 | nn::GeneralResult<Model> convert(const nn::Model& model) { |
| 684 | return validatedConvert(model); |
| 685 | } |
| 686 | |
| 687 | nn::GeneralResult<BufferDesc> convert(const nn::BufferDesc& bufferDesc) { |
| 688 | return validatedConvert(bufferDesc); |
| 689 | } |
| 690 | |
| 691 | nn::GeneralResult<Request> convert(const nn::Request& request) { |
| 692 | return validatedConvert(request); |
| 693 | } |
| 694 | |
| 695 | nn::GeneralResult<OptionalTimePoint> convert(const nn::OptionalTimePoint& optionalTimePoint) { |
| 696 | return validatedConvert(optionalTimePoint); |
| 697 | } |
| 698 | |
| 699 | nn::GeneralResult<OptionalTimeoutDuration> convert( |
| 700 | const nn::OptionalTimeoutDuration& optionalTimeoutDuration) { |
| 701 | return validatedConvert(optionalTimeoutDuration); |
| 702 | } |
| 703 | |
| 704 | nn::GeneralResult<ErrorStatus> convert(const nn::ErrorStatus& errorStatus) { |
| 705 | return validatedConvert(errorStatus); |
| 706 | } |
| 707 | |
| 708 | nn::GeneralResult<hidl_handle> convert(const nn::SharedHandle& handle) { |
| 709 | return validatedConvert(handle); |
| 710 | } |
| 711 | |
| 712 | nn::GeneralResult<hidl_memory> convert(const nn::Memory& memory) { |
| 713 | return validatedConvert(memory); |
| 714 | } |
| 715 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 716 | nn::GeneralResult<hidl_vec<BufferRole>> convert(const std::vector<nn::BufferRole>& bufferRoles) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 717 | return validatedConvert(bufferRoles); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | } // namespace android::hardware::neuralnetworks::V1_3::utils |