Skip to content

Commit 7ad39c4

Browse files
author
chengduo
authored
Enhance pad_constant_like_op (#12999)
* enhance pad_constant_like_op * add API * add API
1 parent 0353edd commit 7ad39c4

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

paddle/fluid/API.spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ paddle.fluid.layers.reshape ArgSpec(args=['x', 'shape', 'actual_shape', 'act', '
147147
paddle.fluid.layers.lod_reset ArgSpec(args=['x', 'y', 'target_lod'], varargs=None, keywords=None, defaults=(None, None))
148148
paddle.fluid.layers.lrn ArgSpec(args=['input', 'n', 'k', 'alpha', 'beta', 'name'], varargs=None, keywords=None, defaults=(5, 1.0, 0.0001, 0.75, None))
149149
paddle.fluid.layers.pad ArgSpec(args=['x', 'paddings', 'pad_value', 'name'], varargs=None, keywords=None, defaults=(0.0, None))
150+
paddle.fluid.layers.pad_constant_like ArgSpec(args=['x', 'y', 'pad_value', 'name'], varargs=None, keywords=None, defaults=(0.0, None))
150151
paddle.fluid.layers.label_smooth ArgSpec(args=['label', 'prior_dist', 'epsilon', 'dtype', 'name'], varargs=None, keywords=None, defaults=(None, 0.1, 'float32', None))
151152
paddle.fluid.layers.roi_pool ArgSpec(args=['input', 'rois', 'pooled_height', 'pooled_width', 'spatial_scale'], varargs=None, keywords=None, defaults=(1, 1, 1.0))
152153
paddle.fluid.layers.dice_loss ArgSpec(args=['input', 'label', 'epsilon'], varargs=None, keywords=None, defaults=(1e-05,))

paddle/fluid/operators/pad_constant_like_op.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ class PadConstantLikeOp : public framework::OperatorWithKernel {
4343
ctx->SetOutputDim("Out", x_dim);
4444
ctx->ShareLoD("X", /*->*/ "Out");
4545
}
46+
47+
protected:
48+
framework::OpKernelType GetExpectedKernelType(
49+
const framework::ExecutionContext &ctx) const override {
50+
return framework::OpKernelType(
51+
framework::ToDataType(ctx.Input<Tensor>("Y")->type()),
52+
ctx.device_context());
53+
}
4654
};
4755

4856
class PadConstantLikeOpMaker : public framework::OpProtoAndCheckerMaker {
@@ -159,6 +167,14 @@ class PadConstantLikeOpGrad : public framework::OperatorWithKernel {
159167
}
160168
}
161169
}
170+
171+
protected:
172+
framework::OpKernelType GetExpectedKernelType(
173+
const framework::ExecutionContext &ctx) const override {
174+
return framework::OpKernelType(
175+
framework::ToDataType(ctx.Input<Tensor>("Y")->type()),
176+
ctx.device_context());
177+
}
162178
};
163179

164180
class PadConstantLikeOpGradMaker : public framework::SingleGradOpDescMaker {

python/paddle/fluid/layers/nn.py

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
'lod_reset',
8989
'lrn',
9090
'pad',
91+
'pad_constant_like',
9192
'label_smooth',
9293
'roi_pool',
9394
'dice_loss',
@@ -4755,6 +4756,86 @@ def pad(x, paddings, pad_value=0., name=None):
47554756
return out
47564757

47574758

4759+
def pad_constant_like(x, y, pad_value=0., name=None):
4760+
"""
4761+
Pad input(Y) with :attr:`pad_value`, the number of values padded to
4762+
the edges of each axis is specified by the difference of the shape
4763+
of X and Y. ((0, shape_x_0 - shape_y_0), ... (0, shape_x_n - shape_y_n))
4764+
unique pad widths for each axis. The input should be a k-D
4765+
tensor(k > 0 and k < 7).
4766+
4767+
See below for an example.
4768+
4769+
.. code-block:: text
4770+
4771+
Given:
4772+
X = [[[[ 0, 1, 2],
4773+
[ 3, 4, 5]],
4774+
[[ 6, 7, 8],
4775+
[ 9, 10, 11]],
4776+
[[12, 13, 14],
4777+
[15, 16, 17]]],
4778+
[[[18, 19, 20],
4779+
[21, 22, 23]],
4780+
[[24, 25, 26],
4781+
[27, 28, 29]],
4782+
[[30, 31, 32],
4783+
[33, 34, 35]]]]
4784+
X.shape = (2, 3, 2, 3)
4785+
4786+
Y = [[[[35, 36, 37]],
4787+
[[38, 39, 40]],
4788+
[[41, 42, 43]]]]
4789+
Y.shape = (1, 3, 1, 3)
4790+
4791+
And
4792+
pad_value = -1,
4793+
4794+
Return:
4795+
Out = [[[[35, 36, 37],
4796+
[-1, -1, -1]],
4797+
[[38, 39, 40],
4798+
[-1, -1, -1]],
4799+
[[41, 42, 43],
4800+
[-1, -1, -1]]],
4801+
[[[-1, -1, -1],
4802+
[-1, -1, -1]],
4803+
[[-1, -1, -1],
4804+
[-1, -1, -1]],
4805+
[[-1, -1, -1],
4806+
[-1, -1, -1]]]]
4807+
Out.shape = (2, 3, 2, 3)
4808+
4809+
Args:
4810+
x (Variable): The input tensor variable.
4811+
y (Variable): The input tensor variable.
4812+
pad_value (float): The constant value used to pad.
4813+
name(str|None): A name for this layer(optional). If set None, the layer
4814+
will be named automatically.
4815+
4816+
Returns:
4817+
Variable: The padded tensor variable.
4818+
4819+
Examples:
4820+
.. code-block:: python
4821+
4822+
# x is a rank 4 tensor variable, x.shape = (2, 3, 2, 3)
4823+
# y is a rank 4 tensor variable, y.shape = (1, 3, 1, 3)
4824+
out = fluid.layers.pad_constant_like(x=x, y=y, pad_value=0.)
4825+
# out is a rank 4 tensor variable, and out.shape = [2, 3 ,2 , 3]
4826+
"""
4827+
helper = LayerHelper('pad_constant_like', input=x, **locals())
4828+
dtype = helper.input_dtype()
4829+
out = helper.create_tmp_variable(dtype)
4830+
helper.append_op(
4831+
type='pad_constant_like',
4832+
inputs={'X': x,
4833+
'Y': y},
4834+
outputs={'Out': out},
4835+
attrs={'pad_value': float(pad_value)})
4836+
return out
4837+
4838+
47584839
def label_smooth(label,
47594840
prior_dist=None,
47604841
epsilon=0.1,
@@ -5351,7 +5432,7 @@ def crop(x, shape=None, offsets=None, name=None):
53515432
helper = LayerHelper('crop', **locals())
53525433

53535434
if not (isinstance(shape, list) or isinstance(shape, tuple) or \
5354-
isinstance(shape, Variable)):
5435+
isinstance(shape, Variable)):
53555436
raise ValueError("The shape should be a list, tuple or Variable.")
53565437

53575438
if offsets is None:

0 commit comments

Comments
 (0)