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

Classes#

class

truncated documentation

_inline_mapping

Overwrites class dictionary to debug more easily.

Functions#

function

truncated documentation

_change_subgraph_io_type_shape_list

_get_new_name

_onnx_function_to_model_convert_io

_onnx_inline_function_graph

_onnx_inline_function_node

change_input_type

Changes the type of an input.

change_subgraph_io_type_shape

Changes the type of an input or an output of a subgraph.

enumerate_model_node_outputs

Enumerates all the nodes of a model.

get_hidden_inputs

Returns the list of hidden inputs used by subgraphs.

get_opsets

Enumerates all opsets used in a model.

hash_onnx_object

Hashes the content of an object. It uses module hashlib.

insert_results_into_onnx

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

onnx_function_to_model

Converts an ONNX FunctionProto into a ModelProto. The function does not handle attributes yet.

onnx_inline_function

Inlines functions in an ONNX graph.

onnx_model_to_function

Converts an ONNX model into a function. The returned function has no attribute.

onnx_rename_inputs_outputs

Renames input or outputs names.

onnx_rename_names

Renames all names except the inputs and outputs.

onnx_replace_functions

Replaces some of the function in model.

onnx_subgraphs_level

Returns the depth of the graph.

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.

Methods#

method

truncated documentation

__init__

__setitem__

Adds a value.

copy

Returns a copy.

remove

Removes one element.

update

Updates many values.

Documentation#

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

source on GitHub

mlprodict.onnx_tools.onnx_manipulations._change_subgraph_io_type_shape_list(io_list, type_changes, shape_changes)#
mlprodict.onnx_tools.onnx_manipulations._get_new_name(prefix, name, existing_names)#
class mlprodict.onnx_tools.onnx_manipulations._inline_mapping(verbose, fLOG, level)#

Bases: dict

Overwrites class dictionary to debug more easily.

Parameters:
  • verbose – verbosity

  • fLOG – logging function

  • level – sub graph level

source on GitHub

__init__(verbose, fLOG, level)#
__setitem__(key, value)#

Adds a value.

copy()#

Returns a copy.

remove(o)#

Removes one element.

update(d)#

Updates many values.

mlprodict.onnx_tools.onnx_manipulations._onnx_function_to_model_convert_io(ens, type_info, shape_fct)#
mlprodict.onnx_tools.onnx_manipulations._onnx_inline_function_graph(graph, protos, existing_names, mapping, verbose, fLOG, rename, level)#
mlprodict.onnx_tools.onnx_manipulations._onnx_inline_function_node(node, protos, existing_names, verbose, fLOG, level)#
mlprodict.onnx_tools.onnx_manipulations.change_input_type(onx, changes)#

Changes the type of an input.

Parameters:
  • onx – ONNX model

  • changes – dictionary ‘{ name: new proto element type }`

Returns:

new onx

source on GitHub

mlprodict.onnx_tools.onnx_manipulations.change_subgraph_io_type_shape(onx, type_changes=None, shape_changes=None, recursive=True)#

Changes the type of an input or an output of a subgraph.

Parameters:
  • onx – ModelProto, GraphProto

  • type_changes – dictionary ‘{ name: new proto element type }` or function f(input) -> type

  • shape_changes – dictionary ‘{ name: new shape }` or function f(input) -> shape

  • recursive – True

Returns:

new onx

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.get_hidden_inputs(nodes)#

Returns the list of hidden inputs used by subgraphs.

Parameters:

nodes – list of nodes

Returns:

list of names

source on GitHub

mlprodict.onnx_tools.onnx_manipulations.get_opsets(model, include_functions=True, exc=True)#

Enumerates all opsets used in a model.

Parameters:
  • modelModelProto or FunctionProto

  • include_functions – include opsets used in functions

  • exc – raise an exception if conflicts are detected

Returns:

dictionary

source on GitHub

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

Hashes the content of an object. It uses module hashlib.

Parameters:
  • obj – onnx graph (it must have a method SerializeToString)

  • max_size – size of the hash

Returns:

hash

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 be 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_function_to_model(onx, functions=None, type_info=None, as_function=False, shape_fct=None)#

Converts an ONNX FunctionProto into a ModelProto. The function does not handle attributes yet.

Parameters:
  • onx – onnx function

  • functions – additional functions to append to the model

  • type_info – dictionary or callable which returns the type of inputs or outputs if it cannot be guessed

  • as_function – if True, the function stays as a function and a single node is created to call that function

  • shape_fct – function to specify the shapes, signature: shape_fct(name, proto_type) -> list

Returns:

function

source on GitHub

mlprodict.onnx_tools.onnx_manipulations.onnx_inline_function(obj, protos=None, existing_names=None, verbose=0, fLOG=None)#

Inlines functions in an ONNX graph.

Parameters:
  • obj – onnx graph, FunctionProto, :epkg:`GraphProto`, ModelProto

  • protos – if None, the function assumes obj is of type ModelProto and the goal is to inline every function. If protos a list of strings, the function only inlines the functions in that list. If protos is a dictionary { (domain, type): FunctionProto }, the function replaces every node (domain, type) by the code given in this dictionary

  • existing_names – no new name will be taken in that set

  • verbose – verbosity

  • fLOG – logging function

Returns:

modified object, list of modified nodes

New in version 0.9.

source on GitHub

mlprodict.onnx_tools.onnx_manipulations.onnx_model_to_function(onx, name=None, domain='custom', opset_imports=None, doc_string=None, inputs2par=None)#

Converts an ONNX model into a function. The returned function has no attribute.

Parameters:
  • onx – onnx model

  • name – function name

  • domain – function domain

  • opset_imports – opset to import as a dictionary {domain: version}

  • doc_string – doc string

  • inputs2par – dictionary to move some inputs as attributes { name: None or default value }

Returns:

function

Warning

FunctionProto does not support default values yet. They are ignored.

source on GitHub

mlprodict.onnx_tools.onnx_manipulations.onnx_rename_inputs_outputs(onx, rename)#

Renames input or outputs names.

Parameters:
  • onx – GraphProto, ModelProto

  • rename – dictionary {old_name: new_name}

Returns:

new onx

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.onnx_replace_functions(model, replace)#

Replaces some of the function in model.

Parameters:
  • modelModelProto

  • replace – dictionary { (domain, name): FunctionProto }

Returns:

new model

source on GitHub

mlprodict.onnx_tools.onnx_manipulations.onnx_subgraphs_level(obj)#

Returns the depth of the graph.

Parameters:

obj – onnx object

Returns:

integer

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)

source on GitHub