96cba60464
This PR adds an autoload mechanism for out-of-tree backends, simplifies
TVM's Python library loading, and removes `TVMError` in favor of native
Python errors.
## Autoload out-of-tree backends
Out-of-tree packages can register an autoload callable under the
`tvm.backends` entry-point group (mirroring torch's device-backend
autoload). At `import tvm` startup each entry point is discovered and
its callable invoked once, after the core runtime and the `tvm`
namespace are fully initialized, so an extension can register
ops/targets/funcs or load extra libraries.
```toml
[project.entry-points."tvm.backends"]
tvm_foo = "tvm_foo:_autoload"
```
A failing extension is caught and surfaced via `warnings.warn` so it
cannot break `import tvm`. Autoload can be disabled with
`TVM_DEVICE_BACKEND_AUTOLOAD=0`.
## Simplify library loading
The library-loading path in `base.py` is consolidated around a single
`_LOADED_LIBS` dict (basename to ctypes handle) so downstream and
autoloaded extensions can skip already-loaded libraries; the per-backend
runtime DSO list is folded into `load_backend_libs`. Accumulated cruft
is removed: the Python-3.9 check, the readline shim, the `_FFI_MODE`
ctypes check, the `base.__version__` re-export, and `py_str` (call sites
inline `.decode("utf-8")`).
## Remove TVMError in favor of native Python errors
`TVMError` added a layer atop `RuntimeError` that downstream code had to
import and learn. It is removed; the registered FFI error kinds
(`InternalError`, `RPCError`, `OpError`, `DiagnosticError`,
`ScheduleError`) now subclass `RuntimeError` directly while staying
registered, so the FFI keeps throwing the right kinds. All `TVMError`
imports, `except`/`raise`/`isinstance` uses, and
`pytest.raises(tvm.TVMError)` sites move to the `RuntimeError` builtin.
128 lines
3.5 KiB
Python
128 lines
3.5 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.
|
|
|
|
"""Util to invoke tarball in the system."""
|
|
|
|
# pylint: disable=invalid-name
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
|
|
from . import utils
|
|
|
|
|
|
def tar(output, files):
|
|
"""Create tarball containing all files in root.
|
|
|
|
Parameters
|
|
----------
|
|
output : str
|
|
The target shared library.
|
|
|
|
files : list
|
|
List of files to be bundled.
|
|
"""
|
|
cmd = ["tar"]
|
|
cmd += ["-czf"]
|
|
temp = utils.tempdir()
|
|
fset = set()
|
|
for fname in files:
|
|
base = os.path.basename(fname)
|
|
if base in fset:
|
|
raise ValueError(f"duplicate file name {base}")
|
|
fset.add(base)
|
|
shutil.copy(fname, temp.relpath(base))
|
|
cmd += [output]
|
|
cmd += ["-C", temp.temp_dir]
|
|
cmd += temp.listdir()
|
|
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
(out, _) = proc.communicate()
|
|
|
|
if proc.returncode != 0:
|
|
msg = "Tar error:\n"
|
|
msg += out.decode("utf-8", errors="replace")
|
|
raise RuntimeError(msg)
|
|
|
|
|
|
# assign output format
|
|
tar.output_format = "tar"
|
|
|
|
|
|
def untar(tar_file, directory):
|
|
"""Unpack all tar files into the directory
|
|
|
|
Parameters
|
|
----------
|
|
tar_file : str
|
|
The source tar file.
|
|
|
|
directory : str
|
|
The target directory
|
|
"""
|
|
cmd = ["tar"]
|
|
cmd += ["-xf"]
|
|
cmd += [tar_file]
|
|
cmd += ["-C", directory]
|
|
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
(out, _) = proc.communicate()
|
|
|
|
if proc.returncode != 0:
|
|
msg = "Tar error:\n"
|
|
msg += out.decode("utf-8", errors="replace")
|
|
raise RuntimeError(msg)
|
|
|
|
|
|
def normalize_file_list_by_unpacking_tars(temp, file_list):
|
|
"""Normalize the file list by unpacking tars in list.
|
|
|
|
When a filename is a tar, it will untar it into an unique dir
|
|
in temp and return the list of files in the tar.
|
|
When a filename is a normal file, it will be simply added to the list.
|
|
|
|
This is useful to untar objects in tar and then turn
|
|
them into a library.
|
|
|
|
Parameters
|
|
----------
|
|
temp: tvm.support.utils.TempDirectory
|
|
A temp dir to hold the untared files.
|
|
|
|
file_list: List[str]
|
|
List of path
|
|
|
|
Returns
|
|
-------
|
|
ret_list: List[str]
|
|
An updated list of files
|
|
"""
|
|
temp_count = 0
|
|
ret_list = []
|
|
for file_path in file_list:
|
|
# enable extracting a tarball
|
|
if file_path.endswith(".tar"):
|
|
temp_dir = temp.relpath(f"temp{temp_count}")
|
|
temp_count += 1
|
|
os.mkdir(temp_dir)
|
|
untar(file_path, temp_dir)
|
|
# append all files inside
|
|
for root, _, files in os.walk(temp_dir):
|
|
for file in files:
|
|
ret_list.append(os.path.join(root, file))
|
|
else:
|
|
ret_list.append(file_path)
|
|
return ret_list
|