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:

GenericFunc()

GenericFunc node reference.

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.

Functions:

arm_cpu([model, options])

Returns a ARM CPU target.

bifrost([model, options])

Return an ARM Mali GPU target (Bifrost architecture).

create(target)

Deprecated.

cuda([model, arch, options])

Returns a cuda target.

generic_func(fdefault)

Wrap a target generic function.

get_native_generic_func(name)

Get a generic function from the global registry.

hexagon([cpu_ver])

Returns a Hexagon target.

intel_graphics([model, options])

Returns an Intel Graphics target.

list_tags()

Returns a dict of tags, which maps each tag name to its corresponding 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.

mali([model, options])

Returns a ARM Mali GPU target.

override_native_generic_func(func_name)

Override a generic function defined in C++

rasp([options])

Return a Raspberry 3b target.

riscv_cpu([model, options])

Returns a RISC-V CPU target.

rocm([model, options])

Returns a ROCM target.

stm32([series, options])

Returns a STM32 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:

get_packed_func()

Get the packed function specified for the current target.

register(func, key_list[, allow_override])

Register a specialization for this GenericFunc.

set_default(func[, allow_override])

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

get_packed_func()[源代码]#

Get the packed function specified for the current target.

Returns#

funcPackedFunc

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

register(func, key_list, allow_override=False)[源代码]#

Register a specialization for this GenericFunc.

Parameters#

funcfunction

The function to be registered.

keystr or list of str

The key to be registered.

allow_overridebool, optional

Whether to allow existing keys to be overridden.

set_default(func, allow_override=False)[源代码]#

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

Parameters#

funcfunction

The default function

allow_overridebool

Whether to allow the current default to be overridden

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:

__init__(target[, host])

Construct a TVM target object from 1) Raw target string 2) Target config dict 3) Target tag

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(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_target_map_and_host(target_map[, ...])

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

current([allow_none])

Returns the current target.

from_device(device)

Detects Target associated with the given device.

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.

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.

mattr

Returns the mattr 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.

max_num_threads

Returns the max_num_threads from the target if it exists.

mcpu

Returns the mcpu from the target if it exists.

model

Returns model from the target if it exists.

thread_warp_size

Returns the thread_warp_size from the target if it exists.

__init__(target, host=None)[源代码]#

Construct a TVM target object from 1) Raw target string 2) Target config dict 3) Target tag

Parameters#

targetUnion[str, Dict[str, Any]]

Can be one of a literal target string, a json string describing a configuration, or a dictionary of configuration options. When using a dictionary or json string to configure target, the possible values are:

kindstr (required)

Which codegen path to use, for example ‘llvm’ or ‘cuda’.

keysList of str (optional)

A set of strategies that can be dispatched to. When using “kind=opencl” for example, one could set keys to [“mali”, “opencl”, “gpu”].

devicestr (optional)

A single key that corresponds to the actual device being run on. This will be effectively appended to the keys.

libsList of str (optional)

The set of external libraries to use. For example [‘cblas’, ‘mkl’].

system-libbool (optional)

If True, build a module that contains self registered functions. Useful for environments where dynamic loading like dlopen is banned.

mcpustr (optional)

The specific cpu being run on. Serves only as an annotation.

modelstr (optional)

An annotation indicating what model a workload came from.

runtimestr (optional)

An annotation indicating which runtime to use with a workload.

mtriplestr (optional)

The llvm triplet describing the target, for example “arm64-linux-android”.

mattrList of str (optional)

The llvm features to compile with, for example [“+avx512f”, “+mmx”].

mfloat-abistr (optional)

An llvm setting that is one of ‘hard’ or ‘soft’ indicating whether to use hardware or software floating-point operations.

mabistr (optional)

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

hostUnion[str, Dict[str, Any]] (optional)

Description for target host. Can be recursive. Similar to target.

hostOptional[Union[str, Dict[str, Any]]]

Similar to target but for target host. Can be one of a literal target host string, a json string describing a configuration, or a dictionary of configuration options. When using a dictionary or json string to configure target, the possible values are same as target.

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(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_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 current(allow_none=True)[源代码]#

Returns the current target.

Parameters#

allow_nonebool

Whether allow the current target to be none

Raises#

ValueError if current target is not set.

static from_device(device)[源代码]#

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

Parameters#

devUnion[str, Device]

The device to detect the target for. Supported device types: [“cuda”, “metal”, “rocm”, “vulkan”, “opencl”, “cpu”]

Returns#

targetTarget

The detected target.

参数:

device (str | Device)

返回类型:

Target

get_kind_attr(attr_name)[源代码]#

Get additional attribute about the target kind.

Parameters#

attr_namestr

The attribute name.

Returns#

valueobject

The attribute value

get_target_device_type()[源代码]#

Returns the device_type for this target.

static list_kinds()[源代码]#

Returns the list of available target names.

static target_or_current(target)[源代码]#

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

property arch#

Returns the cuda arch from the target if it exists.

property mattr#

Returns the mattr 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 max_num_threads#

Returns the max_num_threads from the target if it exists.

property mcpu#

Returns the mcpu from the target if it exists.

property model#

Returns model from the target if it exists.

property thread_warp_size#

Returns the thread_warp_size from the target if it exists.

class tvm.target.TargetKind[源代码]#

Kind of a compilation target

Methods:

options_from_name(kind_name)

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

Attributes:

options

Returns the dict of available option names and types

static options_from_name(kind_name)[源代码]#

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

参数:

kind_name (str)

property options#

Returns the dict of available option names and types

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.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.

optionsstr or list of str

Additional options

tvm.target.bifrost(model='unknown', options=None)[源代码]#

Return an ARM Mali GPU target (Bifrost architecture).

Parameters#

optionsstr or list of str

Additional options

tvm.target.create(target)[源代码]#

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

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)

optionsstr or list of str

Additional options

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#

fdefaultfunction

The default function.

Returns#

fgenericfunction

A wrapped generic 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#

namestring

The name of the generic function to get

Returns#

funcGenericFunc

The generic function for the given name

tvm.target.hexagon(cpu_ver='v68', **kwargs)[源代码]#

Returns a Hexagon target.

Parameters#

cpu_verstr (default: “v68”)

CPU version used for code generation. Not all allowed cpu str will be valid, LLVM will throw an error.

Recognized keyword parameters#

hvxint (default: 128)

Size of HVX vector in bytes. Value of 0 disables HVX codegen.

llvm_optionsstr or list of str (default: None)

User defined compiler arguments.

use_qfloatbool (default: True for cpu_ver >= v68, False otherwise)

Whether to use QFloat HVX instructions.

use_ieee_fpbool (default: False)

Whether to use IEEE HVX instructions

num_coresint (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.intel_graphics(model='unknown', options=None)[源代码]#

Returns an Intel Graphics target.

Parameters#

model: str

The model of this device

optionsstr or list of str

Additional options

tvm.target.list_tags()[源代码]#

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

Returns#

tag_dictOptional[Dict[str, Target]]

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

返回类型:

Dict[str, Target] | None

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.mali(model='unknown', options=None)[源代码]#

Returns a ARM Mali GPU target.

Parameters#

model: str

The model of this device

optionsstr or list of str

Additional options

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_namestring

The name of the generic func to be overridden

Returns#

fgenericfunction

A wrapped generic 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))
tvm.target.rasp(options=None)[源代码]#

Return a Raspberry 3b target.

Parameters#

optionsstr 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.

optionsstr 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

optionsstr or list of str

Additional options

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

optionsstr or list of str

Additional options