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

[Bugfix][TVMScript] Capture fails if var appears only in annotation #14849

Merged

Conversation

junrushao
Copy link
Member

Consider the case below:

dtype = "float32"

@T.prim_func:
def f(
  A: T.Buffer((1, ), dtype),
  B: T.Buffer((1, ), dtype),
):
  ...

The variable dtype only appears in the type annotation of the function being parsed. In this case, the python interpreter will evaluate the annotation first before invoking the decorator, and thus if dtype doesn't appear in the function body, it will not be considered as being captured by the function itself. As a result, inspect module will be unable to supply the value of dtype during parsing, leading to failure.

This PR fixes the bug by maintaining a copy of function annotations that are already parsed. Whenever expression evaluation fails during parsing, it falls back to using the copy that is evaluated by python interpreter.

Consider the case below:

```python
dtype = "float32"

@T.prim_func:
def f(
  A: T.Buffer((1, ), dtype),
  B: T.Buffer((1, ), dtype),
):
  ...
```

The variable `dtype` only appears in the type annotation of the function
being parsed. In this case, the python interpreter will evaluate the
annotation first before invoking the decorator, and thus if `dtype`
doesn't appear in the function body, it will not be considered as being
captured by the function itself. As a result, `inspect` module will be
unable to supply the value of `dtype` during parsing, leading to
failure.

This PR fixes the bug by maintaining a copy of function annotations
that are already parsed. Whenever expression evaluation fails during
parsing, it falls back to using the copy that is evaluated by python
interpreter.
@tvm-bot
Copy link
Collaborator

tvm-bot commented May 14, 2023

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

Generated by tvm-bot

@junrushao junrushao marked this pull request as ready for review May 14, 2023 00:01
@junrushao
Copy link
Member Author

CC: @tqchen @cyx-6

Copy link
Contributor

@cyx-6 cyx-6 left a comment

Choose a reason for hiding this comment

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

Great fix!

@junrushao
Copy link
Member Author

A side note: we might want to migrate all report_error to raising ParserError to avoid nested error reporting

@Hzfengsy Hzfengsy merged commit 9f0c642 into apache:main May 14, 2023
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.

4 participants