
    #i                        d dl mZ d dlZd dlmZmZ d dlmZ  ej                  e	      Z
erd dlmZmZmZ 	 d dlmZ 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 d	dZy# e$ r Y w xY w)
    )annotationsN)TYPE_CHECKINGLiteral)save_or_push_to_hub_model)CrossEncoderSentenceTransformerSparseEncoder)OptimizationConfigc                z  
 	 ddl m}m} ddlm} | j                  }	t        |	|      st        d      |j                  |	      
t        t              r0|j                  vrt        d      xs  t        |             dt        
fd	d
|||d| 	       y# t
        $ r t        d      w xY w)a  
    Export an optimized ONNX model from a SentenceTransformer, SparseEncoder, or CrossEncoder model.

    The O1-O4 optimization levels are defined by Optimum and are documented here:
    https://huggingface.co/docs/optimum-onnx/main/en/onnxruntime/usage_guides/optimization

    The optimization levels are:

    - O1: basic general optimizations.
    - O2: basic and extended general optimizations, transformers-specific fusions.
    - O3: same as O2 with GELU approximation.
    - O4: same as O3 with mixed precision (fp16, GPU-only)

    See the following pages for more information & benchmarks:

    - `Sentence Transformer > Usage > Speeding up Inference <https://sbert.net/docs/sentence_transformer/usage/efficiency.html>`_
    - `Cross Encoder > Usage > Speeding up Inference <https://sbert.net/docs/cross_encoder/usage/efficiency.html>`_

    Args:
        model (SentenceTransformer | SparseEncoder | CrossEncoder): The SentenceTransformer, SparseEncoder,
            or CrossEncoder model to be optimized. Must be loaded with `backend="onnx"`.
        optimization_config (OptimizationConfig | Literal["O1", "O2", "O3", "O4"]): The optimization configuration or level.
        model_name_or_path (str): The path or Hugging Face Hub repository name where the optimized model will be saved.
        push_to_hub (bool, optional): Whether to push the optimized model to the Hugging Face Hub. Defaults to False.
        create_pr (bool, optional): Whether to create a pull request when pushing to the Hugging Face Hub. Defaults to False.
        file_suffix (str | None, optional): The suffix to add to the optimized model file name. Defaults to None.

    Raises:
        ImportError: If the required packages `optimum` and `onnxruntime` are not installed.
        ValueError: If the provided model is not a valid SentenceTransformer, SparseEncoder, or CrossEncoder model loaded with `backend="onnx"`.
        ValueError: If the provided optimization_config is not valid.

    Returns:
        None
    r   )ORTModelORTOptimizer)AutoOptimizationConfigzPlease install Optimum and ONNX Runtime to use this function. You can install them with pip: `pip install sentence-transformers[onnx]` or `pip install sentence-transformers[onnx-gpu]`z}The model must be a Transformer-based SentenceTransformer, SparseEncoder, or CrossEncoder model loaded with `backend="onnx"`.z\optimization_config must be an OptimizationConfig instance or one of 'O1', 'O2', 'O3', 'O4'.N	optimizedc                ,    j                  |       S )N)file_suffix)optimize)save_dirr   optimization_config	optimizers    w/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/sentence_transformers/backend/optimize.py<lambda>z-export_optimized_onnx_model.<locals>.<lambda>^   s    ););<OQYgr);)s     export_optimized_onnx_modelonnx)	export_functionexport_function_nameconfigmodel_name_or_pathpush_to_hub	create_prr   backendmodel)optimum.onnxruntimer   r   !optimum.onnxruntime.configurationr   ImportErrortransformers_model
isinstance
ValueErrorfrom_pretrainedstr_LEVELSgetattrr   )r"   r   r   r   r    r   r   r   r   	ort_modelr   s    `   `    @r   r   r      s    X
>L ((Ii* L
 	
 ,,Y7I%s+&<&D&DDn  "8%8Rg&<>QRT!s:"-
7  
?
 	

s   B% %B:)FFN)r"   z2SentenceTransformer | SparseEncoder | CrossEncoderr   z4OptimizationConfig | Literal['O1', 'O2', 'O3', 'O4']r   r*   r   boolr    r.   r   z
str | NonereturnNone)
__future__r   loggingtypingr   r   #sentence_transformers.backend.utilsr   	getLogger__name__loggersentence_transformersr   r   r	   r$   r
   r%   r    r   r   <module>r:      s    "  ) I			8	$VVH "T=TMT T 	T
 T T 
T	  s   A AA