o
    pis                  (   @   sr  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m	Z	m
Z
m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 dd	l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! e r}d dl"Z"d dl"m#Z# d dl$mZ% d dl$m&Z' d dl$mZ( e rd dl)Z)d dl)m*Z* d dl+m,Z, d dl-m.Z.m/Z/ dZ0dZ1dZ2dZ3dgZ4e5e6Z7d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Z8i Z9e8D ]	Z:e9;e8e:  qdPde<fddZ=dQdee	ej> e?f fd d!Z@ed"d# ZAd$d% ZBd&d' ZC	dPd(d)ZD					dRd*d+ZE		,						dSd-d.ZFd/e?d0e?d1e	e d2ed3e<d4e?d5ee?ejGf d6ee?ej>f fd7d8ZH	9dTd:ee?eIf d;ee?eIf d<e?fd=d>ZJd?d@ ZKd/e?d0e?d1e	e d2ed3e<dAed5ejGdBedCedDe
eee?ejLf e?f  dEe
eeeMe?f eeMe?f f  dFe
ee?ej>f  dGe<dHee?e?f d4e?dIe<dJe?dKe<d6ee?ej>f f&dLdMZNdNdO ZOdS )U    N)Path)AnyDictListOptionalUnion)
model_info)validate_hf_hub_args)version   )__version__)
FLAX_WEIGHTS_NAMEONNX_EXTERNAL_WEIGHTS_NAMEONNX_WEIGHTS_NAMESAFETENSORS_WEIGHTS_NAMEWEIGHTS_NAMEget_class_from_dynamic_moduleis_accelerate_availableis_peft_availableis_transformers_availablelogging)is_compiled_module)PreTrainedModel)r   )SAFE_WEIGHTS_NAME)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)PreTrainedTokenizerPreTrainedTokenizerFastr   FeatureExtractionMixinProcessorMixinImageProcessingMixin	ORTModule)	diffuserstransformerszonnxruntime.trainingreturnc           
      C   s  g }t  }|pg }| D ]8}tj|\}}t|ddkr(|dd |v r(q|dkr6|tj| q|dkrC|tj| q|D ]?}tj|\}}tj|\}}|	drd|
dd}n|}tjtj||}	|	 d}	|	|vrt|	 d  d	S qFd
S )a  
    Checking for safetensors compatibility:
    - By default, all models are saved with the default pytorch serialization, so we use the list of default pytorch
      files to know which safetensors files are needed.
    - The model is safetensors compatible only if there is a matching safetensors file for every default pytorch file.

    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   z.binz.safetensorspytorch_modelmodelz
 not foundFT)setospathsplitextlensplitappendnormpathadd
startswithreplacejoinloggerwarning)
	filenamesvariantpassed_componentspt_filenamessf_filenamesfilename_	extensionr3   expected_sf_filename rH   h/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/diffusers/pipelines/pipeline_loading_utils.pyis_safetensors_compatible\   s2   $

rJ   c                    s  t ttttg}t r|tttg7 }dd |D }dd |D }dd urUt	
dd| d d d dd| d	t	
dd| dd| d
 dt	
dd| d dd| d	 t	
dd| dd| dd urfdd| D }fdd| D }||B }nt } fdd| D }fdd| D }	||	B }
t|}fdd}|
D ]}||}||vr|| q||fS )Nc                 S      g | ]	}| d d qS ).r   r6   .0wrH   rH   rI   
<listcomp>       z/variant_compatible_siblings.<locals>.<listcomp>c                 S   rK   )rL   rM   rN   rH   rH   rI   rQ      rR   z\d{5}-of-\d{5}(|z)\.(-z)$z
)\.index\.z\.json$z)(-z)?\.(z)\.index\.jsonc                    (   h | ]}  |d d dur|qS r.   rS   Nmatchr6   rO   f)variant_file_rerH   rI   	<setcomp>      ( z.variant_compatible_siblings.<locals>.<setcomp>c                    rW   rX   rY   r[   )variant_index_rerH   rI   r^      r_   c                    rW   rX   rY   r[   )non_variant_file_rerH   rI   r^      r_   c                    rW   rX   rY   r[   )non_variant_index_rerH   rI   r^      r_   c              	      s   d| v r|  dd }|S td  | d ur7| dd  d dd| ddd   }|S | dd  d d| dd  }|S )Nindexzindex.z^(.*?)rV   r   rL      )r;   recompilerZ   r6   r<   )rD   variant_filename)transformers_index_formatr@   rH   rI   convert_to_variant   s   2(z7variant_compatible_siblings.<locals>.convert_to_variant)r   r   r   r   r   r   TRANSFORMERS_WEIGHTS_NAMETRANSFORMERS_SAFE_WEIGHTS_NAMETRANSFORMERS_FLAX_WEIGHTS_NAMEre   rf   r<   r1   r9   )r?   r@   weight_namesweight_prefixesweight_suffixsvariant_weightsvariant_indexesvariant_filenamesnon_variant_weightsnon_variant_indexesnon_variant_filenamesusable_filenamesri   r\   rg   rH   )ra   rb   rh   r@   r]   r`   rI   variant_compatible_siblings   sL   .""$
	
rw   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 rH   )	rfilename)rO   siblingrH   rH   rI   r^          z0warn_deprecated_model_variant.<locals>.<setcomp>)r@   c                 S   s6   g | ]}d  |d dd |d dd  qS )rL   Nrd   r   )r<   r6   r[   rH   rH   rI   rQ      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   siblingsrw   r1   issubsetwarningswarnFutureWarning)	pretrained_model_name_or_pathrx   r@   ry   model_filenamesinfor?   comp_model_filenamesrE   rH   rH   rI   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_modelr0   )r0   r   rH   rH   rI   _unwrap_model   s   
r   c                    s   |sMt |  t |} fdd| D }d}	| D ]\}}
|
dur,t||
r,|
}	q|| }t|}|j}t||	sKt||  d| d|	 dS t	
d||  d dS )zNSimple helper method to raise or warn in case incorrect module has been passedc                       i | ]	}|t  |d qS NgetattrrO   clibraryrH   rI   
<dictcomp>   rR   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   keysitems
issubclassr   	__class__
ValueErrorr=   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_clsrH   r   rI   maybe_raise_or_warn   s(   


r   c           
         s   t j||}|r"t|| }t||  fdd| D }	 |	fS t jt j|| d rGt|| d |d  fdd| D }	 |	fS t| t| fdd| D }	 |	fS )zaSimple helper method to retrieve class object of module as well as potential parent class objectsc                       i | ]}| qS rH   rH   r   r   rH   rI   r     r|   z0get_class_obj_and_candidates.<locals>.<dictcomp>.py)module_filer   c                    r   rH   rH   r   r   rH   rI   r   #  r|   c                    r   r   r   r   r   rH   rI   r   )  rR   )	r2   r3   r<   r   r   isfiler   r   r   )
r   r   r   	pipelinesr   component_name	cache_dircomponent_folderpipeline_moduler   rH   )r   r   rI   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   ry   )endswithr   r   parentabsoluteCUSTOM_PIPELINE_FILE_NAMEr   )custom_pipelinerepo_idhub_revisionr   r   ry   r3   	file_namerH   rH   rI   _get_custom_pipeline_class.  s"   

r   Fc	                 C   s   |d urt ||||||dS | jdkr| S t| jdd }	|p%|d }|s,td|dr7|dd  n|}t|	|}
|rnd	d
l	m
} ||
}|d ur]td|j d|
j d nt|
j d|
j d |pm|
}
|
S )N)r   r   r   r   ry   r#   rL   r   _class_namezoThe class name could not be found in the configuration file. Please make sure to pass the correct `class_name`.Flax   rd   )_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__r6   r   r:   r   auto_pipeliner   r=   r   )r   configload_connected_pipeliner   r   r   r   r   ry   diffusers_modulepipeline_clsr   connected_pipeline_clsrH   rH   rI   _get_pipeline_classO  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.0rL   r   r0   pytorch)r+   	file_type	frameworkTforce_downloadFproxieslocal_files_onlyrx   ry   	subfolder)
r   return_unused_kwargsreturn_commit_hashr   r   r   rx   ry   r   
user_agentconfig_classz=`config_class` cannot be None. Please double-check the model.)r   r   r   r   rx   ry   r   dtype)r   r   r
   parser,   r   base_versionr   r   r   r   r   r6   r!   load_configr2   r3   r<   pop
accelerateinit_empty_weightsfrom_configr   r   r    to)r   r   r   r   r   r   r   r   kwargsr   rE   transformers_versionis_transformers_modelr   is_diffusers_modelr0   config_pathr   r   unused_kwargscommit_hashr   rH   rH   rI   _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 )NrS   r   cpurd   )listr   copyr5   r7   )r   r   r   
device_idsdevice_cycledevice_id_component_mappingcurrent_device_index	component	device_idcomponent_memorycurr_device_memoryrH   rH   rI   _assign_components_to_devices  s"   
r   c                    s  ddl m} |dtj i }| D ]q\}	\}
}|dr"tdt||
}t	}d }|	|v rRt
|
|||||	| t  ||	 }W d    n1 sLw   Y  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   z3Flax pipelines are not supported with `device_map`.r   r   r   r   rx   ry   )r   r   r   r   r   pipeline_classr   r   r   r   r   r   rx   ry   c                    s0   i | ]\}}t |tjjr|t| d d qS )r    )r   torchnnModuler   )rO   module_namemoduler   rH   rI   r   -  s    z)_get_final_device_map.<locals>.<dictcomp>c                 S      | d S Nrd   rH   itemrH   rH   rI   <lambda>2      z'_get_final_device_map.<locals>.<lambda>T)keyreversec                 S   r  r  rH   r  rH   rH   rI   r
  6  r  c                 S   s   i | ]\}}|d kr||qS )r   rH   )rO   kvrH   rH   rI   r   7  s    )r   )r+   r   getr   float32r   r:   r   hasattrALL_IMPORTABLE_CLASSESr   r   r   r   dictsortedr   r5   r   )
device_mapr   r   	init_dictr   
max_memoryr   r   init_empty_modulesr   r   r   r   r   loaded_sub_modelr   final_device_mapr   r   
componentsr   rH   r  rI   _get_final_device_map  sx   


	








r  r   providersess_optionsr  r  offload_folderoffload_state_dictmodel_variants	from_flaxr@   low_cpu_mem_usagec           !   	   C   s  t | ||||||d\}}d}| D ]\}}|dur&t||r&|| d }q|du rO|j}|tp7|t}|rAd|v rA|  td| d| dt dt	||}t
tdd	 }i }t|tjjrk||d
< t||jry||d< ||d< t||j}t rtttjj}nd}t ot|to|tdk}|s|r|	|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< tjtj||r|tj||fi |}n||fi |}t |tjjrDt |	t!rDt"|dd |	d dk} | r=t#||$ |	dd	d |S t#||	dd |S ) zLHelper method to load the module `name` from `library_name` and `class_name`r   Nrd   dummyzThe component z of zT cannot be loaded as it does not seem to have any of the loading methods defined in rL   r   r   r  r  r   r   r  r  r   r!  r@   Tr#  z4.27.0zWhen passing `variant='zS'`, please make sure to upgrade your `transformers` version to at least 4.27.0.dev0r$  F)recurser   r   )
state_dictr  force_hooksmain_device)r  r(  )%r   r   r   r   r:   DUMMY_MODULES_FOLDER!TRANSFORMERS_DUMMY_MODULES_FOLDERr   r  r   r   r   r   r6   r   r  r  r$   r!   r   r
   r   r,   r   r   r   r   ImportErrorr2   r3   isdirr<   r   r  r   r   r'  )!r   r   r   r   r   r   r   r  r  r  r  r   r!  r"  r   r#  r@   r$  r   r   r   load_method_namer   none_moduleis_dummy_pathload_methodr   loading_kwargsr   r   r   r  needs_offloading_to_cpurH   rH   rI   load_sub_modelJ  s   


	



r4  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}|j
j}	||	fS )NrL   r   r   r   )r   r   r   r6   r   r   r   r5   r  LOADABLE_CLASSESr   )
r  r   r   not_compiled_moduler   module_path_itemspipeline_dirr3   r   r   rH   rH   rI   _fetch_class_library_tuple  s   
r:  )NNr   )NNNNN)NFNNNNNN)r   )Pr   r2   re   r   pathlibr   typingr   r   r   r   r   r   huggingface_hubr   huggingface_hub.utilsr	   	packagingr
   r   r   utilsr   r   r   r   r   r   r   r   r   r   utils.torch_utilsr   r,   r   transformers.utilsrl   r   rk   rj   r   r   accelerate.hooksr   accelerate.utilsr   r   
INDEX_FILEr   r*  r+  CONNECTED_PIPES_KEYS
get_loggerr   r=   r6  r  r   updateboolrJ   PathLikestrrw   r   r   r   r   r   r   r   r   floatr   r  deviceintr4  r:  rH   rH   rH   rI   <module>   s  0
	0C
 

#
3
U


V	


 