From 3da9d642784a40991bfdf92a7db02f14648eef00 Mon Sep 17 00:00:00 2001 From: mengyuanli Date: Fri, 19 Aug 2022 09:27:55 +0800 Subject: [PATCH] add acos acosh cos tensor interface --- mindspore/ccsrc/pipeline/jit/resource.cc | 3 + .../_extends/parse/standard_method.py | 21 +++++ mindspore/python/mindspore/common/tensor.py | 79 +++++++++++++++++++ mindspore/python/mindspore/ops/functional.py | 3 + tests/st/ops/cpu/test_acos_op.py | 18 +++++ tests/st/ops/cpu/test_acosh_op.py | 18 +++++ tests/st/ops/cpu/test_cos_op.py | 18 +++++ 7 files changed, 160 insertions(+) diff --git a/mindspore/ccsrc/pipeline/jit/resource.cc b/mindspore/ccsrc/pipeline/jit/resource.cc index 1104a87898a..9c19def86c8 100644 --- a/mindspore/ccsrc/pipeline/jit/resource.cc +++ b/mindspore/ccsrc/pipeline/jit/resource.cc @@ -284,6 +284,9 @@ BuiltInTypeMap &GetMethodMap() { {"argmin_with_value", std::string("argmin_with_value")}, // P.ArgMinWithValue {"top_k", std::string("top_k")}, // P.TopK() {"isfinite", std::string("isfinite")}, // P.isfinite() + {"cos", std::string("cos")}, // cos() + {"acos", std::string("acos")}, // acos() + {"acosh", std::string("acosh")}, // acosh() }}, {kObjectTypeRowTensorType, { diff --git a/mindspore/python/mindspore/_extends/parse/standard_method.py b/mindspore/python/mindspore/_extends/parse/standard_method.py index 5ecfb026b93..fbad7c4907c 100644 --- a/mindspore/python/mindspore/_extends/parse/standard_method.py +++ b/mindspore/python/mindspore/_extends/parse/standard_method.py @@ -2820,3 +2820,24 @@ def isfinite(x): Refer to :func:`mindspore.ops.isfinite` for more details. """ return F.isfinite(x) + + +def cos(x): + r""" + Computes cosine of input element-wise. + """ + return F.cos(x) + + +def acos(x): + r""" + Computes arccosine of input tensors element-wise. + """ + return F.acos(x) + + +def acosh(x): + r""" + Computes inverse hyperbolic cosine of the inputs element-wise. + """ + return F.acosh(x) diff --git a/mindspore/python/mindspore/common/tensor.py b/mindspore/python/mindspore/common/tensor.py index bcfe765e9a5..15aa402c4cd 100644 --- a/mindspore/python/mindspore/common/tensor.py +++ b/mindspore/python/mindspore/common/tensor.py @@ -1120,6 +1120,85 @@ class Tensor(Tensor_): self._init_check() return tensor_operator_registry.get('cosh')()(self) + def acos(self): + r""" + Computes arccosine of input tensors element-wise. + + .. math:: + + out_i = cos^{-1}(x_i) + + Returns: + Tensor, has the same shape as `x`. + + Supported Platforms: + ``Ascend`` ``GPU`` ``CPU`` + + Examples: + >>> from mindspore import Tensor + >>> a = Tensor(np.array([0.74, 0.04, 0.30, 0.56]), mindspore.float32) + >>> output = a.acos() + >>> print(output) + [0.737726 1.5307857 1.2661036 0.9764105] + """ + self._init_check() + return tensor_operator_registry.get('acos')(self) + + def cos(self): + r""" + Computes cosine of input element-wise. + + .. math:: + out_i = cos(x_i) + + .. warning:: + Currently support Float16, Float32 data type. If use Float64, there may + be a problem of missing precision. + + Returns: + Tensor, has the same shape as `x`. + + Supported Platforms: + ``Ascend`` ``GPU`` ``CPU`` + + Examples: + >>> from mindspore import Tensor + >>> a = Tensor(np.array([0.24, 0.83, 0.31, 0.09]), mindspore.float32) + >>> output = a.cos() + >>> print(output) + [0.971338 0.6748758 0.95233357 0.9959527] + """ + self._init_check() + return tensor_operator_registry.get('cos')(self) + + def acosh(self): + r""" + Computes inverse hyperbolic cosine of the inputs element-wise. + + .. math:: + + out_i = \cosh^{-1}(input_i) + + .. warning:: + Given an input tensor x, the function computes inverse hyperbolic cosine of every element. + Input range is [1, inf]. + + Returns: + Tensor, has the same shape as `x`. + + Supported Platforms: + ``Ascend`` ``GPU`` ``CPU`` + + Examples: + >>> from mindspore import Tensor + >>> a = Tensor(np.array([1.0, 1.5, 3.0, 100.0]), mindspore.float32) + >>> output = a.acosh() + >>> print(output) + [0. 0.9624237 1.7627472 5.298292] + """ + self._init_check() + return tensor_operator_registry.get('acosh')(self) + def abs(self): """ Return absolute value element-wisely. diff --git a/mindspore/python/mindspore/ops/functional.py b/mindspore/python/mindspore/ops/functional.py index 3f5334a6a15..ea1b5947678 100644 --- a/mindspore/python/mindspore/ops/functional.py +++ b/mindspore/python/mindspore/ops/functional.py @@ -347,6 +347,9 @@ tensor_operator_registry.register('any', P.ReduceAny) tensor_operator_registry.register('atan2', atan2) tensor_operator_registry.register('abs', P.Abs) tensor_operator_registry.register('tan', P.Tan) +tensor_operator_registry.register('acos', acos) +tensor_operator_registry.register('cos', cos) +tensor_operator_registry.register('acosh', acosh) tensor_operator_registry.register('cosh', P.Cosh) tensor_operator_registry.register('pow', P.Pow) tensor_operator_registry.register('amin', amin) diff --git a/tests/st/ops/cpu/test_acos_op.py b/tests/st/ops/cpu/test_acos_op.py index 2dc66341a77..0cefa546c1f 100644 --- a/tests/st/ops/cpu/test_acos_op.py +++ b/tests/st/ops/cpu/test_acos_op.py @@ -50,3 +50,21 @@ def test_acos(dtype): print(output) expect = np.arccos(np_array) assert np.allclose(output.asnumpy(), expect) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.env_onecard +@pytest.mark.parametrize('dtype', [np.float32, np.float64]) +def test_acos_tensor(dtype): + """ + Feature: acos tensor interface + Description: test the rightness of acos tensor interface + Expectation: Success. + """ + np_array = np.array([-1, -0.5, 0, 0.5, 1]).astype(dtype) + input_x = Tensor(np_array) + output = input_x.acos() + print(output) + expect = np.arccos(np_array) + assert np.allclose(output.asnumpy(), expect) diff --git a/tests/st/ops/cpu/test_acosh_op.py b/tests/st/ops/cpu/test_acosh_op.py index a4e49c18f32..f9d711a4aec 100644 --- a/tests/st/ops/cpu/test_acosh_op.py +++ b/tests/st/ops/cpu/test_acosh_op.py @@ -50,3 +50,21 @@ def test_acosh(dtype): print(output) expect = np.arccosh(np_array) assert np.allclose(output.asnumpy(), expect) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.env_onecard +@pytest.mark.parametrize('dtype', [np.float32, np.float64]) +def test_acosh_tensor(dtype): + """ + Feature: acosh tensor interface + Description: test the rightness of acosh tensor interface + Expectation: Success. + """ + np_array = np.array([1, 2, 3, 4, 5]).astype(dtype) + input_x = Tensor(np_array) + output = input_x.acosh() + print(output) + expect = np.arccosh(np_array) + assert np.allclose(output.asnumpy(), expect) diff --git a/tests/st/ops/cpu/test_cos_op.py b/tests/st/ops/cpu/test_cos_op.py index c68de7d1132..6170445379b 100644 --- a/tests/st/ops/cpu/test_cos_op.py +++ b/tests/st/ops/cpu/test_cos_op.py @@ -52,3 +52,21 @@ def test_cos(): print(output) expect = np.cos(np_array) assert np.allclose(output.asnumpy(), expect) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.env_onecard +@pytest.mark.parametrize('dtype', [np.float32, np.float64]) +def test_cos_tensor(dtype): + """ + Feature: cos tensor interface + Description: test the rightness of cos tensor interface + Expectation: Success. + """ + np_array = np.array([-1, -0.5, 0, 0.5, 1]).astype(dtype) + input_x = Tensor(np_array) + output = input_x.cos() + print(output) + expect = np.cos(np_array) + assert np.allclose(output.asnumpy(), expect)