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

paddle.floor_divide 对于负数不符合预期 #46379

Closed
glare-ni opened this issue Sep 22, 2022 · 4 comments · Fixed by #46419
Closed

paddle.floor_divide 对于负数不符合预期 #46379

glare-ni opened this issue Sep 22, 2022 · 4 comments · Fixed by #46419
Assignees
Labels
status/developed 开发完成 type/docs 文档问题

Comments

@glare-ni
Copy link

bug描述 Describe the Bug

竞品对于floor div的计算方式如下,而paddle的策略有所不同,
if value is a negative number eg."-0.2", paddle will use "ceil", the value will be "0" 如果除完的值是负数,那么会用ceil策略,向上取整,(竞品是向下取整)
if value is a positive number eg "1.2", paddle will user "floor" the value will be "1". 如果除完的值是正数,那么会用floor策略,向下取整,(符合预期)
this is a demo.

import numpy as np

x = np.array([1, 2, -3])
y = np.array([-2, 1, 2])

np.floor_divide(x, y)
# [-1, 2, -2]
import paddle
paddle.set_device("cpu")

x = paddle.to_tensor([1, 2, -3])
y = paddle.to_tensor([-2, 1, 2])

paddle.floor_divide(x, y)
# [0, 2, -1]

paddle.floor( x.astype("float32") / y.astype("float32")).astype("int65")
# [-1, 2, -2]

其他补充信息 Additional Supplementary Information

No response

@paddle-bot
Copy link

paddle-bot bot commented Sep 22, 2022

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网API文档常见问题历史IssueAI社区来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

@Bobholamovic
Copy link
Member

Bobholamovic commented Sep 22, 2022

你好,paddle.floor_divide()处理商为负数情形的行为与NumPy确实不同,实际更接近trunc()而不是floor()的行为。感谢指出这一问题,我们将修复此bug,使代码实际功能与文档描述一致。

@glare-ni
Copy link
Author

glare-ni commented Sep 22, 2022

pytorch竞品目前有两种形式,https://pytorch.org/docs/stable/generated/torch.div.html#torch.div
目前paddle这里实现了 trunc模式,也就是 https://pytorch.org/docs/stable/generated/torch.floor_divide.html?highlight=floor_divide#torch.floor_divide 这个代码,paddle目前跟这个对齐了。建议可以跟pytorch一样实现两种模式,供用户选择。

从pytorch竞品对齐的角度来看,这也可不算个bug,算是正常功能。

>>> x = torch.tensor([ 0.3810,  1.2774, -0.2972, -0.3719,  0.4637])
>>> torch.div(x, 0.5)
tensor([ 0.7620,  2.5548, -0.5944, -0.7438,  0.9274])

>>> a = torch.tensor([[-0.3711, -1.9353, -0.4605, -0.2917],
...                   [ 0.1815, -1.0111,  0.9805, -1.5923],
...                   [ 0.1062,  1.4581,  0.7759, -1.2344],
...                   [-0.1830, -0.0313,  1.1908, -1.4757]])
>>> b = torch.tensor([ 0.8032,  0.2930, -0.8113, -0.2308])
>>> torch.div(a, b)
tensor([[-0.4620, -6.6051,  0.5676,  1.2639],
        [ 0.2260, -3.4509, -1.2086,  6.8990],
        [ 0.1322,  4.9764, -0.9564,  5.3484],
        [-0.2278, -0.1068, -1.4678,  6.3938]])

>>> torch.div(a, b, rounding_mode='trunc')
tensor([[-0., -6.,  0.,  1.],
        [ 0., -3., -1.,  6.],
        [ 0.,  4., -0.,  5.],
        [-0., -0., -1.,  6.]])

>>> torch.div(a, b, rounding_mode='floor')
tensor([[-1., -7.,  0.,  1.],
        [ 0., -4., -2.,  6.],
        [ 0.,  4., -1.,  5.],
        [-1., -1., -2.,  6.]])

@Bobholamovic
Copy link
Member

Bobholamovic commented Sep 22, 2022

你说得对!不过PyTorch官方文档也指出,floor_divide这个API因为其名称可能造成的误解即将被废弃,取而代之的是支持两种舍入模式的torch.div。我认为,如果在paddle.floor_divide的基础上增加舍入模式的选项,可能会导致名称带来的误解加重(即,floor_divide这个API可以显式地被指定执行非floor的行为),可能也是考虑到这个原因PyTorch才另外提供了torch.div API的。从这点出发,我倾向于保留paddle.floor_divide现有的功能,但修改文档中的表述。关于rounding_mode的提议,我建议可以另外设计一个API提供这一功能。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/developed 开发完成 type/docs 文档问题
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants