tvm.relay.analysis

The Relay IR namespace containing the analysis passes.

Classes

AnnotatedRegionSet(expr, region_begin_op, …)

Class to represent a relay expression split into regions.

CallGraph(module)

Class to represent a call graph.

Feature

The features a program might contain.

Functions

all_type_vars(expr[, mod])

Get all type variables from expression/type e

all_vars(expr)

Get all vars from expression expr in post-DFS order.

bound_type_vars(expr[, mod])

Get bound type variables from expression/type e

bound_vars(expr)

Get bound vars from expression expr in post-DFS order.

check_constant(expr)

Check whether an expression is constant

check_kind(t[, mod])

Check that the type is well kinded and return the kind.

collect_device_annotation_ops(expr)

Collect the device annotation ops for the given expression.

collect_device_info(expr)

Collect the device allocation map for the given expression.

detect_feature(a[, b])

Detect the feature used in a relay program.

extract_fused_functions(mod)

Pass to extract IRModule of only fused primitive functions.

free_type_vars(expr[, mod])

Get free type variables from expression/type e

free_vars(expr)

Get free Vars from expression expr in Post DFS order.

get_total_mac_number(expr)

Count the number of MACs (multiply-accumulate) of a model

post_order_visit(expr, fvisit)

Recursively visit the ir in post DFS order node, apply fvisit.

unmatched_cases(match[, mod])

Finds cases that the match expression does not catch, if any.

well_formed(expr)

Check that each Var is only bound once (well formed).

class tvm.relay.analysis.AnnotatedRegionSet(expr, region_begin_op, region_end_op)

Class to represent a relay expression split into regions.

Methods

get_region(expr)

Get the region an expression belongs to.

get_region(expr)

Get the region an expression belongs to.

Parameters

expr (tvm.relay.Expr) – The expression.

Returns

The region containing the expression. None if not found.

Return type

region

class tvm.relay.analysis.CallGraph(module)

Class to represent a call graph.

Methods

global_call_count(var)

Return the number of global function calls from a given global var.

is_recursive(var)

Return if the function corresponding to a var is a recursive function.

print_var(var)

Print a call graph of a global function by name or by variable.

ref_count(var)

Return the number of references to the global var

Attributes

module

Return the contained Relay IR module.

property module

Return the contained Relay IR module.

Parameters

None

Returns

ret – The contained IRModule

Return type

tvm.ir.IRModule

ref_count(var)

Return the number of references to the global var

Parameters

var (Union[String, tvm.relay.GlobalVar]) –

Returns

ret – The number reference to the global var

Return type

int

global_call_count(var)

Return the number of global function calls from a given global var.

Parameters

var (Union[String, tvm.relay.GlobalVar]) –

Returns

ret – The number of global function calls from the given var.

Return type

int

is_recursive(var)

Return if the function corresponding to a var is a recursive function.

Parameters

var (Union[String, tvm.relay.GlobalVar]) –

Returns

ret – If the function corresponding to var is recurisve.

Return type

Boolean

print_var(var)

Print a call graph of a global function by name or by variable.

Parameters

var (Union[String, tvm.relay.GlobalVar]) – The name or global variable.

Returns

ret – The call graph represented in string.

Return type

String

class tvm.relay.analysis.Feature

The features a program might contain.

Attributes

fGraph

int(x=0) -> integer

fMatch

int(x=0) -> integer

fMatch = 14

Whether any non-atom fragment of the program is shared, making the program a graph.

fGraph = 15

Whether there is local fixpoint in the program.

tvm.relay.analysis.all_type_vars(expr, mod=None)

Get all type variables from expression/type e

Parameters
  • expr (Union[tvm.relay.Expr,tvm.relay.Type]) – The input expression/type

  • mod (Optional[tvm.IRModule]) – The global module

Returns

free – The list of all type variables in post-DFS order

Return type

List[tvm.relay.TypeVar]

tvm.relay.analysis.all_vars(expr)

Get all vars from expression expr in post-DFS order.

Parameters

expr (tvm.relay.Expr) – The input expression

Returns

free – The list of all variables in post-DFS order.

Return type

List[tvm.relay.Var]

tvm.relay.analysis.bound_type_vars(expr, mod=None)

Get bound type variables from expression/type e

Parameters
  • expr (Union[tvm.relay.Expr,tvm.relay.Type]) – The input expression/type

  • mod (Optional[tvm.IRModule]) – The global module

Returns

free – The list of bound type variables in post-DFS order

Return type

List[tvm.relay.TypeVar]

tvm.relay.analysis.bound_vars(expr)

Get bound vars from expression expr in post-DFS order.

Parameters

expr (tvm.relay.Expr) – The input expression

Returns

free – The list of bound variables in post-DFS order.

Return type

List[tvm.relay.Var]

tvm.relay.analysis.check_constant(expr)

Check whether an expression is constant

Parameters

expr (tvm.relay.Expr) – The input expression

Returns

result – Whether the expression is constant.

Return type

bool

tvm.relay.analysis.check_kind(t, mod=None)

Check that the type is well kinded and return the kind. For example, this mean type cannot has tensor of tensor, or is a tuple type of 2 shapes.

Parameters
  • t (tvm.relay.Type) – The type to check

  • mod (Optional[tvm.IRModule]) – The global module.

Returns

kind – the kind of t

Return type

Kind

Examples

assert check_kind(relay.TupleType([relay.TypeParam('tp1', relay.Kind.Shape)])) == Shape
assert check_kind(relay.TupleType([relay.TypeParam('tp1', relay.Kind.Type)])) == Type
tvm.relay.analysis.collect_device_annotation_ops(expr)

Collect the device annotation ops for the given expression.

Parameters

expr (tvm.relay.Expr) – The input expression.

Returns

ret – A dictionary mapping tvm.relay.Expr to device type where the keys are annotation expressions.

Return type

Dict[tvm.relay.Expr, int]

tvm.relay.analysis.collect_device_info(expr)

Collect the device allocation map for the given expression. The device ids are propagated from the device_copy operators.

Parameters

expr (tvm.relay.Expr) – The input expression.

Returns

ret – A dictionary mapping tvm.relay.Expr to device type.

Return type

Dict[tvm.relay.ir.expr, int]

tvm.relay.analysis.detect_feature(a, b=None)

Detect the feature used in a relay program.

Parameters
  • a (Union[tvm.relay.Expr, tvm.IRModule]) – The input expression or module.

  • b (Optional[Union[tvm.relay.Expr, tvm.IRModule]]) – The input expression or module. The two arguments cannot both be expression or module.

Returns

features – Features used in the program.

Return type

Set[Feature]

tvm.relay.analysis.extract_fused_functions(mod)

Pass to extract IRModule of only fused primitive functions.

The ExtractFusedFunctions pass invokes SimplifyInference, FuseOps(3), and ExtractFusedFunctions in that order

Parameters

mod (tvm.relay.IRModule) –

Returns

ret – A module containing only fused primitive functions

Return type

Dict[int, tvm.relay.function.Function]

tvm.relay.analysis.free_type_vars(expr, mod=None)

Get free type variables from expression/type e

Parameters
  • expr (Union[tvm.relay.Expr,tvm.relay.Type]) – The input expression/type

  • mod (Optional[tvm.IRModule]) – The global module

Returns

free – The list of free type variables in post-DFS order

Return type

List[tvm.relay.TypeVar]

tvm.relay.analysis.free_vars(expr)

Get free Vars from expression expr in Post DFS order.

Parameters

expr (tvm.relay.Expr) – The input expression

Returns

free – The list of free variables in post DFS order.

Return type

List[tvm.relay.Var]

Note

The fact that Vars are post-DFS ordred are useful in neural networks: usually this means weights of previous are ordered first.

tvm.relay.analysis.get_total_mac_number(expr)

Count the number of MACs (multiply-accumulate) of a model

Parameters

expr (tvm.relay.Expr) – The input expression.

Returns

result – The number of MACs (multiply-accumulate) of a model

Return type

int64

tvm.relay.analysis.post_order_visit(expr, fvisit)

Recursively visit the ir in post DFS order node, apply fvisit. Each node is guaranteed to be visited only once.

Parameters
  • expr (tvm.relay.Expr) – The input expression.

  • fvisit (function) – The visitor function to be applied.

tvm.relay.analysis.unmatched_cases(match, mod=None)

Finds cases that the match expression does not catch, if any.

Parameters
  • match (tvm.relay.Match) – The match expression

  • mod (Optional[tvm.IRModule]) – The module (defaults to an empty module)

Returns

missing_patterns – Patterns that the match expression does not catch.

Return type

[tvm.relay.Pattern]

tvm.relay.analysis.well_formed(expr)

Check that each Var is only bound once (well formed).

Parameters

expr (tvm.relay.Expr) – The input expression

Returns

well_form – Whether the input expression is well formed

Return type

bool