autoray.lazy

Submodules

Package Contents

Classes

Function

Get a compiled (by python compile), function that performs the

LazyArray

A lazy array representing a node in a computational graph.

Functions

ascend(lz)

Generate each unique computational node, from leaves to root. I.e. a

descend(lz)

Generate each unique computational node. Use ascend if you need to

compute(lz)

Compute the value of one or more lazy arrays. All nodes that are

get_source(lz[, params])

Write the source code of an unravelled version of the computational

Variable(shape[, backend])

Create a LazyArray from a shape only, representing a leaf node

shared_intermediates([cache])

Context in which intermediate results are shared.

array(x)

Create a LazyArray from an input array, representing a leaf node

transpose(a[, axes])

reshape(a, newshape)

tensordot(a, b[, axes])

einsum(*operands)

trace(a)

diag(a[, k])

matmul(x1, x2)

kron(x1, x2)

clip(a, a_min, a_max)

flip(a[, axis])

sort(a[, axis])

argsort(a[, axis])

stack(arrays[, axis])

concatenate(arrays[, axis])

split(ary, indices_or_sections[, axis])

where(condition, x, y)

take(x, indices)

complex(re, im)

Attributes

autoray.lazy.ascend(lz)[source]

Generate each unique computational node, from leaves to root. I.e. a topological ordering of the computational graph. Moreover, the nodes are visited ‘deepest first’.

Parameters:

lz (pytree of LazyArray) – The output node(s) of the computational graph to ascend to.

Yields:

LazyArray

autoray.lazy.descend(lz)[source]

Generate each unique computational node. Use ascend if you need to visit children before parents.

Parameters:

lz (pytree of LazyArray) – The output node(s) of the computational graph to descend.

Yields:

LazyArray

autoray.lazy.compute(lz)[source]

Compute the value of one or more lazy arrays. All nodes that are computed clear any references to their function, arguments and dependencies, and store the result in their _data attribute.

Parameters:

lz (pytree of LazyArray) – The output node(s) of the computational graph to compute.

Returns:

The computed value(s) of the lazy array(s).

Return type:

array or tuple of array

autoray.lazy.get_source(lz, params=None)[source]

Write the source code of an unravelled version of the computational graph, injecting required runtime objects into params.

Parameters:

lz (LazyArray or sequence of LazyArray) – The output node(s) of the computational graph to write the source code for. Their corresponding label is f"x{id(node)}" in the source code.

Returns:

The source code of the computational graph, suitable for exec.

Return type:

str

class autoray.lazy.Function(inputs, outputs, fold_constants=True)[source]

Get a compiled (by python compile), function that performs the computational graph corresponding to inputs -> outputs. The signature of the function is func(input_arrays) -> output_arrays. As an intermediate step, the computational graph is traced to a flattened source code string.

Parameters:
  • inputs (pytree of LazyArray) – The input node(s) of the computational graph.

  • outputs (pytree of LazyArray) – The output node(s) of the computational graph.

  • fold_constants (bool, optional) – If True, fold constant arrays (those with no dependence on inputs) into the graph ahead of compile.

See also

get_source, compute

__slots__ = ('_in_names', '_out_names', '_out_tree', '_source', '_code', '_locals')
__call__(*args)[source]
__getstate__()[source]

Helper for pickle.

__setstate__(state)[source]
print_source()[source]

Print the source code of the compiled function.

__repr__()[source]

Return repr(self).

class autoray.lazy.LazyArray(backend, fn, args, kwargs, shape, deps=None)[source]

A lazy array representing a node in a computational graph.

Parameters:
  • backend (str) – The backend of the array were it to be computed. This can be None but this may cause problems when propagating information about which functions to import to child nodes.

  • fn (callable) – The function to call to compute the array, presumable imported from backend. This can be None if the array already has data (e.g. is an input).

  • args (tuple) – The positional arguments to pass to fn, which might be LazyArray instances.

  • kwargs (dict) – The keyword arguments to pass to fn, which might be LazyArray instances.

  • shape (tuple) – The shape of the array that fn(*args, **kwargs) will return, or the shape of the array that data has.

  • deps (tuple, optional) – The LazyArray instances that fn(*args, **kwargs) depends on. If not specified, these will be automatically found from args and kwargs, specifying them manually is slightly more efficient.

property fn

The function to use to compute this array.

property fn_name

The name of the function to use to compute this array.

property args

The positional arguments to the function to use to compute this array.

property kwargs

The keyword arguments to the function to use to compute this array.

property shape
property ndim
property size
property backend
property deps

A tuple of the dependencies, other LazyArray instances, of this array.

property depth

The maximum distance to any input array in the computational graph.

property T
property H
property real
property imag
__slots__ = ('_backend', '_fn', '_args', '_kwargs', '_shape', '_data', '_deps', '_depth')
descend[source]
ascend[source]
compute_constants[source]
get_source[source]
plot[source]
plot_graph[source]
plot_circuit[source]
plot_history_size_footprint[source]
plot_history_functions[source]
plot_history_functions_scatter[source]
plot_history_functions_lines[source]
plot_history_functions_image[source]
plot_history_stats[source]
plot_history_stats_counts[source]
plot_history_stats_sizein[source]
__array_ufunc__
classmethod from_data(data)[source]

Create a new LazyArray directly from a concrete array.

classmethod from_shape(shape, backend='numpy')[source]

Create a new LazyArray with a given shape.

to(fn, args=None, kwargs=None, backend=None, shape=None, deps=None)[source]

Create a new LazyArray, by default propagating backend, shape, and deps from the the current LazyArray.

_materialize()[source]

Recursively compute all required args and kwargs for this node before computing itself and dereferencing dependencies. Note using this to materialize a large computation from scratch should be avoided due to the recursion limit, use x.compute() instead.

compute()[source]

Compute the value of this lazy array, clearing any references to the function, arguments and dependencies, and storing the result in the _data attribute as well as returning it.

Unlike self._materialize() this avoids deep recursion.

as_string(params)[source]

Create a string which evaluates to the lazy array creation.

get_function(variables, fold_constants=True)[source]

Get a compiled function that computes fn(arrays), with fn describing the computational graph of this LazyArray and arrays corresponding to the downstream LazyArray nodes variables.

Parameters:
  • variables (sequence of LazyArray) – Input nodes whose data can change between calls.

  • fold_constants (bool, optional) – Compute all intermediates which do not depend on variables prior to compilation.

Returns:

fn – Function with signature fn(arrays).

Return type:

callable

show(filler=' ', max_lines=None, max_depth=None)[source]

Show the computational graph as a nested directory structure.

history_num_nodes()[source]

Return the number of unique computational nodes in the history of this LazyArray.

history_max_size()[source]

Get the largest single tensor size appearing in this computation.

history_size_footprint(include_inputs=True)[source]

Get the combined size of intermediates at each step of the computation. Note this assumes that intermediates are immediately garbage collected when they are no longer required.

Parameters:

include_inputs (bool, optional) – Whether to include the size of the inputs in the computation. If True It is assumed they can be garbage collected once used but are all present at the beginning of the computation.

history_peak_size(include_inputs=True)[source]

Get the peak combined intermediate size of this computation.

Parameters:

include_inputs (bool, optional) – Whether to include the size of the inputs in the computation. If True It is assumed they can be garbage collected once used but are all present at the beginning of the computation.

history_total_size()[source]

The the total size of all unique arrays in the computational graph, possibly relevant e.g. for back-propagation algorithms.

history_stats(fn)[source]

Compute aggregate statistics about the computational graph.

Parameters:

fn (callable or str) – Function to apply to each node in the computational graph. If a string, one of ‘count’, ‘sizein’, ‘sizeout’ can be used to count the number of nodes, the total size of the inputs, or the total size of each output respectively.

Returns:

stats – Dictionary mapping function names to the aggregate statistics.

Return type:

dict

history_fn_frequencies()[source]

Get a dictionary mapping function names to the number of times they are used in the computational graph.

to_nx_digraph(variables=None)[source]

Convert this LazyArray into a networkx.DiGraph.

__len__()[source]
__iter__()[source]
__getitem__(key)[source]
__mul__(other)[source]
__rmul__(other)[source]
__add__(other)[source]
__radd__(other)[source]
__sub__(other)[source]
__rsub__(other)[source]
__floordiv__(other)[source]
__rfloordiv__(other)[source]
__truediv__(other)[source]
__rtruediv__(other)[source]
__pow__(other)[source]
__rpow__(other)[source]
__matmul__(other)[source]
__rmatmul__(other)[source]
__abs__()[source]
__neg__()[source]
__ne__(other)[source]

Return self!=value.

__gt__(other)[source]

Return self>value.

__lt__(other)[source]

Return self<value.

__ge__(other)[source]

Return self>=value.

__le__(other)[source]

Return self<=value.

reshape(shape)[source]
astype(dtype_name)[source]
__repr__()[source]

Return repr(self).

autoray.lazy.Variable(shape, backend=None)[source]

Create a LazyArray from a shape only, representing a leaf node in the computational graph. It can only act as a placeholder for data.

autoray.lazy.shared_intermediates(cache=None)[source]

Context in which intermediate results are shared.

Note that intermediate LazyArray instances (which can reference actual data) will not be garbage collected until 1. this context exits, and 2. the yielded cache is garbage collected (if it was captured).

Parameters:

cache (dict) – If specified, a user-stored dict in which intermediate lazy arrays will be stored. This can be used to interleave sharing contexts.

Returns:

cache – A dictionary in which sharing results are stored. If ignored, sharing results will be garbage collected when this context is exited. This dict can be passed to another context to resume sharing.

Return type:

dict

autoray.lazy.array(x)[source]

Create a LazyArray from an input array, representing a leaf node in the computational graph.

autoray.lazy.transpose(a, axes=None)[source]
autoray.lazy.reshape(a, newshape)[source]
autoray.lazy.tensordot(a, b, axes=2)[source]
autoray.lazy.einsum(*operands)[source]
autoray.lazy.trace(a)[source]
autoray.lazy.diag(a, k=0)[source]
autoray.lazy.matmul(x1, x2)[source]
autoray.lazy.kron(x1, x2)[source]
autoray.lazy.clip(a, a_min, a_max)[source]
autoray.lazy.flip(a, axis=None)[source]
autoray.lazy.sort(a, axis=-1)[source]
autoray.lazy.argsort(a, axis=-1)[source]
autoray.lazy.stack(arrays, axis=0)[source]
autoray.lazy.concatenate(arrays, axis=0)[source]
autoray.lazy.split(ary, indices_or_sections, axis=0)[source]
autoray.lazy.where(condition, x, y)[source]
autoray.lazy.take(x, indices)[source]
autoray.lazy.multiply[source]
autoray.lazy.add[source]
autoray.lazy.floordivide[source]
autoray.lazy.truedivide[source]
autoray.lazy.sin[source]
autoray.lazy.cos[source]
autoray.lazy.tan[source]
autoray.lazy.arcsin[source]
autoray.lazy.arccos[source]
autoray.lazy.arctan[source]
autoray.lazy.sinh[source]
autoray.lazy.cosh[source]
autoray.lazy.tanh[source]
autoray.lazy.arcsinh[source]
autoray.lazy.arccosh[source]
autoray.lazy.arctanh[source]
autoray.lazy.sqrt[source]
autoray.lazy.exp[source]
autoray.lazy.log[source]
autoray.lazy.log2[source]
autoray.lazy.log10[source]
autoray.lazy.conj[source]
autoray.lazy.sign[source]
autoray.lazy.angle[source]
autoray.lazy.real[source]
autoray.lazy.imag[source]
autoray.lazy.prod[source]
autoray.lazy.abs[source]
autoray.lazy.sum[source]
autoray.lazy.min[source]
autoray.lazy.max[source]
autoray.lazy.complex(re, im)[source]