From 08c9b0f882e97a03f43258825fa1c01396fbc3e4 Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 5 Jan 2023 16:45:47 +0800 Subject: [PATCH] add tensor frac diff --- docs/api/api_python/mindspore.ops.rst | 2 + .../Tensor/mindspore.Tensor.diff.rst | 6 +++ .../Tensor/mindspore.Tensor.frac.rst | 6 +++ .../api_python/mindspore/mindspore.Tensor.rst | 2 + .../ops/mindspore.ops.func_diff.rst | 22 +++++++++ .../ops/mindspore.ops.func_frac.rst | 15 ++++++ docs/api/api_python_en/Tensor_list.rst | 2 + docs/api/api_python_en/mindspore.ops.rst | 2 + mindspore/ccsrc/pipeline/jit/resource.cc | 2 + .../_extends/parse/standard_method.py | 14 ++++++ mindspore/python/mindspore/common/tensor.py | 14 ++++++ mindspore/python/mindspore/ops/functional.py | 2 + tests/st/ops/test_func_diff.py | 46 +++++++++++++++++++ tests/st/ops/test_func_frac.py | 46 +++++++++++++++++++ tests/st/tensor/test_diff.py | 45 ++++++++++++++++++ tests/st/tensor/test_frac.py | 45 ++++++++++++++++++ 16 files changed, 271 insertions(+) create mode 100644 docs/api/api_python/mindspore/Tensor/mindspore.Tensor.diff.rst create mode 100644 docs/api/api_python/mindspore/Tensor/mindspore.Tensor.frac.rst create mode 100644 docs/api/api_python/ops/mindspore.ops.func_diff.rst create mode 100644 docs/api/api_python/ops/mindspore.ops.func_frac.rst create mode 100644 tests/st/ops/test_func_diff.py create mode 100644 tests/st/ops/test_func_frac.py create mode 100644 tests/st/tensor/test_diff.py create mode 100644 tests/st/tensor/test_frac.py diff --git a/docs/api/api_python/mindspore.ops.rst b/docs/api/api_python/mindspore.ops.rst index 168a2775639..6e0ff496aeb 100644 --- a/docs/api/api_python/mindspore.ops.rst +++ b/docs/api/api_python/mindspore.ops.rst @@ -224,6 +224,7 @@ mindspore.ops mindspore.ops.cosh mindspore.ops.cosine_similarity mindspore.ops.cov + mindspore.ops.diff mindspore.ops.deg2rad mindspore.ops.digamma mindspore.ops.div @@ -237,6 +238,7 @@ mindspore.ops mindspore.ops.floor_mod mindspore.ops.float_power mindspore.ops.fmod + mindspore.ops.frac mindspore.ops.heaviside mindspore.ops.hypot mindspore.ops.i0 diff --git a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.diff.rst b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.diff.rst new file mode 100644 index 00000000000..dc5d6c8a6b6 --- /dev/null +++ b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.diff.rst @@ -0,0 +1,6 @@ +mindspore.Tensor.diff +===================== + +.. py:method:: mindspore.Tensor.diff(x, n=1, axis=-1, prepend=None, append=None) + + 详情请参考 :func:`mindspore.ops.diff`。 \ No newline at end of file diff --git a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.frac.rst b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.frac.rst new file mode 100644 index 00000000000..1a2964f1d51 --- /dev/null +++ b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.frac.rst @@ -0,0 +1,6 @@ +mindspore.Tensor.frac +===================== + +.. py:method:: mindspore.Tensor.frac() + + 详情请参考 :func:`mindspore.ops.frac`。 \ No newline at end of file diff --git a/docs/api/api_python/mindspore/mindspore.Tensor.rst b/docs/api/api_python/mindspore/mindspore.Tensor.rst index ee73f04afbe..2283a331e60 100644 --- a/docs/api/api_python/mindspore/mindspore.Tensor.rst +++ b/docs/api/api_python/mindspore/mindspore.Tensor.rst @@ -93,6 +93,7 @@ mindspore.Tensor mindspore.Tensor.diag mindspore.Tensor.diagflat mindspore.Tensor.diagonal + mindspore.Tensor.diff mindspore.Tensor.digamma mindspore.Tensor.div mindspore.Tensor.divide @@ -119,6 +120,7 @@ mindspore.Tensor mindspore.Tensor.flush_from_cache mindspore.Tensor.fmod mindspore.Tensor.fold + mindspore.Tensor.frac mindspore.Tensor.from_numpy mindspore.Tensor.gather mindspore.Tensor.gather_elements diff --git a/docs/api/api_python/ops/mindspore.ops.func_diff.rst b/docs/api/api_python/ops/mindspore.ops.func_diff.rst new file mode 100644 index 00000000000..d56f70958e1 --- /dev/null +++ b/docs/api/api_python/ops/mindspore.ops.func_diff.rst @@ -0,0 +1,22 @@ +mindspore.ops.diff +================== + +.. py:function:: mindspore.ops.diff(x, n=1, axis=-1, prepend=None, append=None) + + 沿着给定维度计算输入Tensor的n阶前向差分。 + + 参数: + - **x** (Tensor) - 输入Tensor。x元素的数据类型不支持uint16, uint32 或 uint64。 + - **n** 递归计算差分的阶数,目前只支持1。默认值:1。 + - **axis** - 计算差分的维度,默认是最后一维。默认值:-1。 + - **prepend** - 在计算差分之前,沿 axis 将值添加到 input 或附加到 input。它们的维度必须与输入的维度相同,并且它们的shape必须与输入的shape匹配,但 axis 除外。默认值:None。 + - **append** - 在计算差分之前,沿 axis 将值添加到 input 或附加到 input。它们的维度必须与输入的维度相同,并且它们的shape必须与输入的shape匹配,但 axis 除外。默认值:None。 + + 返回: + Tensor, 输入Tensor。 + + 异常: + - **TypeError** - 如果 `x` 不是Tensor。 + - **TypeError** - 如果 `x` 的元素的数据类型是uint16, uint32 或 uint64。 + - **TypeError** - 如果 `x` 的维度小于1。 + - **RuntimeError** - 如果 `n` 不是1。 \ No newline at end of file diff --git a/docs/api/api_python/ops/mindspore.ops.func_frac.rst b/docs/api/api_python/ops/mindspore.ops.func_frac.rst new file mode 100644 index 00000000000..bc3eed2766d --- /dev/null +++ b/docs/api/api_python/ops/mindspore.ops.func_frac.rst @@ -0,0 +1,15 @@ +mindspore.ops.frac +================== + +.. py:function:: mindspore.ops.frac(x) + + 计算 x 中每个元素的小数部分。 + + 参数: + - **x** (Tensor) - 输入Tensor。 + + 返回: + Tensor, `x` 的frac计算结果。 + + 异常: + - **TypeError** - 如果 `x` 不是Tensor。 \ No newline at end of file diff --git a/docs/api/api_python_en/Tensor_list.rst b/docs/api/api_python_en/Tensor_list.rst index ac61bd22b68..1236cd27d2e 100644 --- a/docs/api/api_python_en/Tensor_list.rst +++ b/docs/api/api_python_en/Tensor_list.rst @@ -99,6 +99,7 @@ mindspore.Tensor.diag mindspore.Tensor.diagflat mindspore.Tensor.diagonal + mindspore.Tensor.diff mindspore.Tensor.digamma mindspore.Tensor.div mindspore.Tensor.divide @@ -125,6 +126,7 @@ mindspore.Tensor.flush_from_cache mindspore.Tensor.fold mindspore.Tensor.fmod + mindspore.Tensor.frac mindspore.Tensor.from_numpy mindspore.Tensor.gather mindspore.Tensor.gather_elements diff --git a/docs/api/api_python_en/mindspore.ops.rst b/docs/api/api_python_en/mindspore.ops.rst index 325fa0c7498..3bd30171ccc 100644 --- a/docs/api/api_python_en/mindspore.ops.rst +++ b/docs/api/api_python_en/mindspore.ops.rst @@ -226,6 +226,7 @@ Element-by-Element Operations mindspore.ops.cov mindspore.ops.deg2rad mindspore.ops.digamma + mindspore.ops.diff mindspore.ops.div mindspore.ops.divide mindspore.ops.erf @@ -237,6 +238,7 @@ Element-by-Element Operations mindspore.ops.floor_mod mindspore.ops.float_power mindspore.ops.fmod + mindspore.ops.frac mindspore.ops.heaviside mindspore.ops.hypot mindspore.ops.i0 diff --git a/mindspore/ccsrc/pipeline/jit/resource.cc b/mindspore/ccsrc/pipeline/jit/resource.cc index 8c9fbf53062..66625414874 100644 --- a/mindspore/ccsrc/pipeline/jit/resource.cc +++ b/mindspore/ccsrc/pipeline/jit/resource.cc @@ -460,6 +460,8 @@ BuiltInTypeMap &GetMethodMap() { {"trunc", std::string("trunc")}, // trunc() {"where", std::string("where")}, // where() {"imag", std::string("imag")}, // imag() + {"diff", std::string("diff")}, // diff() + {"frac", std::string("frac")}, // frac() }}, {kObjectTypeRowTensorType, { diff --git a/mindspore/python/mindspore/_extends/parse/standard_method.py b/mindspore/python/mindspore/_extends/parse/standard_method.py index 072ed6ddfe5..bb2a0669e7a 100644 --- a/mindspore/python/mindspore/_extends/parse/standard_method.py +++ b/mindspore/python/mindspore/_extends/parse/standard_method.py @@ -4260,3 +4260,17 @@ def imag(input): Returns a new tensor containing imaginary value of the input. """ return F.imag(input) + + +def diff(x): + r""" + For details, please refer to :func:`mindspore.ops.diff`. + """ + return F.diff(x) + + +def frac(x): + r""" + For details, please refer to :func:`mindspore.ops.frac`. + """ + return F.frac(x) diff --git a/mindspore/python/mindspore/common/tensor.py b/mindspore/python/mindspore/common/tensor.py index 6aa2444d630..780dadc9fd0 100644 --- a/mindspore/python/mindspore/common/tensor.py +++ b/mindspore/python/mindspore/common/tensor.py @@ -2561,6 +2561,20 @@ class Tensor(Tensor_): self._init_check() return tensor_operator_registry.get('matrix_determinant')(self) + def diff(self): + r""" + For details, please refer to :func:`mindspore.ops.diff`. + """ + self._init_check() + return tensor_operator_registry.get('diff')(self) + + def frac(self): + r""" + For details, please refer to :func:`mindspore.ops.frac`. + """ + self._init_check() + return tensor_operator_registry.get('frac')(self) + def digamma(self): r""" For details, please refer to :func:`mindspore.ops.digamma`. diff --git a/mindspore/python/mindspore/ops/functional.py b/mindspore/python/mindspore/ops/functional.py index c221d89679d..d003a27d0b5 100644 --- a/mindspore/python/mindspore/ops/functional.py +++ b/mindspore/python/mindspore/ops/functional.py @@ -364,9 +364,11 @@ tensor_operator_registry.register('cholesky', P.Cholesky) tensor_operator_registry.register('cholesky_inverse', P.CholeskyInverse) tensor_operator_registry.register('expand', expand) tensor_operator_registry.register('cumprod', cumprod) +tensor_operator_registry.register('diff', diff) tensor_operator_registry.register('div', div) tensor_operator_registry.register('equal', equal) tensor_operator_registry.register('expm1', expm1) +tensor_operator_registry.register('frac', frac) tensor_operator_registry.register('isinf', isinf) tensor_operator_registry.register('isnan', isnan) tensor_operator_registry.register('is_complex', is_complex) diff --git a/tests/st/ops/test_func_diff.py b/tests/st/ops/test_func_diff.py new file mode 100644 index 00000000000..29a0c2496ba --- /dev/null +++ b/tests/st/ops/test_func_diff.py @@ -0,0 +1,46 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +import numpy as np +import pytest + +import mindspore as ms +import mindspore.nn as nn +import mindspore.ops as ops + + +class Net(nn.Cell): + def construct(self, x): + return ops.diff(x) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.platform_arm_cpu +@pytest.mark.platform_x86_gpu_training +@pytest.mark.env_onecard +@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE]) +def test_diff_normal(mode): + """ + Feature: diff + Description: Verify the result of diff + Expectation: success + """ + ms.set_context(mode=mode) + net = Net() + x = ms.Tensor([1, 3, 2], dtype=ms.float32) + output = net(x) + expect_output = np.array([2, -1]) + assert np.allclose(output.asnumpy(), expect_output) diff --git a/tests/st/ops/test_func_frac.py b/tests/st/ops/test_func_frac.py new file mode 100644 index 00000000000..83d2497a851 --- /dev/null +++ b/tests/st/ops/test_func_frac.py @@ -0,0 +1,46 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +import numpy as np +import pytest + +import mindspore as ms +import mindspore.nn as nn +import mindspore.ops as ops + + +class Net(nn.Cell): + def construct(self, x): + return ops.frac(x) + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.platform_arm_cpu +@pytest.mark.platform_x86_gpu_training +@pytest.mark.env_onecard +@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE]) +def test_frac_normal(mode): + """ + Feature: frac + Description: Verify the result of frac + Expectation: success + """ + ms.set_context(mode=mode) + net = Net() + x = ms.Tensor([1, 2.5, -3.2], dtype=ms.float32) + output = net(x) + expect_output = np.array([0.0000, 0.5000, -0.2000]) + assert np.allclose(output.asnumpy(), expect_output) diff --git a/tests/st/tensor/test_diff.py b/tests/st/tensor/test_diff.py new file mode 100644 index 00000000000..c260aed94fa --- /dev/null +++ b/tests/st/tensor/test_diff.py @@ -0,0 +1,45 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +import numpy as np +import pytest + +import mindspore as ms +import mindspore.nn as nn + + +class Net(nn.Cell): + def construct(self, x): + return x.diff() + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.platform_arm_cpu +@pytest.mark.platform_x86_gpu_training +@pytest.mark.env_onecard +@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE]) +def test_diff_normal(mode): + """ + Feature: diff + Description: Verify the result of diff + Expectation: success + """ + ms.set_context(mode=mode) + net = Net() + x = ms.Tensor([1, 3, 2], dtype=ms.float32) + output = net(x) + expect_output = np.array([2, -1]) + assert np.allclose(output.asnumpy(), expect_output) diff --git a/tests/st/tensor/test_frac.py b/tests/st/tensor/test_frac.py new file mode 100644 index 00000000000..bdca95df720 --- /dev/null +++ b/tests/st/tensor/test_frac.py @@ -0,0 +1,45 @@ +# Copyright 2022 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +import numpy as np +import pytest + +import mindspore as ms +import mindspore.nn as nn + + +class Net(nn.Cell): + def construct(self, x): + return x.frac() + + +@pytest.mark.level0 +@pytest.mark.platform_x86_cpu +@pytest.mark.platform_arm_cpu +@pytest.mark.platform_x86_gpu_training +@pytest.mark.env_onecard +@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE]) +def test_frac_normal(mode): + """ + Feature: frac + Description: Verify the result of frac + Expectation: success + """ + ms.set_context(mode=mode) + net = Net() + x = ms.Tensor([1, 2.5, -3.2], dtype=ms.float32) + output = net(x) + expect_output = np.array([0.0000, 0.5000, -0.2000]) + assert np.allclose(output.asnumpy(), expect_output, 1e-3, 1e-3)