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