From b0baa78c84b55fdba2a3f4a89a53d23ec4777f96 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Thu, 18 Mar 2021 16:21:06 -0400 Subject: [PATCH 1/7] add to init files for clean tvmc python --- python/tvm/__init__.py | 1 + python/tvm/driver/tvmc/__init__.py | 3 +++ 2 files changed, 4 insertions(+) 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 diff --git a/python/tvm/driver/tvmc/__init__.py b/python/tvm/driver/tvmc/__init__.py index d96a725877eb..919e650fee4a 100644 --- a/python/tvm/driver/tvmc/__init__.py +++ b/python/tvm/driver/tvmc/__init__.py @@ -21,3 +21,6 @@ from . import autotuner from . import compiler from . import runner +from .frontends import load_model as load +from .compiler import compile_model as compile +from .runner import run_module as run \ No newline at end of file From 483a31c595fe36ed4553b78015f606fd16611f56 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Fri, 19 Mar 2021 15:17:06 -0400 Subject: [PATCH 2/7] black reformat init.py --- python/tvm/driver/tvmc/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/driver/tvmc/__init__.py b/python/tvm/driver/tvmc/__init__.py index 919e650fee4a..aa8d29fbb8dc 100644 --- a/python/tvm/driver/tvmc/__init__.py +++ b/python/tvm/driver/tvmc/__init__.py @@ -23,4 +23,4 @@ from . import runner from .frontends import load_model as load from .compiler import compile_model as compile -from .runner import run_module as run \ No newline at end of file +from .runner import run_module as run From 4f98767c83b4d9aff22cb765e3cbd7424def761c Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Fri, 19 Mar 2021 19:27:52 -0400 Subject: [PATCH 3/7] adjust tests to new imports --- tests/python/driver/tvmc/test_compiler.py | 18 +++++++-------- tests/python/driver/tvmc/test_frontends.py | 26 ++++++++++++++-------- tests/python/driver/tvmc/test_runner.py | 2 +- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/tests/python/driver/tvmc/test_compiler.py b/tests/python/driver/tvmc/test_compiler.py index ae859298facd..b420ca4ec821 100644 --- a/tests/python/driver/tvmc/test_compiler.py +++ b/tests/python/driver/tvmc/test_compiler.py @@ -45,7 +45,7 @@ def test_save_dumps(tmpdir_factory): def verify_compile_tflite_module(model, shape_dict=None): pytest.importorskip("tflite") - graph, lib, params, dumps = tvmc.compiler.compile_model( + graph, lib, params, dumps = tvmc.compile( model, target="llvm", dump_code="ll", alter_layout="NCHW", shape_dict=shape_dict ) @@ -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") - graph, lib, params, dumps = tvmc.compiler.compile_model( + graph, lib, params, dumps = tvmc.compile( tflite_mobilenet_v1_1_quant, target="llvm -device=arm_cpu -mtriple=aarch64-linux-gnu -mattr='+neon'", dump_code="asm", @@ -91,7 +91,7 @@ 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.compiler.compile_model( + graph, lib, params, dumps = tvmc.compile( keras_resnet50, target="llvm", dump_code="ll" ) @@ -111,7 +111,7 @@ 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") - graph, lib, params, dumps = tvmc.compiler.compile_model( + graph, lib, params, dumps = tvmc.compile( keras_resnet50, target="llvm -device=arm_cpu -mtriple=aarch64-linux-gnu -mattr='+neon'", dump_code="asm", @@ -129,7 +129,7 @@ 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") - graph, lib, params, dumps = tvmc.compiler.compile_model( + graph, lib, params, dumps = tvmc.compile( model, target="llvm", dump_code="ll", shape_dict=shape_dict ) @@ -158,7 +158,7 @@ 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") - graph, lib, params, dumps = tvmc.compiler.compile_model( + graph, lib, params, dumps = tvmc.compile( onnx_resnet50, target="llvm -device=arm_cpu -mtriple=aarch64-linux-gnu -mattr=+neon", dump_code="asm", @@ -176,7 +176,7 @@ def test_cross_compile_aarch64_onnx_module(onnx_resnet50): def test_compile_opencl(tflite_mobilenet_v1_0_25_128): pytest.importorskip("tflite") - graph, lib, params, dumps = tvmc.compiler.compile_model( + graph, lib, params, dumps = tvmc.compile( tflite_mobilenet_v1_0_25_128, target="opencl", target_host="llvm", @@ -197,7 +197,7 @@ 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") - graph, lib, params, dumps = tvmc.compiler.compile_model( + graph, lib, params, dumps = tvmc.compile( tflite_mobilenet_v1_1_quant, target="ethos-n77, llvm", dump_code="relay" ) @@ -221,7 +221,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() - graph, lib, params, dumps = tvmc.compiler.compile_model( + graph, lib, params, dumps = tvmc.compile( "no_file_needed", target="mockcodegen -testopt=value, llvm" ) diff --git a/tests/python/driver/tvmc/test_frontends.py b/tests/python/driver/tvmc/test_frontends.py index 5a63c5c47933..16076f329aca 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 @@ -93,7 +93,7 @@ def test_load_model__invalid_path__no_language(): pytest.importorskip("tflite") with pytest.raises(FileNotFoundError): - tvmc.frontends.load_model("not/a/file.tflite") + tvmc.load("not/a/file.tflite") def test_load_model__invalid_path__with_language(): @@ -101,14 +101,14 @@ def test_load_model__invalid_path__with_language(): pytest.importorskip("onnx") with pytest.raises(FileNotFoundError): - tvmc.frontends.load_model("not/a/file.txt", model_format="onnx") + tvmc.load("not/a/file.txt", model_format="onnx") def test_load_model__tflite(tflite_mobilenet_v1_1_quant): # some CI environments wont offer TFLite, so skip in case it is not present pytest.importorskip("tflite") - mod, params = tvmc.frontends.load_model(tflite_mobilenet_v1_1_quant) + mod, params = tvmc.load(tflite_mobilenet_v1_1_quant) assert type(mod) is IRModule assert type(params) is dict # check whether one known value is part of the params dict @@ -137,8 +137,16 @@ def verify_load_model__onnx(model, **kwargs): def test_load_model__onnx(onnx_resnet50): # some CI environments wont offer onnx, so skip in case it is not present pytest.importorskip("onnx") +<<<<<<< HEAD mod, params = verify_load_model__onnx(onnx_resnet50) # check whether one known value is part of the params dict +======= + + mod, params = tvmc.load(onnx_resnet50) + assert type(mod) is IRModule + assert type(params) is dict + ## check whether one known value is part of the params dict +>>>>>>> adjust tests to new imports assert "resnetv24_batchnorm0_gamma" in params.keys() mod, params = verify_load_model__onnx(onnx_resnet50, freeze_params=True) # check that the parameter dict is empty, implying that they have been folded into constants @@ -149,7 +157,7 @@ def test_load_model__pb(pb_mobilenet_v1_1_quant): # some CI environments wont offer TensorFlow, so skip in case it is not present pytest.importorskip("tensorflow") - mod, params = tvmc.frontends.load_model(pb_mobilenet_v1_1_quant) + mod, params = tvmc.load(pb_mobilenet_v1_1_quant) assert type(mod) is IRModule assert type(params) is dict # check whether one known value is part of the params dict @@ -161,7 +169,7 @@ def test_load_model___wrong_language__to_keras(tflite_mobilenet_v1_1_quant): pytest.importorskip("tensorflow") with pytest.raises(OSError): - tvmc.frontends.load_model(tflite_mobilenet_v1_1_quant, model_format="keras") + tvmc.load(tflite_mobilenet_v1_1_quant, model_format="keras") def test_load_model___wrong_language__to_tflite(keras_resnet50): @@ -179,7 +187,7 @@ def test_load_model___wrong_language__to_onnx(tflite_mobilenet_v1_1_quant): from google.protobuf.message import DecodeError with pytest.raises(DecodeError): - tvmc.frontends.load_model(tflite_mobilenet_v1_1_quant, model_format="onnx") + tvmc.load(tflite_mobilenet_v1_1_quant, model_format="onnx") @pytest.mark.skip(reason="https://github.com/apache/tvm/issues/7455") @@ -188,7 +196,7 @@ def test_load_model__pth(pytorch_resnet18): pytest.importorskip("torch") pytest.importorskip("torchvision") - mod, params = tvmc.frontends.load_model( + mod, params = tvmc.load( pytorch_resnet18, shape_dict={"input": [1, 3, 224, 224]} ) assert type(mod) is IRModule @@ -202,7 +210,7 @@ def test_load_model___wrong_language__to_pytorch(tflite_mobilenet_v1_1_quant): pytest.importorskip("torch") with pytest.raises(RuntimeError) as e: - tvmc.frontends.load_model( + tvmc.load( tflite_mobilenet_v1_1_quant, model_format="pytorch", shape_dict={"input": [1, 3, 224, 224]}, diff --git a/tests/python/driver/tvmc/test_runner.py b/tests/python/driver/tvmc/test_runner.py index 544ed9f7e9df..5fdf58fa8d64 100644 --- a/tests/python/driver/tvmc/test_runner.py +++ b/tests/python/driver/tvmc/test_runner.py @@ -73,7 +73,7 @@ def test_run_tflite_module__with_profile__valid_input( # some CI environments wont offer TFLite, so skip in case it is not present pytest.importorskip("tflite") - outputs, times = tvmc.runner.run_module( + outputs, times = tvmc.run( tflite_compiled_module_as_tarfile, inputs_file=imagenet_cat, hostname=None, From bc4a090b1308543b73a631e375736c2ffa1eb757 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Fri, 19 Mar 2021 19:29:25 -0400 Subject: [PATCH 4/7] black test files --- tests/python/driver/tvmc/test_compiler.py | 4 +--- tests/python/driver/tvmc/test_frontends.py | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/python/driver/tvmc/test_compiler.py b/tests/python/driver/tvmc/test_compiler.py index b420ca4ec821..0180c35d7a26 100644 --- a/tests/python/driver/tvmc/test_compiler.py +++ b/tests/python/driver/tvmc/test_compiler.py @@ -91,9 +91,7 @@ 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" - ) + graph, lib, params, dumps = tvmc.compile(keras_resnet50, target="llvm", dump_code="ll") expected_temp_dir = tmpdir_factory.mktemp("saved_output") expected_file_name = "saved.tar" diff --git a/tests/python/driver/tvmc/test_frontends.py b/tests/python/driver/tvmc/test_frontends.py index 16076f329aca..0c0bc144972f 100644 --- a/tests/python/driver/tvmc/test_frontends.py +++ b/tests/python/driver/tvmc/test_frontends.py @@ -196,9 +196,7 @@ def test_load_model__pth(pytorch_resnet18): pytest.importorskip("torch") pytest.importorskip("torchvision") - mod, params = tvmc.load( - pytorch_resnet18, shape_dict={"input": [1, 3, 224, 224]} - ) + mod, params = tvmc.load(pytorch_resnet18, shape_dict={"input": [1, 3, 224, 224]}) assert type(mod) is IRModule assert type(params) is dict # check whether one known value is part of the params dict From 41a0a887b7cc63504c26adce162c5a315a2e9f59 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Sun, 21 Mar 2021 16:34:00 -0400 Subject: [PATCH 5/7] tell lint ignore defined-builtin error for tvmc compile --- python/tvm/driver/tvmc/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/tvm/driver/tvmc/__init__.py b/python/tvm/driver/tvmc/__init__.py index aa8d29fbb8dc..7c6d06d02191 100644 --- a/python/tvm/driver/tvmc/__init__.py +++ b/python/tvm/driver/tvmc/__init__.py @@ -14,6 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +# pylint disable=redefined-builtin """ TVMC - TVM driver command-line interface """ From eba7d63f128cb66a364eae0ffcdb54d7e741f813 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Sun, 21 Mar 2021 16:46:22 -0400 Subject: [PATCH 6/7] add colon to match lint syntax --- python/tvm/driver/tvmc/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/driver/tvmc/__init__.py b/python/tvm/driver/tvmc/__init__.py index 7c6d06d02191..d9c15792349a 100644 --- a/python/tvm/driver/tvmc/__init__.py +++ b/python/tvm/driver/tvmc/__init__.py @@ -14,7 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -# pylint disable=redefined-builtin +# pylint: disable=redefined-builtin """ TVMC - TVM driver command-line interface """ From 250efd11a4b61d7a5362aa0d20c9cb57c7ee7cb2 Mon Sep 17 00:00:00 2001 From: Jocelyn Date: Tue, 23 Mar 2021 15:22:44 -0400 Subject: [PATCH 7/7] change import so must use tvm.driver.tvmc instead of tvm.tvmc --- python/tvm/__init__.py | 1 - tests/python/driver/tvmc/test_frontends.py | 10 +--------- 2 files changed, 1 insertion(+), 10 deletions(-) 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 diff --git a/tests/python/driver/tvmc/test_frontends.py b/tests/python/driver/tvmc/test_frontends.py index 0c0bc144972f..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 @@ -137,16 +137,8 @@ def verify_load_model__onnx(model, **kwargs): def test_load_model__onnx(onnx_resnet50): # some CI environments wont offer onnx, so skip in case it is not present pytest.importorskip("onnx") -<<<<<<< HEAD mod, params = verify_load_model__onnx(onnx_resnet50) # check whether one known value is part of the params dict -======= - - mod, params = tvmc.load(onnx_resnet50) - assert type(mod) is IRModule - assert type(params) is dict - ## check whether one known value is part of the params dict ->>>>>>> adjust tests to new imports assert "resnetv24_batchnorm0_gamma" in params.keys() mod, params = verify_load_model__onnx(onnx_resnet50, freeze_params=True) # check that the parameter dict is empty, implying that they have been folded into constants