From b4dcf5ace6afa6bd1ba6a5eedd9b7d6e01afcb51 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Thu, 18 Mar 2021 16:21:06 -0400 Subject: [PATCH 01/14] add to init files for clean tvmc python --- python/tvm/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/tvm/__init__.py b/python/tvm/__init__.py index 7a5f553ccdd5..328878d84768 100644 --- a/python/tvm/__init__.py +++ b/python/tvm/__init__.py @@ -54,6 +54,7 @@ # tvm.driver from .driver import build, lower +from .driver import tvmc # tvm.parser from . import parser From a7693f64ec080531cb8ffa12c17cd84e780f907f Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Fri, 19 Mar 2021 19:27:52 -0400 Subject: [PATCH 02/14] adjust tests to new imports --- tests/python/driver/tvmc/test_frontends.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/driver/tvmc/test_frontends.py b/tests/python/driver/tvmc/test_frontends.py index 3da63d43ef29..8cf1d20d0661 100644 --- a/tests/python/driver/tvmc/test_frontends.py +++ b/tests/python/driver/tvmc/test_frontends.py @@ -21,7 +21,7 @@ from tvm.ir.module import IRModule -from tvm.driver import tvmc +from tvm import tvmc from tvm.driver.tvmc.common import TVMCException From 284c06440cef8e82929873a733a052475504a2e5 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Fri, 19 Mar 2021 15:10:32 -0400 Subject: [PATCH 03/14] add to compiler.py --- python/tvm/driver/tvmc/compiler.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/python/tvm/driver/tvmc/compiler.py b/python/tvm/driver/tvmc/compiler.py index 83791e50f6d5..390d44744317 100644 --- a/python/tvm/driver/tvmc/compiler.py +++ b/python/tvm/driver/tvmc/compiler.py @@ -111,6 +111,8 @@ def drive_compile(args): """ + mod, params = frontends.load_model(args.FILE, args.model_format, args.input_shapes) + graph, lib, params, dumps = compile_model( args.FILE, args.target, @@ -120,6 +122,8 @@ def drive_compile(args): args.tuning_records, args.desired_layout, args.input_shapes, + mod, + params ) if dumps: @@ -138,6 +142,8 @@ def compile_model( tuning_records=None, alter_layout=None, shape_dict=None, + mod, + params ): """Compile a model from a supported framework into a TVM module. @@ -184,7 +190,7 @@ def compile_model( """ dump_code = [x.strip() for x in dump_code.split(",")] if dump_code else None - mod, params = frontends.load_model(path, model_format, shape_dict) + config = {} if alter_layout: From 4739fc80b18601c86fa11137db277a244b35086d Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Mon, 22 Mar 2021 15:11:09 -0400 Subject: [PATCH 04/14] update so model loads in drive_compile --- python/tvm/driver/tvmc/compiler.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/python/tvm/driver/tvmc/compiler.py b/python/tvm/driver/tvmc/compiler.py index 390d44744317..340b62c6ada3 100644 --- a/python/tvm/driver/tvmc/compiler.py +++ b/python/tvm/driver/tvmc/compiler.py @@ -110,20 +110,16 @@ def drive_compile(args): Zero if successfully completed """ - mod, params = frontends.load_model(args.FILE, args.model_format, args.input_shapes) graph, lib, params, dumps = compile_model( - args.FILE, + mod, + params, args.target, args.dump_code, None, - args.model_format, args.tuning_records, args.desired_layout, - args.input_shapes, - mod, - params ) if dumps: @@ -134,16 +130,14 @@ def drive_compile(args): def compile_model( - path, + mod, + params, target, dump_code=None, target_host=None, model_format=None, tuning_records=None, alter_layout=None, - shape_dict=None, - mod, - params ): """Compile a model from a supported framework into a TVM module. @@ -153,8 +147,10 @@ def compile_model( Parameters ---------- - path: str - Path to a file + mod: IRModule + The relay module to be compiled. + params: dict + A dictionary containing the module's parameters. target : str The target for which to compile. Can be a plain string or a path. @@ -173,9 +169,6 @@ def compile_model( The layout to convert the graph to. Note, the convert layout pass doesn't currently guarantee the whole of the graph will be converted to the chosen layout. - shape_dict: dict, optional - A mapping from input names to their shape. When present, - the default shapes in the model will be overwritten. Returns ------- @@ -190,7 +183,6 @@ def compile_model( """ dump_code = [x.strip() for x in dump_code.split(",")] if dump_code else None - config = {} if alter_layout: From e9f007e0c0e95c5316ab9d5986327a45b63ab685 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Tue, 23 Mar 2021 14:54:06 -0400 Subject: [PATCH 05/14] update test_compiler.py to load outside of tvmc.compile, need to correct one error --- tests/python/driver/tvmc/test_compiler.py | 35 ++++++++++++++--------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/tests/python/driver/tvmc/test_compiler.py b/tests/python/driver/tvmc/test_compiler.py index 0180c35d7a26..e78958b59c0b 100644 --- a/tests/python/driver/tvmc/test_compiler.py +++ b/tests/python/driver/tvmc/test_compiler.py @@ -44,9 +44,9 @@ def test_save_dumps(tmpdir_factory): def verify_compile_tflite_module(model, shape_dict=None): pytest.importorskip("tflite") - + mod, params = tvmc.load(model, shape_dict = shape_dict) graph, lib, params, dumps = tvmc.compile( - model, target="llvm", dump_code="ll", alter_layout="NCHW", shape_dict=shape_dict + mod, params, target="llvm", dump_code="ll", alter_layout="NCHW" ) # check for output types @@ -74,10 +74,12 @@ def test_compile_tflite_module(tflite_mobilenet_v1_1_quant): def test_cross_compile_aarch64_tflite_module(tflite_mobilenet_v1_1_quant): pytest.importorskip("tflite") + mod, params = tvmc.load(flite_mobilenet_v1_1_quant) graph, lib, params, dumps = tvmc.compile( - tflite_mobilenet_v1_1_quant, + mod, + params, target="llvm -device=arm_cpu -mtriple=aarch64-linux-gnu -mattr='+neon'", - dump_code="asm", + dump_code="asm" ) # check for output types @@ -91,7 +93,8 @@ def test_compile_keras__save_module(keras_resnet50, tmpdir_factory): # some CI environments wont offer tensorflow/Keras, so skip in case it is not present pytest.importorskip("tensorflow") - graph, lib, params, dumps = tvmc.compile(keras_resnet50, target="llvm", dump_code="ll") + mod, params = tvmc.load(keras_resnet50) + graph, lib, params, dumps = tvmc.compile(mod, params, target="llvm", dump_code="ll") expected_temp_dir = tmpdir_factory.mktemp("saved_output") expected_file_name = "saved.tar" @@ -109,8 +112,10 @@ def test_cross_compile_aarch64_keras_module(keras_resnet50): # some CI environments wont offer tensorflow/Keras, so skip in case it is not present pytest.importorskip("tensorflow") + mod, params = tvmc.load(keras_resnet50) graph, lib, params, dumps = tvmc.compile( - keras_resnet50, + mods, + params, target="llvm -device=arm_cpu -mtriple=aarch64-linux-gnu -mattr='+neon'", dump_code="asm", ) @@ -126,9 +131,9 @@ def test_cross_compile_aarch64_keras_module(keras_resnet50): def verify_compile_onnx_module(model, shape_dict=None): # some CI environments wont offer onnx, so skip in case it is not present pytest.importorskip("onnx") - + mod, params = tvmc.load(model, shape_dict = shape_dict) graph, lib, params, dumps = tvmc.compile( - model, target="llvm", dump_code="ll", shape_dict=shape_dict + mod, params, target="llvm", dump_code="ll" ) # check for output types @@ -156,8 +161,10 @@ def test_cross_compile_aarch64_onnx_module(onnx_resnet50): # some CI environments wont offer onnx, so skip in case it is not present pytest.importorskip("onnx") + mod, params = tvmc.load(onnx_resnet50) graph, lib, params, dumps = tvmc.compile( - onnx_resnet50, + mod, + params, target="llvm -device=arm_cpu -mtriple=aarch64-linux-gnu -mattr=+neon", dump_code="asm", ) @@ -173,9 +180,10 @@ def test_cross_compile_aarch64_onnx_module(onnx_resnet50): @tvm.testing.requires_opencl def test_compile_opencl(tflite_mobilenet_v1_0_25_128): pytest.importorskip("tflite") - + mod, params = tvmc.load(tflite_mobilenet_v1_0_25_128) graph, lib, params, dumps = tvmc.compile( - tflite_mobilenet_v1_0_25_128, + mod, + params, target="opencl", target_host="llvm", alter_layout="NCHW", @@ -194,9 +202,9 @@ def test_compile_opencl(tflite_mobilenet_v1_0_25_128): ) def test_compile_tflite_module_with_external_codegen(tflite_mobilenet_v1_1_quant): pytest.importorskip("tflite") - + mod, params = tvmc.load(tflite_mobilenet_v1_1_quant) graph, lib, params, dumps = tvmc.compile( - tflite_mobilenet_v1_1_quant, target="ethos-n77, llvm", dump_code="relay" + mod, params, target="ethos-n77, llvm", dump_code="relay" ) # check for output types @@ -219,6 +227,7 @@ def test_compile_check_configs_composite_target(mock_pc, mock_fe, mock_ct, mock_ mock_ct.return_value = mock_codegen mock_relay.return_value = mock.MagicMock() + #mod, params = tvmc.load() #Wait, no file needed???? graph, lib, params, dumps = tvmc.compile( "no_file_needed", target="mockcodegen -testopt=value, llvm" ) From a7d49d5e34eb1b6267606b5115abeb01104762e3 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Tue, 23 Mar 2021 15:09:18 -0400 Subject: [PATCH 06/14] fix mock.patch test --- tests/python/driver/tvmc/test_compiler.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/python/driver/tvmc/test_compiler.py b/tests/python/driver/tvmc/test_compiler.py index e78958b59c0b..6b09ad0fd743 100644 --- a/tests/python/driver/tvmc/test_compiler.py +++ b/tests/python/driver/tvmc/test_compiler.py @@ -216,7 +216,7 @@ def test_compile_tflite_module_with_external_codegen(tflite_mobilenet_v1_1_quant @mock.patch("tvm.relay.build") @mock.patch("tvm.driver.tvmc.composite_target.get_codegen_by_target") -@mock.patch("tvm.driver.tvmc.frontends.load_model") +@mock.patch("tvm.tvmc.load") @mock.patch("tvm.transform.PassContext") def test_compile_check_configs_composite_target(mock_pc, mock_fe, mock_ct, mock_relay): mock_codegen = {} @@ -227,9 +227,9 @@ def test_compile_check_configs_composite_target(mock_pc, mock_fe, mock_ct, mock_ mock_ct.return_value = mock_codegen mock_relay.return_value = mock.MagicMock() - #mod, params = tvmc.load() #Wait, no file needed???? + mod, params = tvmc.load("no_file_needed") graph, lib, params, dumps = tvmc.compile( - "no_file_needed", target="mockcodegen -testopt=value, llvm" + mod, params, target="mockcodegen -testopt=value, llvm" ) mock_pc.assert_called_once_with( From d96a5a878481e0b2842471324ae08cfd465b08e5 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Wed, 24 Mar 2021 15:14:07 -0400 Subject: [PATCH 07/14] remove merge artifact (circular import issue) --- python/tvm/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/tvm/__init__.py b/python/tvm/__init__.py index 328878d84768..7a5f553ccdd5 100644 --- a/python/tvm/__init__.py +++ b/python/tvm/__init__.py @@ -54,7 +54,6 @@ # tvm.driver from .driver import build, lower -from .driver import tvmc # tvm.parser from . import parser From d033ee8a2f9d094c11c1c48deef1957efcebe289 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Wed, 24 Mar 2021 15:16:40 -0400 Subject: [PATCH 08/14] change typo and merge artifact --- tests/python/driver/tvmc/test_compiler.py | 2 +- tests/python/driver/tvmc/test_frontends.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/python/driver/tvmc/test_compiler.py b/tests/python/driver/tvmc/test_compiler.py index 6b09ad0fd743..93f938a15119 100644 --- a/tests/python/driver/tvmc/test_compiler.py +++ b/tests/python/driver/tvmc/test_compiler.py @@ -114,7 +114,7 @@ def test_cross_compile_aarch64_keras_module(keras_resnet50): mod, params = tvmc.load(keras_resnet50) graph, lib, params, dumps = tvmc.compile( - mods, + mod, params, target="llvm -device=arm_cpu -mtriple=aarch64-linux-gnu -mattr='+neon'", dump_code="asm", diff --git a/tests/python/driver/tvmc/test_frontends.py b/tests/python/driver/tvmc/test_frontends.py index 8cf1d20d0661..3da63d43ef29 100644 --- a/tests/python/driver/tvmc/test_frontends.py +++ b/tests/python/driver/tvmc/test_frontends.py @@ -21,7 +21,7 @@ from tvm.ir.module import IRModule -from tvm import tvmc +from tvm.driver import tvmc from tvm.driver.tvmc.common import TVMCException From 6f774acb960473b0d528b1cdb91855e772ce7d74 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Wed, 24 Mar 2021 15:23:39 -0400 Subject: [PATCH 09/14] fix import in test_compiler.py --- tests/python/driver/tvmc/test_compiler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/driver/tvmc/test_compiler.py b/tests/python/driver/tvmc/test_compiler.py index 93f938a15119..512592eaf7c9 100644 --- a/tests/python/driver/tvmc/test_compiler.py +++ b/tests/python/driver/tvmc/test_compiler.py @@ -216,7 +216,7 @@ def test_compile_tflite_module_with_external_codegen(tflite_mobilenet_v1_1_quant @mock.patch("tvm.relay.build") @mock.patch("tvm.driver.tvmc.composite_target.get_codegen_by_target") -@mock.patch("tvm.tvmc.load") +@mock.patch("tvm.driver.tvmc.load") @mock.patch("tvm.transform.PassContext") def test_compile_check_configs_composite_target(mock_pc, mock_fe, mock_ct, mock_relay): mock_codegen = {} From 4a2d468505ec655f4bebc3ddbe3890b7d33fa0e9 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Wed, 24 Mar 2021 15:38:17 -0400 Subject: [PATCH 10/14] black needed files --- python/tvm/driver/tvmc/compiler.py | 2 +- tests/python/driver/tvmc/test_compiler.py | 16 ++++++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/python/tvm/driver/tvmc/compiler.py b/python/tvm/driver/tvmc/compiler.py index 340b62c6ada3..e2e6e622d76d 100644 --- a/python/tvm/driver/tvmc/compiler.py +++ b/python/tvm/driver/tvmc/compiler.py @@ -150,7 +150,7 @@ def compile_model( mod: IRModule The relay module to be compiled. params: dict - A dictionary containing the module's parameters. + A dictionary containing the module's parameters. target : str The target for which to compile. Can be a plain string or a path. diff --git a/tests/python/driver/tvmc/test_compiler.py b/tests/python/driver/tvmc/test_compiler.py index 512592eaf7c9..cb903263de60 100644 --- a/tests/python/driver/tvmc/test_compiler.py +++ b/tests/python/driver/tvmc/test_compiler.py @@ -44,7 +44,7 @@ def test_save_dumps(tmpdir_factory): def verify_compile_tflite_module(model, shape_dict=None): pytest.importorskip("tflite") - mod, params = tvmc.load(model, shape_dict = shape_dict) + mod, params = tvmc.load(model, shape_dict=shape_dict) graph, lib, params, dumps = tvmc.compile( mod, params, target="llvm", dump_code="ll", alter_layout="NCHW" ) @@ -76,10 +76,10 @@ def test_cross_compile_aarch64_tflite_module(tflite_mobilenet_v1_1_quant): mod, params = tvmc.load(flite_mobilenet_v1_1_quant) graph, lib, params, dumps = tvmc.compile( - mod, + mod, params, target="llvm -device=arm_cpu -mtriple=aarch64-linux-gnu -mattr='+neon'", - dump_code="asm" + dump_code="asm", ) # check for output types @@ -131,10 +131,8 @@ def test_cross_compile_aarch64_keras_module(keras_resnet50): def verify_compile_onnx_module(model, shape_dict=None): # some CI environments wont offer onnx, so skip in case it is not present pytest.importorskip("onnx") - mod, params = tvmc.load(model, shape_dict = shape_dict) - graph, lib, params, dumps = tvmc.compile( - mod, params, target="llvm", dump_code="ll" - ) + mod, params = tvmc.load(model, shape_dict=shape_dict) + graph, lib, params, dumps = tvmc.compile(mod, params, target="llvm", dump_code="ll") # check for output types assert type(graph) is str @@ -228,9 +226,7 @@ def test_compile_check_configs_composite_target(mock_pc, mock_fe, mock_ct, mock_ mock_relay.return_value = mock.MagicMock() mod, params = tvmc.load("no_file_needed") - graph, lib, params, dumps = tvmc.compile( - mod, params, target="mockcodegen -testopt=value, llvm" - ) + graph, lib, params, dumps = tvmc.compile(mod, params, target="mockcodegen -testopt=value, llvm") mock_pc.assert_called_once_with( opt_level=3, config={"relay.ext.mock.options": {"testopt": "value"}} From e382307de437b6349a4c97c984ddca466ed7d8dc Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Thu, 25 Mar 2021 15:15:07 -0400 Subject: [PATCH 11/14] remove unnecessary argument model_format from compile_module --- python/tvm/driver/tvmc/compiler.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/python/tvm/driver/tvmc/compiler.py b/python/tvm/driver/tvmc/compiler.py index e2e6e622d76d..8b664b7cba9e 100644 --- a/python/tvm/driver/tvmc/compiler.py +++ b/python/tvm/driver/tvmc/compiler.py @@ -135,7 +135,6 @@ def compile_model( target, dump_code=None, target_host=None, - model_format=None, tuning_records=None, alter_layout=None, ): @@ -160,8 +159,6 @@ def compile_model( target_host : str, optional The target of the host machine if host-side code needs to be generated. - model_format: str, optional - A string representing a name of a frontend to be used tuning_records: str, optional Path to the file produced by the tuning to be used during compilation. From d8e7ea05a7e8879ae8d469db6f36097faa898d42 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Thu, 25 Mar 2021 19:55:49 -0400 Subject: [PATCH 12/14] load before compile in conftest.py --- tests/python/driver/tvmc/conftest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/driver/tvmc/conftest.py b/tests/python/driver/tvmc/conftest.py index 534953deecbc..28fa2ac806ad 100644 --- a/tests/python/driver/tvmc/conftest.py +++ b/tests/python/driver/tvmc/conftest.py @@ -51,6 +51,7 @@ def get_sample_compiled_module(target_dir): temp_dir=target_dir, ) + mod, params = tvmc.frontends.load_model(model_file) return tvmc.compiler.compile_model(model_file, target="llvm") From ff1adce95e393977f7aa8f8c5c54c8af1218cace Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Thu, 25 Mar 2021 20:02:53 -0400 Subject: [PATCH 13/14] fix conftest.py issue --- tests/python/driver/tvmc/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/driver/tvmc/conftest.py b/tests/python/driver/tvmc/conftest.py index 28fa2ac806ad..3345b4f07585 100644 --- a/tests/python/driver/tvmc/conftest.py +++ b/tests/python/driver/tvmc/conftest.py @@ -52,7 +52,7 @@ def get_sample_compiled_module(target_dir): ) mod, params = tvmc.frontends.load_model(model_file) - return tvmc.compiler.compile_model(model_file, target="llvm") + return tvmc.compiler.compile_model(mod, params, target="llvm") # PyTest fixtures From 846400f959c4d94af807fed02dfaf1f6e978fd28 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Fri, 26 Mar 2021 15:09:55 -0400 Subject: [PATCH 14/14] fix typo in test_compiler.py --- tests/python/driver/tvmc/test_compiler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/driver/tvmc/test_compiler.py b/tests/python/driver/tvmc/test_compiler.py index cb903263de60..450c54a578f2 100644 --- a/tests/python/driver/tvmc/test_compiler.py +++ b/tests/python/driver/tvmc/test_compiler.py @@ -74,7 +74,7 @@ def test_compile_tflite_module(tflite_mobilenet_v1_1_quant): def test_cross_compile_aarch64_tflite_module(tflite_mobilenet_v1_1_quant): pytest.importorskip("tflite") - mod, params = tvmc.load(flite_mobilenet_v1_1_quant) + mod, params = tvmc.load(tflite_mobilenet_v1_1_quant) graph, lib, params, dumps = tvmc.compile( mod, params,