From fb62908bd8148bd347bd204e881156f8ebf7835d Mon Sep 17 00:00:00 2001 From: Giorgio Arena Date: Mon, 20 Aug 2018 18:03:27 +0100 Subject: COMPMID-1494 Optimise NEON im2col and weights reshape for NHWC Change-Id: I99ebae61024a7bce9d17292a02c28626ae6c29d5 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/144872 Tested-by: Jenkins Reviewed-by: Gian Marco Iodice --- tests/validation/CL/Im2Col.cpp | 72 +++++++++++++------------------ tests/validation/NEON/Im2Col.cpp | 30 ++++++------- tests/validation/fixtures/Im2ColFixture.h | 8 ++-- tests/validation/reference/Im2Col.cpp | 66 +++------------------------- tests/validation/reference/Im2Col.h | 3 +- 5 files changed, 52 insertions(+), 127 deletions(-) (limited to 'tests') diff --git a/tests/validation/CL/Im2Col.cpp b/tests/validation/CL/Im2Col.cpp index cf7c79ad72..ebf2331e5e 100644 --- a/tests/validation/CL/Im2Col.cpp +++ b/tests/validation/CL/Im2Col.cpp @@ -94,17 +94,15 @@ template using CLIm2ColFixture = Im2ColValidationFixture; TEST_SUITE(Float) TEST_SUITE(FP32) -FIXTURE_DATA_TEST_CASE(RunSmall, CLIm2ColFixture, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)), - conv_args), - framework::dataset::make("ChannelsFirstOutputNHWC", true))) +FIXTURE_DATA_TEST_CASE(RunSmall, CLIm2ColFixture, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)), + conv_args)) { // Validate output validate(CLAccessor(_target), _reference); } -FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F32)), - conv_args), - framework::dataset::make("ChannelsFirstOutputNHWC", true))) +FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F32)), + conv_args)) { // Validate output validate(CLAccessor(_target), _reference); @@ -112,16 +110,14 @@ FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode: TEST_SUITE_END() TEST_SUITE(FP16) -FIXTURE_DATA_TEST_CASE(RunSmall, CLIm2ColFixture, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)), - conv_args), - framework::dataset::make("ChannelsFirstOutputNHWC", true))) +FIXTURE_DATA_TEST_CASE(RunSmall, CLIm2ColFixture, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)), + conv_args)) { // Validate output validate(CLAccessor(_target), _reference); } -FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F16)), - conv_args), - framework::dataset::make("ChannelsFirstOutputNHWC", true))) +FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F16)), + conv_args)) { // Validate output validate(CLAccessor(_target), _reference); @@ -130,16 +126,14 @@ TEST_SUITE_END() TEST_SUITE_END() TEST_SUITE(QASYMM8) -FIXTURE_DATA_TEST_CASE(RunSmall, CLIm2ColFixture, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::QASYMM8)), - conv_args), - framework::dataset::make("ChannelsFirstOutputNHWC", true))) +FIXTURE_DATA_TEST_CASE(RunSmall, CLIm2ColFixture, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::QASYMM8)), + conv_args)) { // Validate output validate(CLAccessor(_target), _reference); } -FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::QASYMM8)), - conv_args), - framework::dataset::make("ChannelsFirstOutputNHWC", true))) +FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::QASYMM8)), + conv_args)) { // Validate output validate(CLAccessor(_target), _reference); @@ -148,19 +142,17 @@ TEST_SUITE_END() TEST_SUITE(Grouped) TEST_SUITE(FP32) -FIXTURE_DATA_TEST_CASE(RunSmall, CLIm2ColFixture, framework::DatasetMode::ALL, combine(combine(combine(datasets::GroupedIm2ColSmallShapes(), framework::dataset::make("DataType", - DataType::F32)), - grouped_args), - framework::dataset::make("ChannelsFirstOutputNHWC", true))) +FIXTURE_DATA_TEST_CASE(RunSmall, CLIm2ColFixture, framework::DatasetMode::ALL, combine(combine(datasets::GroupedIm2ColSmallShapes(), framework::dataset::make("DataType", + DataType::F32)), + grouped_args)) { // Validate output validate(CLAccessor(_target), _reference); } -FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::GroupedIm2ColLargeShapes(), framework::dataset::make("DataType", - DataType::F32)), - grouped_args), - framework::dataset::make("ChannelsFirstOutputNHWC", true))) +FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::GroupedIm2ColLargeShapes(), framework::dataset::make("DataType", + DataType::F32)), + grouped_args)) { // Validate output validate(CLAccessor(_target), _reference); @@ -168,19 +160,17 @@ FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode: TEST_SUITE_END() TEST_SUITE(FP16) -FIXTURE_DATA_TEST_CASE(RunSmall, CLIm2ColFixture, framework::DatasetMode::ALL, combine(combine(combine(datasets::GroupedIm2ColSmallShapes(), framework::dataset::make("DataType", - DataType::F16)), - grouped_args), - framework::dataset::make("ChannelsFirstOutputNHWC", true))) +FIXTURE_DATA_TEST_CASE(RunSmall, CLIm2ColFixture, framework::DatasetMode::ALL, combine(combine(datasets::GroupedIm2ColSmallShapes(), framework::dataset::make("DataType", + DataType::F16)), + grouped_args)) { // Validate output validate(CLAccessor(_target), _reference); } -FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::GroupedIm2ColLargeShapes(), framework::dataset::make("DataType", - DataType::F16)), - grouped_args), - framework::dataset::make("ChannelsFirstOutputNHWC", true))) +FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::GroupedIm2ColLargeShapes(), framework::dataset::make("DataType", + DataType::F16)), + grouped_args)) { // Validate output validate(CLAccessor(_target), _reference); @@ -188,19 +178,17 @@ FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode:: TEST_SUITE_END() TEST_SUITE(QASYMM8) -FIXTURE_DATA_TEST_CASE(RunSmall, CLIm2ColFixture, framework::DatasetMode::ALL, combine(combine(combine(datasets::GroupedIm2ColSmallShapes(), framework::dataset::make("DataType", - DataType::QASYMM8)), - grouped_args), - framework::dataset::make("ChannelsFirstOutputNHWC", true))) +FIXTURE_DATA_TEST_CASE(RunSmall, CLIm2ColFixture, framework::DatasetMode::ALL, combine(combine(datasets::GroupedIm2ColSmallShapes(), framework::dataset::make("DataType", + DataType::QASYMM8)), + grouped_args)) { // Validate output validate(CLAccessor(_target), _reference); } -FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::GroupedIm2ColLargeShapes(), framework::dataset::make("DataType", - DataType::QASYMM8)), - grouped_args), - framework::dataset::make("ChannelsFirstOutputNHWC", true))) +FIXTURE_DATA_TEST_CASE(RunLarge, CLIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::GroupedIm2ColLargeShapes(), framework::dataset::make("DataType", + DataType::QASYMM8)), + grouped_args)) { // Validate output validate(CLAccessor(_target), _reference); diff --git a/tests/validation/NEON/Im2Col.cpp b/tests/validation/NEON/Im2Col.cpp index 0ea68bf49d..5a2b46a550 100644 --- a/tests/validation/NEON/Im2Col.cpp +++ b/tests/validation/NEON/Im2Col.cpp @@ -78,16 +78,14 @@ using NEIm2ColFixture = Im2ColValidationFixture, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)), - conv_args), - framework::dataset::make("ChannelsFirstOutputNHWC", false))) +FIXTURE_DATA_TEST_CASE(RunSmall, NEIm2ColFixture, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)), + conv_args)) { // Validate output validate(Accessor(_target), _reference); } -FIXTURE_DATA_TEST_CASE(RunLarge, NEIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F32)), - conv_args), - framework::dataset::make("ChannelsFirstOutputNHWC", false))) +FIXTURE_DATA_TEST_CASE(RunLarge, NEIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F32)), + conv_args)) { // Validate output validate(Accessor(_target), _reference); @@ -97,16 +95,14 @@ TEST_SUITE_END() #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC TEST_SUITE(FP16) -FIXTURE_DATA_TEST_CASE(RunSmall, NEIm2ColFixture, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)), - conv_args), - framework::dataset::make("ChannelsFirstOutputNHWC", false))) +FIXTURE_DATA_TEST_CASE(RunSmall, NEIm2ColFixture, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)), + conv_args)) { // Validate output validate(Accessor(_target), _reference); } -FIXTURE_DATA_TEST_CASE(RunLarge, NEIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F16)), - conv_args), - framework::dataset::make("ChannelsFirstOutputNHWC", false))) +FIXTURE_DATA_TEST_CASE(RunLarge, NEIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F16)), + conv_args)) { // Validate output validate(Accessor(_target), _reference); @@ -118,16 +114,14 @@ TEST_SUITE_END() TEST_SUITE_END() TEST_SUITE(QASYMM8) -FIXTURE_DATA_TEST_CASE(RunSmall, NEIm2ColFixture, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::QASYMM8)), - conv_args), - framework::dataset::make("ChannelsFirstOutputNHWC", false))) +FIXTURE_DATA_TEST_CASE(RunSmall, NEIm2ColFixture, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::QASYMM8)), + conv_args)) { // Validate output validate(Accessor(_target), _reference); } -FIXTURE_DATA_TEST_CASE(RunLarge, NEIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::QASYMM8)), - conv_args), - framework::dataset::make("ChannelsFirstOutputNHWC", false))) +FIXTURE_DATA_TEST_CASE(RunLarge, NEIm2ColFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::QASYMM8)), + conv_args)) { // Validate output validate(Accessor(_target), _reference); diff --git a/tests/validation/fixtures/Im2ColFixture.h b/tests/validation/fixtures/Im2ColFixture.h index b5e83a9872..809bafd0b2 100644 --- a/tests/validation/fixtures/Im2ColFixture.h +++ b/tests/validation/fixtures/Im2ColFixture.h @@ -50,7 +50,7 @@ class Im2ColValidationFixture : public framework::Fixture public: template void setup(TensorShape input_shape, DataType data_type, const Size2D &kernel_dims, const PadStrideInfo &conv_info, const QuantizationInfo &quant_info, const DataLayout &data_layout, - unsigned int num_groups, bool channels_first_output_nhwc) + unsigned int num_groups) { _kernel_dims = kernel_dims; _conv_info = conv_info; @@ -70,7 +70,7 @@ public: const TensorShape output_shape = compute_im2col_conv_shape(&input_info, _kernel_dims, _conv_info, _has_bias, Size2D(1U, 1U), batch_size_on_z && _num_groups == 1, _num_groups); _target = compute_target(input_shape, output_shape, data_type); - compute_reference(input_shape, output_shape, data_type, channels_first_output_nhwc); + compute_reference(input_shape, output_shape, data_type); } protected: @@ -109,7 +109,7 @@ protected: return dst; } - void compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, DataType data_type, bool channels_first_output_nhwc) + void compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, DataType data_type) { // Create reference SimpleTensor src{ input_shape, data_type, 1, _quant_info, _data_layout }; @@ -118,7 +118,7 @@ protected: // Fill reference fill(src); - reference::im2col(src, _reference, _kernel_dims, _conv_info, _has_bias, _num_groups, channels_first_output_nhwc); + reference::im2col(src, _reference, _kernel_dims, _conv_info, _has_bias, _num_groups); } TensorType _target{}; SimpleTensor _reference{}; diff --git a/tests/validation/reference/Im2Col.cpp b/tests/validation/reference/Im2Col.cpp index 0c41d88f3e..076b2aba07 100644 --- a/tests/validation/reference/Im2Col.cpp +++ b/tests/validation/reference/Im2Col.cpp @@ -91,52 +91,6 @@ void im2col_nchw(const SimpleTensor &src, SimpleTensor &dst, const Size2D template void im2col_nhwc(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias) -{ - ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NHWC); - const int pad_x = conv_info.pad().first; - const int pad_y = conv_info.pad().second; - const int stride_x = conv_info.stride().first; - const int stride_y = conv_info.stride().second; - const int kernel_width = kernel_dims.width; - const int kernel_height = kernel_dims.height; - const int src_width = src.shape().y(); - const int src_height = src.shape().z(); - const int src_depth = src.shape().x(); - const int batches = src.shape().total_size_upper(3); - const int pad_val = is_data_type_quantized_asymmetric(src.data_type()) ? src.quantization_info().offset : 0; - int dst_idx = 0; - - const int lasty = src_height + (kernel_height > 1 ? pad_y : 0) - kernel_height; - const int lastx = src_width + (kernel_width > 1 ? pad_x : 0) - kernel_width; - - for(int b = 0; b < batches; ++b) - { - for(int y = -pad_y; y <= lasty; y += stride_y) - { - for(int x = -pad_x; x <= lastx; x += stride_x) - { - for(int z = 0; z < src_depth; ++z) - { - for(int patch_y = y; patch_y < (y + kernel_height); ++patch_y) - { - for(int patch_x = x; patch_x < (x + kernel_width); ++patch_x) - { - dst[dst_idx++] = tensor_elem_at(src, Coordinates(z, patch_x, patch_y, b), BorderMode::CONSTANT, static_cast(pad_val)); - } - } - } - - if(has_bias) - { - dst[dst_idx++] = static_cast(1); - } - } - } - } -} - -template -void im2col_nhwc_channel_first(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias) { ARM_COMPUTE_ERROR_ON(src.data_layout() != DataLayout::NHWC); const int stride_x = conv_info.stride().first; @@ -185,7 +139,7 @@ void im2col_nhwc_channel_first(const SimpleTensor &src, SimpleTensor &dst, } template -void im2col(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const unsigned int num_groups, bool channels_first_output_nhwc) +void im2col(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const unsigned int num_groups) { switch(src.data_layout()) { @@ -196,14 +150,7 @@ void im2col(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kern } case DataLayout::NHWC: { - if(channels_first_output_nhwc) - { - im2col_nhwc_channel_first(src, dst, kernel_dims, conv_info, has_bias); - } - else - { - im2col_nhwc(src, dst, kernel_dims, conv_info, has_bias); - } + im2col_nhwc(src, dst, kernel_dims, conv_info, has_bias); break; } default: @@ -214,12 +161,9 @@ void im2col(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kern } } -template void im2col(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups, - bool channels_first_output_nhwc); -template void im2col(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups, - bool channels_first_output_nhwc); -template void im2col(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups, - bool channels_first_output_nhwc); +template void im2col(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups); +template void im2col(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups); +template void im2col(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, unsigned int num_groups); } // namespace reference } // namespace validation } // namespace test diff --git a/tests/validation/reference/Im2Col.h b/tests/validation/reference/Im2Col.h index 84ee237453..f519d0e602 100644 --- a/tests/validation/reference/Im2Col.h +++ b/tests/validation/reference/Im2Col.h @@ -35,8 +35,7 @@ namespace validation namespace reference { template -void im2col(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const unsigned int num_groups, - bool channels_first_output_nhwc = false); +void im2col(const SimpleTensor &src, SimpleTensor &dst, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const unsigned int num_groups); } // namespace reference } // namespace validation } // namespace test -- cgit v1.2.1