Skip to content
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][Parser][Bugfix] Fix parsing hierarchical attibute names #7976

Merged
merged 2 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/parser/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ class Parser {
DLOG(INFO) << "Parser::ParseAttrs";
Map<String, ObjectRef> kwargs;
while (Peek()->token_type == TokenType::kIdentifier) {
auto key = Match(TokenType::kIdentifier).ToString();
auto key = GetHierarchicalName(ParseHierarchicalName().data);
Match(TokenType::kEqual);
// TOOD(@jroesch): syntactically what do we allow to appear in attribute right hand side.
auto value = ParseAttributeValue();
Expand Down Expand Up @@ -1545,18 +1545,7 @@ class Parser {
auto spanned_idents = ParseHierarchicalName();
auto idents = spanned_idents.data;
auto span = spanned_idents.span;
ICHECK_NE(idents.size(), 0);
std::stringstream op_name;
int i = 0;
int periods = idents.size() - 1;
for (auto ident : idents) {
op_name << ident;
if (i < periods) {
op_name << ".";
i++;
}
}
return GetOp(op_name.str(), span);
return GetOp(GetHierarchicalName(idents), span);
}
}
case TokenType::kGraph: {
Expand Down Expand Up @@ -1696,6 +1685,21 @@ class Parser {
return Spanned<Array<String>>(idents, span);
}

std::string GetHierarchicalName(Array<String> idents) {
ICHECK_NE(idents.size(), 0);
std::stringstream hierarchical_name;
int i = 0;
int periods = idents.size() - 1;
for (auto ident : idents) {
hierarchical_name << ident;
if (i < periods) {
hierarchical_name << ".";
i++;
}
}
return hierarchical_name.str();
}

/*! \brief Parse a shape. */
Array<tvm::PrimExpr> ParseShape() {
auto dims = ParseSequence<tvm::PrimExpr>(
Expand Down
7 changes: 7 additions & 0 deletions tests/python/relay/test_ir_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,13 @@ def test_tokenize_inf():
mod = relay.transform.AnnotateSpans()(mod)


def test_func_attrs():
attrs = tvm.ir.make_node("DictAttrs", **{"Primitive": 1, "relay.reshape_only": 1})
x = relay.var("x", shape=(2, 3))
func = relay.Function([x], relay.reshape(x, (-1,)), attrs=attrs)
assert_parses_as(func.astext(), func)


if __name__ == "__main__":
import sys

Expand Down