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

[Relay][Frontend] Span filling common API #13402

Merged
merged 4 commits into from
Dec 27, 2022

Conversation

chunit-quic
Copy link
Contributor

@chunit-quic chunit-quic commented Nov 16, 2022

  • Expose and add span attribute of Expr-derived types from C++ to Python
  • Add common API of span filling
  • Add test cases of span filling
  • Add function to control whether to fill span via environment variable

@tvm-bot
Copy link
Collaborator

tvm-bot commented Nov 16, 2022

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.

  • No users to tag found in teams: relay, frontend See #10317 for details
  • Built docs for commit 4f0e02e can be found here.

Generated by tvm-bot

@chunit-quic
Copy link
Contributor Author

chunit-quic commented Nov 16, 2022

Hi community,

This is the very first PR of the RFC, TVM Explorer Infrastructure. We spend a while to extract the code blocks, and verify them after rebasing. Feel free to give us any comment. Thank you! :D

For your reference, here is the related links.
PreRFC in forum
RFC in git
Tracking issue

@haowhsu-quic, @zack-ch

Copy link
Contributor

@echuraev echuraev left a comment

Choose a reason for hiding this comment

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

I have read the RFC about TVM Exprorer. It looks awesome! Thank you for your contribution. Added few minor comments.



def _should_fill_span():
should_fill_span = os.environ.get("TVM_SPANFILLING", "1")
Copy link
Contributor

Choose a reason for hiding this comment

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

Will we document somewhere how to use span filling and describe variable TVM_SPANFILLING?
At least, probably we should add information about this variable to the set_span method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for point out. :D
I have pushed a patch with documentation below set_span(). This document includes environment variable setting, how to use and example.

python/tvm/relay/frontend/common.py Outdated Show resolved Hide resolved
@chunit-quic
Copy link
Contributor Author

@FrozenGene @areusch @mbs-octoml
Tag reviewers in the forum thread and previously reverted PR.
It would be a big help to have your advices. Thank you! :D

Copy link
Contributor

@echuraev echuraev left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks

Copy link
Contributor

@areusch areusch left a comment

Choose a reason for hiding this comment

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

thanks @chunit-quic and sorry for the delay--i've been out on PTO for a while. i've left some comments here, overall this is a great addition!


class _control_span_filling:
def __init__(self, on=True):
self._old_state = os.environ["TVM_SPANFILLING"] if "TVM_SPANFILLING" in os.environ else None
Copy link
Contributor

Choose a reason for hiding this comment

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

just curious why you are consulting os.envrion here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because we can command like the following way to get rid of span without modifying the code. It would be a bit more convenient.

TVM_SPANFILLING=0 python ${your_program.py}

Copy link
Contributor

Choose a reason for hiding this comment

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

right--sorry perhaps I should have commented on _should_fill_span in python/tvm/relay/frontend/common.py. what I mean is--could we consider a global variable or a PassContext option which can be initialized from the env should a driver script want, rather than forcing this control to be in the environment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem, I change the way from using the environment variable to the passContext config. :)

src/relay/ir/expr.cc Show resolved Hide resolved
tests/python/frontend/test_common.py Outdated Show resolved Hide resolved
tests/python/frontend/test_common.py Outdated Show resolved Hide resolved
from tvm.relay.expr_functor import ExprVisitor


def _set_span(expr, src):
Copy link
Contributor

Choose a reason for hiding this comment

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

we have an analogue WithFields in the C++, I think this would be better placed there. for now, see FunctionWithFields.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for let us know that we can leverage this function. :D
Yet about this _set_span, it is just a simple helper function and only used in the tests. Would you hope to add the functions like CallWithFields or TupleWithFields in C++ and expose them to python side in this patch? Or perhaps this _set_span is enough for the test cases?

Copy link
Contributor

Choose a reason for hiding this comment

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

i think if you're going to do something like this, it would be great to just add e.g. CallWithFields and TupleWithFields properly to the FFI now. no need to migrate any other code, but keeping this code in test here seems like it's asking for two implementations to surface. lmk if this is too much of a burden.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing! I have exposed them from C++ and change the functions.

tests/python/relay/utils/tag_span.py Outdated Show resolved Hide resolved
@@ -997,3 +1002,167 @@ def try_resolve_var_to_const(x, graph_params):
return _op.const(value, dtype)

return x


class _SpanFiller(ExprMutator):
Copy link
Contributor

Choose a reason for hiding this comment

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

we had been implementing a bunch of passes in C++, since the Python visitor infra involves a lot of calls that cross the FFI. I wonder if you'd be up for moving this into C++?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Currently we don't have plan to migrate them to a C++ pass. There are two reasons.

  1. First of all, considering the profiling result in the preRFC, the frontend conversion time is not increased too much.
  2. Second, the fill() and __init__() function heavily interact with different frontends. We would like to keep the flexibility to modify them via python script quickly. For example, in the min_max_common conversion of PyTorch, it might end up being a None, which is not common in other frontends.

However, if the performance of frontend conversion is significantly decreased by SpanFiller in some cases, we can arrange a patch to move the mutator part (visitor funcions) to C++ pass. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, given for now we want to invoke this after the import, we can keep it as Python for now

Joey Tsai added 3 commits November 24, 2022 09:44
- Expose and add span attribute of Expr-derived types from C++ to Python
- Add common API of span filling
- Add test cases of span filling
- Add function to control whether to fill span via environment variable
- Modify the way of pretty-print to print span
- Change based on comment
- Discard the change of pretty-print
- Add document to set_span
- Change the test cases to pytest style
- Group the set_span test cases to a class
@chunit-quic
Copy link
Contributor Author

thanks @chunit-quic and sorry for the delay--i've been out on PTO for a while. i've left some comments here, overall this is a great addition!

Hi @areusch,
Thank you for reviewing!
We have modified several parts based on your previous comments. :D
Take your time (It's thanks giving recently.) We will wait for your reply.

Copy link
Contributor

@areusch areusch left a comment

Choose a reason for hiding this comment

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

sorry for the delay @chunit-quic , one more question here

src/relay/ir/expr.cc Show resolved Hide resolved
Copy link
Contributor

@areusch areusch left a comment

Choose a reason for hiding this comment

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

sorry for the long delay here! i just have a couple of actionable comments, but otherwise I'm good with this PR landing.

from tvm.relay.expr_functor import ExprVisitor


def _set_span(expr, src):
Copy link
Contributor

Choose a reason for hiding this comment

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

i think if you're going to do something like this, it would be great to just add e.g. CallWithFields and TupleWithFields properly to the FFI now. no need to migrate any other code, but keeping this code in test here seems like it's asking for two implementations to surface. lmk if this is too much of a burden.

@@ -997,3 +1002,167 @@ def try_resolve_var_to_const(x, graph_params):
return _op.const(value, dtype)

return x


class _SpanFiller(ExprMutator):
Copy link
Contributor

Choose a reason for hiding this comment

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

ok, given for now we want to invoke this after the import, we can keep it as Python for now

def visit_function(self, fn):
new_params = [self.visit(x) for x in fn.params]
new_body = self.visit(fn.body)
return _function.Function(
Copy link
Contributor

Choose a reason for hiding this comment

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

want to use FunctionWithFields here too? (and similar below)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I have changed them in the latest patch.


class _control_span_filling:
def __init__(self, on=True):
self._old_state = os.environ["TVM_SPANFILLING"] if "TVM_SPANFILLING" in os.environ else None
Copy link
Contributor

Choose a reason for hiding this comment

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

right--sorry perhaps I should have commented on _should_fill_span in python/tvm/relay/frontend/common.py. what I mean is--could we consider a global variable or a PassContext option which can be initialized from the env should a driver script want, rather than forcing this control to be in the environment?

- Expose Relay Expr WithFields APIs to python side
- Change the APIs in _SpanFiller from creating new instance to
  WithFields.
- Change the control of frontend span filler from env var to the
  passcontext config
Copy link
Contributor

@areusch areusch left a comment

Choose a reason for hiding this comment

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

thanks @chunit-quic!

@areusch areusch merged commit 520f2c5 into apache:main Dec 27, 2022
fzi-peccia pushed a commit to fzi-peccia/tvm that referenced this pull request Mar 27, 2023
- Expose and add span attribute of Expr-derived types from C++ to Python
- Add common API of span filling
- Add test cases of span filling
- Add function to control whether to fill span via environment variable
- Modify the way of pretty-print to print span

Co-authored-by: Joey Tsai <chunit@qti.qualcomm.com>
mikeseven pushed a commit to mikeseven/tvm that referenced this pull request Sep 27, 2023
- Expose and add span attribute of Expr-derived types from C++ to Python
- Add common API of span filling
- Add test cases of span filling
- Add function to control whether to fill span via environment variable
- Modify the way of pretty-print to print span

Co-authored-by: Joey Tsai <chunit@qti.qualcomm.com>
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