From 816658dac062f32bb9792bbf035bffb9d8909362 Mon Sep 17 00:00:00 2001 From: KJlaccHoeUM9l Date: Tue, 11 Jan 2022 13:14:23 +0300 Subject: [PATCH 1/6] improved usability of TVM EP --- onnxruntime/__init__.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/onnxruntime/__init__.py b/onnxruntime/__init__.py index 25d761da6fcbf..110006f3db00f 100644 --- a/onnxruntime/__init__.py +++ b/onnxruntime/__init__.py @@ -44,6 +44,15 @@ except ImportError: pass +try: + # Working between the C++ and Python parts in TVM EP is done using the PackedFunc and Registry classes. + # In order to use a Python function in C++ code, it must be registered in the global table of functions. + # Registration is carried out through the JIT interface, so it is necessary to call + # special functions for registration. To do this, we need to make the following import. + import onnxruntime.providers.stvm +except ImportError: + pass + from onnxruntime.capi.onnxruntime_validation import package_name, version, cuda_version if version: __version__ = version From 47e89f836ccdbf8587acbdd656cf52339632aab2 Mon Sep 17 00:00:00 2001 From: KJlaccHoeUM9l Date: Mon, 17 Jan 2022 17:58:34 +0300 Subject: [PATCH 2/6] moved technical import under a condition related to TVM EP only --- onnxruntime/__init__.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/onnxruntime/__init__.py b/onnxruntime/__init__.py index 110006f3db00f..01ad28cbfbc4a 100644 --- a/onnxruntime/__init__.py +++ b/onnxruntime/__init__.py @@ -44,14 +44,17 @@ except ImportError: pass -try: - # Working between the C++ and Python parts in TVM EP is done using the PackedFunc and Registry classes. - # In order to use a Python function in C++ code, it must be registered in the global table of functions. - # Registration is carried out through the JIT interface, so it is necessary to call - # special functions for registration. To do this, we need to make the following import. - import onnxruntime.providers.stvm -except ImportError: - pass +if "StvmExecutionProvider" in get_available_providers(): + try: + # Working between the C++ and Python parts in TVM EP is done using the PackedFunc and Registry classes. + # In order to use a Python function in C++ code, it must be registered in the global table of functions. + # Registration is carried out through the JIT interface, so it is necessary to call + # special functions for registration. To do this, we need to make the following import. + import onnxruntime.providers.stvm + except ImportError as e: + onnxruntime_validation.warnings.warn( + f"WARNING: Failed to register python functions to work with TVM EP. More details: {e}" + ) from onnxruntime.capi.onnxruntime_validation import package_name, version, cuda_version if version: From b8551e78e934ef9d917183d17ae1da7b5a47476f Mon Sep 17 00:00:00 2001 From: KJlaccHoeUM9l Date: Thu, 20 Jan 2022 16:52:31 +0300 Subject: [PATCH 3/6] Revert "moved technical import under a condition related to TVM EP only" This reverts commit 47e89f836ccdbf8587acbdd656cf52339632aab2. --- onnxruntime/__init__.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/onnxruntime/__init__.py b/onnxruntime/__init__.py index 01ad28cbfbc4a..110006f3db00f 100644 --- a/onnxruntime/__init__.py +++ b/onnxruntime/__init__.py @@ -44,17 +44,14 @@ except ImportError: pass -if "StvmExecutionProvider" in get_available_providers(): - try: - # Working between the C++ and Python parts in TVM EP is done using the PackedFunc and Registry classes. - # In order to use a Python function in C++ code, it must be registered in the global table of functions. - # Registration is carried out through the JIT interface, so it is necessary to call - # special functions for registration. To do this, we need to make the following import. - import onnxruntime.providers.stvm - except ImportError as e: - onnxruntime_validation.warnings.warn( - f"WARNING: Failed to register python functions to work with TVM EP. More details: {e}" - ) +try: + # Working between the C++ and Python parts in TVM EP is done using the PackedFunc and Registry classes. + # In order to use a Python function in C++ code, it must be registered in the global table of functions. + # Registration is carried out through the JIT interface, so it is necessary to call + # special functions for registration. To do this, we need to make the following import. + import onnxruntime.providers.stvm +except ImportError: + pass from onnxruntime.capi.onnxruntime_validation import package_name, version, cuda_version if version: From 89e17693cc98211f4c1176886352c22fb7d2d9f0 Mon Sep 17 00:00:00 2001 From: KJlaccHoeUM9l Date: Thu, 20 Jan 2022 16:53:06 +0300 Subject: [PATCH 4/6] Revert "improved usability of TVM EP" This reverts commit 816658dac062f32bb9792bbf035bffb9d8909362. --- onnxruntime/__init__.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/onnxruntime/__init__.py b/onnxruntime/__init__.py index 110006f3db00f..25d761da6fcbf 100644 --- a/onnxruntime/__init__.py +++ b/onnxruntime/__init__.py @@ -44,15 +44,6 @@ except ImportError: pass -try: - # Working between the C++ and Python parts in TVM EP is done using the PackedFunc and Registry classes. - # In order to use a Python function in C++ code, it must be registered in the global table of functions. - # Registration is carried out through the JIT interface, so it is necessary to call - # special functions for registration. To do this, we need to make the following import. - import onnxruntime.providers.stvm -except ImportError: - pass - from onnxruntime.capi.onnxruntime_validation import package_name, version, cuda_version if version: __version__ = version From 313e26d056334dcde6d85177803e5d8f48d741bd Mon Sep 17 00:00:00 2001 From: KJlaccHoeUM9l Date: Thu, 20 Jan 2022 17:04:02 +0300 Subject: [PATCH 5/6] add conditional _ld_preload.py file extension for TVM EP --- setup.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/setup.py b/setup.py index 2a1ec7f25d9b5..80af93760ecad 100644 --- a/setup.py +++ b/setup.py @@ -145,6 +145,28 @@ def _rewrite_ld_preload_tensorrt(self, to_preload): f.write(' import os\n') f.write(' os.environ["ORT_TENSORRT_UNAVAILABLE"] = "1"\n') + def _rewrite_ld_preload_tvm(self): + with open('onnxruntime/capi/_ld_preload.py', 'a') as f: + f.write('import warnings\n\n') + f.write('try:\n') + f.write(' # This import is necessary in order to delegate the loading of libtvm.so to TVM.\n') + f.write(' import tvm\n') + f.write('except ImportError as e:\n') + f.write(' warnings.warn(\n') + f.write(' f"WARNING: Failed to import TVM, libtvm.so was not loaded. More details: {e}"\n') + f.write(' )\n\n') + f.write('try:\n') + f.write(' # Working between the C++ and Python parts in TVM EP is done using\n') + f.write(' # the PackedFunc and Registry classes. In order to use a Python function in C++ code,\n') + f.write(' # it must be registered in the global table of functions. Registration is carried out\n') + f.write(' # through JIT interface, so it is necessary to call special functions for registration.\n') + f.write(' # To do this, we need to make the following import.\n') + f.write(' import onnxruntime.providers.stvm\n') + f.write('except ImportError as e:\n') + f.write(' warnings.warn(\n') + f.write(' f"WARNING: Failed to register python functions for TVM EP. More details: {e}"\n') + f.write(' )\n') + def run(self): if is_manylinux: source = 'onnxruntime/capi/onnxruntime_pybind11_state.so' @@ -207,6 +229,8 @@ def run(self): self._rewrite_ld_preload(to_preload) self._rewrite_ld_preload_cuda(to_preload_cuda) self._rewrite_ld_preload_tensorrt(to_preload_tensorrt) + if package_name == 'onnxruntime-tvm': + self._rewrite_ld_preload_tvm() _bdist_wheel.run(self) if is_manylinux and not disable_auditwheel_repair: file = glob(path.join(self.dist_dir, '*linux*.whl'))[0] From 908a5b4099e51f8ca13cb956f0bc32bc8fbd54e1 Mon Sep 17 00:00:00 2001 From: KJlaccHoeUM9l Date: Fri, 21 Jan 2022 10:20:52 +0300 Subject: [PATCH 6/6] improve readability of inserted code --- setup.py | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/setup.py b/setup.py index 80af93760ecad..6130fcdbba2fb 100644 --- a/setup.py +++ b/setup.py @@ -12,6 +12,7 @@ import platform import subprocess import sys +import textwrap import datetime from pathlib import Path @@ -147,25 +148,30 @@ def _rewrite_ld_preload_tensorrt(self, to_preload): def _rewrite_ld_preload_tvm(self): with open('onnxruntime/capi/_ld_preload.py', 'a') as f: - f.write('import warnings\n\n') - f.write('try:\n') - f.write(' # This import is necessary in order to delegate the loading of libtvm.so to TVM.\n') - f.write(' import tvm\n') - f.write('except ImportError as e:\n') - f.write(' warnings.warn(\n') - f.write(' f"WARNING: Failed to import TVM, libtvm.so was not loaded. More details: {e}"\n') - f.write(' )\n\n') - f.write('try:\n') - f.write(' # Working between the C++ and Python parts in TVM EP is done using\n') - f.write(' # the PackedFunc and Registry classes. In order to use a Python function in C++ code,\n') - f.write(' # it must be registered in the global table of functions. Registration is carried out\n') - f.write(' # through JIT interface, so it is necessary to call special functions for registration.\n') - f.write(' # To do this, we need to make the following import.\n') - f.write(' import onnxruntime.providers.stvm\n') - f.write('except ImportError as e:\n') - f.write(' warnings.warn(\n') - f.write(' f"WARNING: Failed to register python functions for TVM EP. More details: {e}"\n') - f.write(' )\n') + f.write(textwrap.dedent( + """ + import warnings + + try: + # This import is necessary in order to delegate the loading of libtvm.so to TVM. + import tvm + except ImportError as e: + warnings.warn( + f"WARNING: Failed to import TVM, libtvm.so was not loaded. More details: {e}" + ) + try: + # Working between the C++ and Python parts in TVM EP is done using the PackedFunc and + # Registry classes. In order to use a Python function in C++ code, it must be registered in + # the global table of functions. Registration is carried out through the JIT interface, + # so it is necessary to call special functions for registration. + # To do this, we need to make the following import. + import onnxruntime.providers.stvm + except ImportError as e: + warnings.warn( + f"WARNING: Failed to register python functions to work with TVM EP. More details: {e}" + ) + """ + )) def run(self): if is_manylinux: