o
    di&                     @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZmZ d dlZd dlZd dlmZ d dlmZmZmZmZmZmZmZmZmZmZm Z  d dl!m"Z" d d	l#m$Z$ d d
l%m&Z& d dl'm(Z( d dl)m*Z*m+Z+ d dl,m-Z-m.Z. d dl/m0Z0 d dl1m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z;m<Z< ddl=m>Z> ddl?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZI ddlJmKZKmLZL ddl?mMZMmNZNmOZO eIddrd dlPmQZQ nd dlRmQZQ eSeTZUG dd deKe"ZVG dd  d eLeZWG d!d" d"eWZXG d#d$ d$eWZYG d%d& d&eWZZG d'd( d(eWZ[G d)d* d*eWZ\G d+d, d,eKZ]d-Z^e5e^G d.d/ d/eVeZ_e5e^G d0d1 d1eVeZ`e5e^G d2d3 d3eVeZae5e^G d4d5 d5eVe Zbe5e^G d6d7 d7eVeZce5e^G d8d9 d9eVeZde5e^G d:d; d;eVeZee5e^G d<d= d=eVeZfG d>d? d?ZgeIdd@rd dAlhmiZimjZj e5e^G dBdC dCeVejZke5e^G dDdE dEeVeiZlnG dFdC dCegZkG dGdE dEegZleIddHrd dIlhmmZmmnZn e5e^G dJdK dKeVenZoe5e^G dLdM dMeVemZpnG dNdK dKegZoG dOdM dMegZpe_e`eaebecedeeefekeleoepgZqdddQerdResfdSdTZtedUepfdVeefdWe_fdXekfdYebfgZuedVeffdWe`fdXelfdYecfgZvedWeafdXeofdYedfgZweuevewgZxdZd[ ZyG d\d] d]eZzG d^d_ d_ezZ{G d`da daezZ|G dbdc dcezZ}dS )e    N)OrderedDict)Path)TemporaryDirectory)AnyDictOptionalSequenceUnion)ConfigMixin)AutoPipelineForImage2ImageAutoPipelineForInpaintingAutoPipelineForText2Image%LatentConsistencyModelImg2ImgPipelineLatentConsistencyModelPipelineStableDiffusionImg2ImgPipelineStableDiffusionInpaintPipelineStableDiffusionPipeline StableDiffusionXLImg2ImgPipeline StableDiffusionXLInpaintPipelineStableDiffusionXLPipeline)DiffusionPipeline)SchedulerMixin)SCHEDULER_CONFIG_NAME)CONFIG_NAME)load_or_create_model_cardpopulate_model_card)HfApicreate_repo)validate_hf_hub_args)CLIPFeatureExtractorCLIPTokenizer)add_end_docstrings)ModelOutput)http_user_agent)InferenceSessionSessionOptions   )main_export)
(DIFFUSION_MODEL_TEXT_ENCODER_2_SUBFOLDER(DIFFUSION_MODEL_TEXT_ENCODER_3_SUBFOLDER&DIFFUSION_MODEL_TEXT_ENCODER_SUBFOLDER%DIFFUSION_MODEL_TRANSFORMER_SUBFOLDERDIFFUSION_MODEL_UNET_SUBFOLDER%DIFFUSION_MODEL_VAE_DECODER_SUBFOLDER%DIFFUSION_MODEL_VAE_ENCODER_SUBFOLDER#DIFFUSION_PIPELINE_CONFIG_FILE_NAMEONNX_WEIGHTS_NAMEis_diffusers_version   )ORTParentMixinORTSessionMixin)get_device_for_providernp_to_pt_generators&prepare_providers_and_provider_optionsz>=z0.25.0)DiagonalGaussianDistributionc                '       s  e Zd ZeZdZdZeZddddddddddddddddddde	d d	e	d d
e	d de	d de	d de	d de	d de	d de	d de	d de	d de	d de
de
de	e
 de	e
 de	eeeef  f" fddZedeee	eeejjf  f fdd Zd!eejeef fd"d#Ze		$				d3d%eeef d&e
d'ed(e	ee  d)e	eeeeef  eeef f  d*e	e de	e
 fd+d,Z	d4d-eeef d.e	e
 fd/d0Zd1d2 Z   Z!S )5ORTDiffusionPipelineauto	diffusersNTF)unet_sessiontransformer_sessionvae_decoder_sessionvae_encoder_sessiontext_encoder_sessiontext_encoder_2_sessiontext_encoder_3_session	scheduler	tokenizertokenizer_2tokenizer_3feature_extractorforce_zeros_for_empty_promptrequires_aesthetics_scoreadd_watermarkeruse_io_bindingmodel_save_dirr<   r$   r=   r>   r?   r@   rA   rB   rC   r   rD   r    rE   rF   rG   r   rH   rI   rJ   rK   rL   c                   s  |d ur
t || |nd | _|d urt|| |nd | _|d ur$t|| |nd | _|d ur1t|| |nd | _|d ur>t|| |nd | _|d urKt|| |nd | _	|d urXt
|| |nd | _t jttd | j| j| j	| j| j| j| jhd | j	d us| jd urt| j	| jnd | _|dd | _|dd | _|| _|	| _|
| _|| _|| _i d| jd| jd| jd| jd| jd	| jd| jd| jd
| jd| jd| jd| jd| jd|d|d|}i }t| jj D ]}||v r|| ||< q| jj| fi | || _ d S )Npartsimage_encodersafety_checkervaeunettransformertext_encodertext_encoder_2text_encoder_3rC   rD   rE   rF   rG   rI   rH   rJ   )!ORTUnetrR   ORTTransformerrS   ORTTextEncoderrT   rU   rV   ORTVaeEncodervae_encoderORTVaeDecodervae_decodersuperinitialize_ort_attributeslistfilterORTVaerQ   poprO   rP   rC   rD   rE   rF   rG   inspect	signatureauto_model_class
parameterskeys__init__rL   )selfr<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   kwargsall_pipeline_init_argsdiffusers_pipeline_argskey	__class__ t/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/optimum/onnxruntime/modeling_diffusion.pyri   X   s   	

zORTDiffusionPipeline.__init__returnc              	   C   s<   | j | j| j| j| j| j| j| jd}dd | D }|S )N)rQ   rR   rS   rT   rU   rV   rP   rO   c                 S   s   i | ]\}}|d ur||qS Nrq   ).0kvrq   rq   rr   
<dictcomp>   s    z3ORTDiffusionPipeline.components.<locals>.<dictcomp>)	rQ   rR   rS   rT   rU   rV   rP   rO   items)rj   
componentsrq   rq   rr   rz      s   
zORTDiffusionPipeline.componentsdevicec                 C   s,   | j  D ]}t|ttfr|| q| S )a  
        Changes the device of the pipeline components to the specified device.

        Args:
            device (`torch.device` or `str` or `int`):
                Device ordinal for CPU/GPU supports. Setting this to -1 will leverage CPU, a positive will run
                the model on the associated CUDA device id. You can pass native `torch.device` or a `str` too.

        Returns:
            `ORTDiffusionPipeline`: The pipeline with the updated device.
        )rz   values
isinstancer4   r3   to)rj   r{   	componentrq   rq   rr   r~      s
   
zORTDiffusionPipeline.toCPUExecutionProvidermodel_name_or_pathexportprovider	providersprovider_optionssession_optionsc               
   K   sV  t |||d\}}|dd|dd|dd|dd|dd|d	d|d
dd}	| j|fi |	}
t|
trA|
d n|
}
d}t|}|rzt }t|j}|dd|ddt	|i j
|ddd}td&||dd| jd||	 | sdd |
 D ddhB }dd |D }|tttth tt djd&t||g dd|	}t|}|t t |t t |t t |t t |t t |t t |t t d}i }i }| D ]@\}}||ddur||||< q|| dddur|| d|| d< q|  r!t!||||d|| d< qi }dD ]M}||ddur:||||< q&|
|dd durr|
|\}}t"#|}t$||}t$|d }||  rl||| ||< q&||||< q&| j%d!kr|
d" }t&|}n| }|d&i ||||d#||}|j'd&i |
 |j'|
d$|d% |S )'a  
        Instantiates a [`ORTDiffusionPipeline`] with ONNX Runtime sessions from a pretrained model.
        This method can be used to load a model from the Hugging Face Hub or from a local directory.

        Args:
            model_name_or_path (`str` or `os.PathLike`):
                Path to a folder containing the model files or a hub repository id.
            export (`bool`, *optional*, defaults to `False`):
                Whether to export the model to ONNX format. If set to `True`, the model will be exported and saved
                in the specified directory.
            provider (`str`, *optional*, defaults to `"CPUExecutionProvider"`):
                The execution provider for ONNX Runtime. Can be `"CUDAExecutionProvider"`, `"DmlExecutionProvider"`,
                etc.
            providers (`Sequence[str]`, *optional*):
                A list of execution providers for ONNX Runtime. Overrides `provider`.
            provider_options (`Union[Sequence[Dict[str, Any]], Dict[str, Any]]`, *optional*):
                Options for each execution provider. Can be a single dictionary for the first provider or a list of
                dictionaries for each provider. The order of the dictionaries should match the order of the providers.
            session_options (`SessionOptions`, *optional*):
                Options for the ONNX Runtime session. Can be used to set optimization levels, graph optimization,
                etc.
            use_io_binding (`bool`, *optional*):
                Whether to use IOBinding for the ONNX Runtime session. If set to `True`, it will use IOBinding for
                input and output tensors.
            **kwargs:
                Can include the following:
                - Export arguments (e.g., `slim`, `dtype`, `device`, `no_dynamic_axes`, etc.).
                - Hugging Face Hub arguments (e.g., `revision`, `cache_dir`, `force_download`, etc.).
                - Preloaded models or sessions for the different components of the pipeline (e.g., `vae_encoder_session`,
                `vae_decoder_session`, `unet_session`, `transformer_session`, `image_encoder`, `safety_checker`, etc.).

        Returns:
            [`ORTDiffusionPipeline`]: The loaded pipeline with ONNX Runtime sessions.
        )r   r   r   force_downloadFresume_downloadNlocal_files_only	cache_dirrevisionproxiestokenr   r   r   r   r   r   r   r   slimdtypeno_dynamic_axes)r   r   r{   r   T)r   outputno_post_processdo_validationtaskc                 S   s   h | ]	}| d s|qS )_)
startswith)ru   rn   rq   rq   rr   	<setcomp>P      z7ORTDiffusionPipeline.from_pretrained.<locals>.<setcomp>r[   r]   c                 S   s   h | ]	}t j|d qS )*)ospathjoin)ru   r   rq   rq   rr   r   Q  r   )
user_agent)z	*.msgpackz*.safetensorsz*.binz*.xml)repo_idallow_patternsignore_patterns)rR   rS   r[   r]   rT   rU   rV   _session)r   r   sess_options>   rC   rD   rE   rF   rG   NNfrom_pretrainedr9   _class_name)rK   rL   _name_or_path)r   rq   )(r7   getload_configr}   tupler   r   namerc   r5   typer'   r   is_dirrh   updater0   r/   r   r   r   r#   snapshot_downloadstrr,   r+   r.   r-   r*   r(   r)   ry   is_filer$   	importlibimport_modulegetattr__name___get_ort_classregister_to_config) clsr   r   r   r   r   r   rK   rk   
hub_kwargsconfigmodel_save_tmpdirmodel_save_pathexport_kwargsall_componentsr   model_save_foldermodel_pathsmodelssessionsmodelr   	submodelssubmodellibrary_namelibrary_classeslibrary	class_objload_methodpipeline_class_nameort_pipeline_classort_pipelinerq   rq   rr   r      s   2


























	z$ORTDiffusionPipeline.from_pretrainedsave_directorypush_to_hubc                 K   s.  t |}|jddd |r;|dd}|dd}|dd}|dd}|d	|tjjd
 }	t|	d||dj}	| 	| | j
|d  | jdurU| j|t  | jdurb| j|t  | jduro| j|t  | jdur|| j|t  | jdur| j|t  | jdur| j|t  | jdur| j|t  | jdur| j|d  | jdur| j|d  | jdur| j|d  | jdur| j|d  | jdur| j|d  | jdur| j|d  |rt |	|dd}
t!|
}
|
"tj#|d | j$||	|||d dS dS )a  
        Saves a model and its configuration file to a directory, so that it can be re-loaded using the
        [`from_pretrained`] class method.

        Args:
            save_directory (`Union[str, os.PathLike]`):
                Directory to which to save. Will be created if it doesn't exist.
            push_to_hub (`bool`, *optional*, defaults to `False`):
                Whether or not to push your model to the Hugging Face model hub after saving it.
            **kwargs:
                Additional keyword arguments passed along to [`~huggingface_hub.create_repo`] and
                [`~huggingface_hub.HfApi.upload_folder`] if `push_to_hub` is set to `True`.
        T)parentsexist_okr   NprivateF	create_prcommit_messager   )r   r   r   rC   rO   rP   rD   rE   rF   rG   )r   is_pipelinez	README.md)r   r   r   )%r   mkdirrc   splitr   r   sepr   r   save_configrC   save_pretrainedrR   r,   rS   r+   r[   r.   r]   r-   rT   r*   rU   r(   rV   r)   rO   rP   rD   rE   rF   rG   r   r   saver   _upload_folder)rj   r   r   rk   r   r   r   r   r   r   
model_cardrq   rq   rr   r     sb   














z$ORTDiffusionPipeline.save_pretrainedc                 O   s   t |}tt|D ]}t|| | j}|| |urtd q
| D ]\}}t|| j}||ur;td |||< q$| jj	| g|R i |S )NzwConverting numpy random state to torch generator is deprecated. Please pass a torch generator directly to the pipeline.)
r`   rangelenr6   r{   loggerwarningry   rf   __call__)rj   argsrk   inew_argsrn   value	new_valuerq   rq   rr   r     s"   zORTDiffusionPipeline.__call__)Fr   NNNN)F)"r   
__module____qualname__r/   config_namer   r   r   rf   r   boolr	   r   r   r   ri   propertyr   r4   torchnnModulerz   r{   intr~   classmethodr   r   r%   r   r   r   __classcell__rq   rq   ro   rr   r9   Q   s    	
t(

"	 6

Lr9   c                   @   sN   e Zd ZU eZeed< 	ddddddee fdd	Z	d
e
eef fddZdS )ORTModelMixinr   Nsessionr$   parentr9   rK   c                 C   sd   | j ||d || _t|jj| j }| s#td| jj d| | 	|}| j
di | d S )N)rK   zConfiguration file for z not found at rq   )r_   r   r   _model_pathr   r   
ValueErrorrp   r   _dict_from_json_filer   )rj   r   r   rK   config_file_pathconfig_dictrq   rq   rr   ri   	  s   
zORTModelMixin.__init__r   c                 C   s   |  | | | dS )a9  
        Saves the ONNX model and its configuration file to a directory, so that it can be re-loaded using the
        [`from_pretrained`] class method.

        Args:
            save_directory (`Union[str, os.PathLike]`):
                Directory to which to save. Will be created if it doesn't exist.
        N)save_sessionr   )rj   r   rq   rq   rr   r     s   

zORTModelMixin.save_pretrainedrt   )r   r   r   r   r   r   __annotations__r   r   ri   r	   r   r   rq   rq   rq   rr   r     s   
 
r   c                       s   e Zd Z fddZ				ddeejejf deejejf deejejf de	eejejf  d	e	e
eef  d
e	e
eef  defddZ  ZS )rW   c                    sX   t  j|i | t| jdstd | jd d t| jd dkr*td d S d S )Ntime_cond_proj_dimzThe `time_cond_proj_dim` attribute is missing from the UNet configuration. Please re-export the model with newer version of optimum and diffusers.)r   timestepr   zThe exported unet onnx model expects a non scalar timestep input. We will have to unsqueeze the timestep input at each iteration which might be inefficient. Please re-export the pipeline with newer version of optimum and diffusers to avoid this warning.)	r^   ri   hasattrr   r   r   r   r   input_shapesrj   r   rk   ro   rq   rr   ri   )  s   zORTUnet.__init__NTsampler   encoder_hidden_statestimestep_condcross_attention_kwargsadded_cond_kwargsreturn_dictc                    s,  t |tj}t| jd dkr|d}||||d|pi |p!i }	| jrmd|ji}
d }d| jj	j
vr8d|i}| j|	|
|d\ | jjdkrP| j| j n| j  | j| j | j   fdd	| jD }n| ||	}| jd |}| ||}|d|d
< |st| S tdi |S )Nr   r   )r  r   r  r  
out_sampleLatentConsistencyModelknown_output_shapesknown_output_bufferscpuc                        i | ]}| |  | qS rq   viewru   r   output_buffersoutput_shapesrq   rr   rx   g       z#ORTUnet.forward.<locals>.<dictcomp>r  rq   )r}   r   Tensorr   r  	unsqueezerK   shaper   rp   r   _prepare_io_bindingr{   r   r   run_with_iobinding_io_bindingsynchronize_inputssynchronize_outputsoutput_names_prepare_onnx_inputsrun_prepare_onnx_outputsrc   r   r|   r"   )rj   r  r   r  r  r  r  r  	use_torchmodel_inputsr  r  model_outputsonnx_inputsonnx_outputsrq   r  rr   forward;  sF   

	



zORTUnet.forward)NNNT)r   r   r   ri   r	   npndarrayr   r  r   r   r   r   r   r(  r   rq   rq   ro   rr   rW   (  s*    rW   c                   @   s   e Zd Z					ddeejejf deejejf deejejf deejejf deeejejf  deeejejf  d	eeejejf  d
ee	e
ef  defddZdS )rX   NThidden_statesr  pooled_projectionsr   guidancetxt_idsimg_idsjoint_attention_kwargsr  c
                    s  t |tj}
|||||||d|pi }| jr^d|ji}d }d| jjjvr)d|i}| j|||d\ | j	j
dkrA| j| j n| j  | j| j | j   fdd| jD }n| |
|}| jd |}| |
|}|d|d< |	st| S td	i |S )
N)r+  r  r,  r   r-  r.  r/  out_hidden_statesFluxr  r  c                    r  rq   r  r  r  rq   rr   rx     r  z*ORTTransformer.forward.<locals>.<dictcomp>r+  rq   )r}   r   r  rK   r  r   rp   r   r  r{   r   r   r  r  r  r  r  r   r!  r"  rc   r   r|   r"   )rj   r+  r  r,  r   r-  r.  r/  r0  r  r#  r$  r  r  r%  r&  r'  rq   r  rr   r(  v  sD   



zORTTransformer.forward)NNNNT)r   r   r   r	   r)  r*  r   r  r   r   r   r   r   r(  rq   rq   rq   rr   rX   u  s2    	
rX   c                	   @   sN   e Zd Z			d	deejejf deeejejf  dee	 de	fddZ
dS )
rY   NT	input_idsattention_maskoutput_hidden_statesr  c                    sX  t |tj}d|i}| jr?| |\ | jjdkr"| j| j	 n| j	
  | j| j	 | j	   fdd| jD }n| ||}| jd |}	| ||	}|rg |d< t| dr`| jn| j}
t|
D ]}|d |d|  qg|d |d nt| dr| jn| j}
t|
D ]}|d| d  q|st| S td	i |S )
Nr3  r  c                    r  rq   r  r  r  rq   rr   rx     r  z*ORTTextEncoder.forward.<locals>.<dictcomp>r+  num_hidden_layerszhidden_states.last_hidden_staterq   )r}   r   r  rK   r  r{   r   r   r  r  r  r  r  r   r!  r"  r   r6  num_decoder_layersr   appendrc   r   r   r|   r"   )rj   r3  r4  r5  r  r#  r$  r%  r&  r'  
num_layersr   rq   r  rr   r(    s4   

zORTTextEncoder.forward)NNT)r   r   r   r	   r)  r*  r   r  r   r   r(  rq   rq   rq   rr   rY     s    rY   c                       J   e Zd Z fddZ		d
deejejf de	ej
 defdd	Z  ZS )rZ   c                    L   t  j|i | t| jds$td | jdt| jjd  d d S d S )Nscaling_factorzThe `scaling_factor` attribute is missing from the VAE encoder configuration. Please re-export the model with newer version of optimum and diffusers to avoid this warning.r&   r2   r=  	r^   ri   r   r   r   r   r   r   block_out_channelsr  ro   rq   rr   ri         zORTVaeEncoder.__init__NTr  	generatorr  c           	         s   t |tj}d|i}| jr?| |\ | jjdkr"| j| j	 n| j	
  | j| j	 | j	   fdd| jD }n| ||}| jd |}| ||}d|v r]|d|d< d|v rkt|dd|d	< |sst| S td
i |S )Nr  r  c                    r  rq   r  r  r  rq   rr   rx     r  z)ORTVaeEncoder.forward.<locals>.<dictcomp>latent_samplelatentslatent_parameters)rg   latent_distrq   )r}   r   r  rK   r  r{   r   r   r  r  r  r  r  r   r!  r"  rc   r8   r   r|   r"   )	rj   r  rB  r  r#  r$  r%  r&  r'  rq   r  rr   r(    s.   


zORTVaeEncoder.forwardNTr   r   r   ri   r	   r)  r*  r   r  r   	Generatorr   r(  r   rq   rq   ro   rr   rZ         rZ   c                       r;  )r\   c                    r<  )Nr=  zThe `scaling_factor` attribute is missing from the VAE decoder configuration. Please re-export the model with newer version of optimum and diffusers to avoid this warning.r&   r2   r>  r?  r  ro   rq   rr   ri     rA  zORTVaeDecoder.__init__NTrC  rB  r  c           	         s   t |tj}d|i}| jr?| |\ | jjdkr"| j| j	 n| j	
  | j| j	 | j	   fdd| jD }n| ||}| jd |}| ||}d|v r]|d|d< |set| S tdi |S )NrC  r  c                    r  rq   r  r  r  rq   rr   rx   8  r  z)ORTVaeDecoder.forward.<locals>.<dictcomp>rD  rq   )r}   r   r  rK   r  r{   r   r   r  r  r  r  r  r   r!  r"  rc   r   r|   r"   )	rj   rC  rB  r  r#  r$  r%  r&  r'  rq   r  rr   r(  "  s&   

zORTVaeDecoder.forwardrG  rH  rq   rq   ro   rr   r\     rJ  r\   c                   @   sD   e Zd Zddee dee fddZdd Zdd	 Ze	d
d Z
dS )rb   Nencoderdecoderc                 C   s.   || _ || _| jttd | j | jhd d S )NrM   )rK  rL  r_   r`   ra   )rj   rK  rL  rq   rq   rr   ri   H  s   "zORTVae.__init__c                 O      | j |i |S rt   )rL  r  rq   rq   rr   decodeN     zORTVae.decodec                 O   rM  rt   )rK  r  rq   rq   rr   encodeQ  rO  zORTVae.encodec                 C   s   | j jS rt   )rL  r   )rj   rq   rq   rr   r   T  s   zORTVae.configr   )r   r   r   r   rZ   r\   ri   rN  rP  r   r   rq   rq   rq   rr   rb   G  s    rb   z
    This Pipeline inherits from [`ORTDiffusionPipeline`] and is used to run inference with the ONNX Runtime.
    The pipeline can be loaded from a pretrained pipeline using the [`ORTDiffusionPipeline.from_pretrained`] method.
c                   @      e Zd ZdZdZdZeZdS )ORTStableDiffusionPipelinez
    ONNX Runtime-powered stable diffusion pipeline corresponding to [diffusers.StableDiffusionPipeline](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline).
    text-to-imagepromptN)r   r   r   __doc__r   main_input_namer   rf   rq   rq   rq   rr   rR  _  
    rR  c                   @   rQ  )!ORTStableDiffusionImg2ImgPipelinez
    ONNX Runtime-powered stable diffusion pipeline corresponding to [diffusers.StableDiffusionImg2ImgPipeline](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/img2img#diffusers.StableDiffusionImg2ImgPipeline).
    image-to-imageimageN)r   r   r   rU  r   rV  r   rf   rq   rq   rq   rr   rX  j  rW  rX  c                   @   rQ  )!ORTStableDiffusionInpaintPipelinez
    ONNX Runtime-powered stable diffusion pipeline corresponding to [diffusers.StableDiffusionInpaintPipeline](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/inpaint#diffusers.StableDiffusionInpaintPipeline).
    
inpaintingrT  N)r   r   r   rU  r   rV  r   rf   rq   rq   rq   rr   r[  u  rW  r[  c                   @   (   e Zd ZdZdZdZeZ	dddZdS )ORTStableDiffusionXLPipelinez
    ONNX Runtime-powered stable diffusion pipeline corresponding to [diffusers.StableDiffusionXLPipeline](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/stable_diffusion_xl#diffusers.StableDiffusionXLPipeline).
    rS  rT  Nc                 C   s$   t || | }tj|g|d}|S N)r   )r`   r   tensor)rj   original_sizecrops_coords_top_lefttarget_sizer   text_encoder_projection_dimadd_time_idsrq   rq   rr   _get_add_time_ids  s   z.ORTStableDiffusionXLPipeline._get_add_time_idsrt   )	r   r   r   rU  r   rV  r   rf   rf  rq   rq   rq   rr   r^    s    r^  c                   @   r]  )#ORTStableDiffusionXLImg2ImgPipelinez
    ONNX Runtime-powered stable diffusion pipeline corresponding to [diffusers.StableDiffusionXLImg2ImgPipeline](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/stable_diffusion_xl#diffusers.StableDiffusionXLImg2ImgPipeline).
    rY  rT  Nc                 C   v   | j jrt|| |f }t|| |f }nt|| | }t|| | }tj|g|	d}tj|g|	d}||fS r_  r   rI   r`   r   r`  rj   ra  rb  rc  aesthetic_scorenegative_aesthetic_scorenegative_original_sizenegative_crops_coords_top_leftnegative_target_sizer   rd  re  add_neg_time_idsrq   rq   rr   rf       z5ORTStableDiffusionXLImg2ImgPipeline._get_add_time_idsrt   )	r   r   r   rU  r   rV  r   rf   rf  rq   rq   rq   rr   rg        rg  c                   @   r]  )#ORTStableDiffusionXLInpaintPipelinez
    ONNX Runtime-powered stable diffusion pipeline corresponding to [diffusers.StableDiffusionXLInpaintPipeline](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/stable_diffusion_xl#diffusers.StableDiffusionXLInpaintPipeline).
    rZ  r\  Nc                 C   rh  r_  ri  rj  rq   rq   rr   rf    rq  z5ORTStableDiffusionXLInpaintPipeline._get_add_time_idsrt   )	r   r   r   rU  rV  r   r   rf   rf  rq   rq   rq   rr   rs    rr  rs  c                   @   rQ  )!ORTLatentConsistencyModelPipelinez
    ONNX Runtime-powered stable diffusion pipeline corresponding to [diffusers.LatentConsistencyModelPipeline](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/latent_consistency#diffusers.LatentConsistencyModelPipeline).
    rS  rT  N)r   r   r   rU  r   rV  r   rf   rq   rq   rq   rr   rt    rW  rt  c                   @   rQ  )(ORTLatentConsistencyModelImg2ImgPipelinea  
    ONNX Runtime-powered stable diffusion pipeline corresponding to [diffusers.LatentConsistencyModelImg2ImgPipeline](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/latent_consistency_img2img#diffusers.LatentConsistencyModelImg2ImgPipeline).
    rY  rZ  N)r   r   r   rU  r   rV  r   rf   rq   rq   rq   rr   ru    rW  ru  c                   @   s   e Zd ZdZdd ZdS )ORTUnavailablePipelineNc                 O   s   t d| jj d| j d)NzThe pipeline zW is not available in the current version of `diffusers`. Please upgrade `diffusers` to z
 or later.)NotImplementedErrorrp   r   MIN_VERSIONr  rq   rq   rr   ri     s
   zORTUnavailablePipeline.__init__)r   r   r   rx  ri   rq   rq   rq   rr   rv    s    rv  0.29.0)StableDiffusion3Img2ImgPipelineStableDiffusion3Pipelinec                   @   rQ  )ORTStableDiffusion3Pipelinez
        ONNX Runtime-powered stable diffusion pipeline corresponding to [diffusers.StableDiffusion3Pipeline](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusion3Pipeline).
        rS  rT  N)r   r   r   rU  r   rV  r{  rf   rq   rq   rq   rr   r|    rW  r|  c                   @   rQ  )"ORTStableDiffusion3Img2ImgPipelinez
        ONNX Runtime-powered stable diffusion pipeline corresponding to [diffusers.StableDiffusion3Img2ImgPipeline](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/img2img#diffusers.StableDiffusion3Img2ImgPipeline).
        rY  rZ  N)r   r   r   rU  r   rV  rz  rf   rq   rq   rq   rr   r}    rW  r}  c                   @      e Zd ZdZdS )r|  ry  Nr   r   r   rx  rq   rq   rq   rr   r|        c                   @   r~  )r}  ry  Nr  rq   rq   rq   rr   r}     r  0.30.0)FluxPipelineStableDiffusion3InpaintPipelinec                   @   rQ  )"ORTStableDiffusion3InpaintPipelinez
        ONNX Runtime-powered stable diffusion pipeline corresponding to [diffusers.StableDiffusion3InpaintPipeline](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/inpaint#diffusers.StableDiffusion3InpaintPipeline).
        r\  rT  N)r   r   r   rU  r   rV  r  rf   rq   rq   rq   rr   r  '  rW  r  c                   @   rQ  )ORTFluxPipelinez
        ONNX Runtime-powered stable diffusion pipeline corresponding to [diffusers.FluxPipeline](https://huggingface.co/docs/diffusers/api/pipelines/flux/text2img#diffusers.FluxPipeline).
        rS  rT  N)r   r   r   rU  r   rV  r  rf   rq   rq   rq   rr   r  1  rW  r  c                   @   r~  )r  r  Nr  rq   rq   rq   rr   r  =  r  c                   @   r~  )r  r  Nr  rq   rq   rq   rr   r  @  r  Tr   throw_error_if_not_existc                 C   s>   t D ]}|j| ks|jj| kr|  S q|rtd|  d S )Nz5ORTDiffusionPipeline can't find a pipeline linked to )SUPPORTED_ORT_PIPELINESr   rf   r   )r   r  r   rq   rq   rr   r   T  s   
r   fluxzlatent-consistencyzstable-diffusionzstable-diffusion-3zstable-diffusion-xlc                 C   sD   dd }||}|d ur|  |d }|d ur|S td| d| )Nc                 S   sB   t D ]}| D ]\}}|j| ks|jj| kr|    S qqd S rt   ) SUPPORTED_ORT_PIPELINES_MAPPINGSry   r   rf   )r   ort_pipelines_mapping
model_namer   rq   rq   rr   _get_model_name  s   
z,_get_task_ort_class.<locals>._get_model_namez3ORTPipelineForTask can't find a pipeline linked to z for )r   r   )mappingr   r  r  
task_classrq   rq   rr   _get_task_ort_class  s   	r  c                   @   s&   e Zd ZdZeedefddZdS )ORTPipelineForTaskzmodel_index.jsonrs   c              
   K   s   | dd| dd | dd| dd | dd | dd | dd d	}| j|fi |}t|tr8|d
 n|}|d }t| j|}|j|fi |S )Nr   Fr   r   r   r   r   r   r   r   r   )r   r   r}   r   r  r  r   )r   pretrained_model_or_pathrk   load_config_kwargsr   
class_namer   rq   rq   rr   r     s   






	z"ORTPipelineForTask.from_pretrainedN)r   r   r   r   r   r   r9   r   rq   rq   rq   rr   r    s
    r  c                   @      e Zd ZeZeZdS )ORTPipelineForText2ImageN)r   r   r   r   rf    ORT_TEXT2IMAGE_PIPELINES_MAPPINGr  rq   rq   rq   rr   r        r  c                   @   r  )ORTPipelineForImage2ImageN)r   r   r   r   rf   !ORT_IMAGE2IMAGE_PIPELINES_MAPPINGr  rq   rq   rq   rr   r    r  r  c                   @   r  )ORTPipelineForInpaintingN)r   r   r   r   rf   ORT_INPAINT_PIPELINES_MAPPINGr  rq   rq   rq   rr   r    r  r  )T)~r   rd   loggingr   collectionsr   pathlibr   tempfiler   typingr   r   r   r   r	   numpyr)  r   diffusers.configuration_utilsr
   diffusers.pipelinesr   r   r   r   r   r   r   r   r   r   r   "diffusers.pipelines.pipeline_utilsr   diffusers.schedulersr   %diffusers.schedulers.scheduling_utilsr   diffusers.utils.constantsr   diffusers.utils.hub_utilsr   r   huggingface_hubr   r   huggingface_hub.utilsr   transformersr   r    transformers.file_utilsr!   transformers.modeling_outputsr"   transformers.utilsr#   onnxruntimer$   r%   exporters.onnxr'   utilsr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   baser3   r4   r5   r6   r7   !diffusers.models.autoencoders.vaer8   diffusers.models.vae	getLoggerr   r   r9   r   rW   rX   rY   rZ   r\   rb   ORT_PIPELINE_DOCSTRINGrR  rX  r[  r^  rg  rs  rt  ru  rv  r;   rz  r{  r|  r}  r  r  r  r  r  r   r   r   r  r  r  r  r  r  r  r  r  rq   rq   rq   rr   <module>   s   40

   8"M</61


%%


		
		