From b7ccdf4490d130573ba1a4e3c5325a765239e0b9 Mon Sep 17 00:00:00 2001 From: Xiping Yan Date: Tue, 19 Oct 2021 19:33:49 +0800 Subject: [PATCH] Xp/fix jira 6671 atanh fail mac wins (#8058) * tmp * Fix atanh correctness test issue and veriyf pass. Signed-off-by: Yan, Xiping * Only keep available input value for correctness test. Signed-off-by: Yan, Xiping --- docs/ops/arithmetic/Atanh_3.md | 11 ++ .../tests/functional/op_reference/atanh.cpp | 112 +++++++++--------- .../ngraph/runtime/reference/atanh.hpp | 20 +++- 3 files changed, 89 insertions(+), 54 deletions(-) diff --git a/docs/ops/arithmetic/Atanh_3.md b/docs/ops/arithmetic/Atanh_3.md index 57deafc4130..0f01caaade2 100644 --- a/docs/ops/arithmetic/Atanh_3.md +++ b/docs/ops/arithmetic/Atanh_3.md @@ -8,8 +8,19 @@ **Detailed description**: *Atanh* performs element-wise hyperbolic inverse tangent (arctangenth) operation on a given input tensor, based on the following mathematical formula: +Float type input: + \f[ a_{i} = atanh(a_{i}) \f] +Signed Intragral type put: + +\f[ a_{i} = (i <= -1) ? std::numeric_limits::min() : (i >= 1) ? std::numeric_limits::max() : atanh(a_{i}) \f] + +Unsigned Intragral type put: + +\f[ a_{i} = (i > 0) ? std::numeric_limits::max() : atanh(a_{i}) \f] + + **Attributes**: Atanh operation has no attributes. **Inputs** diff --git a/docs/template_plugin/tests/functional/op_reference/atanh.cpp b/docs/template_plugin/tests/functional/op_reference/atanh.cpp index d3560e8ec23..7f1f1ecb36e 100644 --- a/docs/template_plugin/tests/functional/op_reference/atanh.cpp +++ b/docs/template_plugin/tests/functional/op_reference/atanh.cpp @@ -2,88 +2,94 @@ // SPDX-License-Identifier: Apache-2.0 // +#include "openvino/op/atanh.hpp" + #include + #include -#include "openvino/op/atanh.hpp" #include "base_reference_test.hpp" #include "openvino/runtime/allocator.hpp" -using namespace reference_tests; using namespace ov; +namespace reference_tests { +namespace { + struct AtanhParams { - template - AtanhParams(const ov::PartialShape& shape, const ov::element::Type& iType, const std::vector& iValues) - : pshape(shape), inType(iType), outType(iType), inputData(CreateTensor(iType, iValues)) { - std::vector oValues; - std::vector output; - for (auto element : iValues) - output.push_back(static_cast(element)); + Tensor input; + Tensor expected; +}; - std::transform(output.begin(), output.end(), output.begin(), [](double input) -> double { - return std::atanh(input); - }); - - if (std::is_integral()) { - std::transform(output.begin(), output.end(), output.begin(), [](double input) -> double { - return std::round(input); - }); - } - - for (auto element : output) - oValues.push_back(static_cast(element)); - refData = CreateTensor(outType, oValues); - } - ov::PartialShape pshape; - ov::element::Type inType; - ov::element::Type outType; - ov::runtime::Tensor inputData; - ov::runtime::Tensor refData; +struct Builder : ParamsBuilder { + REFERENCE_TESTS_ADD_SET_PARAM(Builder, input); + REFERENCE_TESTS_ADD_SET_PARAM(Builder, expected); }; class ReferenceAtanhLayerTest : public testing::TestWithParam, public CommonReferenceTest { public: void SetUp() override { auto params = GetParam(); - function = CreateFunction(params.pshape, params.inType, params.outType); - inputData = {params.inputData}; - refOutData = {params.refData}; + function = CreateFunction(params.input.shape, params.input.type); + inputData = {params.input.data}; + refOutData = {params.expected.data}; } static std::string getTestCaseName(const testing::TestParamInfo& obj) { auto param = obj.param; std::ostringstream result; - result << "shape=" << param.pshape << "_"; - result << "iType=" << param.inType << "_"; - result << "oType=" << param.outType; + result << "shape=" << param.input.shape << "_"; + result << "type=" << param.input.type; + return result.str(); } private: - static std::shared_ptr CreateFunction(const PartialShape& input_shape, const element::Type& input_type, - const element::Type& expected_output_type) { - const auto in = std::make_shared(input_type, input_shape); - const auto atanh = std::make_shared(in); - return std::make_shared(NodeVector {atanh}, ParameterVector {in}); + static std::shared_ptr CreateFunction(const Shape& shape, const element::Type& type) { + const auto in = std::make_shared(type, shape); + const auto out = std::make_shared(in); + return std::make_shared(NodeVector{out}, ParameterVector{in}); } }; -TEST_P(ReferenceAtanhLayerTest, CompareWithRefs) { +TEST_P(ReferenceAtanhLayerTest, AtanhWithHardcodedRefs) { Exec(); } +} // namespace + INSTANTIATE_TEST_SUITE_P( - smoke_Atanh_With_Hardcoded_Refs, ReferenceAtanhLayerTest, - ::testing::Values(AtanhParams(ov::PartialShape {2, 4}, ov::element::f32, - std::vector {-INFINITY, -2.0f, -1.0f, -0.5f, 0.0f, 0.8f, 1.0f, INFINITY}), - AtanhParams(ov::PartialShape {2, 4}, ov::element::f16, - std::vector {-INFINITY, -2.0f, -1.0f, -0.5f, -0.0f, 0.8f, 1.0f, INFINITY}), - AtanhParams(ov::PartialShape {2, 3}, ov::element::i32, - std::vector {std::numeric_limits::min(), -2, -1, 1, 2, std::numeric_limits::max()}), - AtanhParams(ov::PartialShape {2, 3}, ov::element::u32, - std::vector {std::numeric_limits::min(), 0, 1, 2, 3, std::numeric_limits::max()}), - AtanhParams(ov::PartialShape {2, 3}, ov::element::i64, - std::vector {std::numeric_limits::min(), -2, -1, 1, 2, std::numeric_limits::max()}), - AtanhParams(ov::PartialShape {2, 3}, ov::element::u64, - std::vector {std::numeric_limits::min(), 0, 1, 2, 3, std::numeric_limits::max()})), + smoke_Atanh_With_Hardcoded_Refs, + ReferenceAtanhLayerTest, + ::testing::Values( + Builder{} + .input({{5}, element::f16, std::vector{-1.0f, -0.5f, 0.0f, 0.8f, 1.0f}}) + .expected({{5}, + element::f16, + std::vector{-INFINITY, -0.54930614f, 0.00000000f, 1.0986123f, INFINITY}}), + Builder{} + .input({{5}, element::f32, std::vector{-1.0f, -0.5f, 0.0f, 0.8f, 1.0f}}) + .expected( + {{5}, element::f32, std::vector{-INFINITY, -0.54930614f, 0.00000000f, 1.0986123f, INFINITY}}), + Builder{} + .input({{3}, element::i32, std::vector{-1, 0, 1}}) + .expected( + {{3}, + element::i32, + std::vector{std::numeric_limits::min(), 0, std::numeric_limits::max()}}), + Builder{} + .input({{2}, element::u32, std::vector{0, 1}}) + .expected({{2}, element::u32, std::vector{0, std::numeric_limits::max()}}), + Builder{} + .input({{3}, element::i64, std::vector{-1, 0, 1}}) + .expected({{3}, + element::i64, + std::vector{ + std::numeric_limits::min(), + 0, + std::numeric_limits::max(), + }}), + Builder{} + .input({{2}, element::u64, std::vector{0, 1}}) + .expected({{2}, element::u64, std::vector{0, std::numeric_limits::max()}})), ReferenceAtanhLayerTest::getTestCaseName); +} // namespace reference_tests diff --git a/ngraph/core/reference/include/ngraph/runtime/reference/atanh.hpp b/ngraph/core/reference/include/ngraph/runtime/reference/atanh.hpp index 02ca6e87699..de2cfb8979b 100644 --- a/ngraph/core/reference/include/ngraph/runtime/reference/atanh.hpp +++ b/ngraph/core/reference/include/ngraph/runtime/reference/atanh.hpp @@ -21,7 +21,25 @@ void atanh(const T* arg, T* out, size_t count) { template ::value, bool>::type = true> void atanh(const T* arg, T* out, size_t count) { for (size_t i = 0; i < count; i++) { - out[i] = std::roundl(std::atanh(arg[i])); + /** + * Intgral type don't support: NAN and INFINITY. + * So we clip input value, and make sure return avaiable value. + */ + if (std::is_same::value || std::is_same::value || std::is_same::value) { + if (arg[i] > 0) { + out[i] = std::numeric_limits::max(); + } else { + out[i] = std::roundl(std::atanh(arg[i])); + } + } else { + if (arg[i] <= -1) { + out[i] = std::numeric_limits::min(); + } else if (arg[i] >= 1) { + out[i] = std::numeric_limits::max(); + } else { + out[i] = std::roundl(std::atanh(arg[i])); + } + } } } } // namespace reference