Skip to content

Commit 147f68b

Browse files
jroeschwweic
authored andcommitted
[Relay][Prelude] Use the Relay parser to define the Relay prelude (apache#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
1 parent 3b262a9 commit 147f68b

26 files changed

+6380
-148
lines changed

python/tvm/relay/_parser.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,12 @@ def visitProg(self, ctx):
242242
self.visit_list(ctx.defn())
243243
return self.module
244244

245-
return self.visit(ctx.expr())
245+
if ctx.expr():
246+
return self.visit(ctx.expr())
246247

247-
# Exprs
248+
return self.module
248249

250+
# Exprs
249251
def visitOpIdent(self, ctx):
250252
# type: (RelayParser.OpIdentContext) -> op.Op
251253
return op.get(ctx.CNAME().getText())
@@ -368,14 +370,25 @@ def mk_func(self, ctx):
368370
self.enter_var_scope()
369371
# Capture type params in params.
370372
self.enter_type_param_scope()
373+
type_params = ctx.typeParamSeq()
374+
375+
if type_params is not None:
376+
type_params = type_params.ident()
377+
assert type_params
378+
for ty_param in type_params:
379+
name = ty_param.getText()
380+
self.mk_typ(name, ty.Kind.Type)
381+
371382
var_list, attr_list = self.visit(ctx.argList())
372383
ret_type = self.getType_(ctx.type_())
373384

385+
body = self.visit(ctx.body())
386+
# NB(@jroesch): you must stay in the type parameter scope until
387+
# after you exit the body, you can reference the type parameters
388+
# of your parent scopes.
374389
type_params = list(self.exit_type_param_scope())
375390
if type_params:
376391
_, type_params = zip(*type_params)
377-
378-
body = self.visit(ctx.body())
379392
self.exit_var_scope()
380393

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

456-
def visitIdentType(self, ctx):
457-
# type: (RelayParser.IdentTypeContext) -> Union[ty.TensorType, str]
458-
ident_type = ctx.CNAME().getText()
469+
def visitTypeIdent(self, ctx):
470+
# type: (RelayParser.TypeIdentContext) -> Union[ty.TensorType, str]
471+
'''
472+
Handle type identifier.
473+
'''
474+
type_ident = ctx.CNAME().getText()
459475

460-
# look through all type prefixes for a match
476+
# Look through all type prefixes for a match
461477
for type_prefix in TYPE_PREFIXES:
462-
if ident_type.startswith(type_prefix):
463-
return ty.scalar_type(ident_type)
478+
if type_ident.startswith(type_prefix):
479+
return ty.scalar_type(type_ident)
480+
481+
type_param = lookup(self.type_param_scopes, type_ident)
482+
if type_param is not None:
483+
return type_param
464484

465-
raise ParseError("Unknown builtin type: {}".format(ident_type))
485+
raise ParseError("Unknown builtin type: {}".format(type_ident))
466486

467487
# def visitCallType(self, ctx):
468488
# # type: (RelayParser.CallTypeContext) -> Union[expr.Expr, ty.TensorType]

python/tvm/relay/grammar/Relay.g4

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
grammar Relay;
2121

22-
SEMVER: 'v0.0.1' ;
22+
SEMVER: 'v0.0.2' ;
2323

2424
// Lexing
2525
// comments
@@ -111,8 +111,8 @@ expr
111111
// | 'debug' # debug
112112
;
113113

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

117117
argList
118118
: varList
@@ -132,15 +132,20 @@ attr: CNAME '=' expr ;
132132
// relations: 'where' relation (',' relation)* ;
133133
// relation: ident '(' (type_ (',' type_)*)? ')' ;
134134

135+
typeParamSeq
136+
: '[' ']'
137+
| '[' ident (',' ident)* ']'
138+
;
139+
135140
type_
136141
: '(' ')' # tupleType
137142
| '(' type_ ',' ')' # tupleType
138143
| '(' type_ (',' type_)+ ')' # tupleType
139-
| identType # identTypeType
144+
| typeIdent # typeIdentType
140145
| 'Tensor' '[' shapeSeq ',' type_ ']' # tensorType
141146
// currently unused
142-
// | identType '[' (type_ (',' type_)*)? ']' # callType
143-
| 'fn' '(' (type_ (',' type_)*)? ')' '->' type_ # funcType
147+
// | typeIdent '[' (type_ (',' type_)*)? ']' # callType
148+
| 'fn' typeParamSeq? '(' (type_ (',' type_)*)? ')' '->' type_ # funcType
144149
| '_' # incompleteType
145150
| NAT # intType
146151
;
@@ -158,7 +163,7 @@ shape
158163
| NAT # intShape
159164
;
160165

161-
identType: CNAME ;
166+
typeIdent : CNAME ;
162167
// int8, int16, int32, int64
163168
// uint8, uint16, uint32, uint64
164169
// float16, float32, float64
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Relay* binary
2+
Relay* linguist-generated=true
3+
Relay* linguist-detectable=false

python/tvm/relay/grammar/py2/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

python/tvm/relay/grammar/py2/Relay.interp

Lines changed: 109 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/tvm/relay/grammar/py2/Relay.tokens

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)