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