From 572ade736ab344a62afa7da214cd9407fe53a281 Mon Sep 17 00:00:00 2001 From: Moritz Pflanzer Date: Fri, 21 Jul 2017 17:36:33 +0100 Subject: COMPMID-415: Move ActivationLayer to new validation Change-Id: I38ce20d95640f9c1baf699a095c35e592ad4339f Reviewed-on: http://mpd-gerrit.cambridge.arm.com/81115 Reviewed-by: Anthony Barbier Tested-by: Kaizen --- support/ToolchainSupport.h | 11 +- tests/TypePrinter.h | 7 + tests/datasets_new/ActivationFunctionsDataset.h | 58 ++++ tests/validation/CL/ActivationLayer.cpp | 282 ------------------ tests/validation/NEON/ActivationLayer.cpp | 328 --------------------- tests/validation/Reference.cpp | 50 ---- tests/validation/Reference.h | 10 - tests/validation/ReferenceCPP.cpp | 8 - tests/validation/ReferenceCPP.h | 7 - tests/validation/TensorOperations.h | 103 ------- tests/validation/TensorVisitors.h | 20 -- tests/validation_new/CL/ActivationLayer.cpp | 245 +++++++++++++++ tests/validation_new/CPP/ActivationLayer.cpp | 158 ++++++++++ tests/validation_new/CPP/ActivationLayer.h | 47 +++ tests/validation_new/Helpers.h | 85 ++++++ tests/validation_new/NEON/ActivationLayer.cpp | 230 +++++++++++++++ .../fixtures/ActivationLayerFixture.h | 157 ++++++++++ 17 files changed, 996 insertions(+), 810 deletions(-) create mode 100644 tests/datasets_new/ActivationFunctionsDataset.h delete mode 100644 tests/validation/CL/ActivationLayer.cpp delete mode 100644 tests/validation/NEON/ActivationLayer.cpp create mode 100644 tests/validation_new/CL/ActivationLayer.cpp create mode 100644 tests/validation_new/CPP/ActivationLayer.cpp create mode 100644 tests/validation_new/CPP/ActivationLayer.h create mode 100644 tests/validation_new/NEON/ActivationLayer.cpp create mode 100644 tests/validation_new/fixtures/ActivationLayerFixture.h diff --git a/support/ToolchainSupport.h b/support/ToolchainSupport.h index 1a909d56db..c73f2486e1 100644 --- a/support/ToolchainSupport.h +++ b/support/ToolchainSupport.h @@ -51,7 +51,7 @@ namespace cpp11 * @return String representation of @p value. */ template ::type>::value, int>::type = 0> -std::string to_string(T && value) +inline std::string to_string(T && value) { std::stringstream stream; stream << std::forward(value); @@ -165,7 +165,7 @@ inline T copysign(T x, T y) * @return String representation of @p value. */ template -std::string to_string(T &&value) +inline std::string to_string(T &&value) { return ::std::to_string(std::forward(value)); } @@ -261,6 +261,13 @@ inline T copysign(T x, T y) return std::copysign(x, y); } #endif /* __ANDROID__ */ + +inline std::string to_string(bool value) +{ + std::stringstream str; + str << std::boolalpha << value; + return str.str(); +} } // namespace cpp11 namespace cpp14 diff --git a/tests/TypePrinter.h b/tests/TypePrinter.h index 10d33882ce..5f7313552e 100644 --- a/tests/TypePrinter.h +++ b/tests/TypePrinter.h @@ -240,6 +240,13 @@ inline ::std::ostream &operator<<(::std::ostream &os, const ActivationLayerInfo: return os; } +inline std::string to_string(const ActivationLayerInfo::ActivationFunction &function) +{ + std::stringstream str; + str << function; + return str.str(); +} + inline std::string to_string(const ActivationLayerInfo &info) { std::stringstream str; diff --git a/tests/datasets_new/ActivationFunctionsDataset.h b/tests/datasets_new/ActivationFunctionsDataset.h new file mode 100644 index 0000000000..0a11902d51 --- /dev/null +++ b/tests/datasets_new/ActivationFunctionsDataset.h @@ -0,0 +1,58 @@ +/* + * 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_ACTIVATIONFUNCTIONS_DATASET_H__ +#define __ARM_COMPUTE_TEST_ACTIVATIONFUNCTIONS_DATASET_H__ + +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class ActivationFunctions final : public framework::dataset::ContainerDataset> +{ +public: + ActivationFunctions() + : ContainerDataset("ActivationFunction", + { + ActivationLayerInfo::ActivationFunction::ABS, + ActivationLayerInfo::ActivationFunction::LINEAR, + ActivationLayerInfo::ActivationFunction::LOGISTIC, + ActivationLayerInfo::ActivationFunction::RELU, + ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, + ActivationLayerInfo::ActivationFunction::LEAKY_RELU, + ActivationLayerInfo::ActivationFunction::SOFT_RELU, + ActivationLayerInfo::ActivationFunction::SQRT, + ActivationLayerInfo::ActivationFunction::SQUARE, + ActivationLayerInfo::ActivationFunction::TANH + }) + { + } +}; +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_ACTIVATIONFUNCTIONS_DATASET_H__ */ diff --git a/tests/validation/CL/ActivationLayer.cpp b/tests/validation/CL/ActivationLayer.cpp deleted file mode 100644 index a1e00b681b..0000000000 --- a/tests/validation/CL/ActivationLayer.cpp +++ /dev/null @@ -1,282 +0,0 @@ -/* - * 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 "AssetsLibrary.h" -#include "CL/CLAccessor.h" -#include "Globals.h" -#include "PaddingCalculator.h" -#include "TypePrinter.h" -#include "Utils.h" -#include "validation/Datasets.h" -#include "validation/Helpers.h" -#include "validation/Reference.h" -#include "validation/Validation.h" - -#include "arm_compute/core/Helpers.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/CLActivationLayer.h" - -#include "boost_wrapper.h" - -#include -#include -#include - -using namespace arm_compute; -using namespace arm_compute::test; -using namespace arm_compute::test::validation; - -namespace -{ -/** Define tolerance of the activation layer - * - * @param[in] activation The activation function used. - * @param[in] fixed_point_position Number of bits for the fractional part.. - * - * @return Tolerance depending on the activation function. - */ -float activation_layer_tolerance(ActivationLayerInfo::ActivationFunction activation, int fixed_point_position = 0) -{ - switch(activation) - { - case ActivationLayerInfo::ActivationFunction::LOGISTIC: - case ActivationLayerInfo::ActivationFunction::SOFT_RELU: - case ActivationLayerInfo::ActivationFunction::SQRT: - case ActivationLayerInfo::ActivationFunction::TANH: - return (fixed_point_position != 0) ? 5.f : 0.00001f; - break; - default: - return 0.f; - } -} - -/** Compute CL activation layer function. - * - * @param[in] in_place Compute the activation layer in-place. - * @param[in] shape Shape of the input and output tensors. - * @param[in] dt Shape Data type of tensors. - * @param[in] act_info Activation layer information. - * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of fixed point numbers. - * - * @return Computed output tensor. - */ -CLTensor compute_activation_layer(bool in_place, const TensorShape &shape, DataType dt, ActivationLayerInfo act_info, int fixed_point_position = 0) -{ - // Create tensors - CLTensor src = create_tensor(shape, dt, 1, fixed_point_position); - CLTensor dst = create_tensor(shape, dt, 1, fixed_point_position); - - // Create and configure function - CLActivationLayer act_layer; - - if(in_place) - { - act_layer.configure(&src, nullptr, act_info); - } - else - { - act_layer.configure(&src, &dst, act_info); - } - - // Allocate tensors - src.allocator()->allocate(); - BOOST_TEST(!src.info()->is_resizable()); - - if(!in_place) - { - dst.allocator()->allocate(); - BOOST_TEST(!dst.info()->is_resizable()); - } - - // Fill tensors - if(dt == DataType::F32) - { - float min_bound = 0; - float max_bound = 0; - std::tie(min_bound, max_bound) = get_activation_layer_test_bounds(act_info.activation()); - std::uniform_real_distribution<> distribution(min_bound, max_bound); - library->fill(CLAccessor(src), distribution, 0); - } - else - { - int min_bound = 0; - int max_bound = 0; - if(dt == DataType::QS8) - { - std::tie(min_bound, max_bound) = get_activation_layer_test_bounds(act_info.activation(), fixed_point_position); - } - else - { - std::tie(min_bound, max_bound) = get_activation_layer_test_bounds(act_info.activation(), fixed_point_position); - } - std::uniform_int_distribution<> distribution(min_bound, max_bound); - library->fill(CLAccessor(src), distribution, 0); - } - - // Compute function - act_layer.run(); - - if(in_place) - { - return src; - } - else - { - return dst; - } -} -} // namespace - -#ifndef DOXYGEN_SKIP_THIS -BOOST_AUTO_TEST_SUITE(CL) -BOOST_AUTO_TEST_SUITE(ActivationLayer) - -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) -BOOST_DATA_TEST_CASE(Configuration, boost::unit_test::data::make({ false, true }) * (SmallShapes() + LargeShapes()) * CNNDataTypes(), in_place, shape, dt) -{ - // Set fixed point position data type allowed - const int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0; - - // Create tensors - CLTensor src = create_tensor(shape, dt, 1, fixed_point_position); - CLTensor dst = create_tensor(shape, dt, 1, fixed_point_position); - - BOOST_TEST(src.info()->is_resizable()); - BOOST_TEST(dst.info()->is_resizable()); - - // Create and configure function - CLActivationLayer act_layer; - - if(in_place) - { - act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); - } - else - { - act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); - } - - // Validate valid region - const ValidRegion valid_region = shape_to_valid_region(shape); - validate(src.info()->valid_region(), valid_region); - - if(!in_place) - { - validate(dst.info()->valid_region(), valid_region); - } - - // Validate padding - const int step = 16 / arm_compute::data_size_from_type(dt); - const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding(); - validate(src.info()->padding(), padding); - - if(!in_place) - { - validate(dst.info()->padding(), padding); - } -} - -BOOST_AUTO_TEST_SUITE(Float) -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) -BOOST_DATA_TEST_CASE(RunSmall, boost::unit_test::data::make({ false, true }) * SmallShapes() * CNNFloatDataTypes() * ActivationFunctions() * boost::unit_test::data::make({ 0.5f, 1.f }), - in_place, shape, dt, act_function, alpha_beta) -{ - // Create activation layer info - ActivationLayerInfo act_info(act_function, alpha_beta, alpha_beta); - - // Compute function - CLTensor dst = compute_activation_layer(in_place, shape, dt, act_info); - - // Compute reference - RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, dt, act_info); - - // Validate output - validate(CLAccessor(dst), ref_dst, activation_layer_tolerance(act_function)); -} - -BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) -BOOST_DATA_TEST_CASE(RunLarge, boost::unit_test::data::make({ false, true }) * LargeShapes() * CNNFloatDataTypes() * ActivationFunctions() * boost::unit_test::data::make({ 0.5f, 1.f }), - in_place, shape, dt, act_function, alpha_beta) -{ - // Create activation layer info - ActivationLayerInfo act_info(act_function, alpha_beta, alpha_beta); - - // Compute function - CLTensor dst = compute_activation_layer(in_place, shape, dt, act_info); - - // Compute reference - RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, dt, act_info); - - // Validate output - validate(CLAccessor(dst), ref_dst, activation_layer_tolerance(act_function)); -} -BOOST_AUTO_TEST_SUITE_END() - -/** @note We test for fixed point precision [3,5] because [1,2] and [6,7] ranges - * cause overflowing issues in most of the transcendentals functions. - */ -BOOST_AUTO_TEST_SUITE(Quantized) -BOOST_AUTO_TEST_SUITE(QS8) -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) -BOOST_DATA_TEST_CASE(RunSmall, boost::unit_test::data::make({ false, true }) * SmallShapes() * ActivationFunctions() * boost::unit_test::data::xrange(3, 6, 1) * boost::unit_test::data::make({ 0.5f, 1.f }), - in_place, shape, act_function, fixed_point_position, alpha_beta) -{ - // Create activation layer info - ActivationLayerInfo act_info(act_function, alpha_beta, alpha_beta); - - // Compute function - CLTensor dst = compute_activation_layer(in_place, shape, DataType::QS8, act_info, fixed_point_position); - - // Compute reference - RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, DataType::QS8, act_info, fixed_point_position); - - // Validate output - validate(CLAccessor(dst), ref_dst, activation_layer_tolerance(act_function, fixed_point_position)); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(QS16) -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) -BOOST_DATA_TEST_CASE(RunSmall, boost::unit_test::data::make({ false, true }) * SmallShapes() * ActivationFunctions() * boost::unit_test::data::xrange(3, 14, 1) * boost::unit_test::data::make({ 0.5f, 1.f }), - in_place, shape, act_function, fixed_point_position, alpha_beta) -{ - // Create activation layer info - ActivationLayerInfo act_info(act_function, alpha_beta, alpha_beta); - - // Compute function - CLTensor dst = compute_activation_layer(in_place, shape, DataType::QS16, act_info, fixed_point_position); - - // Compute reference - RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, DataType::QS16, act_info, fixed_point_position); - - // Validate output - validate(CLAccessor(dst), ref_dst, activation_layer_tolerance(act_function, fixed_point_position)); -} -BOOST_AUTO_TEST_SUITE_END() -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE_END() -BOOST_AUTO_TEST_SUITE_END() -#endif /* DOXYGEN_SKIP_THIS */ \ No newline at end of file diff --git a/tests/validation/NEON/ActivationLayer.cpp b/tests/validation/NEON/ActivationLayer.cpp deleted file mode 100644 index 5f1a2c6fb6..0000000000 --- a/tests/validation/NEON/ActivationLayer.cpp +++ /dev/null @@ -1,328 +0,0 @@ -/* - * 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 "AssetsLibrary.h" -#include "Globals.h" -#include "NEON/Accessor.h" -#include "PaddingCalculator.h" -#include "TypePrinter.h" -#include "Utils.h" -#include "validation/Datasets.h" -#include "validation/Helpers.h" -#include "validation/Reference.h" -#include "validation/Validation.h" - -#include "arm_compute/core/Helpers.h" -#include "arm_compute/core/Types.h" -#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h" -#include "arm_compute/runtime/Tensor.h" -#include "arm_compute/runtime/TensorAllocator.h" - -#include "boost_wrapper.h" - -#include -#include -#include - -using namespace arm_compute; -using namespace arm_compute::test; -using namespace arm_compute::test::validation; - -namespace -{ -/** Define tolerance of the activation layer - * - * @param[in] dt The data type used. - * @param[in] activation The activation function used. - * @param[in] fixed_point_position Number of bits for the fractional part.. - * - * @return Tolerance depending on the activation function. - */ -float activation_layer_tolerance(DataType dt, ActivationLayerInfo::ActivationFunction activation, int fixed_point_position = 0) -{ - switch(activation) - { - case ActivationLayerInfo::ActivationFunction::LOGISTIC: - case ActivationLayerInfo::ActivationFunction::SOFT_RELU: - case ActivationLayerInfo::ActivationFunction::SQRT: - case ActivationLayerInfo::ActivationFunction::TANH: - switch(dt) - { - case DataType::QS8: - return 5.f; - case DataType::QS16: - return 11.f; - case DataType::F16: - return 0.01f; - default: - return 0.00001f; - } - break; - default: - return 0.f; - } -} - -/** Compute Neon activation layer function. - * - * @param[in] in_place Compute the activation layer in-place. - * @param[in] shape Shape of the input and output tensors. - * @param[in] dt Shape Data type of tensors. - * @param[in] act_info Activation layer information. - * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of fixed point numbers. - * - * @return Computed output tensor. - */ -Tensor compute_activation_layer(bool in_place, const TensorShape &shape, DataType dt, ActivationLayerInfo act_info, int fixed_point_position = 0) -{ - // Create tensors - Tensor src = create_tensor(shape, dt, 1, fixed_point_position); - Tensor dst = create_tensor(shape, dt, 1, fixed_point_position); - - // Create and configure function - NEActivationLayer act_layer; - - if(in_place) - { - act_layer.configure(&src, nullptr, act_info); - } - else - { - act_layer.configure(&src, &dst, act_info); - } - - // Allocate tensors - src.allocator()->allocate(); - BOOST_TEST(!src.info()->is_resizable()); - - if(!in_place) - { - dst.allocator()->allocate(); - BOOST_TEST(!dst.info()->is_resizable()); - } - // Fill tensors - switch(dt) - { - case DataType::QS8: - { - const std::pair bounds = get_activation_layer_test_bounds(act_info.activation(), fixed_point_position); - std::uniform_int_distribution<> distribution(bounds.first, bounds.second); - library->fill(Accessor(src), distribution, 0); - break; - } - case DataType::QS16: - { - const std::pair bounds = get_activation_layer_test_bounds(act_info.activation(), fixed_point_position); - std::uniform_int_distribution<> distribution(bounds.first, bounds.second); - library->fill(Accessor(src), distribution, 0); - break; - } -#ifdef ARM_COMPUTE_ENABLE_FP16 - case DataType::F16: - { - const std::pair bounds = get_activation_layer_test_bounds(act_info.activation()); - std::uniform_real_distribution<> distribution(bounds.first, bounds.second); - library->fill(Accessor(src), distribution, 0); - break; - } -#endif /* ARM_COMPUTE_ENABLE_FP16 */ - case DataType::F32: - { - const std::pair bounds = get_activation_layer_test_bounds(act_info.activation()); - std::uniform_real_distribution<> distribution(bounds.first, bounds.second); - library->fill(Accessor(src), distribution, 0); - break; - } - default: - { - ARM_COMPUTE_ERROR("Not supported"); - break; - } - } - - // Compute function - act_layer.run(); - - if(in_place) - { - return src; - } - else - { - return dst; - } -} -} // namespace - -#ifndef DOXYGEN_SKIP_THIS -BOOST_AUTO_TEST_SUITE(NEON) -BOOST_AUTO_TEST_SUITE(ActivationLayer) - -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) -BOOST_DATA_TEST_CASE(Configuration, boost::unit_test::data::make({ false, true }) * (SmallShapes() + LargeShapes()) * CNNDataTypes(), in_place, shape, dt) -{ - // Set fixed point position data type allowed - const int fixed_point_position = (arm_compute::is_data_type_fixed_point(dt)) ? 3 : 0; - - // Create tensors - Tensor src = create_tensor(shape, dt, 1, fixed_point_position); - Tensor dst = create_tensor(shape, dt, 1, fixed_point_position); - - BOOST_TEST(src.info()->is_resizable()); - BOOST_TEST(dst.info()->is_resizable()); - - // Create and configure function - NEActivationLayer act_layer; - - if(in_place) - { - act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); - } - else - { - act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); - } - - // Validate valid region - const ValidRegion valid_region = shape_to_valid_region(shape); - validate(src.info()->valid_region(), valid_region); - - if(!in_place) - { - validate(dst.info()->valid_region(), valid_region); - } - - // Validate padding - const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); - validate(src.info()->padding(), padding); - - if(!in_place) - { - validate(dst.info()->padding(), padding); - } -} - -#ifdef ARM_COMPUTE_ENABLE_FP16 -BOOST_AUTO_TEST_SUITE(Float16) -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) -BOOST_DATA_TEST_CASE(RunSmall, boost::unit_test::data::make({ false, true }) * SmallShapes() * boost::unit_test::data::make(DataType::F16) * ActivationFunctions() * boost::unit_test::data::make({ 0.5f, 1.f }), - in_place, shape, dt, act_function, alpha_beta) -{ - // Create activation layer info - const ActivationLayerInfo act_info(act_function, alpha_beta); - - // Compute function - Tensor dst = compute_activation_layer(in_place, shape, dt, act_info); - - // Compute reference - RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, dt, act_info); - - // Validate output - validate(Accessor(dst), ref_dst, activation_layer_tolerance(dt, act_function)); -} -BOOST_AUTO_TEST_SUITE_END() -#endif /* ARM_COMPUTE_ENABLE_FP16 */ - -BOOST_AUTO_TEST_SUITE(Float) -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) -BOOST_DATA_TEST_CASE(RunSmall, boost::unit_test::data::make({ false, true }) * SmallShapes() * CNNFloatDataTypes() * ActivationFunctions() * boost::unit_test::data::make({ 0.5f, 1.f }), - in_place, shape, dt, act_function, alpha_beta) -{ - // Create activation layer info - ActivationLayerInfo act_info(act_function, alpha_beta, alpha_beta); - - // Compute function - Tensor dst = compute_activation_layer(in_place, shape, dt, act_info); - - // Compute reference - RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, dt, act_info); - - // Validate output - validate(Accessor(dst), ref_dst, activation_layer_tolerance(dt, act_function)); -} - -BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) -BOOST_DATA_TEST_CASE(RunLarge, boost::unit_test::data::make({ false, true }) * LargeShapes() * CNNFloatDataTypes() * ActivationFunctions() * boost::unit_test::data::make({ 0.5f, 1.f }), - in_place, shape, dt, act_function, alpha_beta) -{ - // Create activation layer info - ActivationLayerInfo act_info(act_function, alpha_beta, alpha_beta); - - // Compute function - Tensor dst = compute_activation_layer(in_place, shape, dt, act_info); - - // Compute reference - RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, dt, act_info); - - // Validate output - validate(Accessor(dst), ref_dst, activation_layer_tolerance(dt, act_function)); -} -BOOST_AUTO_TEST_SUITE_END() - -/** @note We test for fixed point precision [3,5] because [1,2] and [6,7] ranges - * cause overflowing issues in most of the transcendentals functions. - */ -BOOST_AUTO_TEST_SUITE(Quantized) -BOOST_AUTO_TEST_SUITE(QS8) -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) -BOOST_DATA_TEST_CASE(RunSmall, boost::unit_test::data::make({ false, true }) * SmallShapes() * ActivationFunctions() * boost::unit_test::data::xrange(3, 6, 1) * boost::unit_test::data::make({ 0.5f, 1.f }), - in_place, shape, act_function, fixed_point_position, alpha_beta) -{ - // Create activation layer info - ActivationLayerInfo act_info(act_function, alpha_beta, alpha_beta); - - // Compute function - Tensor dst = compute_activation_layer(in_place, shape, DataType::QS8, act_info, fixed_point_position); - - // Compute reference - RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, DataType::QS8, act_info, fixed_point_position); - - // Validate output - validate(Accessor(dst), ref_dst, activation_layer_tolerance(DataType::QS8, act_function, fixed_point_position)); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(QS16) -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) -BOOST_DATA_TEST_CASE(RunSmall, boost::unit_test::data::make({ false, true }) * SmallShapes() * ActivationFunctions() * boost::unit_test::data::xrange(3, 14, 1) * boost::unit_test::data::make({ 0.5f, 1.f }), - in_place, shape, act_function, fixed_point_position, alpha_beta) -{ - // Create activation layer info - ActivationLayerInfo act_info(act_function, alpha_beta, alpha_beta); - - // Compute function - Tensor dst = compute_activation_layer(in_place, shape, DataType::QS16, act_info, fixed_point_position); - - // Compute reference - RawTensor ref_dst = Reference::compute_reference_activation_layer(shape, DataType::QS16, act_info, fixed_point_position); - - // Validate output - validate(Accessor(dst), ref_dst, activation_layer_tolerance(DataType::QS16, act_function, fixed_point_position)); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE_END() -BOOST_AUTO_TEST_SUITE_END() -#endif /* DOXYGEN_SKIP_THIS */ diff --git a/tests/validation/Reference.cpp b/tests/validation/Reference.cpp index f9052f1dba..011fd091f2 100644 --- a/tests/validation/Reference.cpp +++ b/tests/validation/Reference.cpp @@ -453,56 +453,6 @@ RawTensor Reference::compute_reference_threshold(const TensorShape &shape, uint8 return ref_dst; } -RawTensor Reference::compute_reference_activation_layer(const TensorShape &shape, DataType dt, ActivationLayerInfo act_info, int fixed_point_position) -{ - // Create reference - RawTensor ref_src(shape, dt, 1, fixed_point_position); - RawTensor ref_dst(shape, dt, 1, fixed_point_position); - - // Fill tensors - switch(dt) - { - case DataType::QS8: - { - const std::pair bounds = get_activation_layer_test_bounds(act_info.activation(), fixed_point_position); - std::uniform_int_distribution<> distribution(bounds.first, bounds.second); - library->fill(ref_src, distribution, 0); - break; - } - case DataType::QS16: - { - const std::pair bounds = get_activation_layer_test_bounds(act_info.activation(), fixed_point_position); - std::uniform_int_distribution<> distribution(bounds.first, bounds.second); - library->fill(ref_src, distribution, 0); - break; - } - case DataType::F16: - { - const std::pair bounds = get_activation_layer_test_bounds(act_info.activation()); - std::uniform_real_distribution<> distribution(bounds.first, bounds.second); - library->fill(ref_src, distribution, 0); - break; - } - case DataType::F32: - { - const std::pair bounds = get_activation_layer_test_bounds(act_info.activation()); - std::uniform_real_distribution<> distribution(bounds.first, bounds.second); - library->fill(ref_src, distribution, 0); - break; - } - default: - { - ARM_COMPUTE_ERROR("Not supported"); - break; - } - } - - // Compute reference - ReferenceCPP::activation_layer(ref_src, ref_dst, act_info); - - return ref_dst; -} - RawTensor Reference::compute_reference_batch_normalization_layer(const TensorShape &shape0, const TensorShape &shape1, DataType dt, float epsilon, int fixed_point_position) { // Create reference diff --git a/tests/validation/Reference.h b/tests/validation/Reference.h index eeaa55c739..42c62f8d6a 100644 --- a/tests/validation/Reference.h +++ b/tests/validation/Reference.h @@ -295,16 +295,6 @@ public: * @return Computed raw tensor. */ static RawTensor compute_reference_threshold(const TensorShape &shape, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper); - /** Compute reference activation layer. - * - * @param[in] shape Shape of the input and output tensors. - * @param[in] dt Data type of the tensors. - * @param[in] act_info Activation layer information. - * @param[in] fixed_point_position (Optional)Number of bits for the fractional part of fixed point numbers. - * - * @return Computed raw tensor. - */ - static RawTensor compute_reference_activation_layer(const TensorShape &shape, DataType dt, ActivationLayerInfo act_info, int fixed_point_position = 0); /** Compute reference batch normalization layer. * * @param[in] shape0 Shape of the input and output tensors. diff --git a/tests/validation/ReferenceCPP.cpp b/tests/validation/ReferenceCPP.cpp index 81ec60d5b9..117fd5bebb 100644 --- a/tests/validation/ReferenceCPP.cpp +++ b/tests/validation/ReferenceCPP.cpp @@ -283,14 +283,6 @@ void ReferenceCPP::threshold(const RawTensor &src, RawTensor &dst, uint8_t thres tensor_operations::threshold(s, d, threshold, false_value, true_value, type, upper); } -// Activation layer -void ReferenceCPP::activation_layer(const RawTensor &input, RawTensor &output, ActivationLayerInfo act_info) -{ - const TensorVariant s = TensorFactory::get_tensor(input); - TensorVariant d = TensorFactory::get_tensor(output); - boost::apply_visitor(tensor_visitors::activation_layer_visitor(s, act_info), d); -} - // Batch Normalization Layer void ReferenceCPP::batch_normalization_layer(const RawTensor &src, RawTensor &dst, const RawTensor &mean, const RawTensor &var, const RawTensor &beta, const RawTensor &gamma, float epsilon, int fixed_point_position) diff --git a/tests/validation/ReferenceCPP.h b/tests/validation/ReferenceCPP.h index 97e573cfa2..0d1bea48bd 100644 --- a/tests/validation/ReferenceCPP.h +++ b/tests/validation/ReferenceCPP.h @@ -253,13 +253,6 @@ public: * @param[in] upper Upper threshold. Only used when the thresholding type is RANGE. */ static void threshold(const RawTensor &src, RawTensor &dst, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper); - /** Activation layer of @p src base on information from @p act_info. - * - * @param[in] input Input tensor. - * @param[in] output Second tensor. - * @param[out] act_info Activation layer information. - */ - static void activation_layer(const RawTensor &input, RawTensor &output, ActivationLayerInfo act_info); /** Batch Normalization of @p src based on the information from @p norm_info. * * @param[in] src Input tensor. diff --git a/tests/validation/TensorOperations.h b/tests/validation/TensorOperations.h index b472e3d5cf..db145c19ad 100644 --- a/tests/validation/TensorOperations.h +++ b/tests/validation/TensorOperations.h @@ -935,109 +935,6 @@ void threshold(const Tensor &in, Tensor &out, uint8_t threshold, uint8_t f } } -// Activation Layer for floating point type -template ::value, int>::type * = nullptr> -void activation_layer(const Tensor &in, Tensor &out, ActivationLayerInfo act_info) -{ - const T a = static_cast(act_info.a()); - const T b = static_cast(act_info.b()); - - for(int i = 0; i < in.num_elements(); ++i) - { - T x = in[i]; - switch(act_info.activation()) - { - case ActivationLayerInfo::ActivationFunction::ABS: - out[i] = std::abs(x); - break; - case ActivationLayerInfo::ActivationFunction::LINEAR: - out[i] = a * x + b; - break; - case ActivationLayerInfo::ActivationFunction::LOGISTIC: - out[i] = static_cast(1) / (static_cast(1) + std::exp(-x)); - break; - case ActivationLayerInfo::ActivationFunction::RELU: - out[i] = std::max(static_cast(0), x); - break; - case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: - out[i] = std::min(a, std::max(static_cast(0), x)); - break; - case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: - out[i] = (x > 0) ? x : a * x; - break; - case ActivationLayerInfo::ActivationFunction::SOFT_RELU: - out[i] = std::log(static_cast(1) + std::exp(x)); - break; - case ActivationLayerInfo::ActivationFunction::SQRT: - out[i] = std::sqrt(x); - break; - case ActivationLayerInfo::ActivationFunction::SQUARE: - out[i] = x * x; - break; - case ActivationLayerInfo::ActivationFunction::TANH: - out[i] = a * std::tanh(b * x); - break; - default: - ARM_COMPUTE_ERROR("Activation function not recognised"); - break; - } - } -} - -// Activation Layer for fixed point type -template ::value, int>::type * = nullptr> -void activation_layer(const Tensor &in, Tensor &out, ActivationLayerInfo act_info) -{ - using namespace fixed_point_arithmetic; - int fixed_point_position = in.fixed_point_position(); - ActivationLayerInfo::ActivationFunction act_func = act_info.activation(); - const fixed_point a(act_info.a(), fixed_point_position); - const fixed_point b(act_info.b(), fixed_point_position); - const fixed_point const_0(0, fixed_point_position); - const fixed_point const_1(1, fixed_point_position); - - for(int i = 0; i < in.num_elements(); ++i) - { - fixed_point x(in[i], fixed_point_position, true); - switch(act_func) - { - case ActivationLayerInfo::ActivationFunction::ABS: - out[i] = abs(x).raw(); - break; - case ActivationLayerInfo::ActivationFunction::LINEAR: - out[i] = add(b, mul(a, x)).raw(); - break; - case ActivationLayerInfo::ActivationFunction::LOGISTIC: - out[i] = (const_1 / (const_1 + exp(-x))).raw(); - break; - case ActivationLayerInfo::ActivationFunction::RELU: - out[i] = max(const_0, x).raw(); - break; - case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: - out[i] = min(a, max(const_0, x)).raw(); - break; - case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: - out[i] = (x > const_0) ? x.raw() : mul(a, x).raw(); - break; - case ActivationLayerInfo::ActivationFunction::SOFT_RELU: - out[i] = log(const_1 + exp(x)).raw(); - break; - case ActivationLayerInfo::ActivationFunction::SQRT: - out[i] = (const_1 / inv_sqrt(x)).raw(); - break; - case ActivationLayerInfo::ActivationFunction::SQUARE: - out[i] = mul(x, x).raw(); - break; - case ActivationLayerInfo::ActivationFunction::TANH: - out[i] = mul(a, tanh(mul(b, x))).raw(); - break; - default: - ARM_COMPUTE_ERROR("Activation function not recognised"); - break; - } - } -} - // Batch Normalization Layer for fixed point type template ::value, int>::type * = nullptr> void batch_normalization_layer(const Tensor &in, Tensor &out, const Tensor &mean, const Tensor &var, const Tensor &beta, const Tensor &gamma, float epsilon, int fixed_point_position) diff --git a/tests/validation/TensorVisitors.h b/tests/validation/TensorVisitors.h index 44ae6f13e8..365aac7758 100644 --- a/tests/validation/TensorVisitors.h +++ b/tests/validation/TensorVisitors.h @@ -228,26 +228,6 @@ private: template struct arm_compute::test::validation::tensor_visitors::table_lookup; template struct arm_compute::test::validation::tensor_visitors::table_lookup; -// Activation layer visitor -struct activation_layer_visitor : public boost::static_visitor<> -{ -public: - explicit activation_layer_visitor(const TensorVariant &in, ActivationLayerInfo act_info) - : _in(in), _act_info(act_info) - { - } - - template - void operator()(Tensor &out) const - { - const auto &in = boost::get>(_in); - tensor_operations::activation_layer(in, out, _act_info); - } - -private: - const TensorVariant &_in; - const ActivationLayerInfo _act_info; -}; // Batch Normalization Layer visitor struct batch_normalization_layer_visitor : public boost::static_visitor<> { diff --git a/tests/validation_new/CL/ActivationLayer.cpp b/tests/validation_new/CL/ActivationLayer.cpp new file mode 100644 index 0000000000..e1cc4e54e2 --- /dev/null +++ b/tests/validation_new/CL/ActivationLayer.cpp @@ -0,0 +1,245 @@ +/* + * 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/CLActivationLayer.h" +#include "framework/Asserts.h" +#include "framework/Macros.h" +#include "framework/datasets/Datasets.h" +#include "tests/CL/CLAccessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets_new/ActivationFunctionsDataset.h" +#include "tests/datasets_new/ShapeDatasets.h" +#include "tests/validation_new/Validation.h" +#include "tests/validation_new/fixtures/ActivationLayerFixture.h" +#include "tests/validation_new/half.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +/** Define tolerance of the activation layer. + * + * @param[in] activation The activation function used. + * @param[in] data_type Data type. + * + * @return Tolerance depending on the activation function. + */ +float tolerance(ActivationLayerInfo::ActivationFunction activation, DataType data_type) +{ + switch(activation) + { + case ActivationLayerInfo::ActivationFunction::LINEAR: + return data_type == DataType::F16 ? 0.2f : 0.f; + case ActivationLayerInfo::ActivationFunction::SQUARE: + return data_type == DataType::F16 ? 0.1f : 0.f; + case ActivationLayerInfo::ActivationFunction::LOGISTIC: + if(is_data_type_fixed_point(data_type)) + { + return 5.f; + } + else + { + return data_type == DataType::F16 ? 0.001f : 0.f; + } + case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: + return data_type == DataType::F16 ? 0.00001f : 0.f; + case ActivationLayerInfo::ActivationFunction::SOFT_RELU: + case ActivationLayerInfo::ActivationFunction::SQRT: + if(is_data_type_fixed_point(data_type)) + { + return 5.f; + } + else + { + return data_type == DataType::F16 ? 0.01f : 0.00001f; + } + case ActivationLayerInfo::ActivationFunction::TANH: + if(is_data_type_fixed_point(data_type)) + { + return 5.f; + } + else + { + return data_type == DataType::F16 ? 0.001f : 0.00001f; + } + default: + return 0.f; + } +} + +/** CNN data types */ +const auto CNNDataTypes = framework::dataset::make("DataType", +{ + DataType::F16, + DataType::F32, + DataType::QS8, + DataType::QS16, +}); + +/** Input data sets. */ +const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f })); +} // namespace + +TEST_SUITE(CL) +TEST_SUITE(ActivationLayer) + +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })), + shape, data_type, in_place) +{ + // Set fixed point position data type allowed + const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0; + + // Create tensors + CLTensor src = create_tensor(shape, data_type, 1, fixed_point_position); + CLTensor dst = create_tensor(shape, data_type, 1, fixed_point_position); + + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Create and configure function + CLActivationLayer act_layer; + + if(in_place) + { + act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); + } + else + { + act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); + } + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(src.info()->valid_region(), valid_region); + + if(!in_place) + { + validate(dst.info()->valid_region(), valid_region); + } + + // Validate padding + const int step = 16 / arm_compute::data_size_from_type(data_type); + const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding(); + validate(src.info()->padding(), padding); + + if(!in_place) + { + validate(dst.info()->padding(), padding); + } +} + +template +using CLActivationLayerFixture = ActivationValidationFixture; + +TEST_SUITE(Float) +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), + framework::dataset::make("DataType", + DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), + framework::dataset::make("DataType", + DataType::F16))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); +} +TEST_SUITE_END() + +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType", + DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType", + DataType::F32))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); +} +TEST_SUITE_END() +TEST_SUITE_END() + +template +using CLActivationLayerFixedPointFixture = ActivationValidationFixedPointFixture; + +TEST_SUITE(Quantized) +TEST_SUITE(QS8) +// We test for fixed point precision [3,5] because [1,2] and [6,7] ranges cause +// overflowing issues in most of the transcendentals functions. +FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixedPointFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset), + framework::dataset::make("DataType", + DataType::QS8)), + framework::dataset::make("FractionalBits", 3, 6))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixedPointFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset), + framework::dataset::make("DataType", + DataType::QS8)), + framework::dataset::make("FractionalBits", 3, 6))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); +} +TEST_SUITE_END() + +TEST_SUITE(QS16) +// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14 +FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixedPointFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset), + framework::dataset::make("DataType", + DataType::QS16)), + framework::dataset::make("FractionalBits", 1, 14))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixedPointFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset), + framework::dataset::make("DataType", + DataType::QS16)), + framework::dataset::make("FractionalBits", 1, 14))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance(_function, _data_type)); +} +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation_new/CPP/ActivationLayer.cpp b/tests/validation_new/CPP/ActivationLayer.cpp new file mode 100644 index 0000000000..052c3aa566 --- /dev/null +++ b/tests/validation_new/CPP/ActivationLayer.cpp @@ -0,0 +1,158 @@ +/* + * 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 "ActivationLayer.h" + +#include "tests/validation_new/FixedPoint.h" +#include "tests/validation_new/Helpers.h" +#include "tests/validation_new/half.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template ::value, int>::type> +SimpleTensor activation_layer(const SimpleTensor &src, ActivationLayerInfo info) +{ + // Create reference + SimpleTensor dst{ src.shape(), src.data_type(), 1, src.fixed_point_position() }; + + // Compute reference + const T a(info.a()); + const T b(info.b()); + + for(int i = 0; i < src.num_elements(); ++i) + { + T x = src[i]; + + switch(info.activation()) + { + case ActivationLayerInfo::ActivationFunction::ABS: + dst[i] = std::abs(x); + break; + case ActivationLayerInfo::ActivationFunction::LINEAR: + dst[i] = a * x + b; + break; + case ActivationLayerInfo::ActivationFunction::LOGISTIC: + dst[i] = static_cast(1) / (static_cast(1) + std::exp(-x)); + break; + case ActivationLayerInfo::ActivationFunction::RELU: + dst[i] = std::max(static_cast(0), x); + break; + case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: + dst[i] = std::min(a, std::max(static_cast(0), x)); + break; + case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: + dst[i] = (x > 0) ? x : a * x; + break; + case ActivationLayerInfo::ActivationFunction::SOFT_RELU: + dst[i] = std::log(static_cast(1) + std::exp(x)); + break; + case ActivationLayerInfo::ActivationFunction::SQRT: + dst[i] = std::sqrt(x); + break; + case ActivationLayerInfo::ActivationFunction::SQUARE: + dst[i] = x * x; + break; + case ActivationLayerInfo::ActivationFunction::TANH: + dst[i] = a * std::tanh(b * x); + break; + default: + ARM_COMPUTE_ERROR("Unsupported activation function"); + } + } + + return dst; +} + +template ::value, int>::type> +SimpleTensor activation_layer(const SimpleTensor &src, ActivationLayerInfo info) +{ + using namespace fixed_point_arithmetic; + + // Create reference + SimpleTensor dst{ src.shape(), src.data_type(), 1, src.fixed_point_position() }; + + // Compute reference + const int fixed_point_position = src.fixed_point_position(); + const fixed_point a(info.a(), fixed_point_position); + const fixed_point b(info.b(), fixed_point_position); + const fixed_point const_0(0, fixed_point_position); + const fixed_point const_1(1, fixed_point_position); + + for(int i = 0; i < src.num_elements(); ++i) + { + fixed_point x(src[i], fixed_point_position, true); + + switch(info.activation()) + { + case ActivationLayerInfo::ActivationFunction::ABS: + dst[i] = abs(x).raw(); + break; + case ActivationLayerInfo::ActivationFunction::LINEAR: + dst[i] = add(b, mul(a, x)).raw(); + break; + case ActivationLayerInfo::ActivationFunction::LOGISTIC: + dst[i] = (const_1 / (const_1 + exp(-x))).raw(); + break; + case ActivationLayerInfo::ActivationFunction::RELU: + dst[i] = max(const_0, x).raw(); + break; + case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: + dst[i] = min(a, max(const_0, x)).raw(); + break; + case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: + dst[i] = (x > const_0) ? x.raw() : mul(a, x).raw(); + break; + case ActivationLayerInfo::ActivationFunction::SOFT_RELU: + dst[i] = log(const_1 + exp(x)).raw(); + break; + case ActivationLayerInfo::ActivationFunction::SQRT: + dst[i] = (const_1 / inv_sqrt(x)).raw(); + break; + case ActivationLayerInfo::ActivationFunction::SQUARE: + dst[i] = mul(x, x).raw(); + break; + case ActivationLayerInfo::ActivationFunction::TANH: + dst[i] = mul(a, tanh(mul(b, x))).raw(); + break; + default: + ARM_COMPUTE_ERROR("Unsupported activation function"); + } + } + + return dst; +} + +template SimpleTensor activation_layer(const SimpleTensor &src, ActivationLayerInfo info); +template SimpleTensor activation_layer(const SimpleTensor &src, ActivationLayerInfo info); +template SimpleTensor activation_layer(const SimpleTensor &src, ActivationLayerInfo info); +template SimpleTensor activation_layer(const SimpleTensor &src, ActivationLayerInfo info); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation_new/CPP/ActivationLayer.h b/tests/validation_new/CPP/ActivationLayer.h new file mode 100644 index 0000000000..5f4ef46827 --- /dev/null +++ b/tests/validation_new/CPP/ActivationLayer.h @@ -0,0 +1,47 @@ +/* + * 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_ACTIVATION_LAYER_H__ +#define __ARM_COMPUTE_TEST_ACTIVATION_LAYER_H__ + +#include "tests/validation_new/Helpers.h" +#include "tests/validation_new/SimpleTensor.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template ::value, int>::type = 0> +SimpleTensor activation_layer(const SimpleTensor &src, ActivationLayerInfo info); + +template ::value, int>::type = 0> +SimpleTensor activation_layer(const SimpleTensor &src, ActivationLayerInfo info); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_ACTIVATION_LAYER_H__ */ diff --git a/tests/validation_new/Helpers.h b/tests/validation_new/Helpers.h index e25b684c11..3058b8eaee 100644 --- a/tests/validation_new/Helpers.h +++ b/tests/validation_new/Helpers.h @@ -24,9 +24,13 @@ #ifndef __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ #define __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__ +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Utils.h" #include "tests/validation/half.h" +#include #include +#include namespace arm_compute { @@ -43,6 +47,87 @@ template <> struct is_floating_point : public std::true_type { }; + +/** Helper function to get the testing range for each activation layer. + * + * @param[in] activation Activation function to test. + * @param[in] data_type Data type. + * @param[in] fixed_point_position Number of bits for the fractional part. Defaults to 1. + * + * @return A pair containing the lower upper testing bounds for a given function. + */ +template +std::pair get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, DataType data_type, int fixed_point_position = 0) +{ + std::pair bounds; + + switch(data_type) + { + case DataType::F16: + { + using namespace half_float::literal; + + switch(activation) + { + case ActivationLayerInfo::ActivationFunction::SQUARE: + case ActivationLayerInfo::ActivationFunction::LOGISTIC: + case ActivationLayerInfo::ActivationFunction::SOFT_RELU: + // Reduce range as exponent overflows + bounds = std::make_pair(-10._h, 10._h); + break; + case ActivationLayerInfo::ActivationFunction::SQRT: + // Reduce range as sqrt should take a non-negative number + bounds = std::make_pair(0._h, 255._h); + break; + default: + bounds = std::make_pair(-255._h, 255._h); + break; + } + break; + } + case DataType::F32: + switch(activation) + { + case ActivationLayerInfo::ActivationFunction::LOGISTIC: + case ActivationLayerInfo::ActivationFunction::SOFT_RELU: + // Reduce range as exponent overflows + bounds = std::make_pair(-40.f, 40.f); + break; + case ActivationLayerInfo::ActivationFunction::SQRT: + // Reduce range as sqrt should take a non-negative number + bounds = std::make_pair(0.f, 255.f); + break; + default: + bounds = std::make_pair(-255.f, 255.f); + break; + } + break; + case DataType::QS8: + case DataType::QS16: + switch(activation) + { + case ActivationLayerInfo::ActivationFunction::LOGISTIC: + case ActivationLayerInfo::ActivationFunction::SOFT_RELU: + case ActivationLayerInfo::ActivationFunction::TANH: + // Reduce range as exponent overflows + bounds = std::make_pair(-(1 << fixed_point_position), 1 << fixed_point_position); + break; + case ActivationLayerInfo::ActivationFunction::SQRT: + // Reduce range as sqrt should take a non-negative number + // Can't be zero either as inv_sqrt is used in NEON. + bounds = std::make_pair(1, std::numeric_limits::max()); + break; + default: + bounds = std::make_pair(std::numeric_limits::lowest(), std::numeric_limits::max()); + break; + } + break; + default: + ARM_COMPUTE_ERROR("Unsupported data type"); + } + + return bounds; +} } // namespace validation } // namespace test } // namespace arm_compute diff --git a/tests/validation_new/NEON/ActivationLayer.cpp b/tests/validation_new/NEON/ActivationLayer.cpp new file mode 100644 index 0000000000..db0faaecdf --- /dev/null +++ b/tests/validation_new/NEON/ActivationLayer.cpp @@ -0,0 +1,230 @@ +/* + * 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/NEActivationLayer.h" +#include "arm_compute/runtime/Tensor.h" +#include "arm_compute/runtime/TensorAllocator.h" +#include "framework/Asserts.h" +#include "framework/Macros.h" +#include "framework/datasets/Datasets.h" +#include "tests/NEON/Accessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets_new/ActivationFunctionsDataset.h" +#include "tests/datasets_new/ShapeDatasets.h" +#include "tests/validation_new/Validation.h" +#include "tests/validation_new/fixtures/ActivationLayerFixture.h" +#include "tests/validation_new/half.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +/** Define tolerance of the activation layer. + * + * @param[in] data_type The data type used. + * @param[in] activation The activation function used. + * + * @return Tolerance depending on the activation function. + */ +float tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation) +{ + switch(activation) + { + case ActivationLayerInfo::ActivationFunction::LOGISTIC: + case ActivationLayerInfo::ActivationFunction::SOFT_RELU: + case ActivationLayerInfo::ActivationFunction::SQRT: + case ActivationLayerInfo::ActivationFunction::TANH: + switch(data_type) + { + case DataType::QS8: + return 5.f; + case DataType::QS16: + return 11.f; + case DataType::F16: + return 0.01f; + default: + return 0.00001f; + } + break; + default: + return 0.f; + } +} + +/** CNN data types */ +const auto CNNDataTypes = framework::dataset::make("DataType", +{ +#ifdef ARM_COMPUTE_ENABLE_FP16 + DataType::F16, +#endif /* ARM_COMPUTE_ENABLE_FP16 */ + DataType::F32, + DataType::QS8, + DataType::QS16, +}); + +/** Input data sets. */ +const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f })); +} // namespace + +TEST_SUITE(NEON) +TEST_SUITE(ActivationLayer) + +DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })), + shape, data_type, in_place) +{ + // Set fixed point position data type allowed + const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0; + + // Create tensors + Tensor src = create_tensor(shape, data_type, 1, fixed_point_position); + Tensor dst = create_tensor(shape, data_type, 1, fixed_point_position); + + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Create and configure function + NEActivationLayer act_layer; + + if(in_place) + { + act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); + } + else + { + act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS)); + } + + // Validate valid region + const ValidRegion valid_region = shape_to_valid_region(shape); + validate(src.info()->valid_region(), valid_region); + + if(!in_place) + { + validate(dst.info()->valid_region(), valid_region); + } + + // Validate padding + const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding(); + validate(src.info()->padding(), padding); + + if(!in_place) + { + validate(dst.info()->padding(), padding); + } +} + +template +using NEActivationLayerFixture = ActivationValidationFixture; + +TEST_SUITE(Float) +#ifdef ARM_COMPUTE_ENABLE_FP16 +TEST_SUITE(FP16) +FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), + framework::dataset::make("DataType", + DataType::F16))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance(_data_type, _function)); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), + framework::dataset::make("DataType", + DataType::F16))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance(_data_type, _function)); +} +TEST_SUITE_END() +#endif /* ARM_COMPUTE_ENABLE_FP16 */ + +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType", + DataType::F32))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance(_data_type, _function)); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType", + DataType::F32))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance(_data_type, _function)); +} +TEST_SUITE_END() +TEST_SUITE_END() + +template +using NEActivationLayerFixedPointFixture = ActivationValidationFixedPointFixture; + +TEST_SUITE(Quantized) +TEST_SUITE(QS8) +// We test for fixed point precision [3,5] because [1,2] and [6,7] ranges cause +// overflowing issues in most of the transcendentals functions. +FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixedPointFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset), + framework::dataset::make("DataType", + DataType::QS8)), + framework::dataset::make("FractionalBits", 3, 6))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance(_data_type, _function)); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixedPointFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset), + framework::dataset::make("DataType", + DataType::QS8)), + framework::dataset::make("FractionalBits", 3, 6))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance(_data_type, _function)); +} +TEST_SUITE_END() + +TEST_SUITE(QS16) +// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14 +FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixedPointFixture, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset), + framework::dataset::make("DataType", + DataType::QS16)), + framework::dataset::make("FractionalBits", 1, 14))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance(_data_type, _function)); +} +FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixedPointFixture, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset), + framework::dataset::make("DataType", + DataType::QS16)), + framework::dataset::make("FractionalBits", 1, 14))) +{ + // Validate output + validate(Accessor(_target), _reference, tolerance(_data_type, _function)); +} +TEST_SUITE_END() +TEST_SUITE_END() + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation_new/fixtures/ActivationLayerFixture.h b/tests/validation_new/fixtures/ActivationLayerFixture.h new file mode 100644 index 0000000000..bf0e7ba6ea --- /dev/null +++ b/tests/validation_new/fixtures/ActivationLayerFixture.h @@ -0,0 +1,157 @@ +/* + * 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_ACTIVATION_LAYER_FIXTURE +#define ARM_COMPUTE_TEST_ACTIVATION_LAYER_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "framework/Asserts.h" +#include "framework/Fixture.h" +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/validation_new/CPP/ActivationLayer.h" +#include "tests/validation_new/Helpers.h" + +#include + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class ActivationValidationFixedPointFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type, int fractional_bits) + { + _fractional_bits = fractional_bits; + _data_type = data_type; + _function = function; + + ActivationLayerInfo info(function, alpha_beta, alpha_beta); + + _target = compute_target(shape, in_place, info, data_type, fractional_bits); + _reference = compute_reference(shape, info, data_type, fractional_bits); + } + +protected: + template + void fill(U &&tensor) + { + if(is_data_type_float(_data_type)) + { + float min_bound = 0; + float max_bound = 0; + std::tie(min_bound, max_bound) = get_activation_layer_test_bounds(_function, _data_type); + std::uniform_real_distribution<> distribution(min_bound, max_bound); + library->fill(tensor, distribution, 0); + } + else + { + int min_bound = 0; + int max_bound = 0; + std::tie(min_bound, max_bound) = get_activation_layer_test_bounds(_function, _data_type, _fractional_bits); + std::uniform_int_distribution<> distribution(min_bound, max_bound); + library->fill(tensor, distribution, 0); + } + } + + TensorType compute_target(const TensorShape &shape, bool in_place, ActivationLayerInfo info, DataType data_type, int fixed_point_position = 0) + { + // Create tensors + TensorType src = create_tensor(shape, data_type, 1, fixed_point_position); + TensorType dst = create_tensor(shape, data_type, 1, fixed_point_position); + + // Create and configure function + FunctionType act_layer; + + TensorType *dst_ptr = in_place ? &src : &dst; + + act_layer.configure(&src, dst_ptr, info); + + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + src.allocator()->allocate(); + ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); + + if(!in_place) + { + dst.allocator()->allocate(); + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + } + + // Fill tensors + fill(AccessorType(src)); + + // Compute function + act_layer.run(); + + if(in_place) + { + return src; + } + else + { + return dst; + } + } + + SimpleTensor compute_reference(const TensorShape &shape, ActivationLayerInfo info, DataType data_type, int fixed_point_position = 0) + { + // Create reference + SimpleTensor src{ shape, data_type, 1, fixed_point_position }; + + // Fill reference + fill(src); + + return reference::activation_layer(src, info); + } + + TensorType _target{}; + SimpleTensor _reference{}; + int _fractional_bits{}; + DataType _data_type{}; + ActivationLayerInfo::ActivationFunction _function{}; +}; + +template +class ActivationValidationFixture : public ActivationValidationFixedPointFixture +{ +public: + template + void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type) + { + ActivationValidationFixedPointFixture::setup(shape, in_place, function, alpha_beta, data_type, 0); + } +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_ACTIVATION_LAYER_FIXTURE */ -- cgit v1.2.1