diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 23e1d7551f947..2e4dc53290226 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -292,6 +292,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 @@ -443,6 +444,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..569de1574bac0 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_bucketize_api.py @@ -0,0 +1,118 @@ +# 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") + 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=[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) + + 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) + 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: + 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) + + 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) + + 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/__init__.py b/python/paddle/tensor/__init__.py index f4820bbbbc1bc..08d54734bfa9d 100755 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -250,6 +250,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 @@ -503,6 +504,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..b62c5a2f53a63 100644 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -914,6 +914,61 @@ def topk(x, k, axis=None, largest=True, sorted=True, name=None): 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 `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`. + + 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"sorted_sequence 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,