diff options
Diffstat (limited to 'src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test')
3 files changed, 372 insertions, 0 deletions
diff --git a/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/CMakeLists.txt b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/CMakeLists.txt new file mode 100644 index 0000000000..b96782a84d --- /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/memoryOptimizerStrategyLibrary/test/ConstMemoryStrategyTests.cpp b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/ConstMemoryStrategyTests.cpp new file mode 100644 index 0000000000..64312f362f --- /dev/null +++ b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/ConstMemoryStrategyTests.cpp @@ -0,0 +1,77 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include <backendsCommon/memoryOptimizerStrategyLibrary/strategies/ConstantMemoryStrategy.hpp> +#include <backendsCommon/memoryOptimizerStrategyLibrary/strategies/StrategyValidator.hpp> + +#include <doctest/doctest.h> +#include <vector> + +using namespace armnn; + +TEST_SUITE("ConstMemoryStrategyTestSuite") +{ + +TEST_CASE("ConstMemoryStrategyTest") +{ + // create a few memory blocks + MemBlock memBlock0(0, 2, 20, 0, 0); + MemBlock memBlock1(2, 3, 10, 20, 1); + MemBlock memBlock2(3, 5, 15, 30, 2); + MemBlock memBlock3(5, 6, 20, 50, 3); + MemBlock memBlock4(7, 8, 5, 70, 4); + + std::vector<MemBlock> memBlocks; + memBlocks.reserve(5); + memBlocks.push_back(memBlock0); + memBlocks.push_back(memBlock1); + memBlocks.push_back(memBlock2); + memBlocks.push_back(memBlock3); + memBlocks.push_back(memBlock4); + + // 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); + + CHECK(memBins[1].m_MemBlocks.size() == 1); + CHECK(memBins[1].m_MemBlocks[0].m_Offset == 0); + CHECK(memBins[1].m_MemBlocks[0].m_MemSize == 10); + CHECK(memBins[1].m_MemBlocks[0].m_Index == 1); + + CHECK(memBins[4].m_MemBlocks.size() == 1); + CHECK(memBins[4].m_MemBlocks[0].m_Offset == 0); + CHECK(memBins[4].m_MemBlocks[0].m_MemSize == 5); + CHECK(memBins[4].m_MemBlocks[0].m_Index == 4); +} + +TEST_CASE("ConstLayerMemoryOptimizerStrategyValidatorTest") +{ + // create a few memory blocks + MemBlock memBlock0(0, 2, 20, 0, 0); + MemBlock memBlock1(2, 3, 10, 20, 1); + MemBlock memBlock2(3, 5, 15, 30, 2); + MemBlock memBlock3(5, 6, 20, 50, 3); + MemBlock memBlock4(7, 8, 5, 70, 4); + + std::vector<MemBlock> memBlocks; + memBlocks.reserve(5); + memBlocks.push_back(memBlock0); + memBlocks.push_back(memBlock1); + memBlocks.push_back(memBlock2); + memBlocks.push_back(memBlock3); + memBlocks.push_back(memBlock4); + + // Optimize the memory blocks with ConstLayerMemoryOptimizerStrategy + auto ptr = std::make_shared<ConstantMemoryStrategy>(); + StrategyValidator validator; + validator.SetStrategy(ptr); + // Ensure ConstLayerMemoryOptimizerStrategy is valid + CHECK_NOTHROW(validator.Optimize(memBlocks)); +} + +} diff --git a/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/ValidatorStrategyTests.cpp b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/ValidatorStrategyTests.cpp new file mode 100644 index 0000000000..bc04105f4b --- /dev/null +++ b/src/backends/backendsCommon/memoryOptimizerStrategyLibrary/test/ValidatorStrategyTests.cpp @@ -0,0 +1,283 @@ +// +// Copyright © 2021 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include <backendsCommon/memoryOptimizerStrategyLibrary/strategies/StrategyValidator.hpp> + +#include <doctest/doctest.h> +#include <vector> + +using namespace armnn; + +TEST_SUITE("MemoryOptimizerStrategyValidatorTestSuite") +{ + +// TestMemoryOptimizerStrategy: Create a MemBin and put all blocks in it so the can overlap. +class TestMemoryOptimizerStrategy : public IMemoryOptimizerStrategy +{ +public: + TestMemoryOptimizerStrategy(MemBlockStrategyType type) + : m_Name(std::string("testMemoryOptimizerStrategy")) + , m_MemBlockStrategyType(type) {} + + std::string GetName() const override + { + return m_Name; + } + + MemBlockStrategyType GetMemBlockStrategyType() const override + { + return m_MemBlockStrategyType; + } + + std::vector<MemBin> Optimize(std::vector<MemBlock>& memBlocks) override + { + std::vector<MemBin> memBins; + memBins.reserve(memBlocks.size()); + + MemBin memBin; + memBin.m_MemBlocks.reserve(memBlocks.size()); + memBin.m_MemSize = 0; + for (auto& memBlock : memBlocks) + { + + memBin.m_MemSize = memBin.m_MemSize + memBlock.m_MemSize; + memBin.m_MemBlocks.push_back(memBlock); + } + memBins.push_back(memBin); + + return memBins; + } + +private: + std::string m_Name; + MemBlockStrategyType m_MemBlockStrategyType; +}; + +TEST_CASE("MemoryOptimizerStrategyValidatorTestOverlapX") +{ + // create a few memory blocks + MemBlock memBlock0(0, 5, 20, 0, 0); + MemBlock memBlock1(5, 10, 10, 0, 1); + MemBlock memBlock2(10, 15, 15, 0, 2); + MemBlock memBlock3(15, 20, 20, 0, 3); + MemBlock memBlock4(20, 25, 5, 0, 4); + + std::vector<MemBlock> memBlocks; + memBlocks.reserve(5); + memBlocks.push_back(memBlock0); + memBlocks.push_back(memBlock1); + memBlocks.push_back(memBlock2); + memBlocks.push_back(memBlock3); + memBlocks.push_back(memBlock4); + + // Optimize the memory blocks with TestMemoryOptimizerStrategySingle + TestMemoryOptimizerStrategy testMemoryOptimizerStrategySingle(MemBlockStrategyType::SingleAxisPacking); + auto ptr = std::make_shared<TestMemoryOptimizerStrategy>(testMemoryOptimizerStrategySingle); + StrategyValidator validator; + validator.SetStrategy(ptr); + // SingleAxisPacking can overlap on X axis. + CHECK_NOTHROW(validator.Optimize(memBlocks)); + + // Optimize the memory blocks with TestMemoryOptimizerStrategyMulti + TestMemoryOptimizerStrategy testMemoryOptimizerStrategyMulti(MemBlockStrategyType::MultiAxisPacking); + auto ptrMulti = std::make_shared<TestMemoryOptimizerStrategy>(testMemoryOptimizerStrategyMulti); + StrategyValidator validatorMulti; + validatorMulti.SetStrategy(ptrMulti); + // MultiAxisPacking can overlap on X axis. + CHECK_NOTHROW(validatorMulti.Optimize(memBlocks)); +} + +TEST_CASE("MemoryOptimizerStrategyValidatorTestOverlapXAndY") +{ + // create a few memory blocks + MemBlock memBlock0(0, 5, 20, 0, 0); + MemBlock memBlock1(0, 10, 10, 0, 1); + MemBlock memBlock2(0, 15, 15, 0, 2); + MemBlock memBlock3(0, 20, 20, 0, 3); + MemBlock memBlock4(0, 25, 5, 0, 4); + + std::vector<MemBlock> memBlocks; + memBlocks.reserve(5); + memBlocks.push_back(memBlock0); + memBlocks.push_back(memBlock1); + memBlocks.push_back(memBlock2); + memBlocks.push_back(memBlock3); + memBlocks.push_back(memBlock4); + + // Optimize the memory blocks with TestMemoryOptimizerStrategySingle + TestMemoryOptimizerStrategy testMemoryOptimizerStrategySingle(MemBlockStrategyType::SingleAxisPacking); + auto ptr = std::make_shared<TestMemoryOptimizerStrategy>(testMemoryOptimizerStrategySingle); + StrategyValidator validator; + validator.SetStrategy(ptr); + // SingleAxisPacking cannot overlap on both X and Y axis. + CHECK_THROWS(validator.Optimize(memBlocks)); + + // Optimize the memory blocks with TestMemoryOptimizerStrategyMulti + TestMemoryOptimizerStrategy testMemoryOptimizerStrategyMulti(MemBlockStrategyType::MultiAxisPacking); + auto ptrMulti = std::make_shared<TestMemoryOptimizerStrategy>(testMemoryOptimizerStrategyMulti); + StrategyValidator validatorMulti; + validatorMulti.SetStrategy(ptrMulti); + // MultiAxisPacking cannot overlap on both X and Y axis. + CHECK_THROWS(validatorMulti.Optimize(memBlocks)); +} + +TEST_CASE("MemoryOptimizerStrategyValidatorTestOverlapY") +{ + // create a few memory blocks + MemBlock memBlock0(0, 2, 20, 0, 0); + MemBlock memBlock1(0, 3, 10, 20, 1); + MemBlock memBlock2(0, 5, 15, 30, 2); + MemBlock memBlock3(0, 6, 20, 50, 3); + MemBlock memBlock4(0, 8, 5, 70, 4); + + std::vector<MemBlock> memBlocks; + memBlocks.reserve(5); + memBlocks.push_back(memBlock0); + memBlocks.push_back(memBlock1); + memBlocks.push_back(memBlock2); + memBlocks.push_back(memBlock3); + memBlocks.push_back(memBlock4); + + // Optimize the memory blocks with TestMemoryOptimizerStrategySingle + TestMemoryOptimizerStrategy testMemoryOptimizerStrategySingle(MemBlockStrategyType::SingleAxisPacking); + auto ptr = std::make_shared<TestMemoryOptimizerStrategy>(testMemoryOptimizerStrategySingle); + StrategyValidator validator; + validator.SetStrategy(ptr); + // SingleAxisPacking cannot overlap on Y axis + CHECK_THROWS(validator.Optimize(memBlocks)); + + // Optimize the memory blocks with TestMemoryOptimizerStrategyMulti + TestMemoryOptimizerStrategy testMemoryOptimizerStrategyMulti(MemBlockStrategyType::MultiAxisPacking); + auto ptrMulti = std::make_shared<TestMemoryOptimizerStrategy>(testMemoryOptimizerStrategyMulti); + StrategyValidator validatorMulti; + validatorMulti.SetStrategy(ptrMulti); + // MultiAxisPacking can overlap on Y axis + CHECK_NOTHROW(validatorMulti.Optimize(memBlocks)); +} + +// TestMemoryOptimizerStrategyDuplicate: Create a MemBin and put all blocks in it duplicating each so validator +// can check +class TestMemoryOptimizerStrategyDuplicate : public TestMemoryOptimizerStrategy +{ +public: + TestMemoryOptimizerStrategyDuplicate(MemBlockStrategyType type) + : TestMemoryOptimizerStrategy(type) + {} + + std::vector<MemBin> Optimize(std::vector<MemBlock>& memBlocks) override + { + std::vector<MemBin> memBins; + memBins.reserve(memBlocks.size()); + + MemBin memBin; + memBin.m_MemBlocks.reserve(memBlocks.size()); + for (auto& memBlock : memBlocks) + { + memBin.m_MemSize = memBin.m_MemSize + memBlock.m_MemSize; + memBin.m_MemBlocks.push_back(memBlock); + // Put block in twice so it gets found twice + memBin.m_MemBlocks.push_back(memBlock); + } + memBins.push_back(memBin); + + return memBins; + } +}; + +TEST_CASE("MemoryOptimizerStrategyValidatorTestDuplicateBlocks") +{ + // create a few memory blocks + MemBlock memBlock0(0, 2, 20, 0, 0); + MemBlock memBlock1(2, 3, 10, 20, 1); + MemBlock memBlock2(3, 5, 15, 30, 2); + MemBlock memBlock3(5, 6, 20, 50, 3); + MemBlock memBlock4(7, 8, 5, 70, 4); + + std::vector<MemBlock> memBlocks; + memBlocks.reserve(5); + memBlocks.push_back(memBlock0); + memBlocks.push_back(memBlock1); + memBlocks.push_back(memBlock2); + memBlocks.push_back(memBlock3); + memBlocks.push_back(memBlock4); + + // Optimize the memory blocks with TestMemoryOptimizerStrategySingle + // Duplicate strategy is invalid as same block is found twice + TestMemoryOptimizerStrategyDuplicate testMemoryOptimizerStrategySingle(MemBlockStrategyType::SingleAxisPacking); + auto ptr = std::make_shared<TestMemoryOptimizerStrategyDuplicate>(testMemoryOptimizerStrategySingle); + 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); + 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 +// can check +class TestMemoryOptimizerStrategySkip : public TestMemoryOptimizerStrategy +{ +public: + TestMemoryOptimizerStrategySkip(MemBlockStrategyType type) + : TestMemoryOptimizerStrategy(type) + {} + + std::vector<MemBin> Optimize(std::vector<MemBlock>& memBlocks) override + { + std::vector<MemBin> memBins; + memBins.reserve(memBlocks.size()); + + MemBin memBin; + memBin.m_MemBlocks.reserve(memBlocks.size()); + for (unsigned int i = 0; i < memBlocks.size()-1; i+=2) + { + auto memBlock = memBlocks[i]; + memBin.m_MemSize = memBin.m_MemSize + memBlock.m_MemSize; + memBin.m_MemBlocks.push_back(memBlock); + } + memBins.push_back(memBin); + + return memBins; + } +}; + +TEST_CASE("MemoryOptimizerStrategyValidatorTestSkipBlocks") +{ + // create a few memory blocks + MemBlock memBlock0(0, 2, 20, 0, 0); + MemBlock memBlock1(2, 3, 10, 20, 1); + MemBlock memBlock2(3, 5, 15, 30, 2); + MemBlock memBlock3(5, 6, 20, 50, 3); + MemBlock memBlock4(7, 8, 5, 70, 4); + + std::vector<MemBlock> memBlocks; + memBlocks.reserve(5); + memBlocks.push_back(memBlock0); + memBlocks.push_back(memBlock1); + memBlocks.push_back(memBlock2); + memBlocks.push_back(memBlock3); + memBlocks.push_back(memBlock4); + + // Optimize the memory blocks with TestMemoryOptimizerStrategySingle + // Skip strategy is invalid as every second block is not found + TestMemoryOptimizerStrategySkip testMemoryOptimizerStrategySingle(MemBlockStrategyType::SingleAxisPacking); + auto ptr = std::make_shared<TestMemoryOptimizerStrategySkip>(testMemoryOptimizerStrategySingle); + 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); + StrategyValidator validatorMulti; + validatorMulti.SetStrategy(ptrMulti); + CHECK_THROWS(validatorMulti.Optimize(memBlocks)); +} + +} |