Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Conversions.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android/hardware/neuralnetworks/1.0/types.h> |
| 21 | #include <nnapi/OperandTypes.h> |
| 22 | #include <nnapi/OperationTypes.h> |
| 23 | #include <nnapi/Result.h> |
| 24 | #include <nnapi/SharedMemory.h> |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 25 | #include <nnapi/TypeUtils.h> |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 26 | #include <nnapi/Types.h> |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 27 | #include <nnapi/Validation.h> |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 28 | #include <nnapi/hal/CommonUtils.h> |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <functional> |
| 32 | #include <iterator> |
| 33 | #include <memory> |
| 34 | #include <type_traits> |
| 35 | #include <utility> |
| 36 | #include <variant> |
| 37 | |
Michael Butler | 08ee3f9 | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 38 | #include "Utils.h" |
| 39 | |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | template <typename Type> |
| 43 | constexpr std::underlying_type_t<Type> underlyingType(Type value) { |
| 44 | return static_cast<std::underlying_type_t<Type>>(value); |
| 45 | } |
| 46 | |
| 47 | } // namespace |
| 48 | |
| 49 | namespace android::nn { |
| 50 | namespace { |
| 51 | |
| 52 | using hardware::hidl_memory; |
| 53 | using hardware::hidl_vec; |
| 54 | |
| 55 | template <typename Input> |
Michael Butler | 08ee3f9 | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 56 | using UnvalidatedConvertOutput = |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 57 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 58 | |
| 59 | template <typename Type> |
Michael Butler | 08ee3f9 | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 60 | GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> unvalidatedConvert( |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 61 | const hidl_vec<Type>& arguments) { |
Michael Butler | 08ee3f9 | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 62 | std::vector<UnvalidatedConvertOutput<Type>> canonical; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 63 | canonical.reserve(arguments.size()); |
| 64 | for (const auto& argument : arguments) { |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 65 | canonical.push_back(NN_TRY(nn::unvalidatedConvert(argument))); |
| 66 | } |
| 67 | return canonical; |
| 68 | } |
| 69 | |
| 70 | template <typename Type> |
Michael Butler | 08ee3f9 | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 71 | GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& halObject) { |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 72 | auto canonical = NN_TRY(nn::unvalidatedConvert(halObject)); |
Michael Butler | 08ee3f9 | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 73 | NN_TRY(hal::V1_0::utils::compliantVersion(canonical)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 74 | return canonical; |
| 75 | } |
| 76 | |
| 77 | } // anonymous namespace |
| 78 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 79 | GeneralResult<OperandType> unvalidatedConvert(const hal::V1_0::OperandType& operandType) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 80 | return static_cast<OperandType>(operandType); |
| 81 | } |
| 82 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 83 | GeneralResult<OperationType> unvalidatedConvert(const hal::V1_0::OperationType& operationType) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 84 | return static_cast<OperationType>(operationType); |
| 85 | } |
| 86 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 87 | GeneralResult<Operand::LifeTime> unvalidatedConvert(const hal::V1_0::OperandLifeTime& lifetime) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 88 | return static_cast<Operand::LifeTime>(lifetime); |
| 89 | } |
| 90 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 91 | GeneralResult<DeviceStatus> unvalidatedConvert(const hal::V1_0::DeviceStatus& deviceStatus) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 92 | return static_cast<DeviceStatus>(deviceStatus); |
| 93 | } |
| 94 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 95 | GeneralResult<Capabilities::PerformanceInfo> unvalidatedConvert( |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 96 | const hal::V1_0::PerformanceInfo& performanceInfo) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 97 | return Capabilities::PerformanceInfo{ |
| 98 | .execTime = performanceInfo.execTime, |
| 99 | .powerUsage = performanceInfo.powerUsage, |
| 100 | }; |
| 101 | } |
| 102 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 103 | GeneralResult<Capabilities> unvalidatedConvert(const hal::V1_0::Capabilities& capabilities) { |
| 104 | const auto quantized8Performance = |
| 105 | NN_TRY(unvalidatedConvert(capabilities.quantized8Performance)); |
| 106 | const auto float32Performance = NN_TRY(unvalidatedConvert(capabilities.float32Performance)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 107 | |
| 108 | auto table = hal::utils::makeQuantized8PerformanceConsistentWithP(float32Performance, |
| 109 | quantized8Performance); |
| 110 | |
| 111 | return Capabilities{ |
| 112 | .relaxedFloat32toFloat16PerformanceScalar = float32Performance, |
| 113 | .relaxedFloat32toFloat16PerformanceTensor = float32Performance, |
| 114 | .operandPerformance = std::move(table), |
| 115 | }; |
| 116 | } |
| 117 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 118 | GeneralResult<DataLocation> unvalidatedConvert(const hal::V1_0::DataLocation& location) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 119 | return DataLocation{ |
| 120 | .poolIndex = location.poolIndex, |
| 121 | .offset = location.offset, |
| 122 | .length = location.length, |
| 123 | }; |
| 124 | } |
| 125 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 126 | GeneralResult<Operand> unvalidatedConvert(const hal::V1_0::Operand& operand) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 127 | return Operand{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 128 | .type = NN_TRY(unvalidatedConvert(operand.type)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 129 | .dimensions = operand.dimensions, |
| 130 | .scale = operand.scale, |
| 131 | .zeroPoint = operand.zeroPoint, |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 132 | .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), |
| 133 | .location = NN_TRY(unvalidatedConvert(operand.location)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 134 | }; |
| 135 | } |
| 136 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 137 | GeneralResult<Operation> unvalidatedConvert(const hal::V1_0::Operation& operation) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 138 | return Operation{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 139 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 140 | .inputs = operation.inputs, |
| 141 | .outputs = operation.outputs, |
| 142 | }; |
| 143 | } |
| 144 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 145 | GeneralResult<Model::OperandValues> unvalidatedConvert(const hidl_vec<uint8_t>& operandValues) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 146 | return Model::OperandValues(operandValues.data(), operandValues.size()); |
| 147 | } |
| 148 | |
Michael Butler | 79a16eb | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 149 | GeneralResult<SharedMemory> unvalidatedConvert(const hidl_memory& memory) { |
Michael Butler | bbe43d9 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 150 | return hal::utils::createSharedMemoryFromHidlMemory(memory); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 153 | GeneralResult<Model> unvalidatedConvert(const hal::V1_0::Model& model) { |
| 154 | auto operations = NN_TRY(unvalidatedConvert(model.operations)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 155 | |
| 156 | // Verify number of consumers. |
| 157 | const auto numberOfConsumers = |
Michael Butler | c4d9800 | 2021-02-09 15:36:11 -0800 | [diff] [blame] | 158 | NN_TRY(hal::utils::countNumberOfConsumers(model.operands.size(), operations)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 159 | CHECK(model.operands.size() == numberOfConsumers.size()); |
| 160 | for (size_t i = 0; i < model.operands.size(); ++i) { |
| 161 | if (model.operands[i].numberOfConsumers != numberOfConsumers[i]) { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 162 | return NN_ERROR(ErrorStatus::GENERAL_FAILURE) |
| 163 | << "Invalid numberOfConsumers for operand " << i << ", expected " |
| 164 | << numberOfConsumers[i] << " but found " << model.operands[i].numberOfConsumers; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | auto main = Model::Subgraph{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 169 | .operands = NN_TRY(unvalidatedConvert(model.operands)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 170 | .operations = std::move(operations), |
| 171 | .inputIndexes = model.inputIndexes, |
| 172 | .outputIndexes = model.outputIndexes, |
| 173 | }; |
| 174 | |
| 175 | return Model{ |
| 176 | .main = std::move(main), |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 177 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 178 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 179 | }; |
| 180 | } |
| 181 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 182 | GeneralResult<Request::Argument> unvalidatedConvert(const hal::V1_0::RequestArgument& argument) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 183 | const auto lifetime = argument.hasNoValue ? Request::Argument::LifeTime::NO_VALUE |
| 184 | : Request::Argument::LifeTime::POOL; |
| 185 | return Request::Argument{ |
| 186 | .lifetime = lifetime, |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 187 | .location = NN_TRY(unvalidatedConvert(argument.location)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 188 | .dimensions = argument.dimensions, |
| 189 | }; |
| 190 | } |
| 191 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 192 | GeneralResult<Request> unvalidatedConvert(const hal::V1_0::Request& request) { |
| 193 | auto memories = NN_TRY(unvalidatedConvert(request.pools)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 194 | std::vector<Request::MemoryPool> pools; |
| 195 | pools.reserve(memories.size()); |
| 196 | std::move(memories.begin(), memories.end(), std::back_inserter(pools)); |
| 197 | |
| 198 | return Request{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 199 | .inputs = NN_TRY(unvalidatedConvert(request.inputs)), |
| 200 | .outputs = NN_TRY(unvalidatedConvert(request.outputs)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 201 | .pools = std::move(pools), |
| 202 | }; |
| 203 | } |
| 204 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 205 | GeneralResult<ErrorStatus> unvalidatedConvert(const hal::V1_0::ErrorStatus& status) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 206 | switch (status) { |
| 207 | case hal::V1_0::ErrorStatus::NONE: |
| 208 | case hal::V1_0::ErrorStatus::DEVICE_UNAVAILABLE: |
| 209 | case hal::V1_0::ErrorStatus::GENERAL_FAILURE: |
| 210 | case hal::V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 211 | case hal::V1_0::ErrorStatus::INVALID_ARGUMENT: |
| 212 | return static_cast<ErrorStatus>(status); |
| 213 | } |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 214 | return NN_ERROR(ErrorStatus::GENERAL_FAILURE) |
| 215 | << "Invalid ErrorStatus " << underlyingType(status); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 218 | GeneralResult<DeviceStatus> convert(const hal::V1_0::DeviceStatus& deviceStatus) { |
| 219 | return validatedConvert(deviceStatus); |
| 220 | } |
| 221 | |
| 222 | GeneralResult<Capabilities> convert(const hal::V1_0::Capabilities& capabilities) { |
| 223 | return validatedConvert(capabilities); |
| 224 | } |
| 225 | |
| 226 | GeneralResult<Model> convert(const hal::V1_0::Model& model) { |
| 227 | return validatedConvert(model); |
| 228 | } |
| 229 | |
| 230 | GeneralResult<Request> convert(const hal::V1_0::Request& request) { |
| 231 | return validatedConvert(request); |
| 232 | } |
| 233 | |
| 234 | GeneralResult<ErrorStatus> convert(const hal::V1_0::ErrorStatus& status) { |
| 235 | return validatedConvert(status); |
| 236 | } |
| 237 | |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 238 | } // namespace android::nn |
| 239 | |
| 240 | namespace android::hardware::neuralnetworks::V1_0::utils { |
| 241 | namespace { |
| 242 | |
| 243 | template <typename Input> |
Michael Butler | 08ee3f9 | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 244 | using UnvalidatedConvertOutput = |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 245 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 246 | |
| 247 | template <typename Type> |
Michael Butler | 08ee3f9 | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 248 | nn::GeneralResult<hidl_vec<UnvalidatedConvertOutput<Type>>> unvalidatedConvert( |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 249 | const std::vector<Type>& arguments) { |
Michael Butler | 08ee3f9 | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 250 | hidl_vec<UnvalidatedConvertOutput<Type>> halObject(arguments.size()); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 251 | for (size_t i = 0; i < arguments.size(); ++i) { |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 252 | halObject[i] = NN_TRY(utils::unvalidatedConvert(arguments[i])); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 253 | } |
| 254 | return halObject; |
| 255 | } |
| 256 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 257 | template <typename Type> |
Michael Butler | 08ee3f9 | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 258 | nn::GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& canonical) { |
| 259 | NN_TRY(compliantVersion(canonical)); |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 260 | return utils::unvalidatedConvert(canonical); |
| 261 | } |
| 262 | |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 263 | } // anonymous namespace |
| 264 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 265 | nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 266 | return static_cast<OperandType>(operandType); |
| 267 | } |
| 268 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 269 | nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 270 | return static_cast<OperationType>(operationType); |
| 271 | } |
| 272 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 273 | nn::GeneralResult<OperandLifeTime> unvalidatedConvert(const nn::Operand::LifeTime& lifetime) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 274 | if (lifetime == nn::Operand::LifeTime::POINTER) { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 275 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 276 | << "Model cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 277 | } |
| 278 | return static_cast<OperandLifeTime>(lifetime); |
| 279 | } |
| 280 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 281 | nn::GeneralResult<DeviceStatus> unvalidatedConvert(const nn::DeviceStatus& deviceStatus) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 282 | return static_cast<DeviceStatus>(deviceStatus); |
| 283 | } |
| 284 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 285 | nn::GeneralResult<PerformanceInfo> unvalidatedConvert( |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 286 | const nn::Capabilities::PerformanceInfo& performanceInfo) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 287 | return PerformanceInfo{ |
| 288 | .execTime = performanceInfo.execTime, |
| 289 | .powerUsage = performanceInfo.powerUsage, |
| 290 | }; |
| 291 | } |
| 292 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 293 | nn::GeneralResult<Capabilities> unvalidatedConvert(const nn::Capabilities& capabilities) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 294 | return Capabilities{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 295 | .float32Performance = NN_TRY(unvalidatedConvert( |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 296 | capabilities.operandPerformance.lookup(nn::OperandType::TENSOR_FLOAT32))), |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 297 | .quantized8Performance = NN_TRY(unvalidatedConvert( |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 298 | capabilities.operandPerformance.lookup(nn::OperandType::TENSOR_QUANT8_ASYMM))), |
| 299 | }; |
| 300 | } |
| 301 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 302 | nn::GeneralResult<DataLocation> unvalidatedConvert(const nn::DataLocation& location) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 303 | return DataLocation{ |
| 304 | .poolIndex = location.poolIndex, |
| 305 | .offset = location.offset, |
| 306 | .length = location.length, |
| 307 | }; |
| 308 | } |
| 309 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 310 | nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 311 | return Operand{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 312 | .type = NN_TRY(unvalidatedConvert(operand.type)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 313 | .dimensions = operand.dimensions, |
| 314 | .numberOfConsumers = 0, |
| 315 | .scale = operand.scale, |
| 316 | .zeroPoint = operand.zeroPoint, |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 317 | .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), |
| 318 | .location = NN_TRY(unvalidatedConvert(operand.location)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 319 | }; |
| 320 | } |
| 321 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 322 | nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 323 | return Operation{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 324 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 325 | .inputs = operation.inputs, |
| 326 | .outputs = operation.outputs, |
| 327 | }; |
| 328 | } |
| 329 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 330 | nn::GeneralResult<hidl_vec<uint8_t>> unvalidatedConvert( |
| 331 | const nn::Model::OperandValues& operandValues) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 332 | return hidl_vec<uint8_t>(operandValues.data(), operandValues.data() + operandValues.size()); |
| 333 | } |
| 334 | |
Michael Butler | 79a16eb | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 335 | nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::SharedMemory& memory) { |
Michael Butler | bbe43d9 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 336 | return hal::utils::createHidlMemoryFromSharedMemory(memory); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 339 | nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 340 | if (!hal::utils::hasNoPointerData(model)) { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 341 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 342 | << "Mdoel cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 345 | auto operands = NN_TRY(unvalidatedConvert(model.main.operands)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 346 | |
| 347 | // Update number of consumers. |
| 348 | const auto numberOfConsumers = |
Michael Butler | c4d9800 | 2021-02-09 15:36:11 -0800 | [diff] [blame] | 349 | NN_TRY(hal::utils::countNumberOfConsumers(operands.size(), model.main.operations)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 350 | CHECK(operands.size() == numberOfConsumers.size()); |
| 351 | for (size_t i = 0; i < operands.size(); ++i) { |
| 352 | operands[i].numberOfConsumers = numberOfConsumers[i]; |
| 353 | } |
| 354 | |
| 355 | return Model{ |
| 356 | .operands = std::move(operands), |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 357 | .operations = NN_TRY(unvalidatedConvert(model.main.operations)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 358 | .inputIndexes = model.main.inputIndexes, |
| 359 | .outputIndexes = model.main.outputIndexes, |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 360 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 361 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 362 | }; |
| 363 | } |
| 364 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 365 | nn::GeneralResult<RequestArgument> unvalidatedConvert( |
| 366 | const nn::Request::Argument& requestArgument) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 367 | if (requestArgument.lifetime == nn::Request::Argument::LifeTime::POINTER) { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 368 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 369 | << "Request cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 370 | } |
| 371 | const bool hasNoValue = requestArgument.lifetime == nn::Request::Argument::LifeTime::NO_VALUE; |
| 372 | return RequestArgument{ |
| 373 | .hasNoValue = hasNoValue, |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 374 | .location = NN_TRY(unvalidatedConvert(requestArgument.location)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 375 | .dimensions = requestArgument.dimensions, |
| 376 | }; |
| 377 | } |
| 378 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 379 | nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::Request::MemoryPool& memoryPool) { |
Michael Butler | 79a16eb | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 380 | return unvalidatedConvert(std::get<nn::SharedMemory>(memoryPool)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 383 | nn::GeneralResult<Request> unvalidatedConvert(const nn::Request& request) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 384 | if (!hal::utils::hasNoPointerData(request)) { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 385 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 386 | << "Request cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | return Request{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 390 | .inputs = NN_TRY(unvalidatedConvert(request.inputs)), |
| 391 | .outputs = NN_TRY(unvalidatedConvert(request.outputs)), |
| 392 | .pools = NN_TRY(unvalidatedConvert(request.pools)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 393 | }; |
| 394 | } |
| 395 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 396 | nn::GeneralResult<ErrorStatus> unvalidatedConvert(const nn::ErrorStatus& status) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 397 | switch (status) { |
| 398 | case nn::ErrorStatus::NONE: |
| 399 | case nn::ErrorStatus::DEVICE_UNAVAILABLE: |
| 400 | case nn::ErrorStatus::GENERAL_FAILURE: |
| 401 | case nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 402 | case nn::ErrorStatus::INVALID_ARGUMENT: |
| 403 | return static_cast<ErrorStatus>(status); |
| 404 | default: |
| 405 | return ErrorStatus::GENERAL_FAILURE; |
| 406 | } |
| 407 | } |
| 408 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 409 | nn::GeneralResult<DeviceStatus> convert(const nn::DeviceStatus& deviceStatus) { |
| 410 | return validatedConvert(deviceStatus); |
| 411 | } |
| 412 | |
| 413 | nn::GeneralResult<Capabilities> convert(const nn::Capabilities& capabilities) { |
| 414 | return validatedConvert(capabilities); |
| 415 | } |
| 416 | |
| 417 | nn::GeneralResult<Model> convert(const nn::Model& model) { |
| 418 | return validatedConvert(model); |
| 419 | } |
| 420 | |
| 421 | nn::GeneralResult<Request> convert(const nn::Request& request) { |
| 422 | return validatedConvert(request); |
| 423 | } |
| 424 | |
| 425 | nn::GeneralResult<ErrorStatus> convert(const nn::ErrorStatus& status) { |
| 426 | return validatedConvert(status); |
| 427 | } |
| 428 | |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 429 | } // namespace android::hardware::neuralnetworks::V1_0::utils |