
    wi                      V   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            rd dlZ ej        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
          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                      e Zd ZddZd Zeddeeef         fd            Z	ddeeef         de
e         fdZdeeej        f         f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                        Zee	 	 	 ddeeef         dede
e         de
e         fd                        ZdS )OnnxRuntimeModelNc                     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   kwargss      n/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/pipelines/onnx_utils.py__init__zOnnxRuntimeModel.__init__5   sQ    bccc
$jj)94@@!',?AR!S!S    c                 t    d |                                 D             }| j                            d |          S )Nc                 >    i | ]\  }}|t          j        |          S  )nparray).0kvs      r   
<dictcomp>z-OnnxRuntimeModel.__call__.<locals>.<dictcomp><   s&    <<<TQ!RXa[[<<<r   )itemsr   run)r   r   inputss      r   __call__zOnnxRuntimeModel.__call__;   s3    <<V\\^^<<<z~~dF+++r   pathc                 n    |t                               d           d}t          j        | |g|          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,   s      r   
load_modelzOnnxRuntimeModel.load_model?   s<     KKWXXX-H#DXJ\ZZZZr   save_directory	file_namec                    ||nt           }| j                            | j                  }t	          |                              |          }	 t          j        ||           n# t
          j        $ r Y nw xY w| j                            t                    }|	                                rUt	          |                              t                    }	 t          j        ||           dS # t
          j        $ r Y dS w xY 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   r1   r2   r   model_file_namesrc_pathdst_paths          r   _save_pretrainedz!OnnxRuntimeModel._save_pretrainedP   s    (1'<))BS&//0FGG''00AA	OHh////# 	 	 	D	 &//0JKK?? 	N++445OPPH(33333'   		 	s$   A$ $A65A6C+ +C>=C>c                     t           j                            |          r t                              d| d           dS t          j        |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makedirsr<   )r   r1   r   s      r   save_pretrainedz OnnxRuntimeModel.save_pretrainedo   su     7>>.)) 	LL^>^^^___F
NT2222 	n7777777r   Fmodel_idtokenrevisionforce_download	cache_dirr/   r,   zort.SessionOptionsc	                    ||nt           }
t          j                            |          rPt                              t          ||
                                          ||          }t          |          |	d<   n`t          ||
||||          }t          |          j	        |	d<   t          |          j
        |	d<   t                              |||          } | 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filenamerE   rF   rH   rG   r   r   r   )r
   r?   r(   isdirr   r0   r   as_posixr   parentname)clsrD   rE   rF   rG   rH   r2   r/   r,   r   r9   r   model_cache_paths                r   _from_pretrainedz!OnnxRuntimeModel._from_pretrained   s	   L (1'<))BS7=="" 	p$//X//88::X\h 0  E (,H~~F#$$  / (!#-      (,,<'='='DF#$*./?*@*@*EF&'$//0@8bn/ooEs)))&)))r   Tc           	          d }t          t          |                              d                    dk    r|                    d          \  }} | j        d|||||d|S )N@r   )rD   rF   rH   rG   rE   r   )lenstrsplitrR   )rP   rD   rG   rE   rH   model_kwargsrF   s          r   from_pretrainedz OnnxRuntimeModel.from_pretrained   s     s8}}""3''((A--!)!4!4Hh#s# 
)
 
 
 
 	
r   )N)NN)NNFNNNN)TNN)__name__
__module____qualname__r   r'   staticmethodr   rV   r   r0   r   r<   r?   PathLikerC   classmethodr   boolrR   rY   r   r   r   r   r   4   s       T T T T, , , [ [sDy) [ [ [ \[  uS$Y/? HUXM    >8c2;./8 8 8 8,  37/3$#'#'"&7;9* 9*T	"9* dCo./9* 5d+,	9*
 9* C=9* C=9* 3-9* 349* 9* 9*  [9*v   $##'
 
T	"
 
 }	

 C=
 
 
  [
 
 
r   r   )%r?   r5   pathlibr   typingr   r   numpyr   huggingface_hubr   huggingface_hub.utilsr   utilsr	   r
   r   r   onnxruntimer-   
get_loggerrZ   r   bool_int8uint8int16uint16int32uint32int64uint64float16float32float64ORT_TO_NP_TYPEr   r   r   r   <module>rv      sP  $ 
			        " " " " " " " "     + + + + + + 6 6 6 6 6 6 ] ] ] ] ] ] ] ] ] ] ] ]   
	H	%	% HGXXiXiXizZj  c
 c
 c
 c
 c
 c
 c
 c
 c
 c
r   