-
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
[Zero-Dim] Flatten support 0d tensor #49361
[Zero-Dim] Flatten support 0d tensor #49361
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
@@ -712,6 +712,16 @@ def test_scatter_nd(self): | |||
self.assertEqual(out.numpy()[3], 2) | |||
self.assertEqual(out.grad.shape, [5]) | |||
|
|||
def test_flatten(self): | |||
x = paddle.rand([]) |
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.
需要设置x.stop_gradient=False,然后测下反向的shape,不然backward不会求导
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.
Done
@@ -914,6 +924,21 @@ def test_scatter_nd(self): | |||
self.assertEqual(res[0].shape, (5,)) | |||
self.assertEqual(res[0][3], 2) | |||
|
|||
@prog_scope() | |||
def test_flatten(self): | |||
x = paddle.full([], 1, 'float32') |
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.
需要设置x.stop_gradient=False,然后测下反向的shape,不然append_backward不会增加反向
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.
Done
python/paddle/tensor/manipulation.py
Outdated
stop_axis = stop_axis + x_dim | ||
if start_axis > stop_axis: | ||
raise ValueError("The stop_axis should be larger than stat_axis") | ||
if x_dim > 0: |
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.
这样是否会导致没有正确的axis检查,0D时axis必须限制为[0, -1],其他情况应该报错,可以看下torch的
@@ -1075,21 +1075,30 @@ void FlattenWithXShapeInferMeta(const MetaTensor& x, | |||
MetaTensor* xshape) { | |||
auto x_dims = x.dims(); | |||
int in_dims_size = x_dims.size(); | |||
|
|||
if (in_dims_size == 0) { |
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.
这样写死axis,会检查axis报错吗
|
||
int64_t outer = 1; | ||
std::vector<int32_t> out_shape; | ||
out_shape.reserve(in_dims_size - stop_axis + start_axis); | ||
out_shape.reserve(in_dims_size - stop_axis + start_axis + 1); |
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.
这个应该不用+1,不过问题不大
… flatten_support_0dtensor
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 for CI-OP-Benchmark
PR types
New features
PR changes
OPs
Describe
paddle.flatten
支持0D-tensor,其输出为1D-tensor