Skip to content

Commit

Permalink
[Relay] Roundtrip part of pretty printer and parser (apache#3460)
Browse files Browse the repository at this point in the history
* init

fix rebase

lint

fix cmake

try again

fix ci

* add gitignore

* fix format

* do not include .interp and .tokens
  • Loading branch information
MarisaKirisame authored and Wei Chen committed Jul 11, 2019
1 parent 8649bdc commit a339417
Show file tree
Hide file tree
Showing 20 changed files with 333 additions and 581 deletions.
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,18 @@ patched.txt
.mypy_cache/
.pyre/

# pipenv file
# pipenv files
Pipfile
Pipfile.lock

# conda package artifacts
conda/Dockerfile.cuda*
conda/pkg

# nix files
.envrc
*.nix
*.nix

# antlr files
*.tokens
*.interp
54 changes: 41 additions & 13 deletions cmake/modules/ANTLR.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,49 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

if(USE_ANTLR)
set(RELAY_PARSER_DIR
${CMAKE_CURRENT_SOURCE_DIR}/python/tvm/relay/grammar)
find_program(ANTLR4 antlr4)

if (NOT ANTLR4)
file(GLOB_RECURSE ANTLR4JAR
/usr/local/lib/antlr-*-complete.jar
/usr/local/Cellar/*antlr-*-complete.jar)

# Get the first element of the list of antlr jars.
# Sort and reverse the list so the item selected is the highest
# version in lib or else in Cellar if no lib installation exists.
list(SORT ANTLR4JAR)
list(REVERSE ANTLR4JAR)
list(GET ANTLR4JAR 0 ANTLR4JAR)

set(JAVA_HOME $ENV{JAVA_HOME})
if (NOT DEFINED JAVA_HOME)
# Hack to get system to search for Java itself.
set(JAVA_HOME "/usr")
endif()

set(ANTLR4 ${JAVA_HOME}/bin/java -jar ${ANTLR4JAR})
endif()

if(ANTLR4)

set(RELAY_PARSER_DIR
${CMAKE_CURRENT_SOURCE_DIR}/python/tvm/relay/grammar)

set(RELAY_PARSER
${RELAY_PARSER_DIR}/py3/RelayVisitor.py
${RELAY_PARSER_DIR}/py3/RelayParser.py
${RELAY_PARSER_DIR}/py3/RelayLexer.py)

set(RELAY_PARSER
${RELAY_PARSER_DIR}/py3/RelayVisitor.py
${RELAY_PARSER_DIR}/py3/RelayParser.py
${RELAY_PARSER_DIR}/py3/RelayLexer.py)

# Generate ANTLR grammar for parsing.
add_custom_command(OUTPUT ${RELAY_PARSER}
COMMAND antlr4 -visitor -no-listener -Dlanguage=Python3 ${RELAY_PARSER_DIR}/Relay.g4 -o ${RELAY_PARSER_DIR}/py3
DEPENDS ${RELAY_PARSER_DIR}/Relay.g4
WORKING_DIRECTORY ${RELAY_PARSER_DIR})
# Generate ANTLR grammar for parsing.
add_custom_command(OUTPUT ${RELAY_PARSER}
COMMAND ${ANTLR4} -visitor -no-listener -Dlanguage=Python3 ${RELAY_PARSER_DIR}/Relay.g4 -o ${RELAY_PARSER_DIR}/py3
DEPENDS ${RELAY_PARSER_DIR}/Relay.g4
WORKING_DIRECTORY ${RELAY_PARSER_DIR})

add_custom_target(relay_parser ALL DEPENDS ${RELAY_PARSER})
add_custom_target(relay_parser ALL DEPENDS ${RELAY_PARSER})
else()
message(FATAL_ERROR "Can't find ANTLR4")
endif()
endif(USE_ANTLR)
5 changes: 3 additions & 2 deletions python/tvm/relay/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def visitTerminal(self, node):
if node_type == RelayLexer.NAT:
return int(node_text)
if node_type == RelayLexer.FLOAT:
return float(node_text)
return float(node_text[:-1])
if node_type == RelayLexer.BOOL_LIT:
if node_text == "True":
return True
Expand Down Expand Up @@ -375,6 +375,8 @@ def mk_func(self, ctx):
self.mk_typ(name, ty.Kind.Type)

var_list, attr_list = self.visit(ctx.argList())
if var_list is None:
var_list = []
ret_type = self.getType_(ctx.type_())

body = self.visit(ctx.body())
Expand All @@ -387,7 +389,6 @@ def mk_func(self, ctx):
self.exit_var_scope()

attrs = tvm.make.node("DictAttrs", **attr_list) if attr_list is not None else None

return expr.Function(var_list, body, ret_type, type_params, attrs)

@spanify
Expand Down
9 changes: 4 additions & 5 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.2' ;
SEMVER: 'v0.0.3' ;

// Lexing
// comments
Expand Down Expand Up @@ -52,10 +52,9 @@ BOOL_LIT
;

// non-negative floats
FLOAT
: NAT '.' NAT EXP? // 1.35, 1.35E-9, 0.3, 4.5
| NAT EXP // 1e10 3e4
;
fragment PREFLOAT : NAT ('.' NAT)? EXP?; // 1.35, 1.35E-9, 0.3, 4.5, 1, 1e10 3e4

FLOAT : PREFLOAT 'f';

// non-negative ints
NAT: DIGIT+ ;
Expand Down
3 changes: 3 additions & 0 deletions python/tvm/relay/grammar/py3/.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
109 changes: 0 additions & 109 deletions python/tvm/relay/grammar/py3/Relay.interp

This file was deleted.

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

This file was deleted.

Loading

0 comments on commit a339417

Please sign in to comment.