From 9acb5b01a42cd1cec80210913f279b1651c85dad Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Mon, 18 Nov 2024 19:51:56 +0800 Subject: [PATCH 01/21] add_vecdot --- python/paddle/__init__.py | 2 + python/paddle/linalg.py | 2 + python/paddle/tensor/__init__.py | 1 + python/paddle/tensor/linalg.py | 42 +++++++++++ test/legacy_test/test_dot_op.py | 116 +++++++++++++++++++++++++++++++ 5 files changed, 163 insertions(+) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 8dac68efce37a..07b04bead8bce 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -240,6 +240,7 @@ t_, transpose, transpose_, + vecdot, ) from .tensor.logic import ( allclose, @@ -1204,4 +1205,5 @@ 'positive', 'from_dlpack', 'to_dlpack', + 'vecdot', ] diff --git a/python/paddle/linalg.py b/python/paddle/linalg.py index 55a1acba16c07..2a19b79ce9aba 100644 --- a/python/paddle/linalg.py +++ b/python/paddle/linalg.py @@ -47,6 +47,7 @@ svd_lowrank, triangular_solve, vector_norm, + vecdot, ) __all__ = [ @@ -55,6 +56,7 @@ 'norm', 'matrix_norm', 'vector_norm', + 'vecdot', 'cond', 'cov', 'corrcoef', diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 041d5de007b20..049f6ba071f1d 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -102,6 +102,7 @@ t_, transpose, transpose_, + vecdot, ) from .logic import ( # noqa: F401 allclose, diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index db010a6ab816f..135a07c42882a 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1867,6 +1867,48 @@ def dot(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: return out +def vecdot(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: + """ + This operator calculates inner product for vectors. + + Note: + Support 1-d and 2-d Tensor. When it is 2d, the first dimension of this matrix + is the batch dimension, which means that the vectors of multiple batches are dotted. + + Parameters: + x(Tensor): 1-D or 2-D ``Tensor``. Its dtype should be ``float32``, ``float64``, ``int32``, ``int64``, ``complex64``, ``complex128`` + y(Tensor): 1-D or 2-D ``Tensor``. Its dtype should be ``float32``, ``float64``, ``int32``, ``int64``, ``complex64``, ``complex128`` + name(str|None, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name` + + Returns: + Tensor: the calculated result Tensor. + + Examples: + + .. code-block:: python + + >>> import paddle + + >>> # 1-D Tensor * 1-D Tensor + >>> x = paddle.to_tensor([1, 2, 3]) + >>> y = paddle.to_tensor([4, 5, 6]) + >>> z = paddle.linalg.vecdot(x, y) + >>> print(z) + Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, + 32) + + >>> # 2-D Tensor * 2-D Tensor + >>> x = paddle.to_tensor([[1, 2, 3], [2, 4, 6]]) + >>> y = paddle.to_tensor([[4, 5, 6], [4, 5, 6]]) + >>> z = paddle.linalg.vecdot(x, y) + >>> print(z) + Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, + [32, 64]) + + """ + return dot(x, y) + + def cov( x: Tensor, rowvar: bool = True, diff --git a/test/legacy_test/test_dot_op.py b/test/legacy_test/test_dot_op.py index 6cc37dd99cc23..b4ff1f169d76a 100644 --- a/test/legacy_test/test_dot_op.py +++ b/test/legacy_test/test_dot_op.py @@ -138,6 +138,122 @@ def init_dtype(self): self.dtype = np.float64 +class VecDotOp(OpTest): + def setUp(self): + self.op_type = "dot" + self.prim_op_type = "prim" + self.python_api = paddle.vecdot + self.public_python_api = paddle.vecdot + self.init_dtype() + self.init_input_output() + + self.inputs = { + 'X': OpTest.np_dtype_to_base_dtype(self.x), + 'Y': OpTest.np_dtype_to_base_dtype(self.y), + } + self.outputs = {'Out': self.out} + self.attrs = {} + + def test_check_output(self): + self.check_output(check_pir=True) + + def test_check_grad_normal(self): + if self.dtype == np.complex64 or self.dtype == np.complex128: + if core.is_compiled_with_rocm(): + self.check_grad( + ['X', 'Y'], + 'Out', + user_defined_grads=[self.inputs['Y'], self.inputs['X']], + check_pir=True, + ) + else: + self.check_grad(['X', 'Y'], 'Out', check_pir=True) + else: + if core.is_compiled_with_rocm(): + self.check_grad( + ['X', 'Y'], + 'Out', + user_defined_grads=[self.inputs['Y'], self.inputs['X']], + check_pir=True, + ) + else: + self.check_grad( + ['X', 'Y'], 'Out', check_pir=True, check_prim_pir=True + ) + + def test_check_grad_ignore_x(self): + if self.dtype == np.complex64 or self.dtype == np.complex128: + if core.is_compiled_with_rocm(): + self.check_grad( + ['Y'], + 'Out', + no_grad_set=set("X"), + user_defined_grads=[self.inputs['X']], + check_pir=True, + ) + else: + self.check_grad( + ['Y'], 'Out', no_grad_set=set("X"), check_pir=True + ) + else: + if core.is_compiled_with_rocm(): + self.check_grad( + ['Y'], + 'Out', + no_grad_set=set("X"), + user_defined_grads=[self.inputs['X']], + check_pir=True, + ) + else: + self.check_grad( + ['Y'], + 'Out', + no_grad_set=set("X"), + check_pir=True, + check_prim_pir=True, + ) + + def test_check_grad_ignore_y(self): + if self.dtype == np.complex64 or self.dtype == np.complex128: + if core.is_compiled_with_rocm(): + self.check_grad( + ['X'], + 'Out', + no_grad_set=set('Y'), + user_defined_grads=[self.inputs['Y']], + check_pir=True, + ) + else: + self.check_grad( + ['X'], 'Out', no_grad_set=set('Y'), check_pir=True + ) + else: + if core.is_compiled_with_rocm(): + self.check_grad( + ['X'], + 'Out', + no_grad_set=set('Y'), + user_defined_grads=[self.inputs['Y']], + check_pir=True, + ) + else: + self.check_grad( + ['X'], + 'Out', + no_grad_set=set('Y'), + check_pir=True, + check_prim_pir=True, + ) + + def init_input_output(self): + self.x = np.random.uniform(0.1, 1, [121]).astype(self.dtype) + self.y = np.random.uniform(1, 3, [121]).astype(self.dtype) + self.out = np.dot(self.x, self.y).astype(self.dtype) + + def init_dtype(self): + self.dtype = np.float64 + + class DotOpBatch(DotOp): def init_input_output(self): self.x = ( From 74c2c27a0358ebcf3d50ad6c037f54df14249fc8 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 19 Nov 2024 01:25:18 +0800 Subject: [PATCH 02/21] update vecdot --- python/paddle/tensor/linalg.py | 55 ++++++----- test/legacy_test/test_dot_op.py | 116 ---------------------- test/legacy_test/test_linalg_vecdot.py | 130 +++++++++++++++++++++++++ 3 files changed, 159 insertions(+), 142 deletions(-) create mode 100644 test/legacy_test/test_linalg_vecdot.py diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 135a07c42882a..ff58e767205bd 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1867,46 +1867,49 @@ def dot(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: return out -def vecdot(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: +def vecdot( + x: Tensor, + y: Tensor, + axis: int = -1, + name: str | None = None, +) -> Tensor: """ - This operator calculates inner product for vectors. + Computes the dot product of two tensors along a specified axis. - Note: - Support 1-d and 2-d Tensor. When it is 2d, the first dimension of this matrix - is the batch dimension, which means that the vectors of multiple batches are dotted. + This function multiplies two tensors element-wise and sums them along a specified axis to compute their dot product. It supports tensors with a dimensionality of 1-D or 2-D. For 2-D tensors, it treats the first dimension as the batch dimension, allowing the computation of dot products for multiple batch entries simultaneously. Parameters: - x(Tensor): 1-D or 2-D ``Tensor``. Its dtype should be ``float32``, ``float64``, ``int32``, ``int64``, ``complex64``, ``complex128`` - y(Tensor): 1-D or 2-D ``Tensor``. Its dtype should be ``float32``, ``float64``, ``int32``, ``int64``, ``complex64``, ``complex128`` - name(str|None, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name` + x (Tensor): The first input tensor. It should be a 1-D or 2-D tensor with dtype of float32, float64, int32, int64, complex64, or complex128. + y (Tensor): The second input tensor, which must have the same shape and dtype as tensor `x` except possibly at the specified `axis`. + axis (int, optional): The axis along which to compute the dot product. Default is -1, which indicates the last axis. + name (str|None, optional): An optional name for the operation. Default is None. Useful for debugging. Returns: - Tensor: the calculated result Tensor. + Tensor: A tensor containing the dot product of `x` and `y` along the specified axis. Examples: .. code-block:: python >>> import paddle + >>> x = paddle.to_tensor([1, 2, 3], dtype='float32') + >>> y = paddle.to_tensor([4, 5, 6], dtype='float32') + >>> result = paddle.linalg.vecdot(x, y) + >>> print(result) + Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, + [32.0]) - >>> # 1-D Tensor * 1-D Tensor - >>> x = paddle.to_tensor([1, 2, 3]) - >>> y = paddle.to_tensor([4, 5, 6]) - >>> z = paddle.linalg.vecdot(x, y) - >>> print(z) - Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, - 32) - - >>> # 2-D Tensor * 2-D Tensor - >>> x = paddle.to_tensor([[1, 2, 3], [2, 4, 6]]) - >>> y = paddle.to_tensor([[4, 5, 6], [4, 5, 6]]) - >>> z = paddle.linalg.vecdot(x, y) - >>> print(z) - Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True, - [32, 64]) - + >>> x2 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') + >>> y2 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') + >>> result2 = paddle.linalg.vecdot(x2, y2, axis=1) + >>> print(result2) + Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, + [14.0, 77.0]) """ - return dot(x, y) + if x.shape[axis] != y.shape[axis]: + raise ValueError("Size of the specified axis must match for both tensors.") + out = (x.conj() * y).sum(axis=axis) + return out def cov( diff --git a/test/legacy_test/test_dot_op.py b/test/legacy_test/test_dot_op.py index b4ff1f169d76a..6cc37dd99cc23 100644 --- a/test/legacy_test/test_dot_op.py +++ b/test/legacy_test/test_dot_op.py @@ -138,122 +138,6 @@ def init_dtype(self): self.dtype = np.float64 -class VecDotOp(OpTest): - def setUp(self): - self.op_type = "dot" - self.prim_op_type = "prim" - self.python_api = paddle.vecdot - self.public_python_api = paddle.vecdot - self.init_dtype() - self.init_input_output() - - self.inputs = { - 'X': OpTest.np_dtype_to_base_dtype(self.x), - 'Y': OpTest.np_dtype_to_base_dtype(self.y), - } - self.outputs = {'Out': self.out} - self.attrs = {} - - def test_check_output(self): - self.check_output(check_pir=True) - - def test_check_grad_normal(self): - if self.dtype == np.complex64 or self.dtype == np.complex128: - if core.is_compiled_with_rocm(): - self.check_grad( - ['X', 'Y'], - 'Out', - user_defined_grads=[self.inputs['Y'], self.inputs['X']], - check_pir=True, - ) - else: - self.check_grad(['X', 'Y'], 'Out', check_pir=True) - else: - if core.is_compiled_with_rocm(): - self.check_grad( - ['X', 'Y'], - 'Out', - user_defined_grads=[self.inputs['Y'], self.inputs['X']], - check_pir=True, - ) - else: - self.check_grad( - ['X', 'Y'], 'Out', check_pir=True, check_prim_pir=True - ) - - def test_check_grad_ignore_x(self): - if self.dtype == np.complex64 or self.dtype == np.complex128: - if core.is_compiled_with_rocm(): - self.check_grad( - ['Y'], - 'Out', - no_grad_set=set("X"), - user_defined_grads=[self.inputs['X']], - check_pir=True, - ) - else: - self.check_grad( - ['Y'], 'Out', no_grad_set=set("X"), check_pir=True - ) - else: - if core.is_compiled_with_rocm(): - self.check_grad( - ['Y'], - 'Out', - no_grad_set=set("X"), - user_defined_grads=[self.inputs['X']], - check_pir=True, - ) - else: - self.check_grad( - ['Y'], - 'Out', - no_grad_set=set("X"), - check_pir=True, - check_prim_pir=True, - ) - - def test_check_grad_ignore_y(self): - if self.dtype == np.complex64 or self.dtype == np.complex128: - if core.is_compiled_with_rocm(): - self.check_grad( - ['X'], - 'Out', - no_grad_set=set('Y'), - user_defined_grads=[self.inputs['Y']], - check_pir=True, - ) - else: - self.check_grad( - ['X'], 'Out', no_grad_set=set('Y'), check_pir=True - ) - else: - if core.is_compiled_with_rocm(): - self.check_grad( - ['X'], - 'Out', - no_grad_set=set('Y'), - user_defined_grads=[self.inputs['Y']], - check_pir=True, - ) - else: - self.check_grad( - ['X'], - 'Out', - no_grad_set=set('Y'), - check_pir=True, - check_prim_pir=True, - ) - - def init_input_output(self): - self.x = np.random.uniform(0.1, 1, [121]).astype(self.dtype) - self.y = np.random.uniform(1, 3, [121]).astype(self.dtype) - self.out = np.dot(self.x, self.y).astype(self.dtype) - - def init_dtype(self): - self.dtype = np.float64 - - class DotOpBatch(DotOp): def init_input_output(self): self.x = ( diff --git a/test/legacy_test/test_linalg_vecdot.py b/test/legacy_test/test_linalg_vecdot.py new file mode 100644 index 0000000000000..a9ea915d6773e --- /dev/null +++ b/test/legacy_test/test_linalg_vecdot.py @@ -0,0 +1,130 @@ +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. +# +# 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 os +import sys +import unittest + +import numpy as np +import paddle + +if sys.platform == 'win32': + RTOL = {'float32': 1e-02, 'float64': 1e-04} + ATOL = {'float32': 1e-02, 'float64': 1e-04} +else: + RTOL = {'float32': 1e-06, 'float64': 1e-15} + ATOL = {'float32': 1e-06, 'float64': 1e-15} + + +class VecDotTestCase(unittest.TestCase): + def setUp(self): + self.init_config() + self.generate_input() + self.generate_expected_output() + self.places = [paddle.CPUPlace()] + if paddle.is_compiled_with_cuda(): + self.places.append(paddle.CUDAPlace(0)) + + def generate_input(self): + np.random.seed(123) + self.x = np.random.random(self.input_shape).astype(self.dtype) + self.y = np.random.random(self.input_shape).astype(self.dtype) + + def generate_expected_output(self): + self.expected_output = np.sum(self.x * self.y, axis=self.axis) + + def init_config(self): + self.dtype = 'float64' + self.input_shape = (3, 4) + self.axis = -1 + + def test_dygraph(self): + for place in self.places: + paddle.disable_static(place) + x_tensor = paddle.to_tensor(self.x, place=place) + y_tensor = paddle.to_tensor(self.y, place=place) + result = paddle.vecdot(x_tensor, y_tensor, axis=self.axis) + + np.testing.assert_allclose( + result.numpy(), + self.expected_output, + rtol=RTOL[self.dtype], + atol=ATOL[self.dtype], + ) + + def test_static(self): + paddle.enable_static() + for place in self.places: + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + x = paddle.static.data( + name="x", shape=self.input_shape, dtype=self.dtype + ) + y = paddle.static.data( + name="y", shape=self.input_shape, dtype=self.dtype + ) + + result = paddle.vecdot(x, y, axis=self.axis) + exe = paddle.static.Executor(place) + output = exe.run( + feed={"x": self.x, "y": self.y}, + fetch_list=[result], + )[0] + + np.testing.assert_allclose( + output, + self.expected_output, + rtol=RTOL[self.dtype], + atol=ATOL[self.dtype], + ) + + +class VecDotTestCaseFloat32(VecDotTestCase): + def init_config(self): + self.dtype = 'float32' + self.input_shape = (3, 4) + self.axis = -1 + + +class VecDotTestCaseHigherDim(VecDotTestCase): + def init_config(self): + self.dtype = 'float64' + self.input_shape = (2, 3, 4) + self.axis = -1 + + +class VecDotTestCaseAxis(VecDotTestCase): + def init_config(self): + self.dtype = 'float64' + self.input_shape = (3, 4, 5) + self.axis = 1 + + +class VecDotTestCaseError(unittest.TestCase): + def test_axis_mismatch(self): + with self.assertRaises(ValueError): + x = paddle.rand([3, 4], dtype="float32") + y = paddle.rand([3, 5], dtype="float32") + paddle.vecdot(x, y, axis=-1) + + def test_dtype_mismatch(self): + with self.assertRaises(TypeError): + x = paddle.rand([3, 4], dtype="float32") + y = paddle.rand([3, 4], dtype="int32") + paddle.vecdot(x, y, axis=-1) + + +if __name__ == '__main__': + unittest.main() From 78885775e8c49d8e939cab1b52f39f83850e9503 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 19 Nov 2024 01:43:52 +0800 Subject: [PATCH 03/21] fix codestyle --- python/paddle/tensor/linalg.py | 4 +++- test/legacy_test/test_linalg_vecdot.py | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index ff58e767205bd..d873aa53d7d1c 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1907,7 +1907,9 @@ def vecdot( [14.0, 77.0]) """ if x.shape[axis] != y.shape[axis]: - raise ValueError("Size of the specified axis must match for both tensors.") + raise ValueError( + "Size of the specified axis must match for both tensors." + ) out = (x.conj() * y).sum(axis=axis) return out diff --git a/test/legacy_test/test_linalg_vecdot.py b/test/legacy_test/test_linalg_vecdot.py index a9ea915d6773e..52cfefd4f61b0 100644 --- a/test/legacy_test/test_linalg_vecdot.py +++ b/test/legacy_test/test_linalg_vecdot.py @@ -12,11 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os + import sys import unittest import numpy as np + import paddle if sys.platform == 'win32': From 7b1e0ddbe8d059b4559717a452931e4e4be2efa2 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 19 Nov 2024 10:00:36 +0800 Subject: [PATCH 04/21] fix codestyle --- python/paddle/linalg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/linalg.py b/python/paddle/linalg.py index 2a19b79ce9aba..76b0c3604e919 100644 --- a/python/paddle/linalg.py +++ b/python/paddle/linalg.py @@ -46,8 +46,8 @@ svd, svd_lowrank, triangular_solve, - vector_norm, vecdot, + vector_norm, ) __all__ = [ @@ -55,8 +55,8 @@ 'cholesky_inverse', 'norm', 'matrix_norm', - 'vector_norm', 'vecdot', + 'vector_norm', 'cond', 'cov', 'corrcoef', From ebd6c023c5c6894b66323c3bceb06bfe6784e652 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 19 Nov 2024 14:03:03 +0800 Subject: [PATCH 05/21] Update test/legacy_test/test_linalg_vecdot.py Co-authored-by: HydrogenSulfate <490868991@qq.com> --- test/legacy_test/test_linalg_vecdot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/legacy_test/test_linalg_vecdot.py b/test/legacy_test/test_linalg_vecdot.py index 52cfefd4f61b0..536095199d8bd 100644 --- a/test/legacy_test/test_linalg_vecdot.py +++ b/test/legacy_test/test_linalg_vecdot.py @@ -53,8 +53,8 @@ def init_config(self): def test_dygraph(self): for place in self.places: paddle.disable_static(place) - x_tensor = paddle.to_tensor(self.x, place=place) - y_tensor = paddle.to_tensor(self.y, place=place) + x_tensor = paddle.to_tensor(self.x, dtype=self.dtype, place=place) + y_tensor = paddle.to_tensor(self.y, dtype=self.dtype, place=place) result = paddle.vecdot(x_tensor, y_tensor, axis=self.axis) np.testing.assert_allclose( From 41938101a05cf0e8270ab5f793997f4eaefaf24c Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 19 Nov 2024 14:03:15 +0800 Subject: [PATCH 06/21] Update python/paddle/tensor/linalg.py Co-authored-by: HydrogenSulfate <490868991@qq.com> --- python/paddle/tensor/linalg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index d873aa53d7d1c..9240596b7b7af 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1878,7 +1878,7 @@ def vecdot( This function multiplies two tensors element-wise and sums them along a specified axis to compute their dot product. It supports tensors with a dimensionality of 1-D or 2-D. For 2-D tensors, it treats the first dimension as the batch dimension, allowing the computation of dot products for multiple batch entries simultaneously. - Parameters: + Args: x (Tensor): The first input tensor. It should be a 1-D or 2-D tensor with dtype of float32, float64, int32, int64, complex64, or complex128. y (Tensor): The second input tensor, which must have the same shape and dtype as tensor `x` except possibly at the specified `axis`. axis (int, optional): The axis along which to compute the dot product. Default is -1, which indicates the last axis. From abffd22d6be2460edc90b508af22985a1e71bcf1 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 19 Nov 2024 14:46:55 +0800 Subject: [PATCH 07/21] update_vecdot --- python/paddle/tensor/linalg.py | 16 ++-- test/legacy_test/test_linalg_vecdot.py | 106 +++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 6 deletions(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 9240596b7b7af..122f2e4545519 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1876,13 +1876,13 @@ def vecdot( """ Computes the dot product of two tensors along a specified axis. - This function multiplies two tensors element-wise and sums them along a specified axis to compute their dot product. It supports tensors with a dimensionality of 1-D or 2-D. For 2-D tensors, it treats the first dimension as the batch dimension, allowing the computation of dot products for multiple batch entries simultaneously. + This function multiplies two tensors element-wise and sums them along a specified axis to compute their dot product. It supports tensors of any dimensionality, including 0-D tensors, as long as the shapes of `x` and `y` are broadcastable along the specified axis. Args: - x (Tensor): The first input tensor. It should be a 1-D or 2-D tensor with dtype of float32, float64, int32, int64, complex64, or complex128. - y (Tensor): The second input tensor, which must have the same shape and dtype as tensor `x` except possibly at the specified `axis`. + x (Tensor): The first input tensor. It should be a tensor with dtype of float32, float64, int32, int64, complex64, or complex128. + y (Tensor): The second input tensor. Its shape must be broadcastable with `x` along the specified `axis`, and it must have the same dtype as `x`. axis (int, optional): The axis along which to compute the dot product. Default is -1, which indicates the last axis. - name (str|None, optional): An optional name for the operation. Default is None. Useful for debugging. + name (str|None, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name` Returns: Tensor: A tensor containing the dot product of `x` and `y` along the specified axis. @@ -1906,10 +1906,14 @@ def vecdot( Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, [14.0, 77.0]) """ - if x.shape[axis] != y.shape[axis]: + try: + broadcast_shape = paddle.broadcast_shape(x.shape, y.shape) + except ValueError: raise ValueError( - "Size of the specified axis must match for both tensors." + f"Shapes {x.shape} and {y.shape} are not broadcastable." ) + + x, y = paddle.broadcast_to(x, broadcast_shape), paddle.broadcast_to(y, broadcast_shape) out = (x.conj() * y).sum(axis=axis) return out diff --git a/test/legacy_test/test_linalg_vecdot.py b/test/legacy_test/test_linalg_vecdot.py index 536095199d8bd..29c9c80fe4efc 100644 --- a/test/legacy_test/test_linalg_vecdot.py +++ b/test/legacy_test/test_linalg_vecdot.py @@ -127,5 +127,111 @@ def test_dtype_mismatch(self): paddle.vecdot(x, y, axis=-1) +class VecDotTestCaseComplex(unittest.TestCase): + def run_test_dynamic(self): + paddle.disable_static() + x = paddle.to_tensor([[1 + 2j, 3 + 4j], [5 + 6j, 7 + 8j]], dtype="complex64") + y = paddle.to_tensor([[9 + 1j, 8 + 2j], [7 + 3j, 6 + 4j]], dtype="complex64") + result = paddle.vecdot(x, y, axis=-1) + expected = np.sum((x.numpy().conj() * y.numpy()), axis=-1) + np.testing.assert_allclose(result.numpy(), expected, rtol=1e-5, atol=1e-5) + + def run_test_static(self): + paddle.enable_static() + place = paddle.CPUPlace() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data( + name="x", shape=[2, 2], dtype="complex64" + ) + y = paddle.static.data( + name="y", shape=[2, 2], dtype="complex64" + ) + result = paddle.vecdot(x, y, axis=-1) + exe = paddle.static.Executor(place) + output = exe.run( + feed={ + "x": np.array([[1 + 2j, 3 + 4j], [5 + 6j, 7 + 8j]]).astype("complex64"), + "y": np.array([[9 + 1j, 8 + 2j], [7 + 3j, 6 + 4j]]).astype("complex64"), + }, + fetch_list=[result], + )[0] + expected = np.sum( + np.conj(np.array([[1 + 2j, 3 + 4j], [5 + 6j, 7 + 8j]])).astype("complex64") + * np.array([[9 + 1j, 8 + 2j], [7 + 3j, 6 + 4j]]).astype("complex64"), + axis=-1, + ) + np.testing.assert_allclose(output, expected, rtol=1e-5, atol=1e-5) + + def test_complex_conjugate(self): + self.run_test_dynamic() + self.run_test_static() + + +class VecDotTestCaseTypePromotion1(unittest.TestCase): + def test_float32_float64_promotion(self): + paddle.disable_static() + x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0]], dtype="float32") + y = paddle.to_tensor([[5.0, 6.0], [7.0, 8.0]], dtype="float64") + result = paddle.vecdot(x, y, axis=-1) + + expected = np.sum(x.numpy().astype("float64") * y.numpy(), axis=-1) + np.testing.assert_allclose(result.numpy(), expected, rtol=1e-6, atol=1e-6) + + +class VecDotTestCaseTypePromotion2(unittest.TestCase): + def test_float64_complex64_promotion(self): + paddle.disable_static() + x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0]], dtype="float64") + y = paddle.to_tensor([[5 + 6j, 7 + 8j], [9 + 1j, 2 + 3j]], dtype="complex64") + result = paddle.vecdot(x, y, axis=-1) + + expected = np.sum(x.numpy().astype("complex64") * y.numpy(), axis=-1) + np.testing.assert_allclose(result.numpy(), expected, rtol=1e-5, atol=1e-5) + + +class VecDotTestCaseBroadcast0DTensor(unittest.TestCase): + def test_0d_tensor_broadcast(self): + paddle.disable_static() + x = paddle.to_tensor(2.0, dtype="float32") + y = paddle.to_tensor(3.0, dtype="float32") + result = paddle.vecdot(x, y) + + expected = x.numpy() * y.numpy() + np.testing.assert_allclose(result.numpy(), expected, rtol=1e-6, atol=1e-6) + + +class VecDotTestCaseBroadcast1DTensor(unittest.TestCase): + def test_1d_tensor_broadcast(self): + paddle.disable_static() + x = paddle.to_tensor([1.0, 2.0, 3.0], dtype="float32") + y = paddle.to_tensor([4.0, 5.0, 6.0], dtype="float32") + result = paddle.vecdot(x, y) + + expected = np.dot(x.numpy(), y.numpy()) + np.testing.assert_allclose(result.numpy(), expected, rtol=1e-6, atol=1e-6) + + +class VecDotTestCaseBroadcast1DNDTensor(unittest.TestCase): + def test_1d_nd_tensor_broadcast(self): + paddle.disable_static() + x = paddle.to_tensor([1.0, 2.0], dtype="float32") + y = paddle.to_tensor([[3.0, 4.0], [5.0, 6.0]], dtype="float32") + result = paddle.vecdot(x, y, axis=-1) + + expected = np.sum(x.numpy() * y.numpy(), axis=-1) + np.testing.assert_allclose(result.numpy(), expected, rtol=1e-6, atol=1e-6) + + +class VecDotTestCaseBroadcastNDTensor(unittest.TestCase): + def test_nd_nd_tensor_broadcast(self): + paddle.disable_static() + x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0]], dtype="float32") + y = paddle.to_tensor([5.0, 6.0], dtype="float32") # 1-D broadcast to 2-D + result = paddle.vecdot(x, y, axis=-1) + + expected = np.sum(x.numpy() * y.numpy(), axis=-1) + np.testing.assert_allclose(result.numpy(), expected, rtol=1e-6, atol=1e-6) + + if __name__ == '__main__': unittest.main() From ef6edd28fa6d416b2fa18ff71afd0c6dd7c26909 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 19 Nov 2024 15:51:42 +0800 Subject: [PATCH 08/21] fix_codestyle --- test/legacy_test/test_linalg_vecdot.py | 32 +++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/test/legacy_test/test_linalg_vecdot.py b/test/legacy_test/test_linalg_vecdot.py index 29c9c80fe4efc..61dcda32fb690 100644 --- a/test/legacy_test/test_linalg_vecdot.py +++ b/test/legacy_test/test_linalg_vecdot.py @@ -134,7 +134,9 @@ def run_test_dynamic(self): y = paddle.to_tensor([[9 + 1j, 8 + 2j], [7 + 3j, 6 + 4j]], dtype="complex64") result = paddle.vecdot(x, y, axis=-1) expected = np.sum((x.numpy().conj() * y.numpy()), axis=-1) - np.testing.assert_allclose(result.numpy(), expected, rtol=1e-5, atol=1e-5) + np.testing.assert_allclose( + result.numpy(), expected, rtol=1e-5, atol=1e-5 + ) def run_test_static(self): paddle.enable_static() @@ -175,7 +177,9 @@ def test_float32_float64_promotion(self): result = paddle.vecdot(x, y, axis=-1) expected = np.sum(x.numpy().astype("float64") * y.numpy(), axis=-1) - np.testing.assert_allclose(result.numpy(), expected, rtol=1e-6, atol=1e-6) + np.testing.assert_allclose( + result.numpy(), expected, rtol=1e-6, atol=1e-6 + ) class VecDotTestCaseTypePromotion2(unittest.TestCase): @@ -186,7 +190,9 @@ def test_float64_complex64_promotion(self): result = paddle.vecdot(x, y, axis=-1) expected = np.sum(x.numpy().astype("complex64") * y.numpy(), axis=-1) - np.testing.assert_allclose(result.numpy(), expected, rtol=1e-5, atol=1e-5) + np.testing.assert_allclose( + result.numpy(), expected, rtol=1e-5, atol=1e-5 + ) class VecDotTestCaseBroadcast0DTensor(unittest.TestCase): @@ -197,7 +203,9 @@ def test_0d_tensor_broadcast(self): result = paddle.vecdot(x, y) expected = x.numpy() * y.numpy() - np.testing.assert_allclose(result.numpy(), expected, rtol=1e-6, atol=1e-6) + np.testing.assert_allclose( + result.numpy(), expected, rtol=1e-6, atol=1e-6 + ) class VecDotTestCaseBroadcast1DTensor(unittest.TestCase): @@ -208,18 +216,24 @@ def test_1d_tensor_broadcast(self): result = paddle.vecdot(x, y) expected = np.dot(x.numpy(), y.numpy()) - np.testing.assert_allclose(result.numpy(), expected, rtol=1e-6, atol=1e-6) + np.testing.assert_allclose( + result.numpy(), expected, rtol=1e-6, atol=1e-6 + ) class VecDotTestCaseBroadcast1DNDTensor(unittest.TestCase): def test_1d_nd_tensor_broadcast(self): paddle.disable_static() x = paddle.to_tensor([1.0, 2.0], dtype="float32") - y = paddle.to_tensor([[3.0, 4.0], [5.0, 6.0]], dtype="float32") + y = paddle.to_tensor( + [[3.0, 4.0], [5.0, 6.0]], dtype="float32" + ) result = paddle.vecdot(x, y, axis=-1) expected = np.sum(x.numpy() * y.numpy(), axis=-1) - np.testing.assert_allclose(result.numpy(), expected, rtol=1e-6, atol=1e-6) + np.testing.assert_allclose( + result.numpy(), expected, rtol=1e-6, atol=1e-6 + ) class VecDotTestCaseBroadcastNDTensor(unittest.TestCase): @@ -230,7 +244,9 @@ def test_nd_nd_tensor_broadcast(self): result = paddle.vecdot(x, y, axis=-1) expected = np.sum(x.numpy() * y.numpy(), axis=-1) - np.testing.assert_allclose(result.numpy(), expected, rtol=1e-6, atol=1e-6) + np.testing.assert_allclose( + result.numpy(), expected, rtol=1e-6, atol=1e-6 + ) if __name__ == '__main__': From 269668230fc4dc7dfed0b37287341045d7b480b8 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 19 Nov 2024 16:09:08 +0800 Subject: [PATCH 09/21] fix codestyle --- test/legacy_test/test_linalg_vecdot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/legacy_test/test_linalg_vecdot.py b/test/legacy_test/test_linalg_vecdot.py index 61dcda32fb690..b8538494bc7b9 100644 --- a/test/legacy_test/test_linalg_vecdot.py +++ b/test/legacy_test/test_linalg_vecdot.py @@ -240,7 +240,7 @@ class VecDotTestCaseBroadcastNDTensor(unittest.TestCase): def test_nd_nd_tensor_broadcast(self): paddle.disable_static() x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0]], dtype="float32") - y = paddle.to_tensor([5.0, 6.0], dtype="float32") # 1-D broadcast to 2-D + y = paddle.to_tensor([5.0, 6.0], dtype="float32") result = paddle.vecdot(x, y, axis=-1) expected = np.sum(x.numpy() * y.numpy(), axis=-1) From fe9ec10f6ea5e5c7424a3096caaa32193d2fcb7b Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 19 Nov 2024 21:11:52 +0800 Subject: [PATCH 10/21] fix codestyle --- python/paddle/tensor/linalg.py | 4 ++- test/legacy_test/test_linalg_vecdot.py | 40 +++++++++++++++----------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 122f2e4545519..fa82f451f828e 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1913,7 +1913,9 @@ def vecdot( f"Shapes {x.shape} and {y.shape} are not broadcastable." ) - x, y = paddle.broadcast_to(x, broadcast_shape), paddle.broadcast_to(y, broadcast_shape) + x, y = paddle.broadcast_to(x, broadcast_shape), paddle.broadcast_to( + y, broadcast_shape + ) out = (x.conj() * y).sum(axis=axis) return out diff --git a/test/legacy_test/test_linalg_vecdot.py b/test/legacy_test/test_linalg_vecdot.py index b8538494bc7b9..7bb10fc9ec19d 100644 --- a/test/legacy_test/test_linalg_vecdot.py +++ b/test/legacy_test/test_linalg_vecdot.py @@ -130,8 +130,12 @@ def test_dtype_mismatch(self): class VecDotTestCaseComplex(unittest.TestCase): def run_test_dynamic(self): paddle.disable_static() - x = paddle.to_tensor([[1 + 2j, 3 + 4j], [5 + 6j, 7 + 8j]], dtype="complex64") - y = paddle.to_tensor([[9 + 1j, 8 + 2j], [7 + 3j, 6 + 4j]], dtype="complex64") + x = paddle.to_tensor( + [[1 + 2j, 3 + 4j], [5 + 6j, 7 + 8j]], dtype="complex64" + ) + y = paddle.to_tensor( + [[9 + 1j, 8 + 2j], [7 + 3j, 6 + 4j]], dtype="complex64" + ) result = paddle.vecdot(x, y, axis=-1) expected = np.sum((x.numpy().conj() * y.numpy()), axis=-1) np.testing.assert_allclose( @@ -142,24 +146,28 @@ def run_test_static(self): paddle.enable_static() place = paddle.CPUPlace() with paddle.static.program_guard(paddle.static.Program()): - x = paddle.static.data( - name="x", shape=[2, 2], dtype="complex64" - ) - y = paddle.static.data( - name="y", shape=[2, 2], dtype="complex64" - ) + x = paddle.static.data(name="x", shape=[2, 2], dtype="complex64") + y = paddle.static.data(name="y", shape=[2, 2], dtype="complex64") result = paddle.vecdot(x, y, axis=-1) exe = paddle.static.Executor(place) output = exe.run( feed={ - "x": np.array([[1 + 2j, 3 + 4j], [5 + 6j, 7 + 8j]]).astype("complex64"), - "y": np.array([[9 + 1j, 8 + 2j], [7 + 3j, 6 + 4j]]).astype("complex64"), + "x": np.array([[1 + 2j, 3 + 4j], [5 + 6j, 7 + 8j]]).astype( + "complex64" + ), + "y": np.array([[9 + 1j, 8 + 2j], [7 + 3j, 6 + 4j]]).astype( + "complex64" + ), }, fetch_list=[result], )[0] expected = np.sum( - np.conj(np.array([[1 + 2j, 3 + 4j], [5 + 6j, 7 + 8j]])).astype("complex64") - * np.array([[9 + 1j, 8 + 2j], [7 + 3j, 6 + 4j]]).astype("complex64"), + np.conj(np.array([[1 + 2j, 3 + 4j], [5 + 6j, 7 + 8j]])).astype( + "complex64" + ) + * np.array([[9 + 1j, 8 + 2j], [7 + 3j, 6 + 4j]]).astype( + "complex64" + ), axis=-1, ) np.testing.assert_allclose(output, expected, rtol=1e-5, atol=1e-5) @@ -186,7 +194,9 @@ class VecDotTestCaseTypePromotion2(unittest.TestCase): def test_float64_complex64_promotion(self): paddle.disable_static() x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0]], dtype="float64") - y = paddle.to_tensor([[5 + 6j, 7 + 8j], [9 + 1j, 2 + 3j]], dtype="complex64") + y = paddle.to_tensor( + [[5 + 6j, 7 + 8j], [9 + 1j, 2 + 3j]], dtype="complex64" + ) result = paddle.vecdot(x, y, axis=-1) expected = np.sum(x.numpy().astype("complex64") * y.numpy(), axis=-1) @@ -225,9 +235,7 @@ class VecDotTestCaseBroadcast1DNDTensor(unittest.TestCase): def test_1d_nd_tensor_broadcast(self): paddle.disable_static() x = paddle.to_tensor([1.0, 2.0], dtype="float32") - y = paddle.to_tensor( - [[3.0, 4.0], [5.0, 6.0]], dtype="float32" - ) + y = paddle.to_tensor([[3.0, 4.0], [5.0, 6.0]], dtype="float32") result = paddle.vecdot(x, y, axis=-1) expected = np.sum(x.numpy() * y.numpy(), axis=-1) From 9e1be507ada7785d393785929e7b1c19334f9801 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Wed, 20 Nov 2024 16:53:44 +0800 Subject: [PATCH 11/21] update --- python/paddle/tensor/linalg.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index fa82f451f828e..54f02b361b57e 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1906,16 +1906,6 @@ def vecdot( Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, [14.0, 77.0]) """ - try: - broadcast_shape = paddle.broadcast_shape(x.shape, y.shape) - except ValueError: - raise ValueError( - f"Shapes {x.shape} and {y.shape} are not broadcastable." - ) - - x, y = paddle.broadcast_to(x, broadcast_shape), paddle.broadcast_to( - y, broadcast_shape - ) out = (x.conj() * y).sum(axis=axis) return out From 12c2e11a056d88e84e53381ce0b8f48ace557c78 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Thu, 21 Nov 2024 17:56:47 +0800 Subject: [PATCH 12/21] skip_xpu --- test/legacy_test/test_linalg_vecdot.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/test/legacy_test/test_linalg_vecdot.py b/test/legacy_test/test_linalg_vecdot.py index 7bb10fc9ec19d..6d336177e3f71 100644 --- a/test/legacy_test/test_linalg_vecdot.py +++ b/test/legacy_test/test_linalg_vecdot.py @@ -120,13 +120,20 @@ def test_axis_mismatch(self): y = paddle.rand([3, 5], dtype="float32") paddle.vecdot(x, y, axis=-1) + @unittest.skipIf( + core.is_compiled_with_xpu(), + "Skip XPU for not support uniform(dtype=int)", + ) def test_dtype_mismatch(self): with self.assertRaises(TypeError): x = paddle.rand([3, 4], dtype="float32") y = paddle.rand([3, 4], dtype="int32") paddle.vecdot(x, y, axis=-1) - +@unittest.skipIf( + core.is_compiled_with_xpu(), + "Skip XPU for not support uniform(dtype=int)", +) class VecDotTestCaseComplex(unittest.TestCase): def run_test_dynamic(self): paddle.disable_static() @@ -176,7 +183,10 @@ def test_complex_conjugate(self): self.run_test_dynamic() self.run_test_static() - +@unittest.skipIf( + core.is_compiled_with_xpu(), + "Skip XPU for not support uniform(dtype=int)", +) class VecDotTestCaseTypePromotion1(unittest.TestCase): def test_float32_float64_promotion(self): paddle.disable_static() From 31f669fb79a2078d0589a1bf01861a056cebc0c4 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Thu, 21 Nov 2024 19:01:49 +0800 Subject: [PATCH 13/21] Update test_linalg_vecdot.py --- test/legacy_test/test_linalg_vecdot.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/legacy_test/test_linalg_vecdot.py b/test/legacy_test/test_linalg_vecdot.py index 6d336177e3f71..eec74a9196235 100644 --- a/test/legacy_test/test_linalg_vecdot.py +++ b/test/legacy_test/test_linalg_vecdot.py @@ -130,6 +130,7 @@ def test_dtype_mismatch(self): y = paddle.rand([3, 4], dtype="int32") paddle.vecdot(x, y, axis=-1) + @unittest.skipIf( core.is_compiled_with_xpu(), "Skip XPU for not support uniform(dtype=int)", @@ -183,6 +184,7 @@ def test_complex_conjugate(self): self.run_test_dynamic() self.run_test_static() + @unittest.skipIf( core.is_compiled_with_xpu(), "Skip XPU for not support uniform(dtype=int)", From 0c014c7c4f4c6fad81faa9afb46755eecfd8f127 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Thu, 21 Nov 2024 19:20:08 +0800 Subject: [PATCH 14/21] fix codestyle --- test/legacy_test/test_linalg_vecdot.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/legacy_test/test_linalg_vecdot.py b/test/legacy_test/test_linalg_vecdot.py index eec74a9196235..545021373beaa 100644 --- a/test/legacy_test/test_linalg_vecdot.py +++ b/test/legacy_test/test_linalg_vecdot.py @@ -19,6 +19,8 @@ import numpy as np import paddle +from paddle.base import core + if sys.platform == 'win32': RTOL = {'float32': 1e-02, 'float64': 1e-04} From 1a63df0883b482e097dde426629e52b621a1d66d Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Thu, 21 Nov 2024 19:29:26 +0800 Subject: [PATCH 15/21] fix codestyle again --- test/legacy_test/test_linalg_vecdot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/legacy_test/test_linalg_vecdot.py b/test/legacy_test/test_linalg_vecdot.py index 545021373beaa..00819e1e7eac5 100644 --- a/test/legacy_test/test_linalg_vecdot.py +++ b/test/legacy_test/test_linalg_vecdot.py @@ -21,7 +21,6 @@ import paddle from paddle.base import core - if sys.platform == 'win32': RTOL = {'float32': 1e-02, 'float64': 1e-04} ATOL = {'float32': 1e-02, 'float64': 1e-04} From a970f6561a316c711f910f2f6457e0505fd2dc11 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Fri, 22 Nov 2024 13:50:19 +0800 Subject: [PATCH 16/21] fix --- python/paddle/tensor/linalg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index a24f8f7297d28..02b493ba5787a 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1897,14 +1897,14 @@ def vecdot( >>> result = paddle.linalg.vecdot(x, y) >>> print(result) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, - [32.0]) + [32.0]) >>> x2 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') >>> y2 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') >>> result2 = paddle.linalg.vecdot(x2, y2, axis=1) >>> print(result2) Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, - [14.0, 77.0]) + [14.0, 77.0]) """ out = (x.conj() * y).sum(axis=axis) return out From 77744cbf4fb92e294455534a31c02fe97ccf7e54 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Fri, 22 Nov 2024 18:46:57 +0800 Subject: [PATCH 17/21] fix --- test/legacy_test/test_linalg_vecdot.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/legacy_test/test_linalg_vecdot.py b/test/legacy_test/test_linalg_vecdot.py index 00819e1e7eac5..e42755b8d8a8d 100644 --- a/test/legacy_test/test_linalg_vecdot.py +++ b/test/legacy_test/test_linalg_vecdot.py @@ -203,6 +203,10 @@ def test_float32_float64_promotion(self): ) +@unittest.skipIf( + core.is_compiled_with_xpu(), + "Skip XPU for not support uniform(dtype=int)", +) class VecDotTestCaseTypePromotion2(unittest.TestCase): def test_float64_complex64_promotion(self): paddle.disable_static() From 06793f5f40d77ffd582a0c5e790440daf86dff7c Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Mon, 25 Nov 2024 22:23:53 +0800 Subject: [PATCH 18/21] fix --- python/paddle/tensor/linalg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 13f693d87c93e..6e7bd94269a87 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1898,7 +1898,7 @@ def vecdot( >>> result = paddle.linalg.vecdot(x, y) >>> print(result) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, - [32.0]) + 32) >>> x2 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') >>> y2 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') From 0d650790df6f41a9310b3a7577c2aad0cfa547ec Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 26 Nov 2024 14:07:14 +0800 Subject: [PATCH 19/21] fix again --- python/paddle/tensor/linalg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 6e7bd94269a87..3eeac8651b27f 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1898,7 +1898,7 @@ def vecdot( >>> result = paddle.linalg.vecdot(x, y) >>> print(result) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, - 32) + 32.) >>> x2 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') >>> y2 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') From 9e90b9223c99bbfaca076af5d8af472c8d997025 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 26 Nov 2024 15:04:27 +0800 Subject: [PATCH 20/21] change_example --- python/paddle/tensor/linalg.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 3eeac8651b27f..07883f6007e72 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1893,12 +1893,12 @@ def vecdot( .. code-block:: python >>> import paddle - >>> x = paddle.to_tensor([1, 2, 3], dtype='float32') - >>> y = paddle.to_tensor([4, 5, 6], dtype='float32') - >>> result = paddle.linalg.vecdot(x, y) + >>> x = paddle.to_tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype='float32') + >>> y = paddle.to_tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype='float32') + >>> result = paddle.linalg.vecdot(x, y, axis=2) >>> print(result) - Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, - 32.) + Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 5.0, 25.0], [61.0, 113.0]]) >>> x2 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') >>> y2 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') From 0690cd1dca6b0dd14efba0dc0e2fd31a532a8252 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 26 Nov 2024 16:55:11 +0800 Subject: [PATCH 21/21] delete --- python/paddle/tensor/linalg.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 07883f6007e72..e55ffa4eb48b1 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -1893,17 +1893,10 @@ def vecdot( .. code-block:: python >>> import paddle - >>> x = paddle.to_tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype='float32') - >>> y = paddle.to_tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype='float32') - >>> result = paddle.linalg.vecdot(x, y, axis=2) + >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') + >>> y = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') + >>> result = paddle.linalg.vecdot(x, y, axis=1) >>> print(result) - Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, - [[ 5.0, 25.0], [61.0, 113.0]]) - - >>> x2 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') - >>> y2 = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') - >>> result2 = paddle.linalg.vecdot(x2, y2, axis=1) - >>> print(result2) Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, [14.0, 77.0]) """