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