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