module 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.
Classes#
class |
truncated documentation |
---|---|
Defines a sub operation used in Einsum decomposition. |
|
Class gathering all nodes produced to explicit einsum operators. |
Functions#
function |
truncated documentation |
---|---|
axes contains positive values, then it is the position of this axis in the original matrix, otherwise it is -1 … |
Methods#
method |
truncated documentation |
---|---|
Iterates on nodes. |
|
Checks input or output is valid. |
|
Returns the forward nodes. |
|
Removes a sequence of nodes. The method does not check that the graph remains consistent. |
|
Adds information to the node. |
|
Adds one input or result. |
|
Applies one operator on the data. |
|
Applies a sequence of operations on a list of inputs. |
|
Cleans nodes with unused outputs. |
|
Updates row based on the operator. |
|
Displays some informations useful to understand the operator. |
|
Every matrix multiplication can be either: |
|
Marks one input or result as an intermediate result after a full einsum step. |
|
Marks the last node as the final output. |
|
Removes consecutive transpose by merging them. |
|
Node name suffixed by mm are an artifact to keep the graph consistent while building it. They can now … |
|
Produces a graph in dot. |
|
Converts this node into ONNX. Enumerates all ONNX node which participate to the conversion. The last one … |
|
Converts the graph into ONNX. |
Documentation#
Classes representing the sequence of matrix operations to implement einsum computation.
- 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.
- __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.
- _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
- 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:
‘matmul_impl’: if None calls numpy.einsum through
numpy_extended_dot
(default) or ‘py’ to callnumpy_extended_dot_python
instead.
- compute_output_row(row, row2=None, ab=False, verbose=False)#
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:
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.
- 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
- class mlprodict.testing.einsum.einsum_impl_classes.GraphEinsumSubOp(letters, mat, lengths, duplicates)#
Bases:
object
Class gathering all nodes produced to explicit einsum operators.
- Parameters:
letters – list of distinct letters
mat – matrix, see
analyse_einsum_equation
lengths – lengths of every input
duplicates – see
analyse_einsum_equation
- __init__(letters, mat, lengths, duplicates)#
- __iter__()#
Iterates on nodes.
- _get_forward_nodes()#
Returns the forward nodes.
- _pprint_forward()#
- _replace_node_sequence(added, deleted)#
Removes a sequence of nodes. The method does not check that the graph remains consistent.
- 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
- 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
- clean_unused_nodes(verbose=False)#
Cleans nodes with unused outputs.
- Parameters:
verbose – display intermediate information
- 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
.
- mark_last_node()#
Marks the last node as the final output.
- remove_duplicate_transpose(verbose=False)#
Removes consecutive transpose by merging them.
- Parameters:
verbose – display intermediate information
- 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
- to_dot(**kwargs)#
Produces a graph in dot.
- Parameters:
kwargs – additional graph option
- Returns:
string
- 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'.
- 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