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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

TorchScript attempts to compile dead branch of torch.jit.is_scripting #77837

Open
sharvil opened this issue May 19, 2022 · 2 comments
Open

TorchScript attempts to compile dead branch of torch.jit.is_scripting #77837

sharvil opened this issue May 19, 2022 · 2 comments
Labels
oncall: jit Add this issue/PR to JIT oncall triage queue
Projects

Comments

@sharvil
Copy link
Contributor

sharvil commented May 19, 2022

馃悰 Describe the bug

Consider the following code. The else branch of the condition in get_context evaluates to False when scripting. Running this code still produces an exception.

import torch

meow = 1239

def get_context():
  if torch.jit.is_scripting():
    print('hi')
  else:
    global meow
    meow = 9123
    return meow

if __name__ == '__main__':
  x = torch.jit.script(get_context)
  print(x.code)

Output:

torch.jit.frontend.UnsupportedNodeError: global variables aren't supported:
  File "/mnt/data/lmnt/code/tacotron/src/lmnt/tacotron/context.py", line 9
    print('hi')
  else:
    global meow
    ~~~~~~ <--- HERE
    meow = 9123
    return meow

This is the output of the script after removing the global meow line:

def get_context() -> NoneType:
  print("hi")
  return None

Note how the dead branch is eliminated after scripting. Instead, the dead branch should be eliminated before attempting to compile it.

There are workarounds to the current behavior (e.g. move the else clause into a separate function with a @torch.jit.unused annotation) but they aren't ergonomic and often ruin the code flow.

Versions

Any version of PyTorch

@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label May 19, 2022
@github-actions github-actions bot added this to Need triage in JIT Triage May 19, 2022
@Gamrix
Copy link
Contributor

Gamrix commented Jun 3, 2022

@eellison This seems very useful, but also hard to do as the error gets thrown while we are still building the AST. Is there anything we can do here, like special casing torch.jit.is_scripting() and inlining if statements ahead of time?

@sharvil
Copy link
Contributor Author

sharvil commented Jun 3, 2022

I like the idea of special-casing torch.jit.is_scripting() until a more general solution is available.

Perhaps a more general way to handle this issue is to support constant folding and constant propagation and evaluate constant conditionals during the AST building pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
oncall: jit Add this issue/PR to JIT oncall triage queue
Projects
JIT Triage
  
Need triage
Development

No branches or pull requests

3 participants