tvm.target

Target description and codgen module.

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

Note

The list of options include:

  • -device=<device name>

    The device name.

  • -mtriple=<target triple> or -target

    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.

  • -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.create() 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 device information, use through TVM API.

Functions

arm_cpu([model, options])

Returns a ARM CPU target.

bifrost([model, options])

Return an ARM Mali GPU target (Bifrost architecture).

create(target_str)

Get a target given target string.

cuda([model, 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, sim_args, hvx])

Returns a Hexagon target.

intel_graphics([model, options])

Returns an Intel Graphics target.

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.

register_intrin_rule(target, intrin[, f, …])

Register an intrinsic function generation rule.

rocm([model, options])

Returns a ROCM target.

class tvm.target.Target

Target device information, use through TVM API.

Note

Do not use class constructor, you can create target using the following functions

Methods

current([allow_none])

Returns the current target.

Attributes

mcpu

Returns the mcpu from the target if it exists.

property mcpu

Returns the mcpu from the target if it exists.

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.

tvm.target.create(target_str)

Get a target given target string.

Parameters

target_str (str) – The target string.

Returns

target – The target object

Return type

Target

Note

See the note on tvm.target on target string format.

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

Returns a cuda target.

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

  • 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.hexagon(cpu_ver='v66', sim_args=None, hvx=128)

Returns a Hexagon target.

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

  • sim_args (str or list of str) – User defined sim arguments. CPU version defaults to cpu_ver. Otherwise, separate versions are used for codegen and sim. Not all allowed cpu strings will be valid, simulator will throw an error if invalid. Does not affect codegen.

  • hvx (int) – Size of hvx register. Value of 0 indicates disabled hvx.

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

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.

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.

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))
tvm.target.register_intrin_rule(target, intrin, f=None, override=False)

Register an intrinsic function generation rule.

Intrinsic generation rules are callback functions for code generator to get device specific calls. This function simply translates to.

register_func("tvm.intrin.rule.%s.%s" % (target, intrin), f, override)

TVM may already pre-register intrinsic rules in the backend. However, user can use this function to change the intrinsic translation behavior or add new intrinsic rules during runtime.

Parameters
  • target (str) – The name of codegen target.

  • intrin (str) – The name of the intrinsic.

  • f (function, optional) – The function to be registered.

  • override (boolean optional) – Whether override existing entry.

Returns

fregister – Register function if f is not specified.

Return type

function

Examples

The following code registers exp expansion rule for opencl.

register_intrin_rule("opencl", "exp", my_exp_rule, override=True)