From b0013fe3017e314ce00edc97cfb9e9c6c9a638da Mon Sep 17 00:00:00 2001 From: changzherui Date: Sat, 4 Mar 2023 23:21:06 +0800 Subject: [PATCH] modify addcdiv and addcmul --- .../Tensor/mindspore.Tensor.addcdiv.rst | 2 +- .../Tensor/mindspore.Tensor.addcmul.rst | 2 +- .../ops/mindspore.ops.func_addcdiv.rst | 24 ++++----- .../ops/mindspore.ops.func_addcmul.rst | 28 +++++----- .../_extends/parse/standard_method.py | 34 ++++++------ mindspore/python/mindspore/common/tensor.py | 9 ++-- .../mindspore/ops/function/math_func.py | 54 +++++++++---------- tests/st/ops/gpu/test_addcdiv_op.py | 12 ++--- tests/st/ops/gpu/test_addcmul_op.py | 11 ++-- 9 files changed, 82 insertions(+), 94 deletions(-) diff --git a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.addcdiv.rst b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.addcdiv.rst index 0df640757ac..2d82aea0b86 100644 --- a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.addcdiv.rst +++ b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.addcdiv.rst @@ -1,6 +1,6 @@ mindspore.Tensor.addcdiv ======================== -.. py:method:: mindspore.Tensor.addcdiv(x1, x2, value) +.. py:method:: mindspore.Tensor.addcdiv(tensor1, tensor2, value=1) 详情请参考 :func:`mindspore.ops.addcdiv`。 diff --git a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.addcmul.rst b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.addcmul.rst index 5452ec92860..cf08806fb35 100644 --- a/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.addcmul.rst +++ b/docs/api/api_python/mindspore/Tensor/mindspore.Tensor.addcmul.rst @@ -1,6 +1,6 @@ mindspore.Tensor.addcmul ======================== -.. py:method:: mindspore.Tensor.addcmul(x1, x2, value) +.. py:method:: mindspore.Tensor.addcmul(tensor1, tensor2, value=1) 详情请参考 :func:`mindspore.ops.addcmul`。 diff --git a/docs/api/api_python/ops/mindspore.ops.func_addcdiv.rst b/docs/api/api_python/ops/mindspore.ops.func_addcdiv.rst index 07bf02b62f5..b7f5bf1d5ee 100644 --- a/docs/api/api_python/ops/mindspore.ops.func_addcdiv.rst +++ b/docs/api/api_python/ops/mindspore.ops.func_addcdiv.rst @@ -1,24 +1,24 @@ mindspore.ops.addcdiv ====================== -.. py:function:: mindspore.ops.addcdiv(input_data, x1, x2, value) +.. py:function:: mindspore.ops.addcdiv(input, tensor1, tensor2, value=1) - 执行Tensor `x1` 与Tensor `x2` 的逐元素除法,将结果乘以标量值 `value` ,并将其添加到 `input_data` 中。 + 执行Tensor `tensor1` 与Tensor `tensor2` 的逐元素除法,将结果乘以标量值 `value` ,并将其添加到 `input` 中。 .. math:: - y[i] = input\_data[i] + value[i] * (x1[i] / x2[i]) + y[i] = input[i] + value[i] * (tensor1[i] / tensor2[i]) 参数: - - **input_data** (Tensor) - 要添加的Tensor。 - - **x1** (Tensor) - 分子Tensor。 - - **x2** (Tensor) - 分母Tensor。 - - **value** (Tensor) - Tensor x1/x2的乘数。 + - **input** (Tensor) - 要添加的Tensor。 + - **tensor1** (Tensor) - 分子Tensor。 + - **tensor2** (Tensor) - 分母Tensor。 + - **value** (Union[Tensor, Number]) - tensor1 / tensor2的乘数。默认值:1。 返回: - Tensor,具有与x1/x2相同的shape和dtype。 + Tensor,具有与tensor1/tensor2相同的shape和dtype。 异常: - - **TypeError** - 如果 `x1` 、 `x2` 、 `value` 、 `input_data` 不是Tensor。 - - **ValueError** - 如果无法将 `x1` 广播到 `x2` 。 - - **ValueError** - 如果无法将 `value` 广播到 `x1/x2` 。 - - **ValueError** - 如果无法将 `input_data` 广播到 `value*(x1/x2)` 。 + - **TypeError** - 如果 `tensor1` 、 `tensor2`、 `input` 不是Tensor。 + - **ValueError** - 如果无法将 `tensor1` 广播到 `tensor2` 。 + - **ValueError** - 如果无法将 `value` 广播到 `tensor1/tensor2` 。 + - **ValueError** - 如果无法将 `input` 广播到 `value*(tensor1/tensor2)` 。 diff --git a/docs/api/api_python/ops/mindspore.ops.func_addcmul.rst b/docs/api/api_python/ops/mindspore.ops.func_addcmul.rst index 5029a5a722b..23a69a5719e 100644 --- a/docs/api/api_python/ops/mindspore.ops.func_addcmul.rst +++ b/docs/api/api_python/ops/mindspore.ops.func_addcmul.rst @@ -1,27 +1,27 @@ mindspore.ops.addcmul ====================== -.. py:function:: mindspore.ops.addcmul(input_data, x1, x2, value) +.. py:function:: mindspore.ops.addcmul(input, tensor1, tensor2, value=1) - 执行Tensor `x1` 与Tensor `x2` 的逐元素乘积,将结果乘以标量值 `value` ,并将其添加到 `input_data` 中。 + 执行Tensor `tensor1` 与Tensor `tensor2` 的逐元素乘积,将结果乘以标量值 `value` ,并将其添加到 `input` 中。 .. math:: - output[i] = input\_data[i] + value[i] * (x1[i] * x2[i]) + output[i] = input[i] + value[i] * (tensor1[i] * tensor2[i]) 参数: - - **input_data** (Tensor) - 要添加的Tensor。 - - **x1** (Tensor) - 要乘以的Tensor。 - - **x2** (Tensor) - 要乘以的Tensor。 - - **value** (Tensor) - Tensor x1*x2的乘数。 + - **input** (Tensor) - 要添加的Tensor。 + - **tensor1** (Tensor) - 要乘以的Tensor。 + - **tensor2** (Tensor) - 要乘以的Tensor。 + - **value** (Union[Tensor, Number]) - tensor1 * tensor2的乘数。默认值:1。 返回: - Tensor,具有与x1*x2相同的shape和dtype。 + Tensor,具有与tensor1*tensor2相同的shape和dtype。 异常: - - **TypeError** - 如果 `x1` 、 `x2` 、 `value` 、 `input_data` 不是Tensor。 - - **TypeError** - 如果 `input_data` 的dtype不是:float32、float16、int32之一。 - - **TypeError** - 如果 `x1` 或 `x2` 的dtype不是:float32、float16、int32之一. + - **TypeError** - 如果 `tensor1` 、 `tensor2`、 `input` 不是Tensor。 + - **TypeError** - 如果 `input` 的dtype不是:float32、float16、int32之一。 + - **TypeError** - 如果 `tensor1` 或 `tensor2` 的dtype不是:float32、float16、int32之一. - **TypeError** - 如果 `value` 的dtype不是:float32、float16、int32之一。 - - **ValueError** - 如果无法将 `x1` 广播到 `x2` 。 - - **ValueError** - 如果无法将 `value` 广播到 `x1` * `x2` 。 - - **ValueError** - 如果无法将 `input_data` 广播到 `value*(x1*x2)` 。 + - **ValueError** - 如果无法将 `tensor1` 广播到 `tensor2` 。 + - **ValueError** - 如果无法将 `value` 广播到 `tensor1` * `tensor2` 。 + - **ValueError** - 如果无法将 `input` 广播到 `value*(tensor1*tensor2)` 。 diff --git a/mindspore/python/mindspore/_extends/parse/standard_method.py b/mindspore/python/mindspore/_extends/parse/standard_method.py index e5d6151854f..0466d6600fc 100644 --- a/mindspore/python/mindspore/_extends/parse/standard_method.py +++ b/mindspore/python/mindspore/_extends/parse/standard_method.py @@ -57,8 +57,6 @@ _reduce_sum_default = P.ReduceSum() _reduce_sum_keepdims = P.ReduceSum(True) _mean_keepdims = P.ReduceMean(True) _csr_mm = _csr_ops.CSRMM() -_addcdiv = P.Addcdiv() -_addcmul = P.Addcmul() itemsize_map = {mstype.bool_: 1, mstype.int8: 1, mstype.uint8: 1, mstype.float16: 2, mstype.int16: 2, mstype.uint16: 2, @@ -129,38 +127,38 @@ def prod(x, axis=(), keep_dims=False): return F.prod(x, axis, keep_dims) -def addcdiv(input_data, x1, x2, value): +def addcdiv(input, tensor1, tensor2, value=1): """ - Performs the element-wise division of tensor x1 by tensor x2, + Performs the element-wise division of tensor tensor1 by tensor tensor2, multiply the result by the scalar value and add it to input_data. Args: - input_data (Tensor): The tensor to be added. - x1 (Tensor): The numerator tensor. - x2 (Tensor): The denominator tensor. - value (Tensor): The multiplier for tensor x1/x2. + input (Tensor): The tensor to be added. + tensor1 (Tensor): The numerator tensor. + tensor1 (Tensor): The denominator tensor. + value (Union[Tensor, Number]): The multiplier for tensor1/tensor2. Default: 1. Returns: - Tensor, has the same shape and dtype as x1/x2. + Tensor, has the same shape and dtype as tensor1 / tensor2. """ - return _addcdiv(input_data, x1, x2, value) + return F.addcdiv(input, tensor1, tensor2, value) -def addcmul(input_data, x1, x2, value): +def addcmul(input, tensor1, tensor2, value=1): """ - Performs the element-wise product of tensor x1 and tensor x2, + Performs the element-wise product of tensor tensor1 and tensor tensor2, multiply the result by the scalar value and add it to input_data. Args: - input_data (Tensor): The tensor to be added. - x1 (Tensor): The tensor to be multiplied. - x2 (Tensor): The tensor to be multiplied. - value (Tensor): The multiplier for tensor x1*x2. + input (Tensor): The tensor to be added. + tensor1 (Tensor): The tensor to be multiplied. + tensor2 (Tensor): The tensor to be multiplied. + value (Union[Tensor, Number]): The multiplier for tensor1*tensor2. Default: 1. Returns: - Tensor, has the same shape and dtype as x1*x2. + Tensor, has the same shape and dtype as tensor1 * tensor2. """ - return _addcmul(input_data, x1, x2, value) + return F.addcmul(input, tensor1, tensor2, value) def all_(x, axis=(), keep_dims=False): diff --git a/mindspore/python/mindspore/common/tensor.py b/mindspore/python/mindspore/common/tensor.py index bd2d5edab36..8d46f641504 100644 --- a/mindspore/python/mindspore/common/tensor.py +++ b/mindspore/python/mindspore/common/tensor.py @@ -795,20 +795,19 @@ class Tensor(Tensor_, metaclass=_TensorMeta): self._init_check() Tensor_._flush_from_cache(self) - def addcdiv(self, x1, x2, value): + def addcdiv(self, tensor1, tensor2, value=1): r""" For details, please refer to :func:`mindspore.ops.addcdiv`. """ self._init_check() - return tensor_operator_registry.get('addcdiv')()(self, x1, x2, value) + return tensor_operator_registry.get('addcdiv')()(self, tensor1, tensor2, value) - def addcmul(self, x1, x2, value): + def addcmul(self, tensor1, tensor2, value=1): r""" For details, please refer to :func:`mindspore.ops.addcmul`. """ - self._init_check() - return tensor_operator_registry.get('addcmul')()(self, x1, x2, value) + return tensor_operator_registry.get('addcmul')()(self, tensor1, tensor2, value) def add(self, y): r""" diff --git a/mindspore/python/mindspore/ops/function/math_func.py b/mindspore/python/mindspore/ops/function/math_func.py index 9852418db56..83b079309cb 100644 --- a/mindspore/python/mindspore/ops/function/math_func.py +++ b/mindspore/python/mindspore/ops/function/math_func.py @@ -307,28 +307,28 @@ def add(x, y): return tensor_add(x, y) -def addcdiv(input_data, x1, x2, value): +def addcdiv(input, tensor1, tensor2, value=1): r""" - Performs the element-wise division of tensor x1 by tensor x2, + Performs the element-wise division of tensor tensor1 by tensor tensor2, multiply the result by the scalar value and add it to input_data. .. math:: - y[i] = input\_data[i] + value[i] * (x1[i] / x2[i]) + y[i] = input[i] + value[i] * (tensor1[i] / tensor2[i]) Args: - input_data (Tensor): The tensor to be added. - x1 (Tensor): The numerator tensor. - x2 (Tensor): The denominator tensor. - value (Tensor): The multiplier for tensor x1/x2. + input (Tensor): The tensor to be added. + tensor1 (Tensor): The numerator tensor. + tensor2 (Tensor): The denominator tensor. + value (Union[Tensor, Number]): The multiplier for tensor1/tensor2. Default: 1. Returns: - Tensor, has the same shape and dtype as x1/x2. + Tensor, has the same shape and dtype as tensor1/tensor2. Raises: - TypeError: If dtype of `x1`, `x2`, `value`, `input_data` is not tensor. - ValueError: If `x1` could not be broadcast to a tensor with shape of `x2`. - ValueError: If `value` could not be broadcast to tensors with shapes of `x1/x2`. - ValueError: If `input_data` could not be broadcast to tensors with shapes of `value*(x1/x2)`. + TypeError: If dtype of `tensor1`, `tensor2`, `input` is not tensor. + ValueError: If `tensor1` could not be broadcast to a tensor with shape of `tensor2`. + ValueError: If `value` could not be broadcast to tensors with shapes of `tensor1/tensor2`. + ValueError: If `input` could not be broadcast to tensors with shapes of `value*(tensor1/tensor2)`. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` @@ -342,34 +342,34 @@ def addcdiv(input_data, x1, x2, value): >>> print(y) [1.25 1.6666667 2.5 5. ] """ - return _get_cache_prim(P.Addcdiv)()(input_data, x1, x2, value) + return _get_cache_prim(P.Addcdiv)()(input, tensor1, tensor2, Tensor(value)) -def addcmul(input_data, x1, x2, value): +def addcmul(input, tensor1, tensor2, value=1): r""" - Performs the element-wise product of tensor x1 and tensor x2, + Performs the element-wise product of tensor tensor1 and tensor tensor2, multiply the result by the scalar value and add it to input_data. .. math:: - output[i] = input\_data[i] + value[i] * (x1[i] * x2[i]) + output[i] = input[i] + value[i] * (tensor1[i] * tensor2[i]) Args: - input_data (Tensor): The tensor to be added. - x1 (Tensor): The tensor to be multiplied. - x2 (Tensor): The tensor to be multiplied. - value (Tensor): The multiplier for tensor x1*x2. + input (Tensor): The tensor to be added. + tensor1 (Tensor): The tensor to be multiplied. + tensor2 (Tensor): The tensor to be multiplied. + value (Union[Tensor, Number]): The multiplier for tensor1*tensor2. Default: 1. Returns: Tensor, has the same shape and dtype as x1*x2. Raises: - TypeError: If dtype of `x1`, `x2`, `value`, `input_data` is not tensor. - TypeError: If dtype of `input_data` is not one of: float32, float16, int32. - TypeError: If dtype of `x1` or `x2` is not one of: float32, float16, int32. + TypeError: If dtype of `tensor1`, `tensor2`, `input` is not Tensor. + TypeError: If dtype of `input` is not one of: float32, float16, int32. + TypeError: If dtype of `tensor1` or `tensor2` is not one of: float32, float16, int32. TypeError: If dtype of `value` is not one of: float32, float16, int32. - ValueError: If `x1` could not be broadcast to a tensor with shape of `x2`. - ValueError: If `value` could not be broadcast to tensors with shapes of `x1` * `x2`. - ValueError: If `input_data` could not be broadcast to tensors with shapes of `value*(x1*x2)`. + ValueError: If `tensor1` could not be broadcast to a tensor with shape of `tensor2`. + ValueError: If `value` could not be broadcast to tensors with shapes of `tensor1` * `tensor2`. + ValueError: If `input` could not be broadcast to tensors with shapes of `value*(tensor1*tensor2)`. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` @@ -385,7 +385,7 @@ def addcmul(input_data, x1, x2, value): [ 3. 5. 7.] [ 4. 7. 10.]] """ - return _get_cache_prim(P.Addcmul)()(input_data, x1, x2, value) + return _get_cache_prim(P.Addcmul)()(input, tensor1, tensor2, Tensor(value)) def angle(x): diff --git a/tests/st/ops/gpu/test_addcdiv_op.py b/tests/st/ops/gpu/test_addcdiv_op.py index 938ae3ed833..a5eea0be5eb 100644 --- a/tests/st/ops/gpu/test_addcdiv_op.py +++ b/tests/st/ops/gpu/test_addcdiv_op.py @@ -76,14 +76,10 @@ def test_addcdiv_float64_pynative_value(type_s=np.float64): ).astype(type_s) ) x2 = Tensor(np.array([[0.73312728], [0.50139652]]).astype(type_s)) - value = Tensor( - np.array( - [ - [0.6975477, 0.89641169, -0.16985319, -0.6640372], - [0.79931823, -1.65808474, 0.17895249, -1.41405968], - ] - ).astype(type_s) - ) + value = np.array([ + [0.6975477, 0.89641169, -0.16985319, -0.6640372], + [0.79931823, -1.65808474, 0.17895249, -1.41405968], + ]) addcdiv = P.Addcdiv() output = addcdiv(input_data, x1, x2, value) output_ms = output.asnumpy() diff --git a/tests/st/ops/gpu/test_addcmul_op.py b/tests/st/ops/gpu/test_addcmul_op.py index ff3e6d1dbf3..df0ef154315 100644 --- a/tests/st/ops/gpu/test_addcmul_op.py +++ b/tests/st/ops/gpu/test_addcmul_op.py @@ -77,14 +77,9 @@ def test_addcmul_float64_pynative_value(type_s=np.float64): ).astype(type_s) ) x2 = Tensor(np.array([[0.67554175], [0.25244115]]).astype(type_s)) - value = Tensor( - np.array( - [ - [-0.99526738, -2.57715965, -0.73273605, 0.02546449], - [1.34531189, -0.97279591, 2.4665573, -1.18791833], - ] - ).astype(type_s) - ) + value = np.array([ + [-0.99526738, -2.57715965, -0.73273605, 0.02546449], + [1.34531189, -0.97279591, 2.4665573, -1.18791833],]) addcmul = P.Addcmul() output = addcmul(input_data, x1, x2, value) output_ms = output.asnumpy()