o
    Gi                  2   @   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mZ d dl	Z	d dl
Z
d dlZd dlmZmZmZmZ d dlmZmZ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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*m+Z+ e$ rd dl,Z,d dl,m-Z-m.Z. d dl/m0Z1 d dl/mZ2 e%ddrd dl/mZ3 e" rd dl4Z4d dl4m5Z5 d dl6m7Z7 d dl8m9Z9m:Z: dZ;dZ<dZ=dZ>dgZ?e&@eAZBddgddgddgddgddgdddgddgddgddgddgddgddddgid ZCi ZDeCD ]	ZEeDFeCeE  qdd!eGfd"d#ZHd$d% ZId&d' ZJdd!eKejL eMB fd(d)ZNed*d+ ZOd,d- ZPd.d/ ZQd0d1 ZR	dd2d3ZS					dd4d5ZT		6						dd7d8ZUd9eMd:eMd;eKe d<ed=eGd>eMd?eMejVB d@eMejLB fdAdBZW	CddDeXeMeYf dEeXeMeYf dFeMfdGdHZZdIdJ Z[	dd9eMd:eMd;eKe d<ed=eGdKed?ejVdLedMedNeXeMej\f eMB dB dOeXe]eMB e]eMB f dB dPeMejLB dB dQeGdReXeMeMf d>eMdSeGdTeMdUeGd@eMejLB dVeGdWeXeMef dB dXedYeGdZedB f0d[d\Z^d]e_d^eMd_eGd!efd`daZ`dbdc ZaddeMdTeMdeeXd!eXfdfdgZbdhdi ZcdjeMdeeXfdkdlZddmeXdneXdoeXddeMd!eXf
dpdqZe			ddreMdseXeMef dteKeM dB dueKeM dB dTeMdB f
dvdwZf	ddxeKeM dyeKeM dVeGdSeGdzeGd{eGd|eGdTeMdB d!eKeM fd}d~ZgdreMdeMdeMdeMdeMdeGdeMdeMfddZhdd Zidd ZjdS )    N)Path)AnyCallable)	DDUFEntry	ModelCard
model_infosnapshot_download)HfHubHTTPErrorOfflineModeIsEnabledvalidate_hf_hub_args)version   )__version__)FLAX_WEIGHTS_NAMEONNX_EXTERNAL_WEIGHTS_NAMEONNX_WEIGHTS_NAMESAFETENSORS_WEIGHTS_NAMEWEIGHTS_NAME_maybe_remap_transformers_class	deprecateget_class_from_dynamic_moduleis_accelerate_availableis_peft_availableis_transformers_availableis_transformers_versionlogging)is_compiled_module   )_load_tokenizer_from_dduf"_load_transformers_model_from_dduf)PreTrainedModelPreTrainedTokenizerBase)SAFE_WEIGHTS_NAME)r   <=4.56.2)r   )dispatch_model)remove_hook_from_module)compute_module_sizesget_max_memoryzdiffusion_pytorch_model.binzpipeline.pyzdiffusers.utilsztransformers.utilspriorsave_pretrainedfrom_pretrained)
ModelMixinSchedulerMixinDiffusionPipelineOnnxRuntimeModelBaseGuidance)PreTrainedTokenizerPreTrainedTokenizerFastr    FeatureExtractionMixinProcessorMixinImageProcessingMixin	ORTModule)	diffuserstransformerszonnxruntime.trainingreturnc                    s  t ttttg}t r|ttg7 }tddr|t	g7 }dd |D }dd |D }d}t
dd| d	| d| d
| d	d| d}t
dd| d| dd| d}	|p]g } ri fdd| D } i }
| D ]%}t|ddksyqm|d\}}||v rqm|
|g  |
| | qmt }|
s|durt| |}|st| |	}tdd |D S |
 D ]6\}}g }t }|durt||}|st||	}|D ]}tj|\}}|dk}|| qt|s dS qdS )a  
    Checking for safetensors compatibility:
    - The model is safetensors compatible only if there is a safetensors file for each model component present in
      filenames.

    Converting default pytorch serialized filenames to safetensors serialized filenames:
    - For models from the diffusers library, just replace the ".bin" extension with ".safetensors"
    - For models from the transformers library, the filename changes from "pytorch_model" to "model", and the ".bin"
      extension is replaced with ".safetensors"
    r#   r$   c                 S      g | ]	}| d d qS .r   split.0w rB   ^/home/ubuntu/.local/lib/python3.10/site-packages/diffusers/pipelines/pipeline_loading_utils.py
<listcomp>|       z-is_safetensors_compatible.<locals>.<listcomp>c                 S   r:   r<   r=   r?   rB   rB   rC   rD   ~   rE   \d{5}-of-\d{5}(|)\.(-)$)(-)?\.(c                    s$   h | ]}t j|d   v r|qS )r   )ospathr>   r@   ffolder_namesrB   rC   	<setcomp>   s   $ z,is_safetensors_compatible.<locals>.<setcomp>/r   Nc                 s   s    | ]}d |v V  qdS ).safetensorsNrB   )r@   filenamerB   rB   rC   	<genexpr>   s    z,is_safetensors_compatible.<locals>.<genexpr>rX   FT)r   r   r   r   r   r   TRANSFORMERS_WEIGHTS_NAMETRANSFORMERS_SAFE_WEIGHTS_NAMEr   TRANSFORMERS_FLAX_WEIGHTS_NAMErecompilejoinlenr>   
setdefaultappendsetfilter_with_regexanyitemsrP   rQ   splitext)	filenamespassed_componentsrU   variantweight_namesweight_prefixesweight_suffixstransformers_index_formatvariant_file_renon_variant_file_re
componentsrY   	componentcomponent_filenamefiltered_filenamescomponent_filenamesmatchesfiltered_component_filenames	extensionmatch_existsrB   rT   rC   is_safetensors_compatiblec   sj   

."



r{   c                    sT   t ttttg}t r|ttg7 }tddr|t	g7 }dd |D   fdd| D S )zIFilter model repo files for just files/folders that contain model weightsr#   r$   c                 S   r:   rF   r=   )r@   wnrB   rB   rC   rD      rE   z&filter_model_files.<locals>.<listcomp>c                    s&   g | ] t  fd dD r qS )c                 3   s    | ]}  |V  qd S N)endswith)r@   ry   rS   rB   rC   rZ      s    z0filter_model_files.<locals>.<listcomp>.<genexpr>rf   r@   allowed_extensionsr   rC   rD         & )
r   r   r   r   r   r   r[   r\   r   r]   )ri   rl   rB   r   rC   filter_model_files   s   

r   c                    s    fdd| D S )Nc                    s(   h | ]}  |d d dur|qS )rW   rG   N)matchr>   rR   
pattern_rerB   rC   rV      s   ( z$filter_with_regex.<locals>.<setcomp>rB   )ri   r   rB   r   rC   re      s   re   c                 C   s  t ttttg}t r|ttg7 }tddr|t	g7 }dd |D }dd |D }d}|d urt
dd| d	| d| d
| d	d| d}t
dd| d	d| d| d}t
d| d| d}	t
dd| d	d| d| d}
t
dd| d| dd| d}t
dd| d	d| d}d&dd}i }| D ]%}t|ddks|dg | q|d\}}||g | qt }t }| D ]R\}}|||d}t }t }t }|d urt||}t||}t||	}t||
}|s|r ||r||B n||B  qt||}t||}|||B  q|| t|dkrM|d urMd| d}t|t|dkry||krytd| d | d!| d"d#| d$| d"d#||  d% ||fS )'Nr#   r$   c                 S   r:   r;   r=   r?   rB   rB   rC   rD      rE   z/variant_compatible_siblings.<locals>.<listcomp>c                 S   r:   rF   r=   r?   rB   rB   rC   rD      rE   rH   rI   rJ   rK   rL   rM   z
)\.index\.z\.json$z.*-z\.z	\.[a-z]+$z)\.z\.index\.json$rN   rO   z)\.index\.jsonc                    s    s| S  fdd| D S )Nc                    s&   h | ] t  fd dD s qS )c                 3   s     | ]}  |d V  qdS )z*.N)r~   lstrip)r@   patr   rB   rC   rZ     s    zbvariant_compatible_siblings.<locals>.filter_for_compatible_extensions.<locals>.<setcomp>.<genexpr>r   r   ignore_patternsr   rC   rV     r   zXvariant_compatible_siblings.<locals>.filter_for_compatible_extensions.<locals>.<setcomp>rB   )ri   r   rB   r   rC    filter_for_compatible_extensions   s   zEvariant_compatible_siblings.<locals>.filter_for_compatible_extensionsrW   r    r   r   z3You are trying to load model files of the `variant=z-`, but no such modeling files are available. z
A mixture of z	 and non-z" filenames will be loaded.
Loaded z filenames:
[, z]
Loaded non-zF
If this behavior is not expected, please check your folder structure.r}   )r   r   r   r   r   r   r[   r\   r   r]   r^   r_   r`   ra   r>   rb   rc   rd   rg   re   update
ValueErrorloggerwarning)ri   rk   r   rl   rm   rn   ro   rp   variant_index_relegacy_variant_file_relegacy_variant_index_rerq   non_variant_index_rer   rr   rY   rs   _usable_filenamesvariant_filenamesrv   component_variantscomponent_legacy_variantscomponent_non_variantscomponent_variant_index_files$component_legacy_variant_index_fileserror_messagerB   rB   rC   variant_compatible_siblings   s   

."""$
	







r   c           	      C   s   t | |d d}dd |jD }t||d\}}dd |D }t|t|rBtd| d|  d	| d
| d| d| dt d S td| d|  d	| d| d|  d|  d| dt d S )Ntokenrevisionc                 S   s   h | ]}|j qS rB   	rfilenamer@   siblingrB   rB   rC   rV   E  s    z0warn_deprecated_model_variant.<locals>.<setcomp>rk   c                 S   s6   g | ]}d  |d dd |d dd  qS )r<   Nr   r   )r`   r>   rR   rB   rB   rC   rD   G  s   6 z1warn_deprecated_model_variant.<locals>.<listcomp>zYou are loading the variant z from z via `revision='z-'` even though you can load it via `variant=`z)`. Loading model variants via `revision='zK'` is deprecated and will be removed in diffusers v1. Please use `variant='z'` instead.z^'`. This behavior is deprecated and will be removed in diffusers v1. One should use `variant='z%'` instead. However, it appears that z currently does not have the required variant filenames in the 'main' branch. 
 The Diffusers team and community would be very grateful if you could open an issue: https://github.com/huggingface/diffusers/issues/new with the title 'z is missing z6 files' so that the correct variant file can be added.)r   siblingsr   rd   issubsetwarningswarnFutureWarning)	pretrained_model_name_or_pathr   rk   r   model_filenamesinfori   comp_model_filenamesr   rB   rB   rC   warn_deprecated_model_variant>  s"   (.r   c                 C   s6   t | r| j} t rddlm} t| |r| jj} | S )zUnwraps a model.r   )	PeftModel)r   	_orig_modr   peftr   
isinstance
base_modelmodel)r   r   rB   rB   rC   _unwrap_modelU  s   
r   c                    s   |sWt |  | dkrt|p|}t |} fdd| D }d}	| D ]\}}
|
dur6t||
r6|
}	q'|| }t|}|j}t||	sUt	||  d| d|	 dS t
d||  d dS )	zNSimple helper method to raise or warn in case incorrect module has been passedr8   c                       i | ]	}|t  |d qS r}   getattrr@   clibraryrB   rC   
<dictcomp>o  rE   z'maybe_raise_or_warn.<locals>.<dictcomp>Nz is of type: z, but should be z&You have passed a non-standard module z2. We cannot verify whether it has the correct type)	importlibimport_moduler   r   keysrg   
issubclassr   	__class__r   r   r   )library_namer   
class_nameimportable_classespassed_class_objnameis_pipeline_module	class_objclass_candidatesexpected_class_objclass_candidate	sub_modelunwrapped_sub_model	model_clsrB   r   rC   maybe_raise_or_warnc  s(   


r   c                 C   s^   ddl m} t|| }|rt|| }t||}|S t| }| dkr(t|p'|}t||}|S )Nr   	pipelinesr8   )r7   r   hasattrr   r   r   r   )r   r   r   r   pipeline_moduler   r   rB   rB   rC   simple_get_class_obj  s   





r   c                    s   |r|rt j||nd}|r%t|| }t||}	t| |	}
|	|
fS |rIt jt j|| d rIt|| d |d}	t| |	}
|	|
fS t	
|  | dkrXt|pW|}t |}	 fdd| D }
|	|
fS )zaSimple helper method to retrieve class object of module as well as potential parent class objectsN.py)module_filer   r8   c                    r   r}   r   r   r   rB   rC   r     rE   z0get_class_obj_and_candidates.<locals>.<dictcomp>)rP   rQ   r`   r   dictfromkeysr   isfiler   r   r   r   )r   r   r   r   r   component_name	cache_dircomponent_folderr   r   r   rB   r   rC   get_class_obj_and_candidates  s$   




r   c                 C   sf   |  drt| }|j}|j } n|d ur|  d}|} nt}|d ur*|d ur*|}t| ||||dS )Nr   )r   r   r   r   )r~   r   r   parentabsoluteCUSTOM_PIPELINE_FILE_NAMEr   )custom_pipelinerepo_idhub_revisionr   r   r   rQ   	file_namerB   rB   rC   _get_custom_pipeline_class  s"   

r   Fc	                 C   s   |d urt ||||||dS | jdkr| jdkr| S t| jdd }	|p*|d }|s1td|dr<|d	d  n|}t|	|}
|rsd
dl	m
} ||
}|d urbtd|j d|
j d nt|
j d|
j d |pr|
}
|
S )N)r   r   r   r   r   r.   ModularPipeliner<   r   _class_namezoThe class name could not be found in the configuration file. Please make sure to pass the correct `class_name`.Flax   r   )_get_connected_pipelinezLoading connected pipeline z instead of z0 as specified via `load_connected_pipeline=True`z* has no connected pipeline class. Loading )r   __name__r   r   
__module__r>   r   
startswithr   auto_pipeliner   r   r   )r   configload_connected_pipeliner   r   r   r   r   r   diffusers_modulepipeline_clsr   connected_pipeline_clsrB   rB   rC   _get_pipeline_class  s:   	
r   r   r   r   r   r   r   torch_dtypecached_folderc                 K   s  t | ||||||d\}	}
t rtttjj}nd}t o+t|	to+|tdk}t	
tdd }t|	|j}d }|}tddd}|r|	jtj|||d	d	|d
d|dd |dd|dd |dd |dd |d\}}}t  |	j|fi |}W d    n1 sw   Y  nI|rt|	dd }|d u rtd|j|||d
d|dd |dd|dd |dd |d}t  |	|}W d    n1 sw   Y  |d ur|j|d}|S )Nr   r   N/A4.20.0r<   r   r   pytorch)r7   	file_type	frameworkTforce_downloadFproxieslocal_files_onlyr   r   	subfolder)
r   return_unused_kwargsreturn_commit_hashr  r  r  r   r   r  
user_agentconfig_classz=`config_class` cannot be None. Please double-check the model.)r  r  r  r  r   r   r	  dtype)r   r   r   parser8   r   base_versionr   r    r   r   r   r>   r,   load_configrP   rQ   r`   pop
accelerateinit_empty_weightsfrom_configr   r   r+   to)r   r   r   r   r   r   r   r   kwargsr   r   transformers_versionis_transformers_modelr   is_diffusers_modelr   config_pathr	  r   unused_kwargscommit_hashr
  rB   rB   rC   _load_empty_model  s~   
















r  balancedmodule_sizesdevice_memorydevice_mapping_strategyc                 C   s   t | }||d d d  }| }i }d}| D ]9}||t|  }| | }	|| }
|	|
kr5|g|d< q||vr?|g||< n|| | ||  |	8  < |d7 }q|S )NrG   r   cpur   )listr   copyra   rc   )r  r  r   
device_idsdevice_cycledevice_id_component_mappingcurrent_device_indexrs   	device_idcomponent_memorycurr_device_memoryrB   rB   rC   _assign_components_to_devicesa  s"   
r+  c                    s  | dkr| S ddl m} |dtj i }| D ]\}	\}
}|dr(tdt||
}t	}d }|	|v rXt
|
|||||	| t  ||	 }W d    n1 sRw   Y  n<t trh |	 dtjn }t|
||||||	||dd |d	d |d
d |dd |dd |dd d}|d ur|||	< q fdd| D }tt| dd dd}t|}tt| dd dd}dd | D }d }t|dkrt||| d}i }| D ]\}}|D ]}|||< qq|S )Nr  r   r   r   r   z3Flax pipelines are not supported with `device_map`.defaultr   r  r  r  r   r   )r   r   r   r   r   pipeline_classr   r   r   r  r  r  r   r   c                    sP   i | ]$\}}t |tjjr|t|t  tr  | d tjn dd qS )r,  r  r   )r   torchnnModuler'   r   getfloat32)r@   module_namemoduler   rB   rC   r     s    z)_get_final_device_map.<locals>.<dictcomp>c                 S      | d S Nr   rB   itemrB   rB   rC   <lambda>      z'_get_final_device_map.<locals>.<lambda>T)keyreversec                 S   r6  r7  rB   r8  rB   rB   rC   r:    r;  c                 S   s   i | ]\}}|d kr||qS )r!  rB   r@   kvrB   rB   rC   r     s    )r   )r7   r   r1  r.  r2  rg   r   r   r   ALL_IMPORTABLE_CLASSESr   r  r  r   r   r  sortedr(   ra   r+  )
device_mapr-  r   	init_dictr   
max_memoryr  r   init_empty_modulesr   r   r   r   r   loaded_sub_modelsub_model_dtyper  final_device_mapr&  r(  rr   rs   rB   r5  rC   _get_final_device_map  s   


	









rJ  r-  providersess_optionsrC  rE  offload_folderoffload_state_dictmodel_variants	from_flaxrk   low_cpu_mem_usageuse_safetensorsdduf_entriesprovider_optionsdisable_mmapquantization_configc           )   	   C   s  ddl m} t| ||||||d\}}d}| D ]\}}|dur,t||r,|| d }q|du rU|j}|tp=|t}|rGd|v rG|  t	d| d| d	t
 d
t|||dud}ttd
d } i }!t|| j}"t r~tttjj}#nd}#t ot|to|#tdk}$t|tjjr|$r|#tdkr||!d< n||!d< t|| jr||!d< ||!d< ||!d< |"s|$r|	|!d< |
|!d< ||!d< ||!d< ||d|!d< ||!d< |rd|!d< |$r|!d dur|#tdk rtd| d|$r|!d du r|!d |r|$s||!d < nd!|!d < |"r||!d"< |$r+td#d$r+|!d |durNt||rNt|tjjrN|j |"|d%}%|%durN|%|!d&< |r^||!d'< ||fi |!}&n"t!j"#t!j"$||rx|t!j"$||fi |!}&n||fi |!}&t|&tjjrt|	t%rt&|&dd( |	d) d*k}'d}(t'|&d+r|&j(dur|&j(}(|'rt)|&|&* |	dd|(d, |&S t)|&|	d|(d- |&S ).zLHelper method to load the module `name` from `library_name` and `class_name`r   )PipelineQuantizationConfigr   Nr   dummyzThe component z of zT cannot be loaded as it does not seem to have any of the loading methods defined in r<   )is_ddufr   r   r   z4.56.0r  r   rK  rL  rT  rC  rE  rM  rN  rk   rR  TrP  z4.27.0zWhen passing `variant='zS'`, please make sure to upgrade your `transformers` version to at least 4.27.0.dev0rQ  FrU  z>=z4.57.0)is_diffusersr3  rV  rS  )recurser   r!  
_skip_keys)
state_dictrC  force_hooksmain_device	skip_keys)rC  r^  r`  )+
quantizersrW  r   rg   r   r   r   DUMMY_MODULES_FOLDER!TRANSFORMERS_DUMMY_MODULES_FOLDERr   rA  _get_load_methodr   r   r   r>   r,   r   r   r  r8   r   r  r    r.  r/  r0  r/   r  ImportErrorr   r   _resolve_quant_configrP   rQ   isdirr`   r   r&   r   r\  r%   r]  ))r   r   r   r   r   r-  r   rK  rL  rC  rE  rM  rN  rO  r   rP  rk   rQ  r   rR  rS  rT  rU  rV  rW  r   r   load_method_namer   none_moduleis_dummy_pathload_methodr   loading_kwargsr  r  r  model_quant_configrG  needs_offloading_to_cpur`  rB   rB   rC   load_sub_model  s   









ro  r   rh  rY  c                    s:   |rt  tr fddS t  tr fddS t |S )a>  
    Return the method to load the sub model.

    In practice, this method will return the `"from_pretrained"` (or `load_method_name`) method of the class object
    except if loading from a DDUF checkpoint. In that case, transformers models and tokenizers have a specific loading
    method that we need to use.
    c                        t  g| R i |S r}   )r   argsr  r   rB   rC   r:        z"_get_load_method.<locals>.<lambda>c                     rp  r}   )r   rq  rs  rB   rC   r:    rt  )r   r!   r    r   )r   rh  rY  rB   rs  rC   rd    s   


rd  c           
      C   s   t tdd }t|d}t| }|jdd }|jd}t|dkr+|d nd }|jd}||v o;t||}|rA|}n|t	vrH|j}t
|trT|j}	||	fS |jj}	||	fS )Nr<   r   r   r   )r   r   r   r>   r   r   r   ra   r   LOADABLE_CLASSESr   typer   )
r4  r   r   not_compiled_moduler   module_path_itemspipeline_dirrQ   r   r   rB   rB   rC   _fetch_class_library_tuple  s"   

r{  folderr   c                    sn   i } d ur5t | D ])}t j| |}t j|o||v }|o-t fddt |D }|r4 ||< q|S )Nc                 3   s$    | ]}| d d  V  qdS )r<   r   N)r>   r   )r@   pr   rB   rC   rZ     s   " z+_identify_model_variants.<locals>.<genexpr>)rP   listdirrQ   r`   rg  rf   )r|  rk   r   rO  
sub_folderfolder_path	is_foldervariant_existsrB   r   rC   _identify_model_variants  s    r  c                 C   s   d }t jt j| | drt j| | d}||fS t|d ttfrMt jt j| |d d  drMt j| |d d  d}|d d }||fS )Nr   r   r   r   )rP   rQ   r   r`   r   r"  tuple)r|  r   r   custom_class_namerB   rB   rC    _resolve_custom_pipeline_and_cls  s   r  r   c                 C   sr   | j dkr5tt|d jtdkr7ddlm}m} |} d| d| d| d	}td
d|dd d S d S d S )NStableDiffusionInpaintPipeline_diffusers_versionz0.5.1r   )r  $StableDiffusionInpaintPipelineLegacyzeYou are using a legacy checkpoint for inpainting with Stable Diffusion, therefore we are loading the z class instead of z. For better inpainting results, we strongly suggest using Stable Diffusion's official inpainting checkpoint: https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-inpainting instead or adapting your checkpoint z to the format of https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-inpainting. Note that we do not actively maintain the {StableDiffusionInpaintPipelineLegacy} class and will likely remove it in version 1.0.0.r  z1.0.0F)standard_warn)r   r   r  r  r7   r  r  r   )r-  r   r   r  r  deprecation_messagerB   rB   rC   #_maybe_raise_warning_for_inpainting  s"   	r  init_kwargspassed_pipe_kwargspassed_class_objsc           	         s   ddl m  ttj|dfddtD }| d ur5t	dkr5|D ]}d|v r4
|}q)fdd fd	d| D }| D ]\}| fd
d|j D  qM| S )Nr   )r.   z	README.mdc                    s"   i | ]}|t  j|d gd qS )Nr   )r   data)r@   prefix)	modelcardrB   rC   r     s   " z?_update_init_kwargs_with_connected_pipeline.<locals>.<dictcomp>	schedulerc                    s<    fdd  D } fdd  D }i ||}|S )Nc                    6   i | ]\}}| d d  kr|  d d|qS r   r   r   r>   replacer@   r?  rA   r  rB   rC   r          0zd_update_init_kwargs_with_connected_pipeline.<locals>.get_connected_passed_kwargs.<locals>.<dictcomp>c                    r  r  r  r  r  rB   rC   r     r  )rg   )r  connected_passed_class_objconnected_passed_pipe_kwargsconnected_passed_kwargs)r  r  r  rC   get_connected_passed_kwargs  s   

zP_update_init_kwargs_with_connected_pipeline.<locals>.get_connected_passed_kwargsc                    s4   i | ]\}}|d ur| j |fi |qS r}   )r+   )r@   r  r   )r.   r  pipeline_loading_kwargs_cprB   rC   r   
  s    c                    s    i | ]\}}d   |g|qS )r   )r`   )r@   r   rs   r  rB   rC   r     s     )pipeline_utilsr.   r   loadrP   rQ   r`   CONNECTED_PIPES_KEYSr#  ra   r  rg   r   rr   )	r  r  r  r|  pipeline_loading_kwargsconnected_pipesr?  r   connected_piperB   )r.   r  r  r  r  r  r  rC   +_update_init_kwargs_with_connected_pipeline  s$   
r  pretrained_model_nameconfig_dictri   r   c                 C   s   |  }dd | D }ttdd }t|d}i }|D ]6}	||	 d }
|
d u s1t|
ts2q |	 d|
 d}||v rC|
||	< q |
t	vrVt
||
sVt| d|  d	q ||fS )
Nc                 S   s&   g | ]\}}t |tr|d kr|qS )r   )r   r"  r>  rB   rB   rC   rD   %  r   z6_get_custom_components_and_folders.<locals>.<listcomp>r<   r   r   rW   r   z4 as defined in `model_index.json` does not exist in z. and is not a module in 'diffusers/pipelines'.)r#  rg   r   r   r   r>   r   r   strrv  r   r   )r  r  ri   r   rk   rU   r   r   custom_componentsrs   module_candidatecandidate_filerB   rB   rC   "_get_custom_components_and_folders  s$   

r  model_folder_namesr   allow_pickleuse_onnxis_onnxc	           
      C   s   |r|st || ||dstd| d| d|rg d}	|	S |r?t || ||dr?ddg}	|d ur3|n|}|s=|	dd	g7 }	|	S d
dg}	|d urI|n|}|sS|	dd	g7 }	|	S )N)rj   rU   rk   z6Could not find the necessary `safetensors` weights in z
 (variant=))*.bin*.safetensors*.onnx*.pbr  z	*.msgpackr  r  r  )r{   EnvironmentError)
rj   r  r   rR  rP  r  r  r  rk   r   rB   rB   rC   _get_ignore_patterns?  s6   	r  	dduf_filepipeline_class_namer   r  r  r   r   c                 C   s   d }|s3z	t | ||d}	W n% tttjtjfy2 }
 ztd|
 d d}|
}W Y d }
~
nd }
~
ww |sN|d urN|dd |	j	D vrNt
d| d|  d	z|dd
}t| ||||||g|d}|W S  tyx   |d u ro td|  d|w )Nr   zCouldn't connect to the Hub: z$.
Will try to load from local cache.Tc                 s   s    | ]}|j V  qd S r}   r   r   rB   rB   rC   rZ     s    z&_download_dduf_file.<locals>.<genexpr>z
Requested z file is not available in r<   )r-  dduf)r   r  r  r   r   allow_patternsr	  zCannot load model z: model is not cached locally and an error occurred while trying to fetch metadata from the Hub. Please check out the root cause in the stacktrace above.)r   r	   r
   requestsConnectionErrorhttpxNetworkErrorr   r   r   r   r   FileNotFoundErrorr  )r  r  r  r   r  r  r   r   model_info_call_errorr   er	  r   rB   rB   rC   _download_dduf_filek  sJ   



r  c                 C   sR   d}| D ]}t | | tr| | d dk}|r nq|r%tdds'tdd S d S )NFr   r8   >z4.47.1zRPlease upgrade your `transformers` installation to the latest version to use DDUF.)r   r"  r   r   )r  has_transformers_componentr?  rB   rB   rC   -_maybe_raise_error_for_incorrect_transformers  s   r  c                 C   s   |d u rd S t |  }d}d }t|dd d urt |j}nt|dd d ur3t|jtr3t |j }|r>||s>|| }|rQt	d| dd
| d d S d S )Nr   components_to_quantizequant_mappingz4The following components in the quantization config zv will be ignored as they do not belong to the underlying pipeline. Acceptable values for the pipeline components are: r   r<   )rd   r   r   r  r   r  r   r   r   r   r`   )pipe_init_dictquant_configactual_pipe_componentsmissingquant_componentsrB   rB   rC   /_maybe_warn_for_wrong_component_in_quant_config  s$   r  )NNN)NN)NNNNN)NFNNNNNN)r  r}   )kr   rP   r^   r   pathlibr   typingr   r   r  r  r.  huggingface_hubr   r   r   r   huggingface_hub.utilsr	   r
   r   	packagingr   r   r   utilsr   r   r   r   r   r   r   r   r   r   r   r   r   utils.torch_utilsr   transformers_loading_utilsr   r   r8   r    r!   transformers.utilsr"   r\   r[   r]   r  r%   accelerate.hooksr&   accelerate.utilsr'   r(   
INDEX_FILEr   rb  rc  r  
get_loggerr   r   rv  rA  r   r   boolr{   r   re   r"  PathLiker  r   r   r   r   r   r   r   r   r  r  r   floatr+  rJ  deviceintro  objectrd  r{  r  r  r  r  r  r  r  r  r  rB   rB   rB   rC   <module>   s  <

	Zi
#
!
#
3
U


{	


 0!
/



-	

,
8