Skip to content

Commit

Permalink
add docstring skip in hybrid script (apache#1668)
Browse files Browse the repository at this point in the history
* add docstring skip in hybrid script

* fix lint
  • Loading branch information
were authored and yzhliu committed Aug 27, 2018
1 parent 857c0bd commit 100cf48
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions python/tvm/hybrid/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ast
import operator
import sys
from .util import make_nop, halide_imm_types
from .util import make_nop, halide_imm_types, is_docstring
from .intrin import LOOP_INTRIN, MATH_INTRIN
from .var_decl import determine_variable_usage
from ..api import thread_axis
Expand All @@ -15,7 +15,7 @@

def list_to_block(visit, lst):
"""Convert a list of Python IR nodes to HalideIR Block"""
lst = list(map(visit, lst))
lst = [visit(stmt) for stmt in lst if not is_docstring(stmt)]
lst = [stmt for stmt in lst if not _ir_pass.Equal(stmt, make_nop())]
if not lst:
return make_nop()
Expand Down
6 changes: 6 additions & 0 deletions python/tvm/hybrid/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Internal utilities for parsing Python subset to HalideIR"""

import ast
import inspect
import numpy
from .intrin import HYBRID_GLOBALS
Expand All @@ -22,6 +23,11 @@ def make_nop():
return _make.Evaluate(_api.const(0, dtype='int32'))


def is_docstring(node):
"""Checks if a Python AST node is a docstring"""
return isinstance(node, ast.Expr) and isinstance(node.value, ast.Str)


def _pruned_source(func):
"""Prune source code's extra leading spaces"""
lines = inspect.getsource(func).split('\n')
Expand Down
1 change: 1 addition & 0 deletions tests/python/unittest/test_hybrid_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def tvm_val_2_py_val(val):

@script
def outer_product(n, m, a, b, c):
"""This is a simple outer product"""
for i in range(n):
for j in range(m):
c[i, j] = a[i] * b[j]
Expand Down

0 comments on commit 100cf48

Please sign in to comment.