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.git


The following commit(s) were added to refs/heads/main by this push:
     new d883f5064f [REFACTOR] Remove runtime/object.py shim and route Object 
via tvm_ffi (#19440)
d883f5064f is described below

commit d883f5064f2170cf09de6c9d4a8a901dc7209f91
Author: Tianqi Chen <[email protected]>
AuthorDate: Sat Apr 25 12:20:01 2026 -0400

    [REFACTOR] Remove runtime/object.py shim and route Object via tvm_ffi 
(#19440)
    
    ## Summary
    
    TVM-side cleanup that drops the `python/tvm/runtime/object.py` shim and
    routes `tvm.runtime.Object` directly to `tvm_ffi.Object`. The
    `tvm.runtime.Object` re-export is preserved (now a re-export of
    `tvm_ffi.Object`) so external callers keep working.
    
    The load-bearing `__object_repr__` install — which wires TVM IR objects
    up to the rich C++ `ReprPrinter` registered through
    `init_ffi_api("node", ...)` — moves into
    `python/tvm/runtime/_ffi_node_api.py`.
    That module is already imported as a side-effect-only module from
    `python/tvm/runtime/__init__.py`, so the override fires at the right
    time (after `init_ffi_api` registers the C++ printer).
    
    `_ffi_node_api.AsRepr` itself is **kept**: `tvm_ffi`'s default repr is
    primitive (`ClassName(ptr)`); TVM IR objects need the rich printer
    registered via `init_ffi_api("node", ...)`. `AsRepr` is what bridges
    that printer back into Python `repr(obj)` and is also the runtime-only
    fallback when `libtvm.so` is unavailable.
    
    The 7 in-tree importers of the deleted shim (plus one straggler in
    `runtime/disco/session.py`) are switched to either
    `from tvm.runtime import Object` or `from tvm_ffi import Object`,
    depending on which pattern the file already uses.
    
    ## Test plan
    
    - [x] `python -c "import tvm; print(repr(tvm.IRModule({})))"` produces
      TVMScript-style output (rich repr preserved).
    - [x] `pytest tests/python/all-platform-minimal-test/ -x` — 75 passed,
      77 skipped (matches baseline).
    - [x] `pytest tests/python/tirx-base/ -x` — 273 passed, 2 skipped.
    - [x] `pre-commit run --files <changed files>` — all hooks pass.
    - [ ] CI green.
---
 python/tvm/__init__.py                      |  2 +-
 python/tvm/ir/function.py                   |  2 +-
 python/tvm/ir/global_info.py                |  3 +--
 python/tvm/ir/module.py                     |  3 +--
 python/tvm/relax/distributed/struct_info.py |  2 +-
 python/tvm/relax/op/base.py                 |  3 +--
 python/tvm/relax/testing/transform.py       |  2 +-
 python/tvm/relax/testing/vm.py              |  2 +-
 python/tvm/runtime/__init__.py              |  7 +++++--
 python/tvm/runtime/_ffi_node_api.py         |  6 ++++++
 python/tvm/runtime/disco/session.py         |  3 +--
 python/tvm/runtime/object.py                | 27 ---------------------------
 12 files changed, 20 insertions(+), 42 deletions(-)

diff --git a/python/tvm/__init__.py b/python/tvm/__init__.py
index 7dca7b36fb..5798f684dd 100644
--- a/python/tvm/__init__.py
+++ b/python/tvm/__init__.py
@@ -30,7 +30,7 @@ from .base import TVMError, __version__, _RUNTIME_ONLY
 
 # top-level alias
 # tvm.runtime
-from .runtime.object import Object
+from .runtime import Object
 from .runtime._tensor import device, cpu, cuda, opencl, vulkan, metal
 from .runtime._tensor import vpi, rocm, ext_dev, hexagon
 from .runtime import DataType, DataTypeCode
diff --git a/python/tvm/ir/function.py b/python/tvm/ir/function.py
index 4a13170226..c5f4a262bd 100644
--- a/python/tvm/ir/function.py
+++ b/python/tvm/ir/function.py
@@ -22,7 +22,7 @@ from enum import IntEnum
 import tvm_ffi
 
 import tvm.runtime
-from tvm.runtime.object import Object
+from tvm.runtime import Object
 
 from . import _ffi_api
 from .attrs import DictAttrs
diff --git a/python/tvm/ir/global_info.py b/python/tvm/ir/global_info.py
index 1039320f4b..14bdb76b08 100644
--- a/python/tvm/ir/global_info.py
+++ b/python/tvm/ir/global_info.py
@@ -19,8 +19,7 @@
 import tvm_ffi
 
 import tvm
-from tvm.runtime import Device
-from tvm.runtime.object import Object
+from tvm.runtime import Device, Object
 
 from . import _ffi_api
 
diff --git a/python/tvm/ir/module.py b/python/tvm/ir/module.py
index b64baa7edc..a9f43e09bd 100644
--- a/python/tvm/ir/module.py
+++ b/python/tvm/ir/module.py
@@ -19,8 +19,7 @@
 import tvm_ffi
 
 import tvm
-from tvm.runtime import Scriptable
-from tvm.runtime.object import Object
+from tvm.runtime import Object, Scriptable
 
 from . import _ffi_api
 from . import expr as _expr
diff --git a/python/tvm/relax/distributed/struct_info.py 
b/python/tvm/relax/distributed/struct_info.py
index 2d85b2fa6d..39e61f4adc 100644
--- a/python/tvm/relax/distributed/struct_info.py
+++ b/python/tvm/relax/distributed/struct_info.py
@@ -24,7 +24,7 @@ import tvm_ffi
 from tvm import TVMError
 from tvm.ir import Span
 from tvm.relax.struct_info import StructInfo, TensorStructInfo
-from tvm.runtime.object import Object
+from tvm.runtime import Object
 
 from . import _ffi_api
 from .global_info import DeviceMesh
diff --git a/python/tvm/relax/op/base.py b/python/tvm/relax/op/base.py
index 453ca2a3d6..04f12d087f 100644
--- a/python/tvm/relax/op/base.py
+++ b/python/tvm/relax/op/base.py
@@ -23,8 +23,7 @@ import tvm_ffi
 
 import tvm
 import tvm.runtime
-from tvm.runtime import ObjectConvertible
-from tvm.runtime.object import Object
+from tvm.runtime import Object, ObjectConvertible
 
 from ...ir import PrimExpr
 from ..expr import Call, Expr, ExternFunc, GlobalVar, ShapeExpr, StringImm, Var
diff --git a/python/tvm/relax/testing/transform.py 
b/python/tvm/relax/testing/transform.py
index 14da04ebd7..d269b90279 100644
--- a/python/tvm/relax/testing/transform.py
+++ b/python/tvm/relax/testing/transform.py
@@ -25,7 +25,7 @@ import tvm_ffi
 import tvm
 from tvm.ir.module import IRModule
 from tvm.relax.expr import Call, DataflowBlock, Var
-from tvm.runtime.object import Object
+from tvm.runtime import Object
 
 
 def ApplyEmptyCppMutator() -> tvm.ir.transform.Pass:
diff --git a/python/tvm/relax/testing/vm.py b/python/tvm/relax/testing/vm.py
index cbffb92912..4048b904ae 100644
--- a/python/tvm/relax/testing/vm.py
+++ b/python/tvm/relax/testing/vm.py
@@ -23,7 +23,7 @@ import numpy as np  # type: ignore
 
 import tvm
 from tvm import relax
-from tvm.runtime.object import Object
+from tvm.runtime import Object
 
 
 @tvm.register_global_func("test.vm.move")
diff --git a/python/tvm/runtime/__init__.py b/python/tvm/runtime/__init__.py
index 97b9d006eb..6f25216fb4 100644
--- a/python/tvm/runtime/__init__.py
+++ b/python/tvm/runtime/__init__.py
@@ -17,11 +17,14 @@
 # under the License.
 """TVM runtime namespace."""
 
-from tvm_ffi import convert
+from tvm_ffi import convert, Object
 from tvm_ffi._dtype import dtype as DataType, DataTypeCode
 
+# Import _ffi_node_api for its side effect of installing AsRepr as
+# tvm_ffi.core.__object_repr__ so TVM IR objects use the rich C++ ReprPrinter.
+from . import _ffi_node_api
+
 # class exposures
-from .object import Object
 from .script_printer import Scriptable
 from .object_generic import ObjectConvertible
 from .device import Device
diff --git a/python/tvm/runtime/_ffi_node_api.py 
b/python/tvm/runtime/_ffi_node_api.py
index e9537fd529..18af61ec75 100644
--- a/python/tvm/runtime/_ffi_node_api.py
+++ b/python/tvm/runtime/_ffi_node_api.py
@@ -39,3 +39,9 @@ def LoadJSON(json_str):
 
 # Exports functions registered in node namespace.
 tvm_ffi.init_ffi_api("node", __name__)
+
+# Override the default repr function for tvm_ffi.core.Object so TVM IR
+# objects use the rich C++ ReprPrinter (registered above via init_ffi_api),
+# falling back to the runtime-only AsRepr defined in this file when libtvm.so
+# is not available.
+tvm_ffi.core.__object_repr__ = AsRepr
diff --git a/python/tvm/runtime/disco/session.py 
b/python/tvm/runtime/disco/session.py
index 24a3e993bd..08afbbfc80 100644
--- a/python/tvm/runtime/disco/session.py
+++ b/python/tvm/runtime/disco/session.py
@@ -26,12 +26,11 @@ from collections.abc import Callable, Sequence
 from typing import Any, Optional, Union
 
 import numpy as np
-from tvm_ffi import Shape, get_global_func, register_global_func, 
register_object
+from tvm_ffi import Object, Shape, get_global_func, register_global_func, 
register_object
 
 from .._tensor import Tensor
 from .._tensor import tensor as _as_Tensor
 from ..device import Device
-from ..object import Object
 from . import _ffi_api, process_pool  # pylint: disable=unused-import
 
 
diff --git a/python/tvm/runtime/object.py b/python/tvm/runtime/object.py
deleted file mode 100644
index 89ce9aa1fd..0000000000
--- a/python/tvm/runtime/object.py
+++ /dev/null
@@ -1,27 +0,0 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements.  See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership.  The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License.  You may obtain a copy of the License at
-#
-#   http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied.  See the License for the
-# specific language governing permissions and limitations
-# under the License.
-# pylint: disable=invalid-name, unused-import
-"""Runtime Object API"""
-
-import tvm_ffi.core
-from tvm_ffi.core import Object
-
-from . import _ffi_node_api
-
-tvm_ffi.core._set_class_object(Object)
-# override the default repr function for tvm_ffi.core.Object
-tvm_ffi.core.__object_repr__ = _ffi_node_api.AsRepr

Reply via email to