Revise cos (#6429)

* Update docs

* create type_prop test for cos

* Create visitor test foor cos

* add RTTI macro

* Create separate ref impl for integer input

* add cos to SLT integer activation type

* add backend test for int input

* add cos to constants.py

* style apply

* fix spec

* add ngraph check for outputs and inputs

* fix inputs ngraph check

* remove unnecessary includes

* style dix
This commit is contained in:
Piotr Szmelczynski 2021-06-30 17:46:11 +02:00 committed by GitHub
parent 7720bde7ac
commit 6d5e6de895
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 74 additions and 46 deletions

View File

@ -2,35 +2,32 @@
**Versioned name**: *Cos-1*
**Category**: Arithmetic unary operation
**Category**: Arithmetic unary operation
**Short description**: *Cos* performs element-wise cosine operation with given tensor.
**Short description**: *Cos* performs element-wise cosine operation on a given input tensor.
**Attributes**:
No attributes available.
**Inputs**
* **1**: An tensor of type T. **Required.**
**Outputs**
* **1**: The result of element-wise cos operation. A tensor of type T.
**Types**
* *T*: any numeric type.
*Cos* does the following with the input tensor *a*:
**Detailed description**: *Cos* performs element-wise cosine operation on a given input tensor, based on the following mathematical formula:
\f[
a_{i} = cos(a_{i})
\f]
**Examples**
**Attributes**: *Cos* operation has no attributes.
*Example 1*
**Inputs**
* **1**: A tensor of type *T* and arbitrary shape. **Required.**
**Outputs**
* **1**: The result of element-wise *Cos* operation. A tensor of type *T* and the same shape as the input tensor.
**Types**
* *T*: any numeric type.
**Example**
```xml
<layer ... type="Cos">

View File

@ -67,6 +67,7 @@ const std::map<ActivationTypes, std::vector<std::vector<float>>> activationTypes
const std::map<ActivationTypes, std::vector<std::vector<float>>> intActivationTypes = {
{Negative, {}},
{Ceiling, {}},
{Cos, {}},
{Sqrt, {}},
{Tanh, {}},
};

View File

@ -21,6 +21,7 @@ VERIFIED_OP_REFERENCES = [
'ConvertLike-1',
'Convolution-1',
'Constant-1',
'Cos-1',
'DeformableConvolution-1',
'DeformablePSROIPooling-1',
'DetectionOutput-1',

View File

@ -16,8 +16,8 @@ namespace ngraph
class NGRAPH_API Cos : public util::UnaryElementwiseArithmetic
{
public:
static constexpr NodeTypeInfo type_info{"Cos", 0};
const NodeTypeInfo& get_type_info() const override { return type_info; }
NGRAPH_RTTI_DECLARATION;
/// \brief Constructs a cosine operation.
Cos() = default;
/// \brief Constructs a cosine operation.

View File

@ -13,7 +13,8 @@ namespace ngraph
{
namespace reference
{
template <typename T>
template <typename T,
typename std::enable_if<!std::is_integral<T>::value, bool>::type = true>
void cos(const T* arg, T* out, size_t count)
{
for (size_t i = 0; i < count; i++)
@ -21,6 +22,16 @@ namespace ngraph
out[i] = std::cos(arg[i]);
}
}
template <typename T,
typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
void cos(const T* arg, T* out, size_t count)
{
for (size_t i = 0; i < count; i++)
{
out[i] = std::roundl(std::cos(arg[i]));
}
}
} // namespace reference
} // namespace runtime
} // namespace ngraph

View File

@ -5,17 +5,16 @@
#include "itt.hpp"
#include "ngraph/op/cos.hpp"
#include "ngraph/op/multiply.hpp"
#include "ngraph/op/negative.hpp"
#include "ngraph/op/sin.hpp"
#include "ngraph/runtime/host_tensor.hpp"
#include "ngraph/runtime/reference/cos.hpp"
#include "ngraph/validation_util.hpp"
using namespace std;
using namespace ngraph;
constexpr NodeTypeInfo op::Cos::type_info;
NGRAPH_RTTI_DEFINITION(op::v0::Cos, "Cos", 0, util::UnaryElementwiseArithmetic);
op::Cos::Cos(const Output<Node>& arg)
: UnaryElementwiseArithmetic(arg)
@ -69,6 +68,7 @@ namespace cosop
bool op::Cos::evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) const
{
NGRAPH_OP_SCOPE(v0_Cos_evaluate);
NGRAPH_CHECK(validate_host_tensor_vector(outputs, 1) && validate_host_tensor_vector(inputs, 1));
return cosop::evaluate_cos(inputs[0], outputs[0], shape_size(get_output_shape(0)));
}

View File

@ -110,6 +110,7 @@ set(SRC
type_prop/convert.cpp
type_prop/convolution.cpp
type_prop/convolution_backprop_data.cpp
type_prop/cos.cpp
type_prop/ctc_greedy_decoder.cpp
type_prop/ctc_greedy_decoder_seq_len.cpp
type_prop/ctc_loss.cpp
@ -231,6 +232,7 @@ set(SRC
visitors/op/constant.cpp
visitors/op/convert.cpp
visitors/op/convolution_backprop.cpp
visitors/op/cos.cpp
visitors/op/cum_sum.cpp
visitors/op/deformable_psroi_pooling.cpp
visitors/op/depth_to_space.cpp

View File

@ -2,23 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
//
#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdlib>
#include <random>
#include <string>
// 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 "ngraph/ngraph.hpp"
#include "util/engine/test_engines.hpp"
@ -31,7 +14,7 @@ using namespace ngraph;
static string s_manifest = "${MANIFEST}";
using TestEngine = test::ENGINE_CLASS_NAME(${BACKEND_NAME});
NGRAPH_TEST(${BACKEND_NAME}, cos)
NGRAPH_TEST(${BACKEND_NAME}, cos_float)
{
Shape shape{11};
auto A = make_shared<op::Parameter>(element::f32, shape);
@ -53,3 +36,16 @@ NGRAPH_TEST(${BACKEND_NAME}, cos)
-0.65364362f});
test_case.run();
}
NGRAPH_TEST(${BACKEND_NAME}, cos_int)
{
Shape shape{5};
auto A = make_shared<op::Parameter>(element::i32, shape);
auto f = make_shared<Function>(make_shared<op::Cos>(A), ParameterVector{A});
auto test_case = test::TestCase<TestEngine>(f);
test_case.add_input<int32_t>({1, 2, 3, 4, 5});
test_case.add_expected_output<int32_t>(shape,
{1, 0, -1, -1, 0});
test_case.run();
}

View File

@ -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::Cos>;
INSTANTIATE_TYPED_TEST_SUITE_P(type_prop_cos, UnaryOperator, Type);

View File

@ -0,0 +1,11 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "unary_ops.hpp"
using Type = ::testing::Types<UnaryOperatorType<ngraph::op::v0::Cos, element::f32>>;
INSTANTIATE_TYPED_TEST_SUITE_P(visitor_without_attribute,
UnaryOperatorVisitor,
Type,
UnaryOperatorTypeName);