From 375156937a0783432c5d18e199b5d8d2b3ec33f7 Mon Sep 17 00:00:00 2001 From: ramelg01 Date: Sat, 26 Feb 2022 22:06:20 +0000 Subject: Implementation of ClPooling3d - For NDHWC layout - For F16 and F32 data types - Mixed Precision stil not supported Resolves: COMPMID-4670 Signed-off-by: ramy.elgammal@arm.com Change-Id: I0e14a13e4625569e8e5ee67e6033bd1efe0da469 Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/7262 Comments-Addressed: Arm Jenkins Reviewed-by: SiCong Li Reviewed-by: Gunes Bayir Tested-by: Arm Jenkins --- tests/datasets/Pooling3dLayerDataset.h | 121 +++++++++ tests/datasets/ShapeDatasets.h | 28 +++ tests/validation/CL/Pooling3dLayer.cpp | 283 ++++++++++++++++++++++ tests/validation/fixtures/Pooling3dLayerFixture.h | 156 ++++++++++++ tests/validation/reference/Pool3D.cpp | 221 ----------------- tests/validation/reference/Pool3D.h | 49 ---- tests/validation/reference/Pooling3dLayer.cpp | 220 +++++++++++++++++ tests/validation/reference/Pooling3dLayer.h | 50 ++++ 8 files changed, 858 insertions(+), 270 deletions(-) create mode 100644 tests/datasets/Pooling3dLayerDataset.h create mode 100644 tests/validation/CL/Pooling3dLayer.cpp create mode 100644 tests/validation/fixtures/Pooling3dLayerFixture.h delete mode 100644 tests/validation/reference/Pool3D.cpp delete mode 100644 tests/validation/reference/Pool3D.h create mode 100644 tests/validation/reference/Pooling3dLayer.cpp create mode 100644 tests/validation/reference/Pooling3dLayer.h (limited to 'tests') diff --git a/tests/datasets/Pooling3dLayerDataset.h b/tests/datasets/Pooling3dLayerDataset.h new file mode 100644 index 0000000000..cfe970e8be --- /dev/null +++ b/tests/datasets/Pooling3dLayerDataset.h @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2022 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_POOLING_3D_LAYER_DATASET +#define ARM_COMPUTE_TEST_POOLING_3D_LAYER_DATASET + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "utils/TypePrinter.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class Pooling3dLayerDataset +{ +public: + using type = std::tuple; + + struct iterator + { + iterator(std::vector::const_iterator src_it, + std::vector::const_iterator infos_it) + : _src_it{ std::move(src_it) }, + _infos_it{ std::move(infos_it) } + { + } + + std::string description() const + { + std::stringstream description; + description << "In=" << *_src_it << ":"; + description << "Info=" << *_infos_it << ":"; + return description.str(); + } + + Pooling3dLayerDataset::type operator*() const + { + return std::make_tuple(*_src_it, *_infos_it); + } + + iterator &operator++() + { + ++_src_it; + ++_infos_it; + + return *this; + } + + private: + std::vector::const_iterator _src_it; + std::vector::const_iterator _infos_it; + }; + + iterator begin() const + { + return iterator(_src_shapes.begin(), _infos.begin()); + } + + int size() const + { + return std::min(_src_shapes.size(), _infos.size()); + } + + void add_config(TensorShape src, Pooling3dLayerInfo info) + { + _src_shapes.emplace_back(std::move(src)); + _infos.emplace_back(std::move(info)); + } + +protected: + Pooling3dLayerDataset() = default; + Pooling3dLayerDataset(Pooling3dLayerDataset &&) = default; + +private: + std::vector _src_shapes{}; + std::vector _infos{}; +}; + +// Special pooling dataset +class Pooling3dLayerDatasetSpecial final : public Pooling3dLayerDataset +{ +public: + Pooling3dLayerDatasetSpecial() + { + // Special cases + add_config(TensorShape(2U, 3U, 4U, 2U, 4U), Pooling3dLayerInfo(PoolingType::AVG, /*pool size*/ Size3D(2, 2, 1), /*pool strides*/ Size3D(3, 3, 1), /*pool padding*/ Padding3D(0, 0, 0), true)); + add_config(TensorShape(20U, 22U, 10U, 2U), Pooling3dLayerInfo(PoolingType::AVG, Size3D(100, 100, 100), Size3D(5, 5, 5), Padding3D(50, 50, 50), true)); + add_config(TensorShape(10U, 20U, 32U, 3U, 2U), Pooling3dLayerInfo(PoolingType::MAX, /*pool size*/ 3, /*pool strides*/ Size3D(2, 2, 2), Padding3D(1, 1, 1, 1, 1, 1), false, false, + DimensionRoundingType::FLOOR)); + add_config(TensorShape(14U, 10U, 10U, 3U, 5U), Pooling3dLayerInfo(PoolingType::AVG, Size3D(3, 3, 3), /*pool strides*/ Size3D(3, 3, 3), Padding3D(2, 1, 2), true, false, DimensionRoundingType::CEIL)); + add_config(TensorShape(14U, 10U, 10U, 2U, 4U), Pooling3dLayerInfo(PoolingType::AVG, Size3D(3, 3, 3), /*pool strides*/ Size3D(3, 3, 3), Padding3D(2, 1, 2), false, false, DimensionRoundingType::CEIL)); + add_config(TensorShape(15U, 13U, 13U, 3U, 5U), Pooling3dLayerInfo(PoolingType::AVG, Size3D(4, 4, 4), /*pool strides*/ Size3D(2, 2, 2), Padding3D(2, 2, 2), true, false, DimensionRoundingType::CEIL)); + } +}; +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_POOLING_3D_LAYER_DATASET */ diff --git a/tests/datasets/ShapeDatasets.h b/tests/datasets/ShapeDatasets.h index e21589946b..31c9b9e051 100644 --- a/tests/datasets/ShapeDatasets.h +++ b/tests/datasets/ShapeDatasets.h @@ -540,6 +540,21 @@ public: } }; +/** Data set containing small 5D tensor shapes. */ +class Small5dShapes final : public ShapeDataset +{ +public: + Small5dShapes() + : ShapeDataset("Shape", + { + TensorShape{ 5U, 5U, 7U, 4U, 3U }, + TensorShape{ 5U, 5U, 4U, 13U, 2U }, + TensorShape{ 5U, 5U, 3U, 5U , 2U}, + }) + { + } +}; + /** Data set containing large 5x5 tensor shapes. */ class Large5x5Shapes final : public ShapeDataset { @@ -553,6 +568,19 @@ public: } }; +/** Data set containing large 5D tensor shapes. */ +class Large5dShapes final : public ShapeDataset +{ +public: + Large5dShapes() + : ShapeDataset("Shape", + { + TensorShape{ 30U, 40U, 30U, 32U, 3U } + }) + { + } +}; + /** Data set containing small 5x1 tensor shapes. */ class Small5x1Shapes final : public ShapeDataset { diff --git a/tests/validation/CL/Pooling3dLayer.cpp b/tests/validation/CL/Pooling3dLayer.cpp new file mode 100644 index 0000000000..5c80351da2 --- /dev/null +++ b/tests/validation/CL/Pooling3dLayer.cpp @@ -0,0 +1,283 @@ +/* + * Copyright (c) 2022 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "arm_compute/core/TensorShape.h" +#include "tests/framework/datasets/Datasets.h" + +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/CL/CLTensor.h" +#include "arm_compute/runtime/CL/CLTensorAllocator.h" +#include "arm_compute/runtime/CL/functions/CLPooling3dLayer.h" +#include "tests/CL/CLAccessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/Pooling3dLayerDataset.h" +#include "tests/datasets/PoolingTypesDataset.h" +#include "tests/datasets/ShapeDatasets.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/Pooling3dLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +/** Input data sets for floating-point data types */ +const auto Pooling3dLayerDatasetFP = combine(combine(combine(combine(datasets::PoolingTypes(), framework::dataset::make("PoolingSize", { Size3D(2, 3, 2) })), + framework::dataset::make("Stride", { Size3D(1, 1, 1), Size3D(2, 1, 1), Size3D(1, 2, 1), Size3D(2, 2, 1) })), + framework::dataset::make("Padding", { Padding3D(0, 1, 0), Padding3D(1, 1, 1) })), + framework::dataset::make("ExcludePadding", { true, false })); + +const auto Pooling3dLayerDatasetFPSmall = combine(combine(combine(combine(datasets::PoolingTypes(), framework::dataset::make("PoolingSize", { Size3D(2, 2, 2), Size3D(3, 3, 3) })), + framework::dataset::make("Stride", { Size3D(2, 2, 2), Size3D(2, 1, 1) })), + framework::dataset::make("Padding", { Padding3D(0, 0, 0), Padding3D(1, 1, 1), Padding3D(1, 0, 0) })), + framework::dataset::make("ExcludePadding", { true, false })); + +using ShapeDataset = framework::dataset::ContainerDataset>; + +constexpr AbsoluteTolerance tolerance_f32(0.001f); /**< Tolerance value for comparing reference's output against implementation's output for 32-bit floating-point type */ +constexpr AbsoluteTolerance tolerance_f16(0.1f); /**< Tolerance value for comparing reference's output against implementation's output for 16-bit floating-point type */ + +} // namespace + +TEST_SUITE(CL) +TEST_SUITE(Pooling3dLayer) + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip( + framework::dataset::make("InputInfo", { TensorInfo(TensorShape(2U, 27U, 13U, 4U, 3U), 1, DataType::F32, DataLayout::NDHWC), // Mismatching data type + TensorInfo(TensorShape(2U, 27U, 13U, 4U, 2U), 1, DataType::F32, DataLayout::NDHWC), // Invalid pad/size combination + TensorInfo(TensorShape(2U, 27U, 13U, 4U, 2U), 1, DataType::F32, DataLayout::NDHWC), // Invalid pad/size combination + TensorInfo(TensorShape(2U, 27U, 13U, 4U, 3U), 1, DataType::F32, DataLayout::NDHWC), // Invalid output shape + TensorInfo(TensorShape(5U, 13U, 15U, 2U, 3U), 1, DataType::F32, DataLayout::NDHWC), // Global Pooling + TensorInfo(TensorShape(13U,13U, 5U, 1U, 2U), 1, DataType::F32, DataLayout::NDHWC), // Invalid output Global Pooling + TensorInfo(TensorShape(5U, 13U, 13U, 4U, 4U), 1, DataType::F32, DataLayout::NDHWC), // Invalid data type + TensorInfo(TensorShape(5U, 13U, 13U, 4U, 4U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(5U, 13U, 13U, 5U, 4U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(1U, 16U, 1U, 3U, 4U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(5U, 13U, 13U, 4U, 3U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(5U, 13U, 13U, 4U, 2U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(5U, 13U, 13U, 4U, 3U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(5U, 13U, 13U, 4U, 3U), 1, DataType::F32, DataLayout::NDHWC), + }), + framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(2U, 25U, 11U, 3U, 3U), 1, DataType::F16, DataLayout::NDHWC), + TensorInfo(TensorShape(2U, 30U, 11U, 3U, 2U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(2U, 25U, 16U, 3U, 2U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(2U, 27U, 13U, 3U, 3U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(5U, 1U, 1U, 1U, 3U), 1, DataType::F32, DataLayout::NDHWC), // Global pooling applied + TensorInfo(TensorShape(5U, 2U, 2U, 2U, 2U), 1, DataType::F32, DataLayout::NDHWC), // Invalid output Global Pooling + TensorInfo(TensorShape(5U, 12U, 12U, 3U, 4U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(5U, 12U, 12U, 3U, 4U), 1, DataType::QASYMM8, DataLayout::NDHWC), // Invalid data type + TensorInfo(TensorShape(5U, 1U, 1U, 1U, 4U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(1U, 15U, 1U, 2U, 4U), 1, DataType::F32, DataLayout::NDHWC), // Output width larger than input + TensorInfo(TensorShape(5U, 6U, 6U, 2U, 3U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(5U, 6U, 6U, 2U, 2U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(5U, 6U, 6U, 2U, 3U), 1, DataType::F32, DataLayout::NDHWC), + TensorInfo(TensorShape(5U, 6U, 6U, 2U, 3U), 1, DataType::F32, DataLayout::NDHWC), + })), + framework::dataset::make("PoolInfo", { Pooling3dLayerInfo(PoolingType::AVG, 3, Size3D(1, 1, 1), Padding3D(0, 0, 0)), + Pooling3dLayerInfo(PoolingType::AVG, 2, Size3D(1, 1, 1), Padding3D(2, 0, 0)), + Pooling3dLayerInfo(PoolingType::AVG, 2, Size3D(1, 1, 1), Padding3D(0, 0, 0)), + Pooling3dLayerInfo(PoolingType::L2, 3, Size3D(1, 1, 1), Padding3D(0, 0, 0)), + Pooling3dLayerInfo(PoolingType::AVG), + Pooling3dLayerInfo(PoolingType::MAX), + Pooling3dLayerInfo(PoolingType::AVG, 2, Size3D(), Padding3D(), false), + Pooling3dLayerInfo(PoolingType::AVG, 2, Size3D(1U, 1U, 1U), Padding3D(), false), + Pooling3dLayerInfo(PoolingType::AVG), + Pooling3dLayerInfo(PoolingType::MAX, 2, Size3D(1, 1, 2), Padding3D(0, 0, 0), false), + Pooling3dLayerInfo(PoolingType::AVG, 2, Size3D(2U, 2U, 2U), Padding3D(), false), + Pooling3dLayerInfo(PoolingType::AVG, 1, Size3D(2U, 2U, 2U), Padding3D(2, 2, 2), true), // Pool size is smaller than the padding size with padding excluded + Pooling3dLayerInfo(PoolingType::AVG, 1, Size3D(2U, 2U, 2U), Padding3D(2, 2, 2), false), // Pool size is smaller than the padding size with padding included + Pooling3dLayerInfo(PoolingType::AVG, 3, Size3D(2U, 2U, 2U), Padding3D(2,1,2,2,1,2), false, false, DimensionRoundingType::CEIL), // CEIL with asymmetric Padding + })), + framework::dataset::make("Expected", { false, false, false, false, true, false, false, false, true , false, true, false, false, false})), + input_info, output_info, pool_info, expected) +{ + ARM_COMPUTE_EXPECT(bool(CLPooling3dLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), pool_info)) == expected, framework::LogLevel::ERRORS); +} + + +template +using CLPooling3dLayerFixture = Pooling3dLayerValidationFixture; + +template +using CLSpecialPooling3dLayerFixture = SpecialPooling3dLayerValidationFixture; + +template +using CLPooling3dLayerGlobalFixture = Pooling3dLayerGlobalValidationFixture; + +// clang-format on +// *INDENT-ON* +TEST_SUITE(Float) +TEST_SUITE(FP32) + +FIXTURE_DATA_TEST_CASE(RunSpecial, CLSpecialPooling3dLayerFixture, framework::DatasetMode::ALL, datasets::Pooling3dLayerDatasetSpecial() * framework::dataset::make("DataType", DataType::F32)) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f32); +} + +FIXTURE_DATA_TEST_CASE(RunSmall, CLPooling3dLayerFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::Small5dShapes(), combine(Pooling3dLayerDatasetFPSmall, + framework::dataset::make("DataType", DataType::F32)))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f32); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, CLPooling3dLayerFixture, framework::DatasetMode::NIGHTLY, combine(datasets::Large5dShapes(), combine(Pooling3dLayerDatasetFP, + framework::dataset::make("DataType", + DataType::F32)))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f32); +} + +TEST_SUITE(GlobalPooling) +// *INDENT-OFF* +// clang-format off +FIXTURE_DATA_TEST_CASE(RunSmall, CLPooling3dLayerFixture, framework::DatasetMode::ALL, + combine(combine(combine(combine(combine(combine( + framework::dataset::make("InputShape", { TensorShape(3U, 27U, 13U, 4U), + TensorShape(4U, 27U, 13U, 4U, 2U) + }), + framework::dataset::make("PoolingType", { PoolingType::AVG, PoolingType::L2, PoolingType::MAX })), + framework::dataset::make("PoolingSize", { Size3D(27, 13, 4) })), + framework::dataset::make("Strides", Size3D(1, 1, 1))), + framework::dataset::make("Paddings", Padding3D(0, 0, 0))), + framework::dataset::make("ExcludePadding", false)), + framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f32); +} + +FIXTURE_DATA_TEST_CASE(RunSmallGlobal, CLPooling3dLayerGlobalFixture, framework::DatasetMode::ALL, + combine(combine( + framework::dataset::make("InputShape", { TensorShape(27U, 13U, 4U, 3U), + TensorShape(27U, 13U, 4U, 4U, 2U) + }), + framework::dataset::make("PoolingType", { PoolingType::AVG, PoolingType::L2, PoolingType::MAX })), + framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f32); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLPooling3dLayerFixture, framework::DatasetMode::NIGHTLY, + combine(combine(combine(combine(combine(combine( + framework::dataset::make("InputShape", { TensorShape(4U, 79U, 37U, 11U), + TensorShape(4U, 79U, 37U, 11U, 2U) + }), + framework::dataset::make("PoolingType", { PoolingType::AVG, PoolingType::L2, PoolingType::MAX })), + framework::dataset::make("PoolingSize", { Size3D(79, 37, 11) })), + framework::dataset::make("Strides", Size3D(1, 1, 1))), + framework::dataset::make("Paddings", Padding3D(0, 0, 0))), + framework::dataset::make("ExcludePadding", false)), + framework::dataset::make("DataType", DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f32); +} +// clang-format on +// *INDENT-ON* +TEST_SUITE_END() // GlobalPooling +TEST_SUITE_END() // FP32 + +TEST_SUITE(FP16) + +FIXTURE_DATA_TEST_CASE(RunSmall, CLPooling3dLayerFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::Small5x5Shapes(), combine(Pooling3dLayerDatasetFPSmall, + framework::dataset::make("DataType", DataType::F16)))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f16); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, CLPooling3dLayerFixture, framework::DatasetMode::NIGHTLY, combine(datasets::Large5dShapes(), combine(Pooling3dLayerDatasetFP, + framework::dataset::make("DataType", + DataType::F16)))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f16); +} + +TEST_SUITE(GlobalPooling) +// *INDENT-OFF* +// clang-format off +FIXTURE_DATA_TEST_CASE(RunSmall, CLPooling3dLayerFixture, framework::DatasetMode::ALL, + combine(combine(combine(combine(combine(combine( + framework::dataset::make("InputShape", { TensorShape(3U, 27U, 13U, 4U), + TensorShape(4U, 27U, 13U, 4U, 2U) + }), + framework::dataset::make("PoolingType", { PoolingType::AVG, PoolingType::L2, PoolingType::MAX })), + framework::dataset::make("PoolingSize", { Size3D(27, 13, 4) })), + framework::dataset::make("Strides", Size3D(1, 1, 1))), + framework::dataset::make("Paddings", Padding3D(0, 0, 0))), + framework::dataset::make("ExcludePadding", false)), + framework::dataset::make("DataType", DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f16); +} + +FIXTURE_DATA_TEST_CASE(RunSmallGlobal, CLPooling3dLayerGlobalFixture, framework::DatasetMode::ALL, + combine(combine( + framework::dataset::make("InputShape", { TensorShape(27U, 13U, 4U, 3U), + TensorShape(27U, 13U, 4U, 4U, 2U) + }), + framework::dataset::make("PoolingType", { PoolingType::AVG, PoolingType::L2, PoolingType::MAX })), + framework::dataset::make("DataType", DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f16); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLPooling3dLayerFixture, framework::DatasetMode::NIGHTLY, + combine(combine(combine(combine(combine(combine( + framework::dataset::make("InputShape", { TensorShape(4U, 79U, 37U, 11U), + TensorShape(4U, 79U, 37U, 11U, 2U) + }), + framework::dataset::make("PoolingType", { PoolingType::AVG, PoolingType::L2, PoolingType::MAX })), + framework::dataset::make("PoolingSize", { Size3D(79, 37, 11) })), + framework::dataset::make("Strides", Size3D(1, 1, 1))), + framework::dataset::make("Paddings", Padding3D(0, 0, 0))), + framework::dataset::make("ExcludePadding", false)), + framework::dataset::make("DataType", DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f16); +} +// clang-format on +// *INDENT-ON* +TEST_SUITE_END() // GlobalPooling +TEST_SUITE_END() // FP16 +TEST_SUITE_END() // Float +TEST_SUITE_END() // Pooling3dLayer +TEST_SUITE_END() // CL +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/Pooling3dLayerFixture.h b/tests/validation/fixtures/Pooling3dLayerFixture.h new file mode 100644 index 0000000000..c1b3519e80 --- /dev/null +++ b/tests/validation/fixtures/Pooling3dLayerFixture.h @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2022 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_POOLING_3D_LAYER_FIXTURE +#define ARM_COMPUTE_TEST_POOLING_3D_LAYER_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "arm_compute/runtime/Tensor.h" +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/reference/Pooling3dLayer.h" +#include +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class Pooling3dLayerValidationGenericFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape shape, Pooling3dLayerInfo pool_info, DataType data_type) + { + _target = compute_target(shape, pool_info, data_type); + _reference = compute_reference(shape, pool_info, data_type); + } + +protected: + template + void fill(U &&tensor) + { + if(tensor.data_type() == DataType::F32) + { + std::uniform_real_distribution distribution(-1.0f, 1.0f); + library->fill(tensor, distribution, 0); + } + else if(tensor.data_type() == DataType::F16) + { + arm_compute::utils::uniform_real_distribution_16bit distribution{ -1.0f, 1.0f }; + library->fill(tensor, distribution, 0); + } + else // data type is quantized_asymmetric + { + ARM_COMPUTE_ERROR("Passed Type Not Supported"); + } + } + + TensorType compute_target(TensorShape shape, Pooling3dLayerInfo info, + DataType data_type) + { + // Create tensors + TensorType src = create_tensor(shape, data_type, 1, QuantizationInfo(), DataLayout::NDHWC); + const TensorShape dst_shape = misc::shape_calculator::compute_pool3d_shape((src.info()->tensor_shape()), info); + TensorType dst = create_tensor(dst_shape, data_type, 1, QuantizationInfo(), DataLayout::NDHWC); + + // Create and configure function + FunctionType pool_layer; + pool_layer.validate(src.info(), dst.info(), info); + pool_layer.configure(&src, &dst, info); + + ARM_COMPUTE_ASSERT(src.info()->is_resizable()); + ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); + + // Allocate tensors + src.allocator()->allocate(); + dst.allocator()->allocate(); + + ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); + ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); + + // Fill tensors + fill(AccessorType(src)); + + // Compute function + pool_layer.run(); + return dst; + } + + SimpleTensor compute_reference(TensorShape shape, Pooling3dLayerInfo info, DataType data_type) + { + // Create reference + SimpleTensor src(shape, data_type, 1, QuantizationInfo(), DataLayout::NDHWC); + // Fill reference + fill(src); + return reference::pooling_3d_layer(src, info); + } + + TensorType _target{}; + SimpleTensor _reference{}; +}; + +template +class Pooling3dLayerValidationFixture : public Pooling3dLayerValidationGenericFixture +{ +public: + template + void setup(TensorShape shape, PoolingType pool_type, Size3D pool_size, Size3D stride, Padding3D padding, bool exclude_padding, DataType data_type) + { + Pooling3dLayerValidationGenericFixture::setup(shape, Pooling3dLayerInfo(pool_type, pool_size, stride, padding, exclude_padding), + data_type); + } +}; + +template +class Pooling3dLayerGlobalValidationFixture : public Pooling3dLayerValidationGenericFixture +{ +public: + template + void setup(TensorShape shape, PoolingType pool_type, DataType data_type) + { + Pooling3dLayerValidationGenericFixture::setup(shape, Pooling3dLayerInfo(pool_type), data_type); + } +}; + +template +class SpecialPooling3dLayerValidationFixture : public Pooling3dLayerValidationGenericFixture +{ +public: + template + void setup(TensorShape src_shape, Pooling3dLayerInfo pool_info, DataType data_type) + { + Pooling3dLayerValidationGenericFixture::setup(src_shape, pool_info, data_type); + } +}; + +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_POOLING_3D_LAYER_FIXTURE */ diff --git a/tests/validation/reference/Pool3D.cpp b/tests/validation/reference/Pool3D.cpp deleted file mode 100644 index 85a594e262..0000000000 --- a/tests/validation/reference/Pool3D.cpp +++ /dev/null @@ -1,221 +0,0 @@ -/* - * Copyright (c) 2022 Arm Limited. - * - * SPDX-License-Identifier: MIT - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "Pool3D.h" -#include "arm_compute/core/utils/misc/ShapeCalculator.h" -#include "tests/validation/Helpers.h" - -namespace arm_compute -{ -namespace test -{ -namespace validation -{ -namespace reference -{ -using namespace arm_compute::misc::shape_calculator; - -template -SimpleTensor pool3d_internal(const SimpleTensor &src, const Pool3DInfo &pool3d_info, SimpleTensor *indices) -{ - TensorShape pooled_shape = compute_pool3d_shape(src.shape(), pool3d_info); - SimpleTensor dst{ pooled_shape, src.data_type(), 1 }; - - if(indices != nullptr) - { - *indices = SimpleTensor { pooled_shape, DataType::U32, 1 }; - } - - const int idx_channel = 0; - const int idx_width = 1; - const int idx_height = 2; - const int idx_depth = 3; - const int idx_batch = 4; - - const int pool_size_width = pool3d_info.is_global_pooling ? src.shape()[idx_width] : pool3d_info.pool_size.width; - const int pool_size_height = pool3d_info.is_global_pooling ? src.shape()[idx_height] : pool3d_info.pool_size.height; - const int pool_size_depth = pool3d_info.is_global_pooling ? src.shape()[idx_depth] : pool3d_info.pool_size.depth; - - const int pool_stride_width = static_cast(pool3d_info.strides.width); - const int pool_stride_height = static_cast(pool3d_info.strides.height); - const int pool_stride_depth = static_cast(pool3d_info.strides.depth); - - const int pad_left = static_cast(pool3d_info.padding.left); - const int pad_top = static_cast(pool3d_info.padding.top); - const int pad_front = static_cast(pool3d_info.padding.front); - - const int pad_right = static_cast(pool3d_info.padding.right); - const int pad_bottom = static_cast(pool3d_info.padding.bottom); - const int pad_back = static_cast(pool3d_info.padding.back); - - const int num_channels = static_cast(src.shape()[idx_channel]); - const int num_batches = static_cast(src.shape()[idx_batch]); - - ARM_COMPUTE_ERROR_ON(num_channels != static_cast(dst.shape()[idx_channel])); - ARM_COMPUTE_ERROR_ON(num_batches != static_cast(dst.shape()[idx_batch])); - - const int w_src = static_cast(src.shape()[idx_width]); - const int h_src = static_cast(src.shape()[idx_height]); - const int d_src = static_cast(src.shape()[idx_depth]); - const int w_dst = static_cast(dst.shape()[idx_width]); - const int h_dst = static_cast(dst.shape()[idx_height]); - const int d_dst = static_cast(dst.shape()[idx_depth]); - - const bool exclude_padding = pool3d_info.exclude_padding; - - const int height_stride_src = num_channels * w_src; - const int depth_stride_src = height_stride_src * h_src; - const int batch_stride_src = depth_stride_src * d_src; - const int height_stride_dst = num_channels * w_dst; - const int depth_stride_dst = height_stride_dst * h_dst; - const int batch_stride_dst = depth_stride_dst * d_dst; - - for(int b = 0; b < num_batches; ++b) - { - const int batch_offset_dst = b * batch_stride_dst; - const int batch_offset_src = b * batch_stride_src; - for(int c = 0; c < num_channels; ++c) - { - for(int d = 0; d < d_dst; ++d) - { - const int depth_offset_dst = d * depth_stride_dst; - for(int h = 0; h < h_dst; ++h) - { - const int height_offset_dst = h * height_stride_dst; - for(int w = 0; w < w_dst; ++w) - { - int wstart = w * pool_stride_width - pad_left; - int hstart = h * pool_stride_height - pad_top; - int dstart = d * pool_stride_depth - pad_front; - int wend = std::min(wstart + pool_size_width, w_src + pad_right); - int hend = std::min(hstart + pool_size_height, h_src + pad_bottom); - int dend = std::min(dstart + pool_size_depth, d_src + pad_back); - - // this may not be equal to pool_w * pool_h * pool_d because of - // DimensionRoundingType choice (CEIL) - int pool_size = (dend - dstart) * (hend - hstart) * (wend - wstart); - - // limit [start, end) to [0, w_src) - wstart = std::max(wstart, 0); - hstart = std::max(hstart, 0); - dstart = std::max(dstart, 0); - wend = std::min(wend, w_src); - hend = std::min(hend, h_src); - dend = std::min(dend, d_src); - - auto max_val = -std::numeric_limits::infinity(); - int max_index{ 0 }; - T avg_val = static_cast(0.f); - T l2_val = static_cast(0.f); - - if(exclude_padding) - { - pool_size = (dend - dstart) * (hend - hstart) * (wend - wstart); - } - - for(int z = dstart; z < dend; ++z) - { - const int depth_offset_src = z * depth_stride_src; - for(int y = hstart; y < hend; ++y) - { - const int height_offset_src = y * height_stride_src; - for(int x = wstart; x < wend; ++x) - { - const auto val = static_cast( - src[batch_offset_src + depth_offset_src + height_offset_src + x * num_channels + c]); - - if(val > max_val) - { - max_val = val; - max_index = coord2index(src.shape(), Coordinates(c, x, y, z, 0)); - } - - avg_val += val; - l2_val += val * val; - } - } - } - - avg_val /= pool_size; - l2_val = static_cast(std::sqrt(l2_val / pool_size)); - - int dst_index = batch_offset_dst + depth_offset_dst + height_offset_dst + w * num_channels + c; - switch(pool3d_info.pool_type) - { - case PoolingType::MAX: - dst[dst_index] = static_cast(max_val); - break; - case PoolingType::AVG: - dst[dst_index] = static_cast(avg_val); - break; - case PoolingType::L2: - dst[dst_index] = static_cast(l2_val); - break; - default: - ARM_COMPUTE_ERROR("Pooling Type should be either MAX, AVG or L2"); - } - - if(indices != nullptr) - { - (*indices)[dst_index] = max_index; - } - } - } - } - } - } - - return dst; -} - -template SimpleTensor pool3d(const SimpleTensor &src, const Pool3DInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor *indices); -template SimpleTensor pool3d(const SimpleTensor &src, const Pool3DInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor *indices); - -template -SimpleTensor pool3d(const SimpleTensor &src, const Pool3DInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor *indices) -{ - ARM_COMPUTE_UNUSED(output_qinfo); - return pool3d_internal(src, pool3d_info, indices); -} - -template <> -SimpleTensor pool3d(const SimpleTensor &src, const Pool3DInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor *indices) -{ - SimpleTensor src_tmp = convert_from_asymmetric(src); - SimpleTensor dst_tmp = pool3d_internal(src_tmp, pool3d_info, indices); - return convert_to_asymmetric(dst_tmp, output_qinfo); -} - -template <> -SimpleTensor pool3d(const SimpleTensor &src, const Pool3DInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor *indices) -{ - SimpleTensor src_tmp = convert_from_asymmetric(src); - SimpleTensor dst_tmp = pool3d_internal(src_tmp, pool3d_info, indices); - return convert_to_asymmetric(dst_tmp, output_qinfo); -} - -} // namespace reference -} // namespace validation -} // namespace test -} // namespace arm_compute \ No newline at end of file diff --git a/tests/validation/reference/Pool3D.h b/tests/validation/reference/Pool3D.h deleted file mode 100644 index bdb5744ecc..0000000000 --- a/tests/validation/reference/Pool3D.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2022 Arm Limited. - * - * SPDX-License-Identifier: MIT - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#ifndef ARM_COMPUTE_TEST_POOL3D_LAYER_H -#define ARM_COMPUTE_TEST_POOL3D_LAYER_H - -#include "Utils.h" -#include "arm_compute/core/Types.h" -#include "tests/SimpleTensor.h" -#include "tests/validation/Helpers.h" - -namespace arm_compute -{ -namespace test -{ -namespace validation -{ -namespace reference -{ -template -SimpleTensor pool3d_internal(const SimpleTensor &src, const Pool3DInfo &pool3d_info, SimpleTensor *indices); - -template -SimpleTensor pool3d(const SimpleTensor &src, const Pool3DInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor *indices); -} // namespace reference -} // namespace validation -} // namespace test -} // namespace arm_compute -#endif /* ARM_COMPUTE_TEST_POOL3D_LAYER_H */ diff --git a/tests/validation/reference/Pooling3dLayer.cpp b/tests/validation/reference/Pooling3dLayer.cpp new file mode 100644 index 0000000000..2e8f3a0b92 --- /dev/null +++ b/tests/validation/reference/Pooling3dLayer.cpp @@ -0,0 +1,220 @@ +/* + * Copyright (c) 2022 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "Pooling3dLayer.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +using namespace arm_compute::misc::shape_calculator; + +template +SimpleTensor pooling_3d_layer_internal(const SimpleTensor &src, const Pooling3dLayerInfo &pool3d_info, SimpleTensor *indices) +{ + TensorShape pooled_shape = compute_pool3d_shape(src.shape(), pool3d_info); + SimpleTensor dst{ pooled_shape, src.data_type(), 1 }; + + if(indices != nullptr) + { + *indices = SimpleTensor { pooled_shape, DataType::U32, 1 }; + } + + const int idx_channel = 0; + const int idx_width = 1; + const int idx_height = 2; + const int idx_depth = 3; + const int idx_batch = 4; + + const int pool_size_width = pool3d_info.is_global_pooling ? src.shape()[idx_width] : pool3d_info.pool_size.width; + const int pool_size_height = pool3d_info.is_global_pooling ? src.shape()[idx_height] : pool3d_info.pool_size.height; + const int pool_size_depth = pool3d_info.is_global_pooling ? src.shape()[idx_depth] : pool3d_info.pool_size.depth; + + const int pool_stride_width = static_cast(pool3d_info.stride.width); + const int pool_stride_height = static_cast(pool3d_info.stride.height); + const int pool_stride_depth = static_cast(pool3d_info.stride.depth); + + const int pad_left = static_cast(pool3d_info.padding.left); + const int pad_top = static_cast(pool3d_info.padding.top); + const int pad_front = static_cast(pool3d_info.padding.front); + + const int pad_right = static_cast(pool3d_info.padding.right); + const int pad_bottom = static_cast(pool3d_info.padding.bottom); + const int pad_back = static_cast(pool3d_info.padding.back); + + const int num_channels = static_cast(src.shape()[idx_channel]); + const int num_batches = static_cast(src.shape()[idx_batch]); + + ARM_COMPUTE_ERROR_ON(num_channels != static_cast(dst.shape()[idx_channel])); + ARM_COMPUTE_ERROR_ON(num_batches != static_cast(dst.shape()[idx_batch])); + + const int w_src = static_cast(src.shape()[idx_width]); + const int h_src = static_cast(src.shape()[idx_height]); + const int d_src = static_cast(src.shape()[idx_depth]); + const int w_dst = static_cast(dst.shape()[idx_width]); + const int h_dst = static_cast(dst.shape()[idx_height]); + const int d_dst = static_cast(dst.shape()[idx_depth]); + + const bool exclude_padding = pool3d_info.exclude_padding; + + const int height_stride_src = num_channels * w_src; + const int depth_stride_src = height_stride_src * h_src; + const int batch_stride_src = depth_stride_src * d_src; + const int height_stride_dst = num_channels * w_dst; + const int depth_stride_dst = height_stride_dst * h_dst; + const int batch_stride_dst = depth_stride_dst * d_dst; + + for(int b = 0; b < num_batches; ++b) + { + const int batch_offset_dst = b * batch_stride_dst; + const int batch_offset_src = b * batch_stride_src; + for(int c = 0; c < num_channels; ++c) + { + for(int d = 0; d < d_dst; ++d) + { + const int depth_offset_dst = d * depth_stride_dst; + for(int h = 0; h < h_dst; ++h) + { + const int height_offset_dst = h * height_stride_dst; + for(int w = 0; w < w_dst; ++w) + { + int wstart = w * pool_stride_width - pad_left; + int hstart = h * pool_stride_height - pad_top; + int dstart = d * pool_stride_depth - pad_front; + int wend = std::min(wstart + pool_size_width, w_src + pad_right); + int hend = std::min(hstart + pool_size_height, h_src + pad_bottom); + int dend = std::min(dstart + pool_size_depth, d_src + pad_back); + + // this may not be equal to pool_w * pool_h * pool_d because of + // DimensionRoundingType choice (CEIL) + int pool_size = (dend - dstart) * (hend - hstart) * (wend - wstart); + + // limit [start, end) to [0, w_src) + wstart = std::max(wstart, 0); + hstart = std::max(hstart, 0); + dstart = std::max(dstart, 0); + wend = std::min(wend, w_src); + hend = std::min(hend, h_src); + dend = std::min(dend, d_src); + + auto max_val = -std::numeric_limits::infinity(); + int max_index{ 0 }; + T avg_val = static_cast(0.f); + T l2_val = static_cast(0.f); + + if(exclude_padding) + { + pool_size = (dend - dstart) * (hend - hstart) * (wend - wstart); + } + + for(int z = dstart; z < dend; ++z) + { + const int depth_offset_src = z * depth_stride_src; + for(int y = hstart; y < hend; ++y) + { + const int height_offset_src = y * height_stride_src; + for(int x = wstart; x < wend; ++x) + { + const auto val = static_cast( + src[batch_offset_src + depth_offset_src + height_offset_src + x * num_channels + c]); + if(val > max_val) + { + max_val = val; + max_index = coord2index(src.shape(), Coordinates(c, x, y, z, 0)); + } + + avg_val += val; + l2_val += val * val; + } + } + } + + avg_val /= pool_size; + l2_val = static_cast(std::sqrt(l2_val / pool_size)); + + int dst_index = batch_offset_dst + depth_offset_dst + height_offset_dst + w * num_channels + c; + switch(pool3d_info.pool_type) + { + case PoolingType::MAX: + dst[dst_index] = static_cast(max_val); + break; + case PoolingType::AVG: + dst[dst_index] = static_cast(avg_val); + break; + case PoolingType::L2: + dst[dst_index] = static_cast(l2_val); + break; + default: + ARM_COMPUTE_ERROR("Pooling Type should be either MAX, AVG or L2"); + } + + if(indices != nullptr) + { + (*indices)[dst_index] = max_index; + } + } + } + } + } + } + + return dst; +} + +template SimpleTensor pooling_3d_layer(const SimpleTensor &src, const Pooling3dLayerInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor *indices); +template SimpleTensor pooling_3d_layer(const SimpleTensor &src, const Pooling3dLayerInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor *indices); + +template +SimpleTensor pooling_3d_layer(const SimpleTensor &src, const Pooling3dLayerInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor *indices) +{ + ARM_COMPUTE_UNUSED(output_qinfo); + return pooling_3d_layer_internal(src, pool3d_info, indices); +} + +template <> +SimpleTensor pooling_3d_layer(const SimpleTensor &src, const Pooling3dLayerInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor *indices) +{ + SimpleTensor src_tmp = convert_from_asymmetric(src); + SimpleTensor dst_tmp = pooling_3d_layer_internal(src_tmp, pool3d_info, indices); + return convert_to_asymmetric(dst_tmp, output_qinfo); +} + +template <> +SimpleTensor pooling_3d_layer(const SimpleTensor &src, const Pooling3dLayerInfo &pool3d_info, const QuantizationInfo &output_qinfo, SimpleTensor *indices) +{ + SimpleTensor src_tmp = convert_from_asymmetric(src); + SimpleTensor dst_tmp = pooling_3d_layer_internal(src_tmp, pool3d_info, indices); + return convert_to_asymmetric(dst_tmp, output_qinfo); +} + +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/Pooling3dLayer.h b/tests/validation/reference/Pooling3dLayer.h new file mode 100644 index 0000000000..481a0d3024 --- /dev/null +++ b/tests/validation/reference/Pooling3dLayer.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2022 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_POOL3D_LAYER_H +#define ARM_COMPUTE_TEST_POOL3D_LAYER_H + +#include "Utils.h" +#include "arm_compute/core/Types.h" +#include "tests/SimpleTensor.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor pooling_3d_layer_internal(const SimpleTensor &src, const Pooling3dLayerInfo &pool3d_info, SimpleTensor *indices = nullptr); + +template +SimpleTensor pooling_3d_layer(const SimpleTensor &src, const Pooling3dLayerInfo &pool3d_info, const QuantizationInfo &output_qinfo = QuantizationInfo(), + SimpleTensor *indices = nullptr); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_POOL3D_LAYER_H */ -- cgit v1.2.1