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][Prelude] Use the Relay parser to define the Relay prelude #3043

Merged
merged 26 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ef42cc2
Add ability to load Prelude from disk
jroesch Apr 18, 2019
6ddc598
Port over id
jroesch Apr 18, 2019
612b7a2
Define compose
jroesch Apr 18, 2019
0a3df46
Linting errors and style changes
slyubomirsky May 30, 2019
c282ac7
Eliminate unnecessary parens
slyubomirsky May 30, 2019
5598aa8
Rename identType to typeIdent (makes more sense)
slyubomirsky May 30, 2019
b6a1241
Another unnecessary paren
slyubomirsky May 30, 2019
fa1be6c
Bump the version number for the text format
slyubomirsky May 30, 2019
47fe2d1
Ensure .rly (Relay text files) are permitted
slyubomirsky May 30, 2019
093c614
Correct release number and simplify grammar rule
slyubomirsky May 30, 2019
e8b6f6c
Correct load_prelude docstring
slyubomirsky May 30, 2019
d567278
Corrections to _parser
slyubomirsky May 30, 2019
57d93f6
Add Apache headers to prelude source file
slyubomirsky May 30, 2019
e7a9796
Remove test_prelude (redundant)
slyubomirsky May 30, 2019
a56f2f2
Correct misleading error message
slyubomirsky May 30, 2019
137df1a
Add check that parser is enabled in Prelude
slyubomirsky May 30, 2019
24d89a5
Commit pre-generated parser, ensure generated files are treated as bi…
slyubomirsky May 31, 2019
b504501
Permit parser files and git attributes files
slyubomirsky May 31, 2019
72a98b4
Exclude gitattributes and parser files from apache check
slyubomirsky May 31, 2019
67e7237
Another attempt at appeasing Apache audit checker
slyubomirsky May 31, 2019
e2799ed
Corrections to rat-excludes
slyubomirsky May 31, 2019
d56349d
Apache should be truly appeased now
slyubomirsky May 31, 2019
790d5f7
Ignore Relay parser files by name
slyubomirsky May 31, 2019
e417e3e
Mark parser files as generated so they don't show up on Github
slyubomirsky Jun 3, 2019
e67b1b5
Add parsing helper function for tests
slyubomirsky Jun 4, 2019
6e08ad5
Mark parser files as not detectable
slyubomirsky Jun 5, 2019
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
42 changes: 31 additions & 11 deletions python/tvm/relay/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,12 @@ def visitProg(self, ctx):
self.visit_list(ctx.defn())
return self.module

return self.visit(ctx.expr())
if ctx.expr():
return self.visit(ctx.expr())

# Exprs
return self.module

# Exprs
def visitOpIdent(self, ctx):
# type: (RelayParser.OpIdentContext) -> op.Op
return op.get(ctx.CNAME().getText())
Expand Down Expand Up @@ -368,14 +370,25 @@ def mk_func(self, ctx):
self.enter_var_scope()
# Capture type params in params.
self.enter_type_param_scope()
type_params = ctx.typeParamSeq()

if type_params is not None:
type_params = type_params.ident()
assert type_params
for ty_param in type_params:
name = ty_param.getText()
self.mk_typ(name, ty.Kind.Type)

var_list, attr_list = self.visit(ctx.argList())
ret_type = self.getType_(ctx.type_())

body = self.visit(ctx.body())
# NB(@jroesch): you must stay in the type parameter scope until
jroesch marked this conversation as resolved.
Show resolved Hide resolved
# after you exit the body, you can reference the type parameters
# of your parent scopes.
type_params = list(self.exit_type_param_scope())
if type_params:
_, type_params = zip(*type_params)

body = self.visit(ctx.body())
self.exit_var_scope()

attrs = tvm.make.node("DictAttrs", **attr_list) if attr_list is not None else None
Expand Down Expand Up @@ -453,16 +466,23 @@ def visitIncompleteType(self, ctx):
# type (RelayParser.IncompleteTypeContext) -> None:
return None

def visitIdentType(self, ctx):
# type: (RelayParser.IdentTypeContext) -> Union[ty.TensorType, str]
ident_type = ctx.CNAME().getText()
def visitTypeIdent(self, ctx):
# type: (RelayParser.TypeIdentContext) -> Union[ty.TensorType, str]
'''
Handle type identifier.
'''
type_ident = ctx.CNAME().getText()

# look through all type prefixes for a match
# Look through all type prefixes for a match
for type_prefix in TYPE_PREFIXES:
if ident_type.startswith(type_prefix):
return ty.scalar_type(ident_type)
if type_ident.startswith(type_prefix):
return ty.scalar_type(type_ident)

type_param = lookup(self.type_param_scopes, type_ident)
if type_param is not None:
return type_param

raise ParseError("Unknown builtin type: {}".format(ident_type))
raise ParseError("Unknown builtin type: {}".format(type_ident))

# def visitCallType(self, ctx):
# # type: (RelayParser.CallTypeContext) -> Union[expr.Expr, ty.TensorType]
Expand Down
19 changes: 12 additions & 7 deletions python/tvm/relay/grammar/Relay.g4
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

grammar Relay;

SEMVER: 'v0.0.1' ;
SEMVER: 'v0.0.2' ;

// Lexing
// comments
Expand Down Expand Up @@ -111,8 +111,8 @@ expr
// | 'debug' # debug
;

func: 'fn' '(' argList ')' ('->' type_)? body ;
defn: 'def' ident '(' argList ')' ('->' type_)? body ;
func: 'fn' typeParamSeq? '(' argList ')' ('->' type_)? body ;
defn: 'def' ident typeParamSeq? '(' argList ')' ('->' type_)? body ;

argList
: varList
Expand All @@ -132,15 +132,20 @@ attr: CNAME '=' expr ;
// relations: 'where' relation (',' relation)* ;
// relation: ident '(' (type_ (',' type_)*)? ')' ;

typeParamSeq
: '[' ']'
| '[' ident (',' ident)* ']'
;

type_
: '(' ')' # tupleType
| '(' type_ ',' ')' # tupleType
| '(' type_ (',' type_)+ ')' # tupleType
| identType # identTypeType
| typeIdent # typeIdentType
| 'Tensor' '[' shapeSeq ',' type_ ']' # tensorType
// currently unused
// | identType '[' (type_ (',' type_)*)? ']' # callType
| 'fn' '(' (type_ (',' type_)*)? ')' '->' type_ # funcType
// | typeIdent '[' (type_ (',' type_)*)? ']' # callType
| 'fn' typeParamSeq? '(' (type_ (',' type_)*)? ')' '->' type_ # funcType
| '_' # incompleteType
| NAT # intType
;
Expand All @@ -158,7 +163,7 @@ shape
| NAT # intShape
;

identType: CNAME ;
typeIdent : CNAME ;
// int8, int16, int32, int64
// uint8, uint16, uint32, uint64
// float16, float32, float64
Expand Down
3 changes: 3 additions & 0 deletions python/tvm/relay/grammar/py2/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Relay* binary
Relay* linguist-generated=true
Relay* linguist-detectable=false
1 change: 0 additions & 1 deletion python/tvm/relay/grammar/py2/.gitignore

This file was deleted.

109 changes: 109 additions & 0 deletions python/tvm/relay/grammar/py2/Relay.interp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions python/tvm/relay/grammar/py2/Relay.tokens

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading