From 900c28a250297964cf9aa65653b9adce4e6db27a Mon Sep 17 00:00:00 2001 From: Lev Proleev Date: Tue, 26 Jan 2021 19:40:20 +0000 Subject: [PATCH] Add canonical types adapters for NNAPI AIDL interface Also: * Add missing AIDL<->CT conversions * Add AIDL-specific info to neuralnetworks/utils/README.md * Add mock classes and tests AIDL adapters Bug: 179015258 Test: neuralnetworks_utils_hal_test Change-Id: Ifa98fadd46dca5dbf9b3ceb4da811aa8da45b6e4 Merged-In: Ifa98fadd46dca5dbf9b3ceb4da811aa8da45b6e4 (cherry picked from commit 3b93b0b56a4f5128eaa942d804dd490317c0abcb) --- neuralnetworks/TEST_MAPPING | 3 + neuralnetworks/aidl/utils/Android.bp | 37 + .../utils/include/nnapi/hal/aidl/Buffer.h | 56 ++ .../utils/include/nnapi/hal/aidl/Callbacks.h | 53 ++ .../include/nnapi/hal/aidl/Conversions.h | 58 ++ .../utils/include/nnapi/hal/aidl/Device.h | 98 ++ .../include/nnapi/hal/aidl/PreparedModel.h | 70 ++ .../include/nnapi/hal/aidl/ProtectCallback.h | 81 ++ .../utils/include/nnapi/hal/aidl/Service.h | 33 + .../aidl/utils/include/nnapi/hal/aidl/Utils.h | 7 + neuralnetworks/aidl/utils/src/Buffer.cpp | 78 ++ neuralnetworks/aidl/utils/src/Callbacks.cpp | 61 ++ neuralnetworks/aidl/utils/src/Conversions.cpp | 419 ++++++++- neuralnetworks/aidl/utils/src/Device.cpp | 294 ++++++ .../aidl/utils/src/PreparedModel.cpp | 172 ++++ .../aidl/utils/src/ProtectCallback.cpp | 112 +++ neuralnetworks/aidl/utils/src/Service.cpp | 50 + neuralnetworks/aidl/utils/src/Utils.cpp | 31 +- neuralnetworks/aidl/utils/test/BufferTest.cpp | 212 +++++ neuralnetworks/aidl/utils/test/DeviceTest.cpp | 861 ++++++++++++++++++ neuralnetworks/aidl/utils/test/MockBuffer.h | 43 + neuralnetworks/aidl/utils/test/MockDevice.h | 67 ++ .../utils/test/MockFencedExecutionCallback.h | 45 + .../aidl/utils/test/MockPreparedModel.h | 50 + .../aidl/utils/test/PreparedModelTest.cpp | 272 ++++++ neuralnetworks/utils/README.md | 33 +- neuralnetworks/utils/common/Android.bp | 2 + .../common/include/nnapi/hal/CommonUtils.h | 2 + 28 files changed, 3257 insertions(+), 43 deletions(-) create mode 100644 neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Buffer.h create mode 100644 neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Callbacks.h create mode 100644 neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Device.h create mode 100644 neuralnetworks/aidl/utils/include/nnapi/hal/aidl/PreparedModel.h create mode 100644 neuralnetworks/aidl/utils/include/nnapi/hal/aidl/ProtectCallback.h create mode 100644 neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Service.h create mode 100644 neuralnetworks/aidl/utils/src/Buffer.cpp create mode 100644 neuralnetworks/aidl/utils/src/Callbacks.cpp create mode 100644 neuralnetworks/aidl/utils/src/Device.cpp create mode 100644 neuralnetworks/aidl/utils/src/PreparedModel.cpp create mode 100644 neuralnetworks/aidl/utils/src/ProtectCallback.cpp create mode 100644 neuralnetworks/aidl/utils/src/Service.cpp create mode 100644 neuralnetworks/aidl/utils/test/BufferTest.cpp create mode 100644 neuralnetworks/aidl/utils/test/DeviceTest.cpp create mode 100644 neuralnetworks/aidl/utils/test/MockBuffer.h create mode 100644 neuralnetworks/aidl/utils/test/MockDevice.h create mode 100644 neuralnetworks/aidl/utils/test/MockFencedExecutionCallback.h create mode 100644 neuralnetworks/aidl/utils/test/MockPreparedModel.h create mode 100644 neuralnetworks/aidl/utils/test/PreparedModelTest.cpp diff --git a/neuralnetworks/TEST_MAPPING b/neuralnetworks/TEST_MAPPING index 5d168d2766..d29682887a 100644 --- a/neuralnetworks/TEST_MAPPING +++ b/neuralnetworks/TEST_MAPPING @@ -15,6 +15,9 @@ { "name": "neuralnetworks_utils_hal_1_3_test" }, + { + "name": "neuralnetworks_utils_hal_aidl_test" + }, { "name": "VtsHalNeuralnetworksV1_0TargetTest", "options": [ diff --git a/neuralnetworks/aidl/utils/Android.bp b/neuralnetworks/aidl/utils/Android.bp index 2673caef0e..476dac9d4d 100644 --- a/neuralnetworks/aidl/utils/Android.bp +++ b/neuralnetworks/aidl/utils/Android.bp @@ -29,10 +29,12 @@ cc_library_static { srcs: ["src/*"], local_include_dirs: ["include/nnapi/hal/aidl/"], export_include_dirs: ["include"], + cflags: ["-Wthread-safety"], static_libs: [ "libarect", "neuralnetworks_types", "neuralnetworks_utils_hal_common", + "neuralnetworks_utils_hal_1_0", ], shared_libs: [ "android.hardware.neuralnetworks-V1-ndk_platform", @@ -41,3 +43,38 @@ cc_library_static { "libnativewindow", ], } + +cc_test { + name: "neuralnetworks_utils_hal_aidl_test", + defaults: ["neuralnetworks_utils_defaults"], + srcs: [ + "test/*.cpp", + ], + static_libs: [ + "android.hardware.common-V2-ndk_platform", + "android.hardware.neuralnetworks-V1-ndk_platform", + "libgmock", + "libneuralnetworks_common", + "neuralnetworks_types", + "neuralnetworks_utils_hal_aidl", + "neuralnetworks_utils_hal_common", + ], + shared_libs: [ + "android.hidl.allocator@1.0", + "libbase", + "libbinder_ndk", + "libcutils", + "libhidlbase", + "libhidlmemory", + "liblog", + "libnativewindow", + "libutils", + ], + cflags: [ + /* GMOCK defines functions for printing all MOCK_DEVICE arguments and + * MockDevice contains a string pointer which triggers a warning in the + * base logging library. */ + "-Wno-user-defined-warnings", + ], + test_suites: ["general-tests"], +} diff --git a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Buffer.h b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Buffer.h new file mode 100644 index 0000000000..46190c4078 --- /dev/null +++ b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Buffer.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_BUFFER_H +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_BUFFER_H + +#include +#include +#include +#include +#include +#include + +// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface +// lifetimes across processes. + +namespace aidl::android::hardware::neuralnetworks::utils { + +// Class that adapts aidl_hal::IBuffer to nn::IBuffer. +class Buffer final : public nn::IBuffer { + struct PrivateConstructorTag {}; + + public: + static nn::GeneralResult> create( + std::shared_ptr buffer, nn::Request::MemoryDomainToken token); + + Buffer(PrivateConstructorTag tag, std::shared_ptr buffer, + nn::Request::MemoryDomainToken token); + + nn::Request::MemoryDomainToken getToken() const override; + + nn::GeneralResult copyTo(const nn::SharedMemory& dst) const override; + nn::GeneralResult copyFrom(const nn::SharedMemory& src, + const nn::Dimensions& dimensions) const override; + + private: + const std::shared_ptr kBuffer; + const nn::Request::MemoryDomainToken kToken; +}; + +} // namespace aidl::android::hardware::neuralnetworks::utils + +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_BUFFER_H diff --git a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Callbacks.h b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Callbacks.h new file mode 100644 index 0000000000..8651912cb8 --- /dev/null +++ b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Callbacks.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_CALLBACKS_H +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_CALLBACKS_H + +#include +#include +#include +#include +#include +#include +#include +#include + +// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface +// lifetimes across processes and for protecting asynchronous calls across AIDL. + +namespace aidl::android::hardware::neuralnetworks::utils { + +// An AIDL callback class to receive the results of IDevice::prepareModel* asynchronously. +class PreparedModelCallback final : public BnPreparedModelCallback, + public hal::utils::IProtectedCallback { + public: + using Data = nn::GeneralResult; + + ndk::ScopedAStatus notify(ErrorStatus status, + const std::shared_ptr& preparedModel) override; + + void notifyAsDeadObject() override; + + Data get(); + + private: + hal::utils::TransferValue mData; +}; + +} // namespace aidl::android::hardware::neuralnetworks::utils + +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_CALLBACKS_H diff --git a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Conversions.h b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Conversions.h index 1b2f69c245..4922a6ea1e 100644 --- a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Conversions.h +++ b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Conversions.h @@ -46,6 +46,7 @@ #include #include +#include #include #include #include @@ -96,7 +97,11 @@ GeneralResult unvalidatedConvert( const aidl_hal::ExtensionOperandTypeInformation& operandTypeInformation); GeneralResult unvalidatedConvert( const ::aidl::android::hardware::common::NativeHandle& handle); +GeneralResult unvalidatedConvert(const ndk::ScopedFileDescriptor& syncFence); +GeneralResult convert(const aidl_hal::Capabilities& capabilities); +GeneralResult convert(const aidl_hal::DeviceType& deviceType); +GeneralResult convert(const aidl_hal::ErrorStatus& errorStatus); GeneralResult convert( const aidl_hal::ExecutionPreference& executionPreference); GeneralResult convert(const aidl_hal::Memory& memory); @@ -106,9 +111,14 @@ GeneralResult convert(const aidl_hal::OperandType& operandType); GeneralResult convert(const aidl_hal::Priority& priority); GeneralResult convert(const aidl_hal::RequestMemoryPool& memoryPool); GeneralResult convert(const aidl_hal::Request& request); +GeneralResult convert(const aidl_hal::Timing& timing); +GeneralResult convert(const ndk::ScopedFileDescriptor& syncFence); +GeneralResult> convert(const std::vector& extension); GeneralResult> convert(const std::vector& outputShapes); GeneralResult> convert(const std::vector& memories); +GeneralResult> convert( + const std::vector& outputShapes); GeneralResult> toUnsigned(const std::vector& vec); @@ -118,14 +128,62 @@ namespace aidl::android::hardware::neuralnetworks::utils { namespace nn = ::android::nn; +nn::GeneralResult> unvalidatedConvert(const nn::CacheToken& cacheToken); +nn::GeneralResult unvalidatedConvert(const nn::BufferDesc& bufferDesc); +nn::GeneralResult unvalidatedConvert(const nn::BufferRole& bufferRole); +nn::GeneralResult unvalidatedConvert(const nn::MeasureTiming& measureTiming); nn::GeneralResult unvalidatedConvert(const nn::SharedMemory& memory); nn::GeneralResult unvalidatedConvert(const nn::OutputShape& outputShape); nn::GeneralResult unvalidatedConvert(const nn::ErrorStatus& errorStatus); +nn::GeneralResult unvalidatedConvert( + const nn::ExecutionPreference& executionPreference); +nn::GeneralResult unvalidatedConvert(const nn::OperandType& operandType); +nn::GeneralResult unvalidatedConvert(const nn::Operand::LifeTime& operandLifeTime); +nn::GeneralResult unvalidatedConvert(const nn::DataLocation& location); +nn::GeneralResult> unvalidatedConvert( + const nn::Operand::ExtraParams& extraParams); +nn::GeneralResult unvalidatedConvert(const nn::Operand& operand); +nn::GeneralResult unvalidatedConvert(const nn::OperationType& operationType); +nn::GeneralResult unvalidatedConvert(const nn::Operation& operation); +nn::GeneralResult unvalidatedConvert(const nn::Model::Subgraph& subgraph); +nn::GeneralResult> unvalidatedConvert( + const nn::Model::OperandValues& operandValues); +nn::GeneralResult unvalidatedConvert( + const nn::Model::ExtensionNameAndPrefix& extensionNameToPrefix); +nn::GeneralResult unvalidatedConvert(const nn::Model& model); +nn::GeneralResult unvalidatedConvert(const nn::Priority& priority); +nn::GeneralResult unvalidatedConvert(const nn::Request& request); +nn::GeneralResult unvalidatedConvert(const nn::Request::Argument& requestArgument); +nn::GeneralResult unvalidatedConvert(const nn::Request::MemoryPool& memoryPool); +nn::GeneralResult unvalidatedConvert(const nn::Timing& timing); +nn::GeneralResult unvalidatedConvert(const nn::Duration& duration); +nn::GeneralResult unvalidatedConvert(const nn::OptionalDuration& optionalDuration); +nn::GeneralResult unvalidatedConvert(const nn::OptionalTimePoint& optionalTimePoint); +nn::GeneralResult unvalidatedConvert(const nn::SyncFence& syncFence); +nn::GeneralResult unvalidatedConvert(const nn::SharedHandle& sharedHandle); +nn::GeneralResult unvalidatedConvertCache( + const nn::SharedHandle& handle); +nn::GeneralResult> convert(const nn::CacheToken& cacheToken); +nn::GeneralResult convert(const nn::BufferDesc& bufferDesc); +nn::GeneralResult convert(const nn::MeasureTiming& measureTiming); nn::GeneralResult convert(const nn::SharedMemory& memory); nn::GeneralResult convert(const nn::ErrorStatus& errorStatus); +nn::GeneralResult convert(const nn::ExecutionPreference& executionPreference); +nn::GeneralResult convert(const nn::Model& model); +nn::GeneralResult convert(const nn::Priority& priority); +nn::GeneralResult convert(const nn::Request& request); +nn::GeneralResult convert(const nn::Timing& timing); +nn::GeneralResult convert(const nn::OptionalDuration& optionalDuration); +nn::GeneralResult convert(const nn::OptionalTimePoint& optionalTimePoint); + +nn::GeneralResult> convert(const std::vector& bufferRoles); nn::GeneralResult> convert( const std::vector& outputShapes); +nn::GeneralResult> convert( + const std::vector& handles); +nn::GeneralResult> convert( + const std::vector& syncFences); nn::GeneralResult> toSigned(const std::vector& vec); diff --git a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Device.h b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Device.h new file mode 100644 index 0000000000..eb194e3549 --- /dev/null +++ b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Device.h @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_DEVICE_H +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_DEVICE_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface +// lifetimes across processes and for protecting asynchronous calls across AIDL. + +namespace aidl::android::hardware::neuralnetworks::utils { + +// Class that adapts aidl_hal::IDevice to nn::IDevice. +class Device final : public nn::IDevice { + struct PrivateConstructorTag {}; + + public: + static nn::GeneralResult> create( + std::string name, std::shared_ptr device); + + Device(PrivateConstructorTag tag, std::string name, std::string versionString, + nn::DeviceType deviceType, std::vector extensions, + nn::Capabilities capabilities, std::pair numberOfCacheFilesNeeded, + std::shared_ptr device, DeathHandler deathHandler); + + const std::string& getName() const override; + const std::string& getVersionString() const override; + nn::Version getFeatureLevel() const override; + nn::DeviceType getType() const override; + bool isUpdatable() const override; + const std::vector& getSupportedExtensions() const override; + const nn::Capabilities& getCapabilities() const override; + std::pair getNumberOfCacheFilesNeeded() const override; + + nn::GeneralResult wait() const override; + + nn::GeneralResult> getSupportedOperations( + const nn::Model& model) const override; + + nn::GeneralResult prepareModel( + const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority, + nn::OptionalTimePoint deadline, const std::vector& modelCache, + const std::vector& dataCache, + const nn::CacheToken& token) const override; + + nn::GeneralResult prepareModelFromCache( + nn::OptionalTimePoint deadline, const std::vector& modelCache, + const std::vector& dataCache, + const nn::CacheToken& token) const override; + + nn::GeneralResult allocate( + const nn::BufferDesc& desc, const std::vector& preparedModels, + const std::vector& inputRoles, + const std::vector& outputRoles) const override; + + DeathMonitor* getDeathMonitor() const; + + private: + const std::string kName; + const std::string kVersionString; + const nn::DeviceType kDeviceType; + const std::vector kExtensions; + const nn::Capabilities kCapabilities; + const std::pair kNumberOfCacheFilesNeeded; + const std::shared_ptr kDevice; + const DeathHandler kDeathHandler; +}; + +} // namespace aidl::android::hardware::neuralnetworks::utils + +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_DEVICE_H diff --git a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/PreparedModel.h b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/PreparedModel.h new file mode 100644 index 0000000000..9b28588610 --- /dev/null +++ b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/PreparedModel.h @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PREPARED_MODEL_H +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PREPARED_MODEL_H + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface +// lifetimes across processes and for protecting asynchronous calls across AIDL. + +namespace aidl::android::hardware::neuralnetworks::utils { + +// Class that adapts aidl_hal::IPreparedModel to nn::IPreparedModel. +class PreparedModel final : public nn::IPreparedModel, + public std::enable_shared_from_this { + struct PrivateConstructorTag {}; + + public: + static nn::GeneralResult> create( + std::shared_ptr preparedModel); + + PreparedModel(PrivateConstructorTag tag, + std::shared_ptr preparedModel); + + nn::ExecutionResult, nn::Timing>> execute( + const nn::Request& request, nn::MeasureTiming measure, + const nn::OptionalTimePoint& deadline, + const nn::OptionalDuration& loopTimeoutDuration) const override; + + nn::GeneralResult> executeFenced( + const nn::Request& request, const std::vector& waitFor, + nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline, + const nn::OptionalDuration& loopTimeoutDuration, + const nn::OptionalDuration& timeoutDurationAfterFence) const override; + + nn::GeneralResult configureExecutionBurst() const override; + + std::any getUnderlyingResource() const override; + + private: + const std::shared_ptr kPreparedModel; +}; + +} // namespace aidl::android::hardware::neuralnetworks::utils + +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PREPARED_MODEL_H diff --git a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/ProtectCallback.h b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/ProtectCallback.h new file mode 100644 index 0000000000..ab1108c182 --- /dev/null +++ b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/ProtectCallback.h @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PROTECT_CALLBACK_H +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PROTECT_CALLBACK_H + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface +// lifetimes across processes and for protecting asynchronous calls across AIDL. + +namespace aidl::android::hardware::neuralnetworks::utils { + +// Thread safe class +class DeathMonitor final { + public: + static void serviceDied(void* cookie); + void serviceDied(); + // Precondition: `killable` must be non-null. + void add(hal::utils::IProtectedCallback* killable) const; + // Precondition: `killable` must be non-null. + void remove(hal::utils::IProtectedCallback* killable) const; + + private: + mutable std::mutex mMutex; + mutable std::vector mObjects GUARDED_BY(mMutex); +}; + +class DeathHandler final { + public: + static nn::GeneralResult create(std::shared_ptr object); + + DeathHandler(const DeathHandler&) = delete; + DeathHandler(DeathHandler&&) noexcept = default; + DeathHandler& operator=(const DeathHandler&) = delete; + DeathHandler& operator=(DeathHandler&&) noexcept = delete; + ~DeathHandler(); + + using Cleanup = std::function; + // Precondition: `killable` must be non-null. + [[nodiscard]] ::android::base::ScopeGuard protectCallback( + hal::utils::IProtectedCallback* killable) const; + + std::shared_ptr getDeathMonitor() const { return kDeathMonitor; } + + private: + DeathHandler(std::shared_ptr object, + ndk::ScopedAIBinder_DeathRecipient deathRecipient, + std::shared_ptr deathMonitor); + + std::shared_ptr kObject; + ndk::ScopedAIBinder_DeathRecipient kDeathRecipient; + std::shared_ptr kDeathMonitor; +}; + +} // namespace aidl::android::hardware::neuralnetworks::utils + +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PROTECT_CALLBACK_H diff --git a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Service.h b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Service.h new file mode 100644 index 0000000000..b4587acbf7 --- /dev/null +++ b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Service.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_SERVICE_H +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_SERVICE_H + +#include +#include +#include +#include + +#include + +namespace aidl::android::hardware::neuralnetworks::utils { + +nn::GeneralResult getDevice(const std::string& name); + +} // namespace aidl::android::hardware::neuralnetworks::utils + +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_SERVICE_H diff --git a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Utils.h b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Utils.h index 79b511dc56..58dcfe3af6 100644 --- a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Utils.h +++ b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Utils.h @@ -23,6 +23,7 @@ #include #include #include +#include namespace aidl::android::hardware::neuralnetworks::utils { @@ -52,6 +53,12 @@ nn::GeneralResult clone(const Request& request); nn::GeneralResult clone(const RequestMemoryPool& requestPool); nn::GeneralResult clone(const Model& model); +nn::GeneralResult handleTransportError(const ndk::ScopedAStatus& ret); + +#define HANDLE_ASTATUS(ret) \ + for (const auto status = handleTransportError(ret); !status.ok();) \ + return NN_ERROR(status.error().code) << status.error().message << ": " + } // namespace aidl::android::hardware::neuralnetworks::utils #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_H diff --git a/neuralnetworks/aidl/utils/src/Buffer.cpp b/neuralnetworks/aidl/utils/src/Buffer.cpp new file mode 100644 index 0000000000..c729a688d1 --- /dev/null +++ b/neuralnetworks/aidl/utils/src/Buffer.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Buffer.h" + +#include +#include +#include + +#include "Conversions.h" +#include "Utils.h" +#include "nnapi/hal/aidl/Conversions.h" + +#include +#include + +// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface +// lifetimes across processes. + +namespace aidl::android::hardware::neuralnetworks::utils { + +nn::GeneralResult> Buffer::create( + std::shared_ptr buffer, nn::Request::MemoryDomainToken token) { + if (buffer == nullptr) { + return NN_ERROR() << "aidl_hal::utils::Buffer::create must have non-null buffer"; + } + if (token == static_cast(0)) { + return NN_ERROR() << "aidl_hal::utils::Buffer::create must have non-zero token"; + } + + return std::make_shared(PrivateConstructorTag{}, std::move(buffer), token); +} + +Buffer::Buffer(PrivateConstructorTag /*tag*/, std::shared_ptr buffer, + nn::Request::MemoryDomainToken token) + : kBuffer(std::move(buffer)), kToken(token) { + CHECK(kBuffer != nullptr); + CHECK(kToken != static_cast(0)); +} + +nn::Request::MemoryDomainToken Buffer::getToken() const { + return kToken; +} + +nn::GeneralResult Buffer::copyTo(const nn::SharedMemory& dst) const { + const auto aidlDst = NN_TRY(convert(dst)); + + const auto ret = kBuffer->copyTo(aidlDst); + HANDLE_ASTATUS(ret) << "IBuffer::copyTo failed"; + + return {}; +} + +nn::GeneralResult Buffer::copyFrom(const nn::SharedMemory& src, + const nn::Dimensions& dimensions) const { + const auto aidlSrc = NN_TRY(convert(src)); + const auto aidlDimensions = NN_TRY(toSigned(dimensions)); + + const auto ret = kBuffer->copyFrom(aidlSrc, aidlDimensions); + HANDLE_ASTATUS(ret) << "IBuffer::copyFrom failed"; + + return {}; +} + +} // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/src/Callbacks.cpp b/neuralnetworks/aidl/utils/src/Callbacks.cpp new file mode 100644 index 0000000000..8055665228 --- /dev/null +++ b/neuralnetworks/aidl/utils/src/Callbacks.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Callbacks.h" + +#include "Conversions.h" +#include "PreparedModel.h" +#include "ProtectCallback.h" +#include "Utils.h" + +#include +#include +#include + +#include + +// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface +// lifetimes across processes and for protecting asynchronous calls across AIDL. + +namespace aidl::android::hardware::neuralnetworks::utils { +namespace { + +// Converts the results of IDevice::prepareModel* to the NN canonical format. On success, this +// function returns with a non-null nn::SharedPreparedModel with a feature level of +// nn::Version::ANDROID_S. On failure, this function returns with the appropriate nn::GeneralError. +nn::GeneralResult prepareModelCallback( + ErrorStatus status, const std::shared_ptr& preparedModel) { + HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status); + return NN_TRY(PreparedModel::create(preparedModel)); +} + +} // namespace + +ndk::ScopedAStatus PreparedModelCallback::notify( + ErrorStatus status, const std::shared_ptr& preparedModel) { + mData.put(prepareModelCallback(status, preparedModel)); + return ndk::ScopedAStatus::ok(); +} + +void PreparedModelCallback::notifyAsDeadObject() { + mData.put(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object"); +} + +PreparedModelCallback::Data PreparedModelCallback::get() { + return mData.take(); +} + +} // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/src/Conversions.cpp b/neuralnetworks/aidl/utils/src/Conversions.cpp index db3504bb74..5d9c55b1ba 100644 --- a/neuralnetworks/aidl/utils/src/Conversions.cpp +++ b/neuralnetworks/aidl/utils/src/Conversions.cpp @@ -18,6 +18,8 @@ #include #include +#include +#include #include #include #include @@ -42,14 +44,17 @@ #define VERIFY_NON_NEGATIVE(value) \ while (UNLIKELY(value < 0)) return NN_ERROR() -namespace { +#define VERIFY_LE_INT32_MAX(value) \ + while (UNLIKELY(value > std::numeric_limits::max())) return NN_ERROR() +namespace { template constexpr std::underlying_type_t underlyingType(Type value) { return static_cast>(value); } constexpr auto kVersion = android::nn::Version::ANDROID_S; +constexpr int64_t kNoTiming = -1; } // namespace @@ -134,13 +139,8 @@ GeneralResult unvalidatedConvertHelper(const NativeHandle& aidlNativeHan std::vector fds; fds.reserve(aidlNativeHandle.fds.size()); for (const auto& fd : aidlNativeHandle.fds) { - const int dupFd = dup(fd.get()); - if (dupFd == -1) { - // TODO(b/120417090): is ANEURALNETWORKS_UNEXPECTED_NULL the correct error to return - // here? - return NN_ERROR() << "Failed to dup the fd"; - } - fds.emplace_back(dupFd); + auto duplicatedFd = NN_TRY(dupFd(fd.get())); + fds.emplace_back(duplicatedFd.release()); } return Handle{.fds = std::move(fds), .ints = aidlNativeHandle.ints}; @@ -157,16 +157,12 @@ struct NativeHandleDeleter { using UniqueNativeHandle = std::unique_ptr; -static nn::GeneralResult nativeHandleFromAidlHandle( - const NativeHandle& handle) { +static GeneralResult nativeHandleFromAidlHandle(const NativeHandle& handle) { std::vector fds; fds.reserve(handle.fds.size()); for (const auto& fd : handle.fds) { - const int dupFd = dup(fd.get()); - if (dupFd == -1) { - return NN_ERROR() << "Failed to dup the fd"; - } - fds.emplace_back(dupFd); + auto duplicatedFd = NN_TRY(dupFd(fd.get())); + fds.emplace_back(duplicatedFd.release()); } constexpr size_t kIntMax = std::numeric_limits::max(); @@ -382,14 +378,14 @@ static uint32_t roundUpToMultiple(uint32_t value, uint32_t multiple) { GeneralResult unvalidatedConvert(const aidl_hal::Memory& memory) { VERIFY_NON_NEGATIVE(memory.size) << "Memory size must not be negative"; - if (memory.size > std::numeric_limits::max()) { + if (memory.size > std::numeric_limits::max()) { return NN_ERROR() << "Memory: size must be <= std::numeric_limits::max()"; } if (memory.name != "hardware_buffer_blob") { return std::make_shared(Memory{ .handle = NN_TRY(unvalidatedConvertHelper(memory.handle)), - .size = static_cast(memory.size), + .size = static_cast(memory.size), .name = memory.name, }); } @@ -434,11 +430,28 @@ GeneralResult unvalidatedConvert(const aidl_hal::Memory& memory) { return std::make_shared(Memory{ .handle = HardwareBufferHandle(hardwareBuffer, /*takeOwnership=*/true), - .size = static_cast(memory.size), + .size = static_cast(memory.size), .name = memory.name, }); } +GeneralResult unvalidatedConvert(const aidl_hal::Timing& timing) { + if (timing.timeInDriver < -1) { + return NN_ERROR() << "Timing: timeInDriver must not be less than -1"; + } + if (timing.timeOnDevice < -1) { + return NN_ERROR() << "Timing: timeOnDevice must not be less than -1"; + } + constexpr auto convertTiming = [](int64_t halTiming) -> OptionalDuration { + if (halTiming == kNoTiming) { + return {}; + } + return nn::Duration(static_cast(halTiming)); + }; + return Timing{.timeOnDevice = convertTiming(timing.timeOnDevice), + .timeInDriver = convertTiming(timing.timeInDriver)}; +} + GeneralResult unvalidatedConvert(const std::vector& operandValues) { return Model::OperandValues(operandValues.data(), operandValues.size()); } @@ -515,6 +528,23 @@ GeneralResult unvalidatedConvert(const NativeHandle& aidlNativeHan return std::make_shared(NN_TRY(unvalidatedConvertHelper(aidlNativeHandle))); } +GeneralResult unvalidatedConvert(const ndk::ScopedFileDescriptor& syncFence) { + auto duplicatedFd = NN_TRY(dupFd(syncFence.get())); + return SyncFence::create(std::move(duplicatedFd)); +} + +GeneralResult convert(const aidl_hal::Capabilities& capabilities) { + return validatedConvert(capabilities); +} + +GeneralResult convert(const aidl_hal::DeviceType& deviceType) { + return validatedConvert(deviceType); +} + +GeneralResult convert(const aidl_hal::ErrorStatus& errorStatus) { + return validatedConvert(errorStatus); +} + GeneralResult convert( const aidl_hal::ExecutionPreference& executionPreference) { return validatedConvert(executionPreference); @@ -548,6 +578,18 @@ GeneralResult convert(const aidl_hal::Request& request) { return validatedConvert(request); } +GeneralResult convert(const aidl_hal::Timing& timing) { + return validatedConvert(timing); +} + +GeneralResult convert(const ndk::ScopedFileDescriptor& syncFence) { + return unvalidatedConvert(syncFence); +} + +GeneralResult> convert(const std::vector& extension) { + return validatedConvert(extension); +} + GeneralResult> convert(const std::vector& operations) { return unvalidatedConvert(operations); } @@ -556,6 +598,11 @@ GeneralResult> convert(const std::vector> convert( + const std::vector& outputShapes) { + return validatedConvert(outputShapes); +} + GeneralResult> toUnsigned(const std::vector& vec) { if (!std::all_of(vec.begin(), vec.end(), [](int32_t v) { return v >= 0; })) { return NN_ERROR() << "Negative value passed to conversion from signed to unsigned"; @@ -575,13 +622,20 @@ using UnvalidatedConvertOutput = template nn::GeneralResult>> unvalidatedConvertVec( const std::vector& arguments) { - std::vector> halObject(arguments.size()); - for (size_t i = 0; i < arguments.size(); ++i) { - halObject[i] = NN_TRY(unvalidatedConvert(arguments[i])); + std::vector> halObject; + halObject.reserve(arguments.size()); + for (const auto& argument : arguments) { + halObject.push_back(NN_TRY(unvalidatedConvert(argument))); } return halObject; } +template +nn::GeneralResult>> unvalidatedConvert( + const std::vector& arguments) { + return unvalidatedConvertVec(arguments); +} + template nn::GeneralResult> validatedConvert(const Type& canonical) { const auto maybeVersion = nn::validate(canonical); @@ -609,29 +663,29 @@ nn::GeneralResult unvalidatedConvert(const nn::Handle& han common::NativeHandle aidlNativeHandle; aidlNativeHandle.fds.reserve(handle.fds.size()); for (const auto& fd : handle.fds) { - const int dupFd = dup(fd.get()); - if (dupFd == -1) { - // TODO(b/120417090): is ANEURALNETWORKS_UNEXPECTED_NULL the correct error to return - // here? - return NN_ERROR() << "Failed to dup the fd"; - } - aidlNativeHandle.fds.emplace_back(dupFd); + auto duplicatedFd = NN_TRY(nn::dupFd(fd.get())); + aidlNativeHandle.fds.emplace_back(duplicatedFd.release()); } aidlNativeHandle.ints = handle.ints; return aidlNativeHandle; } +// Helper template for std::visit +template +struct overloaded : Ts... { + using Ts::operator()...; +}; +template +overloaded(Ts...)->overloaded; + static nn::GeneralResult aidlHandleFromNativeHandle( const native_handle_t& handle) { common::NativeHandle aidlNativeHandle; aidlNativeHandle.fds.reserve(handle.numFds); for (int i = 0; i < handle.numFds; ++i) { - const int dupFd = dup(handle.data[i]); - if (dupFd == -1) { - return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "Failed to dup the fd"; - } - aidlNativeHandle.fds.emplace_back(dupFd); + auto duplicatedFd = NN_TRY(nn::dupFd(handle.data[i])); + aidlNativeHandle.fds.emplace_back(duplicatedFd.release()); } aidlNativeHandle.ints = std::vector(&handle.data[handle.numFds], @@ -642,6 +696,30 @@ static nn::GeneralResult aidlHandleFromNativeHandle( } // namespace +nn::GeneralResult> unvalidatedConvert(const nn::CacheToken& cacheToken) { + return std::vector(cacheToken.begin(), cacheToken.end()); +} + +nn::GeneralResult unvalidatedConvert(const nn::BufferDesc& bufferDesc) { + return BufferDesc{.dimensions = NN_TRY(toSigned(bufferDesc.dimensions))}; +} + +nn::GeneralResult unvalidatedConvert(const nn::BufferRole& bufferRole) { + VERIFY_LE_INT32_MAX(bufferRole.modelIndex) + << "BufferRole: modelIndex must be <= std::numeric_limits::max()"; + VERIFY_LE_INT32_MAX(bufferRole.ioIndex) + << "BufferRole: ioIndex must be <= std::numeric_limits::max()"; + return BufferRole{ + .modelIndex = static_cast(bufferRole.modelIndex), + .ioIndex = static_cast(bufferRole.ioIndex), + .frequency = bufferRole.frequency, + }; +} + +nn::GeneralResult unvalidatedConvert(const nn::MeasureTiming& measureTiming) { + return measureTiming == nn::MeasureTiming::YES; +} + nn::GeneralResult unvalidatedConvert(const nn::SharedHandle& sharedHandle) { CHECK(sharedHandle != nullptr); return unvalidatedConvert(*sharedHandle); @@ -707,6 +785,230 @@ nn::GeneralResult unvalidatedConvert(const nn::OutputShape& outputS .isSufficient = outputShape.isSufficient}; } +nn::GeneralResult unvalidatedConvert( + const nn::ExecutionPreference& executionPreference) { + return static_cast(executionPreference); +} + +nn::GeneralResult unvalidatedConvert(const nn::OperandType& operandType) { + return static_cast(operandType); +} + +nn::GeneralResult unvalidatedConvert( + const nn::Operand::LifeTime& operandLifeTime) { + return static_cast(operandLifeTime); +} + +nn::GeneralResult unvalidatedConvert(const nn::DataLocation& location) { + VERIFY_LE_INT32_MAX(location.poolIndex) + << "DataLocation: pool index must be <= std::numeric_limits::max()"; + return DataLocation{ + .poolIndex = static_cast(location.poolIndex), + .offset = static_cast(location.offset), + .length = static_cast(location.length), + }; +} + +nn::GeneralResult> unvalidatedConvert( + const nn::Operand::ExtraParams& extraParams) { + return std::visit( + overloaded{ + [](const nn::Operand::NoParams&) + -> nn::GeneralResult> { + return std::nullopt; + }, + [](const nn::Operand::SymmPerChannelQuantParams& symmPerChannelQuantParams) + -> nn::GeneralResult> { + if (symmPerChannelQuantParams.channelDim > + std::numeric_limits::max()) { + // Using explicit type conversion because std::optional in successful + // result confuses the compiler. + return (NN_ERROR() << "symmPerChannelQuantParams.channelDim must be <= " + "std::numeric_limits::max(), received: " + << symmPerChannelQuantParams.channelDim) + . + operator nn::GeneralResult>(); + } + return OperandExtraParams::make( + SymmPerChannelQuantParams{ + .scales = symmPerChannelQuantParams.scales, + .channelDim = static_cast( + symmPerChannelQuantParams.channelDim), + }); + }, + [](const nn::Operand::ExtensionParams& extensionParams) + -> nn::GeneralResult> { + return OperandExtraParams::make( + extensionParams); + }, + }, + extraParams); +} + +nn::GeneralResult unvalidatedConvert(const nn::Operand& operand) { + return Operand{ + .type = NN_TRY(unvalidatedConvert(operand.type)), + .dimensions = NN_TRY(toSigned(operand.dimensions)), + .scale = operand.scale, + .zeroPoint = operand.zeroPoint, + .lifetime = NN_TRY(unvalidatedConvert(operand.lifetime)), + .location = NN_TRY(unvalidatedConvert(operand.location)), + .extraParams = NN_TRY(unvalidatedConvert(operand.extraParams)), + }; +} + +nn::GeneralResult unvalidatedConvert(const nn::OperationType& operationType) { + return static_cast(operationType); +} + +nn::GeneralResult unvalidatedConvert(const nn::Operation& operation) { + return Operation{ + .type = NN_TRY(unvalidatedConvert(operation.type)), + .inputs = NN_TRY(toSigned(operation.inputs)), + .outputs = NN_TRY(toSigned(operation.outputs)), + }; +} + +nn::GeneralResult unvalidatedConvert(const nn::Model::Subgraph& subgraph) { + return Subgraph{ + .operands = NN_TRY(unvalidatedConvert(subgraph.operands)), + .operations = NN_TRY(unvalidatedConvert(subgraph.operations)), + .inputIndexes = NN_TRY(toSigned(subgraph.inputIndexes)), + .outputIndexes = NN_TRY(toSigned(subgraph.outputIndexes)), + }; +} + +nn::GeneralResult> unvalidatedConvert( + const nn::Model::OperandValues& operandValues) { + return std::vector(operandValues.data(), operandValues.data() + operandValues.size()); +} + +nn::GeneralResult unvalidatedConvert( + const nn::Model::ExtensionNameAndPrefix& extensionNameToPrefix) { + return ExtensionNameAndPrefix{ + .name = extensionNameToPrefix.name, + .prefix = extensionNameToPrefix.prefix, + }; +} + +nn::GeneralResult unvalidatedConvert(const nn::Model& model) { + return Model{ + .main = NN_TRY(unvalidatedConvert(model.main)), + .referenced = NN_TRY(unvalidatedConvert(model.referenced)), + .operandValues = NN_TRY(unvalidatedConvert(model.operandValues)), + .pools = NN_TRY(unvalidatedConvert(model.pools)), + .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, + .extensionNameToPrefix = NN_TRY(unvalidatedConvert(model.extensionNameToPrefix)), + }; +} + +nn::GeneralResult unvalidatedConvert(const nn::Priority& priority) { + return static_cast(priority); +} + +nn::GeneralResult unvalidatedConvert(const nn::Request& request) { + return Request{ + .inputs = NN_TRY(unvalidatedConvert(request.inputs)), + .outputs = NN_TRY(unvalidatedConvert(request.outputs)), + .pools = NN_TRY(unvalidatedConvert(request.pools)), + }; +} + +nn::GeneralResult unvalidatedConvert( + const nn::Request::Argument& requestArgument) { + if (requestArgument.lifetime == nn::Request::Argument::LifeTime::POINTER) { + return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) + << "Request cannot be unvalidatedConverted because it contains pointer-based memory"; + } + const bool hasNoValue = requestArgument.lifetime == nn::Request::Argument::LifeTime::NO_VALUE; + return RequestArgument{ + .hasNoValue = hasNoValue, + .location = NN_TRY(unvalidatedConvert(requestArgument.location)), + .dimensions = NN_TRY(toSigned(requestArgument.dimensions)), + }; +} + +nn::GeneralResult unvalidatedConvert(const nn::Request::MemoryPool& memoryPool) { + return std::visit( + overloaded{ + [](const nn::SharedMemory& memory) -> nn::GeneralResult { + return RequestMemoryPool::make( + NN_TRY(unvalidatedConvert(memory))); + }, + [](const nn::Request::MemoryDomainToken& token) + -> nn::GeneralResult { + return RequestMemoryPool::make( + underlyingType(token)); + }, + [](const nn::SharedBuffer& /*buffer*/) { + return (NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) + << "Unable to make memory pool from IBuffer") + . + operator nn::GeneralResult(); + }, + }, + memoryPool); +} + +nn::GeneralResult unvalidatedConvert(const nn::Timing& timing) { + return Timing{ + .timeOnDevice = NN_TRY(unvalidatedConvert(timing.timeOnDevice)), + .timeInDriver = NN_TRY(unvalidatedConvert(timing.timeInDriver)), + }; +} + +nn::GeneralResult unvalidatedConvert(const nn::Duration& duration) { + const uint64_t nanoseconds = duration.count(); + if (nanoseconds > std::numeric_limits::max()) { + return std::numeric_limits::max(); + } + return static_cast(nanoseconds); +} + +nn::GeneralResult unvalidatedConvert(const nn::OptionalDuration& optionalDuration) { + if (!optionalDuration.has_value()) { + return kNoTiming; + } + return unvalidatedConvert(optionalDuration.value()); +} + +nn::GeneralResult unvalidatedConvert(const nn::OptionalTimePoint& optionalTimePoint) { + if (!optionalTimePoint.has_value()) { + return kNoTiming; + } + return unvalidatedConvert(optionalTimePoint->time_since_epoch()); +} + +nn::GeneralResult unvalidatedConvert(const nn::SyncFence& syncFence) { + auto duplicatedFd = NN_TRY(nn::dupFd(syncFence.getFd())); + return ndk::ScopedFileDescriptor(duplicatedFd.release()); +} + +nn::GeneralResult unvalidatedConvertCache( + const nn::SharedHandle& handle) { + if (handle->ints.size() != 0) { + NN_ERROR() << "Cache handle must not contain ints"; + } + if (handle->fds.size() != 1) { + NN_ERROR() << "Cache handle must contain exactly one fd but contains " + << handle->fds.size(); + } + auto duplicatedFd = NN_TRY(nn::dupFd(handle->fds.front().get())); + return ndk::ScopedFileDescriptor(duplicatedFd.release()); +} + +nn::GeneralResult> convert(const nn::CacheToken& cacheToken) { + return unvalidatedConvert(cacheToken); +} + +nn::GeneralResult convert(const nn::BufferDesc& bufferDesc) { + return validatedConvert(bufferDesc); +} + +nn::GeneralResult convert(const nn::MeasureTiming& measureTiming) { + return validatedConvert(measureTiming); +} + nn::GeneralResult convert(const nn::SharedMemory& memory) { return validatedConvert(memory); } @@ -715,11 +1017,62 @@ nn::GeneralResult convert(const nn::ErrorStatus& errorStatus) { return validatedConvert(errorStatus); } +nn::GeneralResult convert(const nn::ExecutionPreference& executionPreference) { + return validatedConvert(executionPreference); +} + +nn::GeneralResult convert(const nn::Model& model) { + return validatedConvert(model); +} + +nn::GeneralResult convert(const nn::Priority& priority) { + return validatedConvert(priority); +} + +nn::GeneralResult convert(const nn::Request& request) { + return validatedConvert(request); +} + +nn::GeneralResult convert(const nn::Timing& timing) { + return validatedConvert(timing); +} + +nn::GeneralResult convert(const nn::OptionalDuration& optionalDuration) { + return validatedConvert(optionalDuration); +} + +nn::GeneralResult convert(const nn::OptionalTimePoint& outputShapes) { + return validatedConvert(outputShapes); +} + +nn::GeneralResult> convert(const std::vector& bufferRoles) { + return validatedConvert(bufferRoles); +} + nn::GeneralResult> convert( const std::vector& outputShapes) { return validatedConvert(outputShapes); } +nn::GeneralResult> convert( + const std::vector& cacheHandles) { + const auto version = NN_TRY(hal::utils::makeGeneralFailure(nn::validate(cacheHandles))); + if (version > kVersion) { + return NN_ERROR() << "Insufficient version: " << version << " vs required " << kVersion; + } + std::vector cacheFds; + cacheFds.reserve(cacheHandles.size()); + for (const auto& cacheHandle : cacheHandles) { + cacheFds.push_back(NN_TRY(unvalidatedConvertCache(cacheHandle))); + } + return cacheFds; +} + +nn::GeneralResult> convert( + const std::vector& syncFences) { + return unvalidatedConvert(syncFences); +} + nn::GeneralResult> toSigned(const std::vector& vec) { if (!std::all_of(vec.begin(), vec.end(), [](uint32_t v) { return v <= std::numeric_limits::max(); })) { diff --git a/neuralnetworks/aidl/utils/src/Device.cpp b/neuralnetworks/aidl/utils/src/Device.cpp new file mode 100644 index 0000000000..02ca861b6b --- /dev/null +++ b/neuralnetworks/aidl/utils/src/Device.cpp @@ -0,0 +1,294 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Device.h" + +#include "Buffer.h" +#include "Callbacks.h" +#include "Conversions.h" +#include "PreparedModel.h" +#include "ProtectCallback.h" +#include "Utils.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface +// lifetimes across processes and for protecting asynchronous calls across AIDL. + +namespace aidl::android::hardware::neuralnetworks::utils { + +namespace { + +nn::GeneralResult>> convert( + const std::vector& preparedModels) { + std::vector> aidlPreparedModels(preparedModels.size()); + for (size_t i = 0; i < preparedModels.size(); ++i) { + std::any underlyingResource = preparedModels[i]->getUnderlyingResource(); + if (const auto* aidlPreparedModel = + std::any_cast>(&underlyingResource)) { + aidlPreparedModels[i] = *aidlPreparedModel; + } else { + return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) + << "Unable to convert from nn::IPreparedModel to aidl_hal::IPreparedModel"; + } + } + return aidlPreparedModels; +} + +nn::GeneralResult getCapabilitiesFrom(IDevice* device) { + CHECK(device != nullptr); + Capabilities capabilities; + const auto ret = device->getCapabilities(&capabilities); + HANDLE_ASTATUS(ret) << "getCapabilities failed"; + return nn::convert(capabilities); +} + +nn::GeneralResult getVersionStringFrom(aidl_hal::IDevice* device) { + CHECK(device != nullptr); + std::string version; + const auto ret = device->getVersionString(&version); + HANDLE_ASTATUS(ret) << "getVersionString failed"; + return version; +} + +nn::GeneralResult getDeviceTypeFrom(aidl_hal::IDevice* device) { + CHECK(device != nullptr); + DeviceType deviceType; + const auto ret = device->getType(&deviceType); + HANDLE_ASTATUS(ret) << "getDeviceType failed"; + return nn::convert(deviceType); +} + +nn::GeneralResult> getSupportedExtensionsFrom( + aidl_hal::IDevice* device) { + CHECK(device != nullptr); + std::vector supportedExtensions; + const auto ret = device->getSupportedExtensions(&supportedExtensions); + HANDLE_ASTATUS(ret) << "getExtensions failed"; + return nn::convert(supportedExtensions); +} + +nn::GeneralResult> getNumberOfCacheFilesNeededFrom( + aidl_hal::IDevice* device) { + CHECK(device != nullptr); + NumberOfCacheFiles numberOfCacheFiles; + const auto ret = device->getNumberOfCacheFilesNeeded(&numberOfCacheFiles); + HANDLE_ASTATUS(ret) << "getNumberOfCacheFilesNeeded failed"; + + if (numberOfCacheFiles.numDataCache < 0 || numberOfCacheFiles.numModelCache < 0) { + return NN_ERROR() << "Driver reported negative numer of cache files needed"; + } + if (static_cast(numberOfCacheFiles.numModelCache) > nn::kMaxNumberOfCacheFiles) { + return NN_ERROR() << "getNumberOfCacheFilesNeeded returned numModelCache files greater " + "than allowed max (" + << numberOfCacheFiles.numModelCache << " vs " + << nn::kMaxNumberOfCacheFiles << ")"; + } + if (static_cast(numberOfCacheFiles.numDataCache) > nn::kMaxNumberOfCacheFiles) { + return NN_ERROR() << "getNumberOfCacheFilesNeeded returned numDataCache files greater " + "than allowed max (" + << numberOfCacheFiles.numDataCache << " vs " << nn::kMaxNumberOfCacheFiles + << ")"; + } + return std::make_pair(numberOfCacheFiles.numDataCache, numberOfCacheFiles.numModelCache); +} + +} // namespace + +nn::GeneralResult> Device::create( + std::string name, std::shared_ptr device) { + if (name.empty()) { + return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) + << "aidl_hal::utils::Device::create must have non-empty name"; + } + if (device == nullptr) { + return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) + << "aidl_hal::utils::Device::create must have non-null device"; + } + + auto versionString = NN_TRY(getVersionStringFrom(device.get())); + const auto deviceType = NN_TRY(getDeviceTypeFrom(device.get())); + auto extensions = NN_TRY(getSupportedExtensionsFrom(device.get())); + auto capabilities = NN_TRY(getCapabilitiesFrom(device.get())); + const auto numberOfCacheFilesNeeded = NN_TRY(getNumberOfCacheFilesNeededFrom(device.get())); + + auto deathHandler = NN_TRY(DeathHandler::create(device)); + return std::make_shared( + PrivateConstructorTag{}, std::move(name), std::move(versionString), deviceType, + std::move(extensions), std::move(capabilities), numberOfCacheFilesNeeded, + std::move(device), std::move(deathHandler)); +} + +Device::Device(PrivateConstructorTag /*tag*/, std::string name, std::string versionString, + nn::DeviceType deviceType, std::vector extensions, + nn::Capabilities capabilities, + std::pair numberOfCacheFilesNeeded, + std::shared_ptr device, DeathHandler deathHandler) + : kName(std::move(name)), + kVersionString(std::move(versionString)), + kDeviceType(deviceType), + kExtensions(std::move(extensions)), + kCapabilities(std::move(capabilities)), + kNumberOfCacheFilesNeeded(numberOfCacheFilesNeeded), + kDevice(std::move(device)), + kDeathHandler(std::move(deathHandler)) {} + +const std::string& Device::getName() const { + return kName; +} + +const std::string& Device::getVersionString() const { + return kVersionString; +} + +nn::Version Device::getFeatureLevel() const { + return nn::Version::ANDROID_S; +} + +nn::DeviceType Device::getType() const { + return kDeviceType; +} + +bool Device::isUpdatable() const { + return false; +} + +const std::vector& Device::getSupportedExtensions() const { + return kExtensions; +} + +const nn::Capabilities& Device::getCapabilities() const { + return kCapabilities; +} + +std::pair Device::getNumberOfCacheFilesNeeded() const { + return kNumberOfCacheFilesNeeded; +} + +nn::GeneralResult Device::wait() const { + const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_ping(kDevice->asBinder().get())); + HANDLE_ASTATUS(ret) << "ping failed"; + return {}; +} + +nn::GeneralResult> Device::getSupportedOperations(const nn::Model& model) const { + // Ensure that model is ready for IPC. + std::optional maybeModelInShared; + const nn::Model& modelInShared = + NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared)); + + const auto aidlModel = NN_TRY(convert(modelInShared)); + + std::vector supportedOperations; + const auto ret = kDevice->getSupportedOperations(aidlModel, &supportedOperations); + HANDLE_ASTATUS(ret) << "getSupportedOperations failed"; + + return supportedOperations; +} + +nn::GeneralResult Device::prepareModel( + const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority, + nn::OptionalTimePoint deadline, const std::vector& modelCache, + const std::vector& dataCache, const nn::CacheToken& token) const { + // Ensure that model is ready for IPC. + std::optional maybeModelInShared; + const nn::Model& modelInShared = + NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared)); + + const auto aidlModel = NN_TRY(convert(modelInShared)); + const auto aidlPreference = NN_TRY(convert(preference)); + const auto aidlPriority = NN_TRY(convert(priority)); + const auto aidlDeadline = NN_TRY(convert(deadline)); + const auto aidlModelCache = NN_TRY(convert(modelCache)); + const auto aidlDataCache = NN_TRY(convert(dataCache)); + const auto aidlToken = NN_TRY(convert(token)); + + const auto cb = ndk::SharedRefBase::make(); + const auto scoped = kDeathHandler.protectCallback(cb.get()); + + const auto ret = kDevice->prepareModel(aidlModel, aidlPreference, aidlPriority, aidlDeadline, + aidlModelCache, aidlDataCache, aidlToken, cb); + HANDLE_ASTATUS(ret) << "prepareModel failed"; + + return cb->get(); +} + +nn::GeneralResult Device::prepareModelFromCache( + nn::OptionalTimePoint deadline, const std::vector& modelCache, + const std::vector& dataCache, const nn::CacheToken& token) const { + const auto aidlDeadline = NN_TRY(convert(deadline)); + const auto aidlModelCache = NN_TRY(convert(modelCache)); + const auto aidlDataCache = NN_TRY(convert(dataCache)); + const auto aidlToken = NN_TRY(convert(token)); + + const auto cb = ndk::SharedRefBase::make(); + const auto scoped = kDeathHandler.protectCallback(cb.get()); + + const auto ret = kDevice->prepareModelFromCache(aidlDeadline, aidlModelCache, aidlDataCache, + aidlToken, cb); + HANDLE_ASTATUS(ret) << "prepareModelFromCache failed"; + + return cb->get(); +} + +nn::GeneralResult Device::allocate( + const nn::BufferDesc& desc, const std::vector& preparedModels, + const std::vector& inputRoles, + const std::vector& outputRoles) const { + const auto aidlDesc = NN_TRY(convert(desc)); + const auto aidlPreparedModels = NN_TRY(convert(preparedModels)); + const auto aidlInputRoles = NN_TRY(convert(inputRoles)); + const auto aidlOutputRoles = NN_TRY(convert(outputRoles)); + + std::vector aidlPreparedModelParcels; + aidlPreparedModelParcels.reserve(aidlPreparedModels.size()); + for (const auto& preparedModel : aidlPreparedModels) { + aidlPreparedModelParcels.push_back({.preparedModel = preparedModel}); + } + + DeviceBuffer buffer; + const auto ret = kDevice->allocate(aidlDesc, aidlPreparedModelParcels, aidlInputRoles, + aidlOutputRoles, &buffer); + HANDLE_ASTATUS(ret) << "IDevice::allocate failed"; + + if (buffer.token < 0) { + return NN_ERROR() << "IDevice::allocate returned negative token"; + } + + return Buffer::create(buffer.buffer, static_cast(buffer.token)); +} + +DeathMonitor* Device::getDeathMonitor() const { + return kDeathHandler.getDeathMonitor().get(); +} + +} // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/src/PreparedModel.cpp b/neuralnetworks/aidl/utils/src/PreparedModel.cpp new file mode 100644 index 0000000000..aee4d90dab --- /dev/null +++ b/neuralnetworks/aidl/utils/src/PreparedModel.cpp @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "PreparedModel.h" + +#include "Callbacks.h" +#include "Conversions.h" +#include "ProtectCallback.h" +#include "Utils.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +// See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface +// lifetimes across processes and for protecting asynchronous calls across AIDL. + +namespace aidl::android::hardware::neuralnetworks::utils { +namespace { + +nn::GeneralResult, nn::Timing>> convertExecutionResults( + const std::vector& outputShapes, const Timing& timing) { + return std::make_pair(NN_TRY(nn::convert(outputShapes)), NN_TRY(nn::convert(timing))); +} + +nn::GeneralResult> convertFencedExecutionResults( + ErrorStatus status, const aidl_hal::Timing& timingLaunched, + const aidl_hal::Timing& timingFenced) { + HANDLE_HAL_STATUS(status) << "fenced execution callback info failed with " << toString(status); + return std::make_pair(NN_TRY(nn::convert(timingLaunched)), NN_TRY(nn::convert(timingFenced))); +} + +} // namespace + +nn::GeneralResult> PreparedModel::create( + std::shared_ptr preparedModel) { + if (preparedModel == nullptr) { + return NN_ERROR() + << "aidl_hal::utils::PreparedModel::create must have non-null preparedModel"; + } + + return std::make_shared(PrivateConstructorTag{}, std::move(preparedModel)); +} + +PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, + std::shared_ptr preparedModel) + : kPreparedModel(std::move(preparedModel)) {} + +nn::ExecutionResult, nn::Timing>> PreparedModel::execute( + const nn::Request& request, nn::MeasureTiming measure, + const nn::OptionalTimePoint& deadline, + const nn::OptionalDuration& loopTimeoutDuration) const { + // Ensure that request is ready for IPC. + std::optional maybeRequestInShared; + const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure( + hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared))); + + const auto aidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared))); + const auto aidlMeasure = NN_TRY(hal::utils::makeExecutionFailure(convert(measure))); + const auto aidlDeadline = NN_TRY(hal::utils::makeExecutionFailure(convert(deadline))); + const auto aidlLoopTimeoutDuration = + NN_TRY(hal::utils::makeExecutionFailure(convert(loopTimeoutDuration))); + + ExecutionResult executionResult; + const auto ret = kPreparedModel->executeSynchronously( + aidlRequest, aidlMeasure, aidlDeadline, aidlLoopTimeoutDuration, &executionResult); + HANDLE_ASTATUS(ret) << "executeSynchronously failed"; + if (!executionResult.outputSufficientSize) { + auto canonicalOutputShapes = + nn::convert(executionResult.outputShapes).value_or(std::vector{}); + return NN_ERROR(nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, std::move(canonicalOutputShapes)) + << "execution failed with " << nn::ErrorStatus::OUTPUT_INSUFFICIENT_SIZE; + } + auto [outputShapes, timing] = NN_TRY(hal::utils::makeExecutionFailure( + convertExecutionResults(executionResult.outputShapes, executionResult.timing))); + + NN_TRY(hal::utils::makeExecutionFailure( + hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared))); + + return std::make_pair(std::move(outputShapes), timing); +} + +nn::GeneralResult> +PreparedModel::executeFenced(const nn::Request& request, const std::vector& waitFor, + nn::MeasureTiming measure, const nn::OptionalTimePoint& deadline, + const nn::OptionalDuration& loopTimeoutDuration, + const nn::OptionalDuration& timeoutDurationAfterFence) const { + // Ensure that request is ready for IPC. + std::optional maybeRequestInShared; + const nn::Request& requestInShared = + NN_TRY(hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)); + + const auto aidlRequest = NN_TRY(convert(requestInShared)); + const auto aidlWaitFor = NN_TRY(convert(waitFor)); + const auto aidlMeasure = NN_TRY(convert(measure)); + const auto aidlDeadline = NN_TRY(convert(deadline)); + const auto aidlLoopTimeoutDuration = NN_TRY(convert(loopTimeoutDuration)); + const auto aidlTimeoutDurationAfterFence = NN_TRY(convert(timeoutDurationAfterFence)); + + FencedExecutionResult result; + const auto ret = kPreparedModel->executeFenced(aidlRequest, aidlWaitFor, aidlMeasure, + aidlDeadline, aidlLoopTimeoutDuration, + aidlTimeoutDurationAfterFence, &result); + HANDLE_ASTATUS(ret) << "executeFenced failed"; + + auto resultSyncFence = nn::SyncFence::createAsSignaled(); + if (result.syncFence.get() != -1) { + resultSyncFence = NN_TRY(nn::convert(result.syncFence)); + } + + auto callback = result.callback; + if (callback == nullptr) { + return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "callback is null"; + } + + // If executeFenced required the request memory to be moved into shared memory, block here until + // the fenced execution has completed and flush the memory back. + if (maybeRequestInShared.has_value()) { + const auto state = resultSyncFence.syncWait({}); + if (state != nn::SyncFence::FenceState::SIGNALED) { + return NN_ERROR() << "syncWait failed with " << state; + } + NN_TRY(hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared)); + } + + // Create callback which can be used to retrieve the execution error status and timings. + nn::ExecuteFencedInfoCallback resultCallback = + [callback]() -> nn::GeneralResult> { + ErrorStatus errorStatus; + Timing timingLaunched; + Timing timingFenced; + const auto ret = callback->getExecutionInfo(&timingLaunched, &timingFenced, &errorStatus); + HANDLE_ASTATUS(ret) << "fenced execution callback getExecutionInfo failed"; + return convertFencedExecutionResults(errorStatus, timingLaunched, timingFenced); + }; + + return std::make_pair(std::move(resultSyncFence), std::move(resultCallback)); +} + +nn::GeneralResult PreparedModel::configureExecutionBurst() const { + return hal::V1_0::utils::Burst::create(shared_from_this()); +} + +std::any PreparedModel::getUnderlyingResource() const { + std::shared_ptr resource = kPreparedModel; + return resource; +} + +} // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/src/ProtectCallback.cpp b/neuralnetworks/aidl/utils/src/ProtectCallback.cpp new file mode 100644 index 0000000000..124641cbb8 --- /dev/null +++ b/neuralnetworks/aidl/utils/src/ProtectCallback.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ProtectCallback.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "Utils.h" + +namespace aidl::android::hardware::neuralnetworks::utils { + +void DeathMonitor::serviceDied() { + std::lock_guard guard(mMutex); + std::for_each(mObjects.begin(), mObjects.end(), + [](hal::utils::IProtectedCallback* killable) { killable->notifyAsDeadObject(); }); +} + +void DeathMonitor::serviceDied(void* cookie) { + auto deathMonitor = static_cast(cookie); + deathMonitor->serviceDied(); +} + +void DeathMonitor::add(hal::utils::IProtectedCallback* killable) const { + CHECK(killable != nullptr); + std::lock_guard guard(mMutex); + mObjects.push_back(killable); +} + +void DeathMonitor::remove(hal::utils::IProtectedCallback* killable) const { + CHECK(killable != nullptr); + std::lock_guard guard(mMutex); + const auto removedIter = std::remove(mObjects.begin(), mObjects.end(), killable); + mObjects.erase(removedIter); +} + +nn::GeneralResult DeathHandler::create(std::shared_ptr object) { + if (object == nullptr) { + return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) + << "utils::DeathHandler::create must have non-null object"; + } + auto deathMonitor = std::make_shared(); + auto deathRecipient = ndk::ScopedAIBinder_DeathRecipient( + AIBinder_DeathRecipient_new(DeathMonitor::serviceDied)); + + // If passed a local binder, AIBinder_linkToDeath will do nothing and return + // STATUS_INVALID_OPERATION. We ignore this case because we only use local binders in tests + // where this is not an error. + if (object->isRemote()) { + const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_linkToDeath( + object->asBinder().get(), deathRecipient.get(), deathMonitor.get())); + HANDLE_ASTATUS(ret) << "AIBinder_linkToDeath failed"; + } + + return DeathHandler(std::move(object), std::move(deathRecipient), std::move(deathMonitor)); +} + +DeathHandler::DeathHandler(std::shared_ptr object, + ndk::ScopedAIBinder_DeathRecipient deathRecipient, + std::shared_ptr deathMonitor) + : kObject(std::move(object)), + kDeathRecipient(std::move(deathRecipient)), + kDeathMonitor(std::move(deathMonitor)) { + CHECK(kObject != nullptr); + CHECK(kDeathRecipient.get() != nullptr); + CHECK(kDeathMonitor != nullptr); +} + +DeathHandler::~DeathHandler() { + if (kObject != nullptr && kDeathRecipient.get() != nullptr && kDeathMonitor != nullptr) { + const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_unlinkToDeath( + kObject->asBinder().get(), kDeathRecipient.get(), kDeathMonitor.get())); + const auto maybeSuccess = handleTransportError(ret); + if (!maybeSuccess.ok()) { + LOG(ERROR) << maybeSuccess.error().message; + } + } +} + +[[nodiscard]] ::android::base::ScopeGuard DeathHandler::protectCallback( + hal::utils::IProtectedCallback* killable) const { + CHECK(killable != nullptr); + kDeathMonitor->add(killable); + return ::android::base::make_scope_guard( + [deathMonitor = kDeathMonitor, killable] { deathMonitor->remove(killable); }); +} + +} // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/src/Service.cpp b/neuralnetworks/aidl/utils/src/Service.cpp new file mode 100644 index 0000000000..5ec6ded5e5 --- /dev/null +++ b/neuralnetworks/aidl/utils/src/Service.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Service.h" + +#include +#include + +#include +#include +#include +#include +#include + +#include "Device.h" + +namespace aidl::android::hardware::neuralnetworks::utils { + +nn::GeneralResult getDevice(const std::string& name) { + hal::utils::ResilientDevice::Factory makeDevice = + [name](bool blocking) -> nn::GeneralResult { + auto service = blocking ? IDevice::fromBinder( + ndk::SpAIBinder(AServiceManager_getService(name.c_str()))) + : IDevice::fromBinder(ndk::SpAIBinder( + AServiceManager_checkService(name.c_str()))); + if (service == nullptr) { + return NN_ERROR() << (blocking ? "AServiceManager_getService" + : "AServiceManager_checkService") + << " returned nullptr"; + } + return Device::create(name, std::move(service)); + }; + + return hal::utils::ResilientDevice::create(std::move(makeDevice)); +} + +} // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/src/Utils.cpp b/neuralnetworks/aidl/utils/src/Utils.cpp index 8d00e5926a..95516c854b 100644 --- a/neuralnetworks/aidl/utils/src/Utils.cpp +++ b/neuralnetworks/aidl/utils/src/Utils.cpp @@ -16,13 +16,12 @@ #include "Utils.h" +#include #include namespace aidl::android::hardware::neuralnetworks::utils { namespace { -using ::android::nn::GeneralResult; - template nn::GeneralResult> cloneVec(const std::vector& arguments) { std::vector clonedObjects; @@ -34,13 +33,13 @@ nn::GeneralResult> cloneVec(const std::vector& arguments } template -GeneralResult> clone(const std::vector& arguments) { +nn::GeneralResult> clone(const std::vector& arguments) { return cloneVec(arguments); } } // namespace -GeneralResult clone(const Memory& memory) { +nn::GeneralResult clone(const Memory& memory) { common::NativeHandle nativeHandle; nativeHandle.ints = memory.handle.ints; nativeHandle.fds.reserve(memory.handle.fds.size()); @@ -58,7 +57,7 @@ GeneralResult clone(const Memory& memory) { }; } -GeneralResult clone(const RequestMemoryPool& requestPool) { +nn::GeneralResult clone(const RequestMemoryPool& requestPool) { using Tag = RequestMemoryPool::Tag; switch (requestPool.getTag()) { case Tag::pool: @@ -70,10 +69,10 @@ GeneralResult clone(const RequestMemoryPool& requestPool) { // compiler. return (NN_ERROR() << "Unrecognized request pool tag: " << requestPool.getTag()) . - operator GeneralResult(); + operator nn::GeneralResult(); } -GeneralResult clone(const Request& request) { +nn::GeneralResult clone(const Request& request) { return Request{ .inputs = request.inputs, .outputs = request.outputs, @@ -81,7 +80,7 @@ GeneralResult clone(const Request& request) { }; } -GeneralResult clone(const Model& model) { +nn::GeneralResult clone(const Model& model) { return Model{ .main = model.main, .referenced = model.referenced, @@ -92,4 +91,20 @@ GeneralResult clone(const Model& model) { }; } +nn::GeneralResult handleTransportError(const ndk::ScopedAStatus& ret) { + if (ret.getStatus() == STATUS_DEAD_OBJECT) { + return nn::error(nn::ErrorStatus::DEAD_OBJECT) + << "Binder transaction returned STATUS_DEAD_OBJECT: " << ret.getDescription(); + } + if (ret.isOk()) { + return {}; + } + if (ret.getExceptionCode() != EX_SERVICE_SPECIFIC) { + return nn::error(nn::ErrorStatus::GENERAL_FAILURE) + << "Binder transaction returned exception: " << ret.getDescription(); + } + return nn::error(static_cast(ret.getServiceSpecificError())) + << ret.getMessage(); +} + } // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/test/BufferTest.cpp b/neuralnetworks/aidl/utils/test/BufferTest.cpp new file mode 100644 index 0000000000..9736160395 --- /dev/null +++ b/neuralnetworks/aidl/utils/test/BufferTest.cpp @@ -0,0 +1,212 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "MockBuffer.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace aidl::android::hardware::neuralnetworks::utils { +namespace { + +using ::testing::_; +using ::testing::Invoke; +using ::testing::InvokeWithoutArgs; +using ::testing::Return; + +const auto kMemory = nn::createSharedMemory(4).value(); +const std::shared_ptr kInvalidBuffer; +constexpr auto kInvalidToken = nn::Request::MemoryDomainToken{0}; +constexpr auto kToken = nn::Request::MemoryDomainToken{1}; + +constexpr auto makeStatusOk = [] { return ndk::ScopedAStatus::ok(); }; + +constexpr auto makeGeneralFailure = [] { + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast(ErrorStatus::GENERAL_FAILURE)); +}; +constexpr auto makeGeneralTransportFailure = [] { + return ndk::ScopedAStatus::fromStatus(STATUS_NO_MEMORY); +}; +constexpr auto makeDeadObjectFailure = [] { + return ndk::ScopedAStatus::fromStatus(STATUS_DEAD_OBJECT); +}; + +} // namespace + +TEST(BufferTest, invalidBuffer) { + // run test + const auto result = Buffer::create(kInvalidBuffer, kToken); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(BufferTest, invalidToken) { + // setup call + const auto mockBuffer = MockBuffer::create(); + + // run test + const auto result = Buffer::create(mockBuffer, kInvalidToken); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(BufferTest, create) { + // setup call + const auto mockBuffer = MockBuffer::create(); + const auto buffer = Buffer::create(mockBuffer, kToken).value(); + + // run test + const auto token = buffer->getToken(); + + // verify result + EXPECT_EQ(token, kToken); +} + +TEST(BufferTest, copyTo) { + // setup call + const auto mockBuffer = MockBuffer::create(); + const auto buffer = Buffer::create(mockBuffer, kToken).value(); + EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(InvokeWithoutArgs(makeStatusOk)); + + // run test + const auto result = buffer->copyTo(kMemory); + + // verify result + EXPECT_TRUE(result.has_value()) << result.error().message; +} + +TEST(BufferTest, copyToError) { + // setup test + const auto mockBuffer = MockBuffer::create(); + const auto buffer = Buffer::create(mockBuffer, kToken).value(); + EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(InvokeWithoutArgs(makeGeneralFailure)); + + // run test + const auto result = buffer->copyTo(kMemory); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(BufferTest, copyToTransportFailure) { + // setup test + const auto mockBuffer = MockBuffer::create(); + const auto buffer = Buffer::create(mockBuffer, kToken).value(); + EXPECT_CALL(*mockBuffer, copyTo(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = buffer->copyTo(kMemory); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(BufferTest, copyToDeadObject) { + // setup test + const auto mockBuffer = MockBuffer::create(); + const auto buffer = Buffer::create(mockBuffer, kToken).value(); + EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = buffer->copyTo(kMemory); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(BufferTest, copyFrom) { + // setup call + const auto mockBuffer = MockBuffer::create(); + const auto buffer = Buffer::create(mockBuffer, kToken).value(); + EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(InvokeWithoutArgs(makeStatusOk)); + + // run test + const auto result = buffer->copyFrom(kMemory, {}); + + // verify result + EXPECT_TRUE(result.has_value()); +} + +TEST(BufferTest, copyFromError) { + // setup test + const auto mockBuffer = MockBuffer::create(); + const auto buffer = Buffer::create(mockBuffer, kToken).value(); + EXPECT_CALL(*mockBuffer, copyFrom(_, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); + + // run test + const auto result = buffer->copyFrom(kMemory, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(BufferTest, copyFromTransportFailure) { + // setup test + const auto mockBuffer = MockBuffer::create(); + const auto buffer = Buffer::create(mockBuffer, kToken).value(); + EXPECT_CALL(*mockBuffer, copyFrom(_, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = buffer->copyFrom(kMemory, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(BufferTest, copyFromDeadObject) { + // setup test + const auto mockBuffer = MockBuffer::create(); + const auto buffer = Buffer::create(mockBuffer, kToken).value(); + EXPECT_CALL(*mockBuffer, copyFrom(_, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = buffer->copyFrom(kMemory, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +} // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/test/DeviceTest.cpp b/neuralnetworks/aidl/utils/test/DeviceTest.cpp new file mode 100644 index 0000000000..e53b0a8df9 --- /dev/null +++ b/neuralnetworks/aidl/utils/test/DeviceTest.cpp @@ -0,0 +1,861 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "MockBuffer.h" +#include "MockDevice.h" +#include "MockPreparedModel.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace aidl::android::hardware::neuralnetworks::utils { +namespace { + +namespace nn = ::android::nn; +using ::testing::_; +using ::testing::DoAll; +using ::testing::Invoke; +using ::testing::InvokeWithoutArgs; +using ::testing::SetArgPointee; + +const nn::Model kSimpleModel = { + .main = {.operands = {{.type = nn::OperandType::TENSOR_FLOAT32, + .dimensions = {1}, + .lifetime = nn::Operand::LifeTime::SUBGRAPH_INPUT}, + {.type = nn::OperandType::TENSOR_FLOAT32, + .dimensions = {1}, + .lifetime = nn::Operand::LifeTime::SUBGRAPH_OUTPUT}}, + .operations = {{.type = nn::OperationType::RELU, .inputs = {0}, .outputs = {1}}}, + .inputIndexes = {0}, + .outputIndexes = {1}}}; + +const std::string kName = "Google-MockV1"; +const std::string kInvalidName = ""; +const std::shared_ptr kInvalidDevice; +constexpr PerformanceInfo kNoPerformanceInfo = {.execTime = std::numeric_limits::max(), + .powerUsage = std::numeric_limits::max()}; +constexpr NumberOfCacheFiles kNumberOfCacheFiles = {.numModelCache = nn::kMaxNumberOfCacheFiles, + .numDataCache = nn::kMaxNumberOfCacheFiles}; + +constexpr auto makeStatusOk = [] { return ndk::ScopedAStatus::ok(); }; + +std::shared_ptr createMockDevice() { + const auto mockDevice = MockDevice::create(); + + // Setup default actions for each relevant call. + ON_CALL(*mockDevice, getVersionString(_)) + .WillByDefault(DoAll(SetArgPointee<0>(kName), InvokeWithoutArgs(makeStatusOk))); + ON_CALL(*mockDevice, getType(_)) + .WillByDefault( + DoAll(SetArgPointee<0>(DeviceType::OTHER), InvokeWithoutArgs(makeStatusOk))); + ON_CALL(*mockDevice, getSupportedExtensions(_)) + .WillByDefault(DoAll(SetArgPointee<0>(std::vector{}), + InvokeWithoutArgs(makeStatusOk))); + ON_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) + .WillByDefault( + DoAll(SetArgPointee<0>(kNumberOfCacheFiles), InvokeWithoutArgs(makeStatusOk))); + ON_CALL(*mockDevice, getCapabilities(_)) + .WillByDefault( + DoAll(SetArgPointee<0>(Capabilities{ + .relaxedFloat32toFloat16PerformanceScalar = kNoPerformanceInfo, + .relaxedFloat32toFloat16PerformanceTensor = kNoPerformanceInfo, + .ifPerformance = kNoPerformanceInfo, + .whilePerformance = kNoPerformanceInfo, + }), + InvokeWithoutArgs(makeStatusOk))); + + // These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the + // uninteresting methods calls. + EXPECT_CALL(*mockDevice, getVersionString(_)).Times(testing::AnyNumber()); + EXPECT_CALL(*mockDevice, getType(_)).Times(testing::AnyNumber()); + EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(testing::AnyNumber()); + EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(testing::AnyNumber()); + EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(testing::AnyNumber()); + + return mockDevice; +} + +constexpr auto makePreparedModelReturnImpl = + [](ErrorStatus launchStatus, ErrorStatus returnStatus, + const std::shared_ptr& preparedModel, + const std::shared_ptr& cb) { + cb->notify(returnStatus, preparedModel); + if (launchStatus == ErrorStatus::NONE) { + return ndk::ScopedAStatus::ok(); + } + return ndk::ScopedAStatus::fromServiceSpecificError(static_cast(launchStatus)); + }; + +auto makePreparedModelReturn(ErrorStatus launchStatus, ErrorStatus returnStatus, + const std::shared_ptr& preparedModel) { + return [launchStatus, returnStatus, preparedModel]( + const Model& /*model*/, ExecutionPreference /*preference*/, + Priority /*priority*/, const int64_t& /*deadline*/, + const std::vector& /*modelCache*/, + const std::vector& /*dataCache*/, + const std::vector& /*token*/, + const std::shared_ptr& cb) -> ndk::ScopedAStatus { + return makePreparedModelReturnImpl(launchStatus, returnStatus, preparedModel, cb); + }; +} + +auto makePreparedModelFromCacheReturn(ErrorStatus launchStatus, ErrorStatus returnStatus, + const std::shared_ptr& preparedModel) { + return [launchStatus, returnStatus, preparedModel]( + const int64_t& /*deadline*/, + const std::vector& /*modelCache*/, + const std::vector& /*dataCache*/, + const std::vector& /*token*/, + const std::shared_ptr& cb) { + return makePreparedModelReturnImpl(launchStatus, returnStatus, preparedModel, cb); + }; +} + +constexpr auto makeGeneralFailure = [] { + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast(ErrorStatus::GENERAL_FAILURE)); +}; +constexpr auto makeGeneralTransportFailure = [] { + return ndk::ScopedAStatus::fromStatus(STATUS_NO_MEMORY); +}; +constexpr auto makeDeadObjectFailure = [] { + return ndk::ScopedAStatus::fromStatus(STATUS_DEAD_OBJECT); +}; + +} // namespace + +TEST(DeviceTest, invalidName) { + // run test + const auto device = MockDevice::create(); + const auto result = Device::create(kInvalidName, device); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT); +} + +TEST(DeviceTest, invalidDevice) { + // run test + const auto result = Device::create(kName, kInvalidDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT); +} + +TEST(DeviceTest, getVersionStringError) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getVersionString(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, getVersionStringTransportFailure) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getVersionString(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, getVersionStringDeadObject) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getVersionString(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(DeviceTest, getTypeError) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getType(_)).Times(1).WillOnce(InvokeWithoutArgs(makeGeneralFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, getTypeTransportFailure) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getType(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, getTypeDeadObject) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getType(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(DeviceTest, getSupportedExtensionsError) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getSupportedExtensions(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, getSupportedExtensionsTransportFailure) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getSupportedExtensions(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, getSupportedExtensionsDeadObject) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getSupportedExtensions(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(DeviceTest, getNumberOfCacheFilesNeededError) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, dataCacheFilesExceedsSpecifiedMax) { + // setup test + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) + .Times(1) + .WillOnce(DoAll(SetArgPointee<0>(NumberOfCacheFiles{ + .numModelCache = nn::kMaxNumberOfCacheFiles + 1, + .numDataCache = nn::kMaxNumberOfCacheFiles}), + InvokeWithoutArgs(makeStatusOk))); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, modelCacheFilesExceedsSpecifiedMax) { + // setup test + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) + .Times(1) + .WillOnce(DoAll(SetArgPointee<0>(NumberOfCacheFiles{ + .numModelCache = nn::kMaxNumberOfCacheFiles, + .numDataCache = nn::kMaxNumberOfCacheFiles + 1}), + InvokeWithoutArgs(makeStatusOk))); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, getNumberOfCacheFilesNeededTransportFailure) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, getNumberOfCacheFilesNeededDeadObject) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(DeviceTest, getCapabilitiesError) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getCapabilities(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, getCapabilitiesTransportFailure) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getCapabilities(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, getCapabilitiesDeadObject) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getCapabilities(_)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = Device::create(kName, mockDevice); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(DeviceTest, getName) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + + // run test + const auto& name = device->getName(); + + // verify result + EXPECT_EQ(name, kName); +} + +TEST(DeviceTest, getFeatureLevel) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + + // run test + const auto featureLevel = device->getFeatureLevel(); + + // verify result + EXPECT_EQ(featureLevel, nn::Version::ANDROID_S); +} + +TEST(DeviceTest, getCachedData) { + // setup call + const auto mockDevice = createMockDevice(); + EXPECT_CALL(*mockDevice, getVersionString(_)).Times(1); + EXPECT_CALL(*mockDevice, getType(_)).Times(1); + EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(1); + EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1); + EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(1); + + const auto result = Device::create(kName, mockDevice); + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; + const auto& device = result.value(); + + // run test and verify results + EXPECT_EQ(device->getVersionString(), device->getVersionString()); + EXPECT_EQ(device->getType(), device->getType()); + EXPECT_EQ(device->getSupportedExtensions(), device->getSupportedExtensions()); + EXPECT_EQ(device->getNumberOfCacheFilesNeeded(), device->getNumberOfCacheFilesNeeded()); + EXPECT_EQ(device->getCapabilities(), device->getCapabilities()); +} + +TEST(DeviceTest, getSupportedOperations) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)) + .Times(1) + .WillOnce(DoAll( + SetArgPointee<1>(std::vector(kSimpleModel.main.operations.size(), true)), + InvokeWithoutArgs(makeStatusOk))); + + // run test + const auto result = device->getSupportedOperations(kSimpleModel); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; + const auto& supportedOperations = result.value(); + EXPECT_EQ(supportedOperations.size(), kSimpleModel.main.operations.size()); + EXPECT_THAT(supportedOperations, Each(testing::IsTrue())); +} + +TEST(DeviceTest, getSupportedOperationsError) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); + + // run test + const auto result = device->getSupportedOperations(kSimpleModel); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, getSupportedOperationsTransportFailure) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = device->getSupportedOperations(kSimpleModel); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, getSupportedOperationsDeadObject) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = device->getSupportedOperations(kSimpleModel); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(DeviceTest, prepareModel) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + const auto mockPreparedModel = MockPreparedModel::create(); + EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) + .Times(1) + .WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::NONE, ErrorStatus::NONE, + mockPreparedModel))); + + // run test + const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, + nn::Priority::DEFAULT, {}, {}, {}, {}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; + EXPECT_NE(result.value(), nullptr); +} + +TEST(DeviceTest, prepareModelLaunchError) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) + .Times(1) + .WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::GENERAL_FAILURE, + ErrorStatus::GENERAL_FAILURE, nullptr))); + + // run test + const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, + nn::Priority::DEFAULT, {}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, prepareModelReturnError) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) + .Times(1) + .WillOnce(Invoke(makePreparedModelReturn(ErrorStatus::NONE, + ErrorStatus::GENERAL_FAILURE, nullptr))); + + // run test + const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, + nn::Priority::DEFAULT, {}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, prepareModelNullptrError) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) + .Times(1) + .WillOnce( + Invoke(makePreparedModelReturn(ErrorStatus::NONE, ErrorStatus::NONE, nullptr))); + + // run test + const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, + nn::Priority::DEFAULT, {}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, prepareModelTransportFailure) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, + nn::Priority::DEFAULT, {}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, prepareModelDeadObject) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, + nn::Priority::DEFAULT, {}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(DeviceTest, prepareModelAsyncCrash) { + // setup test + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + const auto ret = [&device]() { + DeathMonitor::serviceDied(device->getDeathMonitor()); + return ndk::ScopedAStatus::ok(); + }; + EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(ret)); + + // run test + const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT, + nn::Priority::DEFAULT, {}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(DeviceTest, prepareModelFromCache) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + const auto mockPreparedModel = MockPreparedModel::create(); + EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) + .Times(1) + .WillOnce(Invoke(makePreparedModelFromCacheReturn(ErrorStatus::NONE, ErrorStatus::NONE, + mockPreparedModel))); + + // run test + const auto result = device->prepareModelFromCache({}, {}, {}, {}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; + EXPECT_NE(result.value(), nullptr); +} + +TEST(DeviceTest, prepareModelFromCacheLaunchError) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) + .Times(1) + .WillOnce(Invoke(makePreparedModelFromCacheReturn( + ErrorStatus::GENERAL_FAILURE, ErrorStatus::GENERAL_FAILURE, nullptr))); + + // run test + const auto result = device->prepareModelFromCache({}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, prepareModelFromCacheReturnError) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) + .Times(1) + .WillOnce(Invoke(makePreparedModelFromCacheReturn( + ErrorStatus::NONE, ErrorStatus::GENERAL_FAILURE, nullptr))); + + // run test + const auto result = device->prepareModelFromCache({}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, prepareModelFromCacheNullptrError) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) + .Times(1) + .WillOnce(Invoke(makePreparedModelFromCacheReturn(ErrorStatus::NONE, ErrorStatus::NONE, + nullptr))); + + // run test + const auto result = device->prepareModelFromCache({}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, prepareModelFromCacheTransportFailure) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = device->prepareModelFromCache({}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, prepareModelFromCacheDeadObject) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = device->prepareModelFromCache({}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(DeviceTest, prepareModelFromCacheAsyncCrash) { + // setup test + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + const auto ret = [&device]() { + DeathMonitor::serviceDied(device->getDeathMonitor()); + return ndk::ScopedAStatus::ok(); + }; + EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(ret)); + + // run test + const auto result = device->prepareModelFromCache({}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(DeviceTest, allocate) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + const auto mockBuffer = DeviceBuffer{.buffer = MockBuffer::create(), .token = 1}; + EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _)) + .Times(1) + .WillOnce(DoAll(SetArgPointee<4>(mockBuffer), InvokeWithoutArgs(makeStatusOk))); + + // run test + const auto result = device->allocate({}, {}, {}, {}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; + EXPECT_NE(result.value(), nullptr); +} + +TEST(DeviceTest, allocateError) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); + + // run test + const auto result = device->allocate({}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, allocateTransportFailure) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = device->allocate({}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(DeviceTest, allocateDeadObject) { + // setup call + const auto mockDevice = createMockDevice(); + const auto device = Device::create(kName, mockDevice).value(); + EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = device->allocate({}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +} // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/aidl/utils/test/MockBuffer.h b/neuralnetworks/aidl/utils/test/MockBuffer.h new file mode 100644 index 0000000000..5746176116 --- /dev/null +++ b/neuralnetworks/aidl/utils/test/MockBuffer.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_BUFFER +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_BUFFER + +#include +#include +#include +#include +#include + +namespace aidl::android::hardware::neuralnetworks::utils { + +class MockBuffer final : public BnBuffer { + public: + static std::shared_ptr create(); + + MOCK_METHOD(ndk::ScopedAStatus, copyTo, (const Memory& dst), (override)); + MOCK_METHOD(ndk::ScopedAStatus, copyFrom, + (const Memory& src, const std::vector& dimensions), (override)); +}; + +inline std::shared_ptr MockBuffer::create() { + return ndk::SharedRefBase::make(); +} + +} // namespace aidl::android::hardware::neuralnetworks::utils + +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_BUFFER diff --git a/neuralnetworks/aidl/utils/test/MockDevice.h b/neuralnetworks/aidl/utils/test/MockDevice.h new file mode 100644 index 0000000000..9b35bf8f81 --- /dev/null +++ b/neuralnetworks/aidl/utils/test/MockDevice.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_DEVICE +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_DEVICE + +#include +#include +#include +#include +#include + +namespace aidl::android::hardware::neuralnetworks::utils { + +class MockDevice final : public BnDevice { + public: + static std::shared_ptr create(); + + MOCK_METHOD(ndk::ScopedAStatus, allocate, + (const BufferDesc& desc, const std::vector& preparedModels, + const std::vector& inputRoles, + const std::vector& outputRoles, DeviceBuffer* deviceBuffer), + (override)); + MOCK_METHOD(ndk::ScopedAStatus, getCapabilities, (Capabilities * capabilities), (override)); + MOCK_METHOD(ndk::ScopedAStatus, getNumberOfCacheFilesNeeded, + (NumberOfCacheFiles * numberOfCacheFiles), (override)); + MOCK_METHOD(ndk::ScopedAStatus, getSupportedExtensions, (std::vector * extensions), + (override)); + MOCK_METHOD(ndk::ScopedAStatus, getSupportedOperations, + (const Model& model, std::vector* supportedOperations), (override)); + MOCK_METHOD(ndk::ScopedAStatus, getType, (DeviceType * deviceType), (override)); + MOCK_METHOD(ndk::ScopedAStatus, getVersionString, (std::string * version), (override)); + MOCK_METHOD(ndk::ScopedAStatus, prepareModel, + (const Model& model, ExecutionPreference preference, Priority priority, + int64_t deadline, const std::vector& modelCache, + const std::vector& dataCache, + const std::vector& token, + const std::shared_ptr& callback), + (override)); + MOCK_METHOD(ndk::ScopedAStatus, prepareModelFromCache, + (int64_t deadline, const std::vector& modelCache, + const std::vector& dataCache, + const std::vector& token, + const std::shared_ptr& callback), + (override)); +}; + +inline std::shared_ptr MockDevice::create() { + return ndk::SharedRefBase::make(); +} + +} // namespace aidl::android::hardware::neuralnetworks::utils + +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_DEVICE diff --git a/neuralnetworks/aidl/utils/test/MockFencedExecutionCallback.h b/neuralnetworks/aidl/utils/test/MockFencedExecutionCallback.h new file mode 100644 index 0000000000..463e1c9700 --- /dev/null +++ b/neuralnetworks/aidl/utils/test/MockFencedExecutionCallback.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_FENCED_EXECUTION_CALLBACK +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_FENCED_EXECUTION_CALLBACK + +#include +#include +#include +#include +#include +#include + +namespace aidl::android::hardware::neuralnetworks::utils { + +class MockFencedExecutionCallback final : public BnFencedExecutionCallback { + public: + static std::shared_ptr create(); + + // V1_3 methods below. + MOCK_METHOD(ndk::ScopedAStatus, getExecutionInfo, + (Timing * timingLaunched, Timing* timingFenced, ErrorStatus* errorStatus), + (override)); +}; + +inline std::shared_ptr MockFencedExecutionCallback::create() { + return ndk::SharedRefBase::make(); +} + +} // namespace aidl::android::hardware::neuralnetworks::utils + +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_FENCED_EXECUTION_CALLBACK diff --git a/neuralnetworks/aidl/utils/test/MockPreparedModel.h b/neuralnetworks/aidl/utils/test/MockPreparedModel.h new file mode 100644 index 0000000000..545b491f1d --- /dev/null +++ b/neuralnetworks/aidl/utils/test/MockPreparedModel.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_PREPARED_MODEL +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_PREPARED_MODEL + +#include +#include +#include +#include +#include +#include + +namespace aidl::android::hardware::neuralnetworks::utils { + +class MockPreparedModel final : public BnPreparedModel { + public: + static std::shared_ptr create(); + + MOCK_METHOD(ndk::ScopedAStatus, executeSynchronously, + (const Request& request, bool measureTiming, int64_t deadline, + int64_t loopTimeoutDuration, ExecutionResult* executionResult), + (override)); + MOCK_METHOD(ndk::ScopedAStatus, executeFenced, + (const Request& request, const std::vector& waitFor, + bool measureTiming, int64_t deadline, int64_t loopTimeoutDuration, + int64_t duration, FencedExecutionResult* fencedExecutionResult), + (override)); +}; + +inline std::shared_ptr MockPreparedModel::create() { + return ndk::SharedRefBase::make(); +} + +} // namespace aidl::android::hardware::neuralnetworks::utils + +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_TEST_MOCK_PREPARED_MODEL diff --git a/neuralnetworks/aidl/utils/test/PreparedModelTest.cpp b/neuralnetworks/aidl/utils/test/PreparedModelTest.cpp new file mode 100644 index 0000000000..7e28861054 --- /dev/null +++ b/neuralnetworks/aidl/utils/test/PreparedModelTest.cpp @@ -0,0 +1,272 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "MockFencedExecutionCallback.h" +#include "MockPreparedModel.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace aidl::android::hardware::neuralnetworks::utils { +namespace { + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Invoke; +using ::testing::InvokeWithoutArgs; +using ::testing::SetArgPointee; + +const std::shared_ptr kInvalidPreparedModel; +constexpr auto kNoTiming = Timing{.timeOnDevice = -1, .timeInDriver = -1}; + +constexpr auto makeStatusOk = [] { return ndk::ScopedAStatus::ok(); }; + +constexpr auto makeGeneralFailure = [] { + return ndk::ScopedAStatus::fromServiceSpecificError( + static_cast(ErrorStatus::GENERAL_FAILURE)); +}; +constexpr auto makeGeneralTransportFailure = [] { + return ndk::ScopedAStatus::fromStatus(STATUS_NO_MEMORY); +}; +constexpr auto makeDeadObjectFailure = [] { + return ndk::ScopedAStatus::fromStatus(STATUS_DEAD_OBJECT); +}; + +auto makeFencedExecutionResult(const std::shared_ptr& callback) { + return [callback](const Request& /*request*/, + const std::vector& /*waitFor*/, + bool /*measureTiming*/, int64_t /*deadline*/, int64_t /*loopTimeoutDuration*/, + int64_t /*duration*/, FencedExecutionResult* fencedExecutionResult) { + *fencedExecutionResult = FencedExecutionResult{.callback = callback, + .syncFence = ndk::ScopedFileDescriptor(-1)}; + return ndk::ScopedAStatus::ok(); + }; +} + +} // namespace + +TEST(PreparedModelTest, invalidPreparedModel) { + // run test + const auto result = PreparedModel::create(kInvalidPreparedModel); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(PreparedModelTest, executeSync) { + // setup call + const auto mockPreparedModel = MockPreparedModel::create(); + const auto preparedModel = PreparedModel::create(mockPreparedModel).value(); + const auto mockExecutionResult = ExecutionResult{ + .outputSufficientSize = true, + .outputShapes = {}, + .timing = kNoTiming, + }; + EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _, _, _)) + .Times(1) + .WillOnce( + DoAll(SetArgPointee<4>(mockExecutionResult), InvokeWithoutArgs(makeStatusOk))); + + // run test + const auto result = preparedModel->execute({}, {}, {}, {}); + + // verify result + EXPECT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; +} + +TEST(PreparedModelTest, executeSyncError) { + // setup test + const auto mockPreparedModel = MockPreparedModel::create(); + const auto preparedModel = PreparedModel::create(mockPreparedModel).value(); + EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _, _, _)) + .Times(1) + .WillOnce(Invoke(makeGeneralFailure)); + + // run test + const auto result = preparedModel->execute({}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(PreparedModelTest, executeSyncTransportFailure) { + // setup test + const auto mockPreparedModel = MockPreparedModel::create(); + const auto preparedModel = PreparedModel::create(mockPreparedModel).value(); + EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = preparedModel->execute({}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(PreparedModelTest, executeSyncDeadObject) { + // setup test + const auto mockPreparedModel = MockPreparedModel::create(); + const auto preparedModel = PreparedModel::create(mockPreparedModel).value(); + EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = preparedModel->execute({}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(PreparedModelTest, executeFenced) { + // setup call + const auto mockPreparedModel = MockPreparedModel::create(); + const auto preparedModel = PreparedModel::create(mockPreparedModel).value(); + const auto mockCallback = MockFencedExecutionCallback::create(); + EXPECT_CALL(*mockCallback, getExecutionInfo(_, _, _)) + .Times(1) + .WillOnce(DoAll(SetArgPointee<0>(kNoTiming), SetArgPointee<1>(kNoTiming), + SetArgPointee<2>(ErrorStatus::NONE), Invoke(makeStatusOk))); + EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _)) + .Times(1) + .WillOnce(Invoke(makeFencedExecutionResult(mockCallback))); + + // run test + const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; + const auto& [syncFence, callback] = result.value(); + EXPECT_EQ(syncFence.syncWait({}), nn::SyncFence::FenceState::SIGNALED); + ASSERT_NE(callback, nullptr); + + // get results from callback + const auto callbackResult = callback(); + ASSERT_TRUE(callbackResult.has_value()) << "Failed with " << callbackResult.error().code << ": " + << callbackResult.error().message; +} + +TEST(PreparedModelTest, executeFencedCallbackError) { + // setup call + const auto mockPreparedModel = MockPreparedModel::create(); + const auto preparedModel = PreparedModel::create(mockPreparedModel).value(); + const auto mockCallback = MockFencedExecutionCallback::create(); + EXPECT_CALL(*mockCallback, getExecutionInfo(_, _, _)) + .Times(1) + .WillOnce(Invoke(DoAll(SetArgPointee<0>(kNoTiming), SetArgPointee<1>(kNoTiming), + SetArgPointee<2>(ErrorStatus::GENERAL_FAILURE), + Invoke(makeStatusOk)))); + EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _)) + .Times(1) + .WillOnce(Invoke(makeFencedExecutionResult(mockCallback))); + + // run test + const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; + const auto& [syncFence, callback] = result.value(); + EXPECT_NE(syncFence.syncWait({}), nn::SyncFence::FenceState::ACTIVE); + ASSERT_NE(callback, nullptr); + + // verify callback failure + const auto callbackResult = callback(); + ASSERT_FALSE(callbackResult.has_value()); + EXPECT_EQ(callbackResult.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(PreparedModelTest, executeFencedError) { + // setup test + const auto mockPreparedModel = MockPreparedModel::create(); + const auto preparedModel = PreparedModel::create(mockPreparedModel).value(); + EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralFailure)); + + // run test + const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(PreparedModelTest, executeFencedTransportFailure) { + // setup test + const auto mockPreparedModel = MockPreparedModel::create(); + const auto preparedModel = PreparedModel::create(mockPreparedModel).value(); + EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure)); + + // run test + const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(PreparedModelTest, executeFencedDeadObject) { + // setup test + const auto mockPreparedModel = MockPreparedModel::create(); + const auto preparedModel = PreparedModel::create(mockPreparedModel).value(); + EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _)) + .Times(1) + .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure)); + + // run test + const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +// TODO: test burst execution if/when it is added to nn::IPreparedModel. + +TEST(PreparedModelTest, getUnderlyingResource) { + // setup test + const auto mockPreparedModel = MockPreparedModel::create(); + const auto preparedModel = PreparedModel::create(mockPreparedModel).value(); + + // run test + const auto resource = preparedModel->getUnderlyingResource(); + + // verify resource + const std::shared_ptr* maybeMock = + std::any_cast>(&resource); + ASSERT_NE(maybeMock, nullptr); + EXPECT_EQ(maybeMock->get(), mockPreparedModel.get()); +} + +} // namespace aidl::android::hardware::neuralnetworks::utils diff --git a/neuralnetworks/utils/README.md b/neuralnetworks/utils/README.md index 45ca0b442f..87b3f9f676 100644 --- a/neuralnetworks/utils/README.md +++ b/neuralnetworks/utils/README.md @@ -49,7 +49,9 @@ The `convert` functions operate only on types that used in a HIDL method call di (i.e., not as a nested class) or used in a subsequent version of the NN HAL. Prefer using `convert` over `unvalidatedConvert`. -# HIDL Interface Lifetimes across Processes +# Interface Lifetimes across Processes + +## HIDL Some notes about HIDL interface objects and lifetimes across processes: @@ -68,7 +70,20 @@ behave strangely with `::android::wp` references.) If the process which created the HIDL interface object dies, any call on this object from another process will result in a HIDL transport error with the code `DEAD_OBJECT`. -# Protecting Asynchronous Calls across HIDL +## AIDL + +We use NDK backend for AIDL interfaces. Handling of lifetimes is generally the same with the +following differences: +* Interfaces inherit from `ndk::ICInterface`, which inherits from `ndk::SharedRefBase`. The latter + is an analog of `::android::RefBase` using `std::shared_ptr` for reference counting. +* AIDL calls return `ndk::ScopedAStatus` which wraps fields of types `binder_status_t` and + `binder_exception_t`. In case the call is made on a dead object, the call will return + `ndk::ScopedAStatus` with exception `EX_TRANSACTION_FAILED` and binder status + `STATUS_DEAD_OBJECT`. + +# Protecting Asynchronous Calls + +## Across HIDL Some notes about asynchronous calls across HIDL: @@ -95,3 +110,17 @@ died. nnapi/hal/ProtectCallback.h's `DeathHandler` uses `hidl_death_recipient`s driver process has died, and `DeathHandler` will unblock any thread waiting on the results of an `IProtectedCallback` callback object that may otherwise not be signaled. In order for this to work, the `IProtectedCallback` object must have been registered via `DeathHandler::protectCallback()`. + +## Across AIDL + +We use NDK backend for AIDL interfaces. Handling of asynchronous calls is generally the same with +the following differences: +* AIDL calls return `ndk::ScopedAStatus` which wraps fields of types `binder_status_t` and + `binder_exception_t`. In case the call is made on a dead object, the call will return + `ndk::ScopedAStatus` with exception `EX_TRANSACTION_FAILED` and binder status + `STATUS_DEAD_OBJECT`. +* AIDL interface doesn't contain asynchronous `IPreparedModel::execute`. +* Service death is handled using `AIBinder_DeathRecipient` object which is linked to an interface + object using `AIBinder_linkToDeath`. nnapi/hal/aidl/ProtectCallback.h provides `DeathHandler` + object that is a direct analog of HIDL `DeathHandler`, only using libbinder_ndk objects for + implementation. diff --git a/neuralnetworks/utils/common/Android.bp b/neuralnetworks/utils/common/Android.bp index 6162fe8cbc..2ed1e407ef 100644 --- a/neuralnetworks/utils/common/Android.bp +++ b/neuralnetworks/utils/common/Android.bp @@ -35,8 +35,10 @@ cc_library_static { "neuralnetworks_types", ], shared_libs: [ + "android.hardware.neuralnetworks-V1-ndk_platform", "libhidlbase", "libnativewindow", + "libbinder_ndk", ], } diff --git a/neuralnetworks/utils/common/include/nnapi/hal/CommonUtils.h b/neuralnetworks/utils/common/include/nnapi/hal/CommonUtils.h index 2f6112a4c9..8fe6b904b2 100644 --- a/neuralnetworks/utils/common/include/nnapi/hal/CommonUtils.h +++ b/neuralnetworks/utils/common/include/nnapi/hal/CommonUtils.h @@ -32,6 +32,8 @@ namespace hal = ::android::hardware::neuralnetworks; // Shorthands namespace aidl::android::hardware::neuralnetworks { namespace aidl_hal = ::aidl::android::hardware::neuralnetworks; +namespace hal = ::android::hardware::neuralnetworks; +namespace nn = ::android::nn; } // namespace aidl::android::hardware::neuralnetworks // Shorthands