Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon 4th 】add Trapezoid API && add Cumulative_trapezoid API #51195

Merged
merged 42 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
4156440
api_for_trapezoid
Ainavo Mar 4, 2023
a800bc5
fix
Ainavo Mar 4, 2023
a519504
新增paddle.cumulative_trapezoid API
Ainavo Mar 5, 2023
a2fa0ac
格式调整
Ainavo Mar 6, 2023
52e138c
格式调整
Ainavo Mar 7, 2023
76823c3
fix examples
Ainavo Mar 7, 2023
7faa3f6
fix example
Ainavo Mar 8, 2023
a656a4e
add name
Ainavo Mar 8, 2023
15d7bb4
fix example
Ainavo Mar 8, 2023
8dc693e
test
Ainavo Mar 9, 2023
de470c6
emaples:GPU-->CPU
Ainavo Mar 9, 2023
6b866c9
example on cpu
Ainavo Mar 10, 2023
7fc83d0
二分法试错
Ainavo Mar 11, 2023
af59d0f
继续二分法试错
Ainavo Mar 11, 2023
8dad70c
test
Ainavo Mar 11, 2023
e9ba544
test
Ainavo Mar 11, 2023
8c67f8f
test
Ainavo Mar 11, 2023
b2f804e
test
Ainavo Mar 11, 2023
8231dfe
test
Ainavo Mar 11, 2023
5432aa6
test
Ainavo Mar 11, 2023
60ffe08
CPU 下示例代码运行实例
Ainavo Mar 11, 2023
44dfcca
增加fp16
Ainavo Mar 14, 2023
2a0e2ab
修改单测出现的类型错误
Ainavo Mar 15, 2023
a97f70d
Merge branch 'develop' into api_for_trapezoid
Ainavo Mar 15, 2023
3338e90
解决冲突
Ainavo Mar 15, 2023
b37eb10
解决冲突
Ainavo Mar 15, 2023
b86ecfe
Merge branch 'develop' into api_for_trapezoid
Ainavo Mar 15, 2023
6af6ab4
fix test_fp16
Ainavo Mar 15, 2023
7dfb87c
Merge branch 'develop' into api_for_trapezoid
Ainavo Mar 15, 2023
8a6ac3e
增加静态图下的 fp16 单测
Ainavo Mar 16, 2023
a9a557d
Merge branch 'develop' into api_for_trapezoid
Ainavo Mar 17, 2023
9f5cfcb
fix doc
Ainavo Mar 17, 2023
6b948bf
fix doc
Ainavo Mar 17, 2023
9035f80
fix test_cumulative_trapezoid
Ainavo Mar 18, 2023
6b4a446
test
Ainavo Mar 18, 2023
3a4400c
test
Ainavo Mar 18, 2023
d5800d2
Merge branch 'develop' into api_for_trapezoid
Ainavo Mar 18, 2023
23c5cf4
Merge branch 'develop' into api_for_trapezoid
Ainavo Mar 19, 2023
d4f15e1
Merge branch 'develop' into api_for_trapezoid
Ainavo Mar 19, 2023
f203119
'test=document_fix'
Ainavo Mar 24, 2023
7606ebe
test=document fix
Ainavo Mar 24, 2023
800ea16
将 y = n-dimensional 改为 N-D
Ainavo Mar 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@
from .tensor.math import sgn # noqa: F401
from .tensor.math import take # noqa: F401
from .tensor.math import frexp # noqa: F401
from .tensor.math import trapezoid # noqa: F401
from .tensor.math import cumulative_trapezoid # noqa: F401

from .tensor.random import bernoulli # noqa: F401
from .tensor.random import poisson # noqa: F401
Expand Down Expand Up @@ -686,5 +688,7 @@
'triu_indices',
'take',
'frexp',
'trapezoid',
'cumulative_trapezoid',
'polar',
]
102 changes: 102 additions & 0 deletions python/paddle/fluid/tests/unittests/test_cumulative_trapezoid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright (c) 2023 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 scipy.integrate import cumulative_trapezoid
from test_trapezoid import (
Testfp16Trapezoid,
TestTrapezoidAPI,
TestTrapezoidError,
)

import paddle


class TestCumulativeTrapezoidAPI(TestTrapezoidAPI):
def set_api(self):
self.ref_api = cumulative_trapezoid
self.paddle_api = paddle.cumulative_trapezoid


class TestCumulativeTrapezoidWithX(TestCumulativeTrapezoidAPI):
def set_args(self):
self.y = np.array([[2, 4, 8], [3, 5, 9]]).astype('float32')
self.x = np.array([[1, 2, 3], [3, 4, 5]]).astype('float32')
self.dx = None
self.axis = -1


class TestCumulativeTrapezoidAxis(TestCumulativeTrapezoidAPI):
def set_args(self):
self.y = np.array([[2, 4, 8], [3, 5, 9]]).astype('float32')
self.x = None
self.dx = 1.0
self.axis = 0


class TestCumulativeTrapezoidWithDx(TestCumulativeTrapezoidAPI):
def set_args(self):
self.y = np.array([[2, 4, 8], [3, 5, 9]]).astype('float32')
self.x = None
self.dx = 3.0
self.axis = -1


class TestCumulativeTrapezoidfloat64(TestCumulativeTrapezoidAPI):
def set_args(self):
self.y = np.array([[2, 4, 8], [3, 5, 9]]).astype('float64')
self.x = np.array([[1, 2, 3], [3, 4, 5]]).astype('float64')
self.dx = None
self.axis = -1


class TestCumulativeTrapezoidWithOutDxX(TestCumulativeTrapezoidAPI):
def set_args(self):
self.y = np.array([[2, 4, 8], [3, 5, 9]]).astype('float64')
self.x = None
self.dx = None
self.axis = -1


class TestCumulativeTrapezoidBroadcast(TestCumulativeTrapezoidAPI):
def set_args(self):
self.y = np.random.random((3, 3, 4)).astype('float32')
self.x = np.random.random((3)).astype('float32')
self.dx = None
self.axis = 1


class TestCumulativeTrapezoidAxis1(TestCumulativeTrapezoidAPI):
def set_args(self):
self.y = np.random.random((3, 3, 4)).astype('float32')
self.x = None
self.dx = 1
self.axis = 1


class TestCumulativeTrapezoidError(TestTrapezoidError):
def set_api(self):
self.paddle_api = paddle.cumulative_trapezoid


class Testfp16CumulativeTrapezoid(Testfp16Trapezoid):
def set_api(self):
self.paddle_api = paddle.cumulative_trapezoid
self.ref_api = cumulative_trapezoid


if __name__ == '__main__':
paddle.enable_static()
unittest.main()
277 changes: 277 additions & 0 deletions python/paddle/fluid/tests/unittests/test_trapezoid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
# Copyright (c) 2023 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

import paddle
from paddle.fluid.framework import _test_eager_guard


class TestTrapezoidAPI(unittest.TestCase):
def set_args(self):
self.y = np.array([[2, 4, 8], [3, 5, 9]]).astype('float32')
self.x = None
self.dx = None
self.axis = -1

def get_output(self):
if self.x is None and self.dx is None:
self.output = self.ref_api(
y=self.y, x=self.x, dx=1.0, axis=self.axis
)
else:
self.output = self.ref_api(
y=self.y, x=self.x, dx=self.dx, axis=self.axis
)

def set_api(self):
self.ref_api = np.trapz
self.paddle_api = paddle.trapezoid

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

def func_dygraph(self):
for place in self.places:
paddle.disable_static()
y = paddle.to_tensor(self.y, place=place)
if self.x is not None:
self.x = paddle.to_tensor(self.x, place=place)
if self.dx is not None:
self.dx = paddle.to_tensor(self.dx, place=place)
out = self.paddle_api(y=y, x=self.x, dx=self.dx, axis=self.axis)
np.testing.assert_allclose(out, self.output, rtol=1e-05)

def test_dygraph(self):
with _test_eager_guard():
self.setUp()
self.func_dygraph()
self.setUp()
self.func_dygraph()

def test_static(self):
paddle.enable_static()
places = [paddle.CPUPlace()]
if paddle.device.is_compiled_with_cuda():
places.append(paddle.CUDAPlace(0))
for place in places:
with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
y = paddle.static.data(
name="y", shape=self.y.shape, dtype=self.y.dtype
)
x = None
dx = None
if self.x is not None:
x = paddle.static.data(
name="x", shape=self.x.shape, dtype=self.x.dtype
)
if self.dx is not None:
dx = paddle.static.data(
name="dx", shape=[1], dtype='float32'
)

exe = paddle.static.Executor(place)
out = self.paddle_api(y=y, x=x, dx=dx, axis=self.axis)
fetches = exe.run(
paddle.static.default_main_program(),
feed={
"y": self.y,
"x": self.x,
"dx": self.dx,
"axis": self.axis,
},
fetch_list=[out],
)
np.testing.assert_allclose(fetches[0], self.output, rtol=1e-05)


class TestTrapezoidWithX(TestTrapezoidAPI):
def set_args(self):
self.y = np.array([[2, 4, 8], [3, 5, 9]]).astype('float32')
self.x = np.array([[1, 2, 3], [3, 4, 5]]).astype('float32')
self.dx = None
self.axis = -1


class TestTrapezoidAxis(TestTrapezoidAPI):
def set_args(self):
self.y = np.array([[2, 4, 8], [3, 5, 9]]).astype('float32')
self.x = None
self.dx = 1.0
self.axis = 0


class TestTrapezoidWithDx(TestTrapezoidAPI):
def set_args(self):
self.y = np.array([[2, 4, 8], [3, 5, 9]]).astype('float32')
self.x = None
self.dx = 3.0
self.axis = -1


class TestTrapezoidfloat64(TestTrapezoidAPI):
def set_args(self):
self.y = np.array([[2, 4, 8], [3, 5, 9]]).astype('float64')
self.x = np.array([[1, 2, 3], [3, 4, 5]]).astype('float64')
self.dx = None
self.axis = -1


class TestTrapezoidWithOutDxX(TestTrapezoidAPI):
def set_args(self):
self.y = np.array([[2, 4, 8], [3, 5, 9]]).astype('float64')
self.x = None
self.dx = None
self.axis = -1


class TestTrapezoidBroadcast(TestTrapezoidAPI):
def set_args(self):
self.y = np.random.random((3, 3, 4)).astype('float32')
self.x = np.random.random((3)).astype('float32')
self.dx = None
self.axis = 1


class TestTrapezoidAxis1(TestTrapezoidAPI):
def set_args(self):
self.y = np.random.random((3, 3, 4)).astype('float32')
self.x = None
self.dx = 1
self.axis = 1


class TestTrapezoidError(unittest.TestCase):
# test error
def set_api(self):
self.paddle_api = paddle.trapezoid

def test_errors(self):
self.set_api()
with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):

def test_y_dtype():
y = paddle.static.data(
name='y',
shape=[4, 4],
dtype="int64",
)
x = paddle.static.data(name='x', shape=[4, 4], dtype="float32")
dx = None
self.paddle_api(y, x, dx)

self.assertRaises(TypeError, test_y_dtype)

def test_x_dtype():
y1 = paddle.static.data(
name='y1',
shape=[4, 4],
dtype="float32",
)
x1 = paddle.static.data(name='x1', shape=[4, 4], dtype="int64")
dx1 = None
self.paddle_api(y1, x1, dx1)

self.assertRaises(TypeError, test_x_dtype)

def test_dx_dim():
y2 = paddle.static.data(
name='y2',
shape=[4, 4],
dtype="float32",
)
x2 = None
dx2 = paddle.static.data(
name='dx2', shape=[4, 4], dtype="float32"
)
self.paddle_api(y2, x2, dx2)

self.assertRaises(ValueError, test_dx_dim)

def test_xwithdx():
y3 = paddle.static.data(
name='y3',
shape=[4, 4],
dtype="float32",
)
x3 = paddle.static.data(
name='x3', shape=[4, 4], dtype="float32"
)
dx3 = 1.0
self.paddle_api(y3, x3, dx3)

self.assertRaises(ValueError, test_xwithdx)


class Testfp16Trapezoid(TestTrapezoidAPI):
def set_api(self):
self.paddle_api = paddle.trapezoid
self.ref_api = np.trapz

def test_fp16_with_gpu(self):
paddle.enable_static()
if paddle.fluid.core.is_compiled_with_cuda():
place = paddle.CUDAPlace(0)
with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
input_y = np.random.random([4, 4]).astype("float16")
y = paddle.static.data(name="y", shape=[4, 4], dtype="float16")

input_x = np.random.random([4, 4]).astype("float16")
x = paddle.static.data(name="x", shape=[4, 4], dtype="float16")

exe = paddle.static.Executor(place)
out = self.paddle_api(y=y, x=x, dx=self.dx, axis=self.axis)
res = exe.run(
paddle.static.default_main_program(),
feed={
"y": input_y,
"x": input_x,
"dx": self.dx,
"axis": self.axis,
},
fetch_list=[out],
)

def test_fp16_func_dygraph(self):
if paddle.fluid.core.is_compiled_with_cuda():
place = paddle.CUDAPlace(0)
paddle.disable_static()
input_y = np.random.random([4, 4])
y = paddle.to_tensor(input_y, dtype='float16', place=place)
input_x = np.random.random([4, 4])
x = paddle.to_tensor(input_x, dtype='float16', place=place)
out = self.paddle_api(y=y, x=x)

def test_fp16_dygraph(self):
with _test_eager_guard():
self.func_dygraph()
self.func_dygraph()


if __name__ == '__main__':
paddle.enable_static()
unittest.main()
Loading