-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Conversation
chunit-quic
commented
Nov 16, 2022
•
edited
Loading
edited
- 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
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 |
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. |
There was a problem hiding this 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.
python/tvm/relay/frontend/common.py
Outdated
|
||
|
||
def _should_fill_span(): | ||
should_fill_span = os.environ.get("TVM_SPANFILLING", "1") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
@FrozenGene @areusch @mbs-octoml |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks
There was a problem hiding this 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!
python/tvm/testing/utils.py
Outdated
|
||
class _control_span_filling: | ||
def __init__(self, on=True): | ||
self._old_state = os.environ["TVM_SPANFILLING"] if "TVM_SPANFILLING" in os.environ else None |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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}
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. :)
from tvm.relay.expr_functor import ExprVisitor | ||
|
||
|
||
def _set_span(expr, src): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@@ -997,3 +1002,167 @@ def try_resolve_var_to_const(x, graph_params): | |||
return _op.const(value, dtype) | |||
|
|||
return x | |||
|
|||
|
|||
class _SpanFiller(ExprMutator): |
There was a problem hiding this comment.
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++?
There was a problem hiding this comment.
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.
- First of all, considering the profiling result in the preRFC, the frontend conversion time is not increased too much.
- 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. :)
There was a problem hiding this comment.
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
- 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
6ac7339
to
3a873d9
Compare
Hi @areusch, |
There was a problem hiding this 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
There was a problem hiding this 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): |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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
python/tvm/relay/frontend/common.py
Outdated
def visit_function(self, fn): | ||
new_params = [self.visit(x) for x in fn.params] | ||
new_body = self.visit(fn.body) | ||
return _function.Function( |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
python/tvm/testing/utils.py
Outdated
|
||
class _control_span_filling: | ||
def __init__(self, on=True): | ||
self._old_state = os.environ["TVM_SPANFILLING"] if "TVM_SPANFILLING" in os.environ else None |
There was a problem hiding this comment.
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
4f0e02e
to
5a2cfd8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @chunit-quic!
- 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>
- 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>