Model¶

class onnx_ir.Model(graph, *, ir_version, producer_name=None, producer_version=None, domain=None, model_version=None, doc_string=None, functions=(), metadata_props=None, device_configurations=())¶
Parameters:
device_configurations¶
doc_string¶
domain¶
graph¶
ir_version¶
model_version¶
producer_name¶
producer_version¶
add_device_configuration(name, *, num_devices=None, device_names=())[source]¶

Create a ModelConfiguration and register it on the model.

The returned object can be passed to Node.shard() to bind node shardings to this configuration.

Added in version 1.0.0.

Parameters:
  • name (str) – A unique name for the configuration.

  • num_devices (int | None) – The number of devices. Defaults to len(device_names). Must be >= 1.

  • device_names (Sequence[str]) – Optional device names (e.g. ("CPU", "CUDA:0")). These are human-readable names; the device indices used by Node.shard() index into this sequence. When provided, the number of names must equal num_devices.

Returns:

The newly created configuration, already appended to device_configurations.

Raises:

ValueError – If a configuration with name already exists, if name is empty, if num_devices < 1, or if device_names is given and its length does not equal num_devices.

Return type:

ModelConfiguration

clone(deep_copy=False)[source]¶

Create a deep copy of this model.

All graphs, nodes, values, and subgraphs are cloned. The cloned model will have the same structure as this model, but all graphs, nodes, and values will be different objects.

Tensors in initializers and constant values will be shared.

Added in version 0.1.14.

Parameters:

deep_copy (bool) – When True, performs a deep copy of the meta data stores.

Returns:

A deep copy of this model.

Return type:

Model

display(*, page=False)¶

Pretty print the object.

Parameters:

page (bool) – Whether to page the output.

Return type:

None

property functions: dict[tuple[str, str, str], Function]¶
graphs()[source]¶

Get all graphs and subgraphs in the model.

This is a convenience method to traverse the model. Consider using onnx_ir.traversal.RecursiveGraphIterator for more advanced traversals on nodes.

Return type:

Iterable[Graph]

property meta: MetadataStore¶

The metadata store for intermediate analysis.

Write to the metadata_props if you would like the metadata to be serialized to the ONNX proto.

property metadata_props: dict[str, str]¶

The metadata properties of the model.

The metadata properties are used to store additional information about the model. Unlike meta, this property is serialized to the ONNX proto.

property opset_imports: dict[str, int]¶
remove_device_configuration(configuration, *, cascade=False)[source]¶

Remove a device configuration from the model.

This is the counterpart of add_device_configuration().

Added in version 1.0.0.

Parameters:
  • configuration (ModelConfiguration | str) – The ModelConfiguration object to remove, or the name of the configuration to remove.

  • cascade (bool) – When True, also remove every NodeDeviceConfiguration that references this configuration from all nodes in the model’s graph and functions, so that no dangling references remain. If removal was requested by name, node configurations bound to any same-named configuration object are dropped too; if requested by object, only that exact object is matched. When False (default), node references are left intact; they become dangling and can be detected by the internal device-configuration checker.

Returns:

The removed configuration.

Raises:

ValueError – If no matching configuration is registered on the model.

Return type:

ModelConfiguration