# onnx_compatibility

Python API: `modelconverter.utils.onnx_compatibility`

Helpers smoothing over differences between ONNX releases.

ONNX is the input format every platform can take, so modelconverter has to cope with whatever the ONNX package in a given image
offers: helper functions come and go between releases, and a model too large for a single protobuf keeps its weights in companion
files. This module patches the missing helpers back in and gathers the external-data handling the rest of the package needs.

## Functions

| Name | Description |
| --- | --- |
| [`ensure_onnx_helper_compatibility`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/onnx_compatibility.md#onnx_compatibility-Functions-ensure_onnx_helper_compatibility) | Patch the scalar conversion helpers back into `onnx.helper`. |
| [`get_external_data_paths`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/onnx_compatibility.md#onnx_compatibility-Functions-get_external_data_paths) | Return every companion file holding the model's external tensor data. |
| [`has_external_data`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/onnx_compatibility.md#onnx_compatibility-Functions-has_external_data) | Whether the model keeps any of its tensors in companion files. |
| [`save_onnx_model`](https://docs.luxonis.com/software-v3/ai-inference/conversion/rvc-conversion/offline/modelconverter/modelconverter-api-reference/utils/onnx_compatibility.md#onnx_compatibility-Functions-save_onnx_model) | Save an ONNX model, optionally with its tensors kept beside it. |

### function `ensure_onnx_helper_compatibility`

```python
def ensure_onnx_helper_compatibility()
```

Patch the scalar conversion helpers back into `onnx.helper`.

ONNX 1.21 dropped `float32_to_bfloat16` and `float32_to_float8e4m3`, which ONNX GraphSurgeon still imports. Both are reinstated
here, implemented on top of `ml_dtypes`, and an ONNX release that still provides them is left untouched.

### function `get_external_data_paths`

```python
def get_external_data_paths(model_path: PathType) -> list[Path]
```

Return every companion file holding the model's external tensor data.

A model saved with `all_tensors_to_one_file=False` keeps one file per tensor, so there is not necessarily just one.

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If a tensor's external data location resolves to a path outside the model's directory. |

### function `has_external_data`

```python
def has_external_data(model_path: PathType) -> bool
```

Whether the model keeps any of its tensors in companion files.

Callers that re-save the model consolidate every tensor into one new file, so they only need to know whether there is external
data -- not where it currently lives.

Returns

| Type | Description |
| --- | --- |
| `bool` | `True` if any tensor of the model has its data in a companion file. |

### function `save_onnx_model`

```python
def save_onnx_model(
    model: onnx.ModelProto,
    output_path: PathType,
    *,
    save_as_external_data: bool = False,
    location: str | None = None,
)
```

Save an ONNX model, optionally with its tensors kept beside it.

When external data is requested, the initializers are written into a single companion file next to the model, replacing an earlier
file of the same name. Tensors held in node attributes stay in the model file.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `model` | `onnx.ModelProto` | Model to save. | required |
| `output_path` | `PathType` | Path to write the model to. | required |
| `save_as_external_data` | `bool` | Whether to store the tensor data in a companion file instead of in the model file itself. | `False` |
| `location` | `str | None` | Name of that companion file. Defaults to the model's file name with `_data` appended. | `None` |
