diff --git a/neuralnetworks/1.2/utils/include/nnapi/hal/1.2/ExecutionBurstController.h b/neuralnetworks/1.2/utils/include/nnapi/hal/1.2/Burst.h similarity index 83% rename from neuralnetworks/1.2/utils/include/nnapi/hal/1.2/ExecutionBurstController.h rename to neuralnetworks/1.2/utils/include/nnapi/hal/1.2/Burst.h index 8078693aa5..ac9411c462 100644 --- a/neuralnetworks/1.2/utils/include/nnapi/hal/1.2/ExecutionBurstController.h +++ b/neuralnetworks/1.2/utils/include/nnapi/hal/1.2/Burst.h @@ -14,10 +14,10 @@ * limitations under the License. */ -#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_EXECUTION_BURST_CONTROLLER_H -#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_EXECUTION_BURST_CONTROLLER_H +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_BURST_H +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_BURST_H -#include "ExecutionBurstUtils.h" +#include "nnapi/hal/1.2/BurstUtils.h" #include #include @@ -49,13 +49,11 @@ namespace android::hardware::neuralnetworks::V1_2::utils { /** - * The ExecutionBurstController class manages both the serialization and deserialization of data - * across FMQ, making it appear to the runtime as a regular synchronous inference. Additionally, - * this class manages the burst's memory cache. + * The Burst class manages both the serialization and deserialization of data across FMQ, making it + * appear to the runtime as a regular synchronous inference. Additionally, this class manages the + * burst's memory cache. */ -class ExecutionBurstController final - : public nn::IBurst, - public std::enable_shared_from_this { +class Burst final : public nn::IBurst, public std::enable_shared_from_this { struct PrivateConstructorTag {}; public: @@ -150,21 +148,21 @@ class ExecutionBurstController final * Creates a burst controller on a prepared model. * * @param preparedModel Model prepared for execution to execute on. - * @param pollingTimeWindow How much time (in microseconds) the ExecutionBurstController is - * allowed to poll the FMQ before waiting on the blocking futex. Polling may result in lower - * latencies at the potential cost of more power usage. - * @return ExecutionBurstController Execution burst controller object. + * @param pollingTimeWindow How much time (in microseconds) the Burst is allowed to poll the FMQ + * before waiting on the blocking futex. Polling may result in lower latencies at the + * potential cost of more power usage. + * @return Burst Execution burst controller object. */ - static nn::GeneralResult> create( + static nn::GeneralResult> create( nn::SharedPreparedModel preparedModel, const sp& hidlPreparedModel, std::chrono::microseconds pollingTimeWindow); - ExecutionBurstController(PrivateConstructorTag tag, nn::SharedPreparedModel preparedModel, - std::unique_ptr requestChannelSender, - std::unique_ptr resultChannelReceiver, - sp callback, sp burstContext, - std::shared_ptr memoryCache, - neuralnetworks::utils::DeathHandler deathHandler); + Burst(PrivateConstructorTag tag, nn::SharedPreparedModel preparedModel, + std::unique_ptr requestChannelSender, + std::unique_ptr resultChannelReceiver, + sp callback, sp burstContext, + std::shared_ptr memoryCache, + neuralnetworks::utils::DeathHandler deathHandler); // See IBurst::cacheMemory for information on this method. OptionalCacheHold cacheMemory(const nn::SharedMemory& memory) const override; @@ -202,4 +200,4 @@ class ExecutionBurstController final } // namespace android::hardware::neuralnetworks::V1_2::utils -#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_EXECUTION_BURST_CONTROLLER_H +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_BURST_H diff --git a/neuralnetworks/1.2/utils/include/nnapi/hal/1.2/ExecutionBurstUtils.h b/neuralnetworks/1.2/utils/include/nnapi/hal/1.2/BurstUtils.h similarity index 98% rename from neuralnetworks/1.2/utils/include/nnapi/hal/1.2/ExecutionBurstUtils.h rename to neuralnetworks/1.2/utils/include/nnapi/hal/1.2/BurstUtils.h index c081305a86..7a6a241fbe 100644 --- a/neuralnetworks/1.2/utils/include/nnapi/hal/1.2/ExecutionBurstUtils.h +++ b/neuralnetworks/1.2/utils/include/nnapi/hal/1.2/BurstUtils.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_EXECUTION_BURST_UTILS_H -#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_EXECUTION_BURST_UTILS_H +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_BURST_UTILS_H +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_BURST_UTILS_H #include #include @@ -298,4 +298,4 @@ class ResultChannelReceiver final : public neuralnetworks::utils::IProtectedCall } // namespace android::hardware::neuralnetworks::V1_2::utils -#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_EXECUTION_BURST_UTILS_H +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_BURST_UTILS_H diff --git a/neuralnetworks/1.2/utils/src/ExecutionBurstController.cpp b/neuralnetworks/1.2/utils/src/Burst.cpp similarity index 80% rename from neuralnetworks/1.2/utils/src/ExecutionBurstController.cpp rename to neuralnetworks/1.2/utils/src/Burst.cpp index a8ded9e95c..e0a23f1635 100644 --- a/neuralnetworks/1.2/utils/src/ExecutionBurstController.cpp +++ b/neuralnetworks/1.2/utils/src/Burst.cpp @@ -14,10 +14,8 @@ * limitations under the License. */ -#define LOG_TAG "ExecutionBurstController" - -#include "ExecutionBurstController.h" -#include "ExecutionBurstUtils.h" +#include "Burst.h" +#include "BurstUtils.h" #include #include @@ -57,14 +55,13 @@ class BurstExecution final : public nn::IExecution, public: static nn::GeneralResult> create( - std::shared_ptr controller, - std::vector request, hal::utils::RequestRelocation relocation, - std::vector cacheHolds); + std::shared_ptr controller, std::vector request, + hal::utils::RequestRelocation relocation, + std::vector cacheHolds); - BurstExecution(PrivateConstructorTag tag, - std::shared_ptr controller, + BurstExecution(PrivateConstructorTag tag, std::shared_ptr controller, std::vector request, hal::utils::RequestRelocation relocation, - std::vector cacheHolds); + std::vector cacheHolds); nn::ExecutionResult, nn::Timing>> compute( const nn::OptionalTimePoint& deadline) const override; @@ -74,10 +71,10 @@ class BurstExecution final : public nn::IExecution, const nn::OptionalDuration& timeoutDurationAfterFence) const override; private: - const std::shared_ptr kController; + const std::shared_ptr kController; const std::vector kRequest; const hal::utils::RequestRelocation kRelocation; - const std::vector kCacheHolds; + const std::vector kCacheHolds; }; nn::GeneralResult> executionBurstResultCallback( @@ -92,8 +89,7 @@ nn::GeneralResult> executionBurstResultCallback( } nn::GeneralResult> getMemoriesHelper( - const hidl_vec& slots, - const std::shared_ptr& memoryCache) { + const hidl_vec& slots, const std::shared_ptr& memoryCache) { hidl_vec memories(slots.size()); for (size_t i = 0; i < slots.size(); ++i) { const int32_t slot = slots[i]; @@ -110,7 +106,7 @@ nn::GeneralResult> getMemoriesHelper( // MemoryCache methods -ExecutionBurstController::MemoryCache::MemoryCache() { +Burst::MemoryCache::MemoryCache() { constexpr size_t kPreallocatedCount = 1024; std::vector freeSlotsSpace; freeSlotsSpace.reserve(kPreallocatedCount); @@ -119,13 +115,13 @@ ExecutionBurstController::MemoryCache::MemoryCache() { mCacheCleaner.reserve(kPreallocatedCount); } -void ExecutionBurstController::MemoryCache::setBurstContext(sp burstContext) { +void Burst::MemoryCache::setBurstContext(sp burstContext) { std::lock_guard guard(mMutex); mBurstContext = std::move(burstContext); } -std::pair -ExecutionBurstController::MemoryCache::cacheMemory(const nn::SharedMemory& memory) { +std::pair Burst::MemoryCache::cacheMemory( + const nn::SharedMemory& memory) { std::unique_lock lock(mMutex); base::ScopedLockAssertion lockAssert(mMutex); @@ -163,7 +159,7 @@ ExecutionBurstController::MemoryCache::cacheMemory(const nn::SharedMemory& memor return std::make_pair(slot, std::move(cleaner)); } -nn::GeneralResult ExecutionBurstController::MemoryCache::getMemory(int32_t slot) { +nn::GeneralResult Burst::MemoryCache::getMemory(int32_t slot) { std::lock_guard guard(mMutex); if (slot < 0 || static_cast(slot) >= mMemoryCache.size()) { return NN_ERROR() << "Invalid slot: " << slot << " vs " << mMemoryCache.size(); @@ -171,7 +167,7 @@ nn::GeneralResult ExecutionBurstController::MemoryCache::getMe return mMemoryCache[slot]; } -void ExecutionBurstController::MemoryCache::freeMemory(const nn::SharedMemory& memory) { +void Burst::MemoryCache::freeMemory(const nn::SharedMemory& memory) { { std::lock_guard guard(mMutex); const int32_t slot = mMemoryIdToSlot.at(memory); @@ -189,7 +185,7 @@ void ExecutionBurstController::MemoryCache::freeMemory(const nn::SharedMemory& m mCond.notify_all(); } -int32_t ExecutionBurstController::MemoryCache::allocateSlotLocked() { +int32_t Burst::MemoryCache::allocateSlotLocked() { constexpr size_t kMaxNumberOfSlots = std::numeric_limits::max(); // If there is a free slot, use it. @@ -210,18 +206,18 @@ int32_t ExecutionBurstController::MemoryCache::allocateSlotLocked() { // ExecutionBurstCallback methods -ExecutionBurstController::ExecutionBurstCallback::ExecutionBurstCallback( +Burst::ExecutionBurstCallback::ExecutionBurstCallback( const std::shared_ptr& memoryCache) : kMemoryCache(memoryCache) { CHECK(memoryCache != nullptr); } -Return ExecutionBurstController::ExecutionBurstCallback::getMemories( - const hidl_vec& slots, getMemories_cb cb) { +Return Burst::ExecutionBurstCallback::getMemories(const hidl_vec& slots, + getMemories_cb cb) { const auto memoryCache = kMemoryCache.lock(); if (memoryCache == nullptr) { - LOG(ERROR) << "ExecutionBurstController::ExecutionBurstCallback::getMemories called after " - "the MemoryCache has been freed"; + LOG(ERROR) << "Burst::ExecutionBurstCallback::getMemories called after the MemoryCache has " + "been freed"; cb(V1_0::ErrorStatus::GENERAL_FAILURE, {}); return Void(); } @@ -229,8 +225,8 @@ Return ExecutionBurstController::ExecutionBurstCallback::getMemories( const auto maybeMemories = getMemoriesHelper(slots, memoryCache); if (!maybeMemories.has_value()) { const auto& [message, code] = maybeMemories.error(); - LOG(ERROR) << "ExecutionBurstController::ExecutionBurstCallback::getMemories failed with " - << code << ": " << message; + LOG(ERROR) << "Burst::ExecutionBurstCallback::getMemories failed with " << code << ": " + << message; cb(V1_0::ErrorStatus::INVALID_ARGUMENT, {}); return Void(); } @@ -239,14 +235,14 @@ Return ExecutionBurstController::ExecutionBurstCallback::getMemories( return Void(); } -// ExecutionBurstController methods +// Burst methods -nn::GeneralResult> ExecutionBurstController::create( +nn::GeneralResult> Burst::create( nn::SharedPreparedModel preparedModel, const sp& hidlPreparedModel, std::chrono::microseconds pollingTimeWindow) { // check inputs if (preparedModel == nullptr || hidlPreparedModel == nullptr) { - return NN_ERROR() << "ExecutionBurstController::create passed a nullptr"; + return NN_ERROR() << "Burst::create passed a nullptr"; } // create FMQ objects @@ -282,18 +278,18 @@ nn::GeneralResult> ExecutionBurs deathHandler.protectCallbackForLifetimeOfDeathHandler(resultChannelReceiver.get()); // make and return controller - return std::make_shared( + return std::make_shared( PrivateConstructorTag{}, std::move(preparedModel), std::move(requestChannelSender), std::move(resultChannelReceiver), std::move(burstCallback), std::move(burstContext), std::move(memoryCache), std::move(deathHandler)); } -ExecutionBurstController::ExecutionBurstController( - PrivateConstructorTag /*tag*/, nn::SharedPreparedModel preparedModel, - std::unique_ptr requestChannelSender, - std::unique_ptr resultChannelReceiver, - sp callback, sp burstContext, - std::shared_ptr memoryCache, neuralnetworks::utils::DeathHandler deathHandler) +Burst::Burst(PrivateConstructorTag /*tag*/, nn::SharedPreparedModel preparedModel, + std::unique_ptr requestChannelSender, + std::unique_ptr resultChannelReceiver, + sp callback, sp burstContext, + std::shared_ptr memoryCache, + neuralnetworks::utils::DeathHandler deathHandler) : kPreparedModel(std::move(preparedModel)), mRequestChannelSender(std::move(requestChannelSender)), mResultChannelReceiver(std::move(resultChannelReceiver)), @@ -302,21 +298,20 @@ ExecutionBurstController::ExecutionBurstController( mMemoryCache(std::move(memoryCache)), kDeathHandler(std::move(deathHandler)) {} -ExecutionBurstController::OptionalCacheHold ExecutionBurstController::cacheMemory( - const nn::SharedMemory& memory) const { +Burst::OptionalCacheHold Burst::cacheMemory(const nn::SharedMemory& memory) const { auto [slot, hold] = mMemoryCache->cacheMemory(memory); return hold; } -nn::ExecutionResult, nn::Timing>> -ExecutionBurstController::execute(const nn::Request& request, nn::MeasureTiming measure, - const nn::OptionalTimePoint& deadline, - const nn::OptionalDuration& loopTimeoutDuration) const { +nn::ExecutionResult, nn::Timing>> Burst::execute( + const nn::Request& request, nn::MeasureTiming measure, + const nn::OptionalTimePoint& deadline, + const nn::OptionalDuration& loopTimeoutDuration) const { // This is the first point when we know an execution is occurring, so begin to collect // systraces. Note that the first point we can begin collecting systraces in // ExecutionBurstServer is when the RequestChannelReceiver realizes there is data in the FMQ, so // ExecutionBurstServer collects systraces at different points in the code. - NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ExecutionBurstController::execute"); + NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "Burst::execute"); // if the request is valid but of a higher version than what's supported in burst execution, // fall back to another execution path @@ -357,10 +352,10 @@ ExecutionBurstController::execute(const nn::Request& request, nn::MeasureTiming } // See IBurst::createReusableExecution for information on this method. -nn::GeneralResult ExecutionBurstController::createReusableExecution( +nn::GeneralResult Burst::createReusableExecution( const nn::Request& request, nn::MeasureTiming measure, const nn::OptionalDuration& loopTimeoutDuration) const { - NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "ExecutionBurstController::createReusableExecution"); + NNTRACE_RT(NNTRACE_PHASE_EXECUTION, "Burst::createReusableExecution"); // if the request is valid but of a higher version than what's supported in burst execution, // fall back to another execution path @@ -397,12 +392,10 @@ nn::GeneralResult ExecutionBurstController::createReusableE std::move(relocation), std::move(holds)); } -nn::ExecutionResult, nn::Timing>> -ExecutionBurstController::executeInternal(const std::vector& requestPacket, - const hal::utils::RequestRelocation& relocation, - FallbackFunction fallback) const { - NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION, - "ExecutionBurstController::executeInternal"); +nn::ExecutionResult, nn::Timing>> Burst::executeInternal( + const std::vector& requestPacket, + const hal::utils::RequestRelocation& relocation, FallbackFunction fallback) const { + NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION, "Burst::executeInternal"); // Ensure that at most one execution is in flight at any given time. const bool alreadyInFlight = mExecutionInFlight.test_and_set(); @@ -435,9 +428,9 @@ ExecutionBurstController::executeInternal(const std::vector& re } nn::GeneralResult> BurstExecution::create( - std::shared_ptr controller, - std::vector request, hal::utils::RequestRelocation relocation, - std::vector cacheHolds) { + std::shared_ptr controller, std::vector request, + hal::utils::RequestRelocation relocation, + std::vector cacheHolds) { if (controller == nullptr) { return NN_ERROR() << "V1_2::utils::BurstExecution::create must have non-null controller"; } @@ -448,10 +441,10 @@ nn::GeneralResult> BurstExecution::create( } BurstExecution::BurstExecution(PrivateConstructorTag /*tag*/, - std::shared_ptr controller, + std::shared_ptr controller, std::vector request, hal::utils::RequestRelocation relocation, - std::vector cacheHolds) + std::vector cacheHolds) : kController(std::move(controller)), kRequest(std::move(request)), kRelocation(std::move(relocation)), diff --git a/neuralnetworks/1.2/utils/src/ExecutionBurstUtils.cpp b/neuralnetworks/1.2/utils/src/BurstUtils.cpp similarity index 99% rename from neuralnetworks/1.2/utils/src/ExecutionBurstUtils.cpp rename to neuralnetworks/1.2/utils/src/BurstUtils.cpp index e0d029aa59..b589c468ce 100644 --- a/neuralnetworks/1.2/utils/src/ExecutionBurstUtils.cpp +++ b/neuralnetworks/1.2/utils/src/BurstUtils.cpp @@ -14,9 +14,7 @@ * limitations under the License. */ -#define LOG_TAG "ExecutionBurstUtils" - -#include "ExecutionBurstUtils.h" +#include "BurstUtils.h" #include #include diff --git a/neuralnetworks/1.2/utils/src/PreparedModel.cpp b/neuralnetworks/1.2/utils/src/PreparedModel.cpp index b8a5ae087c..6df3df332a 100644 --- a/neuralnetworks/1.2/utils/src/PreparedModel.cpp +++ b/neuralnetworks/1.2/utils/src/PreparedModel.cpp @@ -16,11 +16,11 @@ #include "PreparedModel.h" +#include "Burst.h" +#include "BurstUtils.h" #include "Callbacks.h" #include "Conversions.h" #include "Execution.h" -#include "ExecutionBurstController.h" -#include "ExecutionBurstUtils.h" #include "Utils.h" #include @@ -150,16 +150,8 @@ nn::GeneralResult PreparedModel::createReusableExecution( } nn::GeneralResult PreparedModel::configureExecutionBurst() const { - auto self = shared_from_this(); - auto fallback = [preparedModel = std::move(self)]( - const nn::Request& request, nn::MeasureTiming measure, - const nn::OptionalTimePoint& deadline, - const nn::OptionalDuration& loopTimeoutDuration) - -> nn::ExecutionResult, nn::Timing>> { - return preparedModel->execute(request, measure, deadline, loopTimeoutDuration); - }; const auto pollingTimeWindow = getBurstControllerPollingTimeWindow(); - return ExecutionBurstController::create(shared_from_this(), kPreparedModel, pollingTimeWindow); + return Burst::create(shared_from_this(), kPreparedModel, pollingTimeWindow); } std::any PreparedModel::getUnderlyingResource() const { diff --git a/neuralnetworks/1.3/utils/src/PreparedModel.cpp b/neuralnetworks/1.3/utils/src/PreparedModel.cpp index 2c81cb2ad5..ce977e55ed 100644 --- a/neuralnetworks/1.3/utils/src/PreparedModel.cpp +++ b/neuralnetworks/1.3/utils/src/PreparedModel.cpp @@ -32,9 +32,9 @@ #include #include #include +#include +#include #include -#include -#include #include #include @@ -246,17 +246,8 @@ nn::GeneralResult PreparedModel::createReusableExecution( } nn::GeneralResult PreparedModel::configureExecutionBurst() const { - auto self = shared_from_this(); - auto fallback = [preparedModel = std::move(self)]( - const nn::Request& request, nn::MeasureTiming measure, - const nn::OptionalTimePoint& deadline, - const nn::OptionalDuration& loopTimeoutDuration) - -> nn::ExecutionResult, nn::Timing>> { - return preparedModel->execute(request, measure, deadline, loopTimeoutDuration); - }; const auto pollingTimeWindow = V1_2::utils::getBurstControllerPollingTimeWindow(); - return V1_2::utils::ExecutionBurstController::create(shared_from_this(), kPreparedModel, - pollingTimeWindow); + return V1_2::utils::Burst::create(shared_from_this(), kPreparedModel, pollingTimeWindow); } std::any PreparedModel::getUnderlyingResource() const { diff --git a/neuralnetworks/1.2/utils/include/nnapi/hal/1.2/ExecutionBurstServer.h b/neuralnetworks/utils/adapter/include/nnapi/hal/Burst.h similarity index 67% rename from neuralnetworks/1.2/utils/include/nnapi/hal/1.2/ExecutionBurstServer.h rename to neuralnetworks/utils/adapter/include/nnapi/hal/Burst.h index 500aa0cfd0..a3aa706a0c 100644 --- a/neuralnetworks/1.2/utils/include/nnapi/hal/1.2/ExecutionBurstServer.h +++ b/neuralnetworks/utils/adapter/include/nnapi/hal/Burst.h @@ -14,14 +14,13 @@ * limitations under the License. */ -#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_EXECUTION_BURST_SERVER_H -#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_EXECUTION_BURST_SERVER_H - -#include "ExecutionBurstUtils.h" +#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_ADAPTER_BURST_H +#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_ADAPTER_BURST_H #include #include #include +#include #include #include #include @@ -30,6 +29,7 @@ #include #include #include +#include #include #include @@ -39,13 +39,13 @@ #include #include -namespace android::hardware::neuralnetworks::V1_2::utils { +namespace android::hardware::neuralnetworks::adapter { /** - * The ExecutionBurstServer class is responsible for waiting for and deserializing a request object - * from a FMQ, performing the inference, and serializing the result back across another FMQ. + * The Burst class is responsible for waiting for and deserializing a request object from a FMQ, + * performing the inference, and serializing the result back across another FMQ. */ -class ExecutionBurstServer : public IBurstContext { +class Burst : public V1_2::IBurstContext { struct PrivateConstructorTag {}; public: @@ -58,13 +58,13 @@ class ExecutionBurstServer : public IBurstContext { public: // Precondition: burstExecutor != nullptr // Precondition: burstCallback != nullptr - MemoryCache(nn::SharedBurst burstExecutor, sp burstCallback); + MemoryCache(nn::SharedBurst burstExecutor, sp burstCallback); /** * Get the cached memory objects corresponding to provided slot identifiers. * - * If the slot entry is not present in the cache, this class will use IBurstCallback to - * retrieve those entries that are not present in the cache, then cache them. + * If the slot entry is not present in the cache, this class will use V1_2::IBurstCallback + * to retrieve those entries that are not present in the cache, then cache them. * * @param slots Identifiers of memory objects to be retrieved. * @return A vector where each element is the memory object and a ref-counted cache "hold" @@ -93,7 +93,7 @@ class ExecutionBurstServer : public IBurstContext { std::map> mCache GUARDED_BY(mMutex); nn::SharedBurst kBurstExecutor; - const sp kBurstCallback; + const sp kBurstCallback; }; /** @@ -111,45 +111,45 @@ class ExecutionBurstServer : public IBurstContext { * execution. * @param burstExecutor Object which maintains a local cache of the memory pools and executes * using the cached memory pools. - * @param pollingTimeWindow How much time (in microseconds) the ExecutionBurstServer is allowed - * to poll the FMQ before waiting on the blocking futex. Polling may result in lower - * latencies at the potential cost of more power usage. - * @return IBurstContext Handle to the burst context. + * @param pollingTimeWindow How much time (in microseconds) the Burst is allowed to poll the FMQ + * before waiting on the blocking futex. Polling may result in lower latencies at the + * potential cost of more power usage. + * @return V1_2::IBurstContext Handle to the burst context. */ - static nn::GeneralResult> create( - const sp& callback, - const MQDescriptorSync& requestChannel, - const MQDescriptorSync& resultChannel, nn::SharedBurst burstExecutor, + static nn::GeneralResult> create( + const sp& callback, + const MQDescriptorSync& requestChannel, + const MQDescriptorSync& resultChannel, + nn::SharedBurst burstExecutor, std::chrono::microseconds pollingTimeWindow = std::chrono::microseconds{0}); - ExecutionBurstServer(PrivateConstructorTag tag, const sp& callback, - std::unique_ptr requestChannel, - std::unique_ptr resultChannel, - nn::SharedBurst burstExecutor); - ~ExecutionBurstServer(); + Burst(PrivateConstructorTag tag, const sp& callback, + std::unique_ptr requestChannel, + std::unique_ptr resultChannel, + nn::SharedBurst burstExecutor); + ~Burst(); // Used by the NN runtime to preemptively remove any stored memory. See - // IBurstContext::freeMemory for more information. + // V1_2::IBurstContext::freeMemory for more information. Return freeMemory(int32_t slot) override; private: - // Work loop that will continue processing execution requests until the ExecutionBurstServer - // object is freed. + // Work loop that will continue processing execution requests until the Burst object is freed. void task(); - nn::ExecutionResult, Timing>> execute( + nn::ExecutionResult, V1_2::Timing>> execute( const V1_0::Request& requestWithoutPools, const std::vector& slotsOfPools, - MeasureTiming measure); + V1_2::MeasureTiming measure); std::thread mWorker; std::atomic mTeardown{false}; - const sp mCallback; - const std::unique_ptr mRequestChannelReceiver; - const std::unique_ptr mResultChannelSender; + const sp mCallback; + const std::unique_ptr mRequestChannelReceiver; + const std::unique_ptr mResultChannelSender; const nn::SharedBurst mBurstExecutor; MemoryCache mMemoryCache; }; -} // namespace android::hardware::neuralnetworks::V1_2::utils +} // namespace android::hardware::neuralnetworks::adapter -#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_EXECUTION_BURST_SERVER_H +#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_ADAPTER_BURST_H diff --git a/neuralnetworks/1.2/utils/src/ExecutionBurstServer.cpp b/neuralnetworks/utils/adapter/src/Burst.cpp similarity index 67% rename from neuralnetworks/1.2/utils/src/ExecutionBurstServer.cpp rename to neuralnetworks/utils/adapter/src/Burst.cpp index f30b662182..8b2e1dd465 100644 --- a/neuralnetworks/1.2/utils/src/ExecutionBurstServer.cpp +++ b/neuralnetworks/utils/adapter/src/Burst.cpp @@ -14,11 +14,7 @@ * limitations under the License. */ -#define LOG_TAG "ExecutionBurstServer" - -#include "ExecutionBurstServer.h" -#include "Conversions.h" -#include "ExecutionBurstUtils.h" +#include "Burst.h" #include #include @@ -29,6 +25,8 @@ #include #include #include +#include +#include #include #include @@ -42,11 +40,11 @@ #include "Tracing.h" -namespace android::hardware::neuralnetworks::V1_2::utils { +namespace android::hardware::neuralnetworks::adapter { namespace { -constexpr V1_2::Timing kNoTiming = {std::numeric_limits::max(), - std::numeric_limits::max()}; +constexpr V1_2::Timing kTiming = {std::numeric_limits::max(), + std::numeric_limits::max()}; nn::GeneralResult> getMemoriesCallback( V1_0::ErrorStatus status, const hidl_vec& memories) { @@ -61,15 +59,15 @@ nn::GeneralResult> getMemoriesCallback( } // anonymous namespace -ExecutionBurstServer::MemoryCache::MemoryCache(nn::SharedBurst burstExecutor, - sp burstCallback) +Burst::MemoryCache::MemoryCache(nn::SharedBurst burstExecutor, + sp burstCallback) : kBurstExecutor(std::move(burstExecutor)), kBurstCallback(std::move(burstCallback)) { CHECK(kBurstExecutor != nullptr); CHECK(kBurstCallback != nullptr); } nn::GeneralResult>> -ExecutionBurstServer::MemoryCache::getCacheEntries(const std::vector& slots) { +Burst::MemoryCache::getCacheEntries(const std::vector& slots) { std::lock_guard guard(mMutex); NN_TRY(ensureCacheEntriesArePresentLocked(slots)); @@ -82,7 +80,7 @@ ExecutionBurstServer::MemoryCache::getCacheEntries(const std::vector& s return results; } -nn::GeneralResult ExecutionBurstServer::MemoryCache::ensureCacheEntriesArePresentLocked( +nn::GeneralResult Burst::MemoryCache::ensureCacheEntriesArePresentLocked( const std::vector& slots) { const auto slotIsKnown = [this](int32_t slot) REQUIRES(mMutex) { return mCache.count(slot) > 0; }; @@ -107,11 +105,10 @@ nn::GeneralResult ExecutionBurstServer::MemoryCache::ensureCacheEntriesAre auto returnedMemories = NN_TRY(cb.take()); if (returnedMemories.size() != unknownSlots.size()) { - return NN_ERROR() - << "ExecutionBurstServer::MemoryCache::ensureCacheEntriesArePresentLocked: Error " - "retrieving memories -- count mismatch between requested memories (" - << unknownSlots.size() << ") and returned memories (" << returnedMemories.size() - << ")"; + return NN_ERROR() << "Burst::MemoryCache::ensureCacheEntriesArePresentLocked: Error " + "retrieving memories -- count mismatch between requested memories (" + << unknownSlots.size() << ") and returned memories (" + << returnedMemories.size() << ")"; } // add memories to unknown slots @@ -123,56 +120,54 @@ nn::GeneralResult ExecutionBurstServer::MemoryCache::ensureCacheEntriesAre } nn::GeneralResult> -ExecutionBurstServer::MemoryCache::getCacheEntryLocked(int32_t slot) { +Burst::MemoryCache::getCacheEntryLocked(int32_t slot) { if (const auto iter = mCache.find(slot); iter != mCache.end()) { return iter->second; } - return NN_ERROR() - << "ExecutionBurstServer::MemoryCache::getCacheEntryLocked failed because slot " << slot - << " is not present in the cache"; + return NN_ERROR() << "Burst::MemoryCache::getCacheEntryLocked failed because slot " << slot + << " is not present in the cache"; } -void ExecutionBurstServer::MemoryCache::addCacheEntryLocked(int32_t slot, nn::SharedMemory memory) { +void Burst::MemoryCache::addCacheEntryLocked(int32_t slot, nn::SharedMemory memory) { auto hold = kBurstExecutor->cacheMemory(memory); mCache.emplace(slot, std::make_pair(std::move(memory), std::move(hold))); } -void ExecutionBurstServer::MemoryCache::removeCacheEntry(int32_t slot) { +void Burst::MemoryCache::removeCacheEntry(int32_t slot) { std::lock_guard guard(mMutex); mCache.erase(slot); } -// ExecutionBurstServer methods +// Burst methods -nn::GeneralResult> ExecutionBurstServer::create( - const sp& callback, const MQDescriptorSync& requestChannel, - const MQDescriptorSync& resultChannel, nn::SharedBurst burstExecutor, +nn::GeneralResult> Burst::create( + const sp& callback, + const MQDescriptorSync& requestChannel, + const MQDescriptorSync& resultChannel, nn::SharedBurst burstExecutor, std::chrono::microseconds pollingTimeWindow) { // check inputs if (callback == nullptr || burstExecutor == nullptr) { - return NN_ERROR() << "ExecutionBurstServer::create passed a nullptr"; + return NN_ERROR() << "Burst::create passed a nullptr"; } // create FMQ objects auto requestChannelReceiver = - NN_TRY(RequestChannelReceiver::create(requestChannel, pollingTimeWindow)); - auto resultChannelSender = NN_TRY(ResultChannelSender::create(resultChannel)); + NN_TRY(V1_2::utils::RequestChannelReceiver::create(requestChannel, pollingTimeWindow)); + auto resultChannelSender = NN_TRY(V1_2::utils::ResultChannelSender::create(resultChannel)); // check FMQ objects CHECK(requestChannelReceiver != nullptr); CHECK(resultChannelSender != nullptr); // make and return context - return sp::make(PrivateConstructorTag{}, callback, - std::move(requestChannelReceiver), - std::move(resultChannelSender), std::move(burstExecutor)); + return sp::make(PrivateConstructorTag{}, callback, std::move(requestChannelReceiver), + std::move(resultChannelSender), std::move(burstExecutor)); } -ExecutionBurstServer::ExecutionBurstServer(PrivateConstructorTag /*tag*/, - const sp& callback, - std::unique_ptr requestChannel, - std::unique_ptr resultChannel, - nn::SharedBurst burstExecutor) +Burst::Burst(PrivateConstructorTag /*tag*/, const sp& callback, + std::unique_ptr requestChannel, + std::unique_ptr resultChannel, + nn::SharedBurst burstExecutor) : mCallback(callback), mRequestChannelReceiver(std::move(requestChannel)), mResultChannelSender(std::move(resultChannel)), @@ -182,7 +177,7 @@ ExecutionBurstServer::ExecutionBurstServer(PrivateConstructorTag /*tag*/, mWorker = std::thread([this] { task(); }); } -ExecutionBurstServer::~ExecutionBurstServer() { +Burst::~Burst() { // set teardown flag mTeardown = true; mRequestChannelReceiver->invalidate(); @@ -191,12 +186,12 @@ ExecutionBurstServer::~ExecutionBurstServer() { mWorker.join(); } -Return ExecutionBurstServer::freeMemory(int32_t slot) { +Return Burst::freeMemory(int32_t slot) { mMemoryCache.removeCacheEntry(slot); return Void(); } -void ExecutionBurstServer::task() { +void Burst::task() { // loop until the burst object is being destroyed while (!mTeardown) { // receive request @@ -208,12 +203,12 @@ void ExecutionBurstServer::task() { // if the burst is being torn down, skip the execution so the "task" function can end if (!arguments.has_value()) { if (!mTeardown) { - mResultChannelSender->send(V1_0::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming); + mResultChannelSender->send(V1_0::ErrorStatus::GENERAL_FAILURE, {}, kTiming); } continue; } - // unpack the arguments; types are Request, std::vector, and MeasureTiming, + // unpack the arguments; types are Request, std::vector, and V1_2::MeasureTiming, // respectively const auto [requestWithoutPools, slotsOfPools, measure] = std::move(arguments).value(); @@ -226,17 +221,17 @@ void ExecutionBurstServer::task() { } else { const auto& [message, code, outputShapes] = result.error(); LOG(ERROR) << "IBurst::execute failed with " << code << ": " << message; - mResultChannelSender->send(convert(code).value(), convert(outputShapes).value(), - kNoTiming); + mResultChannelSender->send(V1_2::utils::convert(code).value(), + V1_2::utils::convert(outputShapes).value(), kTiming); } } } -nn::ExecutionResult, Timing>> ExecutionBurstServer::execute( +nn::ExecutionResult, V1_2::Timing>> Burst::execute( const V1_0::Request& requestWithoutPools, const std::vector& slotsOfPools, - MeasureTiming measure) { + V1_2::MeasureTiming measure) { NNTRACE_FULL(NNTRACE_LAYER_IPC, NNTRACE_PHASE_EXECUTION, - "ExecutionBurstServer getting memory, executing, and returning results"); + "Burst getting memory, executing, and returning results"); // ensure executor with cache has required memory const auto cacheEntries = NN_TRY(mMemoryCache.getCacheEntries(slotsOfPools)); @@ -257,7 +252,8 @@ nn::ExecutionResult, Timing>> ExecutionBurstServ const auto [outputShapes, timing] = NN_TRY(mBurstExecutor->execute(canonicalRequest, canonicalMeasure, {}, {})); - return std::make_pair(NN_TRY(convert(outputShapes)), NN_TRY(convert(timing))); + return std::make_pair(NN_TRY(V1_2::utils::convert(outputShapes)), + NN_TRY(V1_2::utils::convert(timing))); } -} // namespace android::hardware::neuralnetworks::V1_2::utils +} // namespace android::hardware::neuralnetworks::adapter diff --git a/neuralnetworks/utils/adapter/src/PreparedModel.cpp b/neuralnetworks/utils/adapter/src/PreparedModel.cpp index b03a1317e9..a14e782b9b 100644 --- a/neuralnetworks/utils/adapter/src/PreparedModel.cpp +++ b/neuralnetworks/utils/adapter/src/PreparedModel.cpp @@ -16,6 +16,8 @@ #include "PreparedModel.h" +#include "Burst.h" + #include #include #include @@ -32,7 +34,6 @@ #include #include #include -#include #include #include #include @@ -277,9 +278,8 @@ nn::GeneralResult> configureExecutionBurst( const MQDescriptorSync& requestChannel, const MQDescriptorSync& resultChannel) { auto burstExecutor = NN_TRY(preparedModel->configureExecutionBurst()); - return V1_2::utils::ExecutionBurstServer::create( - callback, requestChannel, resultChannel, std::move(burstExecutor), - V1_2::utils::getBurstServerPollingTimeWindow()); + return Burst::create(callback, requestChannel, resultChannel, std::move(burstExecutor), + V1_2::utils::getBurstServerPollingTimeWindow()); } nn::GeneralResult>> executeFenced(