From 2ff0009ca9245304c48889c8ba8d3a39d42febed Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Mon, 30 Sep 2019 16:50:08 +0100 Subject: COMPMID-2661: Implement complex function dynamic tensor support. Change-Id: I80772cb25514009b030e5ade28cbb71ed352da67 Signed-off-by: Georgios Pinitas Reviewed-on: https://review.mlplatform.org/c/2019 Tested-by: Arm Jenkins Reviewed-by: Michalis Spyrou Comments-Addressed: Arm Jenkins --- .../CL/functions/CLGEMMConvolutionLayer.cpp | 2 - .../NEON/functions/NEGEMMConvolutionLayer.cpp | 2 - tests/validation/CL/UNIT/DynamicTensor.cpp | 31 +++- .../validation/GLES_COMPUTE/UNIT/DynamicTensor.cpp | 3 +- tests/validation/NEON/UNIT/DynamicTensor.cpp | 35 ++++- .../fixtures/UNIT/DynamicTensorFixture.h | 157 +++++++++++++++++++-- 6 files changed, 211 insertions(+), 19 deletions(-) diff --git a/src/runtime/CL/functions/CLGEMMConvolutionLayer.cpp b/src/runtime/CL/functions/CLGEMMConvolutionLayer.cpp index be6be04703..594c8eef34 100644 --- a/src/runtime/CL/functions/CLGEMMConvolutionLayer.cpp +++ b/src/runtime/CL/functions/CLGEMMConvolutionLayer.cpp @@ -601,8 +601,6 @@ void CLGEMMConvolutionLayer::prepare() { if(!_is_prepared) { - ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); - // Run weights reshaping and mark original weights tensor as unused _weights_reshaped.allocator()->allocate(); _reshape_weights.run(); diff --git a/src/runtime/NEON/functions/NEGEMMConvolutionLayer.cpp b/src/runtime/NEON/functions/NEGEMMConvolutionLayer.cpp index a39e4c5125..0034dd2545 100644 --- a/src/runtime/NEON/functions/NEGEMMConvolutionLayer.cpp +++ b/src/runtime/NEON/functions/NEGEMMConvolutionLayer.cpp @@ -611,8 +611,6 @@ void NEGEMMConvolutionLayer::prepare() { if(!_is_prepared) { - ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); - if(_weights_manager && _weights_manager->are_weights_managed(_original_weights)) { _weights_manager->run(_original_weights, &_reshape_weights_managed); diff --git a/tests/validation/CL/UNIT/DynamicTensor.cpp b/tests/validation/CL/UNIT/DynamicTensor.cpp index 38acbd5c3a..06670478b2 100644 --- a/tests/validation/CL/UNIT/DynamicTensor.cpp +++ b/tests/validation/CL/UNIT/DynamicTensor.cpp @@ -23,6 +23,7 @@ */ #include "arm_compute/runtime/BlobLifetimeManager.h" #include "arm_compute/runtime/CL/CLBufferAllocator.h" +#include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h" #include "arm_compute/runtime/CL/functions/CLL2NormalizeLayer.h" #include "arm_compute/runtime/MemoryGroup.h" #include "arm_compute/runtime/MemoryManagerOnDemand.h" @@ -35,6 +36,7 @@ #include "tests/framework/Asserts.h" #include "tests/framework/Macros.h" #include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" #include "tests/validation/fixtures/UNIT/DynamicTensorFixture.h" namespace arm_compute @@ -45,6 +47,10 @@ namespace validation { namespace { +constexpr AbsoluteTolerance absolute_tolerance_float(0.0001f); /**< Absolute Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ +RelativeTolerance tolerance_f32(0.1f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ +constexpr float tolerance_num = 0.07f; /**< Tolerance number */ + using CLL2NormLayerWrapper = SimpleFunctionWrapper; template <> void CLL2NormLayerWrapper::configure(ICLTensor *src, ICLTensor *dst) @@ -56,7 +62,8 @@ TEST_SUITE(CL) TEST_SUITE(UNIT) TEST_SUITE(DynamicTensor) -using CLDynamicTensorType3SingleFunction = DynamicTensorType3SingleFunction; +using BlobMemoryManagementService = MemoryManagementService; +using CLDynamicTensorType3SingleFunction = DynamicTensorType3SingleFunction; /** Tests the memory manager with dynamic input and output tensors. * @@ -97,6 +104,28 @@ FIXTURE_DATA_TEST_CASE(DynamicTensorType3Single, CLDynamicTensorType3SingleFunct } } +using CLDynamicTensorType3ComplexFunction = DynamicTensorType3ComplexFunction; +/** Tests the memory manager with dynamic input and output tensors. + * + * Create and manage the tensors needed to run a complex function. After the function is executed, + * change the input and output size requesting more memory and go through the manage/allocate process. + * The memory manager should be able to update the inner structures and allocate the requested memory + * */ +FIXTURE_DATA_TEST_CASE(DynamicTensorType3Complex, CLDynamicTensorType3ComplexFunction, framework::DatasetMode::ALL, + framework::dataset::zip(framework::dataset::zip(framework::dataset::zip(framework::dataset::zip( + framework::dataset::make("InputShape", { std::vector{ TensorShape(12U, 12U, 16U), TensorShape(64U, 64U, 16U) } }), + framework::dataset::make("WeightsManager", { TensorShape(3U, 3U, 16U, 5U) })), + framework::dataset::make("BiasShape", { TensorShape(5U) })), + framework::dataset::make("OutputShape", { std::vector{ TensorShape(12U, 12U, 5U), TensorShape(64U, 64U, 5U) } })), + framework::dataset::make("PadStrideInfo", { PadStrideInfo(1U, 1U, 1U, 1U) }))) +{ + for(unsigned int i = 0; i < num_iterations; ++i) + { + run_iteration(i); + validate(CLAccessor(dst_target), dst_ref, tolerance_f32, tolerance_num, absolute_tolerance_float); + } +} + TEST_SUITE_END() // DynamicTensor TEST_SUITE_END() // UNIT TEST_SUITE_END() // CL diff --git a/tests/validation/GLES_COMPUTE/UNIT/DynamicTensor.cpp b/tests/validation/GLES_COMPUTE/UNIT/DynamicTensor.cpp index 2b972843b8..cab854424e 100644 --- a/tests/validation/GLES_COMPUTE/UNIT/DynamicTensor.cpp +++ b/tests/validation/GLES_COMPUTE/UNIT/DynamicTensor.cpp @@ -56,7 +56,8 @@ TEST_SUITE(GC) TEST_SUITE(UNIT) TEST_SUITE(DynamicTensor) -using GCDynamicTensorType3SingleFunction = DynamicTensorType3SingleFunction; +using BlobMemoryManagementService = MemoryManagementService; +using GCDynamicTensorType3SingleFunction = DynamicTensorType3SingleFunction; /** Tests the memory manager with dynamic input and output tensors. * diff --git a/tests/validation/NEON/UNIT/DynamicTensor.cpp b/tests/validation/NEON/UNIT/DynamicTensor.cpp index 319aa6a8ee..dde67b06a6 100644 --- a/tests/validation/NEON/UNIT/DynamicTensor.cpp +++ b/tests/validation/NEON/UNIT/DynamicTensor.cpp @@ -22,19 +22,18 @@ * SOFTWARE. */ #include "arm_compute/runtime/Allocator.h" -#include "arm_compute/runtime/MemoryGroup.h" #include "arm_compute/runtime/MemoryManagerOnDemand.h" +#include "arm_compute/runtime/NEON/functions/NEConvolutionLayer.h" #include "arm_compute/runtime/NEON/functions/NENormalizationLayer.h" #include "arm_compute/runtime/OffsetLifetimeManager.h" #include "arm_compute/runtime/PoolManager.h" #include "support/ToolchainSupport.h" #include "tests/AssetsLibrary.h" -#include "tests/Globals.h" #include "tests/NEON/Accessor.h" -#include "tests/Utils.h" #include "tests/framework/Asserts.h" #include "tests/framework/Macros.h" #include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" #include "tests/validation/fixtures/UNIT/DynamicTensorFixture.h" namespace arm_compute @@ -45,6 +44,10 @@ namespace validation { namespace { +constexpr AbsoluteTolerance absolute_tolerance_float(0.0001f); /**< Absolute Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ +RelativeTolerance tolerance_f32(0.1f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ +constexpr float tolerance_num = 0.07f; /**< Tolerance number */ + using NENormLayerWrapper = SimpleFunctionWrapper; template <> void NENormLayerWrapper::configure(arm_compute::ITensor *src, arm_compute::ITensor *dst) @@ -55,7 +58,9 @@ void NENormLayerWrapper::configure(arm_compute::ITensor *src, arm_compute::ITens TEST_SUITE(NEON) TEST_SUITE(UNIT) TEST_SUITE(DynamicTensor) -using NEDynamicTensorType3SingleFunction = DynamicTensorType3SingleFunction; + +using OffsetMemoryManagementService = MemoryManagementService; +using NEDynamicTensorType3SingleFunction = DynamicTensorType3SingleFunction; /** Tests the memory manager with dynamic input and output tensors. * @@ -79,6 +84,28 @@ FIXTURE_DATA_TEST_CASE(DynamicTensorType3Single, NEDynamicTensorType3SingleFunct } } +using NEDynamicTensorType3ComplexFunction = DynamicTensorType3ComplexFunction; +/** Tests the memory manager with dynamic input and output tensors. + * + * Create and manage the tensors needed to run a complex function. After the function is executed, + * change the input and output size requesting more memory and go through the manage/allocate process. + * The memory manager should be able to update the inner structures and allocate the requested memory + * */ +FIXTURE_DATA_TEST_CASE(DynamicTensorType3Complex, NEDynamicTensorType3ComplexFunction, framework::DatasetMode::ALL, + framework::dataset::zip(framework::dataset::zip(framework::dataset::zip(framework::dataset::zip( + framework::dataset::make("InputShape", { std::vector{ TensorShape(12U, 12U, 6U), TensorShape(128U, 128U, 6U) } }), + framework::dataset::make("WeightsManager", { TensorShape(3U, 3U, 6U, 3U) })), + framework::dataset::make("BiasShape", { TensorShape(3U) })), + framework::dataset::make("OutputShape", { std::vector{ TensorShape(12U, 12U, 3U), TensorShape(128U, 128U, 3U) } })), + framework::dataset::make("PadStrideInfo", { PadStrideInfo(1U, 1U, 1U, 1U) }))) +{ + for(unsigned int i = 0; i < num_iterations; ++i) + { + run_iteration(i); + validate(Accessor(dst_target), dst_ref, tolerance_f32, tolerance_num, absolute_tolerance_float); + } +} + TEST_SUITE_END() // DynamicTensor TEST_SUITE_END() // UNIT TEST_SUITE_END() // NEON diff --git a/tests/validation/fixtures/UNIT/DynamicTensorFixture.h b/tests/validation/fixtures/UNIT/DynamicTensorFixture.h index 66ef6c4aff..b2600f13f0 100644 --- a/tests/validation/fixtures/UNIT/DynamicTensorFixture.h +++ b/tests/validation/fixtures/UNIT/DynamicTensorFixture.h @@ -32,6 +32,7 @@ #include "tests/framework/Asserts.h" #include "tests/framework/Fixture.h" #include "tests/validation/Helpers.h" +#include "tests/validation/reference/ConvolutionLayer.h" #include "tests/validation/reference/NormalizationLayer.h" namespace arm_compute @@ -48,6 +49,9 @@ template struct MemoryManagementService { +public: + using LftMgrType = LifetimeMgrType; + public: MemoryManagementService() : allocator(), lifetime_mgr(nullptr), pool_mgr(nullptr), mm(nullptr), mg(), num_pools(0) @@ -118,15 +122,11 @@ private: */ template class DynamicTensorType3SingleFunction : public framework::Fixture { - using T = float; - using MemoryManagementServiceType = MemoryManagementService; + using T = float; public: template @@ -234,9 +234,148 @@ protected: } public: - TensorShape input_l0{}, input_l1{}; - typename LifetimeMgrType::info_type internal_l0{}, internal_l1{}; - typename LifetimeMgrType::info_type cross_l0{}, cross_l1{}; + TensorShape input_l0{}, input_l1{}; + typename MemoryManagementServiceType::LftMgrType::info_type internal_l0{}, internal_l1{}; + typename MemoryManagementServiceType::LftMgrType::info_type cross_l0{}, cross_l1{}; +}; + +/** Simple test case to run a single function with different shapes twice. + * + * Runs a specified function twice, where the second time the size of the input/output is different + * Internal memory of the function and input/output are managed by different services + */ +template +class DynamicTensorType3ComplexFunction : public framework::Fixture +{ + using T = float; + +public: + template + void setup(std::vector input_shapes, TensorShape weights_shape, TensorShape bias_shape, std::vector output_shapes, PadStrideInfo info) + { + num_iterations = input_shapes.size(); + _data_type = DataType::F32; + _data_layout = DataLayout::NHWC; + _input_shapes = input_shapes; + _output_shapes = output_shapes; + _weights_shape = weights_shape; + _bias_shape = bias_shape; + _info = info; + + // Create function + _f_target = support::cpp14::make_unique(_ms.mm); + } + + void run_iteration(unsigned int idx) + { + auto input_shape = _input_shapes[idx]; + auto output_shape = _output_shapes[idx]; + + dst_ref = run_reference(input_shape, _weights_shape, _bias_shape, output_shape, _info); + dst_target = run_target(input_shape, _weights_shape, _bias_shape, output_shape, _info, WeightsInfo()); + } + +protected: + template + void fill(U &&tensor, int i) + { + switch(tensor.data_type()) + { + case DataType::F32: + { + std::uniform_real_distribution<> distribution(-1.0f, 1.0f); + library->fill(tensor, distribution, i); + break; + } + default: + library->fill_tensor_uniform(tensor, i); + } + } + + TensorType run_target(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, + PadStrideInfo info, WeightsInfo weights_info) + { + if(_data_layout == DataLayout::NHWC) + { + permute(input_shape, PermutationVector(2U, 0U, 1U)); + permute(weights_shape, PermutationVector(2U, 0U, 1U)); + permute(output_shape, PermutationVector(2U, 0U, 1U)); + } + + _weights_target = create_tensor(weights_shape, _data_type, 1, QuantizationInfo(), _data_layout); + _bias_target = create_tensor(bias_shape, _data_type, 1); + + // Create tensors + TensorType src = create_tensor(input_shape, _data_type, 1, QuantizationInfo(), _data_layout); + TensorType dst = create_tensor(output_shape, _data_type, 1, QuantizationInfo(), _data_layout); + + // Create and configure function + _f_target->configure(&src, &_weights_target, &_bias_target, &dst, info, weights_info); + + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + src.allocator()->allocate(); + dst.allocator()->allocate(); + _weights_target.allocator()->allocate(); + _bias_target.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(src), 0); + fill(AccessorType(_weights_target), 1); + fill(AccessorType(_bias_target), 2); + + // Populate and validate memory manager + _ms.clear(); + _ms.populate(1); + _ms.mg.acquire(); + + // Compute NEConvolutionLayer function + _f_target->run(); + _ms.mg.release(); + + return dst; + } + + SimpleTensor run_reference(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info) + { + // Create reference + SimpleTensor src{ input_shape, _data_type, 1 }; + SimpleTensor weights{ weights_shape, _data_type, 1 }; + SimpleTensor bias{ bias_shape, _data_type, 1 }; + + // Fill reference + fill(src, 0); + fill(weights, 1); + fill(bias, 2); + + return reference::convolution_layer(src, weights, bias, output_shape, info); + } + +public: + unsigned int num_iterations{ 0 }; + SimpleTensor dst_ref{}; + TensorType dst_target{}; + +private: + DataType _data_type{ DataType::UNKNOWN }; + DataLayout _data_layout{ DataLayout::UNKNOWN }; + PadStrideInfo _info{}; + std::vector _input_shapes{}; + std::vector _output_shapes{}; + TensorShape _weights_shape{}; + TensorShape _bias_shape{}; + MemoryManagementServiceType _ms{}; + TensorType _weights_target{}; + TensorType _bias_target{}; + std::unique_ptr _f_target{}; }; } // namespace validation } // namespace test -- cgit v1.2.1