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]remove no_value using var.name for ifelse #36513

Merged

Conversation

0x45f
Copy link
Contributor

@0x45f 0x45f commented Oct 18, 2021

PR types

Bug fixes

PR changes

Others

Describe

使用下面代码对返回结果进行讨论,if分支中有两个变量y, z,else分支中有两个变量c, z

import paddle
paddle.set_device('cpu')
def func(x, use_cache=False):
    if use_cache:
        y = x + 1
        z = x + 2
        return y,z
    else:
        c = x + 1
        z = x - 1
        # c = x - 1
        # d = x - 1
        # return c, z
        return c
        # return None
@paddle.jit.to_static
def run(x):
    out = func(x)
    # import pdb; pdb.set_trace()
    return out
x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]])
out = run(x)
print(run.code)
print(out)
print(type(out))

对于两个分支的ifelse,可以分为两种情况,一:两个分支有共有变量,二:两个分支无共有变量。下面的讨论中,else分支返回结果是指的动转静后静态图代码return语句的返回结果。

1 两个分支有共有变量,即else分支中有z变量

  • else分支没有return语句, else分支返回结果return z

  • else分支中return None,else分支返回结果((no_val_0, no_val_1), z)

  • else分支有return语句 return c,else分支返回结果((c, no_val_0), z)

  • else分支有return语句 return z, else分支返回结果((z, no_val_0), z)

  • else分支有return语句 return c, z,else分支返回结果((c, z), z)

2 两个分支无共有变量,即else分支无z变量

  • else分支没有return语句,else分支最后只有return,没有返回变量

  • else分支return None,else分支返回结果(no_val_0, no_val_1)

  • else分支return c,else分支返回结果(c, no_val_0)

  • else分支return c, d,else分支返回结果(c, d)

上面的情况需要考虑,删除用于填充的no_val

NOTE:动转静转写时ifelse中对于reutnr (a, b)return [a, b]动转静处理不同,tuple会被视为为两个变量;list被视为一个变量。

@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
Contributor

@Aurelius84 Aurelius84 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 fe01ba6 into PaddlePaddle:develop Oct 20, 2021
@0x45f 0x45f changed the title remove no_value using var.name [Dy2stat]remove no_value using var.name Oct 20, 2021
@0x45f 0x45f changed the title [Dy2stat]remove no_value using var.name [Dy2stat]remove no_value using var.name for ifelse Oct 20, 2021
0x45f added a commit to 0x45f/Paddle that referenced this pull request Oct 20, 2021
* remove no_value using var.name

* fix unit test for CI

* fix unit test

* add test case

* fix test case

* add more test case
lanxianghit pushed a commit that referenced this pull request Oct 21, 2021
* remove no_value using var.name
@0x45f 0x45f deleted the dy2stat_process_ifelse_no_value branch December 24, 2021 04:49
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