From 48f7394c6e24171da688095ee3427e0f36f271c8 Mon Sep 17 00:00:00 2001 From: Stella Laurenzo Date: Mon, 14 Aug 2023 16:43:41 -0700 Subject: [PATCH] Segregate the user-API and support in python runtime packages. (#14671) This keeps us from loading the Python native library while resolving console script executables. Prior to this, it would trip up Tracy because technically, the python interpreter would attach first and then the console binary would attach. By segregating the packages like this, we make such things impossible. --- runtime/bindings/python/CMakeLists.txt | 14 +++++++------- runtime/bindings/python/iree/_runtime/README.md | 14 ++++++++++++++ .../_runtime_libs.py => _runtime/libs.py} | 12 ++++++------ .../scripts/iree_benchmark_module/__main__.py | 4 ++-- .../scripts/iree_benchmark_trace}/__main__.py | 4 ++-- .../scripts/iree_cpuinfo/__main__.py | 0 .../scripts/iree_dump_module/__main__.py | 2 +- .../scripts/iree_run_module/__main__.py | 4 ++-- .../scripts/iree_run_trace}/__main__.py | 4 ++-- .../scripts/iree_tracy_capture/__main__.py | 0 .../bindings/python/iree/runtime/_binding.py | 7 +++++-- runtime/setup.py | 17 ++++++++++------- 12 files changed, 51 insertions(+), 31 deletions(-) create mode 100644 runtime/bindings/python/iree/_runtime/README.md rename runtime/bindings/python/iree/{runtime/_runtime_libs.py => _runtime/libs.py} (77%) rename runtime/bindings/python/iree/{runtime => _runtime}/scripts/iree_benchmark_module/__main__.py (80%) rename runtime/bindings/python/iree/{runtime/scripts/iree_run_trace => _runtime/scripts/iree_benchmark_trace}/__main__.py (81%) rename runtime/bindings/python/iree/{runtime => _runtime}/scripts/iree_cpuinfo/__main__.py (100%) rename runtime/bindings/python/iree/{runtime => _runtime}/scripts/iree_dump_module/__main__.py (87%) rename runtime/bindings/python/iree/{runtime => _runtime}/scripts/iree_run_module/__main__.py (80%) rename runtime/bindings/python/iree/{runtime/scripts/iree_benchmark_trace => _runtime/scripts/iree_run_trace}/__main__.py (80%) rename runtime/bindings/python/iree/{runtime => _runtime}/scripts/iree_tracy_capture/__main__.py (100%) diff --git a/runtime/bindings/python/CMakeLists.txt b/runtime/bindings/python/CMakeLists.txt index 9d45ca0606f6..0aa4c5ffa36f 100644 --- a/runtime/bindings/python/CMakeLists.txt +++ b/runtime/bindings/python/CMakeLists.txt @@ -114,7 +114,6 @@ iree_py_library( SRCS "iree/runtime/__init__.py" "iree/runtime/_binding.py" - "iree/runtime/_runtime_libs.py" "iree/runtime/array_interop.py" "iree/runtime/benchmark.py" "iree/runtime/flags.py" @@ -123,12 +122,13 @@ iree_py_library( "iree/runtime/system_setup.py" "iree/runtime/tracing.py" "iree/runtime/version.py" - "iree/runtime/scripts/iree_benchmark_trace/__main__.py" - "iree/runtime/scripts/iree_benchmark_module/__main__.py" - "iree/runtime/scripts/iree_cpuinfo/__main__.py" - "iree/runtime/scripts/iree_run_trace/__main__.py" - "iree/runtime/scripts/iree_run_module/__main__.py" - "iree/runtime/scripts/iree_tracy_capture/__main__.py" + "iree/_runtime/libs.py" + "iree/_runtime/scripts/iree_benchmark_trace/__main__.py" + "iree/_runtime/scripts/iree_benchmark_module/__main__.py" + "iree/_runtime/scripts/iree_cpuinfo/__main__.py" + "iree/_runtime/scripts/iree_run_trace/__main__.py" + "iree/_runtime/scripts/iree_run_module/__main__.py" + "iree/_runtime/scripts/iree_tracy_capture/__main__.py" PYEXT_DEPS iree_runtime_bindings_python_PyExtRt ) diff --git a/runtime/bindings/python/iree/_runtime/README.md b/runtime/bindings/python/iree/_runtime/README.md new file mode 100644 index 000000000000..6a6a67764c29 --- /dev/null +++ b/runtime/bindings/python/iree/_runtime/README.md @@ -0,0 +1,14 @@ +# Non user-API runtime support. + +This package contains elements of the runtime which are not user +visible parts of the API. It is a pure namespace package that does +no native initialization by default (unlike the `runtime` package +which initializes the native library as part of exporting its API). + +This distinction is important because some runtime libraries (like +`tracy` will attempt to connect to a profiler on load and there are +cases where we want to do things without triggering such load-time +behavior). + +As peers to this package, there are native `_runtime_libs*` packages +for different variations of native libraries and tools. diff --git a/runtime/bindings/python/iree/runtime/_runtime_libs.py b/runtime/bindings/python/iree/_runtime/libs.py similarity index 77% rename from runtime/bindings/python/iree/runtime/_runtime_libs.py rename to runtime/bindings/python/iree/_runtime/libs.py index d27e300fda79..135cf4277dd1 100644 --- a/runtime/bindings/python/iree/runtime/_runtime_libs.py +++ b/runtime/bindings/python/iree/_runtime/libs.py @@ -3,7 +3,7 @@ # Licensed under the Apache License v2.0 with LLVM Exceptions. # See https://llvm.org/LICENSE.txt for license information. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -"""Trampoline to the right iree._runtime_libs* module.""" +"""Trampoline to the right iree._runtime_libs*._runtime module.""" import os import sys @@ -12,7 +12,7 @@ variant = os.getenv("IREE_PY_RUNTIME", "default") if variant == "tracy": try: - import iree._runtime_libs_tracy as _libs + import iree._runtime_libs_tracy as libs except ModuleNotFoundError as e: raise ModuleNotFoundError( "IREE Tracy runtime requested via IREE_PY_RUNTIME but it is not " @@ -25,8 +25,8 @@ f"Unknown value for IREE_PY_RUNTIME env var ({variant}): " f"Using default" ) variant = "default" - import iree._runtime_libs as _libs + import iree._runtime_libs as libs -_libs.name = variant -_libs.library_path = _libs.__path__[0] -sys.modules[__name__] = _libs +libs.name = variant +libs.library_path = libs.__path__[0] +sys.modules[__name__] = libs diff --git a/runtime/bindings/python/iree/runtime/scripts/iree_benchmark_module/__main__.py b/runtime/bindings/python/iree/_runtime/scripts/iree_benchmark_module/__main__.py similarity index 80% rename from runtime/bindings/python/iree/runtime/scripts/iree_benchmark_module/__main__.py rename to runtime/bindings/python/iree/_runtime/scripts/iree_benchmark_module/__main__.py index 0d9eabdabfd0..6e12f6708d67 100644 --- a/runtime/bindings/python/iree/runtime/scripts/iree_benchmark_module/__main__.py +++ b/runtime/bindings/python/iree/_runtime/scripts/iree_benchmark_module/__main__.py @@ -7,13 +7,13 @@ import os import subprocess import sys -from ... import _runtime_libs +from ... import libs def main(args=None): if args is None: args = sys.argv[1:] - exe = os.path.join(_runtime_libs.library_path, "iree-benchmark-module") + exe = os.path.join(libs.library_path, "iree-benchmark-module") return subprocess.call(args=[exe] + args) diff --git a/runtime/bindings/python/iree/runtime/scripts/iree_run_trace/__main__.py b/runtime/bindings/python/iree/_runtime/scripts/iree_benchmark_trace/__main__.py similarity index 81% rename from runtime/bindings/python/iree/runtime/scripts/iree_run_trace/__main__.py rename to runtime/bindings/python/iree/_runtime/scripts/iree_benchmark_trace/__main__.py index 4464d591cb3f..51a6fe02af7f 100644 --- a/runtime/bindings/python/iree/runtime/scripts/iree_run_trace/__main__.py +++ b/runtime/bindings/python/iree/_runtime/scripts/iree_benchmark_trace/__main__.py @@ -7,13 +7,13 @@ import os import subprocess import sys -from ... import _runtime_libs +from ... import libs def main(args=None): if args is None: args = sys.argv[1:] - exe = os.path.join(_runtime_libs.library_path, "iree-run-trace") + exe = os.path.join(libs.library_path, "iree-benchmark-trace") return subprocess.call(args=[exe] + args) diff --git a/runtime/bindings/python/iree/runtime/scripts/iree_cpuinfo/__main__.py b/runtime/bindings/python/iree/_runtime/scripts/iree_cpuinfo/__main__.py similarity index 100% rename from runtime/bindings/python/iree/runtime/scripts/iree_cpuinfo/__main__.py rename to runtime/bindings/python/iree/_runtime/scripts/iree_cpuinfo/__main__.py diff --git a/runtime/bindings/python/iree/runtime/scripts/iree_dump_module/__main__.py b/runtime/bindings/python/iree/_runtime/scripts/iree_dump_module/__main__.py similarity index 87% rename from runtime/bindings/python/iree/runtime/scripts/iree_dump_module/__main__.py rename to runtime/bindings/python/iree/_runtime/scripts/iree_dump_module/__main__.py index caa84b5df77c..8f9489d0bb3d 100644 --- a/runtime/bindings/python/iree/runtime/scripts/iree_dump_module/__main__.py +++ b/runtime/bindings/python/iree/_runtime/scripts/iree_dump_module/__main__.py @@ -15,7 +15,7 @@ def main(args=None): if args is None: args = sys.argv[1:] - exe = os.path.join(_runtime_libs.library_path, "iree-dump-module") + exe = os.path.join(_runtime_libs.__path__[0], "iree-dump-module") return subprocess.call(args=[exe] + args) diff --git a/runtime/bindings/python/iree/runtime/scripts/iree_run_module/__main__.py b/runtime/bindings/python/iree/_runtime/scripts/iree_run_module/__main__.py similarity index 80% rename from runtime/bindings/python/iree/runtime/scripts/iree_run_module/__main__.py rename to runtime/bindings/python/iree/_runtime/scripts/iree_run_module/__main__.py index e8c2abef6fc2..8b1fd6998591 100644 --- a/runtime/bindings/python/iree/runtime/scripts/iree_run_module/__main__.py +++ b/runtime/bindings/python/iree/_runtime/scripts/iree_run_module/__main__.py @@ -7,13 +7,13 @@ import os import subprocess import sys -from ... import _runtime_libs +from ... import libs def main(args=None): if args is None: args = sys.argv[1:] - exe = os.path.join(_runtime_libs.library_path, "iree-run-module") + exe = os.path.join(libs.library_path, "iree-run-module") return subprocess.call(args=[exe] + args) diff --git a/runtime/bindings/python/iree/runtime/scripts/iree_benchmark_trace/__main__.py b/runtime/bindings/python/iree/_runtime/scripts/iree_run_trace/__main__.py similarity index 80% rename from runtime/bindings/python/iree/runtime/scripts/iree_benchmark_trace/__main__.py rename to runtime/bindings/python/iree/_runtime/scripts/iree_run_trace/__main__.py index 6fbf0e26abeb..ea7170f4775c 100644 --- a/runtime/bindings/python/iree/runtime/scripts/iree_benchmark_trace/__main__.py +++ b/runtime/bindings/python/iree/_runtime/scripts/iree_run_trace/__main__.py @@ -7,13 +7,13 @@ import os import subprocess import sys -from ... import _runtime_libs +from ... import libs def main(args=None): if args is None: args = sys.argv[1:] - exe = os.path.join(_runtime_libs.library_path, "iree-benchmark-trace") + exe = os.path.join(libs.library_path, "iree-run-trace") return subprocess.call(args=[exe] + args) diff --git a/runtime/bindings/python/iree/runtime/scripts/iree_tracy_capture/__main__.py b/runtime/bindings/python/iree/_runtime/scripts/iree_tracy_capture/__main__.py similarity index 100% rename from runtime/bindings/python/iree/runtime/scripts/iree_tracy_capture/__main__.py rename to runtime/bindings/python/iree/_runtime/scripts/iree_tracy_capture/__main__.py diff --git a/runtime/bindings/python/iree/runtime/_binding.py b/runtime/bindings/python/iree/runtime/_binding.py index 2237bb01ba33..99aa55aa2cef 100644 --- a/runtime/bindings/python/iree/runtime/_binding.py +++ b/runtime/bindings/python/iree/runtime/_binding.py @@ -9,10 +9,13 @@ locates the actual _runtime module based on environment configuration. TODO: We could rename this to _runtime since it a trampoline for -the _runtime module we load from elsehwhere. +the _runtime native extension module we load from elsehwhere. """ import sys -from ._runtime_libs import _runtime + +# "_runtime" is the native extension within the _runtime_libs package +# selected. +from .._runtime.libs import _runtime sys.modules[__name__] = _runtime diff --git a/runtime/setup.py b/runtime/setup.py index c4a6212a6379..1a2be87e0138 100644 --- a/runtime/setup.py +++ b/runtime/setup.py @@ -435,6 +435,8 @@ def generate_version_py(): include=[ "iree.runtime", "iree.runtime.*", + "iree._runtime", + "iree._runtime.*", ], ) + [ @@ -521,6 +523,7 @@ def generate_version_py(): # Note: Must be relative path, so we line this up with the absolute # path built above. Note that this must exist prior to the call. "iree.runtime": "bindings/python/iree/runtime", + "iree._runtime": "bindings/python/iree/_runtime", "iree._runtime_libs": f"{CMAKE_INSTALL_DIR_REL}/python_packages/iree_runtime/iree/_runtime_libs", }, { @@ -563,16 +566,16 @@ def generate_version_py(): ), entry_points={ "console_scripts": [ - "iree-run-module = iree.runtime.scripts.iree_run_module.__main__:main", - "iree-run-trace = iree.runtime.scripts.iree_run_trace.__main__:main", - "iree-benchmark-module = iree.runtime.scripts.iree_benchmark_module.__main__:main", - "iree-benchmark-trace = iree.runtime.scripts.iree_benchmark_trace.__main__:main", - "iree-dump-module = iree.runtime.scripts.iree_dump_module.__main__:main", - "iree-cpuinfo = iree.runtime.scripts.iree_cpuinfo.__main__:main", + "iree-run-module = iree._runtime.scripts.iree_run_module.__main__:main", + "iree-run-trace = iree._runtime.scripts.iree_run_trace.__main__:main", + "iree-benchmark-module = iree._runtime.scripts.iree_benchmark_module.__main__:main", + "iree-benchmark-trace = iree._runtime.scripts.iree_benchmark_trace.__main__:main", + "iree-dump-module = iree._runtime.scripts.iree_dump_module.__main__:main", + "iree-cpuinfo = iree._runtime.scripts.iree_cpuinfo.__main__:main", ] + ( [ - "iree-tracy-capture = iree.runtime.scripts.iree_tracy_capture.__main__:main" + "iree-tracy-capture = iree._runtime.scripts.iree_tracy_capture.__main__:main" ] if ENABLE_TRACY_TOOLS else []