-
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
[Unity][TVMScript] Optionally hide StructInfo that can be inferred #16356
[Unity][TVMScript] Optionally hide StructInfo that can be inferred #16356
Conversation
Thanks, @Lunderberg for the improvement. I like this PR to print in a concise format. But I'm not sure the name of |
Thank you, @Hzfengsy, and I had been wondering on the name. Referring to struct info in the name would make it clearer how the option impacts printing of relax functions, but would restrict its usage to only relax functions. Referring to type annotations in the name would make it less clear for relax, but would allow the option to have a similar functionality for TIR. I don't have any strong preference on the name, and will update |
Just want to bring up some background on annotations and complications in struct info. Given the complication of the relax struct info annotation, we cannot guanrantee that the struct info can always be inferred in a reproducible manner from rhs. This is also one reason why we always require the annotation to be printed out. The primary reason is when the struct info itself can involve complicated bindings
Removing the annotation def example(x: R.Tensor):
s = shape_func(x)
s1 = shape_func(x)
y: R.Tensor[s1] = opaque_fn(x) While semantically it is still ok after DCE, however, it is not always desirable. more complicated case can ocurr when the set of annotations involve other set of computations, where some may have common sub expressions (e.g. same shape_func calls being combined after some pass). The main take-away here is that in order for the parser to be able to fully roundtrippable, we would need to rely on the annotations on the lhs. While it is ok to toggle off the signature printing, I think we should note that this will no longer guarantee roundtripping. |
@tqchen Thank you on the details, and agreed that this is primarily for ease of readability. Until and unless the hidden struct info can round-trip in all cases, it shouldn't be used for saving results. For the opaque functions, I think the current implementation will avoid the issue you described. In order to hide the struct info, three conditions need to hold. (1) The I don't see any unit tests that currently use opaque functions. Are there examples somewhere so I could add an appropriate unit test? |
Get it, this is mainly for future proofing so no test cases is necessary atm. Thanks for explaination, as long as it defaults to false and we know the caveat i think it is ok |
Sounds good. The test case would be primarily for future-proofing. Since this can improve readability for many cases, I can imagine a later PR being submitted to change the default, especially if the author is unaware of the potential pitfalls (i.e. like me prior to this thread). A test case for a known case that is difficult to round-trip correctly would catch the error when that hypothetical PR is submitted. |
I am very much in favor of having test cases to avoid regression. |
a87931a
to
6e60cc3
Compare
Updated to target the |
6e60cc3
to
b702e13
Compare
CI is now passing on |
attempt_to_hide_struct_info = true; | ||
} | ||
} | ||
if (attempt_to_hide_struct_info) { |
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.
Hm, I wonder if this is more complicated than what we would really want. Perhaps a syntactic rule for when to write StructInfo might make more sense? E.g., have it for op calls but not tuple indices, etc.? I'm not sure what situations are sufficiently non-obvious to require an annotation if a user wants to hide them. It's not wrong, but it seems a little unusual for the TVMScript printer to be running type inference itself.
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.
My worry with having separate rules for it is that those rules could diverge from the actual struct inference. Even when the syntactic rules allow the struct info to be hidden, it would still need to compare against the actual struct info to see if the annotations provide different information. That comparison would require StructInfo to compare against, and so this function would effectively be another implementation of InferStructInfo
, but one which could get out of sync with the canonical version.
By implementing it in terms of the struct inference, the same inference rules are used for both parsing and printing. Just as parsing can fill in missing information by calling the type inference, the printing can check if information can be omitted by calling the type inference.
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 seems a little like overkill, but we are running the normalizer between every pass anyway so I guess it's not so bad.
e: And you're right, it's futureproof too.
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.
Yeah, probably overkill, but I tend to be more okay with overkill for non-default behavior. Good point on the comparison with normalizing on each 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.
As a futureproof way of condensing the amount of StructInfo printed, this seems fine given the discussion above.
Sounds good, and thank you for the review! Before merging, I'm going to rebase onto main, since the CI results look a bit out of date. |
By default, TVMScript prints the struct info of every variable being bound, which can become quite verbose. This commit adds the configuration option `show_inferable_type_annotations`, which determines whether struct info annotations are shown in cases where they can be inferred. The `show_inferable_type_annotations` option defaults to `True`, preserving the current default behavior.
b702e13
to
f11725f
Compare
Merged from main to PR branch, to resolve CI breakage that was fixed in #16546. |
Ooops looks like the Python decorator here causes clang warning when compiling every .cc file tvm/include/tvm/node/script_printer.h Lines 89 to 91 in 2b813ec
I wonder if we want to find a work around here? |
Fixed in #16571 |
By default, TVMScript prints the struct info of every variable being bound, which can become quite verbose. This commit adds the configuration option
show_inferable_type_annotations
, which determines whether struct info annotations are shown in cases where they can be inferred.The
show_inferable_type_annotations
option defaults toTrue
, preserving the current default behavior.