-
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][Module] Make tags for ADT constructors and ConstructorValues more robust #3369
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8314ee1
Use hash of ADT name and constructor idx to generate tag, add reverse…
slyubomirsky 5ce6f63
Lint and build fixes
slyubomirsky ac1ed8c
Add round-tripping test for getting constructors by tag
slyubomirsky 4840834
Use int64_t everywhere for tags
slyubomirsky 9e35c67
Add additional identity check
slyubomirsky 8737357
Bring out _arg_to_ast again
slyubomirsky 840ec93
Use 8-bit hash of GTV name as MSB of tag, index as LSB for more reada…
slyubomirsky 47ace40
Use int32 instead of int64 for tag
slyubomirsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""Tests for module functionality.""" | ||
import tvm | ||
from tvm import relay | ||
from tvm.relay import Module | ||
from tvm.relay.prelude import Prelude | ||
from tvm.relay.testing import add_nat_definitions | ||
|
||
def constructor_list(p): | ||
return [p.nil, p.cons, p.rose, p.some, p.none, p.z, p.s] | ||
|
||
|
||
def test_constructor_tag_round_trip(): | ||
mod1 = Module() | ||
p1 = Prelude(mod1) | ||
add_nat_definitions(p1) | ||
mod2 = Module() | ||
p2 = Prelude(mod2) | ||
add_nat_definitions(p2) | ||
|
||
# ensure hashes match across modules | ||
ctors1 = constructor_list(p1) | ||
ctors2 = constructor_list(p2) | ||
|
||
for i in range(len(ctors1)): | ||
tag = ctors1[i].tag | ||
ctor = mod2.get_constructor(tag) | ||
assert ctor == ctors2[i] | ||
assert ctor.name_hint == ctors1[i].name_hint |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Using hash value as tag makes opcode not very intuitive to understand, How about maintaining a continuous range [0, num_constructors) for each ADT type?
Then for the following code:
We can generate opcode like:
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.
That's a good point and I agree hashes are not ideal. The reason I went with hashes is to ensure that the tag will be determined completely by the name of the global type var and the index of the constructor in the type data and thus will not be affected, for example, by the order in which types are defined. It also means that tags will not collide across different ADTs. If hashes are undesirable, we can take the approach (as I alluded to before) of requiring a type check and only looking at the index of the constructor.
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, I vote for eliminating the hash.
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.
Should the constructor value carry information about its type, then? The case that is most difficult is the one in
arg_to_ast
: given only the constructor value, how do you work backwards?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 for VM, we might need to discard type information in
Object
. As we already drop the type information in byte codes we generate.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'll see what I can do about
arg_to_ast
thenThere 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'm not sure, though, that it will be possible to drop type information for ADT values without ensuring that all tags be globally unique (and I'm not sure of any better way to do that than hashing).
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 I agree there is no other way . And seems like interpreter has the need to map runtime value back to AST node to support user provided bindings.
How about make a 8 bit hash instead and put it in the most significant byte of the tag. So when we debug, we can easily tell the variant from the least significant byte. What do you think?
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.
Interesting suggestion, definitely more readable too. I will look into implementing that, thanks.