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