From dd6cb13938ab968827497172e40ea905e02185ee Mon Sep 17 00:00:00 2001 From: EunTaik Lee Date: Tue, 3 Nov 2020 17:42:20 +0900 Subject: [PATCH 1/5] [TVMC] add cl support in tvmc runner --- python/tvm/driver/tvmc/runner.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/python/tvm/driver/tvmc/runner.py b/python/tvm/driver/tvmc/runner.py index a4abe8c31f56..75e20a5d64b7 100644 --- a/python/tvm/driver/tvmc/runner.py +++ b/python/tvm/driver/tvmc/runner.py @@ -50,7 +50,7 @@ def add_run_parser(subparsers): # like 'cl', 'webgpu', etc (@leandron) parser.add_argument( "--device", - choices=["cpu", "gpu"], + choices=["cpu", "gpu", "cl"], default="cpu", help="target device to run the compiled module. Defaults to 'cpu'", ) @@ -361,7 +361,12 @@ def run_module( # TODO expand to other supported devices, as listed in tvm.rpc.client (@leandron) logger.debug("device is %s", device) - ctx = session.cpu() if device == "cpu" else session.gpu() + if device == "gpu": + ctx = session.gpu() + elif device == "cl": + ctx = session.cl() + else: + ctx = session.cpu() if profile: logger.debug("creating runtime with profiling enabled") From b63afbbd8afbff32e50df82deff941990ab393ee Mon Sep 17 00:00:00 2001 From: EunTaik Lee Date: Thu, 5 Nov 2020 14:06:15 +0900 Subject: [PATCH 2/5] [TVMC] use target_host when it is set --- python/tvm/driver/tvmc/compiler.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/tvm/driver/tvmc/compiler.py b/python/tvm/driver/tvmc/compiler.py index e1a4a7481f6a..57071476b073 100644 --- a/python/tvm/driver/tvmc/compiler.py +++ b/python/tvm/driver/tvmc/compiler.py @@ -178,18 +178,18 @@ def compile_model( mod = common.convert_graph_layout(mod, alter_layout) tvm_target = common.target_from_cli(target) - target_host = target_host or "" + target_host = tvm_target if not target_host else target_host if tuning_records and os.path.exists(tuning_records): logger.debug("tuning records file provided: %s", tuning_records) with autotvm.apply_history_best(tuning_records): with tvm.transform.PassContext(opt_level=3): logger.debug("building relay graph with tuning records") - graph_module = relay.build(mod, tvm_target, params=params, target_host=tvm_target) + graph_module = relay.build(mod, tvm_target, params=params, target_host=target_host) else: with tvm.transform.PassContext(opt_level=3): logger.debug("building relay graph (no tuning records provided)") - graph_module = relay.build(mod, tvm_target, params=params, target_host=tvm_target) + graph_module = relay.build(mod, tvm_target, params=params, target_host=target_host) # Generate output dump files with sources dump_code = dump_code or [] From 12ea63ebe144b5ebbc5f0b53001f877827069520 Mon Sep 17 00:00:00 2001 From: EunTaik Lee Date: Thu, 5 Nov 2020 19:42:30 +0900 Subject: [PATCH 3/5] Cleanup comment and asssert device type in else case --- python/tvm/driver/tvmc/runner.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/tvm/driver/tvmc/runner.py b/python/tvm/driver/tvmc/runner.py index 75e20a5d64b7..dec0e9842a37 100644 --- a/python/tvm/driver/tvmc/runner.py +++ b/python/tvm/driver/tvmc/runner.py @@ -47,7 +47,7 @@ def add_run_parser(subparsers): parser.set_defaults(func=drive_run) # TODO --device needs to be extended and tested to support other targets, - # like 'cl', 'webgpu', etc (@leandron) + # like 'webgpu', etc (@leandron) parser.add_argument( "--device", choices=["cpu", "gpu", "cl"], @@ -366,6 +366,7 @@ def run_module( elif device == "cl": ctx = session.cl() else: + assert device == "cpu" ctx = session.cpu() if profile: From e95e12892a21879541df56f1284cc517b46eb20f Mon Sep 17 00:00:00 2001 From: EunTaik Lee Date: Fri, 20 Nov 2020 18:35:01 +0900 Subject: [PATCH 4/5] add a test for tvmc compiler --- tests/python/driver/tvmc/conftest.py | 32 +++++++++++++++++++++++ tests/python/driver/tvmc/test_compiler.py | 18 +++++++++++++ 2 files changed, 50 insertions(+) diff --git a/tests/python/driver/tvmc/conftest.py b/tests/python/driver/tvmc/conftest.py index 62af34ee7758..5b92994e2841 100644 --- a/tests/python/driver/tvmc/conftest.py +++ b/tests/python/driver/tvmc/conftest.py @@ -54,6 +54,25 @@ def get_sample_compiled_module(target_dir): return tvmc.compiler.compile_model(model_file, target="llvm") +def get_sample_opencl_compiled_module(target_dir): + """Support function that returns a TFLite compiled module""" + base_url = "https://storage.googleapis.com/download.tensorflow.org/models" + model_url = "mobilenet_v1_2018_02_22/mobilenet_v1_0.25_128.tgz" + model_file = download_and_untar( + "{}/{}".format(base_url, model_url), + "mobilenet_v1_0.25_128.tflite", + temp_dir=target_dir, + ) + return tvmc.compiler.compile_model( + model_file, + target="opencl", + target_host="llvm", + model_format="tflite", + dump_code="ll", + alter_layout="NCHW", + ) + + # PyTest fixtures @@ -148,3 +167,16 @@ def imagenet_cat(tmpdir_factory): np.savez(cat_file_full_path, input=image_data) return cat_file_full_path + + +@pytest.fixture(scope="session") +def tflite_mobilenet_v1_0_25_128(tmpdir_factory): + base_url = "https://storage.googleapis.com/download.tensorflow.org/models" + model_url = "mobilenet_v1_2018_02_22/mobilenet_v1_0.25_128.tgz" + model_file = download_and_untar( + "{}/{}".format(base_url, model_url), + "mobilenet_v1_0.25_128.tflite", + temp_dir=tmpdir_factory.mktemp("data"), + ) + + return model_file diff --git a/tests/python/driver/tvmc/test_compiler.py b/tests/python/driver/tvmc/test_compiler.py index 28a60b19b28e..4bbb6fbf2cf8 100644 --- a/tests/python/driver/tvmc/test_compiler.py +++ b/tests/python/driver/tvmc/test_compiler.py @@ -150,3 +150,21 @@ def test_cross_compile_aarch64_onnx_module(onnx_resnet50): assert type(params) is dict assert type(dumps) is dict assert "asm" in dumps.keys() + + +@tvm.testing.requires_opencl +def test_compile_opencl(tflite_mobilenet_v1_0_25_128): + pytest.importorskip("tflite") + + graph, lib, params, dumps = tvmc.compiler.compile_model( + tflite_mobilenet_v1_0_25_128, + target="opencl", + target_host="llvm", + alter_layout="NCHW", + ) + + # check for output types + assert type(graph) is str + assert type(lib) is tvm.runtime.module.Module + assert type(params) is dict + assert type(dumps) is dict From ccda7d73881c7d500327c07f81382cec25f2f5b9 Mon Sep 17 00:00:00 2001 From: EunTaik Lee Date: Fri, 20 Nov 2020 18:42:45 +0900 Subject: [PATCH 5/5] remove unused func --- tests/python/driver/tvmc/conftest.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/tests/python/driver/tvmc/conftest.py b/tests/python/driver/tvmc/conftest.py index 5b92994e2841..882d793ccebd 100644 --- a/tests/python/driver/tvmc/conftest.py +++ b/tests/python/driver/tvmc/conftest.py @@ -54,25 +54,6 @@ def get_sample_compiled_module(target_dir): return tvmc.compiler.compile_model(model_file, target="llvm") -def get_sample_opencl_compiled_module(target_dir): - """Support function that returns a TFLite compiled module""" - base_url = "https://storage.googleapis.com/download.tensorflow.org/models" - model_url = "mobilenet_v1_2018_02_22/mobilenet_v1_0.25_128.tgz" - model_file = download_and_untar( - "{}/{}".format(base_url, model_url), - "mobilenet_v1_0.25_128.tflite", - temp_dir=target_dir, - ) - return tvmc.compiler.compile_model( - model_file, - target="opencl", - target_host="llvm", - model_format="tflite", - dump_code="ll", - alter_layout="NCHW", - ) - - # PyTest fixtures