forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Hybrid script] Backend support (apache#2477)
* a preliminary version is done? * we no longer need the redundant hybrid/api.py * support assert stmt * cast supported * intrin -> runtime; util is mainly in charge of compilation time * assert statement * fix python lint * fix cpp lint * on the way to module * rollback .cc * fix typo, no direct expose then * @vinx13 ceil is added i guess? * wip... * temp commit * fix import * i preliminary version is done? * on the way to build hybrid module * nearly fixed... * dumped python are equiv as original python * on the way to bootstrap * cpu bootstrap done * bootstrap! * fix lint * fix doc * resolve some review concerns * support load/save * fix lint * thanks to xqdan fixed my typo * fix build, make dump non-optional * add vthread * jesus why i added this
- Loading branch information
Showing
17 changed files
with
1,091 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
message(STATUS "Build with contrib.hybriddump") | ||
file(GLOB HYBRID_CONTRIB_SRC src/contrib/hybrid/*.cc) | ||
list(APPEND COMPILER_SRCS ${HYBRID_CONTRIB_SRC}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
"""Methods and data structures to support dumping HalideIR to Hybrid Script. | ||
This allows users to do quick hack to generated HalideIR and cast it back to | ||
TVM modules. | ||
To enable this feature, you need to build with -DUSE_HYBRID_DUMP=ON. | ||
""" | ||
|
||
import ast | ||
import imp | ||
|
||
from ..contrib import util | ||
from .util import _internal_assert | ||
from .util import _is_tvm_arg_types | ||
from .parser import source_to_op | ||
|
||
|
||
class HybridModule(object): | ||
"""The usage of Hybrid Module is very similar to conventional TVM module, | ||
but conventional TVM module requires a function body which is already fully | ||
lowered. This contradicts to the fact that Hybrid Module is originally a text | ||
format for Phase 0 HalideIR. Thus, a totally separated module is defined.""" | ||
|
||
|
||
def __init__(self, src=None, name=None): | ||
"""The constructor of this a hybrid module | ||
Parameters | ||
---------- | ||
src : str | ||
The source code of this module | ||
name : str | ||
The name of this module | ||
""" | ||
self.src_ = self.name = self.func_ = self.root_ = None | ||
if src is not None: | ||
temp = util.tempdir() | ||
dst = temp.relpath("script.py") | ||
with open(dst, 'w') as f: | ||
f.write("import tvm\n@tvm.hybrid.script\n%s" % src) | ||
|
||
if name is not None: | ||
self.name = name | ||
self.load(dst) | ||
|
||
|
||
def __call__(self, *args): | ||
if _is_tvm_arg_types(args): | ||
return source_to_op(self.root_, globals(), args) | ||
return self.func_(*args) | ||
|
||
|
||
def get_source(self): | ||
return self.src_ | ||
|
||
|
||
def save(self, path): | ||
if not path.endswith('.py'): | ||
path = path + '.py' | ||
with open(path, 'w') as f: | ||
f.write(self.src_) | ||
|
||
|
||
def load(self, path): | ||
"""Load the module from a python file | ||
Parameters | ||
---------- | ||
path : str | ||
Path to the given python file | ||
""" | ||
with open(path, 'r') as f: | ||
self.src_ = f.read() | ||
|
||
src = self.src_ | ||
|
||
class FindFunc(ast.NodeVisitor): | ||
""" Find the function in module to be loaded module. """ | ||
#pylint: disable=invalid-name | ||
def __init__(self): | ||
self.name = None | ||
self.root = None | ||
|
||
|
||
def visit_FunctionDef(self, node): | ||
_internal_assert(self.name is None, "For now, only one function supported!") | ||
self.name = node.name | ||
_internal_assert(self.root is None, "For now, only one function supported!") | ||
self.root = node | ||
|
||
root = ast.parse(src) | ||
finder = FindFunc() | ||
finder.visit(root) | ||
_internal_assert(finder.name is not None and finder.root is not None, \ | ||
"No function found!") | ||
if self.name is None: | ||
self.name = finder.name | ||
self.root_ = finder.root | ||
py_module = imp.load_source(self.name, path) | ||
self.func_ = getattr(py_module, self.name) |
Oops, something went wrong.