Skip to content

Commit

Permalink
revise tan op (openvinotoolkit#6567)
Browse files Browse the repository at this point in the history
* revise tan op

Signed-off-by: Hu, Yuan2 <[email protected]>

* update doc

add examples in desciption
add the unit of measure
clear input type

Signed-off-by: Hu, Yuan2 <[email protected]>

* add template plugin test case for int type

Signed-off-by: Hu, Yuan2 <[email protected]>

* add template plugin test case for uint and float

remove the float test in backend

Signed-off-by: Hu, Yuan2 <[email protected]>

* modify document

change type to any supported numeric type

Signed-off-by: Hu, Yuan2 <[email protected]>

* fix compile error in openvino-lin

Signed-off-by: Hu, Yuan2 <[email protected]>
  • Loading branch information
tiger100256-hu authored and andrei-cv committed Aug 30, 2021
1 parent 54b0834 commit 72f9161
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 81 deletions.
31 changes: 19 additions & 12 deletions docs/ops/arithmetic/Tan_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<layer ... type="Tan">
<input>
Expand Down
85 changes: 85 additions & 0 deletions docs/template_plugin/tests/functional/op_reference/tan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <gtest/gtest.h>

#include <ie_core.hpp>
#include <ie_ngraph_utils.hpp>
#include <ngraph/ngraph.hpp>
#include <shared_test_classes/base/layer_test_utils.hpp>
#include <tuple>

#include "base_reference_test.hpp"

using namespace ngraph;
using namespace InferenceEngine;
using namespace reference_tests;

namespace {
struct TanParams {
template <class IT>
TanParams(const ngraph::PartialShape& shape, const ngraph::element::Type& iType, const std::vector<IT>& iValues,
const std::vector<IT>& 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<TanParams>, 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<TanParams>& 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<Function> CreateFunction(const PartialShape& input_shape, const element::Type& input_type) {
const auto in = std::make_shared<op::Parameter>(input_type, input_shape);
const auto tan = std::make_shared<op::Tan>(in);
return std::make_shared<Function>(tan, ParameterVector {in});
}
};

TEST_P(ReferenceTanLayerTest, CompareWithHardcodedRefs) {
Exec();
}

std::vector<TanParams> generateTanCombinedParams() {
std::vector<TanParams> combinedParams {
TanParams(ngraph::PartialShape {5}, ngraph::element::i32, std::vector<int32_t> {-2, -1, 0, 1, 2},
std::vector<int32_t> {2, -2, 0, 2, -2}),
TanParams(ngraph::PartialShape {5}, ngraph::element::i64, std::vector<int64_t> {-2, -1, 0, 1, 2},
std::vector<int64_t> {2, -2, 0, 2, -2}),
TanParams(ngraph::PartialShape {5}, ngraph::element::u32, std::vector<uint32_t> {1, 2, 3, 4, 5},
std::vector<uint32_t> {2, 0xFFFFFFFF - 1, 0, 1, 0xFFFFFFFF - 2}),
TanParams(ngraph::PartialShape {5}, ngraph::element::u64, std::vector<uint64_t> {1, 2, 3, 4, 5},
std::vector<uint64_t> {2, 0xFFFFFFFFFFFFFFFF - 1, 0, 1, 0xFFFFFFFFFFFFFFFF - 2}),
TanParams(ngraph::PartialShape {11}, ngraph::element::f32, std::vector<float> {0.f, 0.25f,
-0.25f, 0.5f, -0.5f, 1.f, -1.f, 2.f, -2.f, 4.f, -4.f},
std::vector<float> {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<float16> {0.f, 0.25f,
-0.25f, 0.5f, -0.5f, 1.f, -1.f, 2.f, -2.f, 4.f, -4.f},
std::vector<float16> {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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const std::vector<InferenceEngine::Precision> intPrecisions = {

const std::map<ActivationTypes, std::vector<std::vector<float>>> activationTypes = {
{Sigmoid, {}},
{Tan, {}},
{Tanh, {}},
{Relu, {}},
{Exp, {}},
Expand Down Expand Up @@ -76,6 +77,7 @@ const std::map<ActivationTypes, std::vector<std::vector<float>>> intActivationTy
{Sign, {}},
{Sinh, {}},
{Sqrt, {}},
{Tan, {}},
{Tanh, {}},
};

Expand Down
3 changes: 1 addition & 2 deletions ngraph/core/include/ngraph/op/tan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 11 additions & 1 deletion ngraph/core/reference/include/ngraph/runtime/reference/tan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ namespace ngraph
{
namespace reference
{
template <typename T>
template <typename T,
typename std::enable_if<!std::is_integral<T>::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::tan(arg[i]);
}
}
template <typename T,
typename std::enable_if<std::is_integral<T>::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
2 changes: 1 addition & 1 deletion ngraph/core/src/op/tan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Node>& arg)
: UnaryElementwiseArithmetic(arg)
Expand Down
3 changes: 2 additions & 1 deletion ngraph/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,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
Expand Down Expand Up @@ -337,6 +338,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
Expand Down Expand Up @@ -519,7 +521,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
Expand Down
64 changes: 0 additions & 64 deletions ngraph/test/backend/tan.in.cpp

This file was deleted.

9 changes: 9 additions & 0 deletions ngraph/test/type_prop/tan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "unary_ops.hpp"

using Type = ::testing::Types<ngraph::op::Tan>;

INSTANTIATE_TYPED_TEST_SUITE_P(type_prop_tan, UnaryOperator, Type);
12 changes: 12 additions & 0 deletions ngraph/test/visitors/op/tan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "unary_ops.hpp"

using Types = ::testing::Types<UnaryOperatorType<ngraph::op::v0::Tan, ngraph::element::f32>>;

INSTANTIATE_TYPED_TEST_SUITE_P(visitor_without_attribute,
UnaryOperatorVisitor,
Types,
UnaryOperatorTypeName);

0 comments on commit 72f9161

Please sign in to comment.