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: list[str] | None = None, outputs: list[str] | None = None, prefix1: str | None = None, prefix2: str | None = None, name: str | None = None, doc_string: str | None = 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: list[str] | None = None, outputs: list[str] | None = None, prefix1: str | None = None, prefix2: str | None = None, name: str | None = None, doc_string: str | None = None, producer_name: str | None = 'onnx.compose.merge_models', producer_version: str | None = '1.0', domain: str | None = '', model_version: int | None = 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: bool | None = True, rename_edges: bool | None = True, rename_inputs: bool | None = True, rename_outputs: bool | None = True, rename_initializers: bool | None = True, rename_value_infos: bool | None = True, inplace: bool | None = False, name_map: dict[str, str] | None = None) 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

  • name_map – (Dict): shared name_map in subgraph

Returns:

GraphProto

onnx.compose.add_prefix(model: ModelProto, prefix: str, rename_nodes: bool | None = True, rename_edges: bool | None = True, rename_inputs: bool | None = True, rename_outputs: bool | None = True, rename_initializers: bool | None = True, rename_value_infos: bool | None = True, rename_functions: bool | None = True, inplace: bool | None = 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: bool | None = 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: bool | None = 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