o
    pi                      @   s   d dl Z d dlZd dlmZ d dlmZmZ d dlZd dl	m
Z
 d dlmZ ddlmZmZmZmZ e r9d dlZeeZejejejejejejejejejej ej!ej"dZ#G d	d
 d
Z$dS )    N)Path)OptionalUnion)hf_hub_download)validate_hf_hub_args   )ONNX_EXTERNAL_WEIGHTS_NAMEONNX_WEIGHTS_NAMEis_onnx_availablelogging)ztensor(bool)ztensor(int8)ztensor(uint8)ztensor(int16)ztensor(uint16)ztensor(int32)ztensor(uint32)ztensor(int64)ztensor(uint64)ztensor(float16)ztensor(float)ztensor(double)c                   @   s   e Zd ZdddZdd Zeddeeef fddZ	dd	eeef d
e
e fddZd	eeejf fddZee							ddeeef de
eeedf  de
eedf  dede
e d
e
e de
e de
d fddZee			d deeef dede
e de
e fddZdS )!OnnxRuntimeModelNc                 K   s0   t d || _|dd | _|dt| _d S )NzL`diffusers.OnnxRuntimeModel` is experimental and might change in the future.model_save_dirlatest_model_name)loggerinfomodelgetr   r	   r   )selfr   kwargs r   \/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/diffusers/pipelines/onnx_utils.py__init__5   s   
zOnnxRuntimeModel.__init__c                 K   s    dd |  D }| jd |S )Nc                 S   s   i | ]
\}}|t |qS r   )nparray).0kvr   r   r   
<dictcomp><   s    z-OnnxRuntimeModel.__call__.<locals>.<dictcomp>)itemsr   run)r   r   inputsr   r   r   __call__;   s   zOnnxRuntimeModel.__call__pathc                 C   s(   |du rt d d}tj| |g|dS )au  
        Loads an ONNX Inference session with an ExecutionProvider. Default provider is `CPUExecutionProvider`

        Arguments:
            path (`str` or `Path`):
                Directory from which to load
            provider(`str`, *optional*):
                Onnxruntime execution provider to use for loading the model, defaults to `CPUExecutionProvider`
        Nz=No onnxruntime provider specified, using CPUExecutionProviderCPUExecutionProvider)	providerssess_options)r   r   ortInferenceSession)r"   providerr%   r   r   r   
load_model?   s   
zOnnxRuntimeModel.load_modelsave_directory	file_namec                 K   s   |dur|nt }| j| j}t||}zt|| W n
 tjy(   Y nw | jt}|	 rOt|t}z	t|| W dS  tjyN   Y dS w dS )a?  
        Save a model and its configuration file to a directory, so that it can be re-loaded using the
        [`~optimum.onnxruntime.modeling_ort.ORTModel.from_pretrained`] class method. It will always save the
        latest_model_name.

        Arguments:
            save_directory (`str` or `Path`):
                Directory where to save the model file.
            file_name(`str`, *optional*):
                Overwrites the default model file name from `"model.onnx"` to `file_name`. This allows you to save the
                model with a different name.
        N)
r	   r   joinpathr   r   shutilcopyfileSameFileErrorr   exists)r   r*   r+   r   model_file_namesrc_pathdst_pathr   r   r   _save_pretrainedP   s"   z!OnnxRuntimeModel._save_pretrainedc                 K   sF   t j|rtd| d dS t j|dd | j|fi | dS )a$  
        Save a model to a directory, so that it can be re-loaded using the [`~OnnxModel.from_pretrained`] class
        method.:

        Arguments:
            save_directory (`str` or `os.PathLike`):
                Directory to which to save. Will be created if it doesn't exist.
        zProvided path (z#) should be a directory, not a fileNT)exist_ok)osr"   isfiler   errormakedirsr4   )r   r*   r   r   r   r   save_pretrainedo   s
   z OnnxRuntimeModel.save_pretrainedFmodel_idtokenrevisionforce_download	cache_dirr(   r%   zort.SessionOptionsc	                 K   s   |dur|nt }
tj|r"tjt||
 ||d}t||	d< n t||
||||d}t|j	|	d< t|j
|	d< tj|||d}| dd|i|	S )aA  
        Load a model from a directory or the HF Hub.

        Arguments:
            model_id (`str` or `Path`):
                Directory from which to load
            token (`str` or `bool`):
                Is needed to load models from a private or gated repository
            revision (`str`):
                Revision is the specific model version to use. It can be a branch name, a tag name, or a commit id
            cache_dir (`Union[str, Path]`, *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.
            file_name(`str`):
                Overwrites the default model file name from `"model.onnx"` to `file_name`. This allows you to load
                different model files from the same repository or directory.
            provider(`str`):
                The ONNX runtime provider, e.g. `CPUExecutionProvider` or `CUDAExecutionProvider`.
            kwargs (`Dict`, *optional*):
                kwargs will be passed to the model during initialization
        N)r(   r%   r   )repo_idfilenamer<   r=   r?   r>   r   r   r   )r	   r6   r"   isdirr   r)   r   as_posixr   parentname)clsr;   r<   r=   r>   r?   r+   r(   r%   r   r1   r   model_cache_pathr   r   r   _from_pretrained   s$   &z!OnnxRuntimeModel._from_pretrainedTc                 K   sD   d }t t|ddkr|d\}}| jd|||||d|S )N@r   )r;   r=   r?   r>   r<   r   )lenstrsplitrH   )rF   r;   r>   r<   r?   model_kwargsr=   r   r   r   from_pretrained   s   
z OnnxRuntimeModel.from_pretrained)N)NN)NNFNNNN)TNN)__name__
__module____qualname__r   r!   staticmethodr   rK   r   r)   r   r4   r6   PathLiker:   classmethodr   boolrH   rN   r   r   r   r   r   4   sb    
 

	;
r   )%r6   r-   pathlibr   typingr   r   numpyr   huggingface_hubr   huggingface_hub.utilsr   utilsr   r	   r
   r   onnxruntimer&   
get_loggerrO   r   bool_int8uint8int16uint16int32uint32int64uint64float16float32float64ORT_TO_NP_TYPEr   r   r   r   r   <module>   s2   
