class_docstr: Handles the ONNX Runtime optimization process for models shared on huggingface.co/models.
class_docstr_tok: ["Handles","the","ONNX","Runtime","optimization","process","for","models","shared","on","huggingface.co\/models","."]
docstr: Optimizes a model given the optimization specifications defined in `optimization_config`.
  
  Args:
      optimization_config ([`~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.
docstr_tok: ["Optimizes","a","model","given","the","optimization","specifications","defined","in","`","optimization_config","`",".","Args",":","optimization_config","(","[","`","~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","."]
code: def optimize(
      self,
      optimization_config: OptimizationConfig,
      save_dir: Union[str, os.PathLike],
      file_suffix: Optional[str] = "optimized",
      use_external_data_format: Optional[bool] = None,
      one_external_file: bool = True,
  ):
      """
      Optimizes a model given the optimization specifications defined in `optimization_config`.
  
      Args:
          optimization_config ([`~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.
  
      """
      if use_external_data_format is not None:
          logger.warning(
              "The argument use_external_data_format in the ORTOptimizer.optimize() method is deprecated and will"
              " be removed in optimum 2.0."
          )
  
      save_dir = Path(save_dir)
      save_dir.mkdir(parents=True, exist_ok=True)
      ORTConfigManager.check_optimization_supported_model(
          self.model_type, optimization_config
      )
  
      self.config.save_pretrained(save_dir)
      maybe_save_preprocessors(self.onnx_model_path[0].parent, save_dir)
  
      model_type = ORTConfigManager.get_model_ort_type(
          self.config.model_type
      )
      optimization_options = optimization_config.create_fusion_options(
          model_type
      )
  
      logger.info("Optimizing model...")
  
      # TODO: this is quite inefficient as we load in memory if models are <2GB without external data
      model_uses_external_data = False
      for model_path in self.onnx_model_path:
          # check if external data was exported
          onnx_model = onnx.load(
              str(model_path), load_external_data=False
          )
          if check_model_uses_external_data(onnx_model) is True:
              model_uses_external_data = True
              break
      del onnx_model
      gc.collect()
  
      # Create and save the configuration summarizing all the parameters related to optimization
      ort_config = ORTConfig(
          optimization=optimization_config,
          use_external_data_format=model_uses_external_data,
          one_external_file=one_external_file,
      )
  
      for model_path in self.onnx_model_path:
          suffix = f"_{file_suffix}" if file_suffix else ""
          output_path = save_dir.joinpath(
              f"{model_path.stem}{suffix}"
          ).with_suffix(model_path.suffix)
  
          try:
              optimizer = optimize_model(
                  model_path.as_posix(),
                  model_type,
                  self.normalized_config.num_attention_heads,
                  self.normalized_config.hidden_size,
                  opt_level=optimization_config.optimization_level,
                  optimization_options=optimization_options,
                  use_gpu=optimization_config.optimize_for_gpu,
                  only_onnxruntime=not optimization_config.enable_transformers_specific_optimizations,
              )
  
              if optimization_config.fp16:
                  if model_uses_external_data:
                      # Refer to https://github.com/microsoft/onnxruntime/blob/v1.15.0/onnxruntime/python/tools/transformers/float16.py#L204
                      # The ONNX infer_shapes_path method should be used instead of infer_shapes
                      # for models >= 2 GB, and it expects a model written to disk.
                      # Note that convert_float_to_float16 then overwrites optimizer.model as the
                      # new ModelProto.
                      optimizer.save_model_to_file(
                          output_path.as_posix(),
                          use_external_data_format=model_uses_external_data,
                          all_tensors_to_one_file=one_external_file,
                      )
  
                      optimizer.model = output_path.as_posix()
  
                  # keep_io_types to keep inputs/outputs as float32
                  optimizer.convert_float_to_float16(
                      use_symbolic_shape_infer=not optimization_config.disable_shape_inference,
                      keep_io_types=True,
                  )
          except Exception as e:
              if "Incomplete symbolic shape inference" in str(e):
                  err = RuntimeError(
                      f"{str(e)}. Try to set `disable_shape_inference=True` in your optimization configuration."
                  )
                  raise err from e
              raise
  
          # TODO: ORT save_model_to_file will save as `.data` although we save as `.onnx_data` in the export
          optimizer.save_model_to_file(
              output_path.as_posix(),
              use_external_data_format=model_uses_external_data,
              all_tensors_to_one_file=one_external_file,
          )
  
          # if loading from disk and saving in the same repository, remove previous external data
          if (
              Path(model_path.as_posix() + "_data").is_file()
              and self.from_ortmodel is False
          ):
              os.remove(model_path.as_posix() + "_data")
  
      # Save the model configuration
      self.config.save_pretrained(save_dir)
      ort_config.save_pretrained(save_dir)
  
      logger.info(
          f"Optimized model saved at: {save_dir} (external data format: "
          f"{model_uses_external_data}; saved all tensor to one file: "
          f"{one_external_file})"
      )
  
      return Path(save_dir)
  
imports: import gc
  import os
  from pathlib import Path
  from typing import TYPE_CHECKING, Dict, List, Optional, Union
  import onnx
  from onnx import load_model
  from transformers.models.auto.configuration_auto import AutoConfig
  from onnxruntime.transformers.onnx_model_bert import BertOnnxModel
  from onnxruntime.transformers.optimizer import optimize_model
  from ..onnx.utils import check_model_uses_external_data
  from ..utils import CONFIG_NAME, NormalizedConfigManager, logging
  from ..utils.save_utils import maybe_save_preprocessors
  from .configuration import OptimizationConfig, ORTConfig
  from .modeling_decoder import ORTModelForCausalLM
  from .modeling_ort import ORTModel
  from .modeling_seq2seq import ORTModelForConditionalGeneration
  from .utils import ONNX_WEIGHTS_NAME, ORTConfigManager
label_desc: Use image node_id 3 for calling the ORTOptimizer obj's underlying member method code with example usage: obj.optimize(optimization_config, save_dir, file_suffix, use_external_data_format, one_external_file) and returns: Path