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

[Zero-Dim] Flatten support 0d tensor #49361

Merged
merged 14 commits into from
Jan 6, 2023

Conversation

thisjiang
Copy link
Contributor

PR types

New features

PR changes

OPs

Describe

paddle.flatten支持0D-tensor,其输出为1D-tensor

@paddle-bot
Copy link

paddle-bot bot commented Dec 27, 2022

你的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.

@@ -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([])
Copy link
Contributor

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不会求导

Copy link
Contributor Author

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')
Copy link
Contributor

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不会增加反向

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

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:
Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

这样写死axis,会检查axis报错吗

zhwesky2010
zhwesky2010 previously approved these changes Jan 5, 2023

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);
Copy link
Contributor

@zhwesky2010 zhwesky2010 Jan 5, 2023

Choose a reason for hiding this comment

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

这个应该不用+1,不过问题不大

@zhwesky2010 zhwesky2010 changed the title Flatten support 0dtensor 【Zero-Dim】Flatten support 0dtensor Jan 5, 2023
@zhwesky2010 zhwesky2010 changed the title 【Zero-Dim】Flatten support 0dtensor 【Zero-Dim】Flatten support 0d tensor Jan 5, 2023
Copy link
Contributor

@ZzSean ZzSean left a 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

@thisjiang thisjiang merged commit 0093aaa into PaddlePaddle:develop Jan 6, 2023
@thisjiang thisjiang deleted the flatten_support_0dtensor branch January 6, 2023 03:41
@zhwesky2010 zhwesky2010 changed the title 【Zero-Dim】Flatten support 0d tensor [Zero-Dim] Flatten support 0d tensor May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants