From b8a26d8f497f92643288a4c519af4d230ede1d7e Mon Sep 17 00:00:00 2001 From: Sadik Armagan Date: Mon, 4 Oct 2021 15:13:11 +0100 Subject: IVGCVSW-6300 'IMemoryOptimizerStrategy Add strategy library and add support in BackendRegistry' * Updated IRuntime interface for providing custom memory optimizer strategy. * Enabled selecting existing memory optimizer strategy by using BackendOptions * Added MemoryOptimizerStrategyLibrary that sets one of the existing memory optimizer strategies selected by user Signed-off-by: Sadik Armagan Change-Id: I037f8ac8efa79c0f71bd63e379101e3ad92d80c9 --- include/armnn/BackendOptions.hpp | 18 ++++++++++++++++++ include/armnn/BackendRegistry.hpp | 7 +++++++ include/armnn/IRuntime.hpp | 14 ++++++++++++++ include/armnn/TypesUtils.hpp | 10 ++++++++++ 4 files changed, 49 insertions(+) (limited to 'include/armnn') diff --git a/include/armnn/BackendOptions.hpp b/include/armnn/BackendOptions.hpp index b8bf8f51f2..33cecf6614 100644 --- a/include/armnn/BackendOptions.hpp +++ b/include/armnn/BackendOptions.hpp @@ -296,4 +296,22 @@ void ParseOptions(const std::vector& options, BackendId backend, } } +inline bool ParseBooleanBackendOption(const armnn::BackendOptions::Var& value, bool defaultValue) +{ + if (value.IsBool()) + { + return value.AsBool(); + } + return defaultValue; +} + +inline std::string ParseStringBackendOption(const armnn::BackendOptions::Var& value, std::string defaultValue) +{ + if (value.IsString()) + { + return value.AsString(); + } + return defaultValue; +} + } //namespace armnn diff --git a/include/armnn/BackendRegistry.hpp b/include/armnn/BackendRegistry.hpp index c13aa9f8b6..0d09607de2 100644 --- a/include/armnn/BackendRegistry.hpp +++ b/include/armnn/BackendRegistry.hpp @@ -8,7 +8,9 @@ #include #include #include +#include +#include #include #include #include @@ -22,6 +24,7 @@ namespace profiling } class IBackendInternal; using IBackendInternalUniquePtr = std::unique_ptr; +using MemoryOptimizerStrategiesMapRef = std::unordered_map>; class BackendRegistry { @@ -38,6 +41,8 @@ public: void SetProfilingService(armnn::Optional profilingService); void RegisterAllocator(const BackendId& id, std::shared_ptr alloc); std::unordered_map> GetAllocators(); + void RegisterMemoryOptimizerStrategy(const BackendId& id, std::shared_ptr strategy); + MemoryOptimizerStrategiesMapRef GetMemoryOptimizerStrategies(); BackendRegistry() {} virtual ~BackendRegistry() {} @@ -54,6 +59,7 @@ public: void Deregister(const BackendId& id); void DeregisterAllocator(const BackendId &id); + void DeregisterMemoryOptimizerStrategy(const BackendId &id); protected: using FactoryStorage = std::unordered_map; @@ -68,6 +74,7 @@ private: FactoryStorage m_Factories; armnn::Optional m_ProfilingService; std::unordered_map> m_CustomMemoryAllocatorMap; + std::unordered_map> m_MemoryOptimizerStrategyMap; }; BackendRegistry& BackendRegistryInstance(); diff --git a/include/armnn/IRuntime.hpp b/include/armnn/IRuntime.hpp index a46830c95a..ca9a0ceec2 100644 --- a/include/armnn/IRuntime.hpp +++ b/include/armnn/IRuntime.hpp @@ -15,6 +15,7 @@ #include "profiling/ILocalPacketHandler.hpp" #include +#include #include #include @@ -106,6 +107,7 @@ public: , m_DynamicBackendsPath("") , m_ProtectedMode(false) , m_CustomAllocatorMap() + , m_MemoryOptimizerStrategyMap() {} /// If set, uses the GpuAcc tuned parameters from the given object when executing GPU workloads. @@ -135,6 +137,14 @@ public: /// @note Only supported for GpuAcc std::map> m_CustomAllocatorMap; + /// @brief A map to define a custom memory optimizer strategy for specific backend Ids. + /// + /// @details A Memory Optimizer Strategy provides a solution to an abstract representation of + /// a network's memory requirements. This can also be used to return a pre-computed solution + /// for a specific network. Set this if you want to implement a Custom Memory Optimizer Strategy + /// for a given backend. + std::map> m_MemoryOptimizerStrategyMap; + struct ExternalProfilingOptions { ExternalProfilingOptions() @@ -168,6 +178,7 @@ public: /// { /// {"TuningLevel", 2}, /// {"TuningFile", filename} + /// {"MemoryOptimizerStrategy", strategyname} /// } /// }); /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -185,6 +196,9 @@ public: /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /// The following backend options are available: + /// AllBackends: + /// "MemoryOptimizerStrategy" : string [stategynameString] + /// (Existing Memory Optimizer Strategies: ConstLayerMemoryOptimizerStrategy) /// GpuAcc: /// "TuningLevel" : int [0..3] (0=UseOnly(default) | 1=RapidTuning | 2=NormalTuning | 3=ExhaustiveTuning) /// "TuningFile" : string [filenameString] diff --git a/include/armnn/TypesUtils.hpp b/include/armnn/TypesUtils.hpp index 9bd9c8148f..d08f592d86 100644 --- a/include/armnn/TypesUtils.hpp +++ b/include/armnn/TypesUtils.hpp @@ -249,6 +249,16 @@ constexpr const char* GetResizeMethodAsCString(ResizeMethod method) } } +constexpr const char* GetMemBlockStrategyTypeName(MemBlockStrategyType memBlockStrategyType) +{ + switch (memBlockStrategyType) + { + case MemBlockStrategyType::SingleAxisPacking: return "SingleAxisPacking"; + case MemBlockStrategyType::MultiAxisPacking: return "MultiAxisPacking"; + default: return "Unknown"; + } +} + template struct IsHalfType : std::integral_constant::value && sizeof(T) == 2> -- cgit v1.2.1