Repository navigation
[Hybrid script] Backend support #2477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
4b81b22
a preliminary version is done?
b285068
we no longer need the redundant hybrid/api.py
de3ad83
support assert stmt
790396c
cast supported
4be2d5f
intrin -> runtime; util is mainly in charge of compilation time
b0981b0
assert statement
6c0d984
fix python lint
557472b
fix cpp lint
c1f4464
on the way to module
f1d636a
rollback .cc
942d4b6
fix typo, no direct expose then
4f2e842
@vinx13 ceil is added i guess?
151f0fe
wip...
baaaa6a
temp commit
b64cd4e
fix import
9847ef9
i preliminary version is done?
6debdf1
on the way to build hybrid module
5d614d4
nearly fixed...
39048da
dumped python are equiv as original python
11d2077
on the way to bootstrap
3866e1c
cpu bootstrap done
5375aba
bootstrap!
363c4a9
fix lint
d129948
fix doc
0063637
resolve some review concerns
46df563
support load/save
3d495f3
fix lint
3d086cb
thanks to xqdan fixed my typo
fcc761d
fix build, make dump non-optional
17c4a85
add vthread
f2b00f1
jesus why i added this
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.