# onnx_tools

Python API: `modelconverter.utils.onnx_tools`

ONNX graph utilities shared by the conversion backends.

Holds the passes modelconverter applies to an ONNX model before handing it over to a platform's vendor toolchain: baking the
configured input normalization (channel reversal, mean subtraction and scaling) into the graph, and simplifying, optimizing and
fusing the graph with `onnxsim`, `onnxruntime` and `onnx_graphsurgeon`.

## Classes

| Name | Description |
| --- | --- |
| [`ONNXModifier`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/onnx_tools.md#onnx_tools-Classes-ONNXModifier) | ONNX model modifier class to optimize and modify the ONNX model. |

### class `ONNXModifier`

```python
class ONNXModifier
```

ONNX model modifier class to optimize and modify the ONNX model.

Attributes

| Name | Description |
| --- | --- |
| `model_path` | Path to the base ONNX model. |
| `output_path` | Path to save the modified ONNX model. |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/onnx_tools.md#onnx_tools-Classes-ONNXModifier-__init__) | Load the ONNX model and prepare it for modification. |
| [`compare_outputs`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/onnx_tools.md#onnx_tools-Classes-ONNXModifier-compare_outputs) | Compare the outputs of two ONNX models. |
| [`modify_onnx`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/onnx_tools.md#onnx_tools-Classes-ONNXModifier-modify_onnx) | Modify the ONNX model by applying a series of optimizations. |

#### method `__init__`

```python
def __init__(
    model_path: Path,
    output_path: Path,
    *,
    skip_optimization: bool = False,
    skip_constant_folding: bool = False,
)
```

Load the ONNX model and prepare it for modification.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `model_path` | `Path` | Path to the base ONNX model. | required |
| `output_path` | `Path` | Path to save the modified ONNX model to. | required |
| `skip_optimization` | `bool` | Whether to skip the graph optimization performed while loading and exporting the model. | `False` |
| `skip_constant_folding` | `bool` | Whether to skip constant folding during simplification. | `False` |

#### method `compare_outputs`

```python
def compare_outputs(*, from_modelproto: bool = False) -> bool
```

Compare the outputs of two ONNX models.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `from_modelproto` | `bool` | If `True`, compare the in-memory model against its previous state instead of comparing the model files on disk. | `False` |

Returns

| Type | Description |
| --- | --- |
| `bool` | `True` if the outputs of both models match, `False` otherwise. |

Raises

| Type | Description |
| --- | --- |
| `ONNXException` | If a model output is not a tensor. |

#### method `modify_onnx`

```python
def modify_onnx(
    *,
    substitute_sub_with_add: bool = True,
    substitute_div_with_mul: bool = True,
    fuse_add_mul_to_bn: bool = True,
    fuse_comb_add_mul_to_conv: bool = True,
    fuse_single_add_mul_to_conv: bool = True,
    fuse_split_concat_to_conv: bool = True,
) -> bool
```

Modify the ONNX model by applying a series of optimizations.

Each flag enables one step. A step that changes the outputs of the model is reverted.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `substitute_sub_with_add` | `bool` | Whether to substitute `Sub` nodes with `Add` nodes. | `True` |
| `substitute_div_with_mul` | `bool` | Whether to substitute `Div` nodes with `Mul` nodes. | `True` |
| `fuse_add_mul_to_bn` | `bool` | Whether to fuse `Add` and `Mul` nodes into `BatchNormalization` nodes. | `True` |
| `fuse_comb_add_mul_to_conv` | `bool` | Whether to fuse combinations of `Add` and `Mul` nodes into `Conv` nodes. | `True` |
| `fuse_single_add_mul_to_conv` | `bool` | Whether to fuse single `Add` and `Mul` nodes into `Conv` nodes. | `True` |
| `fuse_split_concat_to_conv` | `bool` | Whether to fuse `Split` and `Concat` nodes into `Conv` nodes. | `True` |

Returns

| Type | Description |
| --- | --- |
| `bool` | `True` if the model was modified and exported, `False` otherwise. |

## Functions

| Name | Description |
| --- | --- |
| [`get_opset_version`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/onnx_tools.md#onnx_tools-Functions-get_opset_version) | Return the default domain opset version of an ONNX model. |
| [`onnx_attach_normalization_to_inputs`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/onnx_tools.md#onnx_tools-Functions-onnx_attach_normalization_to_inputs) | Bake the input normalization into an ONNX model's graph. |

### function `get_opset_version`

```python
def get_opset_version(model: onnx.ModelProto) -> int
```

Return the default domain opset version of an ONNX model.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `model` | `onnx.ModelProto` | Model whose `opset_import` is searched. | required |

Returns

| Type | Description |
| --- | --- |
| `int` | Opset version declared for the default (empty) domain. |

Raises

| Type | Description |
| --- | --- |
| `ONNXException` | If the model declares no default domain opset. |

### function `onnx_attach_normalization_to_inputs`

```python
def onnx_attach_normalization_to_inputs(
    model_path: Path,
    save_path: Path,
    input_configs: dict[str, InputConfig],
    *,
    reverse_only: bool = False,
) -> Path
```

Bake the input normalization into an ONNX model's graph.

For every input that requires it, channel reversal (`Split` and `Concat`), mean subtraction (`Sub`) and scaling (`Mul` by the
reciprocal of the scale) nodes are inserted in front of the input, so that the graph itself computes

yc = (xc − meanc)⋅(1)/(scalec)

for every channel c. The resulting model is saved and validated with the ONNX checker. Mean and scale normalization supports any
known channel count; color-channel reversal requires exactly three channels. A scalar mean or scale broadcasts to every channel.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `model_path` | `Path` | Path to the source ONNX model. | required |
| `save_path` | `Path` | Path the modified model is saved to. | required |
| `input_configs` | `dict[str, InputConfig]` | Input configurations keyed by input name. | required |
| `reverse_only` | `bool` | If `True`, only the channel reversal is applied and the mean and scale values are ignored. | `False` |

Returns

| Type | Description |
| --- | --- |
| `Path` | `save_path` if any input required modification, otherwise the unmodified `model_path`. |

Raises

| Type | Description |
| --- | --- |
| `ONNXException` | If the preprocessing request or source model is invalid. |
| `PreprocessingEmbeddingError` | If valid preprocessing cannot be embedded into this ONNX graph. |

## Module Attributes

| Name | Description |
| --- | --- |
| [`FLOATING_TENSOR_TYPES`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/onnx_tools.md#onnx_tools-Module-Attributes-FLOATING_TENSOR_TYPES) |  |

### constant `FLOATING_TENSOR_TYPES`

```python
FLOATING_TENSOR_TYPES = frozenset(
    {TensorProto.FLOAT16, TensorProto.FLOAT, TensorProto.DOUBLE, TensorProto.BFLOAT16},
)
```
