compose.h#

Declares helpers to combine and manipulate ONNX graphs and models.

These functions mirror onnx.compose from the upstream ONNX Python package and are used to add name prefixes, check for overlapping names, merge graphs and models, and expand output dimensions.

namespace onnx_light

Alias that makes onnx-light headers compatible with code that references ONNX_LIGHT_NAMESPACE (the macro used in the standard onnx package).

Set to ONNX_LIGHT_NAMESPACE so both names resolve to the same namespace.

Symbol-visibility attribute for the public onnx-light C++ API.

Defined as empty because onnx-light does not require explicit __declspec(dllexport) or __attribute__((visibility("default"))) annotations — visibility is controlled at the shared-library level. The macro is provided so that vendored ONNX headers that decorate their declarations with ONNX_API compile without modification.

Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal onnx namespace — rather than the ONNX_NAMESPACE macro — resolves to the onnx-light namespace. The standard onnx package lives in namespace onnx; onnx-light uses onnx_light (via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs from onnx.

Functions

std::vector<std::pair<std::string, std::vector<std::string>>> CheckOverlappingNames(const GraphProto &g1, const GraphProto &g2, const std::vector<std::pair<std::string, std::string>> &io_map = {})#

Checks whether two graphs have overlapping names.

Returns a list of (category, names) pairs for each category where names appear in both g1 and g2. Recognised categories are "edge", "value_info", "initializer" and "sparse_initializer".

The optional io_map lists output/input pairs that are intentionally shared between the two graphs; those overlaps are excluded from the result.

GraphProto AddPrefixGraph(const GraphProto &graph, const std::string &prefix, bool rename_nodes = true, bool rename_edges = true, bool rename_inputs = true, bool rename_outputs = true, bool rename_initializers = true, bool rename_value_infos = true, bool inplace = false, std::unordered_map<std::string, std::string> *name_map = nullptr)#

Adds a prefix to names of elements in a graph.

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

Parameters:
  • graphGraph to prefix.

  • prefix – Prefix string to prepend.

  • rename_nodes – Whether to prefix node names.

  • rename_edges – Whether to prefix node edge (input/output) names.

  • rename_inputs – Whether to prefix graph-input names.

  • rename_outputs – Whether to prefix graph-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 operates on a copy.

  • name_map – Optional shared name map that accumulates all renames across recursive subgraph calls.

Returns:

The (possibly new) GraphProto with prefixed names.

ModelProto AddPrefix(const ModelProto &model, const std::string &prefix, bool rename_nodes = true, bool rename_edges = true, bool rename_inputs = true, bool rename_outputs = true, bool rename_initializers = true, bool rename_value_infos = true, bool rename_functions = true, bool inplace = false)#

Adds a prefix to names of elements in a model.

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

Parameters:
  • model – Model to prefix.

  • prefix – Prefix string to prepend.

  • 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 operates on a copy.

Returns:

The (possibly new) ModelProto with prefixed names.

GraphProto MergeGraphs(const GraphProto &g1, const GraphProto &g2, const std::vector<std::pair<std::string, std::string>> &io_map, const std::vector<std::string> &inputs = {}, const std::vector<std::string> &outputs = {}, const std::string &prefix1 = "", const std::string &prefix2 = "", const std::string &name = "", const std::string &doc_string = "")#

Combines two ONNX graphs into a single one.

The combined graph is defined by connecting the specified set of outputs/inputs from g1 to inputs of g2 as listed in io_map. Inputs/outputs not present in io_map remain as inputs/outputs of the combined graph.

Parameters:
  • g1 – First graph.

  • g2 – Second graph.

  • io_map – Pairs [(out_name, in_name), …] mapping outputs of g1 to inputs of g2.

  • inputs – Optional list of inputs to include. When absent all inputs not in io_map are included.

  • outputs – Optional list of outputs to include. When absent all outputs not in io_map are included.

  • prefix1 – Optional prefix for all names in g1.

  • prefix2 – Optional prefix for all names in g2.

  • name – Optional name for the combined graph.

  • doc_string – Optional doc-string for the combined graph.

Returns:

Combined GraphProto.

ModelProto MergeModels(const ModelProto &m1, const ModelProto &m2, const std::vector<std::pair<std::string, std::string>> &io_map, const std::vector<std::string> &inputs = {}, const std::vector<std::string> &outputs = {}, const std::string &prefix1 = "", const std::string &prefix2 = "", const std::string &name = "", const std::string &doc_string = "", const std::string &producer_name = "onnx_light.onnx.compose.merge_models", const std::string &producer_version = "1.0", const std::string &domain = "", int64_t model_version = 1)#

Combines two ONNX models into a single one.

Both models must share the same IR version and operator sets.

Parameters:
  • m1 – First model.

  • m2 – Second model.

  • io_map – Output/input pairs 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 doc-string for the combined graph.

  • producer_name – Producer name for the combined model.

  • producer_version – Producer version string.

  • domain – Domain of the combined model.

  • model_version – Version of the combined model.

Returns:

Combined ModelProto.

GraphProto ExpandOutDimGraph(const GraphProto &graph, int64_t dim_idx, bool inplace = false)#

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

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

Parameters:
  • graphGraph to modify.

  • dim_idx – Index at which the new dimension is inserted. Negative values count from the back.

  • inplace – If true, mutates graph in place; otherwise operates on a copy.

Returns:

The (possibly new) GraphProto with expanded output dimensions.

ModelProto ExpandOutDim(const ModelProto &model, int64_t dim_idx, bool inplace = false)#

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

Parameters:
  • model – Model to modify.

  • dim_idx – Index at which the new dimension is inserted.

  • inplace – If true, mutates model in place; otherwise operates on a copy.

Returns:

The (possibly new) ModelProto with expanded output dimensions.