diff --git a/docs/ops/arithmetic/Tan_1.md b/docs/ops/arithmetic/Tan_1.md index 6ea7d1e9a6b28e..d9086f7ad5fba0 100644 --- a/docs/ops/arithmetic/Tan_1.md +++ b/docs/ops/arithmetic/Tan_1.md @@ -6,32 +6,39 @@ **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] + +*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**: An tensor of type *T*. **Required.** +* **1**: A tensor of type *T* and arbitrary shape, measured in radians. **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/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..5be7a7ad03c810 --- /dev/null +++ b/docs/template_plugin/tests/functional/op_reference/tan.cpp @@ -0,0 +1,85 @@ +// 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; +using namespace reference_tests; + +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(); +} + +std::vector generateTanCombinedParams() { + 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; +} + +INSTANTIATE_TEST_SUITE_P(smoke_TAN_With_Hardcoded_Refs, ReferenceTanLayerTest, ::testing::ValuesIn(generateTanCombinedParams()), + ReferenceTanLayerTest::getTestCaseName); +} // namespace 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..82b988a7375cfe 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 @@ -518,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 6664006ad00c5f..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) -{ - 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))); -} 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..9e786aa3385510 --- /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);