Skip to content

【Paddle Tensor 第二期 API鲁棒性增强】解决paddle.max、paddle.min在输入存在nan时传播不正确问题 #70049

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

Merged
merged 9 commits into from
Dec 10, 2024

Conversation

aquagull
Copy link
Contributor

@aquagull aquagull commented Dec 8, 2024

PR Category

User Experience

PR Types

Bug fixes

Description

解决了paddle.max、paddle.min在gpu、cpu环境下输入存在nan时传播不正确问题.

image

image

image

测试代码

import paddle
import numpy as np

def test_reduce_with_nan(func, on_gpu):
    shape = (2, 3)  
    x_np = np.arange(np.prod(shape), dtype=np.float32).reshape(shape)
    x_np[0, 0] = np.nan  
    x = paddle.to_tensor(x_np, place=paddle.CPUPlace())
    if on_gpu:
        x = x.cuda()

    out = func(x)
    print(f"{func.__name__}({x.place}): {out.numpy()}")

test_reduce_with_nan(paddle.min, False)
test_reduce_with_nan(paddle.max, False)

if paddle.is_compiled_with_cuda():
    test_reduce_with_nan(paddle.min, True)
    test_reduce_with_nan(paddle.max, True)

Copy link

paddle-bot bot commented Dec 8, 2024

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Dec 8, 2024
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate left a comment

Choose a reason for hiding this comment

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

添加单测

@luotao1 luotao1 added the HappyOpenSource 快乐开源活动issue与PR label Dec 9, 2024
Comment on lines 147 to 157
def _test_with_nan(self, func, shape, on_gpu, dtype=np.float32):
with dygraph_guard():
x_np = np.arange(np.prod(shape), dtype=dtype).reshape(shape)
x_np[0, 0] = np.nan
x = paddle.to_tensor(x_np)
if on_gpu:
if not paddle.is_compiled_with_cuda():
return
x = x.cuda()
out = func(x)
self.assertTrue(paddle.isnan(out), "Result should be NaN")
Copy link
Contributor

Choose a reason for hiding this comment

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

这个逻辑不用这么复杂吧

Suggested change
def _test_with_nan(self, func, shape, on_gpu, dtype=np.float32):
with dygraph_guard():
x_np = np.arange(np.prod(shape), dtype=dtype).reshape(shape)
x_np[0, 0] = np.nan
x = paddle.to_tensor(x_np)
if on_gpu:
if not paddle.is_compiled_with_cuda():
return
x = x.cuda()
out = func(x)
self.assertTrue(paddle.isnan(out), "Result should be NaN")
def _test_with_nan(self, func, shape, place, dtype=np.float32):
with dygraph_guard():
x_np = np.arange(np.prod(shape), dtype=dtype).reshape(shape)
x_np[0, 0] = np.nan
x = paddle.to_tensor(x_np, place=place)
out = func(x)
self.assertTrue(paddle.isnan(out), "Result should be NaN")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已更改

@HydrogenSulfate HydrogenSulfate merged commit 914caad into PaddlePaddle:develop Dec 10, 2024
28 checks passed
@HydrogenSulfate
Copy link
Contributor

@aquagull 用SFINAE合并下int32/int64两种模板

@aquagull
Copy link
Contributor Author

用SFINAE合并下int32/int64两种模板

好的

@aquagull aquagull deleted the FixNanPropa branch December 10, 2024 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource 快乐开源活动issue与PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants