Implementation:Onnx Onnx Compose Add Prefix
| Knowledge Sources | |
|---|---|
| Domains | Model_Composition, Name_Management |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for adding namespace prefixes to ONNX model names provided by the ONNX compose module.
Description
The add_prefix function adds a prefix string to all named elements in an ONNX model, including nodes, edges, inputs, outputs, initializers, value infos, and local functions. Each category of names can be selectively enabled or disabled via boolean parameters. The function can operate in-place or return a copy. Internally, it delegates to add_prefix_graph for graph-level renaming and handles function renaming separately.
Usage
Import this function when preparing models for composition via merge_models. Apply distinct prefixes to each model before merging to prevent name collisions. Empty names are not prefixed.
Code Reference
Source Location
- Repository: onnx
- File: onnx/compose.py
- Lines: 564-635
Signature
def 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.
Args:
model: Model to prefix.
prefix: Prefix string to prepend to each name.
rename_nodes: Whether to prefix node names.
rename_edges: Whether to prefix edge (input/output) names on nodes.
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.
rename_functions: Whether to prefix local function names.
inplace: If True, mutate the model directly; otherwise copy first.
Returns:
ModelProto with prefixed names.
"""
Import
from onnx import compose
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | ModelProto | Yes | Model whose names to prefix |
| prefix | str | Yes | Prefix string to prepend |
| rename_nodes | bool | No | Prefix node names (default: True) |
| rename_edges | bool | No | Prefix edge names (default: True) |
| rename_inputs | bool | No | Prefix graph input names (default: True) |
| rename_outputs | bool | No | Prefix graph output names (default: True) |
| rename_initializers | bool | No | Prefix initializer names (default: True) |
| rename_value_infos | bool | No | Prefix value info names (default: True) |
| rename_functions | bool | No | Prefix local function names (default: True) |
| inplace | bool | No | Mutate directly or copy (default: False) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | ModelProto | Model with all selected names prefixed |
Usage Examples
Basic Prefixing
import onnx
from onnx import compose
model = onnx.load_model("model_a.onnx")
# Add "m1/" prefix to all names
prefixed = compose.add_prefix(model, prefix="m1/")
# Verify input names are prefixed
for inp in prefixed.graph.input:
print(inp.name) # e.g., "m1/X", "m1/W"
Selective Prefixing
from onnx import compose
# Only prefix internal names, keep inputs/outputs unchanged
prefixed = compose.add_prefix(
model,
prefix="net1_",
rename_inputs=False,
rename_outputs=False,
)