This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-ffi.git


The following commit(s) were added to refs/heads/main by this push:
     new 1cef9142 [FIX] Locate the Cython extension without importing it in 
find_cython_lib (#831)
1cef9142 is described below

commit 1cef91429a816cc4ccaeace8db32f45ef1524c29
Author: Saqib Khan <[email protected]>
AuthorDate: Sun Oct 11 16:53:12 2026 -0600

    [FIX] Locate the Cython extension without importing it in find_cython_lib 
(#831)
    
    ## Why
    
    `tvm-ffi-config --cython-lib-path` imported `tvm_ffi.core` to read its
    `__file__`. Importing the extension runs its module initialization,
    which
    optionally imports torch. The rest of the config CLI deliberately avoids
    eager imports, so only this flag paid the torch import cost, and any
    warning torch emits at import time (e.g. the pynvml FutureWarning) ended
    up on stderr, failing `tests/python/test_libinfo.py`.
    
    ## What
    
    `libinfo.find_cython_lib()` now uses
    `importlib.util.find_spec("tvm_ffi.core").origin` to locate the
    extension
    without executing it. The returned path is unchanged.
    
    Fixes #805
    
    ---------
    
    Co-authored-by: tqchen <[email protected]>
---
 python/tvm_ffi/libinfo.py | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/python/tvm_ffi/libinfo.py b/python/tvm_ffi/libinfo.py
index e37fa42b..bcdf8931 100644
--- a/python/tvm_ffi/libinfo.py
+++ b/python/tvm_ffi/libinfo.py
@@ -24,6 +24,7 @@ from __future__ import annotations
 
 import ctypes
 import importlib.metadata as im
+import importlib.util
 import os
 import sys
 from pathlib import Path
@@ -112,12 +113,15 @@ def find_dlpack_include_path() -> str:
 
 def find_cython_lib() -> str:
     """Find the path to tvm cython."""
-    from tvm_ffi import core  # noqa: PLC0415
-
-    try:
-        return str(Path(core.__file__).resolve())
-    except OSError:
-        pass
+    # Locate the extension without importing it: importing ``tvm_ffi.core``
+    # has side effects (e.g. it optionally imports torch), which is
+    # undesirable for lightweight callers such as ``tvm-ffi-config``.
+    spec = importlib.util.find_spec("tvm_ffi.core")
+    if spec is not None and spec.origin is not None:
+        try:
+            return str(Path(spec.origin).resolve())
+        except OSError:
+            pass
     raise RuntimeError("Cannot find tvm cython path.")
 
 

Reply via email to