Skip to content

Conversation

NKNaN
Copy link
Contributor

@NKNaN NKNaN commented Jul 2, 2024

PR Category

User Experience

PR Types

Bug fixes

Description

  1. 本 pr 是修复 【Hackathon 6th No.9】Add cartesian_prod API to Paddle -part #65605 (comment) 出现的问题:静态图下
    paddle.reshape 的输入如果是 zero-size tensor,结果返回的是 None。
    修改前:

    paddle.enable_static()
    startup_program = paddle.static.Program()
    main_program = paddle.static.Program()
    executor = paddle.static.Executor()
    data_shape = [2, 1, 0]
    data_np = np.ones(d_shape, 'float32')
    with paddle.static.program_guard(main_program, startup_program):
        x = paddle.static.data(
            name="x", shape=d_shape, dtype='float32'
        )
        out = paddle.reshape(data_shape, [1, 0])
        fetch_list = [out]
        feeds = {"x": data_np}
        executor.run(startup_program)
        res = executor.run(
            main_program, feed=feeds, fetch_list=fetch_list
        )
    print(res[0])
    # None

    修改后:

    paddle.enable_static()
    startup_program = paddle.static.Program()
    main_program = paddle.static.Program()
    executor = paddle.static.Executor()
    data_shape = [2, 1, 0]
    data_np = np.ones(d_shape, 'float32')
    with paddle.static.program_guard(main_program, startup_program):
        x = paddle.static.data(
            name="x", shape=d_shape, dtype='float32'
        )
        out = paddle.reshape(data_shape, [1, 0])
        fetch_list = [out]
        feeds = {"x": data_np}
        executor.run(startup_program)
        res = executor.run(
            main_program, feed=feeds, fetch_list=fetch_list
        )
    print(res[0])
    # []
    print(res[0].shape)
    # [1, 0]
  2. 本 pr 把之前对 infermeta/unary.cc 中 ValidateShape 的修改迁移到了 fluid/operator/reshape_op.cc 的 ValidateShape 中,
    之前的修改是:【Hackathon 6th No.9】Update ValidateShape to support zero sized tensor flatten -part #64715 ,相比当时修改之前增加支持的 case 是:当输入是
    zero-size tensor,且 output_shape 中 0 出现的位置小于 in_dims.size(),例如:
    修改前:

    x = paddle.rand([2, 1, 0])
    x = paddle.reshape(x, [0])
    print(x)
    # ValueError: (InvalidArgument) The 'shape' in ReshapeOp is invalid. The input tensor X'size must be equal to the capacity 
    # of 'shape'. But received X's shape = [2, 1, 0], X's size = 0, 'shape' is [0], the capacity of 'shape' is 2.
    #  [Hint: Expected capacity == in_size, but received capacity:2 != in_size:0.] (at ..\paddle\phi\infermeta\unary.cc:1967)

    修改后:

    x = paddle.rand([2, 1, 0])
    x = paddle.reshape(x, [0])
    print(x)
    # Tensor(shape=[0], dtype=float32, place=Place(cpu), stop_gradient=True,
    #  [])

Copy link

paddle-bot bot commented Jul 2, 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 Jul 2, 2024
Copy link
Contributor

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

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

这个修改有没有不兼容影响呢,PR描述里 写一下被修改了的case情况吧

in_dims.size()));
output_shape[i] = shape[i];
} else if (shape[i] == 0) {
if (static_cast<int>(i) < in_dims.size()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

能否以 if (in_size == 0) 来写一个0-Size分支,上下两个分支合并,这样代码看起来清晰些

Copy link
Contributor Author

Choose a reason for hiding this comment

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

那就改成像原来这样吗:#50389
原来这样应该是可以支持 [2, 3, 0, 4] -> [0, 4] 得到 [0, 4] 的case

Copy link
Contributor

Choose a reason for hiding this comment

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

那就改成像原来这样吗:#50389 原来这样应该是可以支持 [2, 3, 0, 4] -> [0, 4] 得到 [0, 4] 的case

就是你想改的这个写法 也可以写成if (in_size==0) 和 else两个分支的形式

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

@NKNaN
Copy link
Contributor Author

NKNaN commented Jul 4, 2024

这个会有不兼容影响吗,PR描述里 写一下支持的case情况吧

应该没有不兼容影响,这个pr是想修复静态图输出是 None 的问题,上一个 pr 是增加了支持的 case

@zhwesky2010
Copy link
Contributor

这个会有不兼容影响吗,PR描述里 写一下支持的case情况吧

应该没有不兼容影响,这个pr是想修复静态图输出是 None 的问题,上一个 pr 是增加了支持的 case

这个在描述里具体举例一下适配了哪种case及前后的变换吧,因为你在其他PR中遇到的问题,其他人review也不了解背景。

@NKNaN
Copy link
Contributor Author

NKNaN commented Jul 4, 2024

这个在描述里具体举例一下适配了哪种case及前后的变换吧,因为你在其他PR中遇到的问题,其他人review也不了解背景。

好的

"But received shape[%d] = 0, X's dimensions = %d."
% (dim_idx, len(x.shape))
)
import math
Copy link
Member

Choose a reason for hiding this comment

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

这个 import 放全局吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

@luotao1 luotao1 merged commit 61bb43a into PaddlePaddle:develop Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants