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 No.25】为 Paddle 新增 nanquantile 数学计算API #41343

Merged
merged 20 commits into from Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
126720b
add nanquantile and fix quantile bug
Asthestarsfalll Apr 2, 2022
dc89e91
add unittest of nanquantile
Asthestarsfalll Apr 2, 2022
908548f
fix bug of test_quantile
Asthestarsfalll Apr 2, 2022
34f7742
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Asthestarsfalll Apr 2, 2022
b63469e
fix typo
Asthestarsfalll Apr 2, 2022
34f4df9
fig type error
Asthestarsfalll Apr 2, 2022
3306abc
update the code
Asthestarsfalll Apr 2, 2022
56e3be8
fix error
Asthestarsfalll Apr 2, 2022
1b0c13d
refactor unittest and update example code
Asthestarsfalll Apr 7, 2022
350ccad
reduce data scale
Asthestarsfalll Apr 12, 2022
09a5c25
Merge branch 'PaddlePaddle:develop' into nanquantile
Asthestarsfalll Apr 12, 2022
0e2ef42
update
Asthestarsfalll Apr 12, 2022
78d332f
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Asthestarsfalll Apr 12, 2022
34ab31f
Merge branch 'nanquantile' of https://github.com/Asthestarsfalll/Padd…
Asthestarsfalll Apr 12, 2022
31e1734
add nanquantile to __all__
Asthestarsfalll Apr 13, 2022
4669518
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Asthestarsfalll Apr 13, 2022
4250918
Merge branch 'PaddlePaddle:develop' into nanquantile
Asthestarsfalll Apr 13, 2022
86d65bb
add missing comma
Asthestarsfalll Apr 14, 2022
b3bc441
Merge branch 'nanquantile' of https://github.com/Asthestarsfalll/Padd…
Asthestarsfalll Apr 14, 2022
26c993f
Merge branch 'PaddlePaddle:develop' into nanquantile
Asthestarsfalll Apr 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions python/paddle/__init__.py
Expand Up @@ -323,6 +323,7 @@
from .tensor.stat import numel # noqa: F401
from .tensor.stat import median # noqa: F401
from .tensor.stat import quantile # noqa: F401
from .tensor.stat import nanquantile # noqa: F401
from .device import get_cudnn_version # noqa: F401
from .device import set_device # noqa: F401
from .device import get_device # noqa: F401
Expand Down
237 changes: 237 additions & 0 deletions python/paddle/fluid/tests/unittests/test_nanquantile.py
@@ -0,0 +1,237 @@
# Copyright (c) 2022 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.

from __future__ import print_function

import unittest
import numpy as np
import paddle


class TestNaNQuantile(unittest.TestCase):
"""
This class is used for numerical precision testing. If there is
a corresponding numpy API, the precision comparison can be performed directly.
Otherwise, it needs to be verified by numpy implementated function.
"""

def setUp(self):
np.random.seed(2022)
self.input_data = np.random.rand(6, 7, 8, 9, 10)

# Test correctness when q and axis are set.
def test_nanquantile_single_q(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=0.5, axis=2)
np_res = np.nanquantile(self.input_data, q=0.5, axis=2)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

# Test correctness for default axis.
def test_nanquantile_with_no_axis(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=0.35)
np_res = np.nanquantile(self.input_data, q=0.35)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

# Test correctness for multiple axis.
def test_nanquantile_with_multi_axis(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=0.75, axis=[0, 2, 3])
np_res = np.nanquantile(self.input_data, q=0.75, axis=[0, 2, 3])
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

# Test correctness when keepdim is set.
def test_nanquantile_with_keepdim(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=0.35, axis=4, keepdim=True)
np_res = np.nanquantile(self.input_data, q=0.35, axis=4, keepdims=True)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

# Test correctness when all parameters are set.
def test_nanquantile_with_keepdim_and_multiple_axis(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=0.1, axis=[1, 4], keepdim=True)
np_res = np.nanquantile(self.input_data, q=0.1, axis=[1, 4], keepdims=True)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

# Test correctness when q = 0.
def test_nanquantile_with_boundary_q(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=0, axis=3)
np_res = np.nanquantile(self.input_data, q=0, axis=3)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

# Test correctness when input includes NaN.
def test_nanquantile_include_NaN(self):
input_data = np.random.randn(2, 3, 4)
input_data[0, 1, 1] = np.nan
x = paddle.to_tensor(input_data)
paddle_res = paddle.nanquantile(x, q=0.35, axis=0)
np_res = np.nanquantile(x, q=0.35, axis=0)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res, equal_nan=True))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

单测和quantile几乎一摸一样,但nanquantile更要侧重对NAN的测试:

  • 可以修改一下已有单测,每个Class里增加不同位置NAN的测试。包括一个或者多个NAN
  • 缺少全是NAN的测试

因为两份单测非常类似,如果可以的话,看如何更好地进行复用(非强制要求),如

test_case(self.x)
test_case(self.x, [])
test_case(self.x, -1)
test_case(self.x, keepdim=True)
test_case(self.x, 2, keepdim=True)
test_case(self.x, [0, 2])
test_case(self.x, (0, 2))
test_case(self.x, [0, 1, 2, 3])
paddle.enable_static()

_test_static_graph('amax')
_test_static_graph('amin')
_test_static_graph('max')
_test_static_graph('min')



class TestNaNQuantileMuitlpleQ(unittest.TestCase):
"""
This class is used to test multiple input of q.
"""

def setUp(self):
np.random.seed(2022)
self.input_data = np.random.rand(10, 3, 4, 5, 4)

def test_nanquantile(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=[0.3, 0.44], axis=-2)
np_res = np.nanquantile(self.input_data, q=[0.3, 0.44], axis=-2)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

def test_nanquantile_multiple_axis(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(x, q=[0.2, 0.67], axis=[1, -1])
np_res = np.nanquantile(self.input_data, q=[0.2, 0.67], axis=[1, -1])
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

def test_nanquantile_multiple_axis_keepdim(self):
x = paddle.to_tensor(self.input_data)
paddle_res = paddle.nanquantile(
x, q=[0.1, 0.2, 0.3], axis=[1, 2], keepdim=True)
np_res = np.nanquantile(
self.input_data, q=[0.1, 0.2, 0.3], axis=[1, 2], keepdims=True)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))


class TestNaNQuantileError(unittest.TestCase):
"""
This class is used to test that exceptions are thrown correctly.
Validity of all parameter values and types should be considered.
"""

def setUp(self):
self.x = paddle.randn((2, 3, 4))

def test_errors(self):
# Test error when q > 1
def test_q_range_error_1():
paddle_res = paddle.nanquantile(self.x, q=1.5)

self.assertRaises(ValueError, test_q_range_error_1)

# Test error when q < 0
def test_q_range_error_2():
paddle_res = paddle.nanquantile(self.x, q=[0.2, -0.3])

self.assertRaises(ValueError, test_q_range_error_2)

# Test error with no valid q
def test_q_range_error_3():
paddle_res = paddle.nanquantile(self.x, q=[])

self.assertRaises(ValueError, test_q_range_error_3)

# Test error when x is not Tensor
def test_x_type_error():
x = [1, 3, 4]
paddle_res = paddle.nanquantile(x, q=0.9)

self.assertRaises(TypeError, test_x_type_error)

# Test error when scalar axis is not int
def test_axis_type_error_1():
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=0.4)

self.assertRaises(ValueError, test_axis_type_error_1)

# Test error when axis in List is not int
def test_axis_type_error_2():
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=[1, 0.4])

self.assertRaises(ValueError, test_axis_type_error_2)

# Test error when axis not in [-D, D)
def test_axis_value_error_1():
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=10)

self.assertRaises(ValueError, test_axis_value_error_1)

# Test error when axis not in [-D, D)
def test_axis_value_error_2():
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=[1, -10])

self.assertRaises(ValueError, test_axis_value_error_2)

# Test error with no valid axis
def test_axis_value_error_3():
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=[])

self.assertRaises(ValueError, test_axis_value_error_3)


class TestNaNQuantileRuntime(unittest.TestCase):
"""
This class is used to test the API could run correctly with
different devices, different data types, and dygraph/static mode.
"""

def setUp(self):
np.random.seed(2022)
self.input_data = np.random.rand(6, 7, 8, 9, 10)
self.dtypes = ['float32', 'float64']
self.devices = ['cpu']
if paddle.device.is_compiled_with_cuda():
self.devices.append('gpu')

def test_dygraph(self):
paddle.disable_static()
for device in self.devices:
# Check different devices
paddle.set_device(device)
for dtype in self.dtypes:
# Check different dtypes
np_input_data = self.input_data.astype(dtype)
x = paddle.to_tensor(np_input_data, dtype=dtype)
paddle_res = paddle.nanquantile(x, q=0.5, axis=2)
np_res = np.nanquantile(np_input_data, q=0.5, axis=2)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res))

def test_static(self):
paddle.enable_static()
for device in self.devices:
x = paddle.static.data(
name="x", shape=self.input_data.shape, dtype=paddle.float32)
x_fp64 = paddle.static.data(
name="x_fp64",
shape=self.input_data.shape,
dtype=paddle.float64)

results = paddle.nanquantile(x, q=0.5, axis=2)
np_input_data = self.input_data.astype('float32')
results_fp64 = paddle.nanquantile(x_fp64, q=0.5, axis=2)
np_input_data_fp64 = self.input_data.astype('float64')

exe = paddle.static.Executor(device)
paddle_res, paddle_res_fp64 = exe.run(
paddle.static.default_main_program(),
feed={"x": np_input_data,
"x_fp64": np_input_data_fp64},
fetch_list=[results, results_fp64])
np_res = np.nanquantile(np_input_data, q=0.5, axis=2)
np_res_fp64 = np.nanquantile(np_input_data_fp64, q=0.5, axis=2)
self.assertTrue(
np.allclose(paddle_res, np_res) and np.allclose(paddle_res_fp64,
np_res_fp64))


if __name__ == '__main__':
unittest.main()
3 changes: 2 additions & 1 deletion python/paddle/fluid/tests/unittests/test_quantile.py
Expand Up @@ -78,7 +78,8 @@ def test_quantile_include_NaN(self):
input_data[0, 1, 1] = np.nan
x = paddle.to_tensor(input_data)
paddle_res = paddle.quantile(x, q=0.35, axis=0)
self.assertTrue(paddle.isnan(paddle_res[1, 1]))
np_res = np.quantile(x, q=0.35, axis=0)
self.assertTrue(np.allclose(paddle_res.numpy(), np_res, equal_nan=True))


class TestQuantileMuitlpleQ(unittest.TestCase):
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/tensor/__init__.py
Expand Up @@ -260,6 +260,7 @@
from .stat import numel # noqa: F401
from .stat import median # noqa: F401
from .stat import quantile # noqa: F401
from .stat import nanquantile # noqa: F401

from .to_string import set_printoptions # noqa: F401

Expand Down Expand Up @@ -442,6 +443,7 @@
'numel',
'median',
'quantile',
'nanquantile'
Copy link
Contributor

@luotao1 luotao1 Apr 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

又少了一个逗号了。。
可以通过看日志发现:https://xly.bce.baidu.com/paddlepaddle/paddle/newipipe/detail/5392118/job/14041205


2022-04-14 01:51:31 There are 3 approved errors.
2022-04-14 01:51:31 ****************
2022-04-14 01:51:32 API Difference is: 
2022-04-14 01:51:32 - paddle.Tensor.is_complex (ArgSpec(args=['x'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={}), ('document', '9d4dc47b098ce34e65cc23e14ad02281'))
2022-04-14 01:51:32 - paddle.Tensor.quantile (ArgSpec(args=['x', 'q', 'axis', 'keepdim'], varargs=None, varkw=None, defaults=(None, False), kwonlyargs=[], kwonlydefaults=None, annotations={}), ('document', 'd6e25fbeb7751f8e57ad215209f36e00'))
2022-04-14 01:51:32 ?                                                                                                                                                                                          ^ ^^^^^^^   ^^^^^ ^^^^^^^ ^^^^^^^^^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

抱歉,已修改。当时看的时候还以为是__all__里的。。

'is_complex',
'is_integer',
'rank',
Expand Down