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] Fix undefined var used in For #32153

Merged
merged 2 commits into from
Apr 9, 2021

Conversation

Aurelius84
Copy link
Contributor

PR types

Bug fixes

PR changes

Others

Describe

What's New?

Fix undefind var used in For

The reason is if a var is created only in if branch, and is not recognized by parse_cond_return.

  • gast.Param should be considered as a kind of load semantic.

A Bug Example

from paddle.jit import to_static

def case_func(training):
    if training:
        targets = [1,2,3]
        targets_list = [targets]

    num_step = 3
    for i in range(num_step):
        if i > 0:
            rois, rosi_num = 1, 2
            if training:
                ros, rosi_num, targets = -1, -2, [-1,-2, -3]
                targets_list.append(targets)
    
    return rosi_num

to_static(case_func(False))

Transformed code is :

def case_func(training):

    def true_fn_0():
        targets = [1, 2, 3]
        targets_list = [targets]
        return

    def false_fn_0():
        return
    paddle.jit.dy2static.convert_ifelse(training, true_fn_0, false_fn_0, (),  <----- targets is not visible here.
        (), ())
    targets = None
    num_step = 3
    rosi_num = paddle.jit.dy2static.data_layer_not_check(name='rosi_num',
        shape=[-1], dtype='float32')
    i = 0

    def for_loop_condition_0(num_step, i, targets, rosi_num):
        return i < num_step

    def for_loop_body_0(num_step, i, targets, rosi_num):

        def true_fn_2(rosi_num, targets, training):
            rois = 1
            rosi_num = 2

            def true_fn_1(rosi_num, targets):
                y = targets
                ros = -1
                rosi_num = -2
                targets = [-1, -2, -3]
                paddle.jit.dy2static.convert_call(targets_list.append)(targets)
                return rosi_num, targets

            def false_fn_1(rosi_num, targets):
                return rosi_num, targets
            rosi_num, targets = paddle.jit.dy2static.convert_ifelse(training,
                true_fn_1, false_fn_1, (rosi_num, targets), (rosi_num,
                targets), (rosi_num, targets))
            return rosi_num, targets

        def false_fn_2(rosi_num, targets):
            return rosi_num, targets
        rosi_num, targets = paddle.jit.dy2static.convert_ifelse(i > 0,
            true_fn_2, false_fn_2, (rosi_num, targets, training), (rosi_num,
            targets), (rosi_num, targets))                              <----- targets is undefined
        i += 1
        return num_step, i, targets, rosi_num
    [num_step, i, targets, rosi_num] = paddle.jit.dy2static.convert_while_loop(
        for_loop_condition_0, for_loop_body_0, [num_step, i, targets, rosi_num]
        )
    return rosi_num

@paddle-bot-old
Copy link

paddle-bot-old bot commented Apr 8, 2021

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

Copy link
Contributor

@liym27 liym27 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 4636d13 into PaddlePaddle:develop Apr 9, 2021
@Aurelius84 Aurelius84 deleted the jit_load branch April 12, 2021 03:36
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