hidet.utils

Functions:

factorize(n)

example: factor(12) => [1, 2, 3, 4, 6, 12]

initialize(*args, **kwargs)

Decorate an initialization function.

gcd(a, b)

Get the greatest common divisor of non-negative integers a and b.

lcm(a, b)

Get the least common multiple of non-negative integers a and b.

error_tolerance(a, b)

Given two tensors with the same shape and data type, this function finds the minimal e, such that

benchmark_func(run_func[, warmup, number, ...])

Benchmark given function.

Classes:

DirectedGraph()

Directed graph.

hidet.utils.factorize(n)[source]

example: factor(12) => [1, 2, 3, 4, 6, 12]

hidet.utils.initialize(*args, **kwargs)[source]

Decorate an initialization function. After decorating with this function, the initialization function will be called after the definition.

Parameters:
  • args – The positional arguments of initializing.

  • kwargs – The keyword arguments of initializing.

Returns:

A decorator that will call given function with args and kwargs, and return None (to prevent this function to be called again).

Return type:

ret

hidet.utils.gcd(a, b)[source]

Get the greatest common divisor of non-negative integers a and b.

Parameters:
  • a (int) – The lhs operand.

  • b (int) – The rhs operand.

Returns:

ret – The greatest common divisor.

Return type:

int

hidet.utils.lcm(a, b)[source]

Get the least common multiple of non-negative integers a and b. :param a: The lhs operand. :type a: int :param b: The rhs operand. :type b: int

Returns:

ret – The least common multiple.

Return type:

int

Parameters:
  • a (int) –

  • b (int) –

hidet.utils.error_tolerance(a, b)[source]

Given two tensors with the same shape and data type, this function finds the minimal e, such that

abs(a - b) <= abs(b) * e + e

Parameters:
Returns:

ret – The error tolerance between a and b.

Return type:

float

hidet.utils.benchmark_func(run_func, warmup=1, number=5, repeat=5, median=True)[source]

Benchmark given function.

The given function run_func will be executed \(warmup + repeat * number\) times. Each \(number\) times of execution will be grouped and conducted together.

Parameters:
  • run_func (Callable[[], Any]) – Any callable function to be benchmarked.

  • warmup (int) – The number of warm-up executions.

  • number (int) – The number of executions to be grouped for measurement.

  • repeat (int) – The number of repeat times of the group measurement.

  • median (bool) – Whether the median latency is returned, instead of the latency.

Returns:

ret

  • When median == True, a single latency number is returned.

  • When median == False, the latency of each repeat is returned, as a list of floats.

Return type:

Union[float, List[float]]

class hidet.utils.DirectedGraph[source]

Directed graph.

A directed graph representation.

Methods:

from_edges(edges)

Create a directed graph from edges.

has_node(node)

Whether the node has been added to the graph.

has_edge(src, dst)

Whether there is an edge (src, dst) in the graph.

add_node(node)

Add a node.

add_edge(src, dst)

Add an edge.

topological_order()

Get a topological order of the nodes in the directed graph.

static from_edges(edges)[source]

Create a directed graph from edges.

The edges should be a list of (src, dst) tuples, and each tuple represents an edge.

Parameters:

edges (List[Tuple[GraphNode, GraphNode]]) – The edges of the directed graph to be created.

Returns:

ret – The created directed graph.

Return type:

DirectedGraph

has_node(node)[source]

Whether the node has been added to the graph.

Parameters:

node (GraphNode) – The node to be checked.

Returns:

ret – True if the node has been added.

Return type:

bool

has_edge(src, dst)[source]

Whether there is an edge (src, dst) in the graph.

Parameters:
  • src (GraphNode) – The source node of the edge.

  • dst (GraphNode) – The destination node of the edge.

Returns:

ret – True if the edge (src, dst) is in the graph.

Return type:

bool

add_node(node)[source]

Add a node.

Parameters:

node (GraphNode) – The node to be added to the graph.

add_edge(src, dst)[source]

Add an edge.

The node src and dst will be added to the graph if they have not been added.

Parameters:
  • src (GraphNode) – The source of the edge.

  • dst (GraphNode) – The destination of the edge.

topological_order()[source]

Get a topological order of the nodes in the directed graph.

Returns:

ret – The nodes in the topological order.

Return type:

List[GraphNode]

Raises:

ValueError – If the directed graph is cyclic (i.e., there is a loop in the graph).