From 432a7d4845598bf681d968e5ee4ed18fce8496c4 Mon Sep 17 00:00:00 2001 From: Ioan-Cristian Szabo Date: Thu, 12 Oct 2017 09:25:19 +0100 Subject: Porting accumulate tests to the new framework. (COMPMID-626) accumulate_validation (COMPMID-573) accumulate_weighted_validation (COMPMID-574) accumulate_squared_validation Change-Id: I6a0db46e809e03d785515ca51e867bec9c29e376 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/91758 Reviewed-by: Georgios Pinitas Reviewed-by: Pablo Tello Tested-by: Kaizen --- tests/validation/CL/Accumulate.cpp | 189 +++++++++++++++++++++++++ tests/validation/CPP/Accumulate.cpp | 103 ++++++++++++++ tests/validation/CPP/Accumulate.h | 50 +++++++ tests/validation/NEON/Accumulate.cpp | 189 +++++++++++++++++++++++++ tests/validation/fixtures/AccumulateFixture.h | 193 ++++++++++++++++++++++++++ 5 files changed, 724 insertions(+) create mode 100644 tests/validation/CL/Accumulate.cpp create mode 100644 tests/validation/CPP/Accumulate.cpp create mode 100644 tests/validation/CPP/Accumulate.h create mode 100644 tests/validation/NEON/Accumulate.cpp create mode 100644 tests/validation/fixtures/AccumulateFixture.h (limited to 'tests/validation') diff --git a/tests/validation/CL/Accumulate.cpp b/tests/validation/CL/Accumulate.cpp new file mode 100644 index 0000000000..2e33540b5d --- /dev/null +++ b/tests/validation/CL/Accumulate.cpp @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2017 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/Types.h" +#include "arm_compute/runtime/CL/CLTensor.h" +#include "arm_compute/runtime/CL/CLTensorAllocator.h" +#include "arm_compute/runtime/CL/functions/CLAccumulate.h" +#include "tests/CL/CLAccessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/ConvertPolicyDataset.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/AccumulateFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +/** Tolerance value for comparing reference's output against implementation's output for floating point data types */ +constexpr AbsoluteTolerance tolerance(1.0f); +/** Input data sets **/ +const auto AccumulateU8Dataset = combine(framework::dataset::make("DataType", DataType::U8), framework::dataset::make("DataType", DataType::U8)); +const auto AccumulateS16Dataset = combine(framework::dataset::make("DataType", DataType::U8), framework::dataset::make("DataType", DataType::S16)); +} // namespace +TEST_SUITE(CL) +TEST_SUITE(Accumulate) + +TEST_SUITE(U8) +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(framework::dataset::concat(datasets::SmallShapes(), datasets::LargeShapes()), AccumulateS16Dataset), + shape, data_type, output_data_type) +{ + // Create tensors + CLTensor ref_src = create_tensor(shape, data_type); + CLTensor dst = create_tensor(shape, output_data_type); + + // Create and Configure function + CLAccumulate accum; + accum.configure(&ref_src, &dst); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(dst.info()->valid_region(), valid_region); + + // Validate padding + const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); + validate(ref_src.info()->padding(), padding); + validate(dst.info()->padding(), padding); +} + +template +using CLAccumulateFixture = AccumulateValidationFixture; + +FIXTURE_DATA_TEST_CASE(RunSmall, CLAccumulateFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), AccumulateS16Dataset)) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLAccumulateFixture, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapes(), AccumulateS16Dataset)) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance); +} + +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE(AccumulateWeighted) + +TEST_SUITE(U8) +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(framework::dataset::concat(datasets::SmallShapes(), datasets::LargeShapes()), AccumulateU8Dataset), + shape, data_type, output_data_type) +{ + // Generate a random alpha value + std::mt19937 gen(library->seed()); + std::uniform_real_distribution<> float_dist(0, 1); + const float alpha = float_dist(gen); + + // Create tensors + CLTensor ref_src = create_tensor(shape, data_type); + CLTensor dst = create_tensor(shape, output_data_type); + + // Create and Configure function + CLAccumulateWeighted accum_weight; + accum_weight.configure(&ref_src, alpha, &dst); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(dst.info()->valid_region(), valid_region); + + // Validate padding + const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); + validate(ref_src.info()->padding(), padding); + validate(dst.info()->padding(), padding); +} + +template +using CLAccumulateWeightedFixture = AccumulateWeightedValidationFixture; + +FIXTURE_DATA_TEST_CASE(RunSmall, CLAccumulateWeightedFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), AccumulateU8Dataset)) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLAccumulateWeightedFixture, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapes(), AccumulateU8Dataset)) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance); +} + +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE(AccumulateSquared) + +TEST_SUITE(U8) +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(framework::dataset::concat(datasets::SmallShapes(), datasets::LargeShapes()), AccumulateS16Dataset), + shape, data_type, output_data_type) +{ + // Generate a random shift value + std::mt19937 gen(library->seed()); + std::uniform_int_distribution int_dist(0, 15); + const uint32_t shift = int_dist(gen); + + // Create tensors + CLTensor ref_src = create_tensor(shape, data_type); + CLTensor dst = create_tensor(shape, output_data_type); + + // Create and Configure function + CLAccumulateSquared accum_square; + accum_square.configure(&ref_src, shift, &dst); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(dst.info()->valid_region(), valid_region); + + // Validate padding + const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); + validate(ref_src.info()->padding(), padding); + validate(dst.info()->padding(), padding); +} + +template +using CLAccumulateSquaredFixture = AccumulateSquaredValidationFixture; + +FIXTURE_DATA_TEST_CASE(RunSmall, CLAccumulateSquaredFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), AccumulateS16Dataset)) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLAccumulateSquaredFixture, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapes(), AccumulateS16Dataset)) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance); +} + +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/CPP/Accumulate.cpp b/tests/validation/CPP/Accumulate.cpp new file mode 100644 index 0000000000..29a2007bbd --- /dev/null +++ b/tests/validation/CPP/Accumulate.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2017 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 "Accumulate.h" + +#include "arm_compute/core/Types.h" +#include "tests/validation/FixedPoint.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor accumulate(const SimpleTensor &src, DataType output_data_type) +{ + SimpleTensor dst{ src.shape(), output_data_type }; + + library->fill_tensor_uniform(dst, 1, static_cast(0), static_cast(std::numeric_limits::max())); + + using intermediate_type = typename common_promoted_signed_type::intermediate_type; + + for(int i = 0; i < src.num_elements(); ++i) + { + intermediate_type val = static_cast(src[i]) + static_cast(dst[i]); + dst[i] = saturate_cast(val); + } + + return dst; +} + +template +SimpleTensor accumulate_weighted(const SimpleTensor &src, float alpha, DataType output_data_type) +{ + ARM_COMPUTE_ERROR_ON_MSG(alpha < 0.f || alpha > 1.f, "Weight (alpha) specified in accumulate_weighted must be within the range [0, 1]"); + + SimpleTensor dst{ src.shape(), output_data_type }; + + library->fill_tensor_uniform(dst, 1, static_cast(0), static_cast(std::numeric_limits::max())); + + using intermediate_type = typename common_promoted_signed_type::intermediate_type; + + for(int i = 0; i < src.num_elements(); ++i) + { + double val = (1. - static_cast(alpha)) * static_cast(dst[i]) + static_cast(alpha) * static_cast(src[i]); + dst[i] = static_cast(val); + } + + return dst; +} + +template +SimpleTensor accumulate_squared(const SimpleTensor &src, uint32_t shift, DataType output_data_type) +{ + ARM_COMPUTE_ERROR_ON_MSG(shift > 15, "Shift in accumulate_squared must be within the range [0, 15]"); + + SimpleTensor dst{ src.shape(), output_data_type }; + + library->fill_tensor_uniform(dst, 1, static_cast(0), static_cast(std::numeric_limits::max())); + + using intermediate_type = typename common_promoted_signed_type::intermediate_type; + intermediate_type denom = 1 << shift; + + for(int i = 0; i < src.num_elements(); ++i) + { + intermediate_type val = static_cast(dst[i]) + (static_cast(src[i]) * static_cast(src[i]) / denom); + dst[i] = saturate_cast(val); + } + + return dst; +} + +template SimpleTensor accumulate(const SimpleTensor &src, DataType output_data_type); +template SimpleTensor accumulate_weighted(const SimpleTensor &src, float alpha, DataType output_data_type); +template SimpleTensor accumulate_squared(const SimpleTensor &src, uint32_t shift, DataType output_data_type); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/CPP/Accumulate.h b/tests/validation/CPP/Accumulate.h new file mode 100644 index 0000000000..faa570bd05 --- /dev/null +++ b/tests/validation/CPP/Accumulate.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2017 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_ACCUMULATE_H__ +#define __ARM_COMPUTE_TEST_ACCUMULATE_H__ + +#include "tests/SimpleTensor.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor accumulate(const SimpleTensor &src, DataType output_data_type); + +template +SimpleTensor accumulate_weighted(const SimpleTensor &src, float alpha, DataType output_data_type); + +template +SimpleTensor accumulate_squared(const SimpleTensor &src, uint32_t shift, DataType output_data_type); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_ACCUMULATE_H__ */ diff --git a/tests/validation/NEON/Accumulate.cpp b/tests/validation/NEON/Accumulate.cpp new file mode 100644 index 0000000000..cb6894a586 --- /dev/null +++ b/tests/validation/NEON/Accumulate.cpp @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2017 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/Types.h" +#include "arm_compute/runtime/NEON/functions/NEAccumulate.h" +#include "arm_compute/runtime/Tensor.h" +#include "arm_compute/runtime/TensorAllocator.h" +#include "tests/NEON/Accessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/ConvertPolicyDataset.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/AccumulateFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +/** Tolerance value for comparing reference's output against implementation's output for floating point data types */ +constexpr AbsoluteTolerance tolerance(1.0f); +/** Input data sets **/ +const auto AccumulateU8Dataset = combine(framework::dataset::make("DataType", DataType::U8), framework::dataset::make("DataType", DataType::U8)); +const auto AccumulateS16Dataset = combine(framework::dataset::make("DataType", DataType::U8), framework::dataset::make("DataType", DataType::S16)); +} // namespace +TEST_SUITE(NEON) +TEST_SUITE(Accumulate) + +TEST_SUITE(U8) +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(framework::dataset::concat(datasets::SmallShapes(), datasets::LargeShapes()), AccumulateS16Dataset), + shape, data_type, output_data_type) +{ + // Create tensors + Tensor ref_src = create_tensor(shape, data_type); + Tensor dst = create_tensor(shape, output_data_type); + + // Create and Configure function + NEAccumulate accum; + accum.configure(&ref_src, &dst); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(dst.info()->valid_region(), valid_region); + + // Validate padding + const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); + validate(ref_src.info()->padding(), padding); + validate(dst.info()->padding(), padding); +} + +template +using NEAccumulateFixture = AccumulateValidationFixture; + +FIXTURE_DATA_TEST_CASE(RunSmall, NEAccumulateFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), AccumulateS16Dataset)) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEAccumulateFixture, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapes(), AccumulateS16Dataset)) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance); +} + +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE(AccumulateWeighted) + +TEST_SUITE(U8) +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(framework::dataset::concat(datasets::SmallShapes(), datasets::LargeShapes()), AccumulateU8Dataset), + shape, data_type, output_data_type) +{ + // Generate a random alpha value + std::mt19937 gen(library->seed()); + std::uniform_real_distribution<> float_dist(0, 1); + const float alpha = float_dist(gen); + + // Create tensors + Tensor ref_src = create_tensor(shape, data_type); + Tensor dst = create_tensor(shape, output_data_type); + + // Create and Configure function + NEAccumulateWeighted accum_weight; + accum_weight.configure(&ref_src, alpha, &dst); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(dst.info()->valid_region(), valid_region); + + // Validate padding + const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); + validate(ref_src.info()->padding(), padding); + validate(dst.info()->padding(), padding); +} + +template +using NEAccumulateWeightedFixture = AccumulateWeightedValidationFixture; + +FIXTURE_DATA_TEST_CASE(RunSmall, NEAccumulateWeightedFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), AccumulateU8Dataset)) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEAccumulateWeightedFixture, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapes(), AccumulateU8Dataset)) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance); +} + +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE(AccumulateSquared) + +TEST_SUITE(U8) +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(framework::dataset::concat(datasets::SmallShapes(), datasets::LargeShapes()), AccumulateS16Dataset), + shape, data_type, output_data_type) +{ + // Generate a random shift value + std::mt19937 gen(library->seed()); + std::uniform_int_distribution int_dist(0, 15); + const uint32_t shift = int_dist(gen); + + // Create tensors + Tensor ref_src = create_tensor(shape, data_type); + Tensor dst = create_tensor(shape, output_data_type); + + // Create and Configure function + NEAccumulateSquared accum_square; + accum_square.configure(&ref_src, shift, &dst); + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(dst.info()->valid_region(), valid_region); + + // Validate padding + const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); + validate(ref_src.info()->padding(), padding); + validate(dst.info()->padding(), padding); +} + +template +using NEAccumulateSquaredFixture = AccumulateSquaredValidationFixture; + +FIXTURE_DATA_TEST_CASE(RunSmall, NEAccumulateSquaredFixture, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), AccumulateS16Dataset)) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEAccumulateSquaredFixture, framework::DatasetMode::NIGHTLY, combine(datasets::LargeShapes(), AccumulateS16Dataset)) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance); +} + +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/AccumulateFixture.h b/tests/validation/fixtures/AccumulateFixture.h new file mode 100644 index 0000000000..3d8ca2dafc --- /dev/null +++ b/tests/validation/fixtures/AccumulateFixture.h @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2017 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_ACCUMULATE_FIXTURE +#define ARM_COMPUTE_TEST_ACCUMULATE_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.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/CPP/Accumulate.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class AccumulateBaseValidationFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape shape, DataType data_type, DataType output_data_type) + { + _target = compute_target(shape, data_type, output_data_type); + _reference = compute_reference(shape, data_type, output_data_type); + } + +protected: + template + void fill(U &&tensor, int i, D max) + { + library->fill_tensor_uniform(tensor, i, static_cast(0), max); + } + + TensorType compute_target(const TensorShape &shape, DataType data_type, DataType output_data_type) + { + // Create tensors + TensorType ref_src = create_tensor(shape, data_type); + TensorType dst = create_tensor(shape, output_data_type); + + // Create and configure function + FunctionType accum; + accum_conf(accum, ref_src, dst); + + ARM_COMPUTE_EXPECT(ref_src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + ref_src.allocator()->allocate(); + dst.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!ref_src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + const T1 max = std::numeric_limits::max(); + + // Fill tensors + fill(AccessorType(ref_src), 0, max); + fill(AccessorType(dst), 1, static_cast(max)); + + // Compute function + accum.run(); + + return dst; + } + + SimpleTensor compute_reference(const TensorShape &shape, DataType data_type, DataType output_data_type) + { + // Create reference + SimpleTensor ref_src{ shape, data_type }; + + const T1 max = std::numeric_limits::max(); + + // Fill reference + fill(ref_src, 0, max); + + return accum_ref(ref_src, output_data_type); + } + + virtual void accum_conf(FunctionType &func, const TensorType &input, TensorType &accum) = 0; + + virtual SimpleTensor accum_ref(const SimpleTensor &input, DataType output_data_type) = 0; + + TensorType _target{}; + SimpleTensor _reference{}; +}; + +template +class AccumulateValidationFixture : public AccumulateBaseValidationFixture +{ +public: + template + void setup(TensorShape shape, DataType data_type, DataType output_data_type) + { + AccumulateBaseValidationFixture::setup(shape, data_type, output_data_type); + } + + virtual void accum_conf(FunctionType &func, const TensorType &input, TensorType &accum) override + { + func.configure(&input, &accum); + } + + virtual SimpleTensor accum_ref(const SimpleTensor &input, DataType output_data_type) override + { + return reference::accumulate(input, output_data_type); + } +}; + +template +class AccumulateWeightedValidationFixture : public AccumulateBaseValidationFixture +{ +public: + template + void setup(TensorShape shape, DataType data_type, DataType output_data_type) + { + std::mt19937 gen(library->seed()); + std::uniform_real_distribution<> float_dist(0, 1); + + _alpha = float_dist(gen); + + AccumulateBaseValidationFixture::setup(shape, data_type, output_data_type); + } + + virtual void accum_conf(FunctionType &func, const TensorType &input, TensorType &accum) override + { + func.configure(&input, _alpha, &accum); + } + + virtual SimpleTensor accum_ref(const SimpleTensor &input, DataType output_data_type) override + { + return reference::accumulate_weighted(input, _alpha, output_data_type); + } + + float _alpha{ 0.f }; +}; + +template +class AccumulateSquaredValidationFixture : public AccumulateBaseValidationFixture +{ +public: + template + void setup(TensorShape shape, DataType data_type, DataType output_data_type) + { + std::mt19937 gen(library->seed()); + std::uniform_int_distribution int_dist(0, 15); + + _shift = int_dist(gen); + + AccumulateBaseValidationFixture::setup(shape, data_type, output_data_type); + } + + virtual void accum_conf(FunctionType &func, const TensorType &input, TensorType &accum) override + { + func.configure(&input, _shift, &accum); + } + + virtual SimpleTensor accum_ref(const SimpleTensor &input, DataType output_data_type) override + { + return reference::accumulate_squared(input, _shift, output_data_type); + } + + uint32_t _shift{ 0U }; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_ACCUMULATE_FIXTURE */ -- cgit v1.2.1