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

[Dy2Stat]BugFix StaticAanlysis with gast.Subscript #32969

Merged
merged 2 commits into from
May 19, 2021

Conversation

Aurelius84
Copy link
Contributor

@Aurelius84 Aurelius84 commented May 18, 2021

PR types

Bug fixes

PR changes

Others

Describe

1. PR内容

BugFix StaticAanlysis with gast.Subscript

2. 复现样例

在有关for-loop的语法转换中,依赖的变量如果是从上游tensor slice出来的,则在StaticAnalysis阶段无法被解析为Tensor类型。原因是因为StaticAnalysis中缺少对gast.Subscript语法的处理。此PR添加此类处理:如果node.value是个Variable,则slice出来的类型也是Variable,否则返回为statement类型

def test_list_append_in_for_subscript(x):
    x = fluid.dygraph.to_variable(x)
    iter_num = paddle.shape(x)[0]   # iter_num should be consider as Variable
    a = []
    for i in range(iter_num):
        x = x + 1
        a.append(x)
    out = paddle.concat(a)
    return out[0]

Before this PR, a = [] will not be converted because iter_num is not considered as Variable. The transformed codes is:

def test_list_append_in_for_subscript(x):
    x = paddle.assign(x)
    iter_num = paddle.shape(x)[0]
    a = []
    i = 0

    def for_loop_condition_0(i, iter_num, x):
        return i < iter_num

    def for_loop_body_0(i, iter_num, x):
        x = x + 1
        paddle.jit.dy2static.convert_call(a.append)(x)
        i += 1
        return i, iter_num, x
    [i, iter_num, x] = paddle.jit.dy2static.convert_while_loop(
        for_loop_condition_0, for_loop_body_0, [i, iter_num, x])
    out = paddle.concat(a)
    return out[0]

After this PR, The transformed codes is as expected:

def test_list_append_in_for_subscript(x):
    x = paddle.assign(x)
    iter_num = paddle.shape(x)[0]
    a = paddle.tensor.create_array(dtype='float32')
    i = 0

    def for_loop_condition_0(iter_num, a, i, x):
        return i < iter_num

    def for_loop_body_0(iter_num, a, i, x):
        x = x + 1
        paddle.tensor.array_write(x=x, i=paddle.tensor.array_length(a), array=a
            )
        i += 1
        return iter_num, a, i, x
    [iter_num, a, i, x] = paddle.jit.dy2static.convert_while_loop(
        for_loop_condition_0, for_loop_body_0, [iter_num, a, i, x])
    out = paddle.concat(a)
    return out[0]

@paddle-bot-old
Copy link

Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

Copy link
Member

@zhhsplendid zhhsplendid left a comment

Choose a reason for hiding this comment

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

LGTM

@Aurelius84 Aurelius84 merged commit c285261 into PaddlePaddle:develop May 19, 2021
Aurelius84 added a commit to Aurelius84/Paddle that referenced this pull request May 19, 2021
* BugFix StaticAanlysis with gast.Subscript

* remove codes
lanxianghit pushed a commit that referenced this pull request May 20, 2021
) (#32986)

* [Custom Op]Remove PADDLE_WITH_MKLDNN in custom_op  (#32903)

* [Dy2Stat]BugFix StaticAanlysis with gast.Subscript (#32969)

* BugFix StaticAanlysis with gast.Subscript

* remove codes
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.

2 participants