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.
146 lines
4.5 KiB
Python
146 lines
4.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 emscripten compilers in the system."""
|
|
|
|
# pylint: disable=invalid-name
|
|
import os
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
from tvm import libinfo
|
|
|
|
|
|
def find_wasm_lib(name, optional=False):
|
|
"""Find a wasm/web asset (e.g. a ``.bc`` or ``.js`` file) under ``web/dist``.
|
|
|
|
Specializes the asset search to the wasm build outputs that live under
|
|
``web/dist/wasm`` and ``web/dist``, anchored on ``TVM_HOME`` (when set) or
|
|
the TVM source root otherwise.
|
|
|
|
Parameters
|
|
----------
|
|
name : str
|
|
The asset file name to look for.
|
|
|
|
optional : bool
|
|
When True, return None instead of raising if the asset is not found.
|
|
|
|
Returns
|
|
-------
|
|
found : list(str) or None
|
|
List of matching paths, or None when ``optional`` and nothing is found.
|
|
"""
|
|
# use extra TVM_HOME environment for finding libraries.
|
|
if os.environ.get("TVM_HOME", None):
|
|
tvm_source_home_dir = os.environ["TVM_HOME"]
|
|
else:
|
|
tvm_source_home_dir = os.fspath(libinfo._dev_top_directory())
|
|
|
|
dll_path = [
|
|
os.path.join(tvm_source_home_dir, "web", "dist", "wasm"),
|
|
os.path.join(tvm_source_home_dir, "web", "dist"),
|
|
]
|
|
found = [os.path.join(p, name) for p in dll_path if os.path.isfile(os.path.join(p, name))]
|
|
if not found:
|
|
if optional:
|
|
return None
|
|
raise RuntimeError(f"Cannot find {name}; searched {dll_path}")
|
|
return found
|
|
|
|
|
|
def create_tvmjs_wasm(output, objects, options=None, cc="emcc", libs=None):
|
|
"""Create wasm that is supposed to run with the tvmjs.
|
|
|
|
Parameters
|
|
----------
|
|
output : str
|
|
The target shared library.
|
|
|
|
objects : list
|
|
List of object files.
|
|
|
|
options : str
|
|
The additional options.
|
|
|
|
cc : str, optional
|
|
The compile string.
|
|
|
|
libs : list
|
|
List of user-defined library files (e.g. .bc files) to add into the wasm.
|
|
"""
|
|
cmd = [cc]
|
|
cmd += ["-O3"]
|
|
cmd += ["-std=c++17"]
|
|
cmd += ["--no-entry"]
|
|
# NOTE: asynctify conflicts with wasm-exception
|
|
# so we temp disable exception handling for now
|
|
#
|
|
# We also expect user to explicitly pass in
|
|
# -s ASYNCIFY=1 as it can increase wasm size by 2xq
|
|
#
|
|
# cmd += ["-s", "ASYNCIFY=1"]
|
|
# cmd += ["-fwasm-exceptions"]
|
|
cmd += ["-s", "WASM_BIGINT=1"]
|
|
cmd += ["-s", "ERROR_ON_UNDEFINED_SYMBOLS=0"]
|
|
cmd += ["-s", "STANDALONE_WASM=1"]
|
|
cmd += ["-s", "ALLOW_MEMORY_GROWTH=1"]
|
|
cmd += ["-s", "TOTAL_MEMORY=160MB"]
|
|
|
|
objects = [objects] if isinstance(objects, str) else objects
|
|
|
|
with_runtime = False
|
|
for obj in objects:
|
|
if obj.find("wasm_runtime.bc") != -1:
|
|
with_runtime = True
|
|
|
|
all_libs = []
|
|
if not with_runtime:
|
|
all_libs += [find_wasm_lib("wasm_runtime.bc")[0]]
|
|
|
|
all_libs += [find_wasm_lib("tvmjs_support.bc")[0]]
|
|
all_libs += [find_wasm_lib("webgpu_runtime.bc")[0]]
|
|
|
|
if libs:
|
|
if not isinstance(libs, list):
|
|
raise ValueError("Expect `libs` to be a list of paths in string.")
|
|
for lib in libs:
|
|
if not Path(lib).exists():
|
|
raise RuntimeError(
|
|
"Cannot find file from libs:" + lib + "\n Try pass in an absolute path."
|
|
)
|
|
all_libs += libs
|
|
|
|
cmd += ["-o", output]
|
|
|
|
# let libraries go before normal object
|
|
cmd += all_libs + objects
|
|
|
|
if options:
|
|
cmd += options
|
|
|
|
is_windows = os.name == "nt"
|
|
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=is_windows)
|
|
(out, _) = proc.communicate()
|
|
|
|
if proc.returncode != 0:
|
|
msg = "Compilation error:\n"
|
|
msg += out.decode("utf-8", errors="replace")
|
|
raise RuntimeError(msg)
|
|
|
|
|
|
create_tvmjs_wasm.object_format = "bc"
|