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