diff --git a/docs/ops/arithmetic/Cos_1.md b/docs/ops/arithmetic/Cos_1.md index 3ff7c3593cabd8..462b98fde2c5c4 100644 --- a/docs/ops/arithmetic/Cos_1.md +++ b/docs/ops/arithmetic/Cos_1.md @@ -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 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 1ad6c8912f70a5..11a2c41a68e52c 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 @@ -67,6 +67,7 @@ const std::map>> activationTypes const std::map>> intActivationTypes = { {Negative, {}}, {Ceiling, {}}, + {Cos, {}}, {Sqrt, {}}, {Tanh, {}}, }; diff --git a/inference-engine/tests/ie_test_utils/functional_test_utils/layer_tests_summary/utils/constants.py b/inference-engine/tests/ie_test_utils/functional_test_utils/layer_tests_summary/utils/constants.py index 6548a3d201bcde..7693826baf2d10 100644 --- a/inference-engine/tests/ie_test_utils/functional_test_utils/layer_tests_summary/utils/constants.py +++ b/inference-engine/tests/ie_test_utils/functional_test_utils/layer_tests_summary/utils/constants.py @@ -21,6 +21,7 @@ 'ConvertLike-1', 'Convolution-1', 'Constant-1', + 'Cos-1', 'DeformableConvolution-1', 'DeformablePSROIPooling-1', 'DetectionOutput-1', diff --git a/ngraph/core/include/ngraph/op/cos.hpp b/ngraph/core/include/ngraph/op/cos.hpp index 5e5e2df07ee26c..a5ce9c5012cbeb 100644 --- a/ngraph/core/include/ngraph/op/cos.hpp +++ b/ngraph/core/include/ngraph/op/cos.hpp @@ -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. diff --git a/ngraph/core/reference/include/ngraph/runtime/reference/cos.hpp b/ngraph/core/reference/include/ngraph/runtime/reference/cos.hpp index b28947246a446c..f6d385bf2e369f 100644 --- a/ngraph/core/reference/include/ngraph/runtime/reference/cos.hpp +++ b/ngraph/core/reference/include/ngraph/runtime/reference/cos.hpp @@ -13,7 +13,8 @@ namespace ngraph { namespace reference { - template + template ::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 ::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 diff --git a/ngraph/core/src/op/cos.cpp b/ngraph/core/src/op/cos.cpp index aa3ac962123acc..552fafc6854e98 100644 --- a/ngraph/core/src/op/cos.cpp +++ b/ngraph/core/src/op/cos.cpp @@ -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& 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))); } diff --git a/ngraph/test/CMakeLists.txt b/ngraph/test/CMakeLists.txt index 04ce2102bf9f5c..58882ed720e2c0 100644 --- a/ngraph/test/CMakeLists.txt +++ b/ngraph/test/CMakeLists.txt @@ -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 diff --git a/ngraph/test/backend/cos.in.cpp b/ngraph/test/backend/cos.in.cpp index 802a138689e898..78311d9d477baa 100644 --- a/ngraph/test/backend/cos.in.cpp +++ b/ngraph/test/backend/cos.in.cpp @@ -2,23 +2,6 @@ // 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 "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(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(element::i32, shape); + auto f = make_shared(make_shared(A), ParameterVector{A}); + + auto test_case = test::TestCase(f); + test_case.add_input({1, 2, 3, 4, 5}); + test_case.add_expected_output(shape, + {1, 0, -1, -1, 0}); + test_case.run(); +} diff --git a/ngraph/test/type_prop/cos.cpp b/ngraph/test/type_prop/cos.cpp new file mode 100644 index 00000000000000..3612ed7a8d36ef --- /dev/null +++ b/ngraph/test/type_prop/cos.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_cos, UnaryOperator, Type); diff --git a/ngraph/test/visitors/op/cos.cpp b/ngraph/test/visitors/op/cos.cpp new file mode 100644 index 00000000000000..6b48a4b8b0ccf2 --- /dev/null +++ b/ngraph/test/visitors/op/cos.cpp @@ -0,0 +1,11 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "unary_ops.hpp" +using Type = ::testing::Types>; + +INSTANTIATE_TYPED_TEST_SUITE_P(visitor_without_attribute, + UnaryOperatorVisitor, + Type, + UnaryOperatorTypeName);