Skip to content

Commit

Permalink
Merge branch 'relay-parser-pp' of https://github.com/jroesch/tvm into…
Browse files Browse the repository at this point in the history
… relay-parser-pp
  • Loading branch information
joshpoll committed Jan 15, 2019
2 parents 2603606 + fdb335e commit f0f25d6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
18 changes: 14 additions & 4 deletions python/tvm/relay/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from __future__ import absolute_import

import sys
import tvm

from collections import deque
from typing import TypeVar, Deque, Tuple, Optional, Union, NamedTuple, List, Callable, Any, Dict

import tvm

from . import module
from .base import Span, SourceName
from . import expr
Expand Down Expand Up @@ -80,7 +81,14 @@ def lookup(scopes, name):
return None

def spanify(f):
""" Adds span information to the output of f. """
"""A decorator which attaches span information
to the value returned by calling `f`.
Intended for use with the below AST visiting
methods. The idea is that after we do the work
of constructing the AST we attach Span information.
"""

def _wrapper(*args, **kwargs):
# 0th arg assumed to be self. Gets source name from object.
sn = args[0].source_name
Expand Down Expand Up @@ -299,6 +307,7 @@ def visitBinOp(self, ctx):
@spanify
def visitVar(self, ctx):
# type: (RelayParser.VarContext) -> expr.Var
"""Visit a single variable."""
ident = ctx.ident().LOCAL_VAR()

if ident is None:
Expand All @@ -316,13 +325,14 @@ def visitVarList(self, ctx):
def visitAttr(self, ctx):
# type: (RelayParser.AttrContext) -> Tuple[str, expr.Expr]
return (ctx.CNAME().getText(), self.visit(ctx.expr()))

def visitAttrList(self, ctx):
# type: (RelayParser.AttrListContext) -> Dict[str, expr.Expr]
return dict(self.visit_list(ctx.attr()))

def visitArgList(self, ctx):
# type: (RelayParser.ArgListContext) -> Tuple[Optional[List[expr.Var]], Optional[Dict[str, expr.Expr]]]
# type: (RelayParser.ArgListContext) ->
# Tuple[Optional[List[expr.Var]], Optional[Dict[str, expr.Expr]]]
var_list = self.visit(ctx.varList()) if ctx.varList() else None
attr_list = self.visit(ctx.attrList()) if ctx.attrList() else None

Expand Down
6 changes: 5 additions & 1 deletion python/tvm/relay/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from .. import register_func

def enabled():
"""Is the parser enabled/Can we import the parser?"""
"""Checks whether the parser is enabled, this allows users to
optionally support building the parser.
We use this check before importing the parser.
"""
try:
# pylint: disable=unused-variable
from tvm.relay import _parser
Expand Down

0 comments on commit f0f25d6

Please sign in to comment.