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