-
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 5th No.33】为 Paddle 新增 atleast_1d / atleast_2d / atleast_3d API -part #58323
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
Sorry to inform you that 2395977's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually. |
python/paddle/tensor/manipulation.py
Outdated
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. | ||
|
||
Note: | ||
``int8``, ``uint8``, ``complex64``, ``complex128`` are not supported in static graph mode. |
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.
为什么在静态图模式下这几个类型不能支持?
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.
这几个方法里面用到了 reshape
将 0-dim 转为 n-dim,当时 rfc 的时候没想到 reshape
的动/静态图支持的不一样,静态图不支持这几个类型:
def reshape(x, shape, name=None):
if in_dynamic_mode():
...
else:
check_variable_and_dtype(
x,
'x',
[
'float16',
'float32',
'float64',
'int16',
'int32',
'int64',
'bool',
'uint16',
],
'reshape',
)
所以这里单独将这几个类型单拎出来了 ~
另外,bool
应该也支持,当时 rfc 的时候没有提到 bool
类型,是否也加进去?
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.
reshape 的 check_variable_and_dtype 是否可以加上这几个类型呢?如果可以的话,需要单独提一个PR支持静态图下reshape的 int8
, uint8
, complex64
, complex128
类型。
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.
看了一下底层 c++ 算子的实现,没找到 reshape 对于这几种数据类型有单独处理。
尝试修改 paddle/tensor/manipulation.py
:
def reshape(x, shape, name=None):
if in_dynamic_mode():
...
elif in_pir_mode():
...
else:
check_variable_and_dtype(
x,
'x',
[
'float16',
'float32',
'float64',
'int16',
'int32',
'int64',
'bool',
'uint16',
'int8',
'uint8',
'complex64',
'complex128',
],
'reshape',
)
简单验证:
In [1]: import numpy as np
...: import paddle
...:
...: paddle.enable_static()
...: with paddle.static.program_guard(paddle.static.Program()):
...: feed = {}
...: x = paddle.static.data('x', [12], 'int8')
...: feed['x'] = np.arange(12).astype('int8')
...:
...: out = paddle.reshape(x, [2, 6])
...: exe = paddle.static.Executor(paddle.CUDAPlace(0))
...: res = exe.run(feed=feed, fetch_list=[out])
...:
...: print(res)
...:
[array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]], dtype=int8)]
- 未修改之前,会报错 TypeError
- 修改之后,能够正常运行
另外,in_pir_mode
这里先不修改吧 ~ 我试了一下 os.environ['FLAGS_enable_new_ir_in_executor'] = 'True'
,接口会挂掉 ... ...
那我这边单独提 PR 再逐个类型验证一下吧 ~
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.
Got it !
Update 20231104
|
@megemini 可以merge develop重跑CI了 |
from .manipulation import atleast_1d # noqa: F401 | ||
from .manipulation import atleast_2d # noqa: F401 | ||
from .manipulation import atleast_3d # noqa: F401 |
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.
Shall we need to support the usage of x.atleast_1d(), x.atleast_2d(), x.atleast_3d()
when x
is a tensor ? If so, add them to the list of tensor_method_func
below
test/legacy_test/test_atleast_nd.py
Outdated
), | ||
) | ||
class TestAtleastErrorCombineInputs(BaseTest): | ||
"""test combine inputs, like: `at_leastNd((x, y))`, where paddle treats like numpy, not pytorch""" |
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.
please delete not pytorch
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.
cc @zhwesky2010 :atleast_xd 处理语义在遇到tuple/list时和numpy对齐,和pytorch有差异,后续在代码转换时可以关注下
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.
cc @zhwesky2010 :atleast_xd 处理语义在遇到tuple/list时和numpy对齐,和pytorch有差异,后续在代码转换时可以关注下
主要区别是:
- paddle/numpy -> paddle.atleast_1d((x, y)) 将 (x, y) 作为一个整体输入,并转换为 tensor (此处需要注意 broadcast)
- pytorch -> torch.atleast_1d((x, y)) 将 (x, y) 作为两个单独的输入 x 和 y
个人感觉 torch 这种处理方式是有歧义的,如 torch.atleast_1d((x, y)) 和 torch.atleast_1d(x, y) 效果是一样的:
In [4]: y = torch.tensor(0.3)
In [5]: x = torch.tensor(0.3)
In [6]: torch.atleast_1d(x, y)
Out[6]: (tensor([0.3000]), tensor([0.3000]))
In [7]: torch.atleast_1d((x, y))
Out[7]: (tensor([0.3000]), tensor([0.3000]))
所以,根据本次设计接口的签名: paddle.atleast_1d(*inputs),将 paddle.atleast_1d((x, y)) 作为一个输入,而 paddle.atleast_1d(x, y) 是两个输入。
请悉知 ~
Update 20231115
|
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.
LGTM
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.
LGTM @sunzhongkai588
@megemini 请补充对应的中文文档 |
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.
LGTM~
@megemini 顺师傅能修一下单测随机挂么? 单测 : test_atleast_nd, PR: 57785,CI : PR-CI-Mac-Python3,url : https://xly.bce.baidu.com/paddlepaddle/paddle/newipipe/detail/9541163 |
… API -part (PaddlePaddle#58323) * [Init] add atleast api * [Add] add atleast test * [Fix] import atleast * [Change] test_atleast.py to test_atleast_nd.py and add bool data type test * [Update] update dtype supports and unittest * [Fix] dtype error unittest * [Change] static test with test_with_pir_api * [Add] atleast_Nd as tensor method
PR types
New features
PR changes
APIs
Description
【Hackathon 5th No.33】为 Paddle 新增 atleast_1d / atleast_2d / atleast_3d API
RFC: PaddlePaddle/community#679
涉及文件:
python/paddle/__init__.py
将 API 暴露出来python/paddle/tensor/__init__.py
将 API 暴露出来python/paddle/tensor/manipulation.py
实现 APItest/legacy_test/test_atleast.py
单元测试本地测试情况:
请评审!