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.0/types.h> |
| 21 | #include <android/hardware/neuralnetworks/1.1/types.h> |
| 22 | #include <nnapi/OperandTypes.h> |
| 23 | #include <nnapi/OperationTypes.h> |
| 24 | #include <nnapi/Result.h> |
| 25 | #include <nnapi/SharedMemory.h> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 26 | #include <nnapi/TypeUtils.h> |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 27 | #include <nnapi/Types.h> |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 28 | #include <nnapi/Validation.h> |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 29 | #include <nnapi/hal/1.0/Conversions.h> |
| 30 | #include <nnapi/hal/CommonUtils.h> |
| 31 | |
| 32 | #include <algorithm> |
| 33 | #include <functional> |
| 34 | #include <iterator> |
| 35 | #include <type_traits> |
| 36 | #include <utility> |
| 37 | |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 38 | #include "Utils.h" |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 39 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 40 | namespace android::nn { |
| 41 | namespace { |
| 42 | |
| 43 | using hardware::hidl_vec; |
| 44 | |
| 45 | template <typename Input> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 46 | using UnvalidatedConvertOutput = |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 47 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 48 | |
| 49 | template <typename Type> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 50 | GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> unvalidatedConvert( |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 51 | const hidl_vec<Type>& arguments) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 52 | std::vector<UnvalidatedConvertOutput<Type>> canonical; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 53 | canonical.reserve(arguments.size()); |
| 54 | for (const auto& argument : arguments) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 55 | canonical.push_back(NN_TRY(nn::unvalidatedConvert(argument))); |
| 56 | } |
| 57 | return canonical; |
| 58 | } |
| 59 | |
| 60 | template <typename Type> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 61 | GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& halObject) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 62 | auto canonical = NN_TRY(nn::unvalidatedConvert(halObject)); |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 63 | NN_TRY(hal::V1_1::utils::compliantVersion(canonical)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 64 | return canonical; |
| 65 | } |
| 66 | |
| 67 | } // anonymous namespace |
| 68 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 69 | GeneralResult<OperationType> unvalidatedConvert(const hal::V1_1::OperationType& operationType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 70 | return static_cast<OperationType>(operationType); |
| 71 | } |
| 72 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 73 | GeneralResult<Capabilities> unvalidatedConvert(const hal::V1_1::Capabilities& capabilities) { |
| 74 | const auto quantized8Performance = |
| 75 | NN_TRY(unvalidatedConvert(capabilities.quantized8Performance)); |
| 76 | const auto float32Performance = NN_TRY(unvalidatedConvert(capabilities.float32Performance)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 77 | const auto relaxedFloat32toFloat16Performance = |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 78 | NN_TRY(unvalidatedConvert(capabilities.relaxedFloat32toFloat16Performance)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 79 | |
| 80 | auto table = hal::utils::makeQuantized8PerformanceConsistentWithP(float32Performance, |
| 81 | quantized8Performance); |
| 82 | |
| 83 | return Capabilities{ |
| 84 | .relaxedFloat32toFloat16PerformanceScalar = relaxedFloat32toFloat16Performance, |
| 85 | .relaxedFloat32toFloat16PerformanceTensor = relaxedFloat32toFloat16Performance, |
| 86 | .operandPerformance = std::move(table), |
| 87 | }; |
| 88 | } |
| 89 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 90 | GeneralResult<Operation> unvalidatedConvert(const hal::V1_1::Operation& operation) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 91 | return Operation{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 92 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 93 | .inputs = operation.inputs, |
| 94 | .outputs = operation.outputs, |
| 95 | }; |
| 96 | } |
| 97 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 98 | GeneralResult<Model> unvalidatedConvert(const hal::V1_1::Model& model) { |
| 99 | auto operations = NN_TRY(unvalidatedConvert(model.operations)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 100 | |
| 101 | // Verify number of consumers. |
| 102 | const auto numberOfConsumers = |
Michael Butler | 68b6926 | 2021-02-09 15:36:11 -0800 | [diff] [blame] | 103 | NN_TRY(hal::utils::countNumberOfConsumers(model.operands.size(), operations)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 104 | CHECK(model.operands.size() == numberOfConsumers.size()); |
| 105 | for (size_t i = 0; i < model.operands.size(); ++i) { |
| 106 | if (model.operands[i].numberOfConsumers != numberOfConsumers[i]) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 107 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 108 | << "Invalid numberOfConsumers for operand " << i << ", expected " |
| 109 | << numberOfConsumers[i] << " but found " << model.operands[i].numberOfConsumers; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | auto main = Model::Subgraph{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 114 | .operands = NN_TRY(unvalidatedConvert(model.operands)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 115 | .operations = std::move(operations), |
| 116 | .inputIndexes = model.inputIndexes, |
| 117 | .outputIndexes = model.outputIndexes, |
| 118 | }; |
| 119 | |
| 120 | return Model{ |
| 121 | .main = std::move(main), |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 122 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 123 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 124 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
| 125 | }; |
| 126 | } |
| 127 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 128 | GeneralResult<ExecutionPreference> unvalidatedConvert( |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 129 | const hal::V1_1::ExecutionPreference& executionPreference) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 130 | return static_cast<ExecutionPreference>(executionPreference); |
| 131 | } |
| 132 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 133 | GeneralResult<Capabilities> convert(const hal::V1_1::Capabilities& capabilities) { |
| 134 | return validatedConvert(capabilities); |
| 135 | } |
| 136 | |
| 137 | GeneralResult<Model> convert(const hal::V1_1::Model& model) { |
| 138 | return validatedConvert(model); |
| 139 | } |
| 140 | |
| 141 | GeneralResult<ExecutionPreference> convert( |
| 142 | const hal::V1_1::ExecutionPreference& executionPreference) { |
| 143 | return validatedConvert(executionPreference); |
| 144 | } |
| 145 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 146 | } // namespace android::nn |
| 147 | |
| 148 | namespace android::hardware::neuralnetworks::V1_1::utils { |
| 149 | namespace { |
| 150 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 151 | using utils::unvalidatedConvert; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 152 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 153 | nn::GeneralResult<V1_0::PerformanceInfo> unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 154 | const nn::Capabilities::PerformanceInfo& performanceInfo) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 155 | return V1_0::utils::unvalidatedConvert(performanceInfo); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 158 | nn::GeneralResult<V1_0::Operand> unvalidatedConvert(const nn::Operand& operand) { |
| 159 | return V1_0::utils::unvalidatedConvert(operand); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 162 | nn::GeneralResult<hidl_vec<uint8_t>> unvalidatedConvert( |
| 163 | const nn::Model::OperandValues& operandValues) { |
| 164 | return V1_0::utils::unvalidatedConvert(operandValues); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 167 | nn::GeneralResult<hidl_memory> unvalidatedConvert(const nn::SharedMemory& memory) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 168 | return V1_0::utils::unvalidatedConvert(memory); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | template <typename Input> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 172 | using UnvalidatedConvertOutput = |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 173 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 174 | |
| 175 | template <typename Type> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 176 | nn::GeneralResult<hidl_vec<UnvalidatedConvertOutput<Type>>> unvalidatedConvert( |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 177 | const std::vector<Type>& arguments) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 178 | hidl_vec<UnvalidatedConvertOutput<Type>> halObject(arguments.size()); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 179 | for (size_t i = 0; i < arguments.size(); ++i) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 180 | halObject[i] = NN_TRY(unvalidatedConvert(arguments[i])); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 181 | } |
| 182 | return halObject; |
| 183 | } |
| 184 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 185 | template <typename Type> |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 186 | nn::GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& canonical) { |
| 187 | NN_TRY(compliantVersion(canonical)); |
| 188 | return unvalidatedConvert(canonical); |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 191 | } // anonymous namespace |
| 192 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 193 | nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 194 | return static_cast<OperationType>(operationType); |
| 195 | } |
| 196 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 197 | nn::GeneralResult<Capabilities> unvalidatedConvert(const nn::Capabilities& capabilities) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 198 | return Capabilities{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 199 | .float32Performance = NN_TRY(unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 200 | capabilities.operandPerformance.lookup(nn::OperandType::TENSOR_FLOAT32))), |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 201 | .quantized8Performance = NN_TRY(unvalidatedConvert( |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 202 | capabilities.operandPerformance.lookup(nn::OperandType::TENSOR_QUANT8_ASYMM))), |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 203 | .relaxedFloat32toFloat16Performance = NN_TRY( |
| 204 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceTensor)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 205 | }; |
| 206 | } |
| 207 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 208 | nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 209 | return Operation{ |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 210 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 211 | .inputs = operation.inputs, |
| 212 | .outputs = operation.outputs, |
| 213 | }; |
| 214 | } |
| 215 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 216 | nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 217 | if (!hal::utils::hasNoPointerData(model)) { |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 218 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 219 | << "Mdoel cannot be unvalidatedConverted because it contains pointer-based memory"; |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 222 | auto operands = NN_TRY(unvalidatedConvert(model.main.operands)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 223 | |
| 224 | // Update number of consumers. |
| 225 | const auto numberOfConsumers = |
Michael Butler | 68b6926 | 2021-02-09 15:36:11 -0800 | [diff] [blame] | 226 | NN_TRY(hal::utils::countNumberOfConsumers(operands.size(), model.main.operations)); |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 227 | CHECK(operands.size() == numberOfConsumers.size()); |
| 228 | for (size_t i = 0; i < operands.size(); ++i) { |
| 229 | operands[i].numberOfConsumers = numberOfConsumers[i]; |
| 230 | } |
| 231 | |
| 232 | return Model{ |
| 233 | .operands = std::move(operands), |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 234 | .operations = NN_TRY(unvalidatedConvert(model.main.operations)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 235 | .inputIndexes = model.main.inputIndexes, |
| 236 | .outputIndexes = model.main.outputIndexes, |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 237 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 238 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 239 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
| 240 | }; |
| 241 | } |
| 242 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 243 | nn::GeneralResult<ExecutionPreference> unvalidatedConvert( |
| 244 | const nn::ExecutionPreference& executionPreference) { |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 245 | return static_cast<ExecutionPreference>(executionPreference); |
| 246 | } |
| 247 | |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 248 | nn::GeneralResult<Capabilities> convert(const nn::Capabilities& capabilities) { |
| 249 | return validatedConvert(capabilities); |
| 250 | } |
| 251 | |
| 252 | nn::GeneralResult<Model> convert(const nn::Model& model) { |
| 253 | return validatedConvert(model); |
| 254 | } |
| 255 | |
| 256 | nn::GeneralResult<ExecutionPreference> convert(const nn::ExecutionPreference& executionPreference) { |
| 257 | return validatedConvert(executionPreference); |
| 258 | } |
| 259 | |
Michael Butler | 7fd03c2 | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 260 | nn::GeneralResult<V1_0::DeviceStatus> convert(const nn::DeviceStatus& deviceStatus) { |
| 261 | return V1_0::utils::convert(deviceStatus); |
| 262 | } |
| 263 | |
| 264 | nn::GeneralResult<V1_0::Request> convert(const nn::Request& request) { |
| 265 | return V1_0::utils::convert(request); |
| 266 | } |
| 267 | |
| 268 | nn::GeneralResult<V1_0::ErrorStatus> convert(const nn::ErrorStatus& status) { |
| 269 | return V1_0::utils::convert(status); |
| 270 | } |
| 271 | |
Michael Butler | b98aa6d | 2020-02-22 22:37:59 -0800 | [diff] [blame] | 272 | } // namespace android::hardware::neuralnetworks::V1_1::utils |