# 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

### 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.

### 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.

### 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.

### 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.

Parameters

 * `model` (`onnx.ModelProto`): Model to save.
 * `output_path` (`PathType`): Path to write the model to.
 * `save_as_external_data` (`bool`): Whether to store the tensor data in a companion file instead of in the model file itself.
 * `location` (`str | None`): Name of that companion file. Defaults to the model's file name with `_data` appended.
