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) { |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame^] | 307 | if (hidlHandle.getNativeHandle() == nullptr) { |
| 308 | return nullptr; |
| 309 | } |
| 310 | auto handle = NN_TRY(hal::utils::sharedHandleFromNativeHandle(hidlHandle.getNativeHandle())); |
| 311 | return std::make_shared<const Handle>(std::move(handle)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 314 | GeneralResult<DeviceType> convert(const hal::V1_2::DeviceType& deviceType) { |
| 315 | return validatedConvert(deviceType); |
| 316 | } |
| 317 | |
| 318 | GeneralResult<Capabilities> convert(const hal::V1_2::Capabilities& capabilities) { |
| 319 | return validatedConvert(capabilities); |
| 320 | } |
| 321 | |
| 322 | GeneralResult<Model> convert(const hal::V1_2::Model& model) { |
| 323 | return validatedConvert(model); |
| 324 | } |
| 325 | |
| 326 | GeneralResult<MeasureTiming> convert(const hal::V1_2::MeasureTiming& measureTiming) { |
| 327 | return validatedConvert(measureTiming); |
| 328 | } |
| 329 | |
| 330 | GeneralResult<Timing> convert(const hal::V1_2::Timing& timing) { |
| 331 | return validatedConvert(timing); |
| 332 | } |
| 333 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 334 | 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] | 335 | return validatedConvert(extensions); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Slava Shklyaev | 49817a0 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 338 | GeneralResult<std::vector<SharedHandle>> convert(const hidl_vec<hidl_handle>& handles) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 339 | return validatedConvert(handles); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 342 | GeneralResult<std::vector<OutputShape>> convert( |
| 343 | const hidl_vec<hal::V1_2::OutputShape>& outputShapes) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 344 | return validatedConvert(outputShapes); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | } // namespace android::nn |
| 348 | |
| 349 | namespace android::hardware::neuralnetworks::V1_2::utils { |
| 350 | namespace { |
| 351 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 352 | using utils::unvalidatedConvert; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 353 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 354 | nn::GeneralResult<V1_0::OperandLifeTime> unvalidatedConvert(const nn::Operand::LifeTime& lifetime) { |
| 355 | return V1_0::utils::unvalidatedConvert(lifetime); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 358 | nn::GeneralResult<V1_0::PerformanceInfo> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 359 | const nn::Capabilities::PerformanceInfo& performanceInfo) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 360 | return V1_0::utils::unvalidatedConvert(performanceInfo); |
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<V1_0::DataLocation> unvalidatedConvert(const nn::DataLocation& location) { |
| 364 | return V1_0::utils::unvalidatedConvert(location); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 367 | nn::GeneralResult<hidl_vec<uint8_t>> unvalidatedConvert( |
| 368 | const nn::Model::OperandValues& operandValues) { |
| 369 | return V1_0::utils::unvalidatedConvert(operandValues); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 372 | nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::SharedMemory& memory) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 373 | return V1_0::utils::unvalidatedConvert(memory); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | template <typename Input> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 377 | using unvalidatedConvertOutput = |
| 378 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 379 | |
| 380 | template <typename Type> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 381 | nn::GeneralResult<hidl_vec<unvalidatedConvertOutput<Type>>> unvalidatedConvertVec( |
| 382 | const std::vector<Type>& arguments) { |
| 383 | hidl_vec<unvalidatedConvertOutput<Type>> halObject(arguments.size()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 384 | for (size_t i = 0; i < arguments.size(); ++i) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 385 | halObject[i] = NN_TRY(unvalidatedConvert(arguments[i])); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 386 | } |
| 387 | return halObject; |
| 388 | } |
| 389 | |
| 390 | template <typename Type> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 391 | nn::GeneralResult<hidl_vec<unvalidatedConvertOutput<Type>>> unvalidatedConvert( |
| 392 | const std::vector<Type>& arguments) { |
| 393 | return unvalidatedConvertVec(arguments); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 396 | nn::GeneralResult<Operand::ExtraParams> makeExtraParams(nn::Operand::NoParams /*noParams*/) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 397 | return Operand::ExtraParams{}; |
| 398 | } |
| 399 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 400 | nn::GeneralResult<Operand::ExtraParams> makeExtraParams( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 401 | const nn::Operand::SymmPerChannelQuantParams& channelQuant) { |
| 402 | Operand::ExtraParams ret; |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 403 | ret.channelQuant(NN_TRY(unvalidatedConvert(channelQuant))); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 404 | return ret; |
| 405 | } |
| 406 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 407 | nn::GeneralResult<Operand::ExtraParams> makeExtraParams( |
| 408 | const nn::Operand::ExtensionParams& extension) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 409 | Operand::ExtraParams ret; |
| 410 | ret.extension(extension); |
| 411 | return ret; |
| 412 | } |
| 413 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 414 | template <typename Type> |
| 415 | decltype(utils::unvalidatedConvert(std::declval<Type>())) validatedConvert(const Type& canonical) { |
| 416 | const auto maybeVersion = nn::validate(canonical); |
| 417 | if (!maybeVersion.has_value()) { |
| 418 | return nn::error() << maybeVersion.error(); |
| 419 | } |
| 420 | const auto version = maybeVersion.value(); |
| 421 | if (version > kVersion) { |
| 422 | return NN_ERROR() << "Insufficient version: " << version << " vs required " << kVersion; |
| 423 | } |
| 424 | return utils::unvalidatedConvert(canonical); |
| 425 | } |
| 426 | |
| 427 | template <typename Type> |
| 428 | nn::GeneralResult<hidl_vec<unvalidatedConvertOutput<Type>>> validatedConvert( |
| 429 | const std::vector<Type>& arguments) { |
| 430 | hidl_vec<unvalidatedConvertOutput<Type>> halObject(arguments.size()); |
| 431 | for (size_t i = 0; i < arguments.size(); ++i) { |
| 432 | halObject[i] = NN_TRY(validatedConvert(arguments[i])); |
| 433 | } |
| 434 | return halObject; |
| 435 | } |
| 436 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 437 | } // anonymous namespace |
| 438 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 439 | nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 440 | return static_cast<OperandType>(operandType); |
| 441 | } |
| 442 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 443 | nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 444 | return static_cast<OperationType>(operationType); |
| 445 | } |
| 446 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 447 | nn::GeneralResult<DeviceType> unvalidatedConvert(const nn::DeviceType& deviceType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 448 | switch (deviceType) { |
| 449 | case nn::DeviceType::UNKNOWN: |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 450 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Invalid DeviceType UNKNOWN"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 451 | case nn::DeviceType::OTHER: |
| 452 | case nn::DeviceType::CPU: |
| 453 | case nn::DeviceType::GPU: |
| 454 | case nn::DeviceType::ACCELERATOR: |
| 455 | return static_cast<DeviceType>(deviceType); |
| 456 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 457 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 458 | << "Invalid DeviceType " << underlyingType(deviceType); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 461 | nn::GeneralResult<Capabilities> unvalidatedConvert(const nn::Capabilities& capabilities) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 462 | std::vector<nn::Capabilities::OperandPerformance> operandPerformance; |
| 463 | operandPerformance.reserve(capabilities.operandPerformance.asVector().size()); |
| 464 | std::copy_if(capabilities.operandPerformance.asVector().begin(), |
| 465 | capabilities.operandPerformance.asVector().end(), |
| 466 | std::back_inserter(operandPerformance), |
| 467 | [](const nn::Capabilities::OperandPerformance& operandPerformance) { |
| 468 | return nn::validOperandType(operandPerformance.type); |
| 469 | }); |
| 470 | |
| 471 | return Capabilities{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 472 | .relaxedFloat32toFloat16PerformanceScalar = NN_TRY( |
| 473 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceScalar)), |
| 474 | .relaxedFloat32toFloat16PerformanceTensor = NN_TRY( |
| 475 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceTensor)), |
| 476 | .operandPerformance = NN_TRY(unvalidatedConvert(operandPerformance)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 477 | }; |
| 478 | } |
| 479 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 480 | nn::GeneralResult<Capabilities::OperandPerformance> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 481 | const nn::Capabilities::OperandPerformance& operandPerformance) { |
| 482 | return Capabilities::OperandPerformance{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 483 | .type = NN_TRY(unvalidatedConvert(operandPerformance.type)), |
| 484 | .info = NN_TRY(unvalidatedConvert(operandPerformance.info)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 485 | }; |
| 486 | } |
| 487 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 488 | nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 489 | return Operation{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 490 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 491 | .inputs = operation.inputs, |
| 492 | .outputs = operation.outputs, |
| 493 | }; |
| 494 | } |
| 495 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 496 | nn::GeneralResult<SymmPerChannelQuantParams> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 497 | const nn::Operand::SymmPerChannelQuantParams& symmPerChannelQuantParams) { |
| 498 | return SymmPerChannelQuantParams{ |
| 499 | .scales = symmPerChannelQuantParams.scales, |
| 500 | .channelDim = symmPerChannelQuantParams.channelDim, |
| 501 | }; |
| 502 | } |
| 503 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 504 | nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 505 | return Operand{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 506 | .type = NN_TRY(unvalidatedConvert(operand.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 507 | .dimensions = operand.dimensions, |
| 508 | .numberOfConsumers = 0, |
| 509 | .scale = operand.scale, |
| 510 | .zeroPoint = operand.zeroPoint, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 511 | .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), |
| 512 | .location = NN_TRY(unvalidatedConvert(operand.location)), |
| 513 | .extraParams = NN_TRY(unvalidatedConvert(operand.extraParams)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 514 | }; |
| 515 | } |
| 516 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 517 | nn::GeneralResult<Operand::ExtraParams> unvalidatedConvert( |
| 518 | const nn::Operand::ExtraParams& extraParams) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 519 | return std::visit([](const auto& x) { return makeExtraParams(x); }, extraParams); |
| 520 | } |
| 521 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 522 | nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 523 | if (!hal::utils::hasNoPointerData(model)) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 524 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 525 | << "Model cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 528 | auto operands = NN_TRY(unvalidatedConvert(model.main.operands)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 529 | |
| 530 | // Update number of consumers. |
| 531 | const auto numberOfConsumers = |
| 532 | hal::utils::countNumberOfConsumers(operands.size(), model.main.operations); |
| 533 | CHECK(operands.size() == numberOfConsumers.size()); |
| 534 | for (size_t i = 0; i < operands.size(); ++i) { |
| 535 | operands[i].numberOfConsumers = numberOfConsumers[i]; |
| 536 | } |
| 537 | |
| 538 | return Model{ |
| 539 | .operands = std::move(operands), |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 540 | .operations = NN_TRY(unvalidatedConvert(model.main.operations)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 541 | .inputIndexes = model.main.inputIndexes, |
| 542 | .outputIndexes = model.main.outputIndexes, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 543 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 544 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 545 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 546 | .extensionNameToPrefix = NN_TRY(unvalidatedConvert(model.extensionNameToPrefix)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 547 | }; |
| 548 | } |
| 549 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 550 | nn::GeneralResult<Model::ExtensionNameAndPrefix> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 551 | const nn::Model::ExtensionNameAndPrefix& extensionNameAndPrefix) { |
| 552 | return Model::ExtensionNameAndPrefix{ |
| 553 | .name = extensionNameAndPrefix.name, |
| 554 | .prefix = extensionNameAndPrefix.prefix, |
| 555 | }; |
| 556 | } |
| 557 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 558 | nn::GeneralResult<OutputShape> unvalidatedConvert(const nn::OutputShape& outputShape) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 559 | return OutputShape{.dimensions = outputShape.dimensions, |
| 560 | .isSufficient = outputShape.isSufficient}; |
| 561 | } |
| 562 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 563 | nn::GeneralResult<MeasureTiming> unvalidatedConvert(const nn::MeasureTiming& measureTiming) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 564 | return static_cast<MeasureTiming>(measureTiming); |
| 565 | } |
| 566 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 567 | nn::GeneralResult<Timing> unvalidatedConvert(const nn::Timing& timing) { |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 568 | constexpr auto convertTiming = [](nn::OptionalDuration canonicalTiming) -> uint64_t { |
| 569 | if (!canonicalTiming.has_value()) { |
| 570 | return kNoTiming; |
| 571 | } |
| 572 | return std::chrono::ceil<HalDuration>(*canonicalTiming).count(); |
| 573 | }; |
| 574 | return Timing{.timeOnDevice = convertTiming(timing.timeOnDevice), |
| 575 | .timeInDriver = convertTiming(timing.timeInDriver)}; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 578 | nn::GeneralResult<Extension> unvalidatedConvert(const nn::Extension& extension) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 579 | return Extension{ |
| 580 | .name = extension.name, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 581 | .operandTypes = NN_TRY(unvalidatedConvert(extension.operandTypes)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 582 | }; |
| 583 | } |
| 584 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 585 | nn::GeneralResult<Extension::OperandTypeInformation> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 586 | const nn::Extension::OperandTypeInformation& operandTypeInformation) { |
| 587 | return Extension::OperandTypeInformation{ |
| 588 | .type = operandTypeInformation.type, |
| 589 | .isTensor = operandTypeInformation.isTensor, |
| 590 | .byteSize = operandTypeInformation.byteSize, |
| 591 | }; |
| 592 | } |
| 593 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 594 | nn::GeneralResult<hidl_handle> unvalidatedConvert(const nn::SharedHandle& handle) { |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame^] | 595 | if (handle == nullptr) { |
| 596 | return {}; |
| 597 | } |
| 598 | return hal::utils::hidlHandleFromSharedHandle(*handle); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 599 | } |
| 600 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 601 | nn::GeneralResult<DeviceType> convert(const nn::DeviceType& deviceType) { |
| 602 | return validatedConvert(deviceType); |
| 603 | } |
| 604 | |
| 605 | nn::GeneralResult<Capabilities> convert(const nn::Capabilities& capabilities) { |
| 606 | return validatedConvert(capabilities); |
| 607 | } |
| 608 | |
| 609 | nn::GeneralResult<Model> convert(const nn::Model& model) { |
| 610 | return validatedConvert(model); |
| 611 | } |
| 612 | |
| 613 | nn::GeneralResult<MeasureTiming> convert(const nn::MeasureTiming& measureTiming) { |
| 614 | return validatedConvert(measureTiming); |
| 615 | } |
| 616 | |
| 617 | nn::GeneralResult<Timing> convert(const nn::Timing& timing) { |
| 618 | return validatedConvert(timing); |
| 619 | } |
| 620 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 621 | nn::GeneralResult<hidl_vec<Extension>> convert(const std::vector<nn::Extension>& extensions) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 622 | return validatedConvert(extensions); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Slava Shklyaev | 49817a0 | 2020-10-27 18:44:01 +0000 | [diff] [blame] | 625 | 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] | 626 | return validatedConvert(handles); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 629 | nn::GeneralResult<hidl_vec<OutputShape>> convert(const std::vector<nn::OutputShape>& outputShapes) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 630 | return validatedConvert(outputShapes); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 633 | nn::GeneralResult<V1_0::DeviceStatus> convert(const nn::DeviceStatus& deviceStatus) { |
| 634 | return V1_1::utils::convert(deviceStatus); |
| 635 | } |
| 636 | |
| 637 | nn::GeneralResult<V1_0::Request> convert(const nn::Request& request) { |
| 638 | return V1_1::utils::convert(request); |
| 639 | } |
| 640 | |
| 641 | nn::GeneralResult<V1_0::ErrorStatus> convert(const nn::ErrorStatus& status) { |
| 642 | return V1_1::utils::convert(status); |
| 643 | } |
| 644 | |
| 645 | nn::GeneralResult<V1_1::ExecutionPreference> convert( |
| 646 | const nn::ExecutionPreference& executionPreference) { |
| 647 | return V1_1::utils::convert(executionPreference); |
| 648 | } |
| 649 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 650 | } // namespace android::hardware::neuralnetworks::V1_2::utils |