neon.backends.backend.OpTreeNode

class neon.backends.backend.OpTreeNode[source]

Bases: tuple

An OpTreeNode is a tuple of length 3. The first element is a dict specifying the operation, and the second and third elements specify the operands. From an op-tree’s tree perspective, think about the 3 elements as 3 nodes. The second and third element are the left and right child of the first element.

__init__()

x.__init__(...) initializes x; see help(type(x)) for signature

Methods

asnumpyarray() Returns the evaluated value of the optree as a host numpy.ndarray.
astensor() Returns the evaluated value of the optree as a Tensor.
build(op, a, b[, out]) Build OpTreeNode.
count(...)
execute() Execute the optree.
index((value, [start, ...) Raises ValueError if the value is not present.
intrinsic_key_maps() Returns the intrinsic key, tensor_index_map and index_tensor_map for the purpose of identifying a optree.
key() Returns a key for identifying the optree.
list_to_optree(l) Convert list to optree recursively.
optree_to_list(optree) Convert optree to list of lists recursively.
pp() Pretty print of the optree.
transpose([out]) Return a transposed view of the data.
traverse(stack) Post order walk op tree and produce postfix stack.

Attributes

T Return a transposed view of the data.
shape Return the shape of the OpTreeNode.
T

Return a transposed view of the data.

asnumpyarray()[source]

Returns the evaluated value of the optree as a host numpy.ndarray. Allocates new memory, usually used for debug.

Returns:evaluated value
Return type:numpy.ndarray
astensor()[source]

Returns the evaluated value of the optree as a Tensor. Allocates new memory, usually used for debug.

Returns:evaluated value
Return type:Tensor
static build(op, a, b, out=None, **kwargs)[source]

Build OpTreeNode.

Parameters:
  • a (OpTreeNode, Tensor, numeric) – left-hand side operand.
  • b (OpTreeNode, Tensor, numeric) – right-hand side operand.
  • out (Tensor, optional) – where the result will be stored. If out is not None, the op-tree will be executed.
  • kwargs – optional argument such as axis of the reducion.
count(value) → integer -- return number of occurrences of value
execute()[source]

Execute the optree. When calling execute(), there must be one and only one assign operation at the very top of the op-tree. The corresponding backend’s execute function will be called.

index(value[, start[, stop]]) → integer -- return first index of value.

Raises ValueError if the value is not present.

intrinsic_key_maps()[source]

Returns the intrinsic key, tensor_index_map and index_tensor_map for the purpose of identifying a optree. The key is depended on the ops tensors dimensions and the relaion among the tensors.

x0 * x1 + x0 * x2 will have the same intrinsic key as y0 * y1 + y0 * y2, if xi and yi have the same shape.

In tensor_index_map and index_tensor_map, tensors has a one-to-one mapping with indices. The index of the tensor is depended on the first occurance of the tensor in the post-order traversal of the optree.

Returns:(intrinsic_key, tensor_index_map, index_tensor_map)
key()[source]

Returns a key for identifying the optree. The key is depended on the ops and the id of the tensors. Since __eq__ is overloaded, need to manage the hashing of the OpTreeNode manually.

Returns:optree key
Return type:tuple
static list_to_optree(l)[source]

Convert list to optree recursively.

static optree_to_list(optree)[source]

Convert optree to list of lists recursively.

pp()[source]

Pretty print of the optree.

Parameters:node (OpTreeNode) – the top node of the op-tree to print
Returns:string representation of the op-tree
Return type:str
shape

Return the shape of the OpTreeNode.

transpose(out=None)[source]

Return a transposed view of the data.

traverse(stack)[source]

Post order walk op tree and produce postfix stack.

Parameters:stack (list) – user shall give empty list like list(), then it’s used recursively to construct the post-order stack.