-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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.6】implement nan_to_num #42469
Merged
luotao1
merged 9 commits into
PaddlePaddle:develop
from
tiancaishaonvjituizi:nan_to_num
Oct 25, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a755e2f
new nan_to_num impl
tiancaishaonvjituizi 783fcd2
refine var names, add more comments
tiancaishaonvjituizi 5a5f5fd
skip float64 test
tiancaishaonvjituizi 070fbaf
Merge branch 'develop' into nan_to_num
tiancaishaonvjituizi 8972eda
reformat
tiancaishaonvjituizi 9f34c06
restore more tests
tiancaishaonvjituizi c03f6f9
skip test
tiancaishaonvjituizi 5326864
reformat manually
tiancaishaonvjituizi 6c94b23
Merge branch 'develop' into nan_to_num
tiancaishaonvjituizi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
python/paddle/fluid/tests/unittests/test_nan_to_num_op.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
# 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. | ||
|
||
import unittest | ||
from typing import Optional | ||
import numpy as np | ||
import paddle | ||
import paddle.fluid.core as core | ||
|
||
# from op_test import OpTest | ||
|
||
|
||
def np_nan_to_num( | ||
x: np.ndarray, | ||
nan: float = 0.0, | ||
posinf: Optional[float] = None, | ||
neginf: Optional[float] = None, | ||
) -> np.ndarray: | ||
return np.nan_to_num(x, True, nan=nan, posinf=posinf, neginf=neginf) | ||
|
||
|
||
def np_nan_to_num_op( | ||
x: np.ndarray, | ||
nan: float, | ||
replace_posinf_with_max: bool, | ||
posinf: float, | ||
replace_neginf_with_min: bool, | ||
neginf: float, | ||
) -> np.ndarray: | ||
if replace_posinf_with_max: | ||
posinf = None | ||
if replace_neginf_with_min: | ||
neginf = None | ||
return np.nan_to_num(x, True, nan=nan, posinf=posinf, neginf=neginf) | ||
|
||
|
||
def np_nan_to_num_grad(x: np.ndarray, dout: np.ndarray) -> np.ndarray: | ||
dx = np.copy(dout) | ||
dx[np.isnan(x) | (x == np.inf) | (x == -np.inf)] = 0 | ||
return dx | ||
|
||
|
||
class TestNanToNum(unittest.TestCase): | ||
def setUp(self): | ||
self.place = ( | ||
paddle.CUDAPlace(0) | ||
if core.is_compiled_with_cuda() | ||
else paddle.CPUPlace() | ||
) | ||
|
||
def test_static(self): | ||
x_np = np.array([[1, np.nan, -2], [np.inf, 0, -np.inf]]).astype( | ||
np.float32 | ||
) | ||
out1_np = np_nan_to_num(x_np) | ||
out2_np = np_nan_to_num(x_np, 1.0) | ||
out3_np = np_nan_to_num(x_np, 1.0, 9.0) | ||
out4_np = np_nan_to_num(x_np, 1.0, 9.0, -12.0) | ||
paddle.enable_static() | ||
with paddle.static.program_guard(paddle.static.Program()): | ||
x = paddle.fluid.data('X', x_np.shape) | ||
out1 = paddle.nan_to_num(x) | ||
out2 = paddle.nan_to_num(x, 1.0) | ||
out3 = paddle.nan_to_num(x, 1.0, 9.0) | ||
out4 = paddle.nan_to_num(x, 1.0, 9.0, -12.0) | ||
exe = paddle.static.Executor(self.place) | ||
res = exe.run(feed={'X': x_np}, fetch_list=[out1, out2, out3, out4]) | ||
|
||
self.assertTrue(np.allclose(out1_np, res[0])) | ||
self.assertTrue(np.allclose(out2_np, res[1])) | ||
self.assertTrue(np.allclose(out3_np, res[2])) | ||
self.assertTrue(np.allclose(out4_np, res[3])) | ||
|
||
def test_dygraph(self): | ||
|
||
paddle.disable_static(place=self.place) | ||
|
||
with paddle.fluid.dygraph.guard(): | ||
# NOTE(tiancaishaonvjituizi): float64 input fails the test | ||
x_np = np.array([[1, np.nan, -2], [np.inf, 0, -np.inf]]).astype( | ||
np.float32 | ||
# np.float64 | ||
) | ||
x_tensor = paddle.to_tensor(x_np, stop_gradient=False) | ||
|
||
out_tensor = paddle.nan_to_num(x_tensor) | ||
out_np = np_nan_to_num(x_np) | ||
self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) | ||
|
||
out_tensor = paddle.nan_to_num(x_tensor, 1.0, None, None) | ||
out_np = np_nan_to_num(x_np, 1, None, None) | ||
self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) | ||
|
||
out_tensor = paddle.nan_to_num(x_tensor, 1.0, 2.0, None) | ||
out_np = np_nan_to_num(x_np, 1, 2, None) | ||
self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) | ||
|
||
out_tensor = paddle.nan_to_num(x_tensor, 1.0, None, -10.0) | ||
out_np = np_nan_to_num(x_np, 1, None, -10) | ||
self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) | ||
|
||
out_tensor = paddle.nan_to_num(x_tensor, 1.0, 100.0, -10.0) | ||
out_np = np_nan_to_num(x_np, 1, 100, -10) | ||
self.assertTrue(np.allclose(out_tensor.numpy(), out_np)) | ||
|
||
paddle.enable_static() | ||
|
||
def test_check_grad(self): | ||
paddle.disable_static(place=self.place) | ||
x_np = np.array([[1, np.nan, -2], [np.inf, 0, -np.inf]]).astype( | ||
np.float32 | ||
) | ||
x_tensor = paddle.to_tensor(x_np, stop_gradient=False) | ||
|
||
y = paddle.nan_to_num(x_tensor) | ||
dx = paddle.grad(y, x_tensor)[0].numpy() | ||
|
||
np_grad = np_nan_to_num_grad(x_np, np.ones_like(x_np)) | ||
self.assertTrue(np.allclose(np_grad, dx)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
单测报错信息进行了升级,可以在下一个PR里根据提示使用 |
||
|
||
paddle.enable_static() | ||
|
||
|
||
# class BaseTestCases: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以在下一个PR里加一个注释说明下这些单测注释的原因 |
||
# | ||
# class BaseOpTest(OpTest): | ||
# | ||
# def setUp(self): | ||
# self.op_type = "nan_to_num" | ||
# input = np.arange(100, dtype=np.float64) | ||
# input[5] = np.nan | ||
# input[29] = np.inf | ||
# input[97] = -np.inf | ||
# self.inputs = {'X': input} | ||
# self.attrs = self._attrs() | ||
# self.outputs = { | ||
# 'Out': np_nan_to_num_op(self.inputs['X'], **self.attrs) | ||
# } | ||
# paddle.enable_static() | ||
# | ||
# def test_check_output(self): | ||
# self.check_output() | ||
# | ||
# def test_check_grad(self): | ||
# input = self.inputs['X'] | ||
# dout = np.ones_like(input) / input.size | ||
# self.check_grad( | ||
# ['X'], | ||
# 'Out', | ||
# user_defined_grads=[np_nan_to_num_grad(self.inputs['X'], dout)]) | ||
# | ||
# def _attrs(self): | ||
# raise NotImplementedError() | ||
# | ||
# | ||
# class TestNanToNumOp1(BaseTestCases.BaseOpTest): | ||
# | ||
# def _attrs(self): | ||
# return { | ||
# 'nan': 0.0, | ||
# 'replace_posinf_with_max': True, | ||
# 'posinf': -1, | ||
# 'replace_neginf_with_min': True, | ||
# 'neginf': -10 | ||
# } | ||
# | ||
# | ||
# class TestNanToNumOp2(BaseTestCases.BaseOpTest): | ||
# | ||
# def _attrs(self): | ||
# return { | ||
# 'nan': 2.0, | ||
# 'replace_posinf_with_max': False, | ||
# 'posinf': -1, | ||
# 'replace_neginf_with_min': True, | ||
# 'neginf': -10 | ||
# } | ||
# | ||
# | ||
# class TestNanToNumOp3(BaseTestCases.BaseOpTest): | ||
# | ||
# def _attrs(self): | ||
# return { | ||
# 'nan': 0.0, | ||
# 'replace_posinf_with_max': False, | ||
# 'posinf': -1, | ||
# 'replace_neginf_with_min': False, | ||
# 'neginf': -10 | ||
# } | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个api需要同时加在
__all__
中,否则不会被正常显示在api列表里There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已添加