gemini-code-assist[bot] commented on code in PR #19727:
URL: https://github.com/apache/tvm/pull/19727#discussion_r3392114987


##########
python/tvm/support/cc.py:
##########
@@ -367,7 +366,7 @@ def _linux_compile(
     (out, _) = proc.communicate()
     if proc.returncode != 0:
         msg = "Compilation error:\n"
-        msg += py_str(out)
+        msg += out.decode("utf-8")

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   When decoding output from external compilers or tools (such as `gcc`, 
`clang`, `ar`, or `ld`), the output may contain non-UTF-8 characters depending 
on the system's active locale or localized error messages (especially on 
Windows or non-English environments). Calling `.decode("utf-8")` without error 
handling can raise a `UnicodeDecodeError`, which masks the actual compilation 
or linker error.\n\nConsider using `errors="replace"` or 
`errors="backslashreplace"` to ensure that decoding always succeeds and the 
original error message is preserved and displayed.\n\nThis also applies to 
other `.decode("utf-8")` calls on subprocess outputs throughout the `support` 
module (e.g., in `clang.py`, `emcc.py`, `ndk.py`, `nvcc.py`, `rocm.py`, 
`tar.py`, `xcode.py`).
   
   ```python
           msg = "Compilation error:\n"\n        msg += out.decode("utf-8", 
errors="replace")
   ```



##########
python/tvm/base.py:
##########
@@ -16,110 +16,70 @@
 # under the License.
 # coding: utf-8
 # pylint: disable=invalid-name, import-outside-toplevel
-# ruff: noqa: F401
 """Base library for TVM."""
 
 import os
-import sys
 from pathlib import Path
 
 from tvm_ffi.libinfo import load_lib_ctypes
 
 from . import libinfo
 
-# ----------------------------
-# Python3 version.
-# ----------------------------
-if not (sys.version_info[0] >= 3 and sys.version_info[1] >= 9):
-    PY3STATEMENT = "The minimal Python requirement is Python 3.9"
-    raise Exception(PY3STATEMENT)
-
 # ----------------------------
 # library loading
 # ----------------------------
 
-# Known per-backend runtime DSOs that, when present, are loaded with
-# RTLD_GLOBAL so their static initializers register the device backend.
-_BACKEND_RUNTIME_LIBS = ["cuda", "vulkan", "opencl", "metal", "rocm", 
"hexagon", "extra"]
+# Whether only the runtime library is loaded (runtime-only wheel, or
+# ``TVM_USE_RUNTIME_LIB=1``). Set during library loading below.
+_RUNTIME_ONLY = os.environ.get("TVM_USE_RUNTIME_LIB") == "1"
+
+# Handles of the core libraries actually loaded, keyed by basename
+# (e.g. ``{"tvm_runtime": <CDLL>, "tvm_compiler": <CDLL>}``). Downstream /
+# autoloaded extensions can inspect this to skip duplicate libraries
+# (``"tvm_runtime" in _LOADED_LIBS``) and obtain the loaded handle.
+_LOADED_LIBS = {}
 
 
 def load_backend_libs(runtime_lib_path: str) -> None:
-    """Try to load each known backend runtime DSO; failures are silent."""
+    """Load each known backend runtime DSO into ``_LOADED_LIBS``; failures are 
silent."""
+    # Known per-backend runtime DSOs that, when present, are loaded with
+    # RTLD_GLOBAL so their static initializers register the device backend.
+    backend_runtime_libs = ["cuda", "vulkan", "opencl", "metal", "rocm", 
"hexagon", "extra"]
     runtime_dir = Path(runtime_lib_path).resolve().parent
-    for backend in _BACKEND_RUNTIME_LIBS:
+    for backend in backend_runtime_libs:
+        target_name = f"tvm_runtime_{backend}"
         try:
-            load_lib_ctypes(
+            _LOADED_LIBS[target_name] = load_lib_ctypes(
                 package="tvm",
-                target_name=f"tvm_runtime_{backend}",
+                target_name=target_name,
                 mode="RTLD_GLOBAL",
                 extra_lib_paths=[runtime_dir],
             )
         except (OSError, FileNotFoundError, RuntimeError):
             pass
 
 
-# The TVM C++ side is split into two shared libraries:
-#
-# - ``libtvm_runtime`` — runtime-only sources. Loaded with ``RTLD_GLOBAL`` so
-#   its symbols are exposed to subsequent loads (NVRTC kernels, downstream
-#   modules and so on resolve runtime symbols at link time).
-# - ``libtvm_compiler`` — compiler / IR / transform sources, links against
-#   ``libtvm_runtime``. Loaded with ``RTLD_LOCAL`` so compiler internals
-#   don't leak into the global symbol namespace.
-#
-# If the environment variable ``TVM_USE_RUNTIME_LIB`` is set to ``"1"``, or
-# the compiler library is simply not present (runtime-only wheel), only the
-# runtime is loaded and ``_LIB`` aliases ``_LIB_RUNTIME``.
-_extra_lib_paths = libinfo.package_lib_paths()
-_LIB_RUNTIME = load_lib_ctypes(
-    "tvm", "tvm_runtime", "RTLD_GLOBAL", extra_lib_paths=_extra_lib_paths
+# runtime is loaded RTLD_GLOBAL to expose its symbols to subsequent loads;
+# compiler is loaded RTLD_LOCAL.
+_LOADED_LIBS["tvm_runtime"] = load_lib_ctypes(
+    "tvm", "tvm_runtime", "RTLD_GLOBAL", 
extra_lib_paths=libinfo.package_lib_paths()
 )
 
 # After libtvm_runtime.so is in the global symbol namespace, scan the same
 # directory for per-backend DSOs (libtvm_runtime_cuda.so, etc.) and load each
 # with RTLD_GLOBAL so their static initializers register device backends.
-# Failures are swallowed silently — a missing driver just means that backend
-# is unavailable, not an error.
-load_backend_libs(_LIB_RUNTIME._name)
+load_backend_libs(_LOADED_LIBS["tvm_runtime"]._name)
 
-_RUNTIME_ONLY = os.environ.get("TVM_USE_RUNTIME_LIB") == "1"
-if _RUNTIME_ONLY:
-    _LIB = _LIB_RUNTIME
-else:
+if not _RUNTIME_ONLY:
     try:
-        _LIB = load_lib_ctypes(
-            "tvm", "tvm_compiler", "RTLD_LOCAL", 
extra_lib_paths=_extra_lib_paths
+        _LOADED_LIBS["tvm_compiler"] = load_lib_ctypes(
+            "tvm", "tvm_compiler", "RTLD_LOCAL", 
extra_lib_paths=libinfo.package_lib_paths()
         )
     except RuntimeError:
         # Compiler lib not present — fall back to runtime-only mode.
-        _LIB = _LIB_RUNTIME
         _RUNTIME_ONLY = True

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   When attempting to load the compiler library, `load_lib_ctypes` can raise an 
`OSError` (for example, if `libtvm_compiler.so` is present but fails to load 
due to missing dependencies like LLVM, or other system-specific dynamic linker 
issues). Catching only `RuntimeError` will allow `OSError` to propagate, 
crashing the import of `tvm` instead of gracefully falling back to runtime-only 
mode.\n\nConsider catching both `RuntimeError` and `OSError` to make the 
fallback mechanism more robust.
   
   ```python
       except (RuntimeError, OSError):\n        # Compiler lib not present — 
fall back to runtime-only mode.\n        _RUNTIME_ONLY = True
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to