IVGCVSW-6470 Create MemoryStrategyBenchmark
* Refactor the strategy library to be more generic
* Shorten the names of the current strategies
* Change validatorStrat to throw exceptions
Change-Id: I0d9c9ef609b2d8675e5788610d1accac6767c660
Signed-off-by: Finn Williams <finwil01@e127804.cambridge.arm.com>
Signed-off-by: Jim Flynn <jim.flynn@arm.com>
diff --git a/src/backends/backendsCommon/CMakeLists.txt b/src/backends/backendsCommon/CMakeLists.txt
index 90fb376..dfd3501 100644
--- a/src/backends/backendsCommon/CMakeLists.txt
+++ b/src/backends/backendsCommon/CMakeLists.txt
@@ -24,9 +24,6 @@
MemCopyWorkload.hpp
MemImportWorkload.cpp
MemImportWorkload.hpp
- MemoryOptimizerStrategyFactory.hpp
- MemoryOptimizerStrategyLibrary.cpp
- MemoryOptimizerStrategyLibrary.hpp
MemoryManager.cpp
MemoryManager.hpp
MemSyncWorkload.cpp
@@ -51,7 +48,7 @@
WorkloadUtils.hpp
)
-add_subdirectory(memoryOptimizationStrategies)
+add_subdirectory(memoryOptimizerStrategyLibrary)
if(BUILD_UNIT_TESTS)
add_subdirectory(test)
@@ -63,4 +60,4 @@
target_include_directories(armnnBackendsCommon PRIVATE ${PROJECT_SOURCE_DIR}/src/armnnUtils)
target_include_directories(armnnBackendsCommon PRIVATE ${PROJECT_SOURCE_DIR}/src/backends)
target_include_directories(armnnBackendsCommon PRIVATE ${PROJECT_SOURCE_DIR}/src/profiling)
-target_include_directories(armnnBackendsCommon PRIVATE ${PROJECT_SOURCE_DIR}/profiling/common/include)
\ No newline at end of file
+target_include_directories(armnnBackendsCommon PRIVATE ${PROJECT_SOURCE_DIR}/profiling/common/include)
diff --git a/src/backends/backendsCommon/MemoryOptimizerStrategyLibrary.cpp b/src/backends/backendsCommon/MemoryOptimizerStrategyLibrary.cpp
deleted file mode 100644
index 0b48cbd..0000000
--- a/src/backends/backendsCommon/MemoryOptimizerStrategyLibrary.cpp
+++ /dev/null
@@ -1,68 +0,0 @@
-//
-// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
-// SPDX-License-Identifier: MIT
-//
-
-#include "MemoryOptimizerStrategyLibrary.hpp"
-#include "MemoryOptimizerStrategyFactory.hpp"
-
-#include <armnn/BackendHelper.hpp>
-#include <armnn/Logging.hpp>
-#include <armnn/TypesUtils.hpp>
-
-#include <armnn/backends/IMemoryOptimizerStrategy.hpp>
-
-#include <backendsCommon/memoryOptimizationStrategies/ConstLayerMemoryOptimizerStrategy.hpp>
-
-#include <algorithm>
-
-namespace
-{
-// Default Memory Optimizer Strategies
-static const std::vector<std::string> memoryOptimizationStrategies({
- "ConstLayerMemoryOptimizerStrategy",
- });
-
-#define CREATE_MEMORY_OPTIMIZER_STRATEGY(strategyName, memoryOptimizerStrategy) \
-{ \
- MemoryOptimizerStrategyFactory memoryOptimizerStrategyFactory; \
- memoryOptimizerStrategy = memoryOptimizerStrategyFactory.CreateMemoryOptimizerStrategy<strategyName>(); \
-} \
-
-} // anonymous namespace
-
-namespace armnn
-{
-
-bool MemoryOptimizerStrategyLibrary::SetMemoryOptimizerStrategy(const BackendId& id, const std::string& strategyName)
-{
- auto isStrategyExist = std::find(memoryOptimizationStrategies.begin(),
- memoryOptimizationStrategies.end(),
- strategyName) != memoryOptimizationStrategies.end();
- if (isStrategyExist)
- {
- std::shared_ptr<IMemoryOptimizerStrategy> memoryOptimizerStrategy = nullptr;
- CREATE_MEMORY_OPTIMIZER_STRATEGY(armnn::ConstLayerMemoryOptimizerStrategy,
- memoryOptimizerStrategy);
- if (memoryOptimizerStrategy)
- {
- using BackendCapability = BackendOptions::BackendOption;
- auto strategyType = GetMemBlockStrategyTypeName(memoryOptimizerStrategy->GetMemBlockStrategyType());
- BackendCapability memOptimizeStrategyCapability {strategyType, true};
- if (HasCapability(memOptimizeStrategyCapability, id))
- {
- BackendRegistryInstance().RegisterMemoryOptimizerStrategy(id, memoryOptimizerStrategy);
- return true;
- }
- // reset shared_ptr memoryOptimizerStrategy
- memoryOptimizerStrategy.reset();
- }
- }
- ARMNN_LOG(warning) << "Backend "
- << id
- << " is not registered as does not support memory optimizer strategy "
- << strategyName << " \n";
- return false;
-}
-
-} // namespace armnn
\ No newline at end of file
diff --git a/src/backends/backendsCommon/MemoryOptimizerStrategyLibrary.hpp b/src/backends/backendsCommon/MemoryOptimizerStrategyLibrary.hpp
deleted file mode 100644
index 795fc64..0000000
--- a/src/backends/backendsCommon/MemoryOptimizerStrategyLibrary.hpp
+++ /dev/null
@@ -1,21 +0,0 @@
-//
-// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
-// SPDX-License-Identifier: MIT
-//
-#pragma once
-
-#include <armnn/BackendRegistry.hpp>
-
-namespace armnn
-{
-
-class MemoryOptimizerStrategyLibrary
-{
-public:
- MemoryOptimizerStrategyLibrary() = default;
-
- bool SetMemoryOptimizerStrategy(const BackendId& id, const std::string& strategyName);
-
-};
-
-} // namespace armnn
\ No newline at end of file
diff --git a/src/backends/backendsCommon/common.mk b/src/backends/backendsCommon/common.mk
index 910f9a4..f90a7c8 100644
--- a/src/backends/backendsCommon/common.mk
+++ b/src/backends/backendsCommon/common.mk
@@ -17,7 +17,6 @@
MapWorkload.cpp \
MemCopyWorkload.cpp \
MemImportWorkload.cpp \
- MemoryOptimizerStrategyLibrary.cpp \
MemSyncWorkload.cpp \
OptimizationViews.cpp \
TensorHandleFactoryRegistry.cpp \
@@ -25,8 +24,9 @@
WorkloadData.cpp \
WorkloadFactory.cpp \
WorkloadUtils.cpp \
- memoryOptimizationStrategies/ConstLayerMemoryOptimizerStrategy.cpp \
- memoryOptimizationStrategies/MemoryOptimizerStrategyValidator.cpp
+ memoryOptimizerStrategyLibrary/strategies/ConstantMemoryStrategy.cpp \
+ memoryOptimizerStrategyLibrary/strategies/StrategyValidator.cpp \
+
# COMMON_TEST_SOURCES contains the list of files to be included
# in the Android unit test build (armnn-tests) and it is picked
@@ -102,8 +102,8 @@
test/layerTests/SubtractionTestImpl.cpp \
test/layerTests/TransposeConvolution2dTestImpl.cpp \
test/layerTests/UnidirectionalSequenceLstmTestImpl.cpp \
- memoryOptimizationStrategies/test/ConstLayerMemoryOptimizerStrategyTests.cpp \
- memoryOptimizationStrategies/test/MemoryOptimizerStrategyValidatorTests.cpp
+ memoryOptimizerStrategyLibrary/test/ConstMemoryStrategyTests.cpp \
+ memoryOptimizerStrategyLibrary/test/ValidatorStrategyTests.cpp
ifeq ($(ARMNN_REF_ENABLED),1)
COMMON_TEST_SOURCES += \
diff --git a/src/backends/backendsCommon/memoryOptimizationStrategies/CMakeLists.txt b/src/backends/backendsCommon/memoryOptimizationStrategies/CMakeLists.txt
deleted file mode 100644
index de83505..0000000
--- a/src/backends/backendsCommon/memoryOptimizationStrategies/CMakeLists.txt
+++ /dev/null
@@ -1,23 +0,0 @@
-#
-# Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
-# SPDX-License-Identifier: MIT
-#
-
-list(APPEND armnnMemoryOptimizationStrategies_sources
- ConstLayerMemoryOptimizerStrategy.hpp
- ConstLayerMemoryOptimizerStrategy.cpp
- MemoryOptimizerStrategyValidator.hpp
- MemoryOptimizerStrategyValidator.cpp
-)
-
-if(BUILD_UNIT_TESTS)
- add_subdirectory(test)
-endif()
-
-add_library(armnnMemoryOptimizationStrategies OBJECT ${armnnMemoryOptimizationStrategies_sources})
-target_include_directories(armnnMemoryOptimizationStrategies PRIVATE ${PROJECT_SOURCE_DIR}/include/armnn)
-target_include_directories(armnnMemoryOptimizationStrategies PRIVATE ${PROJECT_SOURCE_DIR}/src/armnn)
-target_include_directories(armnnMemoryOptimizationStrategies PRIVATE ${PROJECT_SOURCE_DIR}/src/armnnUtils)
-target_include_directories(armnnMemoryOptimizationStrategies PRIVATE ${PROJECT_SOURCE_DIR}/src/backends)
-target_include_directories(armnnMemoryOptimizationStrategies PRIVATE ${PROJECT_SOURCE_DIR}/src/profiling)
-target_include_directories(armnnMemoryOptimizationStrategies PRIVATE ${PROJECT_SOURCE_DIR}/profiling/common/include)
\ No newline at end of file
diff --git a/src/backends/backendsCommon/memoryOptimizationStrategies/MemoryOptimizerStrategyValidator.hpp b/src/backends/backendsCommon/memoryOptimizationStrategies/MemoryOptimizerStrategyValidator.hpp
deleted file mode 100644
index 13e9059..0000000
--- a/src/backends/backendsCommon/memoryOptimizationStrategies/MemoryOptimizerStrategyValidator.hpp
+++ /dev/null
@@ -1,27 +0,0 @@
-//
-// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
-// SPDX-License-Identifier: MIT
-//
-#pragma once
-
-#include <armnn/Types.hpp>
-#include <armnn/backends/IMemoryOptimizerStrategy.hpp>
-
-namespace armnn
-{
-
-class MemoryOptimizerValidator
-{
-public:
- explicit MemoryOptimizerValidator(std::shared_ptr<IMemoryOptimizerStrategy> strategy)
- : m_Strategy(strategy)
- {
- };
-
- bool Validate(std::vector<MemBlock>& memBlocks);
-
-private:
- std::shared_ptr<IMemoryOptimizerStrategy> m_Strategy;
-};
-
-} // namespace armnn
\ No newline at end of file
diff --git a/src/backends/backendsCommon/memoryOptimizationStrategies/test/CMakeLists.txt b/src/backends/backendsCommon/memoryOptimizationStrategies/test/CMakeLists.txt
deleted file mode 100644
index 1e16f09..0000000
--- a/src/backends/backendsCommon/memoryOptimizationStrategies/test/CMakeLists.txt
+++ /dev/null
@@ -1,17 +0,0 @@
-#
-# Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
-# SPDX-License-Identifier: MIT
-#
-
-list(APPEND armnnMemoryOptimizationStrategiesUnitTests_sources
- ConstLayerMemoryOptimizerStrategyTests.cpp
- MemoryOptimizerStrategyValidatorTests.cpp
-)
-
-add_library(armnnMemoryOptimizationStrategiesUnitTests OBJECT ${armnnMemoryOptimizationStrategiesUnitTests_sources})
-target_include_directories(armnnMemoryOptimizationStrategiesUnitTests PRIVATE ${PROJECT_SOURCE_DIR}/src/armnn)
-target_include_directories(armnnMemoryOptimizationStrategiesUnitTests PRIVATE ${PROJECT_SOURCE_DIR}/src/armnnUtils)
-target_include_directories(armnnMemoryOptimizationStrategiesUnitTests PRIVATE ${PROJECT_SOURCE_DIR}/src/backends)
-target_include_directories(armnnMemoryOptimizationStrategiesUnitTests PRIVATE ${PROJECT_SOURCE_DIR}/src/profiling)
-target_include_directories(armnnMemoryOptimizationStrategiesUnitTests PRIVATE ${PROJECT_SOURCE_DIR}/profiling/common/include)
-target_include_directories(armnnMemoryOptimizationStrategiesUnitTests PRIVATE ${PROJECT_SOURCE_DIR}/third-party)
diff --git a/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/CMakeLists.txt b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/CMakeLists.txt
new file mode 100644
index 0000000..43ec9db
--- /dev/null
+++ b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/CMakeLists.txt
@@ -0,0 +1,19 @@
+#
+# Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
+# SPDX-License-Identifier: MIT
+#
+
+list(APPEND armnnMemoryOptimizationStrategies_sources
+ MemoryOptimizerStrategyLibrary.hpp
+ MemoryOptimizerStrategyFactory.hpp
+ strategies/ConstantMemoryStrategy.hpp
+ strategies/ConstantMemoryStrategy.cpp
+ strategies/StrategyValidator.hpp
+ strategies/StrategyValidator.cpp
+)
+
+if(BUILD_UNIT_TESTS)
+ add_subdirectory(test)
+endif()
+
+add_library(armnnMemoryOptimizationStrategies OBJECT ${armnnMemoryOptimizationStrategies_sources})
\ No newline at end of file
diff --git a/src/backends/backendsCommon/MemoryOptimizerStrategyFactory.hpp b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/MemoryOptimizerStrategyFactory.hpp
similarity index 78%
rename from src/backends/backendsCommon/MemoryOptimizerStrategyFactory.hpp
rename to src/backends/backendsCommon/memoryOptimizerStrategyLibrary/MemoryOptimizerStrategyFactory.hpp
index 62a2182..aff0995 100644
--- a/src/backends/backendsCommon/MemoryOptimizerStrategyFactory.hpp
+++ b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/MemoryOptimizerStrategyFactory.hpp
@@ -18,9 +18,9 @@
MemoryOptimizerStrategyFactory() {}
template <typename T>
- std::shared_ptr<IMemoryOptimizerStrategy> CreateMemoryOptimizerStrategy()
+ std::unique_ptr<IMemoryOptimizerStrategy> CreateMemoryOptimizerStrategy()
{
- return std::make_shared<T>();
+ return std::make_unique<T>();
}
};
diff --git a/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/MemoryOptimizerStrategyLibrary.hpp b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/MemoryOptimizerStrategyLibrary.hpp
new file mode 100644
index 0000000..5e20a9f
--- /dev/null
+++ b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/MemoryOptimizerStrategyLibrary.hpp
@@ -0,0 +1,52 @@
+//
+// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+#pragma once
+
+#include <armnn/backends/IMemoryOptimizerStrategy.hpp>
+#include "MemoryOptimizerStrategyFactory.hpp"
+#include <algorithm>
+
+#include "strategies/ConstantMemoryStrategy.hpp"
+#include "strategies/StrategyValidator.hpp"
+
+namespace
+{
+// Default Memory Optimizer Strategies
+static const std::vector<std::string> memoryOptimizationStrategies(
+{
+ "ConstantMemoryStrategy",
+ "StrategyValidator"
+});
+
+#define CREATE_MEMORY_OPTIMIZER_STRATEGY(strategyName, memoryOptimizerStrategy) \
+{ \
+ MemoryOptimizerStrategyFactory memoryOptimizerStrategyFactory; \
+ memoryOptimizerStrategy = memoryOptimizerStrategyFactory.CreateMemoryOptimizerStrategy<strategyName>(); \
+} \
+
+} // anonymous namespace
+namespace armnn
+{
+ std::unique_ptr<IMemoryOptimizerStrategy> GetMemoryOptimizerStrategy(const std::string& strategyName)
+ {
+ auto doesStrategyExist = std::find(memoryOptimizationStrategies.begin(),
+ memoryOptimizationStrategies.end(),
+ strategyName) != memoryOptimizationStrategies.end();
+ if (doesStrategyExist)
+ {
+ std::unique_ptr<IMemoryOptimizerStrategy> memoryOptimizerStrategy = nullptr;
+ CREATE_MEMORY_OPTIMIZER_STRATEGY(armnn::ConstantMemoryStrategy,
+ memoryOptimizerStrategy);
+ return memoryOptimizerStrategy;
+ }
+ return nullptr;
+ }
+
+
+ const std::vector<std::string>& GetMemoryOptimizerStrategyNames()
+ {
+ return memoryOptimizationStrategies;
+ }
+} // namespace armnn
\ No newline at end of file
diff --git a/src/backends/backendsCommon/memoryOptimizationStrategies/ConstLayerMemoryOptimizerStrategy.cpp b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/strategies/ConstantMemoryStrategy.cpp
similarity index 72%
rename from src/backends/backendsCommon/memoryOptimizationStrategies/ConstLayerMemoryOptimizerStrategy.cpp
rename to src/backends/backendsCommon/memoryOptimizerStrategyLibrary/strategies/ConstantMemoryStrategy.cpp
index 023c636..55f7f89 100644
--- a/src/backends/backendsCommon/memoryOptimizationStrategies/ConstLayerMemoryOptimizerStrategy.cpp
+++ b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/strategies/ConstantMemoryStrategy.cpp
@@ -3,17 +3,17 @@
// SPDX-License-Identifier: MIT
//
-#include "ConstLayerMemoryOptimizerStrategy.hpp"
+#include "ConstantMemoryStrategy.hpp"
namespace armnn
{
-std::string ConstLayerMemoryOptimizerStrategy::GetName() const
+std::string ConstantMemoryStrategy::GetName() const
{
return m_Name;
}
-MemBlockStrategyType ConstLayerMemoryOptimizerStrategy::GetMemBlockStrategyType() const
+MemBlockStrategyType ConstantMemoryStrategy::GetMemBlockStrategyType() const
{
return m_MemBlockStrategyType;
}
@@ -22,7 +22,7 @@
// 1: All MemBlocks have been assigned to a MemBin
// 2: No MemBlock is assigned to multiple MemBins
// 3: No two Memblocks in a MemBin overlap in both the X and Y axis
-std::vector<MemBin> ConstLayerMemoryOptimizerStrategy::Optimize(std::vector<MemBlock>& memBlocks)
+std::vector<MemBin> ConstantMemoryStrategy::Optimize(std::vector<MemBlock>& memBlocks)
{
std::vector<MemBin> memBins;
memBins.reserve(memBlocks.size());
diff --git a/src/backends/backendsCommon/memoryOptimizationStrategies/ConstLayerMemoryOptimizerStrategy.hpp b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/strategies/ConstantMemoryStrategy.hpp
similarity index 79%
rename from src/backends/backendsCommon/memoryOptimizationStrategies/ConstLayerMemoryOptimizerStrategy.hpp
rename to src/backends/backendsCommon/memoryOptimizerStrategyLibrary/strategies/ConstantMemoryStrategy.hpp
index 3f803ab..249c133 100644
--- a/src/backends/backendsCommon/memoryOptimizationStrategies/ConstLayerMemoryOptimizerStrategy.hpp
+++ b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/strategies/ConstantMemoryStrategy.hpp
@@ -10,11 +10,11 @@
namespace armnn
{
// ConstLayerMemoryOptimizer: Create a unique MemBin for each MemBlock and assign it an offset of 0
-class ConstLayerMemoryOptimizerStrategy : public IMemoryOptimizerStrategy
+class ConstantMemoryStrategy : public IMemoryOptimizerStrategy
{
public:
- ConstLayerMemoryOptimizerStrategy()
- : m_Name(std::string("ConstLayerMemoryOptimizerStrategy"))
+ ConstantMemoryStrategy()
+ : m_Name(std::string("ConstantMemoryStrategy"))
, m_MemBlockStrategyType(MemBlockStrategyType::SingleAxisPacking) {}
std::string GetName() const override;
diff --git a/src/backends/backendsCommon/memoryOptimizationStrategies/MemoryOptimizerStrategyValidator.cpp b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/strategies/StrategyValidator.cpp
similarity index 71%
rename from src/backends/backendsCommon/memoryOptimizationStrategies/MemoryOptimizerStrategyValidator.cpp
rename to src/backends/backendsCommon/memoryOptimizerStrategyLibrary/strategies/StrategyValidator.cpp
index 876ad90..48cdfb0 100644
--- a/src/backends/backendsCommon/memoryOptimizationStrategies/MemoryOptimizerStrategyValidator.cpp
+++ b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/strategies/StrategyValidator.cpp
@@ -3,12 +3,14 @@
// SPDX-License-Identifier: MIT
//
-#include <backendsCommon/memoryOptimizationStrategies/MemoryOptimizerStrategyValidator.hpp>
+#include <unordered_map>
+#include <iostream>
+#include "StrategyValidator.hpp"
namespace armnn
{
-bool MemoryOptimizerValidator::Validate(std::vector<MemBlock>& memBlocks)
+std::vector<MemBin> StrategyValidator::Optimize(std::vector<MemBlock>& memBlocks)
{
// Condition #1: All Memblocks have been assigned to a MemBin
@@ -18,36 +20,46 @@
// Memblocks in a MemBin can overlap on the X axis for SingleAxisPacking
// Memblocks in a MemBin can overlap on the Y axis or the X for MultiAxisPacking but not both
+ std::unordered_map<unsigned int, bool> validationMap;
+
+ for (auto memBlock : memBlocks)
+ {
+ validationMap[memBlock.m_Index] = false;
+ }
+
auto memBinVect = m_Strategy->Optimize(memBlocks);
// Compare each of the input memblocks against every assignedBlock in each bin
// if we get through all bins without finding a block return
// if at any stage the block is found twice return
- for (auto memBlock : memBlocks)
+ for (auto memBin : memBinVect)
{
- auto found = false;
-
- for (auto bin : memBinVect)
+ for (auto block : memBin.m_MemBlocks)
{
- for (auto assignedBlock : bin.m_MemBlocks)
+ try
{
- if (memBlock.m_Index == assignedBlock.m_Index)
+ if (!validationMap.at(block.m_Index))
{
- if (found)
- {
- // Condition #2: Memblock is assigned to multiple MemBins
- return false;
- }
-
- found = true;
+ validationMap.at(block.m_Index) = true;
+ }
+ else
+ {
+ throw MemoryValidationException("Condition #2: Memblock is assigned to multiple MemBins");
}
}
+ catch (const std::out_of_range&)
+ {
+ throw MemoryValidationException("Unknown index ");
+ }
}
- // Condition #1: Block not found in any bin so return false as strategy is invalid
- if (!found)
+ }
+
+ for (auto memBlock : memBlocks)
+ {
+ if (!validationMap.at(memBlock.m_Index))
{
- return false;
+ throw MemoryValidationException("Condition #1: Block not found in any bin");
}
}
@@ -84,7 +96,7 @@
(assignedIndex != otherIndex))
{
// Condition #3: two Memblocks overlap on both the X and Y axis
- return false;
+ throw MemoryValidationException("Condition #3: two Memblocks overlap on both the X and Y axis");
}
switch (m_Strategy->GetMemBlockStrategyType())
@@ -96,8 +108,9 @@
// Cant overlap with itself
(assignedIndex != otherIndex))
{
- // Condition #3: invalid as two Memblocks overlap on the Y axis for SingleAxisPacking
- return false;
+ throw MemoryValidationException("Condition #3: "
+ "invalid as two Memblocks overlap on the Y axis for SingleAxisPacking");
+
}
break;
}
@@ -106,16 +119,14 @@
break;
}
default:
- // Unknown MemBlockStrategyType
- return false;
+ throw MemoryValidationException("Unknown MemBlockStrategyType");
}
}
-
}
}
// None of the conditions broken so return true
- return true;
+ return memBinVect;
}
} // namespace armnn
\ No newline at end of file
diff --git a/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/strategies/StrategyValidator.hpp b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/strategies/StrategyValidator.hpp
new file mode 100644
index 0000000..e1f9111
--- /dev/null
+++ b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/strategies/StrategyValidator.hpp
@@ -0,0 +1,41 @@
+//
+// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
+// SPDX-License-Identifier: MIT
+//
+#pragma once
+
+#include <armnn/Types.hpp>
+#include <armnn/Exceptions.hpp>
+#include <armnn/backends/IMemoryOptimizerStrategy.hpp>
+
+namespace armnn
+{
+
+class StrategyValidator : public IMemoryOptimizerStrategy
+{
+public:
+
+ void SetStrategy(std::shared_ptr<IMemoryOptimizerStrategy> strategy)
+ {
+ m_Strategy = strategy;
+ m_MemBlockStrategyType = strategy->GetMemBlockStrategyType();
+ }
+
+ std::string GetName() const override
+ {
+ return "StrategyValidator";
+ }
+
+ MemBlockStrategyType GetMemBlockStrategyType() const override
+ {
+ return m_MemBlockStrategyType;
+ }
+
+ std::vector<MemBin> Optimize(std::vector<MemBlock>& memBlocks) override;
+
+private:
+ std::shared_ptr<IMemoryOptimizerStrategy> m_Strategy;
+ MemBlockStrategyType m_MemBlockStrategyType;
+};
+
+} // namespace armnn
\ No newline at end of file
diff --git a/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/CMakeLists.txt b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/CMakeLists.txt
new file mode 100644
index 0000000..b96782a
--- /dev/null
+++ b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/CMakeLists.txt
@@ -0,0 +1,12 @@
+#
+# Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
+# SPDX-License-Identifier: MIT
+#
+
+list(APPEND armnnMemoryOptimizationStrategiesUnitTests_sources
+ ConstMemoryStrategyTests.cpp
+ ValidatorStrategyTests.cpp
+)
+
+add_library(armnnMemoryOptimizationStrategiesUnitTests OBJECT ${armnnMemoryOptimizationStrategiesUnitTests_sources})
+target_include_directories(armnnMemoryOptimizationStrategiesUnitTests PRIVATE ${PROJECT_SOURCE_DIR}/src/backends)
diff --git a/src/backends/backendsCommon/memoryOptimizationStrategies/test/ConstLayerMemoryOptimizerStrategyTests.cpp b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/ConstMemoryStrategyTests.cpp
similarity index 72%
rename from src/backends/backendsCommon/memoryOptimizationStrategies/test/ConstLayerMemoryOptimizerStrategyTests.cpp
rename to src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/ConstMemoryStrategyTests.cpp
index cfa2d40..64312f3 100644
--- a/src/backends/backendsCommon/memoryOptimizationStrategies/test/ConstLayerMemoryOptimizerStrategyTests.cpp
+++ b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/ConstMemoryStrategyTests.cpp
@@ -3,18 +3,18 @@
// SPDX-License-Identifier: MIT
//
-#include <backendsCommon/memoryOptimizationStrategies/ConstLayerMemoryOptimizerStrategy.hpp>
-#include <backendsCommon/memoryOptimizationStrategies/MemoryOptimizerStrategyValidator.hpp>
+#include <backendsCommon/memoryOptimizerStrategyLibrary/strategies/ConstantMemoryStrategy.hpp>
+#include <backendsCommon/memoryOptimizerStrategyLibrary/strategies/StrategyValidator.hpp>
#include <doctest/doctest.h>
#include <vector>
using namespace armnn;
-TEST_SUITE("ConstLayerMemoryOptimizerStrategyTestSuite")
+TEST_SUITE("ConstMemoryStrategyTestSuite")
{
-TEST_CASE("ConstLayerMemoryOptimizerStrategyTest")
+TEST_CASE("ConstMemoryStrategyTest")
{
// create a few memory blocks
MemBlock memBlock0(0, 2, 20, 0, 0);
@@ -31,9 +31,9 @@
memBlocks.push_back(memBlock3);
memBlocks.push_back(memBlock4);
- // Optimize the memory blocks with ConstLayerMemoryOptimizerStrategy
- ConstLayerMemoryOptimizerStrategy constLayerMemoryOptimizerStrategy;
- CHECK_EQ(constLayerMemoryOptimizerStrategy.GetName(), std::string("ConstLayerMemoryOptimizerStrategy"));
+ // Optimize the memory blocks with ConstantMemoryStrategy
+ ConstantMemoryStrategy constLayerMemoryOptimizerStrategy;
+ CHECK_EQ(constLayerMemoryOptimizerStrategy.GetName(), std::string("ConstantMemoryStrategy"));
CHECK_EQ(constLayerMemoryOptimizerStrategy.GetMemBlockStrategyType(), MemBlockStrategyType::SingleAxisPacking);
auto memBins = constLayerMemoryOptimizerStrategy.Optimize(memBlocks);
CHECK(memBins.size() == 5);
@@ -67,11 +67,11 @@
memBlocks.push_back(memBlock4);
// Optimize the memory blocks with ConstLayerMemoryOptimizerStrategy
- ConstLayerMemoryOptimizerStrategy constLayerMemoryOptimizerStrategy;
- auto ptr = std::make_shared<ConstLayerMemoryOptimizerStrategy>(constLayerMemoryOptimizerStrategy);
- MemoryOptimizerValidator validator(std::move(ptr));
+ auto ptr = std::make_shared<ConstantMemoryStrategy>();
+ StrategyValidator validator;
+ validator.SetStrategy(ptr);
// Ensure ConstLayerMemoryOptimizerStrategy is valid
- CHECK(validator.Validate(memBlocks));
+ CHECK_NOTHROW(validator.Optimize(memBlocks));
}
}
diff --git a/src/backends/backendsCommon/memoryOptimizationStrategies/test/MemoryOptimizerStrategyValidatorTests.cpp b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/ValidatorStrategyTests.cpp
similarity index 86%
rename from src/backends/backendsCommon/memoryOptimizationStrategies/test/MemoryOptimizerStrategyValidatorTests.cpp
rename to src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/ValidatorStrategyTests.cpp
index af09e4e..bc04105 100644
--- a/src/backends/backendsCommon/memoryOptimizationStrategies/test/MemoryOptimizerStrategyValidatorTests.cpp
+++ b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/ValidatorStrategyTests.cpp
@@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT
//
-#include <backendsCommon/memoryOptimizationStrategies/MemoryOptimizerStrategyValidator.hpp>
+#include <backendsCommon/memoryOptimizerStrategyLibrary/strategies/StrategyValidator.hpp>
#include <doctest/doctest.h>
#include <vector>
@@ -75,16 +75,18 @@
// Optimize the memory blocks with TestMemoryOptimizerStrategySingle
TestMemoryOptimizerStrategy testMemoryOptimizerStrategySingle(MemBlockStrategyType::SingleAxisPacking);
auto ptr = std::make_shared<TestMemoryOptimizerStrategy>(testMemoryOptimizerStrategySingle);
- MemoryOptimizerValidator validator(std::move(ptr));
+ StrategyValidator validator;
+ validator.SetStrategy(ptr);
// SingleAxisPacking can overlap on X axis.
- CHECK(validator.Validate(memBlocks));
+ CHECK_NOTHROW(validator.Optimize(memBlocks));
// Optimize the memory blocks with TestMemoryOptimizerStrategyMulti
TestMemoryOptimizerStrategy testMemoryOptimizerStrategyMulti(MemBlockStrategyType::MultiAxisPacking);
auto ptrMulti = std::make_shared<TestMemoryOptimizerStrategy>(testMemoryOptimizerStrategyMulti);
- MemoryOptimizerValidator validatorMulti(std::move(ptrMulti));
+ StrategyValidator validatorMulti;
+ validatorMulti.SetStrategy(ptrMulti);
// MultiAxisPacking can overlap on X axis.
- CHECK(validatorMulti.Validate(memBlocks));
+ CHECK_NOTHROW(validatorMulti.Optimize(memBlocks));
}
TEST_CASE("MemoryOptimizerStrategyValidatorTestOverlapXAndY")
@@ -107,16 +109,18 @@
// Optimize the memory blocks with TestMemoryOptimizerStrategySingle
TestMemoryOptimizerStrategy testMemoryOptimizerStrategySingle(MemBlockStrategyType::SingleAxisPacking);
auto ptr = std::make_shared<TestMemoryOptimizerStrategy>(testMemoryOptimizerStrategySingle);
- MemoryOptimizerValidator validator(std::move(ptr));
+ StrategyValidator validator;
+ validator.SetStrategy(ptr);
// SingleAxisPacking cannot overlap on both X and Y axis.
- CHECK(!validator.Validate(memBlocks));
+ CHECK_THROWS(validator.Optimize(memBlocks));
// Optimize the memory blocks with TestMemoryOptimizerStrategyMulti
TestMemoryOptimizerStrategy testMemoryOptimizerStrategyMulti(MemBlockStrategyType::MultiAxisPacking);
auto ptrMulti = std::make_shared<TestMemoryOptimizerStrategy>(testMemoryOptimizerStrategyMulti);
- MemoryOptimizerValidator validatorMulti(std::move(ptrMulti));
+ StrategyValidator validatorMulti;
+ validatorMulti.SetStrategy(ptrMulti);
// MultiAxisPacking cannot overlap on both X and Y axis.
- CHECK(!validatorMulti.Validate(memBlocks));
+ CHECK_THROWS(validatorMulti.Optimize(memBlocks));
}
TEST_CASE("MemoryOptimizerStrategyValidatorTestOverlapY")
@@ -139,16 +143,18 @@
// Optimize the memory blocks with TestMemoryOptimizerStrategySingle
TestMemoryOptimizerStrategy testMemoryOptimizerStrategySingle(MemBlockStrategyType::SingleAxisPacking);
auto ptr = std::make_shared<TestMemoryOptimizerStrategy>(testMemoryOptimizerStrategySingle);
- MemoryOptimizerValidator validator(std::move(ptr));
+ StrategyValidator validator;
+ validator.SetStrategy(ptr);
// SingleAxisPacking cannot overlap on Y axis
- CHECK(!validator.Validate(memBlocks));
+ CHECK_THROWS(validator.Optimize(memBlocks));
// Optimize the memory blocks with TestMemoryOptimizerStrategyMulti
TestMemoryOptimizerStrategy testMemoryOptimizerStrategyMulti(MemBlockStrategyType::MultiAxisPacking);
auto ptrMulti = std::make_shared<TestMemoryOptimizerStrategy>(testMemoryOptimizerStrategyMulti);
- MemoryOptimizerValidator validatorMulti(std::move(ptrMulti));
+ StrategyValidator validatorMulti;
+ validatorMulti.SetStrategy(ptrMulti);
// MultiAxisPacking can overlap on Y axis
- CHECK(validatorMulti.Validate(memBlocks));
+ CHECK_NOTHROW(validatorMulti.Optimize(memBlocks));
}
// TestMemoryOptimizerStrategyDuplicate: Create a MemBin and put all blocks in it duplicating each so validator
@@ -201,14 +207,16 @@
// Duplicate strategy is invalid as same block is found twice
TestMemoryOptimizerStrategyDuplicate testMemoryOptimizerStrategySingle(MemBlockStrategyType::SingleAxisPacking);
auto ptr = std::make_shared<TestMemoryOptimizerStrategyDuplicate>(testMemoryOptimizerStrategySingle);
- MemoryOptimizerValidator validator(std::move(ptr));
- CHECK(!validator.Validate(memBlocks));
+ StrategyValidator validator;
+ validator.SetStrategy(ptr);
+ CHECK_THROWS(validator.Optimize(memBlocks));
// Optimize the memory blocks with TestMemoryOptimizerStrategyMulti
TestMemoryOptimizerStrategyDuplicate testMemoryOptimizerStrategyMulti(MemBlockStrategyType::MultiAxisPacking);
auto ptrMulti = std::make_shared<TestMemoryOptimizerStrategyDuplicate>(testMemoryOptimizerStrategyMulti);
- MemoryOptimizerValidator validatorMulti(std::move(ptrMulti));
- CHECK(!validatorMulti.Validate(memBlocks));
+ StrategyValidator validatorMulti;
+ validatorMulti.SetStrategy(ptrMulti);
+ CHECK_THROWS(validatorMulti.Optimize(memBlocks));
}
// TestMemoryOptimizerStrategySkip: Create a MemBin and put all blocks in it skipping every other block so validator
@@ -260,14 +268,16 @@
// Skip strategy is invalid as every second block is not found
TestMemoryOptimizerStrategySkip testMemoryOptimizerStrategySingle(MemBlockStrategyType::SingleAxisPacking);
auto ptr = std::make_shared<TestMemoryOptimizerStrategySkip>(testMemoryOptimizerStrategySingle);
- MemoryOptimizerValidator validator(std::move(ptr));
- CHECK(!validator.Validate(memBlocks));
+ StrategyValidator validator;
+ validator.SetStrategy(ptr);
+ CHECK_THROWS(validator.Optimize(memBlocks));
// Optimize the memory blocks with TestMemoryOptimizerStrategyMulti
TestMemoryOptimizerStrategySkip testMemoryOptimizerStrategyMulti(MemBlockStrategyType::MultiAxisPacking);
auto ptrMulti = std::make_shared<TestMemoryOptimizerStrategySkip>(testMemoryOptimizerStrategyMulti);
- MemoryOptimizerValidator validatorMulti(std::move(ptrMulti));
- CHECK(!validatorMulti.Validate(memBlocks));
+ StrategyValidator validatorMulti;
+ validatorMulti.SetStrategy(ptrMulti);
+ CHECK_THROWS(validatorMulti.Optimize(memBlocks));
}
}
diff --git a/src/backends/backendsCommon/test/BackendRegistryTests.cpp b/src/backends/backendsCommon/test/BackendRegistryTests.cpp
index 5acc61f..18b3745 100644
--- a/src/backends/backendsCommon/test/BackendRegistryTests.cpp
+++ b/src/backends/backendsCommon/test/BackendRegistryTests.cpp
@@ -7,7 +7,7 @@
#include <armnn/BackendRegistry.hpp>
#include <armnn/backends/IBackendInternal.hpp>
-#include <backendsCommon/memoryOptimizationStrategies/ConstLayerMemoryOptimizerStrategy.hpp>
+#include <backendsCommon/memoryOptimizerStrategyLibrary/strategies/ConstantMemoryStrategy.hpp>
#include <reference/RefBackend.hpp>
#include <doctest/doctest.h>
@@ -156,7 +156,7 @@
// Register the memory optimizer
std::shared_ptr<IMemoryOptimizerStrategy> memoryOptimizerStrategy =
- std::make_shared<ConstLayerMemoryOptimizerStrategy>();
+ std::make_shared<ConstantMemoryStrategy>();
BackendRegistryInstance().RegisterMemoryOptimizerStrategy(cpuRefBackendId, memoryOptimizerStrategy);
CHECK(!BackendRegistryInstance().GetMemoryOptimizerStrategies().empty());
CHECK(BackendRegistryInstance().GetMemoryOptimizerStrategies().size() == 1);
diff --git a/src/backends/backendsCommon/test/CustomMemoryOptimizerStrategyTests.cpp b/src/backends/backendsCommon/test/CustomMemoryOptimizerStrategyTests.cpp
index a5bf17c..11eae04 100644
--- a/src/backends/backendsCommon/test/CustomMemoryOptimizerStrategyTests.cpp
+++ b/src/backends/backendsCommon/test/CustomMemoryOptimizerStrategyTests.cpp
@@ -80,18 +80,18 @@
options.m_BackendOptions.emplace_back(
BackendOptions{"CpuRef",
{
- {"MemoryOptimizerStrategy", "ConstLayerMemoryOptimizerStrategy"}
+ {"MemoryOptimizerStrategy", "ConstantMemoryStrategy"}
}
});
IRuntimePtr run = IRuntime::Create(options);
- // ConstLayerMemoryOptimizerStrategy should be registered for CpuRef
+ // ConstantMemoryStrategy should be registered for CpuRef
CHECK(!BackendRegistryInstance().GetMemoryOptimizerStrategies().empty());
CHECK(BackendRegistryInstance().GetMemoryOptimizerStrategies().size() == 1);
CHECK(BackendRegistryInstance().GetMemoryOptimizerStrategies().at(RefBackend::GetIdStatic()));
auto optimizerStrategy = BackendRegistryInstance().GetMemoryOptimizerStrategies().at(RefBackend::GetIdStatic());
- CHECK(optimizerStrategy->GetName() == std::string("ConstLayerMemoryOptimizerStrategy"));
+ CHECK(optimizerStrategy->GetName() == std::string("ConstantMemoryStrategy"));
armnn::BackendRegistryInstance().DeregisterMemoryOptimizerStrategy(RefBackend::GetIdStatic());
}