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