---
title: "OnnxModel"
sidebar_label: "OnnxModel"
description: "The clika_runtime.io OnnxModel class."
---

<!-- Generated by tools/api_reference/generate_api_docs.py. Do not edit. -->

An ONNX model file as an editable graph: open or create one, or build one from a ModelGraph (from_graph), inspect and edit it, save it, or compile() it into a ModelGraph.

## `ir_version` (property)

The ONNX IR version.

## `num_initializers` (property)

The initializer (graph constant) count.

## `num_nodes` (property)

The node count.

## `opset` (property)

The ai.onnx opset version.

## `__init__`

```python
__init__(self, /, *args, **kwargs)
```

Initialize self.  See help(type(self)) for accurate signature.

## `add_initializer`

```python
add_initializeradd_initializer(self, value: clika_runtime._core.Tensor) -> str
```

add_initializer(self, value: clika_runtime._core.Tensor) -> str

add_initializer(value) -> str

Create a graph constant from a tensor's bytes; returns the generated initializer name.

## `add_input`

```python
add_inputadd_input(self, name: str, dtype: clika_runtime._core.DataType, shape: collections.abc.Sequence[int]) -> str
```

add_input(self, name: str, dtype: clika_runtime._core.DataType, shape: collections.abc.Sequence[int]) -> str

add_input(name, dtype, shape) -> str

Register a graph input; any dim <= 0 declares a dynamic dimension. Returns the tensor name.

## `add_node`

```python
add_nodeadd_node(self, op_type: str, inputs: collections.abc.Sequence[str], num_outputs: int = 1, attrs: dict = {}, domain: str = '', output_names: collections.abc.Sequence[str] = []) -> list[str]
```

add_node(self, op_type: str, inputs: collections.abc.Sequence[str], num_outputs: int = 1, attrs: dict = {}, domain: str = '', output_names: collections.abc.Sequence[str] = []) -> list[str]

add_node(op_type, inputs, num_outputs=1, attrs={}, domain='', output_names=()) -> list[str]

Append a node: positional input tensor names in, generated (or caller-named) output names back. Attributes ride a dict of int / float / str / list values. A `domain` other than the standard one must be declared first with add_opset_domain(); a node under a domain the model does not import raises InvalidArgumentError and leaves the model as it was.

## `add_opset_domain`

```python
add_opset_domainadd_opset_domain(self, domain: str, version: int) -> None
```

add_opset_domain(self, domain: str, version: int) -> None

add_opset_domain(domain, version) -> None

Declare an opset import for a non-standard domain (for example 'com.microsoft', 1), before add_node() adds a node under that domain.

## `add_output`

```python
add_outputadd_output(self, name: str, dtype: clika_runtime._core.DataType, shape: collections.abc.Sequence[int]) -> str
```

add_output(self, name: str, dtype: clika_runtime._core.DataType, shape: collections.abc.Sequence[int]) -> str

add_output(name, dtype, shape) -> str

Register a graph output. Returns the tensor name.

## `compile`

```python
compilecompile(self, options: object | None = None) -> clika_runtime._core.io.ModelGraph
```

compile(self, options: object | None = None) -> clika_runtime._core.io.ModelGraph

compile(options=None) -> ModelGraph

Build the executable form: every node parses into its runtime operator (an unsupported op fails here, naming it), weights bind, shapes resolve. `options` (a CompileOptions) pins input shapes and names the weights' device. Returns the ModelGraph as built: optimize() it when wanted, then finalize() it to run. The OnnxModel is unchanged and reusable.

## `create`

```python
createcreate(graph_name: str, opset_version: int = 0) -> clika_runtime._core.io.OnnxModel
```

create(graph_name: str, opset_version: int = 0) -> clika_runtime._core.io.OnnxModel

create(graph_name, opset_version=0) -> OnnxModel

Create an empty model at the given ai.onnx opset (0 = the library default); build it up with add_input / add_initializer / add_node / add_output.

## `from_graph`

```python
from_graphfrom_graph(graph: object | None, *, opset_version: int = 0, contrib_ops: bool = True, optimize: bool = True, save_as_external_data: bool | None = None, producer_name: str = '') -> clika_runtime._core.io.OnnxModel
```

from_graph(graph: object | None, *, opset_version: int = 0, contrib_ops: bool = True, optimize: bool = True, save_as_external_data: bool | None = None, producer_name: str = '') -> clika_runtime._core.io.OnnxModel

from_graph(graph, *, opset_version=0, contrib_ops=True, optimize=True, save_as_external_data=None, producer_name='') -> OnnxModel

Build the ONNX model of `graph`, a ModelGraph, in memory, with nothing written: the model graph.export_onnx(path, ...) would write to its file. Inspect it, edit it, optimize(), save() or compile() it like any other model. It reads the graph's weights only when it is saved or compiled, so it holds no copy of them, and it keeps them alive for as long as it lives. The options are ModelGraph.export_onnx()'s. Raises what export_onnx() raises, and TypeError for a graph that is not a ModelGraph.

## `inputs`

```python
inputsinputs(self) -> list
```

inputs(self) -> list

inputs() -> list[tuple[str, DataType, tuple[int, ...]]]

Graph inputs as (name, dtype, dims) tuples; a dynamic dim reads as -1.

## `merge`

```python
mergemerge(self, second: object | None, io_map: collections.abc.Sequence[tuple[str, str]] = []) -> None
```

merge(self, second: object | None, io_map: collections.abc.Sequence[tuple[str, str]] = []) -> None

merge(second, io_map=()) -> None

Combine `second`, an OnnxModel, into this model. Each io_map entry (this_output, second_input) connects one of this model's outputs to one of second's inputs; the connected outputs stop being outputs and the connected inputs stop being inputs. The combined model's inputs are this model's, then second's unconnected ones, and its outputs second's, then this model's unconnected ones. A name of second's that this model already uses is renamed, and the opset imports merge to the highest version per domain. The call consumes `second` whether it merges or refuses, and leaves it empty. The one exception is second being this model, which is refused before anything moves. Raises InvalidArgumentError when the two cannot combine (an io_map name that names no output or input, second being this model, an empty model), and TypeError for a second that is not an OnnxModel.

## `nodes`

```python
nodesnodes(self) -> list[clika_runtime._core.io.OnnxNodeInfo]
```

nodes(self) -> list[clika_runtime._core.io.OnnxNodeInfo]

nodes() -> list[OnnxNodeInfo]

The graph's nodes in graph order, each with its name, op type, domain and the input and output tensor names; a node's name is what remove_node(name) takes.

## `open`

```python
openopen(path: str) -> clika_runtime._core.io.OnnxModel
```

open(path: str) -> clika_runtime._core.io.OnnxModel

open(path) -> OnnxModel

Open a .onnx file (initializer payloads referenced as external data stay on disk beside it).

## `optimize`

```python
optimizeoptimize(self) -> int
```

optimize(self) -> int

optimize() -> int

Run the graph-rewrite pipeline to a fixed point; returns the pass count.

## `outputs`

```python
outputsoutputs(self) -> list
```

outputs(self) -> list

outputs() -> list[tuple[str, DataType, tuple[int, ...]]]

Graph outputs as (name, dtype, dims) tuples.

## `remove_node`

```python
remove_noderemove_node(self, name: str) -> None
```

remove_node(self, name: str) -> None

remove_node(name) -> None

Remove the node named `name` (an error if absent).

## `save`

```python
savesave(self, path: str) -> None
```

save(self, path: str) -> None

save(path) -> None

Assemble the model and write it to `path`.
