> For the complete documentation index, see [llms.txt](https://boinc-ai.gitbook.io/optimum/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boinc-ai.gitbook.io/optimum/onnx-runtime/reference/optimization.md).

# Optimization

## Optimization

### ORTOptimizer

#### class optimum.onnxruntime.ORTOptimizer

[\<source>](https://github.com/huggingface/optimum/blob/main/optimum/onnxruntime/optimization.py#L45)

( onnx\_model\_path: typing.List\[os.PathLike]config: PretrainedConfigfrom\_ortmodel: bool = False )

Handles the ONNX Runtime optimization process for models shared on boincai.com/models.

**from\_pretrained**

[\<source>](https://github.com/huggingface/optimum/blob/main/optimum/onnxruntime/optimization.py#L74)

( model\_or\_path: typing.Union\[str, os.PathLike, optimum.onnxruntime.modeling\_ort.ORTModel]file\_names: typing.Optional\[typing.List\[str]] = None )

Parameters

* **model\_or\_path** (`Union[str, os.PathLike, ORTModel]`) — The path to a local directory hosting the model to optimize or an instance of an `ORTModel` to quantize. Can be either:
  * A path to a local *directory* containing the model to optimize.
  * An instance of [ORTModel](https://huggingface.co/docs/optimum/main/en/onnxruntime/package_reference/modeling_ort#optimum.onnxruntime.ORTModel).
* **file\_names(`Optional[List[str]]`,** defaults to `None`) — The list of file names of the models to optimize.

**get\_fused\_operators**

[\<source>](https://github.com/huggingface/optimum/blob/main/optimum/onnxruntime/optimization.py#L248)

( onnx\_model\_path: typing.Union\[str, os.PathLike] )

Parameters

* **onnx\_model\_path** (`Union[str, os.PathLike]`) — Path of the ONNX model.

Computes the dictionary mapping the name of the fused operators to their number of apparition in the model.

**get\_nodes\_number\_difference**

[\<source>](https://github.com/huggingface/optimum/blob/main/optimum/onnxruntime/optimization.py#L267)

( onnx\_model\_path: typing.Union\[str, os.PathLike]onnx\_optimized\_model\_path: typing.Union\[str, os.PathLike] )

Parameters

* **onnx\_model\_path** (`Union[str, os.PathLike]`) — Path of the ONNX model.
* **onnx\_optimized\_model\_path** (`Union[str, os.PathLike]`) — Path of the optimized ONNX model.

Compute the difference in the number of nodes between the original and the optimized model.

**get\_operators\_difference**

[\<source>](https://github.com/huggingface/optimum/blob/main/optimum/onnxruntime/optimization.py#L296)

( onnx\_model\_path: typing.Union\[str, os.PathLike]onnx\_optimized\_model\_path: typing.Union\[str, os.PathLike] )

Parameters

* **onnx\_model\_path** (`Union[str, os.PathLike]`) — Path of the ONNX model.
* **onnx\_optimized\_model\_path** (`Union[str, os.PathLike]`) — Path of the optimized ONNX model.

Compute the dictionary mapping the operators name to the difference in the number of corresponding nodes between the original and the optimized model.

**optimize**

[\<source>](https://github.com/huggingface/optimum/blob/main/optimum/onnxruntime/optimization.py#L121)

( optimization\_config: OptimizationConfigsave\_dir: typing.Union\[str, os.PathLike]file\_suffix: typing.Optional\[str] = 'optimized'use\_external\_data\_format: typing.Optional\[bool] = Noneone\_external\_file: bool = True )

Parameters

* **optimization\_config** ([OptimizationConfig](https://huggingface.co/docs/optimum/main/en/onnxruntime/package_reference/configuration#optimum.onnxruntime.OptimizationConfig)) — The configuration containing the parameters related to optimization.
* **save\_dir** (`Union[str, os.PathLike]`) — The path used to save the optimized model.
* **file\_suffix** (`str`, defaults to `"optimized"`) — The file suffix used to save the optimized model.
* **use\_external\_data\_format** (`Optional[bool]`, defaults to `None`) — Whether to use external data format to store model of size >= 2Gb. This argument is deprecated.
* **one\_external\_file** (`bool`, defaults to `True`) — When `use_external_data_format=True`, whether to save all tensors to one external file. If False, save each tensor to a file named with the tensor name.

Optimizes a model given the optimization specifications defined in `optimization_config`.
