Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 19 | #include <aidl/android/hardware/common/Ashmem.h> |
| 20 | #include <aidl/android/hardware/common/MappableFile.h> |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 21 | #include <aidl/android/hardware/common/NativeHandle.h> |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 22 | #include <aidl/android/hardware/graphics/common/HardwareBuffer.h> |
| 23 | #include <aidlcommonsupport/NativeHandle.h> |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 24 | #include <android-base/logging.h> |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 25 | #include <android-base/mapped_file.h> |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 26 | #include <android-base/unique_fd.h> |
| 27 | #include <android/binder_auto_utils.h> |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 28 | #include <cutils/native_handle.h> |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 29 | #include <nnapi/OperandTypes.h> |
| 30 | #include <nnapi/OperationTypes.h> |
| 31 | #include <nnapi/Result.h> |
| 32 | #include <nnapi/SharedMemory.h> |
| 33 | #include <nnapi/TypeUtils.h> |
| 34 | #include <nnapi/Types.h> |
| 35 | #include <nnapi/Validation.h> |
| 36 | #include <nnapi/hal/CommonUtils.h> |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 37 | |
| 38 | #include <algorithm> |
| 39 | #include <chrono> |
| 40 | #include <functional> |
| 41 | #include <iterator> |
| 42 | #include <limits> |
| 43 | #include <type_traits> |
| 44 | #include <utility> |
| 45 | |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 46 | #include "Utils.h" |
| 47 | |
Ray Hernandez | 338d6f8 | 2021-08-11 21:29:20 +0000 | [diff] [blame] | 48 | #ifdef __ANDROID__ |
| 49 | #include <android/hardware_buffer.h> |
| 50 | #include <vndk/hardware_buffer.h> |
| 51 | #endif // __ANDROID__ |
| 52 | |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 53 | #define VERIFY_NON_NEGATIVE(value) \ |
| 54 | while (UNLIKELY(value < 0)) return NN_ERROR() |
| 55 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 56 | #define VERIFY_LE_INT32_MAX(value) \ |
| 57 | while (UNLIKELY(value > std::numeric_limits<int32_t>::max())) return NN_ERROR() |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 58 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 59 | namespace { |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 60 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 61 | constexpr int64_t kNoTiming = -1; |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 62 | |
| 63 | } // namespace |
| 64 | |
| 65 | namespace android::nn { |
| 66 | namespace { |
| 67 | |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 68 | using ::aidl::android::hardware::common::NativeHandle; |
Jooyung Han | 0bdded6 | 2022-02-26 21:10:12 +0900 | [diff] [blame^] | 69 | using ::aidl::android::hardware::neuralnetworks::utils::underlyingType; |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 70 | |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 71 | template <typename Input> |
| 72 | using UnvalidatedConvertOutput = |
| 73 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
| 74 | |
| 75 | template <typename Type> |
| 76 | GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> unvalidatedConvertVec( |
| 77 | const std::vector<Type>& arguments) { |
| 78 | std::vector<UnvalidatedConvertOutput<Type>> canonical; |
| 79 | canonical.reserve(arguments.size()); |
| 80 | for (const auto& argument : arguments) { |
| 81 | canonical.push_back(NN_TRY(nn::unvalidatedConvert(argument))); |
| 82 | } |
| 83 | return canonical; |
| 84 | } |
| 85 | |
| 86 | template <typename Type> |
| 87 | GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> unvalidatedConvert( |
| 88 | const std::vector<Type>& arguments) { |
| 89 | return unvalidatedConvertVec(arguments); |
| 90 | } |
| 91 | |
| 92 | template <typename Type> |
| 93 | GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& halObject) { |
| 94 | auto canonical = NN_TRY(nn::unvalidatedConvert(halObject)); |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 95 | NN_TRY(aidl_hal::utils::compliantVersion(canonical)); |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 96 | return canonical; |
| 97 | } |
| 98 | |
| 99 | template <typename Type> |
| 100 | GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> validatedConvert( |
| 101 | const std::vector<Type>& arguments) { |
| 102 | std::vector<UnvalidatedConvertOutput<Type>> canonical; |
| 103 | canonical.reserve(arguments.size()); |
| 104 | for (const auto& argument : arguments) { |
| 105 | canonical.push_back(NN_TRY(validatedConvert(argument))); |
| 106 | } |
| 107 | return canonical; |
| 108 | } |
| 109 | |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 110 | struct NativeHandleDeleter { |
| 111 | void operator()(native_handle_t* handle) const { |
| 112 | if (handle) { |
| 113 | native_handle_close(handle); |
| 114 | native_handle_delete(handle); |
| 115 | } |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | using UniqueNativeHandle = std::unique_ptr<native_handle_t, NativeHandleDeleter>; |
| 120 | |
Ray Hernandez | 338d6f8 | 2021-08-11 21:29:20 +0000 | [diff] [blame] | 121 | #ifdef __ANDROID__ |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 122 | GeneralResult<UniqueNativeHandle> nativeHandleFromAidlHandle(const NativeHandle& handle) { |
| 123 | auto nativeHandle = UniqueNativeHandle(dupFromAidl(handle)); |
| 124 | if (nativeHandle.get() == nullptr) { |
| 125 | return NN_ERROR() << "android::dupFromAidl failed to convert the common::NativeHandle to a " |
| 126 | "native_handle_t"; |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 127 | } |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 128 | if (!std::all_of(nativeHandle->data + 0, nativeHandle->data + nativeHandle->numFds, |
| 129 | [](int fd) { return fd >= 0; })) { |
| 130 | return NN_ERROR() << "android::dupFromAidl returned an invalid native_handle_t"; |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 131 | } |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 132 | return nativeHandle; |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 133 | } |
Ray Hernandez | 338d6f8 | 2021-08-11 21:29:20 +0000 | [diff] [blame] | 134 | #endif // __ANDROID__ |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 135 | |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 136 | } // anonymous namespace |
| 137 | |
| 138 | GeneralResult<OperandType> unvalidatedConvert(const aidl_hal::OperandType& operandType) { |
| 139 | VERIFY_NON_NEGATIVE(underlyingType(operandType)) << "Negative operand types are not allowed."; |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 140 | const auto canonical = static_cast<OperandType>(operandType); |
| 141 | if (canonical == OperandType::OEM || canonical == OperandType::TENSOR_OEM_BYTE) { |
| 142 | return NN_ERROR() << "Unable to convert invalid OperandType " << canonical; |
| 143 | } |
| 144 | return canonical; |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | GeneralResult<OperationType> unvalidatedConvert(const aidl_hal::OperationType& operationType) { |
| 148 | VERIFY_NON_NEGATIVE(underlyingType(operationType)) |
| 149 | << "Negative operation types are not allowed."; |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 150 | const auto canonical = static_cast<OperationType>(operationType); |
| 151 | if (canonical == OperationType::OEM_OPERATION) { |
| 152 | return NN_ERROR() << "Unable to convert invalid OperationType OEM_OPERATION"; |
| 153 | } |
| 154 | return canonical; |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | GeneralResult<DeviceType> unvalidatedConvert(const aidl_hal::DeviceType& deviceType) { |
| 158 | return static_cast<DeviceType>(deviceType); |
| 159 | } |
| 160 | |
| 161 | GeneralResult<Priority> unvalidatedConvert(const aidl_hal::Priority& priority) { |
| 162 | return static_cast<Priority>(priority); |
| 163 | } |
| 164 | |
| 165 | GeneralResult<Capabilities> unvalidatedConvert(const aidl_hal::Capabilities& capabilities) { |
| 166 | const bool validOperandTypes = std::all_of( |
| 167 | capabilities.operandPerformance.begin(), capabilities.operandPerformance.end(), |
| 168 | [](const aidl_hal::OperandPerformance& operandPerformance) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 169 | return validatedConvert(operandPerformance.type).has_value(); |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 170 | }); |
| 171 | if (!validOperandTypes) { |
| 172 | return NN_ERROR() << "Invalid OperandType when unvalidatedConverting OperandPerformance in " |
| 173 | "Capabilities"; |
| 174 | } |
| 175 | |
| 176 | auto operandPerformance = NN_TRY(unvalidatedConvert(capabilities.operandPerformance)); |
Michael Butler | ff9a5a5 | 2021-10-15 16:23:20 -0700 | [diff] [blame] | 177 | auto table = |
| 178 | NN_TRY(Capabilities::OperandPerformanceTable::create(std::move(operandPerformance))); |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 179 | |
| 180 | return Capabilities{ |
| 181 | .relaxedFloat32toFloat16PerformanceScalar = NN_TRY( |
| 182 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceScalar)), |
| 183 | .relaxedFloat32toFloat16PerformanceTensor = NN_TRY( |
| 184 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceTensor)), |
| 185 | .operandPerformance = std::move(table), |
| 186 | .ifPerformance = NN_TRY(unvalidatedConvert(capabilities.ifPerformance)), |
| 187 | .whilePerformance = NN_TRY(unvalidatedConvert(capabilities.whilePerformance)), |
| 188 | }; |
| 189 | } |
| 190 | |
| 191 | GeneralResult<Capabilities::OperandPerformance> unvalidatedConvert( |
| 192 | const aidl_hal::OperandPerformance& operandPerformance) { |
| 193 | return Capabilities::OperandPerformance{ |
| 194 | .type = NN_TRY(unvalidatedConvert(operandPerformance.type)), |
| 195 | .info = NN_TRY(unvalidatedConvert(operandPerformance.info)), |
| 196 | }; |
| 197 | } |
| 198 | |
| 199 | GeneralResult<Capabilities::PerformanceInfo> unvalidatedConvert( |
| 200 | const aidl_hal::PerformanceInfo& performanceInfo) { |
| 201 | return Capabilities::PerformanceInfo{ |
| 202 | .execTime = performanceInfo.execTime, |
| 203 | .powerUsage = performanceInfo.powerUsage, |
| 204 | }; |
| 205 | } |
| 206 | |
| 207 | GeneralResult<DataLocation> unvalidatedConvert(const aidl_hal::DataLocation& location) { |
| 208 | VERIFY_NON_NEGATIVE(location.poolIndex) << "DataLocation: pool index must not be negative"; |
| 209 | VERIFY_NON_NEGATIVE(location.offset) << "DataLocation: offset must not be negative"; |
| 210 | VERIFY_NON_NEGATIVE(location.length) << "DataLocation: length must not be negative"; |
Xusong Wang | 5e36ca0 | 2021-02-16 10:40:32 -0800 | [diff] [blame] | 211 | VERIFY_NON_NEGATIVE(location.padding) << "DataLocation: padding must not be negative"; |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 212 | if (location.offset > std::numeric_limits<uint32_t>::max()) { |
| 213 | return NN_ERROR() << "DataLocation: offset must be <= std::numeric_limits<uint32_t>::max()"; |
| 214 | } |
| 215 | if (location.length > std::numeric_limits<uint32_t>::max()) { |
| 216 | return NN_ERROR() << "DataLocation: length must be <= std::numeric_limits<uint32_t>::max()"; |
| 217 | } |
Xusong Wang | 5e36ca0 | 2021-02-16 10:40:32 -0800 | [diff] [blame] | 218 | if (location.padding > std::numeric_limits<uint32_t>::max()) { |
| 219 | return NN_ERROR() |
| 220 | << "DataLocation: padding must be <= std::numeric_limits<uint32_t>::max()"; |
| 221 | } |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 222 | return DataLocation{ |
| 223 | .poolIndex = static_cast<uint32_t>(location.poolIndex), |
| 224 | .offset = static_cast<uint32_t>(location.offset), |
| 225 | .length = static_cast<uint32_t>(location.length), |
Xusong Wang | 5e36ca0 | 2021-02-16 10:40:32 -0800 | [diff] [blame] | 226 | .padding = static_cast<uint32_t>(location.padding), |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 227 | }; |
| 228 | } |
| 229 | |
| 230 | GeneralResult<Operation> unvalidatedConvert(const aidl_hal::Operation& operation) { |
| 231 | return Operation{ |
| 232 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
| 233 | .inputs = NN_TRY(toUnsigned(operation.inputs)), |
| 234 | .outputs = NN_TRY(toUnsigned(operation.outputs)), |
| 235 | }; |
| 236 | } |
| 237 | |
| 238 | GeneralResult<Operand::LifeTime> unvalidatedConvert( |
| 239 | const aidl_hal::OperandLifeTime& operandLifeTime) { |
| 240 | return static_cast<Operand::LifeTime>(operandLifeTime); |
| 241 | } |
| 242 | |
| 243 | GeneralResult<Operand> unvalidatedConvert(const aidl_hal::Operand& operand) { |
| 244 | return Operand{ |
| 245 | .type = NN_TRY(unvalidatedConvert(operand.type)), |
| 246 | .dimensions = NN_TRY(toUnsigned(operand.dimensions)), |
| 247 | .scale = operand.scale, |
| 248 | .zeroPoint = operand.zeroPoint, |
| 249 | .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), |
| 250 | .location = NN_TRY(unvalidatedConvert(operand.location)), |
| 251 | .extraParams = NN_TRY(unvalidatedConvert(operand.extraParams)), |
| 252 | }; |
| 253 | } |
| 254 | |
| 255 | GeneralResult<Operand::ExtraParams> unvalidatedConvert( |
| 256 | const std::optional<aidl_hal::OperandExtraParams>& optionalExtraParams) { |
| 257 | if (!optionalExtraParams.has_value()) { |
| 258 | return Operand::NoParams{}; |
| 259 | } |
| 260 | const auto& extraParams = optionalExtraParams.value(); |
| 261 | using Tag = aidl_hal::OperandExtraParams::Tag; |
| 262 | switch (extraParams.getTag()) { |
| 263 | case Tag::channelQuant: |
| 264 | return unvalidatedConvert(extraParams.get<Tag::channelQuant>()); |
| 265 | case Tag::extension: |
| 266 | return extraParams.get<Tag::extension>(); |
| 267 | } |
| 268 | return NN_ERROR() << "Unrecognized Operand::ExtraParams tag: " |
| 269 | << underlyingType(extraParams.getTag()); |
| 270 | } |
| 271 | |
| 272 | GeneralResult<Operand::SymmPerChannelQuantParams> unvalidatedConvert( |
| 273 | const aidl_hal::SymmPerChannelQuantParams& symmPerChannelQuantParams) { |
| 274 | VERIFY_NON_NEGATIVE(symmPerChannelQuantParams.channelDim) |
| 275 | << "Per-channel quantization channel dimension must not be negative."; |
| 276 | return Operand::SymmPerChannelQuantParams{ |
| 277 | .scales = symmPerChannelQuantParams.scales, |
| 278 | .channelDim = static_cast<uint32_t>(symmPerChannelQuantParams.channelDim), |
| 279 | }; |
| 280 | } |
| 281 | |
| 282 | GeneralResult<Model> unvalidatedConvert(const aidl_hal::Model& model) { |
| 283 | return Model{ |
| 284 | .main = NN_TRY(unvalidatedConvert(model.main)), |
| 285 | .referenced = NN_TRY(unvalidatedConvert(model.referenced)), |
| 286 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 287 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
| 288 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
| 289 | .extensionNameToPrefix = NN_TRY(unvalidatedConvert(model.extensionNameToPrefix)), |
| 290 | }; |
| 291 | } |
| 292 | |
| 293 | GeneralResult<Model::Subgraph> unvalidatedConvert(const aidl_hal::Subgraph& subgraph) { |
| 294 | return Model::Subgraph{ |
| 295 | .operands = NN_TRY(unvalidatedConvert(subgraph.operands)), |
| 296 | .operations = NN_TRY(unvalidatedConvert(subgraph.operations)), |
| 297 | .inputIndexes = NN_TRY(toUnsigned(subgraph.inputIndexes)), |
| 298 | .outputIndexes = NN_TRY(toUnsigned(subgraph.outputIndexes)), |
| 299 | }; |
| 300 | } |
| 301 | |
Miao Wang | b5c8a82 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 302 | GeneralResult<ExtensionNameAndPrefix> unvalidatedConvert( |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 303 | const aidl_hal::ExtensionNameAndPrefix& extensionNameAndPrefix) { |
Miao Wang | b5c8a82 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 304 | return ExtensionNameAndPrefix{ |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 305 | .name = extensionNameAndPrefix.name, |
| 306 | .prefix = extensionNameAndPrefix.prefix, |
| 307 | }; |
| 308 | } |
| 309 | |
| 310 | GeneralResult<Extension> unvalidatedConvert(const aidl_hal::Extension& extension) { |
| 311 | return Extension{ |
| 312 | .name = extension.name, |
| 313 | .operandTypes = NN_TRY(unvalidatedConvert(extension.operandTypes)), |
| 314 | }; |
| 315 | } |
| 316 | |
| 317 | GeneralResult<Extension::OperandTypeInformation> unvalidatedConvert( |
| 318 | const aidl_hal::ExtensionOperandTypeInformation& operandTypeInformation) { |
| 319 | VERIFY_NON_NEGATIVE(operandTypeInformation.byteSize) |
| 320 | << "Extension operand type byte size must not be negative"; |
| 321 | return Extension::OperandTypeInformation{ |
| 322 | .type = operandTypeInformation.type, |
| 323 | .isTensor = operandTypeInformation.isTensor, |
| 324 | .byteSize = static_cast<uint32_t>(operandTypeInformation.byteSize), |
| 325 | }; |
| 326 | } |
| 327 | |
| 328 | GeneralResult<OutputShape> unvalidatedConvert(const aidl_hal::OutputShape& outputShape) { |
| 329 | return OutputShape{ |
| 330 | .dimensions = NN_TRY(toUnsigned(outputShape.dimensions)), |
| 331 | .isSufficient = outputShape.isSufficient, |
| 332 | }; |
| 333 | } |
| 334 | |
| 335 | GeneralResult<MeasureTiming> unvalidatedConvert(bool measureTiming) { |
| 336 | return measureTiming ? MeasureTiming::YES : MeasureTiming::NO; |
| 337 | } |
| 338 | |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 339 | GeneralResult<SharedMemory> unvalidatedConvert(const aidl_hal::Memory& memory) { |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 340 | using Tag = aidl_hal::Memory::Tag; |
| 341 | switch (memory.getTag()) { |
| 342 | case Tag::ashmem: { |
| 343 | const auto& ashmem = memory.get<Tag::ashmem>(); |
| 344 | VERIFY_NON_NEGATIVE(ashmem.size) << "Memory size must not be negative"; |
| 345 | if (ashmem.size > std::numeric_limits<size_t>::max()) { |
| 346 | return NN_ERROR() << "Memory: size must be <= std::numeric_limits<size_t>::max()"; |
| 347 | } |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 348 | |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 349 | auto handle = Memory::Ashmem{ |
| 350 | .fd = NN_TRY(dupFd(ashmem.fd.get())), |
| 351 | .size = static_cast<size_t>(ashmem.size), |
| 352 | }; |
| 353 | return std::make_shared<const Memory>(Memory{.handle = std::move(handle)}); |
| 354 | } |
| 355 | case Tag::mappableFile: { |
| 356 | const auto& mappableFile = memory.get<Tag::mappableFile>(); |
| 357 | VERIFY_NON_NEGATIVE(mappableFile.length) << "Memory size must not be negative"; |
| 358 | VERIFY_NON_NEGATIVE(mappableFile.offset) << "Memory offset must not be negative"; |
| 359 | if (mappableFile.length > std::numeric_limits<size_t>::max()) { |
| 360 | return NN_ERROR() << "Memory: size must be <= std::numeric_limits<size_t>::max()"; |
| 361 | } |
| 362 | if (mappableFile.offset > std::numeric_limits<size_t>::max()) { |
| 363 | return NN_ERROR() << "Memory: offset must be <= std::numeric_limits<size_t>::max()"; |
| 364 | } |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 365 | |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 366 | const size_t size = static_cast<size_t>(mappableFile.length); |
| 367 | const int prot = mappableFile.prot; |
| 368 | const int fd = mappableFile.fd.get(); |
| 369 | const size_t offset = static_cast<size_t>(mappableFile.offset); |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 370 | |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 371 | return createSharedMemoryFromFd(size, prot, fd, offset); |
| 372 | } |
| 373 | case Tag::hardwareBuffer: { |
Ray Hernandez | 338d6f8 | 2021-08-11 21:29:20 +0000 | [diff] [blame] | 374 | #ifdef __ANDROID__ |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 375 | const auto& hardwareBuffer = memory.get<Tag::hardwareBuffer>(); |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 376 | |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 377 | const UniqueNativeHandle handle = |
| 378 | NN_TRY(nativeHandleFromAidlHandle(hardwareBuffer.handle)); |
| 379 | const native_handle_t* nativeHandle = handle.get(); |
| 380 | |
| 381 | const AHardwareBuffer_Desc desc{ |
| 382 | .width = static_cast<uint32_t>(hardwareBuffer.description.width), |
| 383 | .height = static_cast<uint32_t>(hardwareBuffer.description.height), |
| 384 | .layers = static_cast<uint32_t>(hardwareBuffer.description.layers), |
| 385 | .format = static_cast<uint32_t>(hardwareBuffer.description.format), |
| 386 | .usage = static_cast<uint64_t>(hardwareBuffer.description.usage), |
| 387 | .stride = static_cast<uint32_t>(hardwareBuffer.description.stride), |
| 388 | }; |
| 389 | AHardwareBuffer* ahwb = nullptr; |
| 390 | const status_t status = AHardwareBuffer_createFromHandle( |
| 391 | &desc, nativeHandle, AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_CLONE, &ahwb); |
| 392 | if (status != NO_ERROR) { |
| 393 | return NN_ERROR() << "createFromHandle failed"; |
| 394 | } |
| 395 | |
| 396 | return createSharedMemoryFromAHWB(ahwb, /*takeOwnership=*/true); |
Ray Hernandez | 338d6f8 | 2021-08-11 21:29:20 +0000 | [diff] [blame] | 397 | #else // __ANDROID__ |
| 398 | LOG(FATAL) << "GeneralResult<SharedMemory> unvalidatedConvert(const aidl_hal::Memory& " |
| 399 | "memory): Not Available on Host Build"; |
| 400 | return NN_ERROR() << "createFromHandle failed"; |
| 401 | #endif // __ANDROID__ |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 402 | } |
| 403 | } |
Jooyung Han | 0bdded6 | 2022-02-26 21:10:12 +0900 | [diff] [blame^] | 404 | return NN_ERROR() << "Unrecognized Memory::Tag: " << underlyingType(memory.getTag()); |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 407 | GeneralResult<Timing> unvalidatedConvert(const aidl_hal::Timing& timing) { |
Lev Proleev | 8df7d6e | 2021-04-14 20:54:27 +0100 | [diff] [blame] | 408 | if (timing.timeInDriverNs < -1) { |
| 409 | return NN_ERROR() << "Timing: timeInDriverNs must not be less than -1"; |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 410 | } |
Lev Proleev | 8df7d6e | 2021-04-14 20:54:27 +0100 | [diff] [blame] | 411 | if (timing.timeOnDeviceNs < -1) { |
| 412 | return NN_ERROR() << "Timing: timeOnDeviceNs must not be less than -1"; |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 413 | } |
| 414 | constexpr auto convertTiming = [](int64_t halTiming) -> OptionalDuration { |
| 415 | if (halTiming == kNoTiming) { |
| 416 | return {}; |
| 417 | } |
| 418 | return nn::Duration(static_cast<uint64_t>(halTiming)); |
| 419 | }; |
Lev Proleev | 8df7d6e | 2021-04-14 20:54:27 +0100 | [diff] [blame] | 420 | return Timing{.timeOnDevice = convertTiming(timing.timeOnDeviceNs), |
| 421 | .timeInDriver = convertTiming(timing.timeInDriverNs)}; |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 424 | GeneralResult<Model::OperandValues> unvalidatedConvert(const std::vector<uint8_t>& operandValues) { |
| 425 | return Model::OperandValues(operandValues.data(), operandValues.size()); |
| 426 | } |
| 427 | |
| 428 | GeneralResult<BufferDesc> unvalidatedConvert(const aidl_hal::BufferDesc& bufferDesc) { |
| 429 | return BufferDesc{.dimensions = NN_TRY(toUnsigned(bufferDesc.dimensions))}; |
| 430 | } |
| 431 | |
| 432 | GeneralResult<BufferRole> unvalidatedConvert(const aidl_hal::BufferRole& bufferRole) { |
| 433 | VERIFY_NON_NEGATIVE(bufferRole.modelIndex) << "BufferRole: modelIndex must not be negative"; |
| 434 | VERIFY_NON_NEGATIVE(bufferRole.ioIndex) << "BufferRole: ioIndex must not be negative"; |
| 435 | return BufferRole{ |
| 436 | .modelIndex = static_cast<uint32_t>(bufferRole.modelIndex), |
| 437 | .ioIndex = static_cast<uint32_t>(bufferRole.ioIndex), |
Xusong Wang | 3633d07 | 2021-03-19 13:58:24 -0700 | [diff] [blame] | 438 | .probability = bufferRole.probability, |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 439 | }; |
| 440 | } |
| 441 | |
| 442 | GeneralResult<Request> unvalidatedConvert(const aidl_hal::Request& request) { |
| 443 | return Request{ |
| 444 | .inputs = NN_TRY(unvalidatedConvert(request.inputs)), |
| 445 | .outputs = NN_TRY(unvalidatedConvert(request.outputs)), |
| 446 | .pools = NN_TRY(unvalidatedConvert(request.pools)), |
| 447 | }; |
| 448 | } |
| 449 | |
| 450 | GeneralResult<Request::Argument> unvalidatedConvert(const aidl_hal::RequestArgument& argument) { |
| 451 | const auto lifetime = argument.hasNoValue ? Request::Argument::LifeTime::NO_VALUE |
| 452 | : Request::Argument::LifeTime::POOL; |
| 453 | return Request::Argument{ |
| 454 | .lifetime = lifetime, |
| 455 | .location = NN_TRY(unvalidatedConvert(argument.location)), |
| 456 | .dimensions = NN_TRY(toUnsigned(argument.dimensions)), |
| 457 | }; |
| 458 | } |
| 459 | |
| 460 | GeneralResult<Request::MemoryPool> unvalidatedConvert( |
| 461 | const aidl_hal::RequestMemoryPool& memoryPool) { |
| 462 | using Tag = aidl_hal::RequestMemoryPool::Tag; |
| 463 | switch (memoryPool.getTag()) { |
| 464 | case Tag::pool: |
| 465 | return unvalidatedConvert(memoryPool.get<Tag::pool>()); |
| 466 | case Tag::token: { |
| 467 | const auto token = memoryPool.get<Tag::token>(); |
| 468 | VERIFY_NON_NEGATIVE(token) << "Memory pool token must not be negative"; |
| 469 | return static_cast<Request::MemoryDomainToken>(token); |
| 470 | } |
| 471 | } |
| 472 | return NN_ERROR() << "Invalid Request::MemoryPool tag " << underlyingType(memoryPool.getTag()); |
| 473 | } |
| 474 | |
| 475 | GeneralResult<ErrorStatus> unvalidatedConvert(const aidl_hal::ErrorStatus& status) { |
| 476 | switch (status) { |
| 477 | case aidl_hal::ErrorStatus::NONE: |
| 478 | case aidl_hal::ErrorStatus::DEVICE_UNAVAILABLE: |
| 479 | case aidl_hal::ErrorStatus::GENERAL_FAILURE: |
| 480 | case aidl_hal::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 481 | case aidl_hal::ErrorStatus::INVALID_ARGUMENT: |
| 482 | case aidl_hal::ErrorStatus::MISSED_DEADLINE_TRANSIENT: |
| 483 | case aidl_hal::ErrorStatus::MISSED_DEADLINE_PERSISTENT: |
| 484 | case aidl_hal::ErrorStatus::RESOURCE_EXHAUSTED_TRANSIENT: |
| 485 | case aidl_hal::ErrorStatus::RESOURCE_EXHAUSTED_PERSISTENT: |
| 486 | return static_cast<ErrorStatus>(status); |
| 487 | } |
| 488 | return NN_ERROR() << "Invalid ErrorStatus " << underlyingType(status); |
| 489 | } |
| 490 | |
| 491 | GeneralResult<ExecutionPreference> unvalidatedConvert( |
| 492 | const aidl_hal::ExecutionPreference& executionPreference) { |
| 493 | return static_cast<ExecutionPreference>(executionPreference); |
| 494 | } |
| 495 | |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 496 | GeneralResult<std::vector<Operation>> unvalidatedConvert( |
| 497 | const std::vector<aidl_hal::Operation>& operations) { |
| 498 | return unvalidatedConvertVec(operations); |
| 499 | } |
| 500 | |
Michael Butler | e52a77e | 2021-06-07 13:10:58 -0700 | [diff] [blame] | 501 | GeneralResult<SharedHandle> unvalidatedConvert(const ndk::ScopedFileDescriptor& handle) { |
| 502 | auto duplicatedFd = NN_TRY(dupFd(handle.get())); |
| 503 | return std::make_shared<const Handle>(std::move(duplicatedFd)); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Miao Wang | b5c8a82 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 506 | #ifdef NN_AIDL_V4_OR_ABOVE |
| 507 | GeneralResult<TokenValuePair> unvalidatedConvert(const aidl_hal::TokenValuePair& tokenValuePair) { |
| 508 | return TokenValuePair{.token = tokenValuePair.token, .value = tokenValuePair.value}; |
| 509 | } |
| 510 | #endif // NN_AIDL_V4_OR_ABOVE |
| 511 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 512 | GeneralResult<Capabilities> convert(const aidl_hal::Capabilities& capabilities) { |
| 513 | return validatedConvert(capabilities); |
| 514 | } |
| 515 | |
| 516 | GeneralResult<DeviceType> convert(const aidl_hal::DeviceType& deviceType) { |
| 517 | return validatedConvert(deviceType); |
| 518 | } |
| 519 | |
| 520 | GeneralResult<ErrorStatus> convert(const aidl_hal::ErrorStatus& errorStatus) { |
| 521 | return validatedConvert(errorStatus); |
| 522 | } |
| 523 | |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 524 | GeneralResult<ExecutionPreference> convert( |
| 525 | const aidl_hal::ExecutionPreference& executionPreference) { |
| 526 | return validatedConvert(executionPreference); |
| 527 | } |
| 528 | |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 529 | GeneralResult<SharedMemory> convert(const aidl_hal::Memory& operand) { |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 530 | return validatedConvert(operand); |
| 531 | } |
| 532 | |
| 533 | GeneralResult<Model> convert(const aidl_hal::Model& model) { |
| 534 | return validatedConvert(model); |
| 535 | } |
| 536 | |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 537 | GeneralResult<OperandType> convert(const aidl_hal::OperandType& operandType) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 538 | return validatedConvert(operandType); |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | GeneralResult<Priority> convert(const aidl_hal::Priority& priority) { |
| 542 | return validatedConvert(priority); |
| 543 | } |
| 544 | |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 545 | GeneralResult<Request> convert(const aidl_hal::Request& request) { |
| 546 | return validatedConvert(request); |
| 547 | } |
| 548 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 549 | GeneralResult<Timing> convert(const aidl_hal::Timing& timing) { |
| 550 | return validatedConvert(timing); |
| 551 | } |
| 552 | |
Michael Butler | e52a77e | 2021-06-07 13:10:58 -0700 | [diff] [blame] | 553 | GeneralResult<SharedHandle> convert(const ndk::ScopedFileDescriptor& handle) { |
| 554 | return validatedConvert(handle); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Michael Butler | 5345563 | 2021-10-25 11:20:33 -0700 | [diff] [blame] | 557 | GeneralResult<BufferDesc> convert(const aidl_hal::BufferDesc& bufferDesc) { |
| 558 | return validatedConvert(bufferDesc); |
| 559 | } |
| 560 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 561 | GeneralResult<std::vector<Extension>> convert(const std::vector<aidl_hal::Extension>& extension) { |
| 562 | return validatedConvert(extension); |
| 563 | } |
| 564 | |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 565 | GeneralResult<std::vector<SharedMemory>> convert(const std::vector<aidl_hal::Memory>& memories) { |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 566 | return validatedConvert(memories); |
| 567 | } |
Miao Wang | b5c8a82 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 568 | GeneralResult<std::vector<ExtensionNameAndPrefix>> convert( |
| 569 | const std::vector<aidl_hal::ExtensionNameAndPrefix>& extensionNameAndPrefix) { |
| 570 | return unvalidatedConvert(extensionNameAndPrefix); |
| 571 | } |
| 572 | |
| 573 | #ifdef NN_AIDL_V4_OR_ABOVE |
| 574 | GeneralResult<std::vector<TokenValuePair>> convert( |
| 575 | const std::vector<aidl_hal::TokenValuePair>& metaData) { |
| 576 | return validatedConvert(metaData); |
| 577 | } |
| 578 | #endif // NN_AIDL_V4_OR_ABOVE |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 579 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 580 | GeneralResult<std::vector<OutputShape>> convert( |
| 581 | const std::vector<aidl_hal::OutputShape>& outputShapes) { |
| 582 | return validatedConvert(outputShapes); |
| 583 | } |
| 584 | |
Michael Butler | 5345563 | 2021-10-25 11:20:33 -0700 | [diff] [blame] | 585 | GeneralResult<std::vector<SharedHandle>> convert( |
| 586 | const std::vector<ndk::ScopedFileDescriptor>& handles) { |
| 587 | return validatedConvert(handles); |
| 588 | } |
| 589 | |
| 590 | GeneralResult<std::vector<BufferRole>> convert(const std::vector<aidl_hal::BufferRole>& roles) { |
| 591 | return validatedConvert(roles); |
| 592 | } |
| 593 | |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 594 | GeneralResult<std::vector<uint32_t>> toUnsigned(const std::vector<int32_t>& vec) { |
| 595 | if (!std::all_of(vec.begin(), vec.end(), [](int32_t v) { return v >= 0; })) { |
| 596 | return NN_ERROR() << "Negative value passed to conversion from signed to unsigned"; |
| 597 | } |
| 598 | return std::vector<uint32_t>(vec.begin(), vec.end()); |
| 599 | } |
| 600 | |
| 601 | } // namespace android::nn |
| 602 | |
| 603 | namespace aidl::android::hardware::neuralnetworks::utils { |
| 604 | namespace { |
| 605 | |
Michael Butler | 5345563 | 2021-10-25 11:20:33 -0700 | [diff] [blame] | 606 | using utils::unvalidatedConvert; |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 607 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 608 | // Helper template for std::visit |
| 609 | template <class... Ts> |
| 610 | struct overloaded : Ts... { |
| 611 | using Ts::operator()...; |
| 612 | }; |
| 613 | template <class... Ts> |
Xusong Wang | 8805b2d | 2022-01-21 10:15:19 -0800 | [diff] [blame] | 614 | overloaded(Ts...) -> overloaded<Ts...>; |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 615 | |
Ray Hernandez | 338d6f8 | 2021-08-11 21:29:20 +0000 | [diff] [blame] | 616 | #ifdef __ANDROID__ |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 617 | nn::GeneralResult<common::NativeHandle> aidlHandleFromNativeHandle( |
| 618 | const native_handle_t& nativeHandle) { |
| 619 | auto handle = ::android::dupToAidl(&nativeHandle); |
| 620 | if (!std::all_of(handle.fds.begin(), handle.fds.end(), |
| 621 | [](const ndk::ScopedFileDescriptor& fd) { return fd.get() >= 0; })) { |
| 622 | return NN_ERROR() << "android::dupToAidl returned an invalid common::NativeHandle"; |
| 623 | } |
| 624 | return handle; |
| 625 | } |
Ray Hernandez | 338d6f8 | 2021-08-11 21:29:20 +0000 | [diff] [blame] | 626 | #endif // __ANDROID__ |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 627 | |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 628 | nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::Ashmem& memory) { |
| 629 | if constexpr (std::numeric_limits<size_t>::max() > std::numeric_limits<int64_t>::max()) { |
| 630 | if (memory.size > std::numeric_limits<int64_t>::max()) { |
| 631 | return ( |
| 632 | NN_ERROR() |
| 633 | << "Memory::Ashmem: size must be <= std::numeric_limits<int64_t>::max()") |
| 634 | . |
| 635 | operator nn::GeneralResult<Memory>(); |
| 636 | } |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 639 | auto fd = NN_TRY(nn::dupFd(memory.fd)); |
| 640 | auto handle = common::Ashmem{ |
| 641 | .fd = ndk::ScopedFileDescriptor(fd.release()), |
| 642 | .size = static_cast<int64_t>(memory.size), |
| 643 | }; |
| 644 | return Memory::make<Memory::Tag::ashmem>(std::move(handle)); |
| 645 | } |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 646 | |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 647 | nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::Fd& memory) { |
| 648 | if constexpr (std::numeric_limits<size_t>::max() > std::numeric_limits<int64_t>::max()) { |
| 649 | if (memory.size > std::numeric_limits<int64_t>::max()) { |
| 650 | return (NN_ERROR() << "Memory::Fd: size must be <= std::numeric_limits<int64_t>::max()") |
| 651 | . |
| 652 | operator nn::GeneralResult<Memory>(); |
| 653 | } |
| 654 | if (memory.offset > std::numeric_limits<int64_t>::max()) { |
| 655 | return ( |
| 656 | NN_ERROR() |
| 657 | << "Memory::Fd: offset must be <= std::numeric_limits<int64_t>::max()") |
| 658 | . |
| 659 | operator nn::GeneralResult<Memory>(); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | auto fd = NN_TRY(nn::dupFd(memory.fd)); |
| 664 | auto handle = common::MappableFile{ |
| 665 | .length = static_cast<int64_t>(memory.size), |
| 666 | .prot = memory.prot, |
| 667 | .fd = ndk::ScopedFileDescriptor(fd.release()), |
| 668 | .offset = static_cast<int64_t>(memory.offset), |
| 669 | }; |
| 670 | return Memory::make<Memory::Tag::mappableFile>(std::move(handle)); |
| 671 | } |
| 672 | |
| 673 | nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::HardwareBuffer& memory) { |
Ray Hernandez | 338d6f8 | 2021-08-11 21:29:20 +0000 | [diff] [blame] | 674 | #ifdef __ANDROID__ |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 675 | const native_handle_t* nativeHandle = AHardwareBuffer_getNativeHandle(memory.handle.get()); |
| 676 | if (nativeHandle == nullptr) { |
| 677 | return (NN_ERROR() << "unvalidatedConvert failed because AHardwareBuffer_getNativeHandle " |
| 678 | "returned nullptr") |
| 679 | . |
| 680 | operator nn::GeneralResult<Memory>(); |
| 681 | } |
| 682 | |
| 683 | auto handle = NN_TRY(aidlHandleFromNativeHandle(*nativeHandle)); |
| 684 | |
| 685 | AHardwareBuffer_Desc desc; |
| 686 | AHardwareBuffer_describe(memory.handle.get(), &desc); |
| 687 | |
| 688 | const auto description = graphics::common::HardwareBufferDescription{ |
| 689 | .width = static_cast<int32_t>(desc.width), |
| 690 | .height = static_cast<int32_t>(desc.height), |
| 691 | .layers = static_cast<int32_t>(desc.layers), |
| 692 | .format = static_cast<graphics::common::PixelFormat>(desc.format), |
| 693 | .usage = static_cast<graphics::common::BufferUsage>(desc.usage), |
| 694 | .stride = static_cast<int32_t>(desc.stride), |
| 695 | }; |
| 696 | |
| 697 | auto hardwareBuffer = graphics::common::HardwareBuffer{ |
| 698 | .description = std::move(description), |
| 699 | .handle = std::move(handle), |
| 700 | }; |
| 701 | return Memory::make<Memory::Tag::hardwareBuffer>(std::move(hardwareBuffer)); |
Ray Hernandez | 338d6f8 | 2021-08-11 21:29:20 +0000 | [diff] [blame] | 702 | #else // __ANDROID__ |
| 703 | LOG(FATAL) << "nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::HardwareBuffer& " |
| 704 | "memory): Not Available on Host Build"; |
| 705 | (void)memory; |
| 706 | return (NN_ERROR() << "unvalidatedConvert failed").operator nn::GeneralResult<Memory>(); |
| 707 | #endif // __ANDROID__ |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | nn::GeneralResult<Memory> unvalidatedConvert(const nn::Memory::Unknown& /*memory*/) { |
| 711 | return (NN_ERROR() << "Unable to convert Unknown memory type") |
| 712 | . |
| 713 | operator nn::GeneralResult<Memory>(); |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 714 | } |
| 715 | |
Michael Butler | 5345563 | 2021-10-25 11:20:33 -0700 | [diff] [blame] | 716 | nn::GeneralResult<PerformanceInfo> unvalidatedConvert( |
| 717 | const nn::Capabilities::PerformanceInfo& info) { |
| 718 | return PerformanceInfo{.execTime = info.execTime, .powerUsage = info.powerUsage}; |
| 719 | } |
| 720 | |
| 721 | nn::GeneralResult<OperandPerformance> unvalidatedConvert( |
| 722 | const nn::Capabilities::OperandPerformance& operandPerformance) { |
| 723 | return OperandPerformance{.type = NN_TRY(unvalidatedConvert(operandPerformance.type)), |
| 724 | .info = NN_TRY(unvalidatedConvert(operandPerformance.info))}; |
| 725 | } |
| 726 | |
| 727 | nn::GeneralResult<std::vector<OperandPerformance>> unvalidatedConvert( |
| 728 | const nn::Capabilities::OperandPerformanceTable& table) { |
| 729 | std::vector<OperandPerformance> operandPerformances; |
| 730 | operandPerformances.reserve(table.asVector().size()); |
| 731 | for (const auto& operandPerformance : table.asVector()) { |
| 732 | operandPerformances.push_back(NN_TRY(unvalidatedConvert(operandPerformance))); |
| 733 | } |
| 734 | return operandPerformances; |
| 735 | } |
| 736 | |
| 737 | nn::GeneralResult<ExtensionOperandTypeInformation> unvalidatedConvert( |
| 738 | const nn::Extension::OperandTypeInformation& info) { |
| 739 | return ExtensionOperandTypeInformation{.type = info.type, |
| 740 | .isTensor = info.isTensor, |
| 741 | .byteSize = static_cast<int32_t>(info.byteSize)}; |
| 742 | } |
| 743 | |
| 744 | nn::GeneralResult<int64_t> unvalidatedConvert(const nn::Duration& duration) { |
| 745 | if (duration < nn::Duration::zero()) { |
| 746 | return NN_ERROR() << "Unable to convert invalid (negative) duration"; |
| 747 | } |
| 748 | constexpr std::chrono::nanoseconds::rep kIntMax = std::numeric_limits<int64_t>::max(); |
| 749 | const auto count = duration.count(); |
| 750 | return static_cast<int64_t>(std::min(count, kIntMax)); |
| 751 | } |
| 752 | |
| 753 | template <typename Input> |
| 754 | using UnvalidatedConvertOutput = |
| 755 | std::decay_t<decltype(unvalidatedConvert(std::declval<Input>()).value())>; |
| 756 | |
| 757 | template <typename Type> |
| 758 | nn::GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> unvalidatedConvert( |
| 759 | const std::vector<Type>& arguments) { |
| 760 | std::vector<UnvalidatedConvertOutput<Type>> halObject; |
| 761 | halObject.reserve(arguments.size()); |
| 762 | for (const auto& argument : arguments) { |
| 763 | halObject.push_back(NN_TRY(unvalidatedConvert(argument))); |
| 764 | } |
| 765 | return halObject; |
| 766 | } |
| 767 | |
| 768 | template <typename Type> |
| 769 | nn::GeneralResult<UnvalidatedConvertOutput<Type>> validatedConvert(const Type& canonical) { |
| 770 | NN_TRY(compliantVersion(canonical)); |
| 771 | return utils::unvalidatedConvert(canonical); |
| 772 | } |
| 773 | |
| 774 | template <typename Type> |
| 775 | nn::GeneralResult<std::vector<UnvalidatedConvertOutput<Type>>> validatedConvert( |
| 776 | const std::vector<Type>& arguments) { |
| 777 | std::vector<UnvalidatedConvertOutput<Type>> halObject(arguments.size()); |
| 778 | for (size_t i = 0; i < arguments.size(); ++i) { |
| 779 | halObject[i] = NN_TRY(validatedConvert(arguments[i])); |
| 780 | } |
| 781 | return halObject; |
| 782 | } |
| 783 | |
Michael Butler | ab2f482 | 2021-02-08 00:05:07 -0800 | [diff] [blame] | 784 | } // namespace |
| 785 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 786 | nn::GeneralResult<std::vector<uint8_t>> unvalidatedConvert(const nn::CacheToken& cacheToken) { |
| 787 | return std::vector<uint8_t>(cacheToken.begin(), cacheToken.end()); |
| 788 | } |
| 789 | |
| 790 | nn::GeneralResult<BufferDesc> unvalidatedConvert(const nn::BufferDesc& bufferDesc) { |
| 791 | return BufferDesc{.dimensions = NN_TRY(toSigned(bufferDesc.dimensions))}; |
| 792 | } |
| 793 | |
| 794 | nn::GeneralResult<BufferRole> unvalidatedConvert(const nn::BufferRole& bufferRole) { |
| 795 | VERIFY_LE_INT32_MAX(bufferRole.modelIndex) |
| 796 | << "BufferRole: modelIndex must be <= std::numeric_limits<int32_t>::max()"; |
| 797 | VERIFY_LE_INT32_MAX(bufferRole.ioIndex) |
| 798 | << "BufferRole: ioIndex must be <= std::numeric_limits<int32_t>::max()"; |
| 799 | return BufferRole{ |
| 800 | .modelIndex = static_cast<int32_t>(bufferRole.modelIndex), |
| 801 | .ioIndex = static_cast<int32_t>(bufferRole.ioIndex), |
Xusong Wang | 3633d07 | 2021-03-19 13:58:24 -0700 | [diff] [blame] | 802 | .probability = bufferRole.probability, |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 803 | }; |
| 804 | } |
| 805 | |
Michael Butler | 5345563 | 2021-10-25 11:20:33 -0700 | [diff] [blame] | 806 | nn::GeneralResult<DeviceType> unvalidatedConvert(const nn::DeviceType& deviceType) { |
| 807 | switch (deviceType) { |
| 808 | case nn::DeviceType::UNKNOWN: |
| 809 | break; |
| 810 | case nn::DeviceType::OTHER: |
| 811 | case nn::DeviceType::CPU: |
| 812 | case nn::DeviceType::GPU: |
| 813 | case nn::DeviceType::ACCELERATOR: |
| 814 | return static_cast<DeviceType>(deviceType); |
| 815 | } |
| 816 | return NN_ERROR() << "Invalid DeviceType " << deviceType; |
| 817 | } |
| 818 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 819 | nn::GeneralResult<bool> unvalidatedConvert(const nn::MeasureTiming& measureTiming) { |
| 820 | return measureTiming == nn::MeasureTiming::YES; |
| 821 | } |
| 822 | |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 823 | nn::GeneralResult<Memory> unvalidatedConvert(const nn::SharedMemory& memory) { |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 824 | if (memory == nullptr) { |
| 825 | return (NN_ERROR() << "Unable to convert nullptr memory") |
| 826 | . |
| 827 | operator nn::GeneralResult<Memory>(); |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 828 | } |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 829 | return std::visit([](const auto& x) { return unvalidatedConvert(x); }, memory->handle); |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | nn::GeneralResult<ErrorStatus> unvalidatedConvert(const nn::ErrorStatus& errorStatus) { |
| 833 | switch (errorStatus) { |
| 834 | case nn::ErrorStatus::NONE: |
| 835 | case nn::ErrorStatus::DEVICE_UNAVAILABLE: |
| 836 | case nn::ErrorStatus::GENERAL_FAILURE: |
| 837 | case nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE: |
| 838 | case nn::ErrorStatus::INVALID_ARGUMENT: |
| 839 | case nn::ErrorStatus::MISSED_DEADLINE_TRANSIENT: |
| 840 | case nn::ErrorStatus::MISSED_DEADLINE_PERSISTENT: |
| 841 | case nn::ErrorStatus::RESOURCE_EXHAUSTED_TRANSIENT: |
| 842 | case nn::ErrorStatus::RESOURCE_EXHAUSTED_PERSISTENT: |
| 843 | return static_cast<ErrorStatus>(errorStatus); |
| 844 | default: |
| 845 | return ErrorStatus::GENERAL_FAILURE; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | nn::GeneralResult<OutputShape> unvalidatedConvert(const nn::OutputShape& outputShape) { |
| 850 | return OutputShape{.dimensions = NN_TRY(toSigned(outputShape.dimensions)), |
| 851 | .isSufficient = outputShape.isSufficient}; |
| 852 | } |
| 853 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 854 | nn::GeneralResult<ExecutionPreference> unvalidatedConvert( |
| 855 | const nn::ExecutionPreference& executionPreference) { |
| 856 | return static_cast<ExecutionPreference>(executionPreference); |
| 857 | } |
| 858 | |
| 859 | nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 860 | if (operandType == nn::OperandType::OEM || operandType == nn::OperandType::TENSOR_OEM_BYTE) { |
| 861 | return NN_ERROR() << "Unable to convert invalid OperandType " << operandType; |
| 862 | } |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 863 | return static_cast<OperandType>(operandType); |
| 864 | } |
| 865 | |
| 866 | nn::GeneralResult<OperandLifeTime> unvalidatedConvert( |
| 867 | const nn::Operand::LifeTime& operandLifeTime) { |
| 868 | return static_cast<OperandLifeTime>(operandLifeTime); |
| 869 | } |
| 870 | |
| 871 | nn::GeneralResult<DataLocation> unvalidatedConvert(const nn::DataLocation& location) { |
| 872 | VERIFY_LE_INT32_MAX(location.poolIndex) |
| 873 | << "DataLocation: pool index must be <= std::numeric_limits<int32_t>::max()"; |
| 874 | return DataLocation{ |
| 875 | .poolIndex = static_cast<int32_t>(location.poolIndex), |
| 876 | .offset = static_cast<int64_t>(location.offset), |
| 877 | .length = static_cast<int64_t>(location.length), |
| 878 | }; |
| 879 | } |
| 880 | |
| 881 | nn::GeneralResult<std::optional<OperandExtraParams>> unvalidatedConvert( |
| 882 | const nn::Operand::ExtraParams& extraParams) { |
| 883 | return std::visit( |
| 884 | overloaded{ |
| 885 | [](const nn::Operand::NoParams&) |
| 886 | -> nn::GeneralResult<std::optional<OperandExtraParams>> { |
| 887 | return std::nullopt; |
| 888 | }, |
| 889 | [](const nn::Operand::SymmPerChannelQuantParams& symmPerChannelQuantParams) |
| 890 | -> nn::GeneralResult<std::optional<OperandExtraParams>> { |
| 891 | if (symmPerChannelQuantParams.channelDim > |
| 892 | std::numeric_limits<int32_t>::max()) { |
| 893 | // Using explicit type conversion because std::optional in successful |
| 894 | // result confuses the compiler. |
| 895 | return (NN_ERROR() << "symmPerChannelQuantParams.channelDim must be <= " |
| 896 | "std::numeric_limits<int32_t>::max(), received: " |
| 897 | << symmPerChannelQuantParams.channelDim) |
| 898 | . |
| 899 | operator nn::GeneralResult<std::optional<OperandExtraParams>>(); |
| 900 | } |
| 901 | return OperandExtraParams::make<OperandExtraParams::Tag::channelQuant>( |
| 902 | SymmPerChannelQuantParams{ |
| 903 | .scales = symmPerChannelQuantParams.scales, |
| 904 | .channelDim = static_cast<int32_t>( |
| 905 | symmPerChannelQuantParams.channelDim), |
| 906 | }); |
| 907 | }, |
| 908 | [](const nn::Operand::ExtensionParams& extensionParams) |
| 909 | -> nn::GeneralResult<std::optional<OperandExtraParams>> { |
| 910 | return OperandExtraParams::make<OperandExtraParams::Tag::extension>( |
| 911 | extensionParams); |
| 912 | }, |
| 913 | }, |
| 914 | extraParams); |
| 915 | } |
| 916 | |
| 917 | nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand) { |
| 918 | return Operand{ |
| 919 | .type = NN_TRY(unvalidatedConvert(operand.type)), |
| 920 | .dimensions = NN_TRY(toSigned(operand.dimensions)), |
| 921 | .scale = operand.scale, |
| 922 | .zeroPoint = operand.zeroPoint, |
| 923 | .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), |
| 924 | .location = NN_TRY(unvalidatedConvert(operand.location)), |
| 925 | .extraParams = NN_TRY(unvalidatedConvert(operand.extraParams)), |
| 926 | }; |
| 927 | } |
| 928 | |
| 929 | nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 930 | if (operationType == nn::OperationType::OEM_OPERATION) { |
| 931 | return NN_ERROR() << "Unable to convert invalid OperationType OEM_OPERATION"; |
| 932 | } |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 933 | return static_cast<OperationType>(operationType); |
| 934 | } |
| 935 | |
| 936 | nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation) { |
| 937 | return Operation{ |
| 938 | .type = NN_TRY(unvalidatedConvert(operation.type)), |
| 939 | .inputs = NN_TRY(toSigned(operation.inputs)), |
| 940 | .outputs = NN_TRY(toSigned(operation.outputs)), |
| 941 | }; |
| 942 | } |
| 943 | |
| 944 | nn::GeneralResult<Subgraph> unvalidatedConvert(const nn::Model::Subgraph& subgraph) { |
| 945 | return Subgraph{ |
| 946 | .operands = NN_TRY(unvalidatedConvert(subgraph.operands)), |
| 947 | .operations = NN_TRY(unvalidatedConvert(subgraph.operations)), |
| 948 | .inputIndexes = NN_TRY(toSigned(subgraph.inputIndexes)), |
| 949 | .outputIndexes = NN_TRY(toSigned(subgraph.outputIndexes)), |
| 950 | }; |
| 951 | } |
| 952 | |
| 953 | nn::GeneralResult<std::vector<uint8_t>> unvalidatedConvert( |
| 954 | const nn::Model::OperandValues& operandValues) { |
| 955 | return std::vector<uint8_t>(operandValues.data(), operandValues.data() + operandValues.size()); |
| 956 | } |
| 957 | |
| 958 | nn::GeneralResult<ExtensionNameAndPrefix> unvalidatedConvert( |
Miao Wang | b5c8a82 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 959 | const nn::ExtensionNameAndPrefix& extensionNameToPrefix) { |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 960 | return ExtensionNameAndPrefix{ |
| 961 | .name = extensionNameToPrefix.name, |
| 962 | .prefix = extensionNameToPrefix.prefix, |
| 963 | }; |
| 964 | } |
| 965 | |
| 966 | nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model) { |
Michael Butler | 6b0905c | 2022-01-29 13:54:36 -0800 | [diff] [blame] | 967 | if (!hal::utils::hasNoPointerData(model)) { |
| 968 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 969 | << "Model cannot be unvalidatedConverted because it contains pointer-based memory"; |
| 970 | } |
| 971 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 972 | return Model{ |
| 973 | .main = NN_TRY(unvalidatedConvert(model.main)), |
| 974 | .referenced = NN_TRY(unvalidatedConvert(model.referenced)), |
| 975 | .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), |
| 976 | .pools = NN_TRY(unvalidatedConvert(model.pools)), |
| 977 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
| 978 | .extensionNameToPrefix = NN_TRY(unvalidatedConvert(model.extensionNameToPrefix)), |
| 979 | }; |
| 980 | } |
| 981 | |
| 982 | nn::GeneralResult<Priority> unvalidatedConvert(const nn::Priority& priority) { |
| 983 | return static_cast<Priority>(priority); |
| 984 | } |
| 985 | |
| 986 | nn::GeneralResult<Request> unvalidatedConvert(const nn::Request& request) { |
Michael Butler | 6b0905c | 2022-01-29 13:54:36 -0800 | [diff] [blame] | 987 | if (!hal::utils::hasNoPointerData(request)) { |
| 988 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 989 | << "Request cannot be unvalidatedConverted because it contains pointer-based memory"; |
| 990 | } |
| 991 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 992 | return Request{ |
| 993 | .inputs = NN_TRY(unvalidatedConvert(request.inputs)), |
| 994 | .outputs = NN_TRY(unvalidatedConvert(request.outputs)), |
| 995 | .pools = NN_TRY(unvalidatedConvert(request.pools)), |
| 996 | }; |
| 997 | } |
| 998 | |
| 999 | nn::GeneralResult<RequestArgument> unvalidatedConvert( |
| 1000 | const nn::Request::Argument& requestArgument) { |
| 1001 | if (requestArgument.lifetime == nn::Request::Argument::LifeTime::POINTER) { |
| 1002 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 1003 | << "Request cannot be unvalidatedConverted because it contains pointer-based memory"; |
| 1004 | } |
| 1005 | const bool hasNoValue = requestArgument.lifetime == nn::Request::Argument::LifeTime::NO_VALUE; |
| 1006 | return RequestArgument{ |
| 1007 | .hasNoValue = hasNoValue, |
| 1008 | .location = NN_TRY(unvalidatedConvert(requestArgument.location)), |
| 1009 | .dimensions = NN_TRY(toSigned(requestArgument.dimensions)), |
| 1010 | }; |
| 1011 | } |
| 1012 | |
| 1013 | nn::GeneralResult<RequestMemoryPool> unvalidatedConvert(const nn::Request::MemoryPool& memoryPool) { |
| 1014 | return std::visit( |
| 1015 | overloaded{ |
| 1016 | [](const nn::SharedMemory& memory) -> nn::GeneralResult<RequestMemoryPool> { |
| 1017 | return RequestMemoryPool::make<RequestMemoryPool::Tag::pool>( |
| 1018 | NN_TRY(unvalidatedConvert(memory))); |
| 1019 | }, |
| 1020 | [](const nn::Request::MemoryDomainToken& token) |
| 1021 | -> nn::GeneralResult<RequestMemoryPool> { |
| 1022 | return RequestMemoryPool::make<RequestMemoryPool::Tag::token>( |
| 1023 | underlyingType(token)); |
| 1024 | }, |
| 1025 | [](const nn::SharedBuffer& /*buffer*/) { |
| 1026 | return (NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) |
| 1027 | << "Unable to make memory pool from IBuffer") |
| 1028 | . |
| 1029 | operator nn::GeneralResult<RequestMemoryPool>(); |
| 1030 | }, |
| 1031 | }, |
| 1032 | memoryPool); |
| 1033 | } |
| 1034 | |
| 1035 | nn::GeneralResult<Timing> unvalidatedConvert(const nn::Timing& timing) { |
| 1036 | return Timing{ |
Lev Proleev | 8df7d6e | 2021-04-14 20:54:27 +0100 | [diff] [blame] | 1037 | .timeOnDeviceNs = NN_TRY(unvalidatedConvert(timing.timeOnDevice)), |
| 1038 | .timeInDriverNs = NN_TRY(unvalidatedConvert(timing.timeInDriver)), |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 1039 | }; |
| 1040 | } |
| 1041 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 1042 | nn::GeneralResult<int64_t> unvalidatedConvert(const nn::OptionalDuration& optionalDuration) { |
| 1043 | if (!optionalDuration.has_value()) { |
| 1044 | return kNoTiming; |
| 1045 | } |
| 1046 | return unvalidatedConvert(optionalDuration.value()); |
| 1047 | } |
| 1048 | |
| 1049 | nn::GeneralResult<int64_t> unvalidatedConvert(const nn::OptionalTimePoint& optionalTimePoint) { |
| 1050 | if (!optionalTimePoint.has_value()) { |
| 1051 | return kNoTiming; |
| 1052 | } |
| 1053 | return unvalidatedConvert(optionalTimePoint->time_since_epoch()); |
| 1054 | } |
| 1055 | |
| 1056 | nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvert(const nn::SyncFence& syncFence) { |
| 1057 | auto duplicatedFd = NN_TRY(nn::dupFd(syncFence.getFd())); |
| 1058 | return ndk::ScopedFileDescriptor(duplicatedFd.release()); |
| 1059 | } |
| 1060 | |
Michael Butler | e52a77e | 2021-06-07 13:10:58 -0700 | [diff] [blame] | 1061 | nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvert(const nn::SharedHandle& handle) { |
| 1062 | auto duplicatedFd = NN_TRY(nn::dupFd(handle->get())); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 1063 | return ndk::ScopedFileDescriptor(duplicatedFd.release()); |
| 1064 | } |
| 1065 | |
Michael Butler | 5345563 | 2021-10-25 11:20:33 -0700 | [diff] [blame] | 1066 | nn::GeneralResult<Capabilities> unvalidatedConvert(const nn::Capabilities& capabilities) { |
| 1067 | return Capabilities{ |
| 1068 | .relaxedFloat32toFloat16PerformanceTensor = NN_TRY( |
| 1069 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceTensor)), |
| 1070 | .relaxedFloat32toFloat16PerformanceScalar = NN_TRY( |
| 1071 | unvalidatedConvert(capabilities.relaxedFloat32toFloat16PerformanceScalar)), |
| 1072 | .operandPerformance = NN_TRY(unvalidatedConvert(capabilities.operandPerformance)), |
| 1073 | .ifPerformance = NN_TRY(unvalidatedConvert(capabilities.ifPerformance)), |
| 1074 | .whilePerformance = NN_TRY(unvalidatedConvert(capabilities.whilePerformance)), |
| 1075 | }; |
| 1076 | } |
| 1077 | |
| 1078 | nn::GeneralResult<Extension> unvalidatedConvert(const nn::Extension& extension) { |
| 1079 | return Extension{.name = extension.name, |
| 1080 | .operandTypes = NN_TRY(unvalidatedConvert(extension.operandTypes))}; |
| 1081 | } |
Miao Wang | b5c8a82 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 1082 | #ifdef NN_AIDL_V4_OR_ABOVE |
| 1083 | nn::GeneralResult<TokenValuePair> unvalidatedConvert(const nn::TokenValuePair& tokenValuePair) { |
| 1084 | return TokenValuePair{.token = tokenValuePair.token, .value = tokenValuePair.value}; |
| 1085 | } |
| 1086 | #endif // NN_AIDL_V4_OR_ABOVE |
Michael Butler | 5345563 | 2021-10-25 11:20:33 -0700 | [diff] [blame] | 1087 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 1088 | nn::GeneralResult<std::vector<uint8_t>> convert(const nn::CacheToken& cacheToken) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 1089 | return validatedConvert(cacheToken); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | nn::GeneralResult<BufferDesc> convert(const nn::BufferDesc& bufferDesc) { |
| 1093 | return validatedConvert(bufferDesc); |
| 1094 | } |
| 1095 | |
Michael Butler | 5345563 | 2021-10-25 11:20:33 -0700 | [diff] [blame] | 1096 | nn::GeneralResult<DeviceType> convert(const nn::DeviceType& deviceType) { |
| 1097 | return validatedConvert(deviceType); |
| 1098 | } |
| 1099 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 1100 | nn::GeneralResult<bool> convert(const nn::MeasureTiming& measureTiming) { |
| 1101 | return validatedConvert(measureTiming); |
| 1102 | } |
| 1103 | |
Michael Butler | fadeb8a | 2021-02-07 00:11:13 -0800 | [diff] [blame] | 1104 | nn::GeneralResult<Memory> convert(const nn::SharedMemory& memory) { |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 1105 | return validatedConvert(memory); |
| 1106 | } |
| 1107 | |
| 1108 | nn::GeneralResult<ErrorStatus> convert(const nn::ErrorStatus& errorStatus) { |
| 1109 | return validatedConvert(errorStatus); |
| 1110 | } |
| 1111 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 1112 | nn::GeneralResult<ExecutionPreference> convert(const nn::ExecutionPreference& executionPreference) { |
| 1113 | return validatedConvert(executionPreference); |
| 1114 | } |
| 1115 | |
| 1116 | nn::GeneralResult<Model> convert(const nn::Model& model) { |
| 1117 | return validatedConvert(model); |
| 1118 | } |
| 1119 | |
| 1120 | nn::GeneralResult<Priority> convert(const nn::Priority& priority) { |
| 1121 | return validatedConvert(priority); |
| 1122 | } |
| 1123 | |
| 1124 | nn::GeneralResult<Request> convert(const nn::Request& request) { |
| 1125 | return validatedConvert(request); |
| 1126 | } |
| 1127 | |
| 1128 | nn::GeneralResult<Timing> convert(const nn::Timing& timing) { |
| 1129 | return validatedConvert(timing); |
| 1130 | } |
| 1131 | |
| 1132 | nn::GeneralResult<int64_t> convert(const nn::OptionalDuration& optionalDuration) { |
| 1133 | return validatedConvert(optionalDuration); |
| 1134 | } |
| 1135 | |
| 1136 | nn::GeneralResult<int64_t> convert(const nn::OptionalTimePoint& outputShapes) { |
| 1137 | return validatedConvert(outputShapes); |
| 1138 | } |
| 1139 | |
Michael Butler | 5345563 | 2021-10-25 11:20:33 -0700 | [diff] [blame] | 1140 | nn::GeneralResult<Capabilities> convert(const nn::Capabilities& capabilities) { |
| 1141 | return validatedConvert(capabilities); |
| 1142 | } |
| 1143 | |
| 1144 | nn::GeneralResult<Extension> convert(const nn::Extension& extension) { |
| 1145 | return validatedConvert(extension); |
| 1146 | } |
| 1147 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 1148 | nn::GeneralResult<std::vector<BufferRole>> convert(const std::vector<nn::BufferRole>& bufferRoles) { |
| 1149 | return validatedConvert(bufferRoles); |
| 1150 | } |
| 1151 | |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 1152 | nn::GeneralResult<std::vector<OutputShape>> convert( |
| 1153 | const std::vector<nn::OutputShape>& outputShapes) { |
| 1154 | return validatedConvert(outputShapes); |
| 1155 | } |
| 1156 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 1157 | nn::GeneralResult<std::vector<ndk::ScopedFileDescriptor>> convert( |
| 1158 | const std::vector<nn::SharedHandle>& cacheHandles) { |
Michael Butler | e52a77e | 2021-06-07 13:10:58 -0700 | [diff] [blame] | 1159 | return validatedConvert(cacheHandles); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | nn::GeneralResult<std::vector<ndk::ScopedFileDescriptor>> convert( |
| 1163 | const std::vector<nn::SyncFence>& syncFences) { |
Michael Butler | 388bceb | 2021-02-03 15:15:43 -0800 | [diff] [blame] | 1164 | return validatedConvert(syncFences); |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 1165 | } |
Miao Wang | b5c8a82 | 2021-10-26 20:03:05 +0000 | [diff] [blame] | 1166 | nn::GeneralResult<std::vector<ExtensionNameAndPrefix>> convert( |
| 1167 | const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) { |
| 1168 | return unvalidatedConvert(extensionNameToPrefix); |
| 1169 | } |
| 1170 | |
| 1171 | #ifdef NN_AIDL_V4_OR_ABOVE |
| 1172 | nn::GeneralResult<std::vector<TokenValuePair>> convert( |
| 1173 | const std::vector<nn::TokenValuePair>& metaData) { |
| 1174 | return validatedConvert(metaData); |
| 1175 | } |
| 1176 | #endif // NN_AIDL_V4_OR_ABOVE |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 1177 | |
Michael Butler | 5345563 | 2021-10-25 11:20:33 -0700 | [diff] [blame] | 1178 | nn::GeneralResult<std::vector<Extension>> convert(const std::vector<nn::Extension>& extensions) { |
| 1179 | return validatedConvert(extensions); |
| 1180 | } |
| 1181 | |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 1182 | nn::GeneralResult<std::vector<int32_t>> toSigned(const std::vector<uint32_t>& vec) { |
| 1183 | if (!std::all_of(vec.begin(), vec.end(), |
| 1184 | [](uint32_t v) { return v <= std::numeric_limits<int32_t>::max(); })) { |
| 1185 | return NN_ERROR() << "Vector contains a value that doesn't fit into int32_t."; |
| 1186 | } |
| 1187 | return std::vector<int32_t>(vec.begin(), vec.end()); |
| 1188 | } |
| 1189 | |
Xusong Wang | 8805b2d | 2022-01-21 10:15:19 -0800 | [diff] [blame] | 1190 | std::vector<uint8_t> toVec(const std::array<uint8_t, IDevice::BYTE_SIZE_OF_CACHE_TOKEN>& token) { |
| 1191 | return std::vector<uint8_t>(token.begin(), token.end()); |
| 1192 | } |
| 1193 | |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 1194 | } // namespace aidl::android::hardware::neuralnetworks::utils |