Skip to content

Commit

Permalink
Add diff op (#37441)
Browse files Browse the repository at this point in the history
* add diff op, test=develop

* rm some notes, test=develop

* update diff doc

* update sample code

* fix diff api params and example code, test=develop
  • Loading branch information
andyjiang1116 authored Nov 30, 2021
1 parent 0c8b999 commit 2f4c089
Show file tree
Hide file tree
Showing 4 changed files with 383 additions and 2 deletions.
4 changes: 3 additions & 1 deletion python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
from .tensor.math import digamma # noqa: F401
from .tensor.math import neg # noqa: F401
from .tensor.math import lgamma # noqa: F401
from .tensor.math import diff # noqa: F401

from .tensor.random import multinomial # noqa: F401
from .tensor.random import standard_normal # noqa: F401
Expand Down Expand Up @@ -531,5 +532,6 @@
'broadcast_tensors',
'einsum',
'set_flags',
'get_flags'
'get_flags',
'diff'
]
214 changes: 214 additions & 0 deletions python/paddle/fluid/tests/unittests/test_diff_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
# Copyright (c) 2021 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
import paddle.fluid as fluid
import paddle.fluid.layers as layers
import paddle.fluid.core as core


class TestDiffOp(unittest.TestCase):
def set_args(self):
self.input = np.array([1, 4, 5, 2]).astype('float32')
self.n = 1
self.axis = -1
self.prepend = None
self.append = None

def get_output(self):
if self.prepend is not None and self.append is not None:
self.output = np.diff(
self.input,
n=self.n,
axis=self.axis,
prepend=self.prepend,
append=self.append)
elif self.prepend is not None:
self.output = np.diff(
self.input, n=self.n, axis=self.axis, prepend=self.prepend)
elif self.append is not None:
self.output = np.diff(
self.input, n=self.n, axis=self.axis, append=self.append)
else:
self.output = np.diff(self.input, n=self.n, axis=self.axis)

def setUp(self):
self.set_args()
self.get_output()
self.places = [paddle.CPUPlace()]
if core.is_compiled_with_cuda():
self.places.append(paddle.CUDAPlace(0))

def test_dygraph(self):
for place in self.places:
paddle.disable_static(place)
x = paddle.to_tensor(self.input, place=place)
if self.prepend is not None:
self.prepend = paddle.to_tensor(self.prepend, place=place)
if self.append is not None:
self.append = paddle.to_tensor(self.append, place=place)
out = paddle.diff(
x,
n=self.n,
axis=self.axis,
prepend=self.prepend,
append=self.append)
self.assertTrue((out.numpy() == self.output).all(), True)

def test_static(self):
paddle.enable_static()
places = [fluid.CPUPlace()]
if core.is_compiled_with_cuda():
places.append(fluid.CUDAPlace(0))
for place in places:
with fluid.program_guard(fluid.Program(), fluid.Program()):
x = paddle.fluid.data(
name="input",
shape=self.input.shape,
dtype=self.input.dtype)
has_pend = False
prepend = None
append = None
if self.prepend is not None:
has_pend = True
prepend = paddle.fluid.data(
name="prepend",
shape=self.prepend.shape,
dtype=self.prepend.dtype)
if self.append is not None:
has_pend = True
append = paddle.fluid.data(
name="append",
shape=self.append.shape,
dtype=self.append.dtype)

exe = fluid.Executor(place)
out = paddle.diff(
x, n=self.n, axis=self.axis, prepend=prepend, append=append)
fetches = exe.run(fluid.default_main_program(),
feed={
"input": self.input,
"prepend": self.prepend,
"append": self.append
},
fetch_list=[out])
self.assertTrue((fetches[0] == self.output).all(), True)

def test_grad(self):
for place in self.places:
x = paddle.to_tensor(self.input, place=place, stop_gradient=False)
if self.prepend is not None:
self.prepend = paddle.to_tensor(self.prepend, place=place)
if self.append is not None:
self.append = paddle.to_tensor(self.append, place=place)
out = paddle.diff(
x,
n=self.n,
axis=self.axis,
prepend=self.prepend,
append=self.append)
try:
out.backward()
x_grad = x.grad
except:
raise RuntimeError("Check Diff Gradient Failed")


class TestDiffOpAxis(TestDiffOp):
def set_args(self):
self.input = np.array([[1, 4, 5, 2], [1, 5, 4, 2]]).astype('float32')
self.n = 1
self.axis = 0
self.prepend = None
self.append = None


class TestDiffOpNDim(TestDiffOp):
def set_args(self):
self.input = np.random.rand(10, 10).astype('float32')
self.n = 1
self.axis = -1
self.prepend = None
self.append = None


class TestDiffOpBool(TestDiffOp):
def set_args(self):
self.input = np.array([0, 1, 1, 0, 1, 0]).astype('bool')
self.n = 1
self.axis = -1
self.prepend = None
self.append = None


class TestDiffOpPrepend(TestDiffOp):
def set_args(self):
self.input = np.array([[1, 4, 5, 2], [1, 5, 4, 2]]).astype('float32')
self.n = 1
self.axis = -1
self.prepend = np.array([[2, 3, 4], [1, 3, 5]]).astype('float32')
self.append = None


class TestDiffOpPrependAxis(TestDiffOp):
def set_args(self):
self.input = np.array([[1, 4, 5, 2], [1, 5, 4, 2]]).astype('float32')
self.n = 1
self.axis = 0
self.prepend = np.array(
[[0, 2, 3, 4], [1, 3, 5, 7], [2, 5, 8, 0]]).astype('float32')
self.append = None


class TestDiffOpAppend(TestDiffOp):
def set_args(self):
self.input = np.array([[1, 4, 5, 2], [1, 5, 4, 2]]).astype('float32')
self.n = 1
self.axis = -1
self.prepend = None
self.append = np.array([[2, 3, 4], [1, 3, 5]]).astype('float32')


class TestDiffOpAppendAxis(TestDiffOp):
def set_args(self):
self.input = np.array([[1, 4, 5, 2], [1, 5, 4, 2]]).astype('float32')
self.n = 1
self.axis = 0
self.prepend = None
self.append = np.array([[2, 3, 4, 1]]).astype('float32')


class TestDiffOpPreAppend(TestDiffOp):
def set_args(self):
self.input = np.array([[1, 4, 5, 2], [1, 5, 4, 2]]).astype('float32')
self.n = 1
self.axis = -1
self.prepend = np.array([[0, 4], [5, 9]]).astype('float32')
self.append = np.array([[2, 3, 4], [1, 3, 5]]).astype('float32')


class TestDiffOpPreAppendAxis(TestDiffOp):
def set_args(self):
self.input = np.array([[1, 4, 5, 2], [1, 5, 4, 2]]).astype('float32')
self.n = 1
self.axis = 0
self.prepend = np.array([[0, 4, 5, 9], [5, 9, 2, 3]]).astype('float32')
self.append = np.array([[2, 3, 4, 7], [1, 3, 5, 6]]).astype('float32')


if __name__ == '__main__':
unittest.main()
4 changes: 3 additions & 1 deletion python/paddle/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
from .math import neg # noqa: F401
from .math import lgamma # noqa: F401
from .math import diagonal # noqa: F401
from .math import diff # noqa: F401

from .random import multinomial # noqa: F401
from .random import standard_normal # noqa: F401
Expand Down Expand Up @@ -400,7 +401,8 @@
'uniform_',
'multi_dot',
'solve',
'triangular_solve'
'triangular_solve',
'diff'
]

#this list used in math_op_patch.py for magic_method bind
Expand Down
Loading

0 comments on commit 2f4c089

Please sign in to comment.