Skip to content

Commit

Permalink
[PYTHON] Make decorator optional for runtime (#1350)
Browse files Browse the repository at this point in the history
  • Loading branch information
tqchen committed Jun 28, 2018
1 parent 6c97ef2 commit 206e4ae
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
15 changes: 15 additions & 0 deletions python/tvm/_ffi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,18 @@ def c_array(ctype, values):
Created ctypes array
"""
return (ctype * len(values))(*values)


def decorate(func, fwrapped):
"""A wrapper call of decorator package, differs to call time
Parameters
----------
func : function
The original function
fwrapped : function
The wrapped function
"""
import decorator
return decorator.decorate(func, fwrapped)
32 changes: 21 additions & 11 deletions python/tvm/hybrid/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@
from __future__ import absolute_import as _abs

import types
import decorator
from .._ffi.base import decorate
from .parser import parse_python

@decorator.decorator
def script(func, *args):
"""If the arguments are tvm types, compile it to HalideIR.
O.W. return the python emulated result"""
from .util import _enter_hybrid_runtime, _restore_runtime, _is_tvm_arg_types
if _is_tvm_arg_types(args):
return parse(func, args)
else:

def script(pyfunc):
"""Decorate a python function function as hybrid script.
The hybrid function support emulation mode and parsing to
the internal language IR.
Returns
-------
hybrid_func : function
A decorated hybrid script function.
"""
def wrapped_func(func, *args, **kwargs):
from .util import _enter_hybrid_runtime, _restore_runtime, _is_tvm_arg_types
if _is_tvm_arg_types(args):
return parse(func, args)

intersect = _enter_hybrid_runtime(func)
func(*args)
value = func(*args, **kwargs)
_restore_runtime(func, intersect)
return func
return value
return decorate(pyfunc, wrapped_func)


def parse(func, args):
Expand Down
8 changes: 1 addition & 7 deletions python/tvm/tag.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
"""Tag class for TVM operators."""
from ._ffi.base import _LIB_NAME
try:
from decorator import decorate
except ImportError as err_msg:
# Allow decorator to be missing in runtime
if _LIB_NAME != "libtvm_runtime.so":
raise err_msg
from ._ffi.base import decorate

class TagScope(object):
"""Tag scope object to set tag for operators, working as context
Expand Down

1 comment on commit 206e4ae

@were
Copy link
Contributor

@were were commented on 206e4ae Jul 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

Please sign in to comment.