Skip to content

Commit

Permalink
[Relay][Prelude] Use the Relay parser to define the Relay prelude (ap…
Browse files Browse the repository at this point in the history
…ache#3043)

* Add ability to load Prelude from disk

* Port over id

* Define compose

* Linting errors and style changes

* Eliminate unnecessary parens

* Rename identType to typeIdent (makes more sense)

* Another unnecessary paren

* Bump the version number for the text format

* Ensure .rly (Relay text files) are permitted

* Correct release number and simplify grammar rule

* Correct load_prelude docstring

* Corrections to _parser

* Add Apache headers to prelude source file

* Remove test_prelude (redundant)

* Correct misleading error message

* Add check that parser is enabled in Prelude

* Commit pre-generated parser, ensure generated files are treated as binaries, and have parser tests always fire

* Permit parser files and git attributes files

* Exclude gitattributes and parser files from apache check

* Another attempt at appeasing Apache audit checker

* Corrections to rat-excludes

* Apache should be truly appeased now

* Ignore Relay parser files by name

* Mark parser files as generated so they don't show up on Github

* Add parsing helper function for tests

* Mark parser files as not detectable
  • Loading branch information
jroesch authored and Wei Chen committed Jun 26, 2019
1 parent ac20b98 commit 92dfe37
Show file tree
Hide file tree
Showing 26 changed files with 6,380 additions and 148 deletions.
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
# 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

0 comments on commit 92dfe37

Please sign in to comment.