-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Relay] Better shape inference in TensorFlow Frontend. #3176
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
Conversation
@srkreddy1238 @Huyuwei @kazum please help review this PR |
@jwfromm Can you add some test cases for new operators and also to cover the infer_value additions. Thanks |
Test cases are nearly finished, however I think I've found an unrelated Relay bug on the master branch. It looks like Relay fails to build a graph with a simple fill operation when using opt_level >= 2. Here's a snippet of code that can be run to demonstrate the error.
This triggers the error The above code works as expected with opt_level 0 or 1. Any thoughts on why this is happening? It makes it difficult to test one of the cases for infer_value. |
@jwfromm I think this is probably because const folding optimized the |
2403f6a
to
f116baa
Compare
Just pushed with added tests for depth_to_space, resize_bilinear, and fill. In the latter two tests, the output shape is based on the value of placeholders, which requires infer_value to have a proper output. At high opt_level, relay was trying to optimize the fill operation away, so I added an argument to change the opt level used during testing. Currently fill is the only test that needs this but it's a decent option to have for future tests. |
@srkreddy1238 @zhiics please take another look and https://docs.tvm.ai/contribute/code_review.html#approve-and-request-changes-explicitly |
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.
It mostly looks good to me. Just left some minor comments. It would be good if @srkreddy1238 or @yongwww can take a look as well.
f116baa
to
95f6830
Compare
I know it's not great to add ops so late in the review, but I've also added the BroadcastTo op to the tensorflow parser and corresponding tests. This op also requires infer_value so it seems like it makes sense to include in this PR. It's actually a very common operation in tensorflow so we definitely want to include it eventually regardless. |
|
||
__all__ = ['from_tensorflow'] | ||
|
||
def _infer_value(input_val, params): |
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.
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.
Do you think something like this function could help?
https://github.com/dmlc/tvm/blob/master/python/tvm/relay/backend/interpreter.py#L136
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 doubt it fits here.
What we need here is list of Var nodes current Expr is dependent on recursively.
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.
Can you just take free_vars if it is an expr? Var has name_hint.
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.
free_vars should work.
@jwfromm can you cross check free_vars with params and add an assert ?
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.
Added in the latest push.
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 @jwfromm . Good to go now.
* Some bug fixes in tensorflow graph converter and added DepthToSpace operator. * Made DepthToSpace better comply with other function syntax. * Added better shape inference for unusual situations. * Lint fixes. * Added depthtospace test. * Added test cases for value inference and depthtospace. * Added fill testing. * Made comment changes and added BroadcastTo op and tests. * Fixed underlining and unneeded opt_level forcing. * Added _infer_value assertion that all values to infer are available in passed parameters.
* Some bug fixes in tensorflow graph converter and added DepthToSpace operator. * Made DepthToSpace better comply with other function syntax. * Added better shape inference for unusual situations. * Lint fixes. * Added depthtospace test. * Added test cases for value inference and depthtospace. * Added fill testing. * Made comment changes and added BroadcastTo op and tests. * Fixed underlining and unneeded opt_level forcing. * Added _infer_value assertion that all values to infer are available in passed parameters.
In some cases it may be necessary to infer the value of a tensor to determine the shape. For example Fill, Reshape, and Resize have shape arguments that must be known. Reshape already handled this but fill and resize did not. I've added a function that infers the value of an input and sets the appropriate output shape for these classes.
Also added a DepthToSpace operation, a mapping from ResizeBicubic to _resize_bilinear (better than nothing for now), and added flexibility to bias add for non NHWC layers.