forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Relay][Module] Make tags for ADT constructors and ConstructorValues …
…more robust (apache#3369) * Use hash of ADT name and constructor idx to generate tag, add reverse mapping to module and use where appropriate * Lint and build fixes * Add round-tripping test for getting constructors by tag * Use int64_t everywhere for tags * Add additional identity check * Bring out _arg_to_ast again * Use 8-bit hash of GTV name as MSB of tag, index as LSB for more readable tags * Use int32 instead of int64 for tag
- Loading branch information
1 parent
f718cda
commit 6d69e63
Showing
8 changed files
with
145 additions
and
18 deletions.
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,68 @@ | ||
# 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 adt_list(p): | ||
return [p.nat, p.l, p.optional, p.tree] | ||
|
||
|
||
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 | ||
|
||
|
||
def test_constructor_tag_differences(): | ||
# ensure that if we have the type data for a given ADT, the tags | ||
# for the constructors of the *same ADT* are simple offsets from | ||
# each other | ||
mod = Module() | ||
p = Prelude(mod) | ||
add_nat_definitions(p) | ||
|
||
adts = adt_list(p) | ||
for adt in adts: | ||
data = mod[adt] | ||
for i in range(len(data.constructors) - 1): | ||
ctor1 = data.constructors[i] | ||
ctor2 = data.constructors[i + 1] | ||
assert ctor2.tag - ctor1.tag == 1 | ||
# make sure there is something present at the MSB | ||
assert ctor1.tag - i != 0 | ||
assert ctor2.tag - (i + 1) != 0 |