diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index eb7532c05499231cd4fb1ca5b8a607ab9972a999..fcafa992beb84736f0cef767afe53325a511c63b 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -116,7 +116,6 @@ __all__ = [ 'sum', 'slice', 'shape', - 'size', 'clip', 'clip_by_norm', 'mean', @@ -6912,53 +6911,6 @@ def shape(input): return out -@deprecated(since="2.0.0", update_to="paddle.numel") -def size(input): - """ - **Size Layer** - - Returns the number of elements for a tensor, which is a int64 Tensor with shape [1]. - - Args: - input (Tensor): The input Tensor, it's data type can be bool, float16, float32, float64, int32, int64. - - Returns: - Tensor: The number of elements for the input Tensor. - - Raises: - TypeError: ``input`` must be a Tensor and the data type of ``input`` must be one of bool, float16, float32, float64, int32, int64. - - Examples: - .. code-block:: python - - import paddle - import paddle.fluid.layers as layers - paddle.enable_static() - - input = layers.data( - name="input", shape=[3, 100], dtype="float32", append_batch_size=False) - rank = layers.size(input) # 300 - """ - - if in_dygraph_mode(): - return _C_ops.numel(input) - - if _in_legacy_dygraph(): - return _legacy_C_ops.size(input) - - check_variable_and_dtype( - input, - 'input', - ['bool', 'float16', 'float32', 'float64', 'int32', 'int64'], - "size", - ) - helper = LayerHelper('size', **locals()) - out = helper.create_variable_for_type_inference(dtype='int64') - helper.append_op(type='size', inputs={'Input': input}, outputs={'Out': out}) - - return out - - def _elementwise_op(helper): op_type = helper.layer_type x = helper.kwargs.get('x', None) diff --git a/python/paddle/fluid/tests/unittests/mlu/test_size_op_mlu.py b/python/paddle/fluid/tests/unittests/mlu/test_size_op_mlu.py index 977359f8a9634df49007f74cd5f35d8b51b13fc3..a0cd8eba6dc5b1c592955b0e64fd3fa84d6b387e 100644 --- a/python/paddle/fluid/tests/unittests/mlu/test_size_op_mlu.py +++ b/python/paddle/fluid/tests/unittests/mlu/test_size_op_mlu.py @@ -71,8 +71,8 @@ class TestSizeAPI(unittest.TestCase): x_2 = paddle.fluid.data(shape=shape2, dtype='int32', name='x_2') input_1 = np.random.random(shape1).astype("int32") input_2 = np.random.random(shape2).astype("int32") - out_1 = paddle.fluid.layers.size(x_1) - out_2 = paddle.fluid.layers.size(x_2) + out_1 = paddle.numel(x_1) + out_2 = paddle.numel(x_2) exe = paddle.static.Executor(place=paddle.MLUPlace(0)) res_1, res_2 = exe.run( feed={ @@ -94,8 +94,8 @@ class TestSizeAPI(unittest.TestCase): input_2 = np.random.random([1, 4, 5]).astype("int32") x_1 = paddle.to_tensor(input_1) x_2 = paddle.to_tensor(input_2) - out_1 = paddle.fluid.layers.size(x_1) - out_2 = paddle.fluid.layers.size(x_2) + out_1 = paddle.numel(x_1) + out_2 = paddle.numel(x_2) assert np.array_equal(out_1.numpy().item(0), np.size(input_1)) assert np.array_equal(out_2.numpy().item(0), np.size(input_2)) paddle.enable_static() @@ -108,7 +108,7 @@ class TestSizeAPI(unittest.TestCase): def test_x_type(): shape = [1, 4, 5] input_1 = np.random.random(shape).astype("int32") - out_1 = paddle.fluid.layers.size(input_1) + out_1 = paddle.numel(input_1) self.assertRaises(TypeError, test_x_type) diff --git a/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py index 1e768a5dd185a4d0acb8970f26a47f58a3f7075e..0b1d87b3dfaf26d2b3c582c9cdda099dd6dd8b57 100755 --- a/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py @@ -100,8 +100,8 @@ class TestSizeAPI(unittest.TestCase): x_2 = paddle.fluid.data(shape=shape2, dtype='int32', name='x_2') input_1 = np.random.random(shape1).astype("int32") input_2 = np.random.random(shape2).astype("int32") - out_1 = paddle.fluid.layers.size(x_1) - out_2 = paddle.fluid.layers.size(x_2) + out_1 = paddle.numel(x_1) + out_2 = paddle.numel(x_2) exe = paddle.static.Executor(place=self.place) res_1, res_2 = exe.run( feed={ @@ -123,8 +123,8 @@ class TestSizeAPI(unittest.TestCase): input_2 = np.random.random([1, 4, 5]).astype("int32") x_1 = paddle.to_tensor(input_1) x_2 = paddle.to_tensor(input_2) - out_1 = paddle.fluid.layers.size(x_1) - out_2 = paddle.fluid.layers.size(x_2) + out_1 = paddle.numel(x_1) + out_2 = paddle.numel(x_2) assert np.array_equal(out_1.numpy().item(0), np.size(input_1)) assert np.array_equal(out_2.numpy().item(0), np.size(input_2)) paddle.enable_static() @@ -137,7 +137,7 @@ class TestSizeAPI(unittest.TestCase): def test_x_type(): shape = [1, 4, 5] input_1 = np.random.random(shape).astype("int32") - out_1 = paddle.fluid.layers.size(input_1) + out_1 = paddle.numel(input_1) self.assertRaises(TypeError, test_x_type) diff --git a/python/paddle/fluid/tests/unittests/test_size_op.py b/python/paddle/fluid/tests/unittests/test_size_op.py index 87fcfdf5a9646b86e5283be9f1d1b3517c78808a..ac070c099e63c02642b135447890068fadc33ca1 100644 --- a/python/paddle/fluid/tests/unittests/test_size_op.py +++ b/python/paddle/fluid/tests/unittests/test_size_op.py @@ -17,6 +17,7 @@ import numpy as np import paddle import paddle.fluid as fluid from op_test import OpTest +import paddle class TestSizeOp(OpTest): @@ -66,8 +67,8 @@ class TestSizeAPI(unittest.TestCase): x_2 = paddle.fluid.data(shape=shape2, dtype='int32', name='x_2') input_1 = np.random.random(shape1).astype("int32") input_2 = np.random.random(shape2).astype("int32") - out_1 = paddle.fluid.layers.size(x_1) - out_2 = paddle.fluid.layers.size(x_2) + out_1 = paddle.numel(x_1) + out_2 = paddle.numel(x_2) exe = paddle.static.Executor(place=paddle.CPUPlace()) res_1, res_2 = exe.run( feed={ @@ -90,8 +91,8 @@ class TestSizeAPI(unittest.TestCase): input_2 = np.random.random([1, 4, 5]).astype("int32") x_1 = paddle.to_tensor(input_1) x_2 = paddle.to_tensor(input_2) - out_1 = paddle.fluid.layers.size(x_1) - out_2 = paddle.fluid.layers.size(x_2) + out_1 = paddle.numel(x_1) + out_2 = paddle.numel(x_2) assert np.array_equal(out_1.numpy().item(0), np.size(input_1)) assert np.array_equal(out_2.numpy().item(0), np.size(input_2)) paddle.enable_static() @@ -104,7 +105,7 @@ class TestSizeAPI(unittest.TestCase): def test_x_type(): shape = [1, 4, 5] input_1 = np.random.random(shape).astype("int32") - out_1 = paddle.fluid.layers.size(input_1) + out_1 = paddle.numel(input_1) self.assertRaises(TypeError, test_x_type)