o
    Gi "                     @  s   d dl mZ d dlZd dlZd dl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 r7d 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 )    )annotationsN)Path)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%dd	Zd#d&ddZd'ddZe	e
							d(d)ddZe	e
	 		d*d+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   R/home/ubuntu/.local/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__path
str | Pathc                 C  sH   |du rt d d}|du rg }nt|ts|g}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provider_options)r   r   
isinstancelistortInferenceSession)r!   providerr%   r&   r   r   r   
load_model?   s   


zOnnxRuntimeModel.load_modelsave_directory	file_name
str | Nonec                 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_pretrainedW   s"   z!OnnxRuntimeModel._save_pretrainedstr | os.PathLikec                 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makedirsr8   )r   r-   r   r   r   r   save_pretrainedv   s
   z OnnxRuntimeModel.save_pretrainedFmodel_idtokenbool | str | None | Nonerevisionstr | None | Noneforce_downloadbool	cache_dirr+   r%   'ort.SessionOptions' | Nonec	                 K  s   |dur|nt }
tj|r&tjt||
 |||	dd}t||	d< n$t	||
||||d}t|j
|	d< t|j|	d< tj||||	dd}| dd|i|	S )	a;  
        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 (`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
        Nr&   )r+   r%   r&   r   )repo_idfilenamerA   rC   rG   rE   r   r   r   )r   r;   r!   isdirr   r,   r   as_posixpopr   parentname)clsr@   rA   rC   rE   rG   r.   r+   r%   r   r5   r   model_cache_pathr   r   r   _from_pretrained   s4   &z!OnnxRuntimeModel._from_pretrainedTc                 K  sD   d }t t|ddkr|d\}}| jd|||||d|S )N@r   )r@   rC   rG   rE   rA   r   )lenstrsplitrR   )rP   r@   rE   rA   rG   model_kwargsrC   r   r   r   from_pretrained   s   
z OnnxRuntimeModel.from_pretrained)N)NNN)r!   r"   )r-   r"   r.   r/   )r-   r9   )NNFNNNN)r@   r"   rA   rB   rC   rD   rE   rF   rG   r/   r.   r/   r+   r/   r%   rH   )TNN)r@   r"   rE   rF   rA   r/   rG   r/   )__name__
__module____qualname__r   r    staticmethodr,   r8   r?   classmethodr   rR   rX   r   r   r   r   r   4   s.    

Cr   )$
__future__r   r;   r1   pathlibr   numpyr   huggingface_hubr   huggingface_hub.utilsr   utilsr   r   r	   r
   onnxruntimer)   
get_loggerrY   r   bool_int8uint8int16uint16int32uint32int64uint64float16float32float64ORT_TO_NP_TYPEr   r   r   r   r   <module>   s2   
