From 3e19c90a611f1401e45ec6a4ddac3470d171dda7 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Tue, 3 Oct 2023 22:52:06 +0800 Subject: [PATCH 01/19] add pdist api --- python/paddle/__init__.py | 2 + python/paddle/tensor/__init__.py | 2 + python/paddle/tensor/linalg.py | 52 ++++++++++- test/legacy_test/test_pdist.py | 146 +++++++++++++++++++++++++++++++ 4 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 test/legacy_test/test_pdist.py diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index e73b9ae0cc309d..45715e8a43eb8e 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -146,6 +146,7 @@ t, t_, cdist, + pdist, cross, cholesky, bmm, @@ -689,6 +690,7 @@ 'sin_', 'dist', 'cdist', + 'pdist', 'unbind', 'meshgrid', 'arange', diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index b728392b0452de..9816a4fdbef60b 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -78,6 +78,7 @@ from .linalg import lu # noqa: F401 from .linalg import lu_unpack # noqa: F401 from .linalg import cdist # noqa: F401 +from .linalg import pdist # noqa: F401 from .logic import equal # noqa: F401 from .logic import equal_ # noqa: F401 from .logic import greater_equal # noqa: F401 @@ -634,6 +635,7 @@ 'lu', 'lu_unpack', 'cdist', + 'pdist', 'as_complex', 'as_real', 'rad2deg', diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index acc59d6385e570..9c1a7a2e47b5ee 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -22,7 +22,8 @@ from ..base.data_feeder import check_dtype, check_type, check_variable_and_dtype from ..common_ops_import import Variable from ..framework import LayerHelper, in_dynamic_mode, in_dynamic_or_pir_mode -from .creation import full +from .creation import full, tril +from .search import masked_select from .manipulation import cast from .math import _get_reduce_axis @@ -3730,3 +3731,52 @@ def cdist( return paddle.linalg.norm( x[..., None, :] - y[..., None, :, :], p=p, axis=-1 ) + + +def pdist( + x, p=2.0, compute_mode="use_mm_for_euclid_dist_if_necessary", name=None +): + r''' + Computes the p-norm distance between every pair of row vectors in the input. + + Args: + x (Tensor): A tensor with shape :math:`N \times M`. + p (float, optional): The value for the p-norm distance to calculate between each vector pair. Default: :math:`2.0`. + compute_mode (str, optional): The mode for compute distance. + + - ``use_mm_for_euclid_dist_if_necessary`` , for p = 2.0 and (P > 25 or R > 25), it will use matrix multiplication to calculate euclid distance if possible. + - ``use_mm_for_euclid_dist`` , for p = 2.0, it will use matrix multiplication to calculate euclid distance. + - ``donot_use_mm_for_euclid_dist`` , it will not use matrix multiplication to calculate euclid distance. + + Default: ``use_mm_for_euclid_dist_if_necessary``. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + + Returns: + Tensor with shape: math:`N(N-1)/2` the dtype is same as input tensor. + + Examples: + .. code-block:: python + + >>> import paddle + >>> a = paddle.randn([4, 5]) + >>> a + Tensor(shape=[4, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[-0.33173719, -0.93648648, -0.01741328, -0.94435263, 2.22178721], + [-0.65466857, 0.10307083, 0.08741203, -0.91078597, 0.72589827], + [ 0.06907391, -0.27584535, 1.35355449, -0.69688839, 0.18408430], + [-0.00939178, -0.32901841, -1.06503606, 0.81856263, 0.16791444]]) + >>> pdist_out=paddle.pdist(a) + >>> pdist_out + Tensor(shape=[6], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [1.85331142, 2.58652687, 2.98273396, 1.61549115, 2.28762150, 2.85576940]) + + ''' + + x_shape = list(x.shape) + assert len(x_shape) == 2, ( + "The x must be 2-dimensional" + ) + d = cdist(x, x, p, compute_mode) + mask = ~paddle.tril(paddle.ones(d.shape, dtype='bool')) + return masked_select(d, mask) + diff --git a/test/legacy_test/test_pdist.py b/test/legacy_test/test_pdist.py new file mode 100644 index 00000000000000..7e876459970b30 --- /dev/null +++ b/test/legacy_test/test_pdist.py @@ -0,0 +1,146 @@ +# Copyright (c) 2023 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 unittest +import numpy as np +import paddle + +def ref_pdist(x, p=2.0): + dist = np.linalg.norm(x[..., None, :] - x[None, :, :], ord=p, axis=-1) + res = [] + rows, cols = dist.shape + for i in range(rows): + for j in range(cols): + if i >= j: continue + res.append(dist[i][j]) + return np.array(res) + + +class TestpdistAPI(unittest.TestCase): + def setUp(self): + self.x = np.random.rand(10, 20).astype('float32') + self.p = 2.0 + self.compute_mode = "use_mm_for_euclid_dist_if_necessary" + self.init_input() + self.place = ( + paddle.CUDAPlace(0) + if paddle.is_compiled_with_cuda() + else paddle.CPUPlace() + ) + + def init_input(self): + pass + + def test_static_api(self): + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) + out = paddle.pdist(x, self.p, self.compute_mode) + exe = paddle.static.Executor(self.place) + res = exe.run(feed={'x': self.x}, fetch_list=[out]) + out_ref = ref_pdist(self.x, self.p) + np.testing.assert_allclose(out_ref, res[0], rtol=1e-5, atol=1e-5) + + def test_dygraph_api(self): + paddle.disable_static(self.place) + x = paddle.to_tensor(self.x) + out = paddle.pdist(x, self.p, self.compute_mode) + out_ref = ref_pdist(self.x, self.p) + np.testing.assert_allclose(out_ref, out.numpy(), rtol=1e-5, atol=1e-5) + paddle.enable_static() + + +class TestpdistAPICase1(TestpdistAPI): + def init_input(self): + self.p = 0 + + +class TestpdistAPICase2(TestpdistAPI): + def init_input(self): + self.p = 1.0 + + +class TestpdistAPICase3(TestpdistAPI): + def init_input(self): + self.p = 3.0 + + +class TestpdistAPICase4(TestpdistAPI): + def init_input(self): + self.p = 1.5 + + +class TestpdistAPICase5(TestpdistAPI): + def init_input(self): + self.p = 2.5 + + +class TestpdistAPICase6(TestpdistAPI): + def init_input(self): + self.p = float('inf') + + +class TestpdistAPICase7(TestpdistAPI): + def init_input(self): + self.x = np.random.rand(50, 20).astype('float64') + self.compute_mode = "use_mm_for_euclid_dist" + + +class TestpdistAPICase8(TestpdistAPI): + def init_input(self): + self.x = np.random.rand(50, 20).astype('float64') + self.compute_mode = "donot_use_mm_for_euclid_dist" + + +class TestpdistAPICase9(TestpdistAPI): + def init_input(self): + self.x = np.random.rand(500, 100).astype('float64') + + + def test_static_api(self): + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) + out0 = paddle.pdist(x, self.p, self.compute_mode) + out1 = paddle.pdist(x, self.p, "donot_use_mm_for_euclid_dist") + out2 = paddle.pdist(x, self.p, "use_mm_for_euclid_dist") + exe = paddle.static.Executor(self.place) + res = exe.run( + feed={'x': self.x}, fetch_list=[out0, out1, out2] + ) + out_ref = ref_pdist(self.x, self.p) + np.testing.assert_allclose(out_ref, res[0]) + np.testing.assert_allclose(out_ref, res[1]) + np.testing.assert_allclose(out_ref, res[2]) + + def test_dygraph_api(self): + paddle.disable_static(self.place) + x = paddle.to_tensor(self.x) + out0 = paddle.pdist(x, self.p, self.compute_mode) + out1 = paddle.pdist(x, self.p, "donot_use_mm_for_euclid_dist") + out2 = paddle.pdist(x, self.p, "use_mm_for_euclid_dist") + out_ref = ref_pdist(self.x, self.p) + np.testing.assert_allclose(out_ref, out0.numpy()) + np.testing.assert_allclose(out_ref, out1.numpy()) + np.testing.assert_allclose(out_ref, out2.numpy()) + paddle.enable_static() + + +if __name__ == '__main__': + paddle.enable_static() + unittest.main() + + + + From 51b908a98af711b69370cc867a10b4a06a6f9ba9 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Sun, 8 Oct 2023 00:53:08 +0800 Subject: [PATCH 02/19] move pdist to nn.functional, expose paddle.pdist api --- python/paddle/__init__.py | 5 +- python/paddle/nn/functional/__init__.py | 2 + python/paddle/nn/functional/distance.py | 50 ++++++++++++++++ python/paddle/tensor/__init__.py | 2 - python/paddle/tensor/linalg.py | 78 ++++++++++++------------- 5 files changed, 95 insertions(+), 42 deletions(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 45715e8a43eb8e..b433843167182d 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -146,7 +146,6 @@ t, t_, cdist, - pdist, cross, cholesky, bmm, @@ -513,6 +512,10 @@ flops, ) +from .nn.functional.distance import ( # noqa: F401 + pdist, +) + import paddle.text # noqa: F401 import paddle.vision # noqa: F401 diff --git a/python/paddle/nn/functional/__init__.py b/python/paddle/nn/functional/__init__.py index 87f2eabba1f59e..217f8980c099b5 100644 --- a/python/paddle/nn/functional/__init__.py +++ b/python/paddle/nn/functional/__init__.py @@ -73,6 +73,7 @@ from .conv import conv3d # noqa: F401 from .conv import conv3d_transpose # noqa: F401 from .distance import pairwise_distance # noqa: F401 +from .distance import pdist # noqa: F401 from .extension import diag_embed # noqa: F401 from .extension import sequence_mask from .loss import binary_cross_entropy # noqa: F401 @@ -149,6 +150,7 @@ 'conv3d', 'conv3d_transpose', 'pairwise_distance', + 'pdist', 'elu', 'elu_', 'gelu', diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index dc69092daed084..b5926321bf8940 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -108,3 +108,53 @@ def pairwise_distance(x, y, p=2.0, epsilon=1e-6, keepdim=False, name=None): ) return out + +def pdist( + x, p=2.0, compute_mode="use_mm_for_euclid_dist_if_necessary", name=None +): + r''' + Computes the p-norm distance between every pair of row vectors in the input. + + Args: + x (Tensor): A tensor with shape :math:`N \times M`. + p (float, optional): The value for the p-norm distance to calculate between each vector pair. Default: :math:`2.0`. + compute_mode (str, optional): The mode for compute distance. + + - ``use_mm_for_euclid_dist_if_necessary`` , for p = 2.0 and (P > 25 or R > 25), it will use matrix multiplication to calculate euclid distance if possible. + - ``use_mm_for_euclid_dist`` , for p = 2.0, it will use matrix multiplication to calculate euclid distance. + - ``donot_use_mm_for_euclid_dist`` , it will not use matrix multiplication to calculate euclid distance. + + Default: ``use_mm_for_euclid_dist_if_necessary``. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. + + Returns: + Tensor with shape: math:`N(N-1)/2` the dtype is same as input tensor. + + Examples: + .. code-block:: python + + >>> import paddle + >>> a = paddle.randn([4, 5]) + >>> a + Tensor(shape=[4, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[-0.33173719, -0.93648648, -0.01741328, -0.94435263, 2.22178721], + [-0.65466857, 0.10307083, 0.08741203, -0.91078597, 0.72589827], + [ 0.06907391, -0.27584535, 1.35355449, -0.69688839, 0.18408430], + [-0.00939178, -0.32901841, -1.06503606, 0.81856263, 0.16791444]]) + >>> pdist_out=paddle.pdist(a) + >>> pdist_out + Tensor(shape=[6], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [1.85331142, 2.58652687, 2.98273396, 1.61549115, 2.28762150, 2.85576940]) + + ''' + + x_shape = list(x.shape) + assert len(x_shape) == 2, ( + "The x must be 2-dimensional" + ) + d = paddle.cdist(x, x, p, compute_mode) + mask = ~paddle.tril(paddle.ones(d.shape, dtype='bool')) + return paddle.masked_select(d, mask) + + + \ No newline at end of file diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 9816a4fdbef60b..b728392b0452de 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -78,7 +78,6 @@ from .linalg import lu # noqa: F401 from .linalg import lu_unpack # noqa: F401 from .linalg import cdist # noqa: F401 -from .linalg import pdist # noqa: F401 from .logic import equal # noqa: F401 from .logic import equal_ # noqa: F401 from .logic import greater_equal # noqa: F401 @@ -635,7 +634,6 @@ 'lu', 'lu_unpack', 'cdist', - 'pdist', 'as_complex', 'as_real', 'rad2deg', diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 9c1a7a2e47b5ee..2f82f8c89ef8e8 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -3733,50 +3733,50 @@ def cdist( ) -def pdist( - x, p=2.0, compute_mode="use_mm_for_euclid_dist_if_necessary", name=None -): - r''' - Computes the p-norm distance between every pair of row vectors in the input. +# def pdist( +# x, p=2.0, compute_mode="use_mm_for_euclid_dist_if_necessary", name=None +# ): +# r''' +# Computes the p-norm distance between every pair of row vectors in the input. - Args: - x (Tensor): A tensor with shape :math:`N \times M`. - p (float, optional): The value for the p-norm distance to calculate between each vector pair. Default: :math:`2.0`. - compute_mode (str, optional): The mode for compute distance. +# Args: +# x (Tensor): A tensor with shape :math:`N \times M`. +# p (float, optional): The value for the p-norm distance to calculate between each vector pair. Default: :math:`2.0`. +# compute_mode (str, optional): The mode for compute distance. - - ``use_mm_for_euclid_dist_if_necessary`` , for p = 2.0 and (P > 25 or R > 25), it will use matrix multiplication to calculate euclid distance if possible. - - ``use_mm_for_euclid_dist`` , for p = 2.0, it will use matrix multiplication to calculate euclid distance. - - ``donot_use_mm_for_euclid_dist`` , it will not use matrix multiplication to calculate euclid distance. +# - ``use_mm_for_euclid_dist_if_necessary`` , for p = 2.0 and (P > 25 or R > 25), it will use matrix multiplication to calculate euclid distance if possible. +# - ``use_mm_for_euclid_dist`` , for p = 2.0, it will use matrix multiplication to calculate euclid distance. +# - ``donot_use_mm_for_euclid_dist`` , it will not use matrix multiplication to calculate euclid distance. - Default: ``use_mm_for_euclid_dist_if_necessary``. - name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. +# Default: ``use_mm_for_euclid_dist_if_necessary``. +# name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. - Returns: - Tensor with shape: math:`N(N-1)/2` the dtype is same as input tensor. +# Returns: +# Tensor with shape: math:`N(N-1)/2` the dtype is same as input tensor. - Examples: - .. code-block:: python +# Examples: +# .. code-block:: python - >>> import paddle - >>> a = paddle.randn([4, 5]) - >>> a - Tensor(shape=[4, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, - [[-0.33173719, -0.93648648, -0.01741328, -0.94435263, 2.22178721], - [-0.65466857, 0.10307083, 0.08741203, -0.91078597, 0.72589827], - [ 0.06907391, -0.27584535, 1.35355449, -0.69688839, 0.18408430], - [-0.00939178, -0.32901841, -1.06503606, 0.81856263, 0.16791444]]) - >>> pdist_out=paddle.pdist(a) - >>> pdist_out - Tensor(shape=[6], dtype=float32, place=Place(gpu:0), stop_gradient=True, - [1.85331142, 2.58652687, 2.98273396, 1.61549115, 2.28762150, 2.85576940]) +# >>> import paddle +# >>> a = paddle.randn([4, 5]) +# >>> a +# Tensor(shape=[4, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, +# [[-0.33173719, -0.93648648, -0.01741328, -0.94435263, 2.22178721], +# [-0.65466857, 0.10307083, 0.08741203, -0.91078597, 0.72589827], +# [ 0.06907391, -0.27584535, 1.35355449, -0.69688839, 0.18408430], +# [-0.00939178, -0.32901841, -1.06503606, 0.81856263, 0.16791444]]) +# >>> pdist_out=paddle.pdist(a) +# >>> pdist_out +# Tensor(shape=[6], dtype=float32, place=Place(gpu:0), stop_gradient=True, +# [1.85331142, 2.58652687, 2.98273396, 1.61549115, 2.28762150, 2.85576940]) - ''' - - x_shape = list(x.shape) - assert len(x_shape) == 2, ( - "The x must be 2-dimensional" - ) - d = cdist(x, x, p, compute_mode) - mask = ~paddle.tril(paddle.ones(d.shape, dtype='bool')) - return masked_select(d, mask) +# ''' + +# x_shape = list(x.shape) +# assert len(x_shape) == 2, ( +# "The x must be 2-dimensional" +# ) +# d = cdist(x, x, p, compute_mode) +# mask = ~paddle.tril(paddle.ones(d.shape, dtype='bool')) +# return masked_select(d, mask) From ced239c46ddd52004569f9113cbddb63e422e904 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Sun, 8 Oct 2023 00:57:15 +0800 Subject: [PATCH 03/19] clean --- python/paddle/tensor/linalg.py | 54 ++-------------------------------- 1 file changed, 2 insertions(+), 52 deletions(-) diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index 2f82f8c89ef8e8..89d714a3090ad3 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -22,8 +22,7 @@ from ..base.data_feeder import check_dtype, check_type, check_variable_and_dtype from ..common_ops_import import Variable from ..framework import LayerHelper, in_dynamic_mode, in_dynamic_or_pir_mode -from .creation import full, tril -from .search import masked_select +from .creation import full from .manipulation import cast from .math import _get_reduce_axis @@ -3730,53 +3729,4 @@ def cdist( return paddle.linalg.norm( x[..., None, :] - y[..., None, :, :], p=p, axis=-1 - ) - - -# def pdist( -# x, p=2.0, compute_mode="use_mm_for_euclid_dist_if_necessary", name=None -# ): -# r''' -# Computes the p-norm distance between every pair of row vectors in the input. - -# Args: -# x (Tensor): A tensor with shape :math:`N \times M`. -# p (float, optional): The value for the p-norm distance to calculate between each vector pair. Default: :math:`2.0`. -# compute_mode (str, optional): The mode for compute distance. - -# - ``use_mm_for_euclid_dist_if_necessary`` , for p = 2.0 and (P > 25 or R > 25), it will use matrix multiplication to calculate euclid distance if possible. -# - ``use_mm_for_euclid_dist`` , for p = 2.0, it will use matrix multiplication to calculate euclid distance. -# - ``donot_use_mm_for_euclid_dist`` , it will not use matrix multiplication to calculate euclid distance. - -# Default: ``use_mm_for_euclid_dist_if_necessary``. -# name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. - -# Returns: -# Tensor with shape: math:`N(N-1)/2` the dtype is same as input tensor. - -# Examples: -# .. code-block:: python - -# >>> import paddle -# >>> a = paddle.randn([4, 5]) -# >>> a -# Tensor(shape=[4, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, -# [[-0.33173719, -0.93648648, -0.01741328, -0.94435263, 2.22178721], -# [-0.65466857, 0.10307083, 0.08741203, -0.91078597, 0.72589827], -# [ 0.06907391, -0.27584535, 1.35355449, -0.69688839, 0.18408430], -# [-0.00939178, -0.32901841, -1.06503606, 0.81856263, 0.16791444]]) -# >>> pdist_out=paddle.pdist(a) -# >>> pdist_out -# Tensor(shape=[6], dtype=float32, place=Place(gpu:0), stop_gradient=True, -# [1.85331142, 2.58652687, 2.98273396, 1.61549115, 2.28762150, 2.85576940]) - -# ''' - -# x_shape = list(x.shape) -# assert len(x_shape) == 2, ( -# "The x must be 2-dimensional" -# ) -# d = cdist(x, x, p, compute_mode) -# mask = ~paddle.tril(paddle.ones(d.shape, dtype='bool')) -# return masked_select(d, mask) - + ) \ No newline at end of file From 9c651d99fcc7e2d8ffb49fc672ddb74c6d58db31 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Sun, 8 Oct 2023 00:58:57 +0800 Subject: [PATCH 04/19] clean --- 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 89d714a3090ad3..acc59d6385e570 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -3729,4 +3729,4 @@ def cdist( return paddle.linalg.norm( x[..., None, :] - y[..., None, :, :], p=p, axis=-1 - ) \ No newline at end of file + ) From 13d04ad7a0907d3ee91237e8bdadbba38969fbfd Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Sat, 21 Oct 2023 22:44:43 +0800 Subject: [PATCH 05/19] fix codestyle --- python/paddle/nn/functional/distance.py | 16 ++++++---------- test/legacy_test/test_pdist.py | 15 ++++++--------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index b5926321bf8940..3cad27615411ca 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -109,12 +109,13 @@ def pairwise_distance(x, y, p=2.0, epsilon=1e-6, keepdim=False, name=None): return out + def pdist( x, p=2.0, compute_mode="use_mm_for_euclid_dist_if_necessary", name=None ): r''' - Computes the p-norm distance between every pair of row vectors in the input. - + Computes the p-norm distance between every pair of row vectors in the input. + Args: x (Tensor): A tensor with shape :math:`N \times M`. p (float, optional): The value for the p-norm distance to calculate between each vector pair. Default: :math:`2.0`. @@ -132,7 +133,7 @@ def pdist( Examples: .. code-block:: python - + >>> import paddle >>> a = paddle.randn([4, 5]) >>> a @@ -145,16 +146,11 @@ def pdist( >>> pdist_out Tensor(shape=[6], dtype=float32, place=Place(gpu:0), stop_gradient=True, [1.85331142, 2.58652687, 2.98273396, 1.61549115, 2.28762150, 2.85576940]) - + ''' x_shape = list(x.shape) - assert len(x_shape) == 2, ( - "The x must be 2-dimensional" - ) + assert len(x_shape) == 2, "The x must be 2-dimensional" d = paddle.cdist(x, x, p, compute_mode) mask = ~paddle.tril(paddle.ones(d.shape, dtype='bool')) return paddle.masked_select(d, mask) - - - \ No newline at end of file diff --git a/test/legacy_test/test_pdist.py b/test/legacy_test/test_pdist.py index 7e876459970b30..6a08d4cd1abac0 100644 --- a/test/legacy_test/test_pdist.py +++ b/test/legacy_test/test_pdist.py @@ -13,16 +13,20 @@ # limitations under the License. import unittest + import numpy as np + import paddle + def ref_pdist(x, p=2.0): dist = np.linalg.norm(x[..., None, :] - x[None, :, :], ord=p, axis=-1) res = [] rows, cols = dist.shape for i in range(rows): for j in range(cols): - if i >= j: continue + if i >= j: + continue res.append(dist[i][j]) return np.array(res) @@ -107,7 +111,6 @@ class TestpdistAPICase9(TestpdistAPI): def init_input(self): self.x = np.random.rand(500, 100).astype('float64') - def test_static_api(self): paddle.enable_static() with paddle.static.program_guard(paddle.static.Program()): @@ -116,9 +119,7 @@ def test_static_api(self): out1 = paddle.pdist(x, self.p, "donot_use_mm_for_euclid_dist") out2 = paddle.pdist(x, self.p, "use_mm_for_euclid_dist") exe = paddle.static.Executor(self.place) - res = exe.run( - feed={'x': self.x}, fetch_list=[out0, out1, out2] - ) + res = exe.run(feed={'x': self.x}, fetch_list=[out0, out1, out2]) out_ref = ref_pdist(self.x, self.p) np.testing.assert_allclose(out_ref, res[0]) np.testing.assert_allclose(out_ref, res[1]) @@ -140,7 +141,3 @@ def test_dygraph_api(self): if __name__ == '__main__': paddle.enable_static() unittest.main() - - - - From 7bbc22eed96da37ab7e0753639037fbc6a9e57f4 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Sat, 25 Nov 2023 00:44:48 +0800 Subject: [PATCH 06/19] remove compute_mode --- python/paddle/nn/functional/distance.py | 15 ++------ test/legacy_test/test_pdist.py | 50 ++++++++++++++----------- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index 656e941a8f7aae..532dbf31113230 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -108,22 +108,13 @@ def pairwise_distance(x, y, p=2.0, epsilon=1e-6, keepdim=False, name=None): return out -def pdist( - x, p=2.0, compute_mode="use_mm_for_euclid_dist_if_necessary", name=None -): +def pdist(x, p=2.0, name=None): r''' Computes the p-norm distance between every pair of row vectors in the input. Args: x (Tensor): A tensor with shape :math:`N \times M`. p (float, optional): The value for the p-norm distance to calculate between each vector pair. Default: :math:`2.0`. - compute_mode (str, optional): The mode for compute distance. - - - ``use_mm_for_euclid_dist_if_necessary`` , for p = 2.0 and (P > 25 or R > 25), it will use matrix multiplication to calculate euclid distance if possible. - - ``use_mm_for_euclid_dist`` , for p = 2.0, it will use matrix multiplication to calculate euclid distance. - - ``donot_use_mm_for_euclid_dist`` , it will not use matrix multiplication to calculate euclid distance. - - Default: ``use_mm_for_euclid_dist_if_necessary``. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: @@ -141,7 +132,7 @@ def pdist( [ 0.06907391, -0.27584535, 1.35355449, -0.69688839, 0.18408430], [-0.00939178, -0.32901841, -1.06503606, 0.81856263, 0.16791444]]) >>> pdist_out=paddle.pdist(a) - >>> pdist_out + >>> print(pdist_out) Tensor(shape=[6], dtype=float32, place=Place(gpu:0), stop_gradient=True, [1.85331142, 2.58652687, 2.98273396, 1.61549115, 2.28762150, 2.85576940]) @@ -149,6 +140,6 @@ def pdist( x_shape = list(x.shape) assert len(x_shape) == 2, "The x must be 2-dimensional" - d = paddle.cdist(x, x, p, compute_mode) + d = paddle.linalg.norm(x[..., None, :] - x[..., None, :, :], p=p, axis=-1) mask = ~paddle.tril(paddle.ones(d.shape, dtype='bool')) return paddle.masked_select(d, mask) diff --git a/test/legacy_test/test_pdist.py b/test/legacy_test/test_pdist.py index 6a08d4cd1abac0..afdc1287ce6277 100644 --- a/test/legacy_test/test_pdist.py +++ b/test/legacy_test/test_pdist.py @@ -35,7 +35,6 @@ class TestpdistAPI(unittest.TestCase): def setUp(self): self.x = np.random.rand(10, 20).astype('float32') self.p = 2.0 - self.compute_mode = "use_mm_for_euclid_dist_if_necessary" self.init_input() self.place = ( paddle.CUDAPlace(0) @@ -50,7 +49,10 @@ def test_static_api(self): paddle.enable_static() with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) - out = paddle.pdist(x, self.p, self.compute_mode) + out = paddle.pdist( + x, + self.p, + ) exe = paddle.static.Executor(self.place) res = exe.run(feed={'x': self.x}, fetch_list=[out]) out_ref = ref_pdist(self.x, self.p) @@ -59,7 +61,10 @@ def test_static_api(self): def test_dygraph_api(self): paddle.disable_static(self.place) x = paddle.to_tensor(self.x) - out = paddle.pdist(x, self.p, self.compute_mode) + out = paddle.pdist( + x, + self.p, + ) out_ref = ref_pdist(self.x, self.p) np.testing.assert_allclose(out_ref, out.numpy(), rtol=1e-5, atol=1e-5) paddle.enable_static() @@ -98,16 +103,9 @@ def init_input(self): class TestpdistAPICase7(TestpdistAPI): def init_input(self): self.x = np.random.rand(50, 20).astype('float64') - self.compute_mode = "use_mm_for_euclid_dist" class TestpdistAPICase8(TestpdistAPI): - def init_input(self): - self.x = np.random.rand(50, 20).astype('float64') - self.compute_mode = "donot_use_mm_for_euclid_dist" - - -class TestpdistAPICase9(TestpdistAPI): def init_input(self): self.x = np.random.rand(500, 100).astype('float64') @@ -115,29 +113,39 @@ def test_static_api(self): paddle.enable_static() with paddle.static.program_guard(paddle.static.Program()): x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) - out0 = paddle.pdist(x, self.p, self.compute_mode) - out1 = paddle.pdist(x, self.p, "donot_use_mm_for_euclid_dist") - out2 = paddle.pdist(x, self.p, "use_mm_for_euclid_dist") + out0 = paddle.pdist( + x, + self.p, + ) exe = paddle.static.Executor(self.place) - res = exe.run(feed={'x': self.x}, fetch_list=[out0, out1, out2]) + res = exe.run(feed={'x': self.x}, fetch_list=[out0]) out_ref = ref_pdist(self.x, self.p) np.testing.assert_allclose(out_ref, res[0]) - np.testing.assert_allclose(out_ref, res[1]) - np.testing.assert_allclose(out_ref, res[2]) def test_dygraph_api(self): paddle.disable_static(self.place) x = paddle.to_tensor(self.x) - out0 = paddle.pdist(x, self.p, self.compute_mode) - out1 = paddle.pdist(x, self.p, "donot_use_mm_for_euclid_dist") - out2 = paddle.pdist(x, self.p, "use_mm_for_euclid_dist") + out0 = paddle.pdist( + x, + self.p, + ) out_ref = ref_pdist(self.x, self.p) np.testing.assert_allclose(out_ref, out0.numpy()) - np.testing.assert_allclose(out_ref, out1.numpy()) - np.testing.assert_allclose(out_ref, out2.numpy()) paddle.enable_static() +class TestShapeError(unittest.TestCase): + def test_error(self): + with self.assertRaises(AssertionError): + self.x = np.random.rand(500, 100, 200).astype('float64') + self.p = 2.0 + x = paddle.to_tensor(self.x) + out0 = paddle.pdist( + x, + self.p, + ) + + if __name__ == '__main__': paddle.enable_static() unittest.main() From 20f82de8a20f4a73aba7a2b663b442fee822bc43 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Sat, 25 Nov 2023 02:38:56 +0800 Subject: [PATCH 07/19] for api name rules --- test/legacy_test/test_pdist.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/legacy_test/test_pdist.py b/test/legacy_test/test_pdist.py index afdc1287ce6277..6b9ead1f679873 100644 --- a/test/legacy_test/test_pdist.py +++ b/test/legacy_test/test_pdist.py @@ -31,7 +31,7 @@ def ref_pdist(x, p=2.0): return np.array(res) -class TestpdistAPI(unittest.TestCase): +class TestPdistAPI(unittest.TestCase): def setUp(self): self.x = np.random.rand(10, 20).astype('float32') self.p = 2.0 @@ -70,42 +70,42 @@ def test_dygraph_api(self): paddle.enable_static() -class TestpdistAPICase1(TestpdistAPI): +class TestPdistAPICase1(TestPdistAPI): def init_input(self): self.p = 0 -class TestpdistAPICase2(TestpdistAPI): +class TestPdistAPICase2(TestPdistAPI): def init_input(self): self.p = 1.0 -class TestpdistAPICase3(TestpdistAPI): +class TestPdistAPICase3(TestPdistAPI): def init_input(self): self.p = 3.0 -class TestpdistAPICase4(TestpdistAPI): +class TestPdistAPICase4(TestPdistAPI): def init_input(self): self.p = 1.5 -class TestpdistAPICase5(TestpdistAPI): +class TestPdistAPICase5(TestPdistAPI): def init_input(self): self.p = 2.5 -class TestpdistAPICase6(TestpdistAPI): +class TestPdistAPICase6(TestPdistAPI): def init_input(self): self.p = float('inf') -class TestpdistAPICase7(TestpdistAPI): +class TestPdistAPICase7(TestPdistAPI): def init_input(self): self.x = np.random.rand(50, 20).astype('float64') -class TestpdistAPICase8(TestpdistAPI): +class TestPdistAPICase8(TestPdistAPI): def init_input(self): self.x = np.random.rand(500, 100).astype('float64') @@ -134,10 +134,10 @@ def test_dygraph_api(self): paddle.enable_static() -class TestShapeError(unittest.TestCase): +class TestPdistShapeError(unittest.TestCase): def test_error(self): with self.assertRaises(AssertionError): - self.x = np.random.rand(500, 100, 200).astype('float64') + self.x = np.random.rand(50, 10, 20).astype('float64') self.p = 2.0 x = paddle.to_tensor(self.x) out0 = paddle.pdist( From e9dfa5ab215bbf2345b563040e6a9c4ace6724e1 Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Thu, 30 Nov 2023 18:48:45 +0800 Subject: [PATCH 08/19] Update test_pdist.py --- test/legacy_test/test_pdist.py | 43 ++++++---------------------------- 1 file changed, 7 insertions(+), 36 deletions(-) diff --git a/test/legacy_test/test_pdist.py b/test/legacy_test/test_pdist.py index 6b9ead1f679873..503004de0a07c3 100644 --- a/test/legacy_test/test_pdist.py +++ b/test/legacy_test/test_pdist.py @@ -70,70 +70,41 @@ def test_dygraph_api(self): paddle.enable_static() -class TestPdistAPICase1(TestPdistAPI): +class TestPdistAPICase1_param_p1(TestPdistAPI): def init_input(self): self.p = 0 -class TestPdistAPICase2(TestPdistAPI): +class TestPdistAPICase2_param_p2(TestPdistAPI): def init_input(self): self.p = 1.0 -class TestPdistAPICase3(TestPdistAPI): +class TestPdistAPICase3_param_p3(TestPdistAPI): def init_input(self): self.p = 3.0 -class TestPdistAPICase4(TestPdistAPI): +class TestPdistAPICase4_param_p4(TestPdistAPI): def init_input(self): self.p = 1.5 -class TestPdistAPICase5(TestPdistAPI): +class TestPdistAPICase5_param_p5(TestPdistAPI): def init_input(self): self.p = 2.5 -class TestPdistAPICase6(TestPdistAPI): +class TestPdistAPICase6_param_p6(TestPdistAPI): def init_input(self): self.p = float('inf') -class TestPdistAPICase7(TestPdistAPI): +class TestPdistAPICase7_input_x1(TestPdistAPI): def init_input(self): self.x = np.random.rand(50, 20).astype('float64') -class TestPdistAPICase8(TestPdistAPI): - def init_input(self): - self.x = np.random.rand(500, 100).astype('float64') - - def test_static_api(self): - paddle.enable_static() - with paddle.static.program_guard(paddle.static.Program()): - x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) - out0 = paddle.pdist( - x, - self.p, - ) - exe = paddle.static.Executor(self.place) - res = exe.run(feed={'x': self.x}, fetch_list=[out0]) - out_ref = ref_pdist(self.x, self.p) - np.testing.assert_allclose(out_ref, res[0]) - - def test_dygraph_api(self): - paddle.disable_static(self.place) - x = paddle.to_tensor(self.x) - out0 = paddle.pdist( - x, - self.p, - ) - out_ref = ref_pdist(self.x, self.p) - np.testing.assert_allclose(out_ref, out0.numpy()) - paddle.enable_static() - - class TestPdistShapeError(unittest.TestCase): def test_error(self): with self.assertRaises(AssertionError): From 23d5f7e9a2af411503ef23af2b6691892b01b430 Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Fri, 1 Dec 2023 01:50:38 +0800 Subject: [PATCH 09/19] add seed --- python/paddle/nn/functional/distance.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index 532dbf31113230..9f9fc328bac239 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -124,6 +124,7 @@ def pdist(x, p=2.0, name=None): .. code-block:: python >>> import paddle + >>> paddle.seed(2023) >>> a = paddle.randn([4, 5]) >>> a Tensor(shape=[4, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, From 3a9fbfb75b28f3bf5827c2296ec0806fe36a9379 Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Fri, 1 Dec 2023 12:48:31 +0800 Subject: [PATCH 10/19] fix code sample --- python/paddle/nn/functional/distance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index 9f9fc328bac239..eaa3c5ac1cb17e 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -126,7 +126,7 @@ def pdist(x, p=2.0, name=None): >>> import paddle >>> paddle.seed(2023) >>> a = paddle.randn([4, 5]) - >>> a + >>> print(a) Tensor(shape=[4, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[-0.33173719, -0.93648648, -0.01741328, -0.94435263, 2.22178721], [-0.65466857, 0.10307083, 0.08741203, -0.91078597, 0.72589827], From 008ae5bba659b6d841be488cd318b40aaecbf3ec Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Tue, 5 Dec 2023 17:17:21 +0800 Subject: [PATCH 11/19] fix doc --- python/paddle/nn/functional/distance.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index eaa3c5ac1cb17e..10958948f0d533 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -128,15 +128,14 @@ def pdist(x, p=2.0, name=None): >>> a = paddle.randn([4, 5]) >>> print(a) Tensor(shape=[4, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, - [[-0.33173719, -0.93648648, -0.01741328, -0.94435263, 2.22178721], - [-0.65466857, 0.10307083, 0.08741203, -0.91078597, 0.72589827], - [ 0.06907391, -0.27584535, 1.35355449, -0.69688839, 0.18408430], - [-0.00939178, -0.32901841, -1.06503606, 0.81856263, 0.16791444]]) + [[-0.49133155, 0.53819323, -3.10416031, -1.51671720, -0.29990962], + [ 0.22085167, -0.00404538, 0.40126652, 0.53417486, 0.84864247], + [ 0.78248203, -1.59652555, -0.14399840, 1.29321253, 0.06063633], + [-0.30991879, -0.99713278, -0.51025450, -0.42649266, 0.61627960]]) >>> pdist_out=paddle.pdist(a) >>> print(pdist_out) Tensor(shape=[6], dtype=float32, place=Place(gpu:0), stop_gradient=True, - [1.85331142, 2.58652687, 2.98273396, 1.61549115, 2.28762150, 2.85576940]) - + [4.31446123, 4.79248190, 3.33863401, 2.08466482, 1.75376320, 2.22550654]) ''' x_shape = list(x.shape) From 114453a6b7afc071ebf6c08a58d626e3a2d1856e Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Wed, 6 Dec 2023 06:30:51 +0800 Subject: [PATCH 12/19] Update distance.py --- python/paddle/nn/functional/distance.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index 10958948f0d533..b65fe04256038c 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -126,12 +126,6 @@ def pdist(x, p=2.0, name=None): >>> import paddle >>> paddle.seed(2023) >>> a = paddle.randn([4, 5]) - >>> print(a) - Tensor(shape=[4, 5], dtype=float32, place=Place(gpu:0), stop_gradient=True, - [[-0.49133155, 0.53819323, -3.10416031, -1.51671720, -0.29990962], - [ 0.22085167, -0.00404538, 0.40126652, 0.53417486, 0.84864247], - [ 0.78248203, -1.59652555, -0.14399840, 1.29321253, 0.06063633], - [-0.30991879, -0.99713278, -0.51025450, -0.42649266, 0.61627960]]) >>> pdist_out=paddle.pdist(a) >>> print(pdist_out) Tensor(shape=[6], dtype=float32, place=Place(gpu:0), stop_gradient=True, From 171339db14ac32dcf5c60d00d49d6be3799918ba Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Sat, 9 Dec 2023 10:22:49 +0800 Subject: [PATCH 13/19] gpu0 to cpu in api doc --- python/paddle/nn/functional/distance.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index b65fe04256038c..7339ffc3156f40 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -123,13 +123,18 @@ def pdist(x, p=2.0, name=None): Examples: .. code-block:: python - >>> import paddle - >>> paddle.seed(2023) - >>> a = paddle.randn([4, 5]) - >>> pdist_out=paddle.pdist(a) - >>> print(pdist_out) - Tensor(shape=[6], dtype=float32, place=Place(gpu:0), stop_gradient=True, - [4.31446123, 4.79248190, 3.33863401, 2.08466482, 1.75376320, 2.22550654]) + >>> paddle.seed(2023) + >>> a = paddle.randn([4, 5]) + >>> print(a) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.06132207, 1.11349595, 0.41906244, -0.24858207, -1.85169315], + [-1.50370061, 1.73954511, 0.13331604, 1.66359663, -0.55764782], + [-0.59911072, -0.57773495, -1.03176904, -0.33741450, -0.29695082], + [-1.50258386, 0.67233968, -1.07747352, 0.80170447, -0.06695852]]) + >>> pdist_out=paddle.pdist(a) + >>> print(pdist_out) + Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True, + [2.87295413, 2.79758120, 3.02793980, 3.40844536, 1.89435327, 1.93171620]) ''' x_shape = list(x.shape) From 8a281b483523ef35042dc9e8b5f389bcac3c8a95 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Sat, 9 Dec 2023 10:28:10 +0800 Subject: [PATCH 14/19] gpu0 to cpu in api doc --- python/paddle/nn/functional/distance.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index 7339ffc3156f40..5d06b9b024e257 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -123,6 +123,7 @@ def pdist(x, p=2.0, name=None): Examples: .. code-block:: python + >>> import paddle >>> paddle.seed(2023) >>> a = paddle.randn([4, 5]) >>> print(a) From a9d053c6c1422c190b00401e0fe0d1ea30e01163 Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Tue, 12 Dec 2023 12:59:24 +0800 Subject: [PATCH 15/19] remove pdist in nn.functional __all__ list --- python/paddle/nn/functional/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/nn/functional/__init__.py b/python/paddle/nn/functional/__init__.py index b4c701a24b26ee..95a648a3d9cd27 100644 --- a/python/paddle/nn/functional/__init__.py +++ b/python/paddle/nn/functional/__init__.py @@ -163,7 +163,6 @@ 'conv3d', 'conv3d_transpose', 'pairwise_distance', - 'pdist', 'elu', 'elu_', 'gelu', From 3f7b60755ffbf652a41953849f22febb65f30524 Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Tue, 12 Dec 2023 13:04:26 +0800 Subject: [PATCH 16/19] Update __init__.py --- python/paddle/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 875489d4dc7c7b..9d7af06e870ade 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -526,7 +526,6 @@ ) from .tensor.to_string import set_printoptions - # CINN has to set a flag to include a lib if is_compiled_with_cinn(): import os From e3e5abf2aafc323d12e99b26f839bfb0903ecefb Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Wed, 13 Dec 2023 14:35:19 +0800 Subject: [PATCH 17/19] fix en doc --- python/paddle/nn/functional/distance.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index 5d06b9b024e257..96ce0ac3384a06 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -113,15 +113,16 @@ def pdist(x, p=2.0, name=None): Computes the p-norm distance between every pair of row vectors in the input. Args: - x (Tensor): A tensor with shape :math:`N \times M`. + x (Tensor): The input tensor with shape :math:`N \times M`. p (float, optional): The value for the p-norm distance to calculate between each vector pair. Default: :math:`2.0`. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: - Tensor with shape: math:`N(N-1)/2` the dtype is same as input tensor. + Tensor with shape: :math:`N(N-1)/2` the dtype is same as input tensor. Examples: .. code-block:: python + :name: example >>> import paddle >>> paddle.seed(2023) From 8172f28375c3781662d810a5e9c01c8d07980669 Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Wed, 13 Dec 2023 15:59:08 +0800 Subject: [PATCH 18/19] Update python/paddle/nn/functional/distance.py Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> --- python/paddle/nn/functional/distance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index 96ce0ac3384a06..8362d30a8fbf74 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -118,7 +118,7 @@ def pdist(x, p=2.0, name=None): name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: - Tensor with shape: :math:`N(N-1)/2` the dtype is same as input tensor. + Tensor with shape :math:`N(N-1)/2` , the dtype is same as input tensor. Examples: .. code-block:: python From 0146a3715fb090478ec0a7bd9076df2b46e60293 Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Wed, 13 Dec 2023 15:59:26 +0800 Subject: [PATCH 19/19] Update python/paddle/nn/functional/distance.py Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> --- python/paddle/nn/functional/distance.py | 29 ++++++++++++------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/python/paddle/nn/functional/distance.py b/python/paddle/nn/functional/distance.py index 8362d30a8fbf74..f14c220c0c0dbf 100644 --- a/python/paddle/nn/functional/distance.py +++ b/python/paddle/nn/functional/distance.py @@ -122,21 +122,20 @@ def pdist(x, p=2.0, name=None): Examples: .. code-block:: python - :name: example - - >>> import paddle - >>> paddle.seed(2023) - >>> a = paddle.randn([4, 5]) - >>> print(a) - Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, - [[ 0.06132207, 1.11349595, 0.41906244, -0.24858207, -1.85169315], - [-1.50370061, 1.73954511, 0.13331604, 1.66359663, -0.55764782], - [-0.59911072, -0.57773495, -1.03176904, -0.33741450, -0.29695082], - [-1.50258386, 0.67233968, -1.07747352, 0.80170447, -0.06695852]]) - >>> pdist_out=paddle.pdist(a) - >>> print(pdist_out) - Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True, - [2.87295413, 2.79758120, 3.02793980, 3.40844536, 1.89435327, 1.93171620]) + + >>> import paddle + >>> paddle.seed(2023) + >>> a = paddle.randn([4, 5]) + >>> print(a) + Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.06132207, 1.11349595, 0.41906244, -0.24858207, -1.85169315], + [-1.50370061, 1.73954511, 0.13331604, 1.66359663, -0.55764782], + [-0.59911072, -0.57773495, -1.03176904, -0.33741450, -0.29695082], + [-1.50258386, 0.67233968, -1.07747352, 0.80170447, -0.06695852]]) + >>> pdist_out=paddle.pdist(a) + >>> print(pdist_out) + Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True, + [2.87295413, 2.79758120, 3.02793980, 3.40844536, 1.89435327, 1.93171620]) ''' x_shape = list(x.shape)