未验证 提交 a7c43ffa 编写于 作者: V Vvsmile 提交者: GitHub

[Clean Fluid API]Remove API: pad (#48100)

* replace pad with paddle.nn.functional.pad and fix call arguments

* solve the ImportError in PR-CI-Mac-Python3, which is called "cannot
import name 'assign' from 'paddle.fluid.layers'"

* fix the CI error of remove_pad by remove import paddle.nn.functional as
F

* fix the ImportError of remove_pad

* fix functional call of pad
上级 33d90ae8
......@@ -87,7 +87,6 @@ __all__ = [
'autoincreased_step_counter',
'unsqueeze',
'lod_reset',
'pad',
'image_resize',
'resize_bilinear',
'resize_trilinear',
......@@ -3878,92 +3877,6 @@ def lod_reset(x, y=None, target_lod=None):
return out
def pad(x, paddings, pad_value=0.0, name=None):
r"""
:alias_main: paddle.nn.functional.pad
:alias: paddle.nn.functional.pad,paddle.nn.functional.common.pad
:old_api: paddle.fluid.layers.pad
This op will pad a tensor with a constant value given by :attr:`pad_value`, and the
padded shape is specified by :attr:`paddings`.
Specifically, the number of values padded before the elements of :attr:`x`
in dimension :attr:`i` is indicated by :attr:`paddings[2*i]`, and the number
of values padded after the elements of :attr:`x` in dimension :attr:`i` is
indicated by :attr:`paddings[2*i+1]`.
See below for an example.
.. code-block:: text
Given:
x = [[1, 2], [3, 4]]
paddings = [0, 1, 1, 2]
pad_value = 0
Return:
out = [[0, 1, 2, 0, 0]
[0, 3, 4, 0, 0]
[0, 0, 0, 0, 0]]
Args:
x (Variable): Tensor, data type is float32.
paddings (list): A list of integers. Its elements specify the padded
width before and after each dimension in turn.
The length of :attr:`paddings` must be equal to
:math:`rank(x) \\times 2`.
pad_value (float): The constant value used to pad.
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:
The padded tensor, with the same data type and rank as :attr:`x`
Return Type:
Variable
Examples:
.. code-block:: python
# x is a rank 2 tensor variable
import paddle.fluid as fluid
x = fluid.data(name='data', shape=[300, 300], dtype='float32')
out = fluid.layers.pad(x=x, paddings=[0, 1, 1, 2], pad_value=0.)
"""
check_variable_and_dtype(
x,
'x',
[
'float16',
'float32',
'float64',
'int32',
'int64',
'complex64',
'complex128',
],
"pad",
)
check_type(pad_value, 'pad_value', (float, int, Variable), 'pad')
if isinstance(pad_value, int):
pad_value = float(pad_value)
helper = LayerHelper('pad', **locals())
dtype = helper.input_dtype(input_param_name='x')
out = helper.create_variable_for_type_inference(dtype)
helper.append_op(
type='pad',
inputs={'X': x},
outputs={'Out': out},
attrs={'paddings': paddings, 'pad_value': pad_value},
)
return out
def image_resize(
input,
out_shape=None,
......
......@@ -1990,9 +1990,9 @@ class TrainingHelper(DecodeHelper):
# extend inputs to avoid to slice out of range in `next_inputs`
# may be easier and have better performance than condition_op
self.inputs_ = map_structure(
lambda x: nn.pad(
lambda x: paddle.nn.functional.pad(
x,
paddings=([0, 1] + [0, 0] * (len(x.shape) - 1))
pad=([0, 1] + [0, 0] * (len(x.shape) - 1))
if time_major
else ([0, 0, 0, 1] + [0, 0] * (len(x.shape) - 2)),
),
......
......@@ -17,6 +17,7 @@ import unittest
import numpy as np
from inference_pass_test import InferencePassTest
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.core import AnalysisConfig
......@@ -28,8 +29,8 @@ class PadOpTRTTest(InferencePassTest):
data = fluid.data(
name="data", shape=[1, 3, 128, 128], dtype="float32"
)
pad_out = fluid.layers.pad(
x=data, paddings=[0, 0, 0, 0, 0, 1, 1, 2], pad_value=0.0
pad_out = paddle.nn.functional.pad(
x=data, pad=[0, 0, 0, 0, 0, 1, 1, 2], value=0.0
)
out = fluid.layers.batch_norm(pad_out, is_test=True)
......
......@@ -118,12 +118,12 @@ class TestPadOpError(unittest.TestCase):
input_data = np.random.random((2, 2)).astype("float32")
def test_Variable():
fluid.layers.pad(x=input_data, paddings=[1, 1, 1, 1])
paddle.nn.functional.pad(x=input_data, pad=[1, 1, 1, 1])
self.assertRaises(TypeError, test_Variable)
data = fluid.data(name='data', shape=[4], dtype='float16')
fluid.layers.pad(x=data, paddings=[0, 1])
paddle.nn.functional.pad(x=data, pad=[0, 1])
if __name__ == '__main__':
......
......@@ -112,12 +112,12 @@ class TestPadOpError(unittest.TestCase):
input_data = np.random.random((2, 2)).astype("float32")
def test_Variable():
fluid.layers.pad(x=input_data, paddings=[1, 1, 1, 1])
paddle.nn.functional.pad(x=input_data, pad=[1, 1, 1, 1])
self.assertRaises(TypeError, test_Variable)
data = fluid.data(name='data', shape=[4], dtype='float16')
fluid.layers.pad(x=data, paddings=[0, 1])
paddle.nn.functional.pad(x=data, pad=[0, 1])
class TestPaddingValueTensor(UnittestBase):
......@@ -173,10 +173,8 @@ class TestPaddingValueTensor2(TestPaddingValueTensor):
def call_func(self, x):
padding_value = paddle.assign([1.0])
# test for int value
tmp = paddle.fluid.layers.pad(x, paddings=[1, 1, 1, 1], pad_value=1)
out = paddle.fluid.layers.pad(
x, paddings=[1, 1, 1, 1], pad_value=padding_value
)
tmp = paddle.nn.functional.pad(x, pad=[1, 1, 1, 1], value=1)
out = paddle.nn.functional.pad(x, pad=[1, 1, 1, 1], value=padding_value)
return out
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册