module testing.einsum.einsum_impl_classes#

Inheritance diagram of mlprodict.testing.einsum.einsum_impl_classes

Short summary#

module mlprodict.testing.einsum.einsum_impl_classes

Classes representing the sequence of matrix operations to implement einsum computation.

source on GitHub

Classes#

class

truncated documentation

EinsumSubOp

Defines a sub operation used in Einsum decomposition.

GraphEinsumSubOp

Class gathering all nodes produced to explicit einsum operators.

Functions#

function

truncated documentation

single_axes

axes contains positive values, then it is the position of this axis in the original matrix, otherwise it is -1 …

Methods#

method

truncated documentation

__init__

__init__

__iter__

Iterates on nodes.

__repr__

_apply_batch_dot

_apply_diagonal

_apply_expand_dims

_apply_id

_apply_matmul

_apply_mul

_apply_reduce_sum

_apply_reduce_sum_mm

_apply_squeeze

_apply_transpose

_apply_transpose_mm

_check_

_check_arg_

_check_inputs_

_check_onnx_opset_

_check_row_

Checks input or output is valid.

_check_shape_

_compute_output_row_batch_dot

_compute_output_row_diagonal

_compute_output_row_expand_dims

_compute_output_row_id

_compute_output_row_matmul

_compute_output_row_mul

_compute_output_row_reduce_sum

_compute_output_row_reduce_sum_mm

_compute_output_row_squeeze

_compute_output_row_transpose

_compute_output_row_transpose_mm

_get_data

_get_forward_nodes

Returns the forward nodes.

_onnx_name

_pprint_forward

_replace_node_sequence

Removes a sequence of nodes. The method does not check that the graph remains consistent.

_to_onnx_batch_dot

_to_onnx_expand_dims

_to_onnx_id

_to_onnx_mul

_to_onnx_reduce_sum

_to_onnx_squeeze

_to_onnx_transpose

add_info

Adds information to the node.

append

Adds one input or result.

apply

Applies one operator on the data.

apply_sequence

Applies a sequence of operations on a list of inputs.

clean_unused_nodes

Cleans nodes with unused outputs.

compute_output_row

Updates row based on the operator.

dot_label

Displays some informations useful to understand the operator.

get_dot_kind

Every matrix multiplication can be either:

mark

Marks one input or result as an intermediate result after a full einsum step.

mark_last_node

Marks the last node as the final output.

remove_duplicate_transpose

Removes consecutive transpose by merging them.

simplify_mm_nodes

Node name suffixed by mm are an artifact to keep the graph consistent while building it. They can now …

to_dot

Produces a graph in dot.

to_onnx

Converts this node into ONNX. Enumerates all ONNX node which participate to the conversion. The last one …

to_onnx

Converts the graph into ONNX.

Documentation#

Classes representing the sequence of matrix operations to implement einsum computation.

source on GitHub

class mlprodict.testing.einsum.einsum_impl_classes.EinsumSubOp(full_dim, name, *inputs, **kwargs)#

Bases: object

Defines a sub operation used in Einsum decomposition.

Parameters:
  • name – name (reshape, transpose, reduce_sum, matmul, id, squeeze, diagonal, mul, batch_dot)

  • inputs – inputs

  • kwargs – arguments

Operator suffixed by _mm (transpose_mm, reduce_sum_mm) are equivalent to the same operator without the suffix but takes two inputs and only changes the first one.

Attributes _info summarizes the known information about dimensions. Many of them are empty because inserted. Value 1 means it was the case, 2 means it is a plain dimension.

source on GitHub

__init__(full_dim, name, *inputs, **kwargs)#
__repr__()#

Return repr(self).

_allowed = {'batch_dot', 'diagonal', 'expand_dims', 'id', 'matmul', 'mul', 'reduce_sum', 'reduce_sum_mm', 'squeeze', 'transpose', 'transpose_mm'}#
_apply_batch_dot(data, verbose=False, **kwargs)#
_apply_diagonal(data, verbose=False, **kwargs)#
_apply_expand_dims(data, verbose=False, **kwargs)#
_apply_id(data, verbose=False, **kwargs)#
_apply_matmul(data, verbose=False, **kwargs)#
_apply_mul(data, verbose=False, **kwargs)#
_apply_reduce_sum(data, verbose=False, **kwargs)#
_apply_reduce_sum_mm(data, verbose=False, **kwargs)#
_apply_squeeze(data, verbose=False, **kwargs)#
_apply_transpose(data, verbose=False, **kwargs)#
_apply_transpose_mm(data, verbose=False, **kwargs)#
_check_()#
_check_arg_(name, typ, empty=False)#
_check_inputs_(n_expected, check_dim=False)#
_check_onnx_opset_(opset, limit)#
_check_row_(row, inp=False, verbose=False)#

Checks input or output is valid.

source on GitHub

_check_shape_(m)#
_compute_output_row_batch_dot(row, row2=None, ab=False, verbose=False)#
_compute_output_row_diagonal(row, row2=None, ab=False, verbose=False)#
_compute_output_row_expand_dims(row, row2=None, ab=False, verbose=False)#
_compute_output_row_id(row, row2=None, ab=False, verbose=False)#
_compute_output_row_matmul(row, row2=None, ab=False, verbose=False)#
_compute_output_row_mul(row, row2=None, ab=False, verbose=False)#
_compute_output_row_reduce_sum(row, row2=None, ab=False, verbose=False)#
_compute_output_row_reduce_sum_mm(row, row2=None, ab=False, verbose=False)#
_compute_output_row_squeeze(row, row2=None, ab=False, verbose=False)#
_compute_output_row_transpose(row, row2=None, ab=False, verbose=False)#
_compute_output_row_transpose_mm(row, row2=None, ab=False, verbose=False)#
_get_data(data, key)#
_onnx_name()#
_to_onnx_batch_dot(names, opset, verbose=False, **kwargs)#
_to_onnx_expand_dims(names, opset, verbose=False, **kwargs)#
_to_onnx_id(names, opset, verbose=False, **kwargs)#
_to_onnx_mul(data, verbose=False, **kwargs)#
_to_onnx_reduce_sum(names, opset, verbose=False, **kwargs)#
_to_onnx_squeeze(names, opset, verbose=False, **kwargs)#
_to_onnx_transpose(names, opset, verbose=False, **kwargs)#
add_info(**kwargs)#

Adds information to the node.

Parameters:

kwargs – dictionary

source on GitHub

apply(data, verbose=False, **kwargs)#

Applies one operator on the data.

Parameters:
  • data – dictionary storing the results

  • verbose – prints out intermediate results

  • kwargs – additional parameters, see methods _apply*

Returns:

output

Known additional paramaters:

source on GitHub

compute_output_row(row, row2=None, ab=False, verbose=False)#

Updates row based on the operator.

source on GitHub

dot_label()#

Displays some informations useful to understand the operator.

source on GitHub

get_dot_kind()#

Every matrix multiplication can be either:

  • a simple multiplication (M) (undetected)

  • a 2D matrix multiplication (11)

  • a broadcasted matrix multiplication (N1 or 1N)

  • a batch matrix multiplication (NN)

This method returns which kind it is.

source on GitHub

to_onnx(names, opset=None, verbose=False, **kwargs)#

Converts this node into ONNX. Enumerates all ONNX node which participate to the conversion. The last one is the final output.

Parameters:
  • names – dictionary where to find already converted name

  • opset – opset

  • verbose – prints out intermediate results

  • kwargs – additional parameter for the conversion

Returns:

output

source on GitHub

class mlprodict.testing.einsum.einsum_impl_classes.GraphEinsumSubOp(letters, mat, lengths, duplicates)#

Bases: object

Class gathering all nodes produced to explicit einsum operators.

Parameters:

source on GitHub

__init__(letters, mat, lengths, duplicates)#
__iter__()#

Iterates on nodes.

_get_forward_nodes()#

Returns the forward nodes.

source on GitHub

_pprint_forward()#
_replace_node_sequence(added, deleted)#

Removes a sequence of nodes. The method does not check that the graph remains consistent.

source on GitHub

append(op)#

Adds one input or result.

Parameters:

op – integer (an input) or an instance of EinsumSubOp.

Returns:

op or None if op is an integer

source on GitHub

apply_sequence(*inputs, verbose=False, **kwargs)#

Applies a sequence of operations on a list of inputs.

Parameters:
  • inputs – inputs:

  • verbose – prints out intermediate results

  • kwargs – additional parameters, see apply.

Returns:

output

source on GitHub

clean_unused_nodes(verbose=False)#

Cleans nodes with unused outputs.

Parameters:

verbose – display intermediate information

source on GitHub

mark(i, op)#

Marks one input or result as an intermediate result after a full einsum step.

Parameters:

op – integer (an input) or an instance of EinsumSubOp.

source on GitHub

mark_last_node()#

Marks the last node as the final output.

source on GitHub

remove_duplicate_transpose(verbose=False)#

Removes consecutive transpose by merging them.

Parameters:

verbose – display intermediate information

source on GitHub

simplify_mm_nodes(verbose=False)#

Node name suffixed by mm are an artifact to keep the graph consistent while building it. They can now be replaced by the equivalent node without suffix mm.

Parameters:

verbose – display intermediate information

source on GitHub

to_dot(**kwargs)#

Produces a graph in dot.

Parameters:

kwargs – additional graph option

Returns:

string

source on GitHub

to_onnx(output, *inputs, dtype=None, verbose=False, opset=None, **kwargs)#

Converts the graph into ONNX.

Parameters:
  • output – output name

  • inputs – input names

  • dtype – type used for all operators

  • opset – desired opset, None for the last one

  • verbose – display intermediate operators

  • kwargs – additional parameter to use when building the ONNX graph, list of supported parameters: name, ir_version, producer_name, producer_version, initializer

Returns:

ONNX graph

Not all graphs can be converted into ONNX. Only graphs produced with strategy=’numpy’ can be converted otherwise the following error shows up:

NotImplementedError: to_onnx not implemented for 'matmul'.

source on GitHub

mlprodict.testing.einsum.einsum_impl_classes.single_axes(axes)#

axes contains positive values, then it is the position of this axis in the original matrix, otherwise it is -1 meaning this axis is an added single dimension to align all the dimensions based on the einsum equation.

Parameters:

axes – axes described above

Returns:

list of integer in set {1, 2}, 1 for a single axis, 2 otherwise

source on GitHub