From 91654c45cf1de5f41127536a0fdd310c17fdfc8e Mon Sep 17 00:00:00 2001 From: Pablo Tello Date: Wed, 5 Jul 2017 11:32:17 +0100 Subject: COMPMID-421: Added FP16 support in ActivationLayer. Change-Id: I7ba573b19d56e3c87996edb5218a00e5bfca451e Reviewed-on: http://mpd-gerrit.cambridge.arm.com/79755 Reviewed-by: Anthony Barbier Tested-by: Kaizen --- tests/validation/Helpers.h | 13 ++++-- tests/validation/NEON/ActivationLayer.cpp | 73 +++++++++++++++++++++++-------- tests/validation/Reference.cpp | 51 +++++++++++++-------- tests/validation/TensorOperations.h | 4 +- 4 files changed, 100 insertions(+), 41 deletions(-) (limited to 'tests/validation') diff --git a/tests/validation/Helpers.h b/tests/validation/Helpers.h index 4ee2112bcc..191e32813c 100644 --- a/tests/validation/Helpers.h +++ b/tests/validation/Helpers.h @@ -35,6 +35,10 @@ #include #include +#ifdef ARM_COMPUTE_ENABLE_FP16 +#include +#endif /* ARM_COMPUTE_ENABLE_FP16 */ + namespace arm_compute { namespace test @@ -49,9 +53,13 @@ namespace validation * @return A pair containing the lower upper testing bounds for a given function. */ template -std::pair get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, int fixed_point_position = 1) +inline std::pair get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, int fixed_point_position = 1) { - bool is_float = std::is_floating_point::value; + bool is_float = std::is_same::value; +#ifdef ARM_COMPUTE_ENABLE_FP16 + is_float = is_float || std::is_same::value; +#endif /* ARM_COMPUTE_ENABLE_FP16 */ + std::pair bounds; // Set initial values @@ -98,7 +106,6 @@ std::pair get_activation_layer_test_bounds(ActivationLayerInfo::Activation } return bounds; } - /** Helper function to get the testing range for batch normalization layer. * * @param[in] fixed_point_position (Optional) Number of bits for the fractional part. Defaults to 1. diff --git a/tests/validation/NEON/ActivationLayer.cpp b/tests/validation/NEON/ActivationLayer.cpp index 2b24fd5175..b8827a5324 100644 --- a/tests/validation/NEON/ActivationLayer.cpp +++ b/tests/validation/NEON/ActivationLayer.cpp @@ -73,6 +73,8 @@ float activation_layer_tolerance(DataType dt, ActivationLayerInfo::ActivationFun return 5.f; case DataType::QS16: return 11.f; + case DataType::F16: + return 0.01f; default: return 0.00001f; } @@ -119,30 +121,44 @@ Tensor compute_activation_layer(bool in_place, const TensorShape &shape, DataTyp 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(NEAccessor(src), distribution, 0); - } - else + switch(dt) { - int min_bound = 0; - int max_bound = 0; - if(dt == DataType::QS8) + 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(NEAccessor(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(NEAccessor(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(NEAccessor(src), distribution, 0); + break; + } +#endif /* ARM_COMPUTE_ENABLE_FP16 */ + case DataType::F32: { - std::tie(min_bound, max_bound) = get_activation_layer_test_bounds(act_info.activation(), fixed_point_position); + const std::pair bounds = get_activation_layer_test_bounds(act_info.activation()); + std::uniform_real_distribution<> distribution(bounds.first, bounds.second); + library->fill(NEAccessor(src), distribution, 0); + break; } - else + default: { - std::tie(min_bound, max_bound) = get_activation_layer_test_bounds(act_info.activation(), fixed_point_position); + ARM_COMPUTE_ERROR("Not supported"); + break; } - std::uniform_int_distribution<> distribution(min_bound, max_bound); - library->fill(NEAccessor(src), distribution, 0); } // Compute function @@ -207,6 +223,27 @@ BOOST_DATA_TEST_CASE(Configuration, boost::unit_test::data::make({ false, true } } } +#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(NEAccessor(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 }), diff --git a/tests/validation/Reference.cpp b/tests/validation/Reference.cpp index 3b429c1ee6..0ce25c5567 100644 --- a/tests/validation/Reference.cpp +++ b/tests/validation/Reference.cpp @@ -459,29 +459,44 @@ RawTensor Reference::compute_reference_activation_layer(const TensorShape &shape RawTensor ref_src = library->get(shape, dt, 1, fixed_point_position); RawTensor ref_dst = library->get(shape, dt, 1, fixed_point_position); - // Fill reference - 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(ref_src, distribution, 0); - } - else + // Fill tensors + switch(dt) { - int min_bound = 0; - int max_bound = 0; - if(dt == DataType::QS8) + case DataType::QS8: { - std::tie(min_bound, max_bound) = get_activation_layer_test_bounds(act_info.activation(), fixed_point_position); + 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; } - else + case DataType::QS16: { - std::tie(min_bound, max_bound) = get_activation_layer_test_bounds(act_info.activation(), fixed_point_position); + 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; + } +#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(ref_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(ref_src, distribution, 0); + break; + } + default: + { + ARM_COMPUTE_ERROR("Not supported"); + break; } - std::uniform_int_distribution<> distribution(min_bound, max_bound); - library->fill(ref_src, distribution, 0); } // Compute reference diff --git a/tests/validation/TensorOperations.h b/tests/validation/TensorOperations.h index 9e201e2f04..67dadd6da3 100644 --- a/tests/validation/TensorOperations.h +++ b/tests/validation/TensorOperations.h @@ -569,7 +569,7 @@ void box3x3(const Tensor &in, Tensor &out, BorderMode border_mode, T const } // Depth conversion -template < typename T1, typename T2, typename std::enable_if < std::is_integral::value &&std::is_floating_point::value, int >::type = 0 > +template < typename T1, typename T2, typename std::enable_if < std::is_integral::value &&is_floating_point::value, int >::type = 0 > void depth_convert(const Tensor &in, Tensor &out, ConvertPolicy policy, uint32_t shift) { using namespace fixed_point_arithmetic; @@ -581,7 +581,7 @@ void depth_convert(const Tensor &in, Tensor &out, ConvertPolicy policy, } } -template < typename T1, typename T2, typename std::enable_if < std::is_floating_point::value &&std::is_integral::value, int >::type = 0 > +template < typename T1, typename T2, typename std::enable_if < is_floating_point::value &&std::is_integral::value, int >::type = 0 > void depth_convert(const Tensor &in, Tensor &out, ConvertPolicy policy, uint32_t shift) { using namespace fixed_point_arithmetic; -- cgit v1.2.1