module tools.graphs
#
Short summary#
module mlprodict.tools.graphs
Alternative to dot to display a graph.
Classes#
class |
truncated documentation |
---|---|
Structure which contains the necessary information to display a graph using an adjacency matrix. |
|
BiGraph representation. |
Functions#
function |
truncated documentation |
---|---|
Creates a hash of length length. |
|
Converts an ONNX graph into a graph representation, edges, vertices. |
|
Computes a distance between two ONNX graphs. They must not be too big otherwise this function might take for ever. … |
Static Methods#
staticmethod |
truncated documentation |
---|---|
Implements graph type ‘basic’ for function |
|
Implements graph type ‘simplified’ for function |
|
Computes a distance between two ONNX graphs. They must not be too big otherwise this function might take for ever. … |
Methods#
method |
truncated documentation |
---|---|
Returns a vertex is key is a string or an edge if it is a tuple. |
|
Iterates over actions. |
|
Iterates over all vertices and edges. It produces 3-uples: |
|
usual |
|
usual |
|
Adds an action to display the graph. |
|
Builds an adjacency matrix. |
|
Creates a display structure which contains all the necessary steps to display a graph. |
|
Order nodes. Depth first. Returns a sequence of keys of mixed v1, v2. |
|
Orders the vertices from the input to the output. |
|
Creates a text summary of the graph. |
|
Displays the graph as a single string. See |
Documentation#
Alternative to dot to display a graph.
New in version 0.7.
- class mlprodict.tools.graphs.AdjacencyGraphDisplay#
Bases:
object
Structure which contains the necessary information to display a graph using an adjacency matrix.
New in version 0.7.
- class Action(x, y, kind, label, orientation=None)#
Bases:
object
One action to do.
- __init__(x, y, kind, label, orientation=None)#
- __repr__()#
usual
- __init__()#
- __iter__()#
Iterates over actions.
- __str__()#
usual
- add(x, y, kind, label, orientation=None)#
Adds an action to display the graph.
- Parameters:
x – x coordinate
y – y coordinate
kind – ‘cross’ or ‘text’
label – specific to kind
orientation – a 2-uple (i,j) where i or j in {-1,0,1}
- to_text()#
Displays the graph as a single string. See
onnx2bigraph
to see how the result looks like.- Returns:
str
- class mlprodict.tools.graphs.BiGraph(v0, v1, edges)#
Bases:
object
BiGraph representation.
New in version 0.7.
- Parameters:
v0 – first set of vertices (dictionary)
v1 – second set of vertices (dictionary)
edges – edges
- class A(kind)#
Bases:
object
Additional information for a vertex or an edge.
- __init__(kind)#
- __repr__()#
Return repr(self).
- class B(name, content, onnx_name)#
Bases:
object
Additional information for a vertex or an edge.
- __init__(name, content, onnx_name)#
- __repr__()#
Return repr(self).
- __getitem__(key)#
Returns a vertex is key is a string or an edge if it is a tuple.
- Parameters:
key – vertex or edge name
- Returns:
value
- __init__(v0, v1, edges)#
- Parameters:
v0 – first set of vertices (dictionary)
v1 – second set of vertices (dictionary)
edges – edges
- __iter__()#
Iterates over all vertices and edges. It produces 3-uples:
0, name, A: vertices in v0
1, name, A: vertices in v1
-1, name, A: edges
- __str__()#
usual
- static _onnx2bigraph_basic(model_onnx, recursive=False)#
Implements graph type ‘basic’ for function
onnx2bigraph
.
- static _onnx2bigraph_simplified(model_onnx, recursive=False)#
Implements graph type ‘simplified’ for function
onnx2bigraph
.
- adjacency_matrix()#
Builds an adjacency matrix.
- Returns:
matrix, list of row vertices, list of column vertices
- display_structure(grid=5, distance=5)#
Creates a display structure which contains all the necessary steps to display a graph.
- Parameters:
grid – align text to this grid
distance – distance to the text
- Returns:
instance of
AdjacencyGraphDisplay
- static onnx_graph_distance(onx1, onx2, verbose=0, fLOG=<built-in function print>)#
Computes a distance between two ONNX graphs. They must not be too big otherwise this function might take for ever. The function relies on package mlstatpy.
- Parameters:
onx1 – first graph (ONNX graph or model file name)
onx2 – second graph (ONNX graph or model file name)
verbose – verbosity
fLOG – logging function
- Returns:
distance and differences
Warning
This is very experimental and very slow.
New in version 0.7.
- order()#
Order nodes. Depth first. Returns a sequence of keys of mixed v1, v2.
- order_vertices()#
Orders the vertices from the input to the output.
- Returns:
dictionary {vertex name: order}
- summarize()#
Creates a text summary of the graph.
- mlprodict.tools.graphs.make_hash_bytes(data, length=20)#
Creates a hash of length length.
- mlprodict.tools.graphs.onnx2bigraph(model_onnx, recursive=False, graph_type='basic')#
Converts an ONNX graph into a graph representation, edges, vertices.
- Parameters:
model_onnx – ONNX graph
recursive – dig into subgraphs too
graph_type – kind of graph it creates
- Returns:
see @cl BiGraph
About graph_type:
- ‘basic’: basic graph structure, it returns an instance
of type
BiGraph
. The structure keeps the original names.
- ‘simplified’: simplifed graph structure, names are removed
as they could be prevent the algorithm to find any matching.
Displays an ONNX graph as text
The function uses an adjacency matrix of the graph. Results are displayed by rows, operator by columns. Results kinds are shows on the left, their names on the right. Operator types are displayed on the top, their names on the bottom.
<<<
import numpy from mlprodict.onnx_conv import to_onnx from mlprodict import __max_supported_opset__ as opv from mlprodict.tools.graphs import onnx2bigraph from mlprodict.npy.xop import loadop OnnxAdd, OnnxSub = loadop('Add', 'Sub') idi = numpy.identity(2).astype(numpy.float32) A = OnnxAdd('X', idi, op_version=opv) B = OnnxSub(A, 'W', output_names=['Y'], op_version=opv) onx = B.to_onnx({'X': idi, 'W': idi}) bigraph = onnx2bigraph(onx) graph = bigraph.display_structure() text = graph.to_text() print(text)
>>>
somewhere/workspace/mlprodict/mlprodict_UT_39_std/_doc/sphinxdoc/source/mlprodict/npy/xop_variable.py:64: DeprecationWarning: `mapping.NP_TYPE_TO_TENSOR_TYPE` is now deprecated and will be removed in the next release or so.To silence this warning, please use `helper.{self._future_function}` instead. return NP_TYPE_TO_TENSOR_TYPE[dtype] somewhere/workspace/mlprodict/mlprodict_UT_39_std/_doc/sphinxdoc/source/mlprodict/npy/xop_variable.py:64: DeprecationWarning: `mapping.NP_TYPE_TO_TENSOR_TYPE` is now deprecated and will be removed in the next release or so.To silence this warning, please use `helper.{self._future_function}` instead. return NP_TYPE_TO_TENSOR_TYPE[dtype] A S d u d b Input-0 I1 W Input-1 I0 X Init I1 init inout O0 I0 out_add_0 Output-0 O0 Y _ _ a s d u d b
New in version 0.7.
- mlprodict.tools.graphs.onnx_graph_distance(onx1, onx2, verbose=0, fLOG=<built-in function print>)#
Computes a distance between two ONNX graphs. They must not be too big otherwise this function might take for ever. The function relies on package mlstatpy.
- Parameters:
onx1 – first graph (ONNX graph or model file name)
onx2 – second graph (ONNX graph or model file name)
verbose – verbosity
fLOG – logging function
- Returns:
distance and differences
Warning
This is very experimental and very slow.
New in version 0.7.