# Optimization

## ORTOptimizer[[optimum.onnxruntime.ORTOptimizer]]

#### optimum.onnxruntime.ORTOptimizer[[optimum.onnxruntime.ORTOptimizer]]

[Source](https://github.com/huggingface/optimum-onnx/blob/main/optimum/onnxruntime/optimization.py#L49)

Handles the ONNX Runtime optimization process for models shared on huggingface.co/models.

from_pretrainedoptimum.onnxruntime.ORTOptimizer.from_pretrainedhttps://github.com/huggingface/optimum-onnx/blob/main/optimum/onnxruntime/optimization.py#L80[{"name": "model_or_path", "val": ": str | os.PathLike | ORTModel"}, {"name": "file_names", "val": ": list[str] | None = None"}]- **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](/docs/optimum/main/en/onnxruntime/package_reference/modeling#optimum.onnxruntime.ORTModel).
- **file_names(`Optional[List[str]]`,** defaults to `None`) --
  The list of file names of the models to optimize.0
Initializes the `ORTOptimizer` from a local directory or an `ORTModel`.

**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](/docs/optimum/main/en/onnxruntime/package_reference/modeling#optimum.onnxruntime.ORTModel).

file_names(`Optional[List[str]]`, defaults to `None`) : The list of file names of the models to optimize.
#### get_fused_operators[[optimum.onnxruntime.ORTOptimizer.get_fused_operators]]

[Source](https://github.com/huggingface/optimum-onnx/blob/main/optimum/onnxruntime/optimization.py#L247)

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

**Parameters:**

onnx_model_path (`Union[str, os.PathLike]`) : Path of the ONNX model.

**Returns:**

The dictionary mapping the name of the fused operators to their number of apparition in the model.
#### get_nodes_number_difference[[optimum.onnxruntime.ORTOptimizer.get_nodes_number_difference]]

[Source](https://github.com/huggingface/optimum-onnx/blob/main/optimum/onnxruntime/optimization.py#L265)

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

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

**Returns:**

The difference in the number of nodes between the original and the optimized model.
#### get_operators_difference[[optimum.onnxruntime.ORTOptimizer.get_operators_difference]]

[Source](https://github.com/huggingface/optimum-onnx/blob/main/optimum/onnxruntime/optimization.py#L293)

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

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

**Returns:**

The dictionary mapping the operators name to the difference in the number of corresponding nodes between the
original and the optimized model.
#### optimize[[optimum.onnxruntime.ORTOptimizer.optimize]]

[Source](https://github.com/huggingface/optimum-onnx/blob/main/optimum/onnxruntime/optimization.py#L128)

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

**Parameters:**

optimization_config ([OptimizationConfig](/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.

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.