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:

libinfo()

Returns a dictionary of compile-time info — minimal Python fallback.

describe()

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.GetLibInfo global 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]

tvm.support.cc.create_shared(output, objects, options=None, cc=None, cwd=None, ccache_env=None)

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.

Parameters:
  • output (str) – The target shared library.

  • inputs (List[str]) – List of inputs files. Each input file can be a tarball of objects or an object file.

  • ar (Optional[str]) – Path to the ar command to be used

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

Parameters:
  • path (str) – The library path

  • nm (str) – The path to nm command

Returns:

symbol_section_map – A map from defined global symbol to their sections

Return type:

Dict[str, str]

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:

Callable[[str, str, Optional[str]], None]

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.

  • options (str or list of str) – The additional options.

  • 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:

bytearray

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:

str

tvm.support.nvcc.get_cuda_version(cuda_path=None)

Utility function to get CUDA version

Parameters:

cuda_path (Optional[str]) – Path to CUDA root. If None is passed, will use find_cuda_path() as default.

Returns:

version – The CUDA version

Return type:

float

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:

list of str

Note

This function will first search ld.lld that matches the major llvm version that built with tvm

Link relocatable ELF object to shared ELF object using lld

Parameters:
  • in_file (str) – Input file name (relocatable ELF object file)

  • out_file (str) – Output file name (shared ELF object file)

  • lld (str, optional) – The lld linker, if not specified, we will try to guess the matched clang version.

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

Parameters:

compute_version (str, optional) – compute capability of a GPU (e.g. “7.0”).

Returns:

have_matrixcore – True if MatrixCore support is provided, False otherwise

Return type:

bool

tvm.support.rocm.find_rocm_path()

Utility function to find ROCm path

Returns:

path – Path to ROCm root.

Return type:

str

tvm.support.ndk

Util to invoke NDK compiler toolchain.

tvm.support.ndk.create_shared(output, objects, options=None)

Create shared library.

Parameters:
  • output (str) – The target shared library.

  • objects (list) – List of object files.

  • options (list of str, optional) – The additional options.

tvm.support.ndk.create_staticlib(output, inputs)

Create static library:

Parameters:
  • output (str) – The target static library.

  • inputs (list) – List of object files or tar files

tvm.support.ndk.get_global_symbol_section_map(path, *, nm=None) dict[str, str]

Get global symbols from a library via nm -gU in NDK

Parameters:
  • path (str) – The library path

  • nm (str) – The path to nm command

Returns:

symbol_section_map – A map from defined global symbol to their sections

Return type:

Dict[str, str]

tvm.support.xcode

Utility to invoke Xcode compiler toolchain

tvm.support.xcode.xcrun(cmd)

Run xcrun and return the output.

Parameters:

cmd (list of str) – The command sequence.

Returns:

out – The output string.

Return type:

str

tvm.support.xcode.create_dylib(output, objects, arch, sdk='macosx', min_os_version=None)

Create dynamic library.

Parameters:
  • output (str) – The target shared library.

  • objects (list) – List of object files.

  • options (str) – The additional options.

  • arch (str) – Target major architectures

  • sdk (str) – The sdk to be used.

tvm.support.xcode.compile_metal(code, path_target=None, sdk='macosx', min_os_version=None)

Compile Metal with CLI tool from env.

Parameters:
  • code (str) – The Metal code.

  • path_target (str, optional) – Output file.

  • sdk (str, optional) – The target platform SDK.

Returns:

metallib – The bytearray of the metallib

Return type:

bytearray

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:

list of str

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:

str

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.

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.

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:
  • fn (function) – The function to be invoked.

  • args (list) – Positional argument.

  • kwargs (dict) – Keyword arguments

  • timeout (float) – Timeout value when executing the function

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:

object

Raises:
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:
  • fn (function) – The function to be invoked.

  • args (list) – Positional argument.

  • kwargs (dict) – Keyword arguments

Returns:

future – A future that can be used to access the result.

Return type:

concurrent.futures.Future

map_with_error_catching(fn, iterator)

Same as map, but catches exceptions and return them instead.

Parameters:
  • fn (function) – The function to be invoked.

  • iterator (Iterator) – Input iterator.

Returns:

out_iter – The result iterator.

Return type:

Iterator[MapResult]

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

Parameters:

name (str) – The name of the file.

Returns:

path – The concatenated path.

Return type:

str

listdir()

List contents in the dir.

Returns:

names – The content of directory

Return type:

list

tvm.support.utils.tempdir(custom_path=None, keep_for_debug=None)

Create temp dir which deletes the contents when exit.

Parameters:
  • custom_path (str, optional) – Manually specify the exact temp dir path

  • keep_for_debug (bool) – Keep temp directory for debugging purposes

Returns:

temp – The temp directory object

Return type:

TempDirectory

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.

Parameters:

path (str) – A possible path

Returns:

valid – Whether path is a possible source path

Return type:

bool

tvm.support.utils.which(exec_name)

Try to find full path of exec_name

Parameters:

exec_name (str) – The executable name

Returns:

path – The full path of executable if found, otherwise returns None

Return type:

str

tvm.support.tar

Util to invoke tarball in the system.

tvm.support.tar.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.

tvm.support.tar.untar(tar_file, directory)

Unpack all tar files into the directory

Parameters:
  • tar_file (str) – The source tar file.

  • directory (str) – The target 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:
Returns:

ret_list – An updated list of files

Return type:

List[str]