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.3/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> |
| 29 | #include <nnapi/hal/1.2/Conversions.h> |
| 30 | #include <nnapi/hal/CommonUtils.h> |
| 31 | |
| 32 | #include <algorithm> |
| 33 | #include <chrono> |
| 34 | #include <functional> |
| 35 | #include <iterator> |
| 36 | #include <limits> |
| 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 | |
Michael Butler | 382d513 | 2021-03-18 21:15:09 -0700 | [diff] [blame] | 44 | std::chrono::nanoseconds makeNanosFromUint64(uint64_t nanoseconds) { |
| 45 | constexpr auto kMaxCount = std::chrono::nanoseconds::max().count(); |
| 46 | using CommonType = std::common_type_t<std::chrono::nanoseconds::rep, uint64_t>; |
| 47 | const auto count = std::min<CommonType>(kMaxCount, nanoseconds); |
| 48 | return std::chrono::nanoseconds{static_cast<std::chrono::nanoseconds::rep>(count)}; |
| 49 | } |
| 50 | |
| 51 | uint64_t makeUint64FromNanos(std::chrono::nanoseconds nanoseconds) { |
| 52 | if (nanoseconds < std::chrono::nanoseconds::zero()) { |
| 53 | return 0; |
| 54 | } |
| 55 | constexpr auto kMaxCount = std::numeric_limits<uint64_t>::max(); |
| 56 | using CommonType = std::common_type_t<std::chrono::nanoseconds::rep, uint64_t>; |
| 57 | const auto count = std::min<CommonType>(kMaxCount, nanoseconds.count()); |
| 58 | return static_cast<uint64_t>(count); |
| 59 | } |
| 60 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 61 | template <typename Type> |
| 62 | constexpr std::underlying_type_t<Type> underlyingType(Type value) { |
| 63 | return static_cast<std::underlying_type_t<Type>>(value); |
| 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | |
| 68 | namespace android::nn { |
| 69 | namespace { |
| 70 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 71 | using hardware::hidl_vec; |
| 72 | |
| 73 | template <typename Input> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 74 | using UnvalidatedConvertOutput = |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 75 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 76 | |
| 77 | template <typename Type> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 78 | GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> unvalidatedConvert( |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 79 | const hidl_vec<Type>& arguments) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 80 | std::vector<UnvalidatedConvertOutput<Type>> canonical; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 81 | canonical.reserve(arguments.size()); |
| 82 | for (const auto& argument : arguments) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 83 | canonical.push_back(NN_TRY(nn::unvalidatedConvert(argument))); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 84 | } |
| 85 | return canonical; |
| 86 | } |
| 87 | |
| 88 | template <typename Type> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 89 | GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& halObject) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 90 | auto canonical = NN_TRY(nn::unvalidatedConvert(halObject)); |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 91 | NN_TRY(hal::V1_3::utils::compliantVersion(canonical)); |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 92 | return canonical; |
| 93 | } |
| 94 | |
| 95 | template <typename Type> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 96 | GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> validatedConvert( |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 97 | const hidl_vec<Type>& arguments) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 98 | std::vector<UnvalidatedConvertOutput<Type>> canonical; |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 99 | canonical.reserve(arguments.size()); |
| 100 | for (const auto& argument : arguments) { |
| 101 | canonical.push_back(NN_TRY(validatedConvert(argument))); |
| 102 | } |
| 103 | return canonical; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // anonymous namespace |
| 107 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 108 | GeneralResult<OperandType> unvalidatedConvert(const hal::V1_3::OperandType& operandType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 109 | return static_cast<OperandType>(operandType); |
| 110 | } |
| 111 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 112 | GeneralResult<OperationType> unvalidatedConvert(const hal::V1_3::OperationType& operationType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 113 | return static_cast<OperationType>(operationType); |
| 114 | } |
| 115 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 116 | GeneralResult<Priority> unvalidatedConvert(const hal::V1_3::Priority& priority) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 117 | return static_cast<Priority>(priority); |
| 118 | } |
| 119 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 120 | GeneralResult<Capabilities> unvalidatedConvert(const hal::V1_3::Capabilities& capabilities) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 121 | const bool validOperandTypes = std::all_of( |
| 122 | capabilities.operandPerformance.begin(), capabilities.operandPerformance.end(), |
| 123 | [](const hal::V1_3::Capabilities::OperandPerformance& operandPerformance) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 124 | return validatedConvert(operandPerformance.type).has_value(); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 125 | }); |
| 126 | if (!validOperandTypes) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 127 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 128 | << "Invalid OperandType when unvalidatedConverting OperandPerformance in " |
| 129 | "Capabilities"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 132 | auto operandPerformance = NN_TRY(unvalidatedConvert(capabilities.operandPerformance)); |
Michael Butler | ff9a5a5 | 2021-10-15 16:23:20 -0700 | [diff] [blame] | 133 | auto table = |
| 134 | NN_TRY(Capabilities::OperandPerformanceTable::create(std::move(operandPerformance))); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 135 | |
| 136 | return Capabilities{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 137 | .relaxedFloat32toFloat16PerformanceScalar = NN_TRY( |
| 138 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceScalar)), |
| 139 | .relaxedFloat32toFloat16PerformanceTensor = NN_TRY( |
| 140 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceTensor)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 141 | .operandPerformance = std::move(table), |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 142 | .ifPerformance = NN_TRY(unvalidatedConvert(capabilities.ifPerformance)), |
| 143 | .whilePerformance = NN_TRY(unvalidatedConvert(capabilities.whilePerformance)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 144 | }; |
| 145 | } |
| 146 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 147 | GeneralResult<Capabilities::OperandPerformance> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 148 | const hal::V1_3::Capabilities::OperandPerformance& operandPerformance) { |
| 149 | return Capabilities::OperandPerformance{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 150 | .type = NN_TRY(unvalidatedConvert(operandPerformance.type)), |
| 151 | .info = NN_TRY(unvalidatedConvert(operandPerformance.info)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 152 | }; |
| 153 | } |
| 154 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 155 | GeneralResult<Operation> unvalidatedConvert(const hal::V1_3::Operation& operation) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 156 | return Operation{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 157 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 158 | .inputs = operation.inputs, |
| 159 | .outputs = operation.outputs, |
| 160 | }; |
| 161 | } |
| 162 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 163 | GeneralResult<Operand::LifeTime> unvalidatedConvert( |
| 164 | const hal::V1_3::OperandLifeTime& operandLifeTime) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 165 | return static_cast<Operand::LifeTime>(operandLifeTime); |
| 166 | } |
| 167 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 168 | GeneralResult<Operand> unvalidatedConvert(const hal::V1_3::Operand& operand) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 169 | return Operand{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 170 | .type = NN_TRY(unvalidatedConvert(operand.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 171 | .dimensions = operand.dimensions, |
| 172 | .scale = operand.scale, |
| 173 | .zeroPoint = operand.zeroPoint, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 174 | .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), |
| 175 | .location = NN_TRY(unvalidatedConvert(operand.location)), |
| 176 | .extraParams = NN_TRY(unvalidatedConvert(operand.extraParams)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 177 | }; |
| 178 | } |
| 179 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 180 | GeneralResult<Model> unvalidatedConvert(const hal::V1_3::Model& model) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 181 | return Model{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 182 | .main = NN_TRY(unvalidatedConvert(model.main)), |
| 183 | .referenced = NN_TRY(unvalidatedConvert(model.referenced)), |
| 184 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 185 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 186 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 187 | .extensionNameToPrefix = NN_TRY(unvalidatedConvert(model.extensionNameToPrefix)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 188 | }; |
| 189 | } |
| 190 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 191 | GeneralResult<Model::Subgraph> unvalidatedConvert(const hal::V1_3::Subgraph& subgraph) { |
| 192 | auto operations = NN_TRY(unvalidatedConvert(subgraph.operations)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 193 | |
| 194 | // Verify number of consumers. |
| 195 | const auto numberOfConsumers = |
Michael Butler | 301ef06 | 2021-10-14 22:04:59 -0700 | [diff] [blame] | 196 | NN_TRY(countNumberOfConsumers(subgraph.operands.size(), operations)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 197 | CHECK(subgraph.operands.size() == numberOfConsumers.size()); |
| 198 | for (size_t i = 0; i < subgraph.operands.size(); ++i) { |
| 199 | if (subgraph.operands[i].numberOfConsumers != numberOfConsumers[i]) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 200 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 201 | << "Invalid numberOfConsumers for operand " << i << ", expected " |
| 202 | << numberOfConsumers[i] << " but found " |
| 203 | << subgraph.operands[i].numberOfConsumers; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | return Model::Subgraph{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 208 | .operands = NN_TRY(unvalidatedConvert(subgraph.operands)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 209 | .operations = std::move(operations), |
| 210 | .inputIndexes = subgraph.inputIndexes, |
| 211 | .outputIndexes = subgraph.outputIndexes, |
| 212 | }; |
| 213 | } |
| 214 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 215 | GeneralResult<BufferDesc> unvalidatedConvert(const hal::V1_3::BufferDesc& bufferDesc) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 216 | return BufferDesc{.dimensions = bufferDesc.dimensions}; |
| 217 | } |
| 218 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 219 | GeneralResult<BufferRole> unvalidatedConvert(const hal::V1_3::BufferRole& bufferRole) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 220 | return BufferRole{ |
| 221 | .modelIndex = bufferRole.modelIndex, |
| 222 | .ioIndex = bufferRole.ioIndex, |
Xusong Wang | 3633d07 | 2021-03-19 13:58:24 -0700 | [diff] [blame] | 223 | .probability = bufferRole.frequency, |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 224 | }; |
| 225 | } |
| 226 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 227 | GeneralResult<Request> unvalidatedConvert(const hal::V1_3::Request& request) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 228 | return Request{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 229 | .inputs = NN_TRY(unvalidatedConvert(request.inputs)), |
| 230 | .outputs = NN_TRY(unvalidatedConvert(request.outputs)), |
| 231 | .pools = NN_TRY(unvalidatedConvert(request.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 232 | }; |
| 233 | } |
| 234 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 235 | GeneralResult<Request::MemoryPool> unvalidatedConvert( |
| 236 | const hal::V1_3::Request::MemoryPool& memoryPool) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 237 | using Discriminator = hal::V1_3::Request::MemoryPool::hidl_discriminator; |
| 238 | switch (memoryPool.getDiscriminator()) { |
| 239 | case Discriminator::hidlMemory: |
Michael Butler | e52a77e | 2021-06-07 13:10:58 -0700 | [diff] [blame] | 240 | return unvalidatedConvert(memoryPool.hidlMemory()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 241 | case Discriminator::token: |
| 242 | return static_cast<Request::MemoryDomainToken>(memoryPool.token()); |
| 243 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 244 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 245 | << "Invalid Request::MemoryPool discriminator " |
| 246 | << underlyingType(memoryPool.getDiscriminator()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 249 | GeneralResult<OptionalTimePoint> unvalidatedConvert( |
| 250 | const hal::V1_3::OptionalTimePoint& optionalTimePoint) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 251 | using Discriminator = hal::V1_3::OptionalTimePoint::hidl_discriminator; |
| 252 | switch (optionalTimePoint.getDiscriminator()) { |
| 253 | case Discriminator::none: |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 254 | return {}; |
Michael Butler | 382d513 | 2021-03-18 21:15:09 -0700 | [diff] [blame] | 255 | case Discriminator::nanosecondsSinceEpoch: { |
| 256 | const auto currentSteadyTime = std::chrono::steady_clock::now(); |
| 257 | const auto currentBootTime = Clock::now(); |
| 258 | |
| 259 | const auto timeSinceEpoch = |
| 260 | makeNanosFromUint64(optionalTimePoint.nanosecondsSinceEpoch()); |
| 261 | const auto steadyTimePoint = std::chrono::steady_clock::time_point{timeSinceEpoch}; |
| 262 | |
| 263 | // Both steadyTimePoint and currentSteadyTime are guaranteed to be non-negative, so this |
| 264 | // subtraction will never overflow or underflow. |
| 265 | const auto timeRemaining = steadyTimePoint - currentSteadyTime; |
| 266 | |
| 267 | // currentBootTime is guaranteed to be non-negative, so this code only protects against |
| 268 | // an overflow. |
| 269 | nn::TimePoint bootTimePoint; |
| 270 | constexpr auto kZeroNano = std::chrono::nanoseconds::zero(); |
| 271 | constexpr auto kMaxTime = nn::TimePoint::max(); |
| 272 | if (timeRemaining > kZeroNano && currentBootTime > kMaxTime - timeRemaining) { |
| 273 | bootTimePoint = kMaxTime; |
| 274 | } else { |
| 275 | bootTimePoint = currentBootTime + timeRemaining; |
| 276 | } |
| 277 | |
| 278 | constexpr auto kZeroTime = nn::TimePoint{}; |
| 279 | return std::max(bootTimePoint, kZeroTime); |
| 280 | } |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 281 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 282 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 283 | << "Invalid OptionalTimePoint discriminator " |
| 284 | << underlyingType(optionalTimePoint.getDiscriminator()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 287 | GeneralResult<OptionalDuration> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 288 | const hal::V1_3::OptionalTimeoutDuration& optionalTimeoutDuration) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 289 | using Discriminator = hal::V1_3::OptionalTimeoutDuration::hidl_discriminator; |
| 290 | switch (optionalTimeoutDuration.getDiscriminator()) { |
| 291 | case Discriminator::none: |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 292 | return {}; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 293 | case Discriminator::nanoseconds: |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 294 | return Duration(optionalTimeoutDuration.nanoseconds()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 295 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 296 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 297 | << "Invalid OptionalTimeoutDuration discriminator " |
| 298 | << underlyingType(optionalTimeoutDuration.getDiscriminator()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 301 | GeneralResult<ErrorStatus> unvalidatedConvert(const hal::V1_3::ErrorStatus& status) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 302 | switch (status) { |
| 303 | case hal::V1_3::ErrorStatus::NONE: |
| 304 | case hal::V1_3::ErrorStatus::DEVICE_UNAVAILABLE: |
| 305 | case hal::V1_3::ErrorStatus::GENERAL_FAILURE: |
| 306 | case hal::V1_3::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 307 | case hal::V1_3::ErrorStatus::INVALID_ARGUMENT: |
| 308 | case hal::V1_3::ErrorStatus::MISSED_DEADLINE_TRANSIENT: |
| 309 | case hal::V1_3::ErrorStatus::MISSED_DEADLINE_PERSISTENT: |
| 310 | case hal::V1_3::ErrorStatus::RESOURCE_EXHAUSTED_TRANSIENT: |
| 311 | case hal::V1_3::ErrorStatus::RESOURCE_EXHAUSTED_PERSISTENT: |
| 312 | return static_cast<ErrorStatus>(status); |
| 313 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 314 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 315 | << "Invalid ErrorStatus " << underlyingType(status); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 318 | GeneralResult<Priority> convert(const hal::V1_3::Priority& priority) { |
| 319 | return validatedConvert(priority); |
| 320 | } |
| 321 | |
| 322 | GeneralResult<Capabilities> convert(const hal::V1_3::Capabilities& capabilities) { |
| 323 | return validatedConvert(capabilities); |
| 324 | } |
| 325 | |
| 326 | GeneralResult<Model> convert(const hal::V1_3::Model& model) { |
| 327 | return validatedConvert(model); |
| 328 | } |
| 329 | |
| 330 | GeneralResult<BufferDesc> convert(const hal::V1_3::BufferDesc& bufferDesc) { |
| 331 | return validatedConvert(bufferDesc); |
| 332 | } |
| 333 | |
| 334 | GeneralResult<Request> convert(const hal::V1_3::Request& request) { |
| 335 | return validatedConvert(request); |
| 336 | } |
| 337 | |
| 338 | GeneralResult<OptionalTimePoint> convert(const hal::V1_3::OptionalTimePoint& optionalTimePoint) { |
| 339 | return validatedConvert(optionalTimePoint); |
| 340 | } |
| 341 | |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 342 | GeneralResult<OptionalDuration> convert( |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 343 | const hal::V1_3::OptionalTimeoutDuration& optionalTimeoutDuration) { |
| 344 | return validatedConvert(optionalTimeoutDuration); |
| 345 | } |
| 346 | |
| 347 | GeneralResult<ErrorStatus> convert(const hal::V1_3::ErrorStatus& errorStatus) { |
| 348 | return validatedConvert(errorStatus); |
| 349 | } |
| 350 | |
| 351 | GeneralResult<SharedHandle> convert(const hardware::hidl_handle& handle) { |
| 352 | return validatedConvert(handle); |
| 353 | } |
| 354 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 355 | GeneralResult<std::vector<BufferRole>> convert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 356 | const hardware::hidl_vec<hal::V1_3::BufferRole>& bufferRoles) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 357 | return validatedConvert(bufferRoles); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | } // namespace android::nn |
| 361 | |
| 362 | namespace android::hardware::neuralnetworks::V1_3::utils { |
| 363 | namespace { |
| 364 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 365 | using utils::unvalidatedConvert; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 366 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 367 | nn::GeneralResult<V1_0::PerformanceInfo> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 368 | const nn::Capabilities::PerformanceInfo& performanceInfo) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 369 | return V1_0::utils::unvalidatedConvert(performanceInfo); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 372 | nn::GeneralResult<V1_0::DataLocation> unvalidatedConvert(const nn::DataLocation& dataLocation) { |
| 373 | return V1_0::utils::unvalidatedConvert(dataLocation); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 376 | nn::GeneralResult<hidl_vec<uint8_t>> unvalidatedConvert( |
| 377 | const nn::Model::OperandValues& operandValues) { |
| 378 | return V1_0::utils::unvalidatedConvert(operandValues); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 381 | nn::GeneralResult<hidl_handle> unvalidatedConvert(const nn::SharedHandle& handle) { |
Michael Butler | 1596582 | 2021-10-14 23:45:11 -0700 | [diff] [blame] | 382 | return V1_0::utils::unvalidatedConvert(handle); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 385 | nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::SharedMemory& memory) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 386 | return V1_0::utils::unvalidatedConvert(memory); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 389 | nn::GeneralResult<V1_0::RequestArgument> unvalidatedConvert(const nn::Request::Argument& argument) { |
| 390 | return V1_0::utils::unvalidatedConvert(argument); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 393 | nn::GeneralResult<V1_2::Operand::ExtraParams> unvalidatedConvert( |
| 394 | const nn::Operand::ExtraParams& extraParams) { |
| 395 | return V1_2::utils::unvalidatedConvert(extraParams); |
| 396 | } |
| 397 | |
| 398 | nn::GeneralResult<V1_2::Model::ExtensionNameAndPrefix> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 399 | const nn::Model::ExtensionNameAndPrefix& extensionNameAndPrefix) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 400 | return V1_2::utils::unvalidatedConvert(extensionNameAndPrefix); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | template <typename Input> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 404 | using UnvalidatedConvertOutput = |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 405 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 406 | |
| 407 | template <typename Type> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 408 | nn::GeneralResult<hidl_vec<UnvalidatedConvertOutput<Type>>> unvalidatedConvert( |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 409 | const std::vector<Type>& arguments) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 410 | hidl_vec<UnvalidatedConvertOutput<Type>> halObject(arguments.size()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 411 | for (size_t i = 0; i < arguments.size(); ++i) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 412 | halObject[i] = NN_TRY(unvalidatedConvert(arguments[i])); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 413 | } |
| 414 | return halObject; |
| 415 | } |
| 416 | |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 417 | nn::GeneralResult<Request::MemoryPool> makeMemoryPool(const nn::SharedMemory& memory) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 418 | Request::MemoryPool ret; |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 419 | ret.hidlMemory(NN_TRY(unvalidatedConvert(memory))); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 420 | return ret; |
| 421 | } |
| 422 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 423 | nn::GeneralResult<Request::MemoryPool> makeMemoryPool(const nn::Request::MemoryDomainToken& token) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 424 | Request::MemoryPool ret; |
| 425 | ret.token(underlyingType(token)); |
| 426 | return ret; |
| 427 | } |
| 428 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 429 | nn::GeneralResult<Request::MemoryPool> makeMemoryPool(const nn::SharedBuffer& /*buffer*/) { |
| 430 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Unable to make memory pool from IBuffer"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 433 | using utils::unvalidatedConvert; |
| 434 | |
| 435 | template <typename Type> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 436 | nn::GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& canonical) { |
| 437 | NN_TRY(compliantVersion(canonical)); |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 438 | return unvalidatedConvert(canonical); |
| 439 | } |
| 440 | |
| 441 | template <typename Type> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 442 | nn::GeneralResult<hidl_vec<UnvalidatedConvertOutput<Type>>> validatedConvert( |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 443 | const std::vector<Type>& arguments) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 444 | hidl_vec<UnvalidatedConvertOutput<Type>> halObject(arguments.size()); |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 445 | for (size_t i = 0; i < arguments.size(); ++i) { |
| 446 | halObject[i] = NN_TRY(validatedConvert(arguments[i])); |
| 447 | } |
| 448 | return halObject; |
| 449 | } |
| 450 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 451 | } // anonymous namespace |
| 452 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 453 | nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 454 | return static_cast<OperandType>(operandType); |
| 455 | } |
| 456 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 457 | nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 458 | return static_cast<OperationType>(operationType); |
| 459 | } |
| 460 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 461 | nn::GeneralResult<Priority> unvalidatedConvert(const nn::Priority& priority) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 462 | return static_cast<Priority>(priority); |
| 463 | } |
| 464 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 465 | nn::GeneralResult<Capabilities> unvalidatedConvert(const nn::Capabilities& capabilities) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 466 | std::vector<nn::Capabilities::OperandPerformance> operandPerformance; |
| 467 | operandPerformance.reserve(capabilities.operandPerformance.asVector().size()); |
| 468 | std::copy_if(capabilities.operandPerformance.asVector().begin(), |
| 469 | capabilities.operandPerformance.asVector().end(), |
| 470 | std::back_inserter(operandPerformance), |
| 471 | [](const nn::Capabilities::OperandPerformance& operandPerformance) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 472 | return compliantVersion(operandPerformance.type).has_value(); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 473 | }); |
| 474 | |
| 475 | return Capabilities{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 476 | .relaxedFloat32toFloat16PerformanceScalar = NN_TRY( |
| 477 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceScalar)), |
| 478 | .relaxedFloat32toFloat16PerformanceTensor = NN_TRY( |
| 479 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceTensor)), |
| 480 | .operandPerformance = NN_TRY(unvalidatedConvert(operandPerformance)), |
| 481 | .ifPerformance = NN_TRY(unvalidatedConvert(capabilities.ifPerformance)), |
| 482 | .whilePerformance = NN_TRY(unvalidatedConvert(capabilities.whilePerformance)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 483 | }; |
| 484 | } |
| 485 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 486 | nn::GeneralResult<Capabilities::OperandPerformance> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 487 | const nn::Capabilities::OperandPerformance& operandPerformance) { |
| 488 | return Capabilities::OperandPerformance{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 489 | .type = NN_TRY(unvalidatedConvert(operandPerformance.type)), |
| 490 | .info = NN_TRY(unvalidatedConvert(operandPerformance.info)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 491 | }; |
| 492 | } |
| 493 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 494 | nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 495 | return Operation{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 496 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 497 | .inputs = operation.inputs, |
| 498 | .outputs = operation.outputs, |
| 499 | }; |
| 500 | } |
| 501 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 502 | nn::GeneralResult<OperandLifeTime> unvalidatedConvert( |
| 503 | const nn::Operand::LifeTime& operandLifeTime) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 504 | if (operandLifeTime == nn::Operand::LifeTime::POINTER) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 505 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 506 | << "Model cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 507 | } |
| 508 | return static_cast<OperandLifeTime>(operandLifeTime); |
| 509 | } |
| 510 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 511 | nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 512 | return Operand{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 513 | .type = NN_TRY(unvalidatedConvert(operand.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 514 | .dimensions = operand.dimensions, |
| 515 | .numberOfConsumers = 0, |
| 516 | .scale = operand.scale, |
| 517 | .zeroPoint = operand.zeroPoint, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 518 | .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), |
| 519 | .location = NN_TRY(unvalidatedConvert(operand.location)), |
| 520 | .extraParams = NN_TRY(unvalidatedConvert(operand.extraParams)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 521 | }; |
| 522 | } |
| 523 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 524 | nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 525 | if (!hal::utils::hasNoPointerData(model)) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 526 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 527 | << "Model cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | return Model{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 531 | .main = NN_TRY(unvalidatedConvert(model.main)), |
| 532 | .referenced = NN_TRY(unvalidatedConvert(model.referenced)), |
| 533 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 534 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 535 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 536 | .extensionNameToPrefix = NN_TRY(unvalidatedConvert(model.extensionNameToPrefix)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 537 | }; |
| 538 | } |
| 539 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 540 | nn::GeneralResult<Subgraph> unvalidatedConvert(const nn::Model::Subgraph& subgraph) { |
| 541 | auto operands = NN_TRY(unvalidatedConvert(subgraph.operands)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 542 | |
| 543 | // Update number of consumers. |
| 544 | const auto numberOfConsumers = |
Michael Butler | 301ef06 | 2021-10-14 22:04:59 -0700 | [diff] [blame] | 545 | NN_TRY(countNumberOfConsumers(operands.size(), subgraph.operations)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 546 | CHECK(operands.size() == numberOfConsumers.size()); |
| 547 | for (size_t i = 0; i < operands.size(); ++i) { |
| 548 | operands[i].numberOfConsumers = numberOfConsumers[i]; |
| 549 | } |
| 550 | |
| 551 | return Subgraph{ |
| 552 | .operands = std::move(operands), |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 553 | .operations = NN_TRY(unvalidatedConvert(subgraph.operations)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 554 | .inputIndexes = subgraph.inputIndexes, |
| 555 | .outputIndexes = subgraph.outputIndexes, |
| 556 | }; |
| 557 | } |
| 558 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 559 | nn::GeneralResult<BufferDesc> unvalidatedConvert(const nn::BufferDesc& bufferDesc) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 560 | return BufferDesc{.dimensions = bufferDesc.dimensions}; |
| 561 | } |
| 562 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 563 | nn::GeneralResult<BufferRole> unvalidatedConvert(const nn::BufferRole& bufferRole) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 564 | return BufferRole{ |
| 565 | .modelIndex = bufferRole.modelIndex, |
| 566 | .ioIndex = bufferRole.ioIndex, |
Xusong Wang | 3633d07 | 2021-03-19 13:58:24 -0700 | [diff] [blame] | 567 | .frequency = bufferRole.probability, |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 568 | }; |
| 569 | } |
| 570 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 571 | nn::GeneralResult<Request> unvalidatedConvert(const nn::Request& request) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 572 | if (!hal::utils::hasNoPointerData(request)) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 573 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 574 | << "Request cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | return Request{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 578 | .inputs = NN_TRY(unvalidatedConvert(request.inputs)), |
| 579 | .outputs = NN_TRY(unvalidatedConvert(request.outputs)), |
| 580 | .pools = NN_TRY(unvalidatedConvert(request.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 581 | }; |
| 582 | } |
| 583 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 584 | nn::GeneralResult<Request::MemoryPool> unvalidatedConvert( |
| 585 | const nn::Request::MemoryPool& memoryPool) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 586 | return std::visit([](const auto& o) { return makeMemoryPool(o); }, memoryPool); |
| 587 | } |
| 588 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 589 | nn::GeneralResult<OptionalTimePoint> unvalidatedConvert( |
| 590 | const nn::OptionalTimePoint& optionalTimePoint) { |
Michael Butler | 382d513 | 2021-03-18 21:15:09 -0700 | [diff] [blame] | 591 | const auto currentSteadyTime = std::chrono::steady_clock::now(); |
| 592 | const auto currentBootTime = nn::Clock::now(); |
| 593 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 594 | OptionalTimePoint ret; |
| 595 | if (optionalTimePoint.has_value()) { |
Michael Butler | 382d513 | 2021-03-18 21:15:09 -0700 | [diff] [blame] | 596 | const auto bootTimePoint = optionalTimePoint.value(); |
| 597 | |
| 598 | if (bootTimePoint < nn::TimePoint{}) { |
| 599 | return NN_ERROR() << "Trying to cast invalid time point"; |
| 600 | } |
| 601 | |
| 602 | // Both bootTimePoint and currentBootTime are guaranteed to be non-negative, so this |
| 603 | // subtraction will never overflow or underflow. |
| 604 | const auto timeRemaining = bootTimePoint - currentBootTime; |
| 605 | |
| 606 | // currentSteadyTime is guaranteed to be non-negative, so this code only protects against an |
| 607 | // overflow. |
| 608 | std::chrono::steady_clock::time_point steadyTimePoint; |
| 609 | constexpr auto kZeroNano = std::chrono::nanoseconds::zero(); |
| 610 | constexpr auto kMaxTime = std::chrono::steady_clock::time_point::max(); |
| 611 | if (timeRemaining > kZeroNano && currentSteadyTime > kMaxTime - timeRemaining) { |
| 612 | steadyTimePoint = kMaxTime; |
| 613 | } else { |
| 614 | steadyTimePoint = currentSteadyTime + timeRemaining; |
| 615 | } |
| 616 | |
| 617 | const uint64_t count = makeUint64FromNanos(steadyTimePoint.time_since_epoch()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 618 | ret.nanosecondsSinceEpoch(count); |
| 619 | } |
| 620 | return ret; |
| 621 | } |
| 622 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 623 | nn::GeneralResult<OptionalTimeoutDuration> unvalidatedConvert( |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 624 | const nn::OptionalDuration& optionalTimeoutDuration) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 625 | OptionalTimeoutDuration ret; |
| 626 | if (optionalTimeoutDuration.has_value()) { |
| 627 | const auto count = optionalTimeoutDuration.value().count(); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 628 | ret.nanoseconds(count); |
| 629 | } |
| 630 | return ret; |
| 631 | } |
| 632 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 633 | nn::GeneralResult<ErrorStatus> unvalidatedConvert(const nn::ErrorStatus& errorStatus) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 634 | switch (errorStatus) { |
| 635 | case nn::ErrorStatus::NONE: |
| 636 | case nn::ErrorStatus::DEVICE_UNAVAILABLE: |
| 637 | case nn::ErrorStatus::GENERAL_FAILURE: |
| 638 | case nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 639 | case nn::ErrorStatus::INVALID_ARGUMENT: |
| 640 | case nn::ErrorStatus::MISSED_DEADLINE_TRANSIENT: |
| 641 | case nn::ErrorStatus::MISSED_DEADLINE_PERSISTENT: |
| 642 | case nn::ErrorStatus::RESOURCE_EXHAUSTED_TRANSIENT: |
| 643 | case nn::ErrorStatus::RESOURCE_EXHAUSTED_PERSISTENT: |
| 644 | return static_cast<ErrorStatus>(errorStatus); |
| 645 | default: |
| 646 | return ErrorStatus::GENERAL_FAILURE; |
| 647 | } |
| 648 | } |
| 649 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 650 | nn::GeneralResult<Priority> convert(const nn::Priority& priority) { |
| 651 | return validatedConvert(priority); |
| 652 | } |
| 653 | |
| 654 | nn::GeneralResult<Capabilities> convert(const nn::Capabilities& capabilities) { |
| 655 | return validatedConvert(capabilities); |
| 656 | } |
| 657 | |
| 658 | nn::GeneralResult<Model> convert(const nn::Model& model) { |
| 659 | return validatedConvert(model); |
| 660 | } |
| 661 | |
| 662 | nn::GeneralResult<BufferDesc> convert(const nn::BufferDesc& bufferDesc) { |
| 663 | return validatedConvert(bufferDesc); |
| 664 | } |
| 665 | |
| 666 | nn::GeneralResult<Request> convert(const nn::Request& request) { |
| 667 | return validatedConvert(request); |
| 668 | } |
| 669 | |
| 670 | nn::GeneralResult<OptionalTimePoint> convert(const nn::OptionalTimePoint& optionalTimePoint) { |
| 671 | return validatedConvert(optionalTimePoint); |
| 672 | } |
| 673 | |
| 674 | nn::GeneralResult<OptionalTimeoutDuration> convert( |
Michael Butler | 4024d8f | 2020-12-04 17:38:20 -0800 | [diff] [blame] | 675 | const nn::OptionalDuration& optionalTimeoutDuration) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 676 | return validatedConvert(optionalTimeoutDuration); |
| 677 | } |
| 678 | |
| 679 | nn::GeneralResult<ErrorStatus> convert(const nn::ErrorStatus& errorStatus) { |
| 680 | return validatedConvert(errorStatus); |
| 681 | } |
| 682 | |
| 683 | nn::GeneralResult<hidl_handle> convert(const nn::SharedHandle& handle) { |
| 684 | return validatedConvert(handle); |
| 685 | } |
| 686 | |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 687 | nn::GeneralResult<hidl_memory> convert(const nn::SharedMemory& memory) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 688 | return validatedConvert(memory); |
| 689 | } |
| 690 | |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 691 | nn::GeneralResult<hidl_vec<BufferRole>> convert(const std::vector<nn::BufferRole>& bufferRoles) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 692 | return validatedConvert(bufferRoles); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 693 | } |
| 694 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 695 | nn::GeneralResult<V1_0::DeviceStatus> convert(const nn::DeviceStatus& deviceStatus) { |
| 696 | return V1_2::utils::convert(deviceStatus); |
| 697 | } |
| 698 | |
| 699 | nn::GeneralResult<V1_1::ExecutionPreference> convert( |
| 700 | const nn::ExecutionPreference& executionPreference) { |
| 701 | return V1_2::utils::convert(executionPreference); |
| 702 | } |
| 703 | |
| 704 | nn::GeneralResult<hidl_vec<V1_2::Extension>> convert(const std::vector<nn::Extension>& extensions) { |
| 705 | return V1_2::utils::convert(extensions); |
| 706 | } |
| 707 | |
| 708 | nn::GeneralResult<hidl_vec<hidl_handle>> convert(const std::vector<nn::SharedHandle>& handles) { |
| 709 | return V1_2::utils::convert(handles); |
| 710 | } |
| 711 | |
| 712 | nn::GeneralResult<hidl_vec<V1_2::OutputShape>> convert( |
| 713 | const std::vector<nn::OutputShape>& outputShapes) { |
| 714 | return V1_2::utils::convert(outputShapes); |
| 715 | } |
| 716 | |
| 717 | nn::GeneralResult<V1_2::DeviceType> convert(const nn::DeviceType& deviceType) { |
| 718 | return V1_2::utils::convert(deviceType); |
| 719 | } |
| 720 | |
| 721 | nn::GeneralResult<V1_2::MeasureTiming> convert(const nn::MeasureTiming& measureTiming) { |
| 722 | return V1_2::utils::convert(measureTiming); |
| 723 | } |
| 724 | |
| 725 | nn::GeneralResult<V1_2::Timing> convert(const nn::Timing& timing) { |
| 726 | return V1_2::utils::convert(timing); |
| 727 | } |
| 728 | |
Michael Butler | 1596582 | 2021-10-14 23:45:11 -0700 | [diff] [blame] | 729 | nn::GeneralResult<hidl_vec<hidl_handle>> convertSyncFences( |
| 730 | const std::vector<nn::SyncFence>& syncFences) { |
| 731 | std::vector<nn::SharedHandle> handles; |
| 732 | handles.reserve(syncFences.size()); |
| 733 | std::transform(syncFences.begin(), syncFences.end(), std::back_inserter(handles), |
| 734 | [](const nn::SyncFence& syncFence) { return syncFence.getSharedHandle(); }); |
| 735 | return convert(handles); |
| 736 | } |
| 737 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 738 | } // namespace android::hardware::neuralnetworks::V1_3::utils |