Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise cos #6429

Merged
merged 17 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions docs/ops/arithmetic/Cos_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:
**Detailed description**: *Cos* performs element-wise cosine operation on a given input tensor, based on the following mathematical formula:

No attributes available.
\f[
a_{i} = cos(a_{i})
\f]

**Attributes**: *Cos* 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 cos operation. A tensor of type T.
* **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.

*Cos* does the following with the input tensor *a*:

\f[
a_{i} = cos(a_{i})
\f]

**Examples**

*Example 1*
**Example**

```xml
<layer ... type="Cos">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, {}},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'ConvertLike-1',
'Convolution-1',
'Constant-1',
'Cos-1',
'DeformableConvolution-1',
'DeformablePSROIPooling-1',
'DetectionOutput-1',
Expand Down
4 changes: 2 additions & 2 deletions ngraph/core/include/ngraph/op/cos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion ngraph/core/reference/include/ngraph/runtime/reference/cos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@ 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++)
{
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
8 changes: 4 additions & 4 deletions ngraph/core/src/op/cos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)));
}

Expand Down
2 changes: 2 additions & 0 deletions ngraph/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 14 additions & 18 deletions ngraph/test/backend/cos.in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);
Expand All @@ -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();
}
9 changes: 9 additions & 0 deletions ngraph/test/type_prop/cos.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::Cos>;

INSTANTIATE_TYPED_TEST_SUITE_P(type_prop_cos, UnaryOperator, Type);
11 changes: 11 additions & 0 deletions ngraph/test/visitors/op/cos.cpp
Original file line number Diff line number Diff line change
@@ -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);