onnx.compose#

merge_graphs(g1, g2, io_map[, inputs, ...])

Combines two ONNX graphs into a single one.

merge_models(m1, m2, io_map[, inputs, ...])

Combines two ONNX models into a single one.

merge_graphs#

onnx.compose.merge_graphs(g1: GraphProto, g2: GraphProto, io_map: List[Tuple[str, str]], inputs: Optional[List[str]] = None, outputs: Optional[List[str]] = None, prefix1: Optional[str] = None, prefix2: Optional[str] = None, name: Optional[str] = None, doc_string: Optional[str] = None) GraphProto[source]#

Combines two ONNX graphs into a single one.

The combined graph is defined by connecting the specified set of outputs/inputs. Those inputs/outputs not specified in the io_map argument will remain as inputs/outputs of the combined graph.

Parameters:
  • g1 (GraphProto) – First graph

  • g2 (GraphProto) – Second graph

  • io_map (list of pairs of string) – The pairs of names [(out0, in0), (out1, in1), …] representing outputs of the first graph and inputs of the second to be connected

  • inputs (list of string) – Optional list of inputs to be included in the combined graph By default, all inputs not present in the io_map argument will be included in the combined model

  • outputs (list of string) – Optional list of outputs to be included in the combined graph By default, all outputs not present in the io_map argument will be included in the combined model

  • prefix1 (string) – Optional prefix to be added to all names in g1

  • prefix2 (string) – Optional prefix to be added to all names in g2

  • name (string) – Optional name for the combined graph By default, the name is g1.name and g2.name concatenated with an undescore delimiter

  • doc_string (string) – Optional docstring for the combined graph If not provided, a default docstring with the concatenation of g1 and g2 docstrings is used

Returns:

GraphProto

merge_models#

onnx.compose.merge_models(m1: ModelProto, m2: ModelProto, io_map: List[Tuple[str, str]], inputs: Optional[List[str]] = None, outputs: Optional[List[str]] = None, prefix1: Optional[str] = None, prefix2: Optional[str] = None, name: Optional[str] = None, doc_string: Optional[str] = None, producer_name: Optional[str] = 'onnx.compose.merge_models', producer_version: Optional[str] = '1.0', domain: Optional[str] = '', model_version: Optional[int] = 1) ModelProto[source]#

Combines two ONNX models into a single one.

The combined model is defined by connecting the specified set of outputs/inputs. Those inputs/outputs not specified in the io_map argument will remain as inputs/outputs of the combined model.

Both models should have the same IR version, and same operator sets imported.

Parameters:
  • m1 (ModelProto) – First model

  • m2 (ModelProto) – Second model

  • io_map (list of pairs of string) – The pairs of names [(out0, in0), (out1, in1), …] representing outputs of the first graph and inputs of the second to be connected

  • inputs (list of string) – Optional list of inputs to be included in the combined graph By default, all inputs not present in the io_map argument will be included in the combined model

  • outputs (list of string) – Optional list of outputs to be included in the combined graph By default, all outputs not present in the io_map argument will be included in the combined model

  • prefix1 (string) – Optional prefix to be added to all names in m1

  • prefix2 (string) – Optional prefix to be added to all names in m2

  • name (string) – Optional name for the combined graph By default, the name is g1.name and g2.name concatenated with an undescore delimiter

  • doc_string (string) – Optional docstring for the combined graph If not provided, a default docstring with the concatenation of g1 and g2 docstrings is used

  • producer_name (string) – Optional producer name for the combined model. Default: ‘onnx.compose’

  • producer_version (string) – Optional producer version for the combined model. Default: “1.0”

  • domain (string) – Optional domain of the combined model. Default: “”

  • model_version (int) – Optional version of the graph encoded. Default: 1

Returns:

ModelProto

prefix#

onnx.compose.add_prefix_graph(graph: GraphProto, prefix: str, rename_nodes: Optional[bool] = True, rename_edges: Optional[bool] = True, rename_inputs: Optional[bool] = True, rename_outputs: Optional[bool] = True, rename_initializers: Optional[bool] = True, rename_value_infos: Optional[bool] = True, inplace: Optional[bool] = False) GraphProto[source]#

Adds a prefix to names of elements in a graph: nodes, edges, inputs, outputs, initializers, sparse initializer, value infos.

It can be used as a utility before merging graphs that have overlapping names. Empty names are not prefixed.

Parameters:
  • graph (GraphProto) – Graph

  • prefix (str) – Prefix to be added to each name in the graph

  • rename_nodes (bool) – Whether to prefix node names

  • rename_edges (bool) – Whether to prefix node edge names

  • rename_inputs (bool) – Whether to prefix input names

  • rename_outputs (bool) – Whether to prefix output names

  • rename_initializers (bool) – Whether to prefix initializer and sparse initializer names

  • rename_value_infos (bool) – Whether to prefix value info names

  • inplace (bool) – If True, mutates the graph directly. Otherwise, a copy will be created

Returns:

GraphProto

onnx.compose.add_prefix(model: ModelProto, prefix: str, rename_nodes: Optional[bool] = True, rename_edges: Optional[bool] = True, rename_inputs: Optional[bool] = True, rename_outputs: Optional[bool] = True, rename_initializers: Optional[bool] = True, rename_value_infos: Optional[bool] = True, rename_functions: Optional[bool] = True, inplace: Optional[bool] = False) ModelProto[source]#

Adds a prefix to names of elements in a graph: nodes, edges, inputs, outputs, initializers, sparse initializer, value infos, and local functions.

It can be used as a utility before merging graphs that have overlapping names. Empty names are not _prefixed.

Parameters:
  • model (ModelProto) – Model

  • prefix (str) – Prefix to be added to each name in the graph

  • rename_nodes (bool) – Whether to prefix node names

  • rename_edges (bool) – Whether to prefix node edge names

  • rename_inputs (bool) – Whether to prefix input names

  • rename_outputs (bool) – Whether to prefix output names

  • rename_initializers (bool) – Whether to prefix initializer and sparse initializer names

  • rename_value_infos (bool) – Whether to prefix value info nanes

  • rename_functions (bool) – Whether to prefix local function names

  • inplace (bool) – If True, mutates the model directly. Otherwise, a copy will be created

Returns:

ModelProto

dimension#

onnx.compose.expand_out_dim(model: ModelProto, dim_idx: int, inplace: Optional[bool] = False) ModelProto[source]#

Inserts an extra dimension with extent 1 to each output in the graph.

Inserts an Unsqueeze node for each output. It can be used as a utility before merging graphs, for example when the second one expects a batch dimension.

Parameters:
  • model (ModelProto) – Model

  • dim_idx (int) – Index of the dimension to be inserted. A negative value means counting dimensions from the back.

  • inplace (bool) – If True, mutates the model directly. Otherwise, a copy will be created

Returns:

ModelProto

onnx.compose.expand_out_dim_graph(graph: GraphProto, dim_idx: int, inplace: Optional[bool] = False) GraphProto[source]#

Inserts an extra dimension with extent 1 to each output in the graph.

Inserts an Unsqueeze node for each output. It can be used as a utility before merging graphs, for example when the second one expects a batch dimension.

Parameters:
  • graph (GraphProto) – Graph

  • dim_idx (int) – Index of the dimension to be inserted. A negative value means counting dimensions from the back.

  • inplace (bool) – If True, mutates the model directly. Otherwise, a copy will be created

Returns:

GraphProto