onnx_light.onnx.compose#

onnx_light.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#

Adds a prefix to names of elements in a model.

Applies the prefix to graph nodes, edges, inputs, outputs, initializers, sparse initializers, value infos, and local functions as requested. Empty names are not prefixed.

Parameters:
  • model – The model to prefix.

  • prefix – Prefix string to prepend to each name.

  • rename_nodes – Whether to prefix node names.

  • rename_edges – Whether to prefix node edge names.

  • rename_inputs – Whether to prefix input names.

  • rename_outputs – Whether to prefix output names.

  • rename_initializers – Whether to prefix initializer names.

  • rename_value_infos – Whether to prefix value info names.

  • rename_functions – Whether to prefix local function names.

  • inplace – If True, mutates model in place; otherwise a copy is made.

Returns:

The (possibly new) ModelProto with prefixed names.

onnx_light.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#

Adds a prefix to names of elements in a graph.

Applies the prefix to nodes, edges, inputs, outputs, initializers, sparse initializers, and value infos as requested. Empty names are not prefixed.

Parameters:
  • graph – The graph to prefix.

  • prefix – Prefix string to prepend to each name.

  • rename_nodes – Whether to prefix node names.

  • rename_edges – Whether to prefix node edge names.

  • rename_inputs – Whether to prefix input names.

  • rename_outputs – Whether to prefix output names.

  • rename_initializers – Whether to prefix initializer names.

  • rename_value_infos – Whether to prefix value info names.

  • inplace – If True, mutates graph in place; otherwise a copy is made.

  • name_map – Shared name-map used when recursing into subgraphs.

Returns:

The (possibly new) GraphProto with prefixed names.

onnx_light.onnx.compose.check_overlapping_names(g1: GraphProto, g2: GraphProto, io_map: list[tuple[str, str]] | None = None) list[tuple[str, list[str]]]#

Checks whether there are name collisions between two graphs.

Returns a list of tuples where the first element represents the member containing overlapping names (one of: "edge", "value_info", "initializer", "sparse_initializer"), and the second element contains a list of names that appear in both graphs under that category.

Optionally takes an io_map representing the output/input pairs to be connected. Overlaps present in the io_map are ignored.

onnx_light.onnx.compose.expand_out_dim(model: ModelProto, dim_idx: int, inplace: bool | None = False) ModelProto#

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

Inserts an Unsqueeze node for each output. Useful before merging models when the second one expects a batch dimension.

Parameters:
  • model – The model to modify.

  • dim_idx – Index of the dimension to insert. Negative values count from the back.

  • inplace – If True, mutates model in place; otherwise a copy is made.

Returns:

The (possibly new) ModelProto with expanded output dimensions.

onnx_light.onnx.compose.expand_out_dim_graph(graph: GraphProto, dim_idx: int, inplace: bool | None = False) GraphProto#

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

Inserts an Unsqueeze node for each output. Useful before merging graphs when the second graph expects a batch dimension.

Parameters:
  • graph – The graph to modify.

  • dim_idx – Index of the dimension to insert. Negative values count from the back.

  • inplace – If True, mutates graph in place; otherwise a copy is made.

Returns:

The (possibly new) GraphProto with expanded output dimensions.

onnx_light.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#

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 io_map will remain as inputs/outputs of the combined graph.

Parameters:
  • g1 – First graph.

  • g2 – Second graph.

  • io_map – Pairs [(out0, in0), ...] mapping outputs of g1 to inputs of g2 to be connected.

  • inputs – Optional list of inputs to include in the combined graph. By default all inputs not present in io_map are included.

  • outputs – Optional list of outputs to include in the combined graph. By default all outputs not present in io_map are included.

  • prefix1 – Optional prefix added to all names in g1.

  • prefix2 – Optional prefix added to all names in g2.

  • name – Optional name for the combined graph.

  • doc_string – Optional docstring for the combined graph.

Returns:

Combined GraphProto.

onnx_light.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_light.onnx.compose.merge_models', producer_version: str | None = '1.0', domain: str | None = '', model_version: int | None = 1) ModelProto#

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 io_map will remain as inputs/outputs of the combined model.

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

Parameters:
  • m1 – First model.

  • m2 – Second model.

  • io_map – Pairs [(out0, in0), ...] mapping outputs of m1 to inputs of m2.

  • inputs – Optional list of inputs for the combined model.

  • outputs – Optional list of outputs for the combined model.

  • prefix1 – Optional prefix for all names in m1.

  • prefix2 – Optional prefix for all names in m2.

  • name – Optional name for the combined graph.

  • doc_string – Optional docstring for the combined graph.

  • producer_name – Producer name for the combined model.

  • producer_version – Producer version for the combined model.

  • domain – Domain of the combined model.

  • model_version – Version of the combined model.

Returns:

Combined ModelProto.