tvm.support
tvm.support — Python helpers that integrate TVM with external CLIs and host-side tools (compilers, archivers, subprocess pools, build-info queries). Distinct from tvm.contrib, which is reserved for optional vendor SDK integrations and experimental features.
Functions:
|
Returns a dictionary of compile-time info — minimal Python fallback. |
|
Print out information about TVM and the current Python environment |
- tvm.support.libinfo()
Returns a dictionary of compile-time info — minimal Python fallback.
The native
support.GetLibInfoglobal function is no longer registered after the upstream sync, so we synthesize the values from build-time hints instead.
- tvm.support.describe()
Print out information about TVM and the current Python environment
tvm.support.cc
Util to invoke C/C++ compilers in the system.
- tvm.support.cc.get_cc()
Return the path to the default C/C++ compiler.
- Returns:
out – The path to the default C/C++ compiler, or None if none was found.
- Return type:
Optional[str]
Create shared library.
- Parameters:
output (str) – The target shared library.
objects (List[str]) – List of object files.
options (List[str]) – The list of additional options string.
cc (Optional[str]) – The compiler command.
cwd (Optional[str]) – The current working directory.
ccache_env (Optional[Dict[str, str]]) – The environment variable for ccache. Set None to disable ccache by default.
- tvm.support.cc.create_staticlib(output, inputs, ar=None)
Create static library.
- tvm.support.cc.create_executable(output, objects, options=None, cc=None, cwd=None, ccache_env=None)
Create executable binary.
- Parameters:
output (str) – The target executable.
objects (List[str]) – List of object files.
options (List[str]) – The list of additional options string.
cc (Optional[str]) – The compiler command.
cwd (Optional[str]) – The urrent working directory.
ccache_env (Optional[Dict[str, str]]) – The environment variable for ccache. Set None to disable ccache by default.
- tvm.support.cc.get_global_symbol_section_map(path, *, nm=None) dict[str, str]
Get global symbols from a library via nm -g
- tvm.support.cc.get_target_by_dump_machine(compiler)
Functor of get_target_triple that can get the target triple using compiler.
- Parameters:
compiler (Optional[str]) – The compiler.
- Returns:
out – A function that can get target triple according to dumpmachine option of compiler.
- Return type:
Callable
- tvm.support.cc.cross_compiler(compile_func, options=None, output_format=None, get_target_triple=None, add_files=None)
Create a cross compiler function by specializing compile_func with options.
This function can be used to construct compile functions that can be passed to AutoTVM measure or export_library.
- Parameters:
compile_func (Union[str, Callable[[str, str, Optional[str]], None]]) – Function that performs the actual compilation
options (Optional[List[str]]) – List of additional optional string.
output_format (Optional[str]) – Library output format.
get_target_triple (Optional[Callable]) – Function that can target triple according to dumpmachine option of compiler.
add_files (Optional[List[str]]) – List of paths to additional object, source, library files to pass as part of the compilation.
- Returns:
fcompile – A compilation function that can be passed to export_library.
- Return type:
Examples
from tvm.support import cc, ndk # export using arm gcc mod = build_runtime_module() mod.export_library(path_dso, fcompile=cc.cross_compiler("arm-linux-gnueabihf-gcc")) # specialize ndk compilation options. specialized_ndk = cc.cross_compiler( ndk.create_shared, ["--sysroot=/path/to/sysroot", "-shared", "-fPIC", "-lm"]) mod.export_library(path_dso, fcompile=specialized_ndk)
tvm.support.nvcc
Utility to invoke nvcc compiler in the system
- tvm.support.nvcc.compile_cuda(code, target_format=None, arch=None, options=None, path_target=None, compiler='nvcc')
Compile CUDA code with NVCC or NVRTC.
- Parameters:
code (str) – The CUDA code.
target_format (str) – The target format of the compiler (“ptx”, “cubin”, or “fatbin”).
arch (str) – The CUDA architecture.
path_target (str, optional) – Output file.
compiler (str, optional) – Compiler backend: “nvcc” or “nvrtc”. This can be set by the TVM_CUDA_COMPILE_MODE environment variable.
- Returns:
res_binary – The bytearray of the compiled binary (ptx/cubin/fatbin).
- Return type:
Notes
NVRTC is a “runtime” compilation library and can be faster for JIT compilation.
NVRTC requires cuda-python: pip install cuda-python
- tvm.support.nvcc.find_cuda_path()
Utility function to find CUDA path
- Returns:
path – Path to CUDA root.
- Return type:
- tvm.support.nvcc.get_cuda_version(cuda_path=None)
Utility function to get CUDA version
- tvm.support.nvcc.find_nvshmem_paths() tuple[str, str]
Searches for the NVSHMEM include and library directories.
- Return type:
A tuple containing the path to the include directory and the library directory.
- tvm.support.nvcc.parse_compute_version(compute_version)
Parse compute capability string to divide major and minor version
- Parameters:
compute_version (str) – compute capability of a GPU (e.g. “6.0”)
- Returns:
major (int) – major version number
minor (int) – minor version number
- tvm.support.nvcc.have_fp16(compute_version)
Either fp16 support is provided in the compute capability or not
- Parameters:
compute_version (str) – compute capability of a GPU (e.g. “6.0”)
- tvm.support.nvcc.have_int8(compute_version)
Either int8 support is provided in the compute capability or not
- Parameters:
compute_version (str) – compute capability of a GPU (e.g. “6.1”)
- tvm.support.nvcc.have_tensorcore(compute_version=None, target=None)
Either TensorCore support is provided in the compute capability or not
- Parameters:
compute_version (str, optional) – compute capability of a GPU (e.g. “7.0”).
target (tvm.target.Target, optional) – The compilation target, will be used to determine arch if compute_version isn’t specified.
- tvm.support.nvcc.have_cudagraph()
Either CUDA Graph support is provided
tvm.support.rocm
Utility for ROCm backend
- tvm.support.rocm.find_lld(required=True)
Find ld.lld in system.
- Parameters:
required (bool) – Whether it is required, runtime error will be raised if the compiler is required.
- Returns:
valid_list – List of possible paths.
- Return type:
Note
This function will first search ld.lld that matches the major llvm version that built with tvm
- tvm.support.rocm.rocm_link(in_file, out_file, lld=None)
Link relocatable ELF object to shared ELF object using lld
- tvm.support.rocm.parse_compute_version(compute_version)
Parse compute capability string to divide major and minor version
- Parameters:
compute_version (str) – compute capability of a GPU (e.g. “6.0”)
- Returns:
major (int) – major version number
minor (int) – minor version number
- tvm.support.rocm.have_matrixcore(compute_version=None)
Either MatrixCore support is provided in the compute capability or not
tvm.support.ndk
Util to invoke NDK compiler toolchain.
Create shared library.
- tvm.support.ndk.create_staticlib(output, inputs)
Create static library:
tvm.support.xcode
Utility to invoke Xcode compiler toolchain
- tvm.support.xcode.xcrun(cmd)
Run xcrun and return the output.
- tvm.support.xcode.create_dylib(output, objects, arch, sdk='macosx', min_os_version=None)
Create dynamic library.
- tvm.support.xcode.compile_metal(code, path_target=None, sdk='macosx', min_os_version=None)
Compile Metal with CLI tool from env.
- tvm.support.xcode.compile_coreml(model, model_name='main', out_dir='.')
Compile coreml model and return the compiled model path.
tvm.support.clang
Util to invoke clang in the system.
- tvm.support.clang.find_clang(required=True)
Find clang in system.
- Parameters:
required (bool) – Whether it is required, runtime error will be raised if the compiler is required.
- Returns:
valid_list – List of possible paths.
- Return type:
Note
This function will first search clang that matches the major llvm version that built with tvm
- tvm.support.clang.create_llvm(inputs, output=None, options=None, cc=None)
Create llvm text ir.
- Parameters:
inputs (list of str) – List of input files name or code source.
output (str, optional) – Output file, if it is none a temporary file is created
options (list) – The list of additional options string.
cc (str, optional) – The clang compiler, if not specified, we will try to guess the matched clang version.
- Returns:
code – The generated llvm text IR.
- Return type:
tvm.support.emcc
Util to invoke emscripten compilers in the system.
- tvm.support.emcc.create_tvmjs_wasm(output, objects, options=None, cc='emcc', libs=None)
Create wasm that is supposed to run with the tvmjs.
tvm.support.popen_pool
Multiprocessing via Popen.
This module provides a multi-processing pool backed by Popen. with additional timeout support.
- tvm.support.popen_pool.kill_child_processes(pid)
Kill all child processes recursively for a given pid.
- Parameters:
pid (int) – The given parameter id.
- class tvm.support.popen_pool.StatusKind(value)
Running and return value status.
- class tvm.support.popen_pool.MapResult(status, value)
Result of map_with_error_catching.
- Parameters:
status (StatusKind) – The status of the result.
value (Any) – The result value.
- class tvm.support.popen_pool.PopenWorker(initializer=None, initargs=(), maximum_uses=None, stdout=None, stderr=None)
A subprocess worker via Popen.
PopenWorker provides a low-level API to interact with a separate process via Popen.
- Parameters:
initializer (callable or None) – A callable initializer, or None
initargs (Tuple[object]) – A tuple of args for the initializer
maximum_uses (Optional[int]) – The maximum number of times a process can be used before being recycled, i.e. killed and restarted. If None, the process will be reused until an operation times out.
stdout (Union[None, int, IO[Any]]) – The standard output streams handler specified for the popen process.
stderr (Union[None, int, IO[Any]]) – The standard error streams handler specified for the popen process.
- kill()
Kill the current running process and cleanup.
Note
The worker can start a new process when send is called again.
- join(timeout=None)
Join the current process worker before it terminates.
- Parameters:
timeout (Optional[number]) – Timeout value, block at most timeout seconds if it is a positive number.
- is_alive()
Check if the process is alive
- send(fn, args=(), kwargs=None, timeout=None)
Send a new function task
fn(*args, **kwargs)to the subprocess.- Parameters:
Note
The caller must call recv before calling the next send in order to make sure the timeout and child process exit won’t affect the later requests.
- recv()
Receive the result of the last send.
- Returns:
result – The result of the last send.
- Return type:
- Raises:
ChildProcessError – if the child process exited abnormally.:
TimeoutError – if timeout happens:
Exception – if other exception happens during the execution.:
- class tvm.support.popen_pool.PopenPoolExecutor(max_workers=None, timeout=None, initializer=None, initargs=(), maximum_process_uses=None, stdout=None, stderr=None)
An parallel executor backed by Popen processes.
- Parameters:
max_worker (int) – Maximum number of workers
timeout (float) – Timeout value for each function submit.
initializer (callable or None) – A callable initializer, or None
initargs (Tuple[object]) – A tuple of args for the initializer
maximum_process_uses (Optional[int]) – The maximum number of times each process can be used before being recycled, i.e. killed and restarted. If None, processes will be reused until an operation times out.
stdout (Union[None, int, IO[Any]]) – The standard output streams handler specified for the workers in the pool.
stderr (Union[None, int, IO[Any]]) – The standard error streams handler specified for the workers in the pool.
Note
If max_workers is NONE then the number returned by os.cpu_count() is used. This method aligns with the behavior of multiprocessing.pool().
- shutdown(wait=True)
Shutdown the executor and clean up resources.
- Parameters:
wait (bool) – If True, wait for pending work to complete.
Note
DEADLOCK WARNING: This method can deadlock when called during garbage collection due to exception reference cycles. When exceptions occur, Python creates reference cycles that delay garbage collection. The deadlock happens when: exception creates reference cycle → new pool creates worker → GC cleans old pool → old pool’s __del__ calls shutdown() which tries to acquire locks again.
- submit(fn, *args, **kwargs) Future
Submit a new function job to the pool
- Parameters:
- Returns:
future – A future that can be used to access the result.
- Return type:
tvm.support.utils
Common system utilities
- exception tvm.support.utils.DirectoryCreatedPastAtExit
Raised when a TempDirectory is created after the atexit hook runs.
- class tvm.support.utils.TempDirectory(custom_path=None, keep_for_debug=None)
Helper object to manage temp directory during testing.
Automatically removes the directory when it went out of scope.
- classmethod set_keep_for_debug(set_to=True)
Keep temporary directories past program exit for debugging.
- remove()
Remove the tmp dir
- relpath(name)
Relative path in temp dir
- tvm.support.utils.tempdir(custom_path=None, keep_for_debug=None)
Create temp dir which deletes the contents when exit.
- Parameters:
- Returns:
temp – The temp directory object
- Return type:
- class tvm.support.utils.FileLock(path)
File lock object
- Parameters:
path (str) – The path to the lock
- release()
Release the lock
- tvm.support.utils.filelock(path)
Create a file lock which locks on path
- Parameters:
path (str) – The path to the lock
- Returns:
lock
- Return type:
File lock object
- tvm.support.utils.is_source_path(path)
Check if path is source code path.
tvm.support.tar
Util to invoke tarball in the system.
- tvm.support.tar.tar(output, files)
Create tarball containing all files in root.
- tvm.support.tar.untar(tar_file, directory)
Unpack all tar files into the directory
- tvm.support.tar.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 – An updated list of files
- Return type:
List[str]