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