From 71360e406b0821828a001049fc965d999c2fd52e Mon Sep 17 00:00:00 2001 From: "Hu, Yuan2" Date: Wed, 7 Jul 2021 10:32:22 +0800 Subject: [PATCH 1/6] revise tan op Signed-off-by: Hu, Yuan2 --- docs/ops/arithmetic/Tan_1.md | 21 ++++++++--------- .../single_layer_tests/activation.cpp | 2 ++ ngraph/core/include/ngraph/op/tan.hpp | 3 +-- .../include/ngraph/runtime/reference/tan.hpp | 12 +++++++++- ngraph/core/src/op/tan.cpp | 2 +- ngraph/test/CMakeLists.txt | 2 ++ ngraph/test/backend/tan.in.cpp | 23 ++++++++++++++++++- ngraph/test/type_prop/tan.cpp | 9 ++++++++ ngraph/test/visitors/op/tan.cpp | 12 ++++++++++ 9 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 ngraph/test/type_prop/tan.cpp create mode 100644 ngraph/test/visitors/op/tan.cpp diff --git a/docs/ops/arithmetic/Tan_1.md b/docs/ops/arithmetic/Tan_1.md index 6ea7d1e9a6b28e..b5b1868b3e457e 100644 --- a/docs/ops/arithmetic/Tan_1.md +++ b/docs/ops/arithmetic/Tan_1.md @@ -6,32 +6,29 @@ **Short description**: *Tan* performs element-wise tangent operation with given tensor. -**Attributes**: +**Detailed description**: Operation takes one input tensor and performs the element-wise tangent function on a given input tensor, based on the following mathematical formula: - No attributes available. +\f[ +a_{i} = tan(a_{i}) +\f] + +**Attributes**: *tan* operation has no attributes. **Inputs** -* **1**: An tensor of type *T*. **Required.** +* **1**: A tensor of type *T* and arbitrary shape. **Required.** **Outputs** -* **1**: The result of element-wise tan operation. A tensor of type *T*. +* **1**: The result of element-wise *tan* applied to the input tensor. A tensor of type *T* and same shape as the input tensor. **Types** -* *T*: any numeric type. +* *T*: any supported numeric type. -*Tan* does the following with the input tensor *a*: - -\f[ -a_{i} = tan(a_{i}) -\f] **Examples** -*Example 1* - ```xml diff --git a/inference-engine/tests/functional/plugin/cpu/shared_tests_instances/single_layer_tests/activation.cpp b/inference-engine/tests/functional/plugin/cpu/shared_tests_instances/single_layer_tests/activation.cpp index 933bf1a75141c6..f78179275e2877 100644 --- a/inference-engine/tests/functional/plugin/cpu/shared_tests_instances/single_layer_tests/activation.cpp +++ b/inference-engine/tests/functional/plugin/cpu/shared_tests_instances/single_layer_tests/activation.cpp @@ -28,6 +28,7 @@ const std::vector intPrecisions = { const std::map>> activationTypes = { {Sigmoid, {}}, + {Tan, {}}, {Tanh, {}}, {Relu, {}}, {Exp, {}}, @@ -76,6 +77,7 @@ const std::map>> intActivationTy {Sign, {}}, {Sinh, {}}, {Sqrt, {}}, + {Tan, {}}, {Tanh, {}}, }; diff --git a/ngraph/core/include/ngraph/op/tan.hpp b/ngraph/core/include/ngraph/op/tan.hpp index 3a6fe15d6e983a..3f191b92de54c5 100644 --- a/ngraph/core/include/ngraph/op/tan.hpp +++ b/ngraph/core/include/ngraph/op/tan.hpp @@ -30,8 +30,7 @@ namespace ngraph class NGRAPH_API Tan : public util::UnaryElementwiseArithmetic { public: - static constexpr NodeTypeInfo type_info{"Tan", 0}; - const NodeTypeInfo& get_type_info() const override { return type_info; } + NGRAPH_RTTI_DECLARATION; /// \brief Constructs a tangent operation. /// /// \param arg Node that produces the input tensor. diff --git a/ngraph/core/reference/include/ngraph/runtime/reference/tan.hpp b/ngraph/core/reference/include/ngraph/runtime/reference/tan.hpp index 33dc63e680495f..e1368f510e211c 100644 --- a/ngraph/core/reference/include/ngraph/runtime/reference/tan.hpp +++ b/ngraph/core/reference/include/ngraph/runtime/reference/tan.hpp @@ -13,7 +13,8 @@ namespace ngraph { namespace reference { - template + template ::value, bool>::type = true> void tan(const T* arg, T* out, size_t count) { for (size_t i = 0; i < count; i++) @@ -21,6 +22,15 @@ namespace ngraph out[i] = std::tan(arg[i]); } } + template ::value, bool>::type = true> + void tan(const T* arg, T* out, size_t count) + { + for (size_t i = 0; i < count; i++) + { + out[i] = std::roundl(std::tan(arg[i])); + } + } } // namespace reference } // namespace runtime } // namespace ngraph diff --git a/ngraph/core/src/op/tan.cpp b/ngraph/core/src/op/tan.cpp index d81d23db097564..905519a1c2a3ff 100644 --- a/ngraph/core/src/op/tan.cpp +++ b/ngraph/core/src/op/tan.cpp @@ -15,7 +15,7 @@ using namespace std; using namespace ngraph; -constexpr NodeTypeInfo op::Tan::type_info; +NGRAPH_RTTI_DEFINITION(op::v0::Tan, "Tan", 0, util::UnaryElementwiseArithmetic); op::Tan::Tan(const Output& arg) : UnaryElementwiseArithmetic(arg) diff --git a/ngraph/test/CMakeLists.txt b/ngraph/test/CMakeLists.txt index 9cbf15c9fe5225..e9435a1d14b4d4 100644 --- a/ngraph/test/CMakeLists.txt +++ b/ngraph/test/CMakeLists.txt @@ -221,6 +221,7 @@ set(SRC type_prop/squared_difference.cpp type_prop/squeeze.cpp type_prop/swish.cpp + type_prop/tan.cpp type_prop/ti.cpp type_prop/tile.cpp type_prop/top_k.cpp @@ -335,6 +336,7 @@ set(SRC visitors/op/strided_slice.cpp visitors/op/subtract.cpp visitors/op/swish.cpp + visitors/op/tan.cpp visitors/op/tanh.cpp visitors/op/topk.cpp visitors/op/transpose.cpp diff --git a/ngraph/test/backend/tan.in.cpp b/ngraph/test/backend/tan.in.cpp index 6664006ad00c5f..7d02d5e45a806d 100644 --- a/ngraph/test/backend/tan.in.cpp +++ b/ngraph/test/backend/tan.in.cpp @@ -34,7 +34,7 @@ using namespace ngraph; static string s_manifest = "${MANIFEST}"; -NGRAPH_TEST(${BACKEND_NAME}, tan) +NGRAPH_TEST(${BACKEND_NAME}, tan_float) { Shape shape{11}; auto A = make_shared(element::f32, shape); @@ -62,3 +62,24 @@ NGRAPH_TEST(${BACKEND_NAME}, tan) -1.15782128f}, read_vector(result))); } + + +NGRAPH_TEST(${BACKEND_NAME}, tan_int32) +{ + Shape shape{5}; + auto A = make_shared(element::i32, shape); + auto f = make_shared(make_shared(A), ParameterVector{A}); + + auto backend = runtime::Backend::create("${BACKEND_NAME}"); + + // Create some tensors for input/output + auto a = backend->create_tensor(element::i32, shape); + vector input{-2, -1, 0, 1, 2}; + copy_data(a, input); + auto result = backend->create_tensor(element::i32, shape); + + auto handle = backend->compile(f); + handle->call_with_validate({result}, {a}); + vector expected{2, -2, 0, 2, -2}; + EXPECT_EQ(expected, read_vector(result)); +} diff --git a/ngraph/test/type_prop/tan.cpp b/ngraph/test/type_prop/tan.cpp new file mode 100644 index 00000000000000..19cf06169628c2 --- /dev/null +++ b/ngraph/test/type_prop/tan.cpp @@ -0,0 +1,9 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "unary_ops.hpp" + +using Type = ::testing::Types; + +INSTANTIATE_TYPED_TEST_SUITE_P(type_prop_tan, UnaryOperator, Type); diff --git a/ngraph/test/visitors/op/tan.cpp b/ngraph/test/visitors/op/tan.cpp new file mode 100644 index 00000000000000..66f61a8dc7b555 --- /dev/null +++ b/ngraph/test/visitors/op/tan.cpp @@ -0,0 +1,12 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "unary_ops.hpp" + +using Types = ::testing::Types>; + +INSTANTIATE_TYPED_TEST_SUITE_P(visitor_without_attribute, + UnaryOperatorVisitor, + Types, + UnaryOperatorTypeName); From 099fac022a982e17eb09ea5171e37f2f6a60a59e Mon Sep 17 00:00:00 2001 From: "Hu, Yuan2" Date: Mon, 12 Jul 2021 17:37:11 +0800 Subject: [PATCH 2/6] update doc add examples in desciption add the unit of measure clear input type Signed-off-by: Hu, Yuan2 --- docs/ops/arithmetic/Tan_1.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/ops/arithmetic/Tan_1.md b/docs/ops/arithmetic/Tan_1.md index b5b1868b3e457e..6e2524f4bc64f8 100644 --- a/docs/ops/arithmetic/Tan_1.md +++ b/docs/ops/arithmetic/Tan_1.md @@ -12,11 +12,21 @@ a_{i} = tan(a_{i}) \f] +*Example 1* + + input = [0.0, 0.25, -0.25, 0.5, -0.5] + output = [0.0, 0.25534192, -0.25534192, 0.54630249, -0.54630249] + +*Example 2* + + input = [-2, -1, 0, 1, 2] + output = [2, -2, 0, 2, -2] + **Attributes**: *tan* operation has no attributes. **Inputs** -* **1**: A tensor of type *T* and arbitrary shape. **Required.** +* **1**: A tensor of type *T* and arbitrary shape, measured in radians. **Required.** **Outputs** @@ -24,7 +34,7 @@ a_{i} = tan(a_{i}) **Types** -* *T*: any supported numeric type. +* *T*: int32,int64,uint32,uint64,float16,float32 **Examples** From 17a20c84a22c272019e2e660b2fc949df496563e Mon Sep 17 00:00:00 2001 From: "Hu, Yuan2" Date: Tue, 20 Jul 2021 09:00:33 +0800 Subject: [PATCH 3/6] add template plugin test case for int type Signed-off-by: Hu, Yuan2 --- .../tests/functional/op_reference/tan.cpp | 82 +++++++++++++++++++ ngraph/test/backend/tan.in.cpp | 21 ----- 2 files changed, 82 insertions(+), 21 deletions(-) create mode 100644 docs/template_plugin/tests/functional/op_reference/tan.cpp diff --git a/docs/template_plugin/tests/functional/op_reference/tan.cpp b/docs/template_plugin/tests/functional/op_reference/tan.cpp new file mode 100644 index 00000000000000..74540c9ffad03c --- /dev/null +++ b/docs/template_plugin/tests/functional/op_reference/tan.cpp @@ -0,0 +1,82 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include +#include +#include +#include +#include + +#include "base_reference_test.hpp" + +using namespace ngraph; +using namespace InferenceEngine; + +namespace { +struct TanParams { + template + TanParams(const ngraph::PartialShape& shape, const ngraph::element::Type& iType, const std::vector& iValues, + const std::vector& oValues) + :pshape(shape), inType(iType), outType(iType), inputData(CreateBlob(iType, iValues)), refData(CreateBlob(iType, oValues)) {} + ngraph::PartialShape pshape; + ngraph::element::Type inType; + ngraph::element::Type outType; + InferenceEngine::Blob::Ptr inputData; + InferenceEngine::Blob::Ptr refData; +}; + +class ReferenceTanLayerTest : public testing::TestWithParam, public CommonReferenceTest { +public: + void SetUp() override { + auto params = GetParam(); + function = CreateFunction(params.pshape, params.inType); + inputData = {params.inputData}; + refOutData = {params.refData}; + } + 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; + return result.str(); + } + +private: + static std::shared_ptr CreateFunction(const PartialShape& input_shape, const element::Type& input_type) { + const auto in = std::make_shared(input_type, input_shape); + const auto tan = std::make_shared(in); + return std::make_shared(tan, ParameterVector {in}); + } +}; + +TEST_P(ReferenceTanLayerTest, CompareWithHardcodedRefs) { + Exec(); +} + +template +std::vector generateTanParamsInt(const ngraph::element::Type& type) { + using T = typename element_type_traits::value_type; + std::vector tanParams { + TanParams(ngraph::PartialShape {5}, type, std::vector {-2, -1, 0, 1, 2}, + std::vector {2, -2, 0, 2, -2}) + }; + return tanParams; +} + +std::vector generateTanCombinedParams() { + const std::vector> tanTypeParams {generateTanParamsInt(ngraph::element::i32), + generateTanParamsInt(ngraph::element::i64)}; + std::vector combinedParams; + std::for_each(tanTypeParams.begin(), tanTypeParams.end(), [&](std::vector params) { + combinedParams.insert(combinedParams.end(), params.begin(), params.end()); + }); + return combinedParams; +} + +INSTANTIATE_TEST_SUITE_P(smoke_TAN_With_Hardcoded_Refs, ReferenceTanLayerTest, ::testing::ValuesIn(generateTanCombinedParams()), + ReferenceTanLayerTest::getTestCaseName); +} // namespace diff --git a/ngraph/test/backend/tan.in.cpp b/ngraph/test/backend/tan.in.cpp index 7d02d5e45a806d..a234639d73d1ef 100644 --- a/ngraph/test/backend/tan.in.cpp +++ b/ngraph/test/backend/tan.in.cpp @@ -62,24 +62,3 @@ NGRAPH_TEST(${BACKEND_NAME}, tan_float) -1.15782128f}, read_vector(result))); } - - -NGRAPH_TEST(${BACKEND_NAME}, tan_int32) -{ - Shape shape{5}; - auto A = make_shared(element::i32, shape); - auto f = make_shared(make_shared(A), ParameterVector{A}); - - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - - // Create some tensors for input/output - auto a = backend->create_tensor(element::i32, shape); - vector input{-2, -1, 0, 1, 2}; - copy_data(a, input); - auto result = backend->create_tensor(element::i32, shape); - - auto handle = backend->compile(f); - handle->call_with_validate({result}, {a}); - vector expected{2, -2, 0, 2, -2}; - EXPECT_EQ(expected, read_vector(result)); -} From d917b22700d2d9b1a6b5cd82248010bfecc0163f Mon Sep 17 00:00:00 2001 From: "Hu, Yuan2" Date: Wed, 21 Jul 2021 15:39:28 +0800 Subject: [PATCH 4/6] add template plugin test case for uint and float remove the float test in backend Signed-off-by: Hu, Yuan2 --- .../tests/functional/op_reference/tan.cpp | 34 +++++----- ngraph/test/CMakeLists.txt | 1 - ngraph/test/backend/tan.in.cpp | 64 ------------------- 3 files changed, 18 insertions(+), 81 deletions(-) delete mode 100644 ngraph/test/backend/tan.in.cpp diff --git a/docs/template_plugin/tests/functional/op_reference/tan.cpp b/docs/template_plugin/tests/functional/op_reference/tan.cpp index 74540c9ffad03c..837fc06617b7da 100644 --- a/docs/template_plugin/tests/functional/op_reference/tan.cpp +++ b/docs/template_plugin/tests/functional/op_reference/tan.cpp @@ -57,23 +57,25 @@ TEST_P(ReferenceTanLayerTest, CompareWithHardcodedRefs) { Exec(); } -template -std::vector generateTanParamsInt(const ngraph::element::Type& type) { - using T = typename element_type_traits::value_type; - std::vector tanParams { - TanParams(ngraph::PartialShape {5}, type, std::vector {-2, -1, 0, 1, 2}, - std::vector {2, -2, 0, 2, -2}) - }; - return tanParams; -} - std::vector generateTanCombinedParams() { - const std::vector> tanTypeParams {generateTanParamsInt(ngraph::element::i32), - generateTanParamsInt(ngraph::element::i64)}; - std::vector combinedParams; - std::for_each(tanTypeParams.begin(), tanTypeParams.end(), [&](std::vector params) { - combinedParams.insert(combinedParams.end(), params.begin(), params.end()); - }); + std::vector combinedParams { + TanParams(ngraph::PartialShape {5}, ngraph::element::i32, std::vector {-2, -1, 0, 1, 2}, + std::vector {2, -2, 0, 2, -2}), + TanParams(ngraph::PartialShape {5}, ngraph::element::i64, std::vector {-2, -1, 0, 1, 2}, + std::vector {2, -2, 0, 2, -2}), + TanParams(ngraph::PartialShape {5}, ngraph::element::u32, std::vector {1, 2, 3, 4, 5}, + std::vector {2, 0xFFFFFFFF - 1, 0, 1, 0xFFFFFFFF - 2}), + TanParams(ngraph::PartialShape {5}, ngraph::element::u64, std::vector {1, 2, 3, 4, 5}, + std::vector {2, 0xFFFFFFFFFFFFFFFF - 1, 0, 1, 0xFFFFFFFFFFFFFFFF - 2}), + TanParams(ngraph::PartialShape {11}, ngraph::element::f32, std::vector {0.f, 0.25f, + -0.25f, 0.5f, -0.5f, 1.f, -1.f, 2.f, -2.f, 4.f, -4.f}, + std::vector {0.00000000f, 0.25534192f, -0.25534192f, 0.54630249f, -0.54630249f, + 1.55740772f, -1.55740772f, -2.18503986f, 2.18503986f, 1.15782128f, -1.15782128f}), + TanParams(ngraph::PartialShape {11}, ngraph::element::f16, std::vector {0.f, 0.25f, + -0.25f, 0.5f, -0.5f, 1.f, -1.f, 2.f, -2.f, 4.f, -4.f}, + std::vector {0.00000000f, 0.25534192f, -0.25534192f, 0.54630249f, -0.54630249f, + 1.55740772f, -1.55740772f, -2.18503986f, 2.18503986f, 1.15782128f, -1.15782128f}) + }; return combinedParams; } diff --git a/ngraph/test/CMakeLists.txt b/ngraph/test/CMakeLists.txt index e9435a1d14b4d4..82b988a7375cfe 100644 --- a/ngraph/test/CMakeLists.txt +++ b/ngraph/test/CMakeLists.txt @@ -520,7 +520,6 @@ set(MULTI_TEST_SRC backend/squeeze.in.cpp backend/subtract.in.cpp backend/swish.in.cpp - backend/tan.in.cpp backend/tanh.in.cpp backend/tile.in.cpp backend/topk.in.cpp diff --git a/ngraph/test/backend/tan.in.cpp b/ngraph/test/backend/tan.in.cpp deleted file mode 100644 index a234639d73d1ef..00000000000000 --- a/ngraph/test/backend/tan.in.cpp +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright (C) 2018-2021 Intel Corporation -// SPDX-License-Identifier: Apache-2.0 -// - -#include -#include -#include -#include -#include -#include - -// clang-format off -#ifdef ${BACKEND_NAME}_FLOAT_TOLERANCE_BITS -#define DEFAULT_FLOAT_TOLERANCE_BITS ${BACKEND_NAME}_FLOAT_TOLERANCE_BITS -#endif - -#ifdef ${BACKEND_NAME}_DOUBLE_TOLERANCE_BITS -#define DEFAULT_DOUBLE_TOLERANCE_BITS ${BACKEND_NAME}_DOUBLE_TOLERANCE_BITS -#endif -// clang-format on - -#include "gtest/gtest.h" -#include "runtime/backend.hpp" -#include "ngraph/runtime/tensor.hpp" -#include "ngraph/ngraph.hpp" -#include "util/all_close.hpp" -#include "util/all_close_f.hpp" -#include "util/ndarray.hpp" -#include "util/test_control.hpp" -#include "util/test_tools.hpp" - -using namespace std; -using namespace ngraph; - -static string s_manifest = "${MANIFEST}"; - -NGRAPH_TEST(${BACKEND_NAME}, tan_float) -{ - Shape shape{11}; - auto A = make_shared(element::f32, shape); - auto f = make_shared(make_shared(A), ParameterVector{A}); - - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - - // Create some tensors for input/output - auto a = backend->create_tensor(element::f32, shape); - vector input{0.f, 0.25f, -0.25f, 0.5f, -0.5f, 1.f, -1.f, 2.f, -2.f, 4.f, -4.f}; - copy_data(a, input); - auto result = backend->create_tensor(element::f32, shape); - auto handle = backend->compile(f); - handle->call_with_validate({result}, {a}); - EXPECT_TRUE(test::all_close_f(vector{0.00000000f, - 0.25534192f, - -0.25534192f, - 0.54630249f, - -0.54630249f, - 1.55740772f, - -1.55740772f, - -2.18503986f, - 2.18503986f, - 1.15782128f, - -1.15782128f}, - read_vector(result))); -} From 5c1835ff2e37601214e0a1dab00d7b95b4cbd967 Mon Sep 17 00:00:00 2001 From: "Hu, Yuan2" Date: Mon, 2 Aug 2021 16:48:25 +0800 Subject: [PATCH 5/6] modify document change type to any supported numeric type Signed-off-by: Hu, Yuan2 --- docs/ops/arithmetic/Tan_1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ops/arithmetic/Tan_1.md b/docs/ops/arithmetic/Tan_1.md index 6e2524f4bc64f8..d9086f7ad5fba0 100644 --- a/docs/ops/arithmetic/Tan_1.md +++ b/docs/ops/arithmetic/Tan_1.md @@ -34,7 +34,7 @@ a_{i} = tan(a_{i}) **Types** -* *T*: int32,int64,uint32,uint64,float16,float32 +* *T*: any supported numeric type. **Examples** From 3e24746738eb9c5784b26d8219dc71a1bc1a9c46 Mon Sep 17 00:00:00 2001 From: "Hu, Yuan2" Date: Tue, 3 Aug 2021 16:08:21 +0800 Subject: [PATCH 6/6] fix compile error in openvino-lin Signed-off-by: Hu, Yuan2 --- docs/template_plugin/tests/functional/op_reference/tan.cpp | 1 + ngraph/test/visitors/op/tan.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/template_plugin/tests/functional/op_reference/tan.cpp b/docs/template_plugin/tests/functional/op_reference/tan.cpp index 837fc06617b7da..5be7a7ad03c810 100644 --- a/docs/template_plugin/tests/functional/op_reference/tan.cpp +++ b/docs/template_plugin/tests/functional/op_reference/tan.cpp @@ -14,6 +14,7 @@ using namespace ngraph; using namespace InferenceEngine; +using namespace reference_tests; namespace { struct TanParams { diff --git a/ngraph/test/visitors/op/tan.cpp b/ngraph/test/visitors/op/tan.cpp index 66f61a8dc7b555..9e786aa3385510 100644 --- a/ngraph/test/visitors/op/tan.cpp +++ b/ngraph/test/visitors/op/tan.cpp @@ -4,7 +4,7 @@ #include "unary_ops.hpp" -using Types = ::testing::Types>; +using Types = ::testing::Types>; INSTANTIATE_TYPED_TEST_SUITE_P(visitor_without_attribute, UnaryOperatorVisitor,