tvm.target

Target description and codgen module.

TVM’s target string is in format <target_kind> [-option=value]....

Note

The list of options include:

  • -device=<device name>

    The device name.

  • -mtriple=<target triple>

    Specify the target triple, which is useful for cross compilation.

  • -mcpu=<cpuname>

    Specify a specific chip in the current architecture to generate code for. By default this is infered from the target triple and autodetected to the current architecture.

  • -mattr=a1,+a2,-a3,…

    Override or control specific attributes of the target, such as whether SIMD operations are enabled or not. The default set of attributes is set by the current CPU.

  • -mabi=<abi>

    Generate code for the specified ABI, for example “lp64d”.

  • -system-lib

    Build TVM system library module. System lib is a global module that contains self registered functions in program startup. User can get the module using tvm.runtime.system_lib. It is useful in environments where dynamic loading api like dlopen is banned. The system lib will be available as long as the result code is linked by the program.

We can use tvm.target.Target() to create a tvm.target.Target from the target string. We can also use other specific function in this module to create specific targets.

Classes:

Target(target[, host])

Target device information, use through TVM API.

TargetKind

Kind of a compilation target

VirtualDevice([device, target, memory_scope])

A compile time representation for where data is to be stored at runtime, and how to compile code to compute it.

GenericFunc

GenericFunc node reference.

Functions:

create(target)

Deprecated.

cuda([model, arch, options])

Returns a cuda target.

rocm([model, options])

Returns a ROCM target.

mali([model, options])

Returns a ARM Mali GPU target.

intel_graphics([model, options])

Returns an Intel Graphics target.

arm_cpu([model, options])

Returns a ARM CPU target.

rasp([options])

Return a Raspberry 3b target.

bifrost([model, options])

Return an ARM Mali GPU target (Bifrost architecture).

riscv_cpu([model, options])

Returns a RISC-V CPU target.

hexagon([cpu_ver])

Returns a Hexagon target.

stm32([series, options])

Returns a STM32 target.

make_compilation_config(ctxt, target[, ...])

Returns a CompilationConfig appropriate for target and target_host, using the same representation conventions as for the standard build interfaces.

list_tags()

Returns a dict of tags, which maps each tag name to its corresponding target.

generic_func(fdefault)

Wrap a target generic function.

get_native_generic_func(name)

Get a generic function from the global registry.

override_native_generic_func(func_name)

Override a generic function defined in C++

class tvm.target.Target(target, host=None)

Target device information, use through TVM API.

Note

You can create target using the constructor or the following functions

Methods:

from_device(device)

Detects Target associated with the given device.

current([allow_none])

Returns the current target.

get_kind_attr(attr_name)

Get additional attribute about the target kind.

get_target_device_type()

Returns the device_type for this target.

list_kinds()

Returns the list of available target names.

canon_target(target)

Given a single target-like object, returns the TVM Target object representing it.

canon_target_and_host(target[, target_host])

Returns a TVM Target capturing target and target_host.

canon_multi_target(multi_targets)

Given a single target-like object, or a collection-like object of target-like objects, returns a TVM Array of TVM Target objects representing then.

canon_multi_target_and_host(target[, ...])

Returns a TVM Array<Target> capturing target and target_host.

canon_target_map_and_host(target_map[, ...])

Returns target_map as a map from TVM Target's in canonical form to IRModules.

target_or_current(target)

Returns target, or the current target in the environment if target is None

Attributes:

arch

Returns the cuda arch from the target if it exists.

max_num_threads

Returns the max_num_threads from the target if it exists.

max_block_size_x

Returns the max block size in x-dimension from the target if it exists.

max_block_size_y

Returns the max block size in y-dimension from the target if it exists.

thread_warp_size

Returns the thread_warp_size from the target if it exists.

model

Returns model from the target if it exists.

mcpu

Returns the mcpu from the target if it exists.

mattr

Returns the mattr from the target if it exists.

static from_device(device: str | Device) Target

Detects Target associated with the given device. If the device does not exist, there will be an Error.

Parameters:

dev (Union[str, Device]) – The device to detect the target for. Supported device types: [“cuda”, “metal”, “rocm”, “vulkan”, “opencl”, “cpu”]

Returns:

target – The detected target.

Return type:

Target

static current(allow_none=True)

Returns the current target.

Parameters:

allow_none (bool) – Whether allow the current target to be none

Raises:

ValueError if current target is not set.

property arch

Returns the cuda arch from the target if it exists.

property max_num_threads

Returns the max_num_threads from the target if it exists.

property max_block_size_x

Returns the max block size in x-dimension from the target if it exists.

property max_block_size_y

Returns the max block size in y-dimension from the target if it exists.

property thread_warp_size

Returns the thread_warp_size from the target if it exists.

property model

Returns model from the target if it exists.

property mcpu

Returns the mcpu from the target if it exists.

property mattr

Returns the mattr from the target if it exists.

get_kind_attr(attr_name)

Get additional attribute about the target kind.

Parameters:

attr_name (str) – The attribute name.

Returns:

value – The attribute value

Return type:

object

get_target_device_type()

Returns the device_type for this target.

static list_kinds()

Returns the list of available target names.

static canon_target(target)

Given a single target-like object, returns the TVM Target object representing it. Can convert from: - None (to None). - An existing TVM Target object. - A string, eg “cuda” or “cuda -arch=sm_80” - A Python dictionary, eg {“kind”: “cuda”, “arch”: “sm_80” }

static canon_target_and_host(target, target_host=None)

Returns a TVM Target capturing target and target_host. Also returns the host in canonical form. The given target can be in any form recognized by Target.canon_target. If given, target_host can be in any form recognized by Target.canon_target. If target_host is given it will be set as the ‘host’ in the result Target object (and a warning given).

Note that this method does not support heterogeneous compilation targets.

static canon_multi_target(multi_targets)

Given a single target-like object, or a collection-like object of target-like objects, returns a TVM Array of TVM Target objects representing then. Can convert from: - None (to None). - A single target-like object in a form recognized by canon_target. - A Python list or TVM Array of target-like objects in a form recognized by canon_target. - A Python dict or TVM Map from TVM IntImm objects representing device types to a target-like object in a form recognized by canon_target. (This is a legacy method to represent heterogeneous targets. The keys are ignored.)

static canon_multi_target_and_host(target, target_host=None)

Returns a TVM Array<Target> capturing target and target_host. The given target can be in any form recognized by Target.canon_multi_target. If given, target_host can be in any form recognized by Target.canon_target. If target_host is given it will be set as the ‘host’ in each result Target object (and a warning given).

static canon_target_map_and_host(target_map, target_host=None)

Returns target_map as a map from TVM Target’s in canonical form to IRModules. The keys of the input target_map can be in any form recognized by Target.canon_target. Similarly, if given, target_host can be in any form recognized by Target.canon_target. The final target_map keys will capture the target_host in canonical form. Also returns the target_host in canonical form.

static target_or_current(target)

Returns target, or the current target in the environment if target is None

tvm.target.create(target)

Deprecated. Use the constructor of tvm.target.Target directly.

class tvm.target.TargetKind

Kind of a compilation target

Attributes:

options

Returns the dict of available option names and types

Methods:

options_from_name(kind_name)

Returns the dict of available option names and types from a name of TargetKind

property options

Returns the dict of available option names and types

static options_from_name(kind_name: str)

Returns the dict of available option names and types from a name of TargetKind

tvm.target.cuda(model='unknown', arch=None, options=None)

Returns a cuda target.

Parameters:
  • model (str) – The model of cuda device (e.g. 1080ti)

  • arch (str) – The cuda architecture (e.g. sm_61)

  • options (str or list of str) – Additional options

tvm.target.rocm(model='unknown', options=None)

Returns a ROCM target.

Parameters:
  • model (str) – The model of this device

  • options (str or list of str) – Additional options

tvm.target.mali(model='unknown', options=None)

Returns a ARM Mali GPU target.

Parameters:
  • model (str) – The model of this device

  • options (str or list of str) – Additional options

tvm.target.intel_graphics(model='unknown', options=None)

Returns an Intel Graphics target.

Parameters:
  • model (str) – The model of this device

  • options (str or list of str) – Additional options

tvm.target.arm_cpu(model='unknown', options=None)

Returns a ARM CPU target. This function will also download pre-tuned op parameters when there is none.

Parameters:
  • model (str) – SoC name or phone name of the arm board.

  • options (str or list of str) – Additional options

tvm.target.rasp(options=None)

Return a Raspberry 3b target.

Parameters:

options (str or list of str) – Additional options

tvm.target.bifrost(model='unknown', options=None)

Return an ARM Mali GPU target (Bifrost architecture).

Parameters:

options (str or list of str) – Additional options

tvm.target.riscv_cpu(model='sifive-u54', options=None)

Returns a RISC-V CPU target. Default: sifive-u54 rv64gc

Parameters:
  • model (str) – CPU name.

  • options (str or list of str) – Additional options

tvm.target.hexagon(cpu_ver='v68', **kwargs)

Returns a Hexagon target.

Parameters:
  • cpu_ver (str (default: "v68")) – CPU version used for code generation. Not all allowed cpu str will be valid, LLVM will throw an error.

  • parameters (Recognized keyword)

  • -----------------------------

  • hvx (int (default: 128)) – Size of HVX vector in bytes. Value of 0 disables HVX codegen.

  • llvm_options (str or list of str (default: None)) – User defined compiler arguments.

  • use_qfloat (bool (default: True for cpu_ver >= v68, False otherwise)) – Whether to use QFloat HVX instructions.

  • use_ieee_fp (bool (default: False)) – Whether to use IEEE HVX instructions

  • num_cores (int (default: 4)) – The number of HVX threads. This attribute is required by meta scheduler.

  • vtcm_capacity (int (default: 0)) – Hexagon VTCM capacity limitation. If the value is 0, the capacity is treated as unbounded.

  • Note (Floating point support in HVX requires LLVM 14+.)

tvm.target.stm32(series='unknown', options=None)

Returns a STM32 target.

Parameters:
  • series (str) – Series name of a STM32 board series, eg. stm32H7xx or stm32F4xx

  • options (str or list of str) – Additional options

class tvm.target.VirtualDevice(device=None, target=None, memory_scope='')

A compile time representation for where data is to be stored at runtime, and how to compile code to compute it.

tvm.target.make_compilation_config(ctxt, target, target_host=None)

Returns a CompilationConfig appropriate for target and target_host, using the same representation conventions as for the standard build interfaces. Intended only for unit testing.

tvm.target.list_tags() Dict[str, Target] | None

Returns a dict of tags, which maps each tag name to its corresponding target.

Returns:

tag_dict – The dict of tags mapping each tag name to its corresponding target. None if TVM is built in runtime-only mode.

Return type:

Optional[Dict[str, Target]]

class tvm.target.GenericFunc

GenericFunc node reference. This represents a generic function that may be specialized for different targets. When this object is called, a specialization is chosen based on the current target.

Note

Do not construct an instance of this object, it should only ever be used as a return value from calling into C++.

Methods:

set_default(func[, allow_override])

Set the default function to be used if no specializations match the current target.

register(func, key_list[, allow_override])

Register a specialization for this GenericFunc.

get_packed_func()

Get the packed function specified for the current target.

set_default(func, allow_override=False)

Set the default function to be used if no specializations match the current target.

Parameters:
  • func (function) – The default function

  • allow_override (bool) – Whether to allow the current default to be overridden

register(func, key_list, allow_override=False)

Register a specialization for this GenericFunc.

Parameters:
  • func (function) – The function to be registered.

  • key (str or list of str) – The key to be registered.

  • allow_override (bool, optional) – Whether to allow existing keys to be overridden.

get_packed_func()

Get the packed function specified for the current target.

Returns:

func – The function specified for the current target. Return the default function if no specializations match the current target.

Return type:

PackedFunc

tvm.target.generic_func(fdefault)

Wrap a target generic function.

Generic function allows registration of further functions that can be dispatched on current target context. If no registered dispatch is matched, the fdefault will be called.

Parameters:

fdefault (function) – The default function.

Returns:

fgeneric – A wrapped generic function.

Return type:

function

Example

import tvm
# wrap function as target generic
@tvm.target.generic_func
def my_func(a):
    return a + 1
# register specialization of my_func under target cuda
@my_func.register("cuda")
def my_func_cuda(a):
    return a + 2
# displays 3, because my_func is called
print(my_func(2))
# displays 4, because my_func_cuda is called
with tvm.target.cuda():
    print(my_func(2))
tvm.target.get_native_generic_func(name)

Get a generic function from the global registry. If no function is registered under the given name, a new generic function is created.

Parameters:

name (string) – The name of the generic function to get

Returns:

func – The generic function for the given name

Return type:

GenericFunc

tvm.target.override_native_generic_func(func_name)

Override a generic function defined in C++

Generic function allows registration of further functions that can be dispatched on current target context. If no registered dispatch is matched, the fdefault will be called.

Parameters:

func_name (string) – The name of the generic func to be overridden

Returns:

fgeneric – A wrapped generic function.

Return type:

function

Example

import tvm
# wrap function as target generic
@tvm.target.override_native_generic_func("my_func")
def my_func(a):
    return a + 1
# register specialization of my_func under target cuda
@my_func.register("cuda")
def my_func_cuda(a):
    return a + 2
# displays 3, because my_func is called
print(my_func(2))
# displays 4, because my_func_cuda is called
with tvm.target.cuda():
    print(my_func(2))