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 | |
| 38 | namespace { |
| 39 | |
| 40 | template <typename Type> |
| 41 | constexpr std::underlying_type_t<Type> underlyingType(Type value) { |
| 42 | return static_cast<std::underlying_type_t<Type>>(value); |
| 43 | } |
| 44 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 45 | constexpr auto kVersion = android::nn::Version::ANDROID_OC_MR1; |
| 46 | |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 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 | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 56 | using unvalidatedConvertOutput = |
| 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 | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 60 | GeneralResult<std::vector<unvalidatedConvertOutput<Type>>> unvalidatedConvert( |
| 61 | const hidl_vec<Type>& arguments) { |
| 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> |
| 71 | decltype(nn::unvalidatedConvert(std::declval<Type>())) validatedConvert(const Type& halObject) { |
| 72 | auto canonical = NN_TRY(nn::unvalidatedConvert(halObject)); |
| 73 | const auto maybeVersion = validate(canonical); |
| 74 | if (!maybeVersion.has_value()) { |
| 75 | return error() << maybeVersion.error(); |
| 76 | } |
| 77 | const auto version = maybeVersion.value(); |
| 78 | if (version > kVersion) { |
| 79 | return NN_ERROR() << "Insufficient version: " << version << " vs required " << kVersion; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 80 | } |
| 81 | return canonical; |
| 82 | } |
| 83 | |
| 84 | } // anonymous namespace |
| 85 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 86 | GeneralResult<OperandType> unvalidatedConvert(const hal::V1_0::OperandType& operandType) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 87 | return static_cast<OperandType>(operandType); |
| 88 | } |
| 89 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 90 | GeneralResult<OperationType> unvalidatedConvert(const hal::V1_0::OperationType& operationType) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 91 | return static_cast<OperationType>(operationType); |
| 92 | } |
| 93 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 94 | GeneralResult<Operand::LifeTime> unvalidatedConvert(const hal::V1_0::OperandLifeTime& lifetime) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 95 | return static_cast<Operand::LifeTime>(lifetime); |
| 96 | } |
| 97 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 98 | GeneralResult<DeviceStatus> unvalidatedConvert(const hal::V1_0::DeviceStatus& deviceStatus) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 99 | return static_cast<DeviceStatus>(deviceStatus); |
| 100 | } |
| 101 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 102 | GeneralResult<Capabilities::PerformanceInfo> unvalidatedConvert( |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 103 | const hal::V1_0::PerformanceInfo& performanceInfo) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 104 | return Capabilities::PerformanceInfo{ |
| 105 | .execTime = performanceInfo.execTime, |
| 106 | .powerUsage = performanceInfo.powerUsage, |
| 107 | }; |
| 108 | } |
| 109 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 110 | GeneralResult<Capabilities> unvalidatedConvert(const hal::V1_0::Capabilities& capabilities) { |
| 111 | const auto quantized8Performance = |
| 112 | NN_TRY(unvalidatedConvert(capabilities.quantized8Performance)); |
| 113 | const auto float32Performance = NN_TRY(unvalidatedConvert(capabilities.float32Performance)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 114 | |
| 115 | auto table = hal::utils::makeQuantized8PerformanceConsistentWithP(float32Performance, |
| 116 | quantized8Performance); |
| 117 | |
| 118 | return Capabilities{ |
| 119 | .relaxedFloat32toFloat16PerformanceScalar = float32Performance, |
| 120 | .relaxedFloat32toFloat16PerformanceTensor = float32Performance, |
| 121 | .operandPerformance = std::move(table), |
| 122 | }; |
| 123 | } |
| 124 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 125 | GeneralResult<DataLocation> unvalidatedConvert(const hal::V1_0::DataLocation& location) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 126 | return DataLocation{ |
| 127 | .poolIndex = location.poolIndex, |
| 128 | .offset = location.offset, |
| 129 | .length = location.length, |
| 130 | }; |
| 131 | } |
| 132 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 133 | GeneralResult<Operand> unvalidatedConvert(const hal::V1_0::Operand& operand) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 134 | return Operand{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 135 | .type = NN_TRY(unvalidatedConvert(operand.type)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 136 | .dimensions = operand.dimensions, |
| 137 | .scale = operand.scale, |
| 138 | .zeroPoint = operand.zeroPoint, |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 139 | .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), |
| 140 | .location = NN_TRY(unvalidatedConvert(operand.location)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 141 | }; |
| 142 | } |
| 143 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 144 | GeneralResult<Operation> unvalidatedConvert(const hal::V1_0::Operation& operation) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 145 | return Operation{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 146 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 147 | .inputs = operation.inputs, |
| 148 | .outputs = operation.outputs, |
| 149 | }; |
| 150 | } |
| 151 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 152 | GeneralResult<Model::OperandValues> unvalidatedConvert(const hidl_vec<uint8_t>& operandValues) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 153 | return Model::OperandValues(operandValues.data(), operandValues.size()); |
| 154 | } |
| 155 | |
Michael Butler | 79a16eb | 2021-02-07 00:11:13 -0800 | [diff] [blame^] | 156 | GeneralResult<SharedMemory> unvalidatedConvert(const hidl_memory& memory) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 157 | return createSharedMemoryFromHidlMemory(memory); |
| 158 | } |
| 159 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 160 | GeneralResult<Model> unvalidatedConvert(const hal::V1_0::Model& model) { |
| 161 | auto operations = NN_TRY(unvalidatedConvert(model.operations)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 162 | |
| 163 | // Verify number of consumers. |
| 164 | const auto numberOfConsumers = |
| 165 | hal::utils::countNumberOfConsumers(model.operands.size(), operations); |
| 166 | CHECK(model.operands.size() == numberOfConsumers.size()); |
| 167 | for (size_t i = 0; i < model.operands.size(); ++i) { |
| 168 | if (model.operands[i].numberOfConsumers != numberOfConsumers[i]) { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 169 | return NN_ERROR(ErrorStatus::GENERAL_FAILURE) |
| 170 | << "Invalid numberOfConsumers for operand " << i << ", expected " |
| 171 | << numberOfConsumers[i] << " but found " << model.operands[i].numberOfConsumers; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | auto main = Model::Subgraph{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 176 | .operands = NN_TRY(unvalidatedConvert(model.operands)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 177 | .operations = std::move(operations), |
| 178 | .inputIndexes = model.inputIndexes, |
| 179 | .outputIndexes = model.outputIndexes, |
| 180 | }; |
| 181 | |
| 182 | return Model{ |
| 183 | .main = std::move(main), |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 184 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 185 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 186 | }; |
| 187 | } |
| 188 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 189 | GeneralResult<Request::Argument> unvalidatedConvert(const hal::V1_0::RequestArgument& argument) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 190 | const auto lifetime = argument.hasNoValue ? Request::Argument::LifeTime::NO_VALUE |
| 191 | : Request::Argument::LifeTime::POOL; |
| 192 | return Request::Argument{ |
| 193 | .lifetime = lifetime, |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 194 | .location = NN_TRY(unvalidatedConvert(argument.location)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 195 | .dimensions = argument.dimensions, |
| 196 | }; |
| 197 | } |
| 198 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 199 | GeneralResult<Request> unvalidatedConvert(const hal::V1_0::Request& request) { |
| 200 | auto memories = NN_TRY(unvalidatedConvert(request.pools)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 201 | std::vector<Request::MemoryPool> pools; |
| 202 | pools.reserve(memories.size()); |
| 203 | std::move(memories.begin(), memories.end(), std::back_inserter(pools)); |
| 204 | |
| 205 | return Request{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 206 | .inputs = NN_TRY(unvalidatedConvert(request.inputs)), |
| 207 | .outputs = NN_TRY(unvalidatedConvert(request.outputs)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 208 | .pools = std::move(pools), |
| 209 | }; |
| 210 | } |
| 211 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 212 | GeneralResult<ErrorStatus> unvalidatedConvert(const hal::V1_0::ErrorStatus& status) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 213 | switch (status) { |
| 214 | case hal::V1_0::ErrorStatus::NONE: |
| 215 | case hal::V1_0::ErrorStatus::DEVICE_UNAVAILABLE: |
| 216 | case hal::V1_0::ErrorStatus::GENERAL_FAILURE: |
| 217 | case hal::V1_0::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 218 | case hal::V1_0::ErrorStatus::INVALID_ARGUMENT: |
| 219 | return static_cast<ErrorStatus>(status); |
| 220 | } |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 221 | return NN_ERROR(ErrorStatus::GENERAL_FAILURE) |
| 222 | << "Invalid ErrorStatus " << underlyingType(status); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 225 | GeneralResult<DeviceStatus> convert(const hal::V1_0::DeviceStatus& deviceStatus) { |
| 226 | return validatedConvert(deviceStatus); |
| 227 | } |
| 228 | |
| 229 | GeneralResult<Capabilities> convert(const hal::V1_0::Capabilities& capabilities) { |
| 230 | return validatedConvert(capabilities); |
| 231 | } |
| 232 | |
| 233 | GeneralResult<Model> convert(const hal::V1_0::Model& model) { |
| 234 | return validatedConvert(model); |
| 235 | } |
| 236 | |
| 237 | GeneralResult<Request> convert(const hal::V1_0::Request& request) { |
| 238 | return validatedConvert(request); |
| 239 | } |
| 240 | |
| 241 | GeneralResult<ErrorStatus> convert(const hal::V1_0::ErrorStatus& status) { |
| 242 | return validatedConvert(status); |
| 243 | } |
| 244 | |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 245 | } // namespace android::nn |
| 246 | |
| 247 | namespace android::hardware::neuralnetworks::V1_0::utils { |
| 248 | namespace { |
| 249 | |
| 250 | template <typename Input> |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 251 | using unvalidatedConvertOutput = |
| 252 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 253 | |
| 254 | template <typename Type> |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 255 | nn::GeneralResult<hidl_vec<unvalidatedConvertOutput<Type>>> unvalidatedConvert( |
| 256 | const std::vector<Type>& arguments) { |
| 257 | hidl_vec<unvalidatedConvertOutput<Type>> halObject(arguments.size()); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 258 | for (size_t i = 0; i < arguments.size(); ++i) { |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 259 | halObject[i] = NN_TRY(utils::unvalidatedConvert(arguments[i])); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 260 | } |
| 261 | return halObject; |
| 262 | } |
| 263 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 264 | template <typename Type> |
| 265 | decltype(utils::unvalidatedConvert(std::declval<Type>())) validatedConvert(const Type& canonical) { |
| 266 | const auto maybeVersion = nn::validate(canonical); |
| 267 | if (!maybeVersion.has_value()) { |
| 268 | return nn::error() << maybeVersion.error(); |
| 269 | } |
| 270 | const auto version = maybeVersion.value(); |
| 271 | if (version > kVersion) { |
| 272 | return NN_ERROR() << "Insufficient version: " << version << " vs required " << kVersion; |
| 273 | } |
| 274 | return utils::unvalidatedConvert(canonical); |
| 275 | } |
| 276 | |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 277 | } // anonymous namespace |
| 278 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 279 | nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 280 | return static_cast<OperandType>(operandType); |
| 281 | } |
| 282 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 283 | nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 284 | return static_cast<OperationType>(operationType); |
| 285 | } |
| 286 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 287 | nn::GeneralResult<OperandLifeTime> unvalidatedConvert(const nn::Operand::LifeTime& lifetime) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 288 | if (lifetime == nn::Operand::LifeTime::POINTER) { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 289 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 290 | << "Model cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 291 | } |
| 292 | return static_cast<OperandLifeTime>(lifetime); |
| 293 | } |
| 294 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 295 | nn::GeneralResult<DeviceStatus> unvalidatedConvert(const nn::DeviceStatus& deviceStatus) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 296 | return static_cast<DeviceStatus>(deviceStatus); |
| 297 | } |
| 298 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 299 | nn::GeneralResult<PerformanceInfo> unvalidatedConvert( |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 300 | const nn::Capabilities::PerformanceInfo& performanceInfo) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 301 | return PerformanceInfo{ |
| 302 | .execTime = performanceInfo.execTime, |
| 303 | .powerUsage = performanceInfo.powerUsage, |
| 304 | }; |
| 305 | } |
| 306 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 307 | nn::GeneralResult<Capabilities> unvalidatedConvert(const nn::Capabilities& capabilities) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 308 | return Capabilities{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 309 | .float32Performance = NN_TRY(unvalidatedConvert( |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 310 | capabilities.operandPerformance.lookup(nn::OperandType::TENSOR_FLOAT32))), |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 311 | .quantized8Performance = NN_TRY(unvalidatedConvert( |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 312 | capabilities.operandPerformance.lookup(nn::OperandType::TENSOR_QUANT8_ASYMM))), |
| 313 | }; |
| 314 | } |
| 315 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 316 | nn::GeneralResult<DataLocation> unvalidatedConvert(const nn::DataLocation& location) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 317 | return DataLocation{ |
| 318 | .poolIndex = location.poolIndex, |
| 319 | .offset = location.offset, |
| 320 | .length = location.length, |
| 321 | }; |
| 322 | } |
| 323 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 324 | nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 325 | return Operand{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 326 | .type = NN_TRY(unvalidatedConvert(operand.type)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 327 | .dimensions = operand.dimensions, |
| 328 | .numberOfConsumers = 0, |
| 329 | .scale = operand.scale, |
| 330 | .zeroPoint = operand.zeroPoint, |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 331 | .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), |
| 332 | .location = NN_TRY(unvalidatedConvert(operand.location)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 333 | }; |
| 334 | } |
| 335 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 336 | nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 337 | return Operation{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 338 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 339 | .inputs = operation.inputs, |
| 340 | .outputs = operation.outputs, |
| 341 | }; |
| 342 | } |
| 343 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 344 | nn::GeneralResult<hidl_vec<uint8_t>> unvalidatedConvert( |
| 345 | const nn::Model::OperandValues& operandValues) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 346 | return hidl_vec<uint8_t>(operandValues.data(), operandValues.data() + operandValues.size()); |
| 347 | } |
| 348 | |
Michael Butler | 79a16eb | 2021-02-07 00:11:13 -0800 | [diff] [blame^] | 349 | nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::SharedMemory& memory) { |
| 350 | CHECK(memory != nullptr); |
| 351 | return hidl_memory(memory->name, NN_TRY(hal::utils::hidlHandleFromSharedHandle(memory->handle)), |
| 352 | memory->size); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 355 | nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 356 | if (!hal::utils::hasNoPointerData(model)) { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 357 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 358 | << "Mdoel cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 361 | auto operands = NN_TRY(unvalidatedConvert(model.main.operands)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 362 | |
| 363 | // Update number of consumers. |
| 364 | const auto numberOfConsumers = |
| 365 | hal::utils::countNumberOfConsumers(operands.size(), model.main.operations); |
| 366 | CHECK(operands.size() == numberOfConsumers.size()); |
| 367 | for (size_t i = 0; i < operands.size(); ++i) { |
| 368 | operands[i].numberOfConsumers = numberOfConsumers[i]; |
| 369 | } |
| 370 | |
| 371 | return Model{ |
| 372 | .operands = std::move(operands), |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 373 | .operations = NN_TRY(unvalidatedConvert(model.main.operations)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 374 | .inputIndexes = model.main.inputIndexes, |
| 375 | .outputIndexes = model.main.outputIndexes, |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 376 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 377 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 378 | }; |
| 379 | } |
| 380 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 381 | nn::GeneralResult<RequestArgument> unvalidatedConvert( |
| 382 | const nn::Request::Argument& requestArgument) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 383 | if (requestArgument.lifetime == nn::Request::Argument::LifeTime::POINTER) { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 384 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 385 | << "Request cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 386 | } |
| 387 | const bool hasNoValue = requestArgument.lifetime == nn::Request::Argument::LifeTime::NO_VALUE; |
| 388 | return RequestArgument{ |
| 389 | .hasNoValue = hasNoValue, |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 390 | .location = NN_TRY(unvalidatedConvert(requestArgument.location)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 391 | .dimensions = requestArgument.dimensions, |
| 392 | }; |
| 393 | } |
| 394 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 395 | nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::Request::MemoryPool& memoryPool) { |
Michael Butler | 79a16eb | 2021-02-07 00:11:13 -0800 | [diff] [blame^] | 396 | return unvalidatedConvert(std::get<nn::SharedMemory>(memoryPool)); |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 399 | nn::GeneralResult<Request> unvalidatedConvert(const nn::Request& request) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 400 | if (!hal::utils::hasNoPointerData(request)) { |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 401 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 402 | << "Request cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | return Request{ |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 406 | .inputs = NN_TRY(unvalidatedConvert(request.inputs)), |
| 407 | .outputs = NN_TRY(unvalidatedConvert(request.outputs)), |
| 408 | .pools = NN_TRY(unvalidatedConvert(request.pools)), |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 409 | }; |
| 410 | } |
| 411 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 412 | nn::GeneralResult<ErrorStatus> unvalidatedConvert(const nn::ErrorStatus& status) { |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 413 | switch (status) { |
| 414 | case nn::ErrorStatus::NONE: |
| 415 | case nn::ErrorStatus::DEVICE_UNAVAILABLE: |
| 416 | case nn::ErrorStatus::GENERAL_FAILURE: |
| 417 | case nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 418 | case nn::ErrorStatus::INVALID_ARGUMENT: |
| 419 | return static_cast<ErrorStatus>(status); |
| 420 | default: |
| 421 | return ErrorStatus::GENERAL_FAILURE; |
| 422 | } |
| 423 | } |
| 424 | |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 425 | nn::GeneralResult<DeviceStatus> convert(const nn::DeviceStatus& deviceStatus) { |
| 426 | return validatedConvert(deviceStatus); |
| 427 | } |
| 428 | |
| 429 | nn::GeneralResult<Capabilities> convert(const nn::Capabilities& capabilities) { |
| 430 | return validatedConvert(capabilities); |
| 431 | } |
| 432 | |
| 433 | nn::GeneralResult<Model> convert(const nn::Model& model) { |
| 434 | return validatedConvert(model); |
| 435 | } |
| 436 | |
| 437 | nn::GeneralResult<Request> convert(const nn::Request& request) { |
| 438 | return validatedConvert(request); |
| 439 | } |
| 440 | |
| 441 | nn::GeneralResult<ErrorStatus> convert(const nn::ErrorStatus& status) { |
| 442 | return validatedConvert(status); |
| 443 | } |
| 444 | |
Michael Butler | a685c3d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 445 | } // namespace android::hardware::neuralnetworks::V1_0::utils |