Created
March 21, 2019 01:08
-
-
Save wweic/6a76b0d7245dcc57b3b7707d99c7b57e to your computer and use it in GitHub Desktop.
Relay CLI and REPL
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
import argparse | |
import tvm | |
from tvm import relay | |
from tvm.relay import testing, create_executor | |
SEM_VER='v0.0.1' | |
def parse_args(): | |
parser = argparse.ArgumentParser(description='Run relay program') | |
parser.add_argument('prog_file', metavar='prog', type=str, default=None, nargs='?', | |
help='relay source code file name') | |
return parser.parse_args() | |
def relay_eval(source): | |
if SEM_VER not in source: | |
source = SEM_VER + source | |
prog = relay.fromtext(source) | |
target = 'llvm' | |
ctx = tvm.context(target, 0) | |
if not ctx.exist: | |
return | |
intrp = create_executor(ctx=ctx, target=target) | |
result = intrp.evaluate(prog) | |
return result | |
def prompt_continuation(width, line_number, is_soft_wrap): | |
return '.' * width | |
def repl(): | |
try: | |
from prompt_toolkit import prompt | |
i = 0 | |
while True: | |
try: | |
source = prompt('In [{}]: '.format(i), multiline=True, prompt_continuation=prompt_continuation) | |
val = relay_eval(source) | |
print('Val: {}'.format(val)) | |
except Exception as e: | |
print("Failed: {}".format(e)) | |
i += 1 | |
except KeyboardInterrupt: | |
pass | |
def main(): | |
args = parse_args() | |
if args.prog_file is None: | |
return repl() | |
with open(args.prog_file) as f: | |
source = f.read().strip() | |
print("Val: {}".format(relay_eval(source))) | |
if __name__ == '__main__': | |
main() |
Author
wweic
commented
Mar 21, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment