From b9421dc13903d3f15487cf01ec5fdae73e1bb991 Mon Sep 17 00:00:00 2001 From: heyanru <81976792+heyanru01@users.noreply.github.com> Date: Thu, 1 Dec 2022 16:12:27 +0800 Subject: [PATCH] [Fluid Clean] remove pixel_shuffle, fsp_matrix, where, sign, unique, unique_with_counts (#48441) --- python/paddle/fluid/layers/nn.py | 307 ------------------ .../unittests/mlu/test_where_index_op_mlu.py | 4 +- .../unittests/npu/test_where_index_npu.py | 4 +- .../fluid/tests/unittests/test_fsp_op.py | 27 -- .../fluid/tests/unittests/test_layers.py | 11 +- .../fluid/tests/unittests/test_sign_op.py | 8 +- .../fluid/tests/unittests/test_unique.py | 4 +- .../unittests/test_unique_with_counts.py | 5 +- .../fluid/tests/unittests/test_var_base.py | 2 +- .../fluid/tests/unittests/test_where_index.py | 123 ------- .../unittests/xpu/test_where_index_xpu.py | 4 +- python/paddle/fluid/variable_index.py | 6 +- 12 files changed, 19 insertions(+), 486 deletions(-) delete mode 100644 python/paddle/fluid/tests/unittests/test_where_index.py diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 03059afb19..5ab7f3fbdc 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -96,8 +96,6 @@ __all__ = [ 'resize_trilinear', 'resize_nearest', 'relu', - 'unique', - 'unique_with_counts', 'elementwise_add', 'elementwise_div', 'elementwise_sub', @@ -117,11 +115,7 @@ __all__ = [ 'get_tensor_from_selected_rows', 'temporal_shift', 'py_func', - 'pixel_shuffle', - 'fsp_matrix', 'continuous_value_model', - 'where', - 'sign', 'unfold', 'deformable_roi_pooling', 'shard_index', @@ -7020,121 +7014,6 @@ py_func.registered_func = PyFuncRegistry.registered_func py_func.registered_func_num = PyFuncRegistry.registered_func_num -def pixel_shuffle(x, upscale_factor): - """ - - This op rearranges elements in a tensor of shape [N, C, H, W] - to a tensor of shape [N, C/r**2, H*r, W*r]. - This is useful for implementing efficient sub-pixel convolution - with a stride of 1/r. - Please refer to the paper: `Real-Time Single Image and Video Super-Resolution - Using an Efficient Sub-Pixel Convolutional Neural Network `_ . - by Shi et. al (2016) for more details. - - Parameters: - - x(Variable): 4-D tensor, the data type should be float32 or float64. - upscale_factor(int): factor to increase spatial resolution. - - Returns: - Out(Variable): Reshaped tensor according to the new dimension. - - Raises: - ValueError: If the square of upscale_factor cannot divide the channels of input. - - Examples: - .. code-block:: python - - # declarative mode - import paddle.fluid as fluid - import numpy as np - input = fluid.data(name="input", shape=[2,9,4,4]) - output = fluid.layers.pixel_shuffle(x=input, upscale_factor=3) - place = fluid.CPUPlace() - exe = fluid.Executor(place) - exe.run(fluid.default_startup_program()) - - input_data = np.random.rand(2,9,4,4).astype("float32") - output_data = exe.run(fluid.default_main_program(), - feed={"input":input_data}, - fetch_list=[output], - return_numpy=True) - - # print(output.shape) - # (2L, 1L, 12L, 12L) - - """ - - check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'pixel_shuffle') - helper = LayerHelper("pixel_shuffle", **locals()) - - out = helper.create_variable_for_type_inference(dtype=x.dtype) - - if not isinstance(upscale_factor, int): - raise TypeError("upscale factor must be int type") - - helper.append_op( - type="pixel_shuffle", - inputs={"X": x}, - outputs={"Out": out}, - attrs={"upscale_factor": upscale_factor}, - ) - return out - - -def fsp_matrix(x, y): - """ - - **FSP matrix op** - - This op is used to calculate the flow of solution procedure (FSP) matrix of two 4-D Tensor feature maps. - Given feature map x with shape [x_channel, h, w] and feature map y with shape - [y_channel, h, w], we can get the fsp matrix of x and y in two steps: - - 1. reshape x into matrix with shape [x_channel, h * w] and reshape and - transpose y into matrix with shape [h * w, y_channel]. - 2. multiply x and y to get fsp matrix with shape [x_channel, y_channel]. - - The output is a batch of fsp matrices. - - Args: - - x (Variable): A 4-D Tensor feature map with shape [batch_size, x_channel, height, width]. - A Tensor with type float32, float64. - y (Variable): A 4-D Tensor feature map with shape [batch_size, y_channel, height, width]. - The y_channel can be different with the x_channel of Input(X) - while the other dimensions must be the same with Input(X)'s. A Tensor with - type float32, float64. - - Returns: - - fsp matrix (Variable): The output of FSP op with shape [batch_size, x_channel, y_channel]. - The x_channel is the channel of x and the y_channel is the channel of y. A Tensor with - type float32, float64. - - Examples: - - .. code-block:: python - - import paddle.fluid as fluid - data = fluid.data(name='data', shape=[None, 3, 32, 32]) - feature_map_0 = fluid.layers.conv2d(data, num_filters=2, - filter_size=3) - feature_map_1 = fluid.layers.conv2d(feature_map_0, num_filters=2, - filter_size=1) - loss = fluid.layers.fsp_matrix(feature_map_0, feature_map_1) - - """ - check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'fsp_matrix') - check_variable_and_dtype(y, 'y', ['float32', 'float64'], 'fsp_matrix') - helper = LayerHelper('fsp_matrix', **locals()) - out = helper.create_variable_for_type_inference( - dtype=helper.input_dtype(input_param_name='x') - ) - helper.append_op(type='fsp', inputs={'X': x, 'Y': y}, outputs={'Out': out}) - return out - - def continuous_value_model(input, cvm, use_cvm=True): r""" @@ -7192,192 +7071,6 @@ def continuous_value_model(input, cvm, use_cvm=True): return out -def where(condition): - """ - Return an int64 tensor with rank 2, specifying the coordinate of true element in `condition`. - - Args: - condition(Variable): A bool tensor with rank at least 1, the data type is bool. - - Returns: - Variable, the output data type is int64. : The tensor variable storing a 2-D tensor, which involves all coordinate. - - Examples: - .. code-block:: python - - import paddle.fluid as fluid - import paddle.fluid.layers as layers - import numpy as np - - # condition is a tensor [True, False, True] - condition = layers.assign(np.array([1, 0, 1], dtype='int32')) - condition = layers.cast(condition, 'bool') - out = layers.where(condition) # [[0], [2]] - - # condition is a tensor [[True, False], [False, True]] - condition = layers.assign(np.array([[1, 0], [0, 1]], dtype='int32')) - condition = layers.cast(condition, 'bool') - out = layers.where(condition) # [[0, 0], [1, 1]] - - # condition is a tensor [False, False, False] - condition = layers.assign(np.array([0, 0, 0], dtype='int32')) - condition = layers.cast(condition, 'bool') - out = layers.where(condition) # [[]] - - """ - - if in_dygraph_mode(): - return _C_ops.nonzero(condition) - if _in_legacy_dygraph(): - return _legacy_C_ops.where_index(condition) - - helper = LayerHelper("where_index", **locals()) - - out = helper.create_variable_for_type_inference( - dtype=core.VarDesc.VarType.INT64 - ) - - helper.append_op( - type='where_index', - inputs={'Condition': condition}, - outputs={'Out': [out]}, - ) - return out - - -@deprecated(since="2.0.0", update_to="paddle.sign") -def sign(x): - r""" - This OP returns sign of every element in `x`: 1 for positive, -1 for negative and 0 for zero. - - Args: - x(Variable|numpy.ndarray): The input variable could be N-D tensor or N-D numpy array, \ - the input data type is float32 or float64. - - Returns: - Variable, the output data type is the same as input data type. : The output sign tensor with identical shape to input :attr:`x`. - - Examples: - .. code-block:: python - - import paddle.fluid as fluid - import numpy as np - - # [1.0, 0.0, -1.0] - data = fluid.layers.sign(np.array([3.0, 0.0, -2.0], dtype='float32')) - """ - - helper = LayerHelper("sign", **locals()) - check_type(x, 'x', (Variable, np.ndarray), 'sign') - if isinstance(x, np.ndarray): - x = assign(x) - check_dtype(x.dtype, 'x', ['float16', 'float32', 'float64'], 'sign') - out = helper.create_variable_for_type_inference(dtype=x.dtype) - - helper.append_op(type='sign', inputs={'X': [x]}, outputs={'Out': [out]}) - - return out - - -def unique(x, dtype='int32'): - r""" - Return a unique tensor for `x` and an index tensor pointing to this unique tensor. - - Args: - x(Tensor): A 1-D input tensor, it's data type should be float32, float64, int32, int64. - dtype(np.dtype|str, optional): The type of index tensor: int32, int64. Default: int32. - - Returns: - tuple: (out, index). `out` is the unique tensor for `x`, with identical dtype to `x`, and \ - `index` is an index tensor pointing to `out`, by which user can recover the original `x` tensor. - - Examples: - .. code-block:: python - - import numpy as np - import paddle.fluid as fluid - x = fluid.layers.assign(np.array([2, 3, 3, 1, 5, 3], dtype='int32')) - out, index = fluid.layers.unique(x) # out is [2, 3, 1, 5]; index is [0, 1, 1, 2, 3, 1] - """ - - check_variable_and_dtype( - x, "x", ['float32', 'float64', 'int32', 'int64'], "unique" - ) - helper = LayerHelper("unique", **locals()) - - out = helper.create_variable_for_type_inference(dtype=x.dtype) - - index = helper.create_variable_for_type_inference(dtype) - - helper.append_op( - type='unique', - inputs={'X': x}, - attrs={'dtype': convert_np_dtype_to_dtype_(dtype)}, - outputs={'Out': [out], 'Index': [index]}, - ) - - return out, index - - -def unique_with_counts(x, dtype='int32'): - r""" - This OP return a unique tensor for `x` , and count tensor that the count of unique result in raw input, \ - and an index tensor pointing to this unique tensor. - - **NOTICE**: This op support the variable type of Tensor only. - - Args: - x(Variable): A 1-D input tensor with input shape of :math:`[N]` , the input data type is float32, float64, int32, int64. - dtype(np.dtype|core.VarDesc.VarType|str): The type of count and index tensor, it could be int32, int64. Default value is int32. - - Returns: - tuple, the variable type in tuple is Tensor, the output :attr:`out` data type is the same as input :attr:`x`, \ - and data type of output :attr:`index` and :attr:`count` will be int32 or int64.: The :attr:`out` is unique tensor for input :attr:`x`,\ - the data shape is :math:`[K]`, the `K` may be different to the `N` in shape of :attr:`x`. :attr:`index` is an index tensor pointing\ - to :attr:`out`, the data shape is :math:`[N]` , the data shape is the same as input :attr:`x`. :attr:`count` is count of unique element in\ - the :attr:`x`, the data shape is :math:`[K]`, the data shape is the same as output :attr:`out`. - - Examples: - .. code-block:: python - - import numpy as np - import paddle.fluid as fluid - x = fluid.layers.assign(np.array([2, 3, 3, 1, 5, 3], dtype='int32')) - out, index, count = fluid.layers.unique_with_counts(x) # out is [2, 3, 1, 5]; index is [0, 1, 1, 2, 3, 1] - # count is [1, 3, 1, 1] - # x.shape=(6,) out.shape=(4,), index.shape=(6,), count.shape=(4,) - """ - check_variable_and_dtype( - x, "x", ['float32', 'float64', 'int32', 'int64'], "unique_with_counts" - ) - if not (dtype == 'int32' or dtype == 'int64'): - raise TypeError( - "Op unique_with_counts, index dtype must be int32 or int64" - ) - - if x is None or len(x.shape) != 1: - raise ValueError( - "Op unique_with_counts, x must not be null and size of dim must be 1" - ) - - helper = LayerHelper("unique_with_counts", **locals()) - - out = helper.create_variable_for_type_inference(dtype=x.dtype) - - index = helper.create_variable_for_type_inference(dtype) - - count = helper.create_variable_for_type_inference(dtype) - - helper.append_op( - type='unique_with_counts', - inputs={'X': x}, - attrs={'dtype': convert_np_dtype_to_dtype_(dtype)}, - outputs={'Out': [out], 'Index': [index], 'Count': [count]}, - ) - - return out, index, count - - def unfold(x, kernel_sizes, strides=1, paddings=0, dilations=1, name=None): r""" diff --git a/python/paddle/fluid/tests/unittests/mlu/test_where_index_op_mlu.py b/python/paddle/fluid/tests/unittests/mlu/test_where_index_op_mlu.py index 163d47e33c..877207eab7 100644 --- a/python/paddle/fluid/tests/unittests/mlu/test_where_index_op_mlu.py +++ b/python/paddle/fluid/tests/unittests/mlu/test_where_index_op_mlu.py @@ -108,7 +108,7 @@ class TestWhereOpError(unittest.TestCase): def test_api(self): with program_guard(Program(), Program()): cond = fluid.layers.data(name='cond', shape=[4], dtype='bool') - result = fluid.layers.where(cond) + result = paddle.nonzero(cond) exe = fluid.Executor(paddle.device.MLUPlace(0)) exe.run(fluid.default_startup_program()) @@ -119,7 +119,7 @@ class TestWhereOpError(unittest.TestCase): class TestWhereRaiseError(unittest.TestCase): def test_errors(self): def test_type(): - fluid.layers.where([10]) + paddle.nonzero([10]) self.assertRaises(TypeError, test_type) diff --git a/python/paddle/fluid/tests/unittests/npu/test_where_index_npu.py b/python/paddle/fluid/tests/unittests/npu/test_where_index_npu.py index 370a1934bf..315f475a5c 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_where_index_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_where_index_npu.py @@ -98,7 +98,7 @@ class TestWhereOpError(unittest.TestCase): def test_api(self): with program_guard(Program(), Program()): cond = fluid.layers.data(name='cond', shape=[4], dtype='bool') - result = fluid.layers.where(cond) + result = paddle.nonzero(cond) exe = fluid.Executor(paddle.NPUPlace(0)) exe.run(fluid.default_startup_program()) @@ -109,7 +109,7 @@ class TestWhereOpError(unittest.TestCase): class TestWhereRaiseError(unittest.TestCase): def test_errors(self): def test_type(): - fluid.layers.where([10]) + paddle.nonzero([10]) self.assertRaises(TypeError, test_type) diff --git a/python/paddle/fluid/tests/unittests/test_fsp_op.py b/python/paddle/fluid/tests/unittests/test_fsp_op.py index 3e325d1891..abeaae9f24 100644 --- a/python/paddle/fluid/tests/unittests/test_fsp_op.py +++ b/python/paddle/fluid/tests/unittests/test_fsp_op.py @@ -17,8 +17,6 @@ import unittest import numpy as np from op_test import OpTest -import paddle.fluid as fluid - def fsp_matrix(a, b): batch = a.shape[0] @@ -62,30 +60,5 @@ class TestFSPOp(OpTest): self.check_grad(['X', 'Y'], 'Out') -class BadInputTest(unittest.TestCase): - def test_error(self): - with fluid.program_guard(fluid.Program()): - - def test_bad_x(): - data = fluid.layers.data(name='data', shape=[3, 32, 32]) - feature_map_0 = [1, 2, 3] - feature_map_1 = fluid.layers.conv2d( - data, num_filters=2, filter_size=3 - ) - loss = fluid.layers.fsp_matrix(feature_map_0, feature_map_1) - - self.assertRaises(TypeError, test_bad_x) - - def test_bad_y(): - data = fluid.layers.data(name='data', shape=[3, 32, 32]) - feature_map_0 = fluid.layers.conv2d( - data, num_filters=2, filter_size=3 - ) - feature_map_1 = [1, 2, 3] - loss = fluid.layers.fsp_matrix(feature_map_0, feature_map_1) - - self.assertRaises(TypeError, test_bad_y) - - if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 7750f6b613..912382f49a 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -3677,21 +3677,12 @@ class TestBook(LayerTest): out = layers.temporal_shift(x, seg_num=2, shift_ratio=0.2) return out - def make_fsp_matrix(self): - with program_guard( - fluid.default_main_program(), fluid.default_startup_program() - ): - x = self._get_data(name="X", shape=[16, 4, 4], dtype="float32") - y = self._get_data(name="Y", shape=[8, 4, 4], dtype="float32") - out = layers.fsp_matrix(x, y) - return out - def make_pixel_shuffle(self): with program_guard( fluid.default_main_program(), fluid.default_startup_program() ): x = self._get_data(name="X", shape=[9, 4, 4], dtype="float32") - out = layers.pixel_shuffle(x, upscale_factor=3) + out = paddle.nn.functional.pixel_shuffle(x, upscale_factor=3) return out def make_mse_loss(self): diff --git a/python/paddle/fluid/tests/unittests/test_sign_op.py b/python/paddle/fluid/tests/unittests/test_sign_op.py index c48de7d586..33e4ca795b 100644 --- a/python/paddle/fluid/tests/unittests/test_sign_op.py +++ b/python/paddle/fluid/tests/unittests/test_sign_op.py @@ -46,7 +46,7 @@ class TestSignOpError(unittest.TestCase): with program_guard(Program(), Program()): # The input type of sign_op must be Variable or numpy.ndarray. input1 = 12 - self.assertRaises(TypeError, fluid.layers.sign, input1) + self.assertRaises(TypeError, paddle.sign, input1) # The input dtype of sign_op must be float16, float32, float64. input2 = fluid.layers.data( name='input2', shape=[12, 10], dtype="int32" @@ -54,12 +54,12 @@ class TestSignOpError(unittest.TestCase): input3 = fluid.layers.data( name='input3', shape=[12, 10], dtype="int64" ) - self.assertRaises(TypeError, fluid.layers.sign, input2) - self.assertRaises(TypeError, fluid.layers.sign, input3) + self.assertRaises(TypeError, paddle.sign, input2) + self.assertRaises(TypeError, paddle.sign, input3) input4 = fluid.layers.data( name='input4', shape=[4], dtype="float16" ) - fluid.layers.sign(input4) + paddle.sign(input4) class TestSignAPI(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/test_unique.py b/python/paddle/fluid/tests/unittests/test_unique.py index c56ec313a3..d454d96a44 100644 --- a/python/paddle/fluid/tests/unittests/test_unique.py +++ b/python/paddle/fluid/tests/unittests/test_unique.py @@ -74,13 +74,13 @@ class TestRandom(TestUniqueOp): class TestUniqueRaiseError(unittest.TestCase): def test_errors(self): def test_type(): - fluid.layers.unique([10]) + paddle.unique([10]) self.assertRaises(TypeError, test_type) def test_dtype(): data = fluid.data(shape=[10], dtype="float16", name="input") - fluid.layers.unique(data) + paddle.unique(data) self.assertRaises(TypeError, test_dtype) diff --git a/python/paddle/fluid/tests/unittests/test_unique_with_counts.py b/python/paddle/fluid/tests/unittests/test_unique_with_counts.py index cc72b9e11d..ccbf56c664 100644 --- a/python/paddle/fluid/tests/unittests/test_unique_with_counts.py +++ b/python/paddle/fluid/tests/unittests/test_unique_with_counts.py @@ -17,6 +17,7 @@ import unittest import numpy as np from op_test import OpTest +import paddle import paddle.fluid as fluid import paddle.fluid.core as core @@ -82,13 +83,13 @@ class TestRandom(TestUniqueWithCountsOp): class TestUniqueWithCountsRaiseError(unittest.TestCase): def test_errors(self): def test_type(): - fluid.layers.unique_with_counts([10]) + paddle.unique([10]) self.assertRaises(TypeError, test_type) def test_dtype(): data = fluid.data(shape=[10], dtype="float16", name="input") - fluid.layers.unique_with_counts(data) + paddle.unique(data) self.assertRaises(TypeError, test_dtype) diff --git a/python/paddle/fluid/tests/unittests/test_var_base.py b/python/paddle/fluid/tests/unittests/test_var_base.py index 439ff3d2b8..62b66be093 100644 --- a/python/paddle/fluid/tests/unittests/test_var_base.py +++ b/python/paddle/fluid/tests/unittests/test_var_base.py @@ -1289,7 +1289,7 @@ class TestVarBase(unittest.TestCase): def func_test_tensor_str_shape_with_zero(self): paddle.disable_static(paddle.CPUPlace()) x = paddle.ones((10, 10)) - y = paddle.fluid.layers.where(x == 0) + y = paddle.nonzero(x == 0) a_str = str(y) expected = '''Tensor(shape=[0, 2], dtype=int64, place=Place(cpu), stop_gradient=True, diff --git a/python/paddle/fluid/tests/unittests/test_where_index.py b/python/paddle/fluid/tests/unittests/test_where_index.py deleted file mode 100644 index d6960621d7..0000000000 --- a/python/paddle/fluid/tests/unittests/test_where_index.py +++ /dev/null @@ -1,123 +0,0 @@ -# Copyright (c) 2019 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 -from op_test import OpTest - -import paddle.fluid as fluid -import paddle.fluid.core as core -from paddle.fluid import Program, program_guard -from paddle.fluid.op import Operator - - -class TestWhereIndexOp(OpTest): - def setUp(self): - self.op_type = "where_index" - self.init_config() - - def test_check_output(self): - self.check_output() - - def init_config(self): - self.inputs = { - 'Condition': np.array([True, False, True]), - } - - self.outputs = {'Out': np.array([[0], [2]], dtype='int64')} - - -class TestAllFalse(unittest.TestCase): - def setUp(self): - self.op_type = "where_index" - self.init_config() - - def check_with_place(self, place): - scope = core.Scope() - condition = scope.var('Condition').get_tensor() - condition.set(self.cond_data, place) - - out = scope.var("Out").get_tensor() - out.set(np.full(self.shape, 0).astype('int64'), place) - - op = Operator("where_index", Condition="Condition", Out="Out") - op.run(scope, place) - - out_array = np.array(out) - self.assertTrue((out_array == self.out_data).all()) - - def init_config(self): - self.cond_data = np.array([False, False, False]) - self.shape = (3, 1) - self.out_data = np.array([], dtype='int64') - - def test_all_false(self): - self.check_with_place(core.CPUPlace()) - - if core.is_compiled_with_cuda(): - self.check_with_place(core.CUDAPlace(0)) - - -class TestRank2(TestWhereIndexOp): - def init_config(self): - self.inputs = { - 'Condition': np.array([[True, False], [False, True]]), - } - - self.outputs = {'Out': np.array([[0, 0], [1, 1]], dtype='int64')} - - -class TestRank3(TestWhereIndexOp): - def init_config(self): - self.inputs = { - 'Condition': np.array( - [ - [[True, False], [False, True]], - [[False, True], [True, False]], - [[False, False], [False, True]], - ] - ), - } - - self.outputs = { - 'Out': np.array( - [[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0], [2, 1, 1]], - dtype='int64', - ) - } - - -class TestWhereOpError(unittest.TestCase): - def test_api(self): - with program_guard(Program(), Program()): - cond = fluid.layers.data(name='cond', shape=[4], dtype='bool') - result = fluid.layers.where(cond) - - exe = fluid.Executor(fluid.CPUPlace()) - exe.run(fluid.default_startup_program()) - cond_i = np.array([True, False, False, False]).astype("bool") - out = exe.run(fluid.default_main_program(), feed={'cond': cond_i}) - - -class TestWhereRaiseError(unittest.TestCase): - def test_errors(self): - def test_type(): - fluid.layers.where([10]) - - self.assertRaises(TypeError, test_type) - - -if __name__ == "__main__": - unittest.main() diff --git a/python/paddle/fluid/tests/unittests/xpu/test_where_index_xpu.py b/python/paddle/fluid/tests/unittests/xpu/test_where_index_xpu.py index 939a586864..1c74bd715a 100644 --- a/python/paddle/fluid/tests/unittests/xpu/test_where_index_xpu.py +++ b/python/paddle/fluid/tests/unittests/xpu/test_where_index_xpu.py @@ -102,7 +102,7 @@ class TestWhereOpError(unittest.TestCase): def test_api(self): with program_guard(Program(), Program()): cond = fluid.layers.data(name='cond', shape=[4], dtype='bool') - result = fluid.layers.where(cond) + result = paddle.nonzero(cond) exe = fluid.Executor(paddle.XPUPlace(0)) exe.run(fluid.default_startup_program()) @@ -113,7 +113,7 @@ class TestWhereOpError(unittest.TestCase): class TestWhereRaiseError(unittest.TestCase): def test_errors(self): def test_type(): - fluid.layers.where([10]) + paddle.nonzero([10]) self.assertRaises(TypeError, test_type) diff --git a/python/paddle/fluid/variable_index.py b/python/paddle/fluid/variable_index.py index 552fb7a9aa..cf298501a2 100644 --- a/python/paddle/fluid/variable_index.py +++ b/python/paddle/fluid/variable_index.py @@ -331,10 +331,9 @@ def get_value_for_bool_tensor(var, item): ) def idx_not_empty(var, item): - from .layers.nn import where from ..tensor import gather_nd - bool_2_idx = where(item == True) + bool_2_idx = paddle.nonzero(item == True) return gather_nd(var, bool_2_idx) def idx_empty(var): @@ -864,13 +863,12 @@ def set_value_for_bool_tensor(var, item, value): def idx_not_empty(var, item, value): from .framework import Variable from .layers import assign - from .layers.nn import where from ..tensor import gather_nd, scatter_nd_add if not isinstance(value, Variable): value = assign(value).cast(var.dtype) - idx = where(item) + idx = paddle.nonzero(item) gather_val = gather_nd(var, idx) gather_val_new = value - gather_val out = scatter_nd_add(var, idx, gather_val_new) -- GitLab