Skip to content

Commit

Permalink
add reverse yaml (#44518)
Browse files Browse the repository at this point in the history
* add reverse yaml
  • Loading branch information
wanghuancoder authored Jul 26, 2022
1 parent e0dd7f3 commit 6198ff2
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
18 changes: 18 additions & 0 deletions paddle/phi/api/yaml/legacy_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,24 @@
intermediate : xshape
backward: reshape_grad

- api : reverse
args : (Tensor x, int[] axis)
output : Tensor
infer_meta :
func : ReverseInferMeta
kernel :
func : reverse
backward : reverse_grad

- api : reverse_array
args : (Tensor[] x, int[] axis)
output : Tensor[]{x.size()}
infer_meta :
func : ReverseArrayInferMeta
kernel :
func : reverse_array
backward : reverse_array_grad

- api : roi_align
args : (Tensor x, Tensor boxes, Tensor boxes_num, int pooled_height, int pooled_width, float spatial_scale, int sampling_ratio, bool aligned)
output : Tensor
Expand Down
17 changes: 17 additions & 0 deletions paddle/phi/api/yaml/legacy_backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,23 @@
backward : reshape_double_grad
inplace : (out_grad -> x_grad)

- backward_api : reverse_array_grad
forward : reverse_array (Tensor[] x, int[] axis) -> Tensor[](out)
args : (Tensor[] out_grad, int[] axis)
output : Tensor[](x_grad){out_grad.size()}
infer_meta :
func : ReverseArrayInferMeta
kernel :
func : reverse

- backward_api : reverse_grad
forward : reverse (Tensor x, int[] axis) -> Tensor(out)
args : (Tensor out_grad, int[] axis)
output : Tensor(x_grad)
infer_meta :
func : ReverseInferMeta
invoke : reverse(out_grad, axis)

- backward_api : roi_align_grad
forward : roi_align (Tensor x, Tensor boxes, Tensor boxes_num, int pooled_height, int pooled_width, float spatial_scale, int sampling_ratio, bool aligned) -> Tensor(out)
args : (Tensor x, Tensor boxes, Tensor boxes_num, Tensor out_grad, int pooled_height, int pooled_width, float spatial_scale, int sampling_ratio, bool aligned)
Expand Down
19 changes: 19 additions & 0 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,25 @@ void ReverseInferMeta(const MetaTensor& x,
out->share_meta(x);
}

void ReverseArrayInferMeta(const std::vector<const phi::MetaTensor*>& x,
const std::vector<int>& axis,
std::vector<phi::MetaTensor*> out) {
PADDLE_ENFORCE_EQ(
axis.size(),
1,
phi::errors::InvalidArgument(
"The size of axis must be 1 when the Input(X) is LoDTensorArray, "
"but received %d.",
axis.size()));
PADDLE_ENFORCE_EQ(
axis[0],
0,
phi::errors::InvalidArgument("The value of axis should be 1 when "
"the Input(X) is LoDTensorArray, "
"but received %d.",
axis[0]));
}

void RollInferMeta(const MetaTensor& x,
const IntArray& shifts,
const std::vector<int64_t>& axis,
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/infermeta/unary.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ void ReverseInferMeta(const MetaTensor& x,
const std::vector<int>& axis,
MetaTensor* out);

void ReverseArrayInferMeta(const std::vector<const phi::MetaTensor*>& x,
const std::vector<int>& axis,
std::vector<phi::MetaTensor*> out);

void RollInferMeta(const MetaTensor& x,
const IntArray& shifts,
const std::vector<int64_t>& axis,
Expand Down
5 changes: 5 additions & 0 deletions python/paddle/fluid/layers/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,11 @@ def reverse(x, axis):
check_type(axis, 'axis', (int, tuple, list), 'reverse')
if isinstance(axis, int):
axis = [axis]
if in_dygraph_mode():
if x.type == core.VarDesc.VarType.LOD_TENSOR_ARRAY:
return _C_ops.final_state_reverse_array(x, axis)
else:
return _C_ops.final_state_reverse(x, axis)
helper = LayerHelper("reverse", **locals())
out = helper.create_variable_for_type_inference(dtype=x.dtype)
helper.append_op(type='reverse',
Expand Down
5 changes: 3 additions & 2 deletions python/paddle/fluid/tests/unittests/test_reverse_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def initTestCase(self):
def setUp(self):
self.initTestCase()
self.op_type = "reverse"
self.python_api = fluid.layers.reverse
self.inputs = {"X": self.x}
self.attrs = {'axis': self.axis}
out = self.x
Expand All @@ -39,10 +40,10 @@ def setUp(self):
self.outputs = {'Out': out}

def test_check_output(self):
self.check_output()
self.check_output(check_eager=True)

def test_check_grad(self):
self.check_grad(['X'], 'Out')
self.check_grad(['X'], 'Out', check_eager=True)


class TestCase0(TestReverseOp):
Expand Down

0 comments on commit 6198ff2

Please sign in to comment.