Files
Tianqi Chen 9edd5bd958 [REFACTOR] Remove tvm.runtime.packed_func and container shims; route via tvm_ffi (#19442)
## Summary

- Delete the three Python shim modules that re-exported tvm-ffi types
under `tvm.runtime` / `tvm.ir`:
`python/tvm/runtime/packed_func.py`, `python/tvm/runtime/container.py`,
`python/tvm/ir/container.py`.
- Drop the matching re-exports from `tvm.runtime`, `tvm.ir`, and `tvm`
package init files, so
`tvm.runtime.PackedFunc`, `tvm.runtime.ShapeTuple`,
`tvm.runtime.String`, `tvm.ir.Array`,
  `tvm.ir.Map`, and `tvm.container.Array` no longer exist.
- Migrate every productive caller, test, and tutorial to the canonical
names: `tvm_ffi.Function`,
`tvm_ffi.Shape`, `tvm_ffi.core.String`, `tvm_ffi.Array`, and
`tvm_ffi.Map`.

## Test plan

- [x] `pytest tests/python/all-platform-minimal-test` (75 passed, 77
skipped)
- [x] `pytest tests/python/runtime/test_runtime_container.py
tests/python/all-platform-minimal-test/test_runtime_packed_func.py` (20
passed)
- [x] `pytest tests/python/ir/test_node_reflection.py
tests/python/ir/test_container_structural_equal.py` (32 passed)
- [x] `pytest tests/python/relax/test_vm_build.py
tests/python/relax/test_vm_execbuilder.py
tests/python/relax/test_vm_codegen_only.py` (125 passed, 2 xfailed)
- [x] `pytest tests/python/relax/test_runtime_builtin.py
tests/python/relax/test_op_misc.py` (19 passed)
- [x] `pytest tests/python/target/test_target_target.py` (37 passed, 3
skipped)
- [x] `pre-commit run` clean on touched files
2026-04-25 11:02:08 -04:00

128 lines
3.8 KiB
Python

# 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
# ruff: noqa: RUF005
"""Internal DiscoWorker for Disco ProcessSession."""
import os
import sys
from collections.abc import Callable
from tvm_ffi import Shape, get_global_func, register_global_func
from tvm_ffi.core import String
import tvm
from tvm.runtime import Tensor, tensor
@register_global_func("tests.disco.add_one", override=True)
def _add_one(x: int) -> int:
return x + 1
@register_global_func("tests.disco.add_one_float", override=True)
def _add_one_float(x: float):
return x + 0.5
@register_global_func("tests.disco.add_one_tensor", override=True)
def _add_one_tensor(x: Tensor) -> Tensor:
return tensor(x.numpy() + 1)
@register_global_func("tests.disco.str", override=True)
def _str_func(x: str):
return x + "_suffix"
@register_global_func("tests.disco.str_obj", override=True)
def _str_obj_func(x: str):
assert isinstance(x, str)
return String(x + "_suffix")
@register_global_func("tests.disco.shape_tuple", override=True)
def _shape_tuple_func(x: Shape):
assert isinstance(x, Shape)
return Shape(list(x) + [4, 5])
@register_global_func("tests.disco.test_callback", override=True)
def _make_callback(device: tvm.runtime.Device) -> Callable[[str, int], Tensor]:
"""For use in tests/python/disco/test_callback.py
This function simulates a callback to be used for lazy parameter
loading.
Parameters
----------
device: tvm.runtime.Device
The device on which parameters should be located, when
returned by the callback function.
Returns
-------
fget_item: Callable[[str,int], Tensor]
A callback function that accepts a parameter's name and index,
and returns the specified parameter.
"""
import numpy as np # pylint: disable=import-outside-toplevel
def fget_item(param_name: str, param_index: int) -> Tensor:
if param_index == 0:
assert param_name == "A"
arr = np.arange(16).reshape([4, 4]).astype("int32")
elif param_index == 1:
assert param_name == "B"
arr = np.arange(4).reshape([2, 2]).astype("float32")
else:
raise ValueError(f"Unexpected index {param_index}")
return tvm.runtime.tensor(arr, device=device)
return fget_item
def main():
"""Main worker function"""
if len(sys.argv) != 6:
print("Usage: <worker_id> <num_workers> <num_groups> <read_fd> <write_fd>")
return
worker_id = int(sys.argv[1])
num_workers = int(sys.argv[2])
num_groups = int(sys.argv[3])
if sys.platform == "win32":
import msvcrt # pylint: disable=import-outside-toplevel,import-error
reader = msvcrt.open_osfhandle(int(sys.argv[4]), os.O_BINARY)
writer = msvcrt.open_osfhandle(int(sys.argv[5]), os.O_BINARY)
else:
reader = int(sys.argv[4])
writer = int(sys.argv[5])
worker_func = get_global_func("runtime.disco.WorkerProcess")
worker_func(worker_id, num_workers, num_groups, reader, writer)
if __name__ == "__main__":
try:
main()
except (OSError, KeyboardInterrupt):
pass