Optimization

ORTOptimizer

class optimum.onnxruntime.ORTOptimizer

< >

( ort_config: ORTConfig optimization_config: OptimizationConfig **kwargs )

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

export

< >

( model_name_or_path: typing.Union[str, os.PathLike] output_path: typing.Union[str, os.PathLike] feature: str = 'default' **kwargs )

Parameters

  • model_name_or_path (Union[str, os.PathLike]) — Repository name in the Hugging Face Hub or path to a local directory hosting the model.
  • output_path (os.PathLike) — The path used to save the model exported to an ONNX Intermediate Representation (IR).
  • feature (str, defaults to “default”) — Feature to use when exporting the model.
  • cache_dir (str, optional) — Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used.
  • force_download (bool, optional, defaults to False) — Whether or not to force the (re-)download of the model weights and configuration files, overriding the cached versions if they exist.
  • resume_download (bool, optional, defaults to False) — Whether or not to delete incompletely received file. Attempts to resume the download if such a file exists.
  • revision(str, optional) — The specific version to use. It can be a branch name, a tag name, or a commit id, since we use a git-based system for storing models and other artifacts on huggingface.co, so revision can be any identifier allowed by git.

Loads and exports a model to an ONNX Intermediate Representation (IR).

fit

< >

( model_name_or_path: typing.Union[str, os.PathLike] output_dir: typing.Union[str, os.PathLike] feature: str = 'default' config: typing.Optional[transformers.configuration_utils.PretrainedConfig] = None **kwargs )

Parameters

  • model_name_or_path (Union[str, os.PathLike]) — Repository name in the Hugging Face Hub, path to a local directory hosting the model or path to a pre-existing onnx model.
  • output_dir (Union[str, os.PathLike]) — The output directory where the optimized model will be saved.
  • feature (str, defaults to “default”) — Feature to use when exporting the model.
  • config (PretrainedConfig, optional) — A configuration associated to the pre-existing ONNX model.
  • cache_dir (str, optional) — Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used.
  • force_download (bool, optional, defaults to False) — Whether or not to force the (re-)download of the model weights and configuration files, overriding the cached versions if they exist.
  • resume_download (bool, optional, defaults to False) — Whether or not to delete incompletely received file. Attempts to resume the download if such a file exists.
  • revision(str, optional) — The specific version to use. It can be a branch name, a tag name, or a commit id, since we use a git-based system for storing models and other artifacts on huggingface.co, so revision can be any identifier allowed by git.
  • disable_gelu (bool, optional, defaults to False) — Whether or not to disable Gelu fusion.
  • disable_layer_norm (bool, optional, defaults to False) — Whether or not to disable LayerNormalization fusion.
  • disable_attention (bool, optional, defaults to False) — Whether or not to disable Attention fusion.
  • disable_skip_layer_norm (bool, optional, defaults to False) — Whether or not to disable SkipLayerNormalization fusion.
  • disable_bias_skip_layer_norm (bool, optional, defaults to False) — Whether or not to disable Add Bias and SkipLayerNormalization fusion.
  • disable_bias_gelu (bool, optional, defaults to False) — Whether or not to disable Add Bias and Gelu/FastGelu fusion.
  • enable_gelu_approximation (bool, optional, defaults to False) — Whether or not to enable Gelu/BiasGelu to FastGelu conversion. The default value is set to False since the approximation might slightly impact the accuracy of models.
  • use_mask_index (bool, optional, defaults to False) — Whether or not to use mask index instead of raw attention mask in attention operator.
  • no_attention_mask (bool, optional, defaults to False) — No attention mask. Only works for model_type=bert.
  • disable_embed_layer_norm (bool, optional, defaults to True) — Whether or not to disable EmbedLayerNormalization fusion. The default value is set to True since the fusion is incompatible with ONNX Runtime quantization.

Applies the ONNX Runtime graph-level optimization on a given model and saves the resulting model.

get_optimize_details

< >

( onnx_model_path: typing.Optional[str] = None optimized_model_path: typing.Optional[str] = None summary: bool = True nodes_details: bool = True ) sorted_nodes_change (List[Tuple[str, int]])

Parameters

  • onnx_model_path (str, optional) — Path of a stored ONNX model.
  • optimized_model_path (str, optional) — Path of the corresponding optimized ONNX model.
  • summary (bool, defaults to True) — Whether report the optimization details: reduction of nodes, and complex node fusions.
  • nodes_details (bool, defaults to True) — Whether report the top 5 reduced op_types, and return the detailed node change list.

Returns

sorted_nodes_change (List[Tuple[str, int]])

Returns a sorted list with op types and its change after the optimization.

Returns a dictionary reporting the optimization.