From 0d48548614b41fb4a0852b0861e807fc20c4ec6a Mon Sep 17 00:00:00 2001 From: Li-fAngyU <972117977@qq.com> Date: Sun, 10 Jul 2022 15:46:01 +0800 Subject: [PATCH 1/5] add paddle.bucketize api add paddle.bucketize api --- python/paddle/__init__.py | 2 + .../tests/unittests/test_bucketize_api.py | 98 +++++++++++++++++++ python/paddle/tensor/__init__.py | 2 + python/paddle/tensor/search.py | 55 +++++++++++ 4 files changed, 157 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/test_bucketize_api.py diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 6e47f4f9eab43..fbfa2ae8a55a1 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -291,6 +291,7 @@ from .tensor.search import argmin # noqa: F401 from .tensor.search import argsort # noqa: F401 from .tensor.search import searchsorted # noqa: F401 +from .tensor.search import bucketize # noqa: F401 from .tensor.search import masked_select # noqa: F401 from .tensor.search import topk # noqa: F401 from .tensor.search import where # noqa: F401 @@ -442,6 +443,7 @@ 'flops', 'sort', 'searchsorted', + 'bucketize', 'split', 'logical_and', 'full_like', diff --git a/python/paddle/fluid/tests/unittests/test_bucketize_api.py b/python/paddle/fluid/tests/unittests/test_bucketize_api.py new file mode 100644 index 0000000000000..04c73e996204f --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_bucketize_api.py @@ -0,0 +1,98 @@ +# Copyright (c) 2022 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. + +from __future__ import print_function +from re import X + +import unittest +import numpy as np +import paddle +import paddle.fluid as fluid +import paddle.fluid.core as core +from paddle.fluid import Program, program_guard + +np.random.seed(10) + + +class TestBucketizeAPI(unittest.TestCase): + # test paddle.tensor.math.nanmean + + def setUp(self): + self.sorted_sequence = np.array([2, 4, 8, 16]).astype("float64") + self.x = np.array([[0, 8, 4, 16], [-1, 2, 8, 4]]).astype("float64") + self.place = [paddle.CPUPlace()] + if core.is_compiled_with_cuda(): + self.place.append(paddle.CUDAPlace(0)) + + def test_api_static(self): + paddle.enable_static() + + def run(place): + with paddle.static.program_guard(paddle.static.Program()): + sorted_sequence = paddle.static.data( + 'SortedSequence', + shape=self.sorted_sequence.shape, + dtype="float64") + x = paddle.static.data('x', + shape=self.x.shape, + dtype="float64") + out = paddle.bucketize(x, sorted_sequence) + exe = paddle.static.Executor(place) + res = exe.run(feed={ + 'SortedSequence': self.sorted_sequence, + 'x': self.x + }, + fetch_list=out) + out_ref = np.searchsorted(self.sorted_sequence, self.x) + self.assertTrue(np.allclose(out_ref, res)) + + for place in self.place: + run(place) + + def test_api_dygraph(self): + + def run(place): + paddle.disable_static(place) + sorted_sequence = paddle.to_tensor(self.sorted_sequence) + x = paddle.to_tensor(self.x) + out = paddle.bucketize(x, sorted_sequence, right=True) + out_ref = np.searchsorted(self.sorted_sequence, + self.x, + side='right') + self.assertEqual(np.allclose(out_ref, out.numpy()), True) + paddle.enable_static() + + for place in self.place: + run(place) + + def test_out_int32(self): + paddle.disable_static() + sorted_sequence = paddle.to_tensor(self.sorted_sequence) + x = paddle.to_tensor(self.x) + out = paddle.bucketize(x, sorted_sequence, out_int32=True) + self.assertTrue(out.type, 'int32') + + def test_bucketize_dims_error(self): + with paddle.static.program_guard(paddle.static.Program()): + sorted_sequence = paddle.static.data('SortedSequence', + shape=[2, 2], + dtype="float64") + x = paddle.static.data('x', + shape=[2, 5], + dtype="float64") + self.assertRaises(ValueError, paddle.bucketize, x, sorted_sequence) + + +if __name__ == "__main__": + unittest.main() diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 08b0af26bd46e..76be3048422f7 100755 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -249,6 +249,7 @@ from .search import argmin # noqa: F401 from .search import argsort # noqa: F401 from .search import searchsorted # noqa: F401 +from .search import bucketize # noqa: F401 from .search import topk # noqa: F401 from .search import where # noqa: F401 from .search import index_select # noqa: F401 @@ -501,6 +502,7 @@ 'put_along_axis_', 'exponential_', 'heaviside', + 'bucketize', ] #this list used in math_op_patch.py for magic_method bind diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index a962f49358e87..3df6c17ba9b46 100644 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -913,6 +913,61 @@ def topk(x, k, axis=None, largest=True, sorted=True, name=None): indices.stop_gradient = True return values, indices +def bucketize(x, + sorted_sequence, + out_int32=False, + right=False, + name=None): + """ + This API is used to find the index of the corresponding 1D tensor `sorted_sequence` in the innermost dimension based on the given `x`. + + Args: + x(Tensor): An input N-D tensor value with type int32, int64, float32, float64. + sorted_sequence(Tensor): An input 1-D tensor with type int32, int64, float32, float64. The value of the tensor monotonically increases in the innermost dimension. + out_int32(bool, optional): Data type of the output tensor which can be int32, int64. The default value is False, and it indicates that the output data type is int64. + right(bool, optional): Find the upper or lower bounds of the sorted_sequence range in the innermost dimension based on the given `values`. If the value of the sorted_sequence is nan or inf, return the size of the innermost dimension. + The default value is False and it shows the lower bounds. + name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. + + Returns: + Tensor(the same sizes of the `x`), return the tensor of int32 if set :attr:`out_int32` is True, otherwise return the tensor of int64. + + Examples: + + .. code-block:: python + + import paddle + + sorted_sequence = paddle.to_tensor([2, 4, 8, 16], dtype='int32') + x = paddle.to_tensor([[0, 8, 4, 16], [-1, 2, 8, 4]], dtype='int32') + out1 = paddle.bucketize(x, sorted_sequence) + print(out1) + # Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True, + # [[0, 2, 1, 3], + # [0, 0, 2, 1]]) + out2 = paddle.bucketize(x, sorted_sequence, right=True) + print(out2) + # Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True, + # [[0, 3, 2, 4], + # [0, 1, 3, 2]]) + out3 = x.bucketize(sorted_sequence) + print(out3) + # Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True, + # [[0, 2, 1, 3], + # [0, 0, 2, 1]]) + out4 = x.bucketize(sorted_sequence, right=True) + print(out4) + # Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True, + # [[0, 3, 2, 4], + # [0, 1, 3, 2]]) + + """ + check_variable_and_dtype(sorted_sequence, 'SortedSequence', + ['float32', 'float64', 'int32', 'int64'], + 'paddle.searchsorted') + if sorted_sequence.dim() != 1: + raise ValueError(f"boundaries tensor must be 1 dimension, but got dim {sorted_sequence.dim()}") + return searchsorted(sorted_sequence, x, out_int32, right, name) def searchsorted(sorted_sequence, values, From 5be4e3363b7fe5803e3badbced4fdfa706e25d1a Mon Sep 17 00:00:00 2001 From: Li-fAngyU <972117977@qq.com> Date: Mon, 11 Jul 2022 10:46:53 +0800 Subject: [PATCH 2/5] updata paddle.bucketize code style. --- .../tests/unittests/test_bucketize_api.py | 24 ++++++++----------- python/paddle/tensor/search.py | 16 ++++++------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_bucketize_api.py b/python/paddle/fluid/tests/unittests/test_bucketize_api.py index 04c73e996204f..ee21c3dad866a 100644 --- a/python/paddle/fluid/tests/unittests/test_bucketize_api.py +++ b/python/paddle/fluid/tests/unittests/test_bucketize_api.py @@ -41,19 +41,17 @@ def test_api_static(self): def run(place): with paddle.static.program_guard(paddle.static.Program()): sorted_sequence = paddle.static.data( - 'SortedSequence', - shape=self.sorted_sequence.shape, - dtype="float64") - x = paddle.static.data('x', - shape=self.x.shape, - dtype="float64") + 'SortedSequence', + shape=self.sorted_sequence.shape, + dtype="float64") + x = paddle.static.data('x', shape=self.x.shape, dtype="float64") out = paddle.bucketize(x, sorted_sequence) exe = paddle.static.Executor(place) res = exe.run(feed={ 'SortedSequence': self.sorted_sequence, 'x': self.x }, - fetch_list=out) + fetch_list=out) out_ref = np.searchsorted(self.sorted_sequence, self.x) self.assertTrue(np.allclose(out_ref, res)) @@ -68,8 +66,8 @@ def run(place): x = paddle.to_tensor(self.x) out = paddle.bucketize(x, sorted_sequence, right=True) out_ref = np.searchsorted(self.sorted_sequence, - self.x, - side='right') + self.x, + side='right') self.assertEqual(np.allclose(out_ref, out.numpy()), True) paddle.enable_static() @@ -86,11 +84,9 @@ def test_out_int32(self): def test_bucketize_dims_error(self): with paddle.static.program_guard(paddle.static.Program()): sorted_sequence = paddle.static.data('SortedSequence', - shape=[2, 2], - dtype="float64") - x = paddle.static.data('x', - shape=[2, 5], - dtype="float64") + shape=[2, 2], + dtype="float64") + x = paddle.static.data('x', shape=[2, 5], dtype="float64") self.assertRaises(ValueError, paddle.bucketize, x, sorted_sequence) diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index 3df6c17ba9b46..f0cc2c901e57d 100644 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -913,11 +913,8 @@ def topk(x, k, axis=None, largest=True, sorted=True, name=None): indices.stop_gradient = True return values, indices -def bucketize(x, - sorted_sequence, - out_int32=False, - right=False, - name=None): + +def bucketize(x, sorted_sequence, out_int32=False, right=False, name=None): """ This API is used to find the index of the corresponding 1D tensor `sorted_sequence` in the innermost dimension based on the given `x`. @@ -963,12 +960,15 @@ def bucketize(x, """ check_variable_and_dtype(sorted_sequence, 'SortedSequence', - ['float32', 'float64', 'int32', 'int64'], - 'paddle.searchsorted') + ['float32', 'float64', 'int32', 'int64'], + 'paddle.searchsorted') if sorted_sequence.dim() != 1: - raise ValueError(f"boundaries tensor must be 1 dimension, but got dim {sorted_sequence.dim()}") + raise ValueError( + f"boundaries tensor must be 1 dimension, but got dim {sorted_sequence.dim()}" + ) return searchsorted(sorted_sequence, x, out_int32, right, name) + def searchsorted(sorted_sequence, values, out_int32=False, From e7070ab1d3a5801a8e11b80599f32e917081fa5a Mon Sep 17 00:00:00 2001 From: Li-fAngyU <972117977@qq.com> Date: Mon, 11 Jul 2022 19:19:07 +0800 Subject: [PATCH 3/5] upgrade unittests/test_bucketize_api.py. --- .../tests/unittests/test_bucketize_api.py | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_bucketize_api.py b/python/paddle/fluid/tests/unittests/test_bucketize_api.py index ee21c3dad866a..ff2ae88d029bb 100644 --- a/python/paddle/fluid/tests/unittests/test_bucketize_api.py +++ b/python/paddle/fluid/tests/unittests/test_bucketize_api.py @@ -13,7 +13,6 @@ # limitations under the License. from __future__ import print_function -from re import X import unittest import numpy as np @@ -45,15 +44,20 @@ def run(place): shape=self.sorted_sequence.shape, dtype="float64") x = paddle.static.data('x', shape=self.x.shape, dtype="float64") - out = paddle.bucketize(x, sorted_sequence) + out1 = paddle.bucketize(x, sorted_sequence) + out2 = paddle.bucketize(x, sorted_sequence, right=True) exe = paddle.static.Executor(place) res = exe.run(feed={ 'SortedSequence': self.sorted_sequence, 'x': self.x }, - fetch_list=out) - out_ref = np.searchsorted(self.sorted_sequence, self.x) - self.assertTrue(np.allclose(out_ref, res)) + fetch_list=[out1, out2]) + out_ref = np.searchsorted(self.sorted_sequence, self.x) + out_ref1 = np.searchsorted(self.sorted_sequence, + self.x, + side='right') + self.assertTrue(np.allclose(out_ref, res[0])) + self.assertTrue(np.allclose(out_ref1, res[1])) for place in self.place: run(place) @@ -64,11 +68,14 @@ def run(place): paddle.disable_static(place) sorted_sequence = paddle.to_tensor(self.sorted_sequence) x = paddle.to_tensor(self.x) - out = paddle.bucketize(x, sorted_sequence, right=True) - out_ref = np.searchsorted(self.sorted_sequence, - self.x, - side='right') - self.assertEqual(np.allclose(out_ref, out.numpy()), True) + out1 = paddle.bucketize(x, sorted_sequence) + out2 = paddle.bucketize(x, sorted_sequence, right=True) + out_ref1 = np.searchsorted(self.sorted_sequence, self.x) + out_ref2 = np.searchsorted(self.sorted_sequence, + self.x, + side='right') + self.assertEqual(np.allclose(out_ref1, out1.numpy()), True) + self.assertEqual(np.allclose(out_ref2, out2.numpy()), True) paddle.enable_static() for place in self.place: @@ -89,6 +96,13 @@ def test_bucketize_dims_error(self): x = paddle.static.data('x', shape=[2, 5], dtype="float64") self.assertRaises(ValueError, paddle.bucketize, x, sorted_sequence) + def test_input_error(self): + for place in self.place: + paddle.disable_static(place) + sorted_sequence = paddle.to_tensor(self.sorted_sequence) + self.assertRaises(ValueError, paddle.bucketize, self.x, + sorted_sequence) + if __name__ == "__main__": unittest.main() From c1b11e121e9fd8b6213ea2f192859dfba5a64d5e Mon Sep 17 00:00:00 2001 From: Li-fAngyU <972117977@qq.com> Date: Wed, 20 Jul 2022 13:52:22 +0800 Subject: [PATCH 4/5] update tests/unittests/test_bucketize_api.py file. --- .../paddle/fluid/tests/unittests/test_bucketize_api.py | 10 ++++++++++ python/paddle/tensor/search.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/tests/unittests/test_bucketize_api.py b/python/paddle/fluid/tests/unittests/test_bucketize_api.py index ff2ae88d029bb..569de1574bac0 100644 --- a/python/paddle/fluid/tests/unittests/test_bucketize_api.py +++ b/python/paddle/fluid/tests/unittests/test_bucketize_api.py @@ -13,6 +13,7 @@ # limitations under the License. from __future__ import print_function +from re import X import unittest import numpy as np @@ -103,6 +104,15 @@ def test_input_error(self): self.assertRaises(ValueError, paddle.bucketize, self.x, sorted_sequence) + def test_empty_input_error(self): + for place in self.place: + paddle.disable_static(place) + sorted_sequence = paddle.to_tensor(self.sorted_sequence) + x = paddle.to_tensor(self.x) + self.assertRaises(ValueError, paddle.bucketize, None, + sorted_sequence) + self.assertRaises(AttributeError, paddle.bucketize, x, None) + if __name__ == "__main__": unittest.main() diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index f0cc2c901e57d..c460adec70161 100644 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -964,7 +964,7 @@ def bucketize(x, sorted_sequence, out_int32=False, right=False, name=None): 'paddle.searchsorted') if sorted_sequence.dim() != 1: raise ValueError( - f"boundaries tensor must be 1 dimension, but got dim {sorted_sequence.dim()}" + f"sorted_sequence tensor must be 1 dimension, but got dim {sorted_sequence.dim()}" ) return searchsorted(sorted_sequence, x, out_int32, right, name) From d2e5a9a26974d7963792b0a3362f48049d59e555 Mon Sep 17 00:00:00 2001 From: Li-fAngyU <56572498+Li-fAngyU@users.noreply.github.com> Date: Fri, 29 Jul 2022 19:27:34 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9bucketize=20API=20?= =?UTF-8?q?=E7=9A=84=E8=8B=B1=E6=96=87=E5=8F=82=E6=95=B0=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/paddle/tensor/search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index c460adec70161..b62c5a2f53a63 100644 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -922,7 +922,7 @@ def bucketize(x, sorted_sequence, out_int32=False, right=False, name=None): x(Tensor): An input N-D tensor value with type int32, int64, float32, float64. sorted_sequence(Tensor): An input 1-D tensor with type int32, int64, float32, float64. The value of the tensor monotonically increases in the innermost dimension. out_int32(bool, optional): Data type of the output tensor which can be int32, int64. The default value is False, and it indicates that the output data type is int64. - right(bool, optional): Find the upper or lower bounds of the sorted_sequence range in the innermost dimension based on the given `values`. If the value of the sorted_sequence is nan or inf, return the size of the innermost dimension. + right(bool, optional): Find the upper or lower bounds of the sorted_sequence range in the innermost dimension based on the given `x`. If the value of the sorted_sequence is nan or inf, return the size of the innermost dimension. The default value is False and it shows the lower bounds. name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`.