module onnx_tools.onnx_manipulations#

Short summary#

module mlprodict.onnx_tools.onnx_manipulations

Implements a class able to compute the predictions from on an ONNX model.

source on GitHub

Functions#

function

truncated documentation

enumerate_model_node_outputs

Enumerates all the nodes of a model.

hash_onnx_object

Hash the content of an object.

insert_results_into_onnx

Inserts results into an ONNX graph to produce an extended ONNX graph. It can saved and looked into with a tool such …

onnx_rename_names

Renames all names except the inputs and outputs.

overwrite_opset

Overwrites the main opset in an ONNX file. Does not change any node definition.

select_model_inputs_outputs

Takes a model and changes its outputs.

Documentation#

Implements a class able to compute the predictions from on an ONNX model.

source on GitHub

mlprodict.onnx_tools.onnx_manipulations.enumerate_model_node_outputs(model, add_node=False, order=False)#

Enumerates all the nodes of a model.

Parameters
  • modelONNX graph

  • add_node – if False, the function enumerates all output names from every node, otherwise, it enumerates tuple (output name, node)

  • order – goes through outputs following the graph order

Returns

enumerator

source on GitHub

mlprodict.onnx_tools.onnx_manipulations.hash_onnx_object(obj, max_size)#

Hash the content of an object.

source on GitHub

mlprodict.onnx_tools.onnx_manipulations.insert_results_into_onnx(model, results, as_parameter=True, suffix='_DBG', param_name=None, node_type='DEBUG', domain='DEBUG', domain_opset=1)#

Inserts results into an ONNX graph to produce an extended ONNX graph. It can saved and looked into with a tool such as netron.

Parameters
  • model – ONNX graph

  • results – results to be added in a dictionary

  • as_parameter – add new nodes with results as one parameter (True) or as initializer (False)

  • suffix – suffix to add to new results

  • param_name – name of the parameter to add (by default the result name), it can be a function param_name(reult_name) -> parameter_name

  • node_type – type of the new node

  • domain – domain the new node

  • domain_opset – opset for domain

Returns

new ONNX graph

See method OnnxInference.run2onnx to see a graph this function produces.

mlprodict/onnx_tools/debug.png

New in version 0.7.

source on GitHub

mlprodict.onnx_tools.onnx_manipulations.onnx_rename_names(model, strategy='simple', recursive=True, verbose=0, fLOG=<built-in function print>, counts=None, replace=None, taken=None)#

Renames all names except the inputs and outputs.

Parameters
  • model – onnx model

  • strategy – two strategies are implemented, see below

  • recursive – walk through subgraphs

  • verbose – verbose, if positive, reports on all changed names

  • fLOG – logging function

  • counts – used for recursion

  • replace – used for recursion, it can be also used to to fix some replacements

  • taken – used for recursion

Returns

onnx model (the model is modified in place)

Strategies:

  • ‘simple’: use a letter n for node, r, i for initializer,

    this letter is followed by a number

  • ‘type’: the name depends on the node type and content,

    the hash is kept as small as possible

source on GitHub

mlprodict.onnx_tools.onnx_manipulations.overwrite_opset(model, new_opset)#

Overwrites the main opset in an ONNX file. Does not change any node definition.

Parameters
  • model – ONNX model

  • new_opset – new opset

Returns

ONNX model

source on GitHub

mlprodict.onnx_tools.onnx_manipulations.select_model_inputs_outputs(model, outputs=None, inputs=None, infer_shapes=False, overwrite=None, remove_unused=True, verbose=0, fLOG=None)#

Takes a model and changes its outputs.

Parameters
  • modelONNX model

  • inputs – new inputs, same ones if None

  • outputs – new outputs, same ones if None

  • infer_shapes – infer inputs and outputs shapes

  • overwrite – overwrite type and shapes for inputs or outputs, overwrite is a dictionary {‘name’: (numpy dtype, shape)}

  • remove_unused – remove unused nodes from the graph

  • verbose – display information while converting

  • fLOG – logging function

Returns

modified model

The function removes unneeded nodes.

Change ONNX model inputs

The following exampels shows how to change the inputs of model to bypass the first nodes. Shape inferences fails to determine the new inputs type. They need to be overwritten. verbose=1, fLOG=print shows the number of deleted nodes.

import onnx
from mlprodict.onnx_tools.onnx_manipulations import select_model_inputs_outputs

onx = onnx.load(path)
onx2 = select_model_inputs_outputs(
    onx, inputs=["SentenceTokenizer/SentencepieceTokenizeOp:0",
                 "SentenceTokenizer/SentencepieceTokenizeOp:1"],
    infer_shapes=True, verbose=1, fLOG=print,
    overwrite={'SentenceTokenizer/SentencepieceTokenizeOp:0': (numpy.int32, None),
               'SentenceTokenizer/SentencepieceTokenizeOp:1': (numpy.int64, None)})
onnx.save(onx2, path2)

Changed in version 0.6: Supports the case where inputs are changed.

Changed in version 0.7: Parameter remove_unused was added. Unused are removed by default.

source on GitHub