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