o
    piV!                     @   sn  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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 eeZdd	d
diZe rcd dlmZ d dlmZm Z m!Z! dej"j#fddZ$dd Z%d"de
e&ej'f de	e& fddZ(			d#dede	e
e&ej)f  de	e
e&ej*f  de	e& dee& f
ddZ+dedee& fddZ,d d! Z-dS )$    N)OrderedDict)Path)ListOptionalUnion)EntryNotFoundError   )SAFE_WEIGHTS_INDEX_NAMESAFETENSORS_FILE_EXTENSIONWEIGHTS_INDEX_NAME_add_variant_get_model_fileis_accelerate_availableis_torch_versionloggingTransformer2DModelDiTTransformer2DModelPixArtTransformer2DModel)ada_norm_zeroada_norm_single)infer_auto_device_map)get_balanced_memoryget_max_memoryset_module_tensor_to_devicemodelc                 C   sj   t |tr3| |}d|i}|dkr!t| f||dk|d|}nt|}||d< t| fd|i|}|S )Nno_split_module_classes
sequentialbalanced_low_0)dtypelow_zero
max_memoryr    r   )
isinstancestr_get_no_split_modulesr   r   r   )r   
device_mapr    torch_dtypeno_split_modulesdevice_map_kwargs r(   b/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/diffusers/models/model_loading_utils.py_determine_device_map8   s"   

r*   c              	   C   sd   |j }t|| d d }|r0tt dd }t||}td| d| d| d |S |S )N	norm_type.r   z Changing class object to be of `z` type from `z` type.This is because `ze` is scheduled to be deprecated in a future version. Note that this DOESN'T affect the final results.)	__name___CLASS_REMAPPING_DICTget	importlibimport_modulesplitgetattrloggerinfo)config	old_classprevious_class_nameremapped_class_namediffusers_libraryremapped_classr(   r(   r)   _fetch_remapped_cls_from_configN   s   
r<   checkpoint_filevariantc                 C   s   z/t j| dd }|tkrtjj| ddW S tddr"ddini }tj	| fd	di|W S  t
yz } z?z#t| }| d
rHtdtd|  d|1 sTw   Y  W n ttfyn   td|  d|  dw W Y d}~dS d}~ww )zU
    Reads a checkpoint file, returning properly formatted errors if they arise.
    r,   cpu)devicez>=z1.13weights_onlyTmap_locationversionzYou seem to have cloned a repository without having git-lfs installed. Please install git-lfs and run `git lfs install` followed by `git lfs pull` in the folder you cloned.zUnable to locate the file z_ which is necessary to load this pretrained model. Make sure you have saved the model properly.Nz1Unable to load weights from checkpoint file for 'z' at 'z'. )ospathbasenamer2   r
   safetensorstorch	load_filer   load	Exceptionopenread
startswithOSError
ValueErrorUnicodeDecodeError)r=   r>   file_extensionweights_only_kwargefr(   r(   r)   load_state_dictb   sB   

rW   
state_dictrA   r   model_name_or_pathreturnc                 C   s   |pt d}|pt j}dtttj v }g }| 	 }|
 D ]I\}}	||vr0|| q"|| j|	jkrW|d urA| dnd}
td|
 d| d||  d|	j d		|rct| |||	|d
 q"t| |||	d q"|S )Nr@   r     zCannot load zbecause z expected shape z
, but got a  . If you want to instead overwrite randomly initialized weights, please make sure to pass both `low_cpu_mem_usage=False` and `ignore_mismatched_sizes=True`. For more information, see also: https://github.com/huggingface/diffusers/issues/1619#issuecomment-1345604389 as an example.)valuer   )r]   )rI   rA   float32setinspect	signaturer   
parameterskeysrX   itemsappendshaperQ   )r   rX   rA   r   rY   accepts_dtypeunexpected_keysempty_state_dict
param_nameparammodel_name_or_path_strr(   r(   r)   load_model_dict_into_meta   s$   

"rm   c                    s8     g  ddtjjdtf fdd|   S )Nr\   moduleprefixc                    sN   |i dg g  f}| j |  | j D ]\}}|d ur$||| d  qd S )NTr,   )_load_from_state_dict_modulesrd   )rn   ro   argsnamechild
error_msgsrK   rX   r(   r)   rK      s   
z)_load_state_dict_into_model.<locals>.load)r\   )copyrI   nnModuler"   )model_to_loadrX   r(   ru   r)   _load_state_dict_into_model   s
    r{   c                 C   s   | rt ||pdt|rtnt|}|S t |pdt|rtnt| }zt|||||||	|
d ||d}t |}W |S  ttfyG   d }Y |S w )Nr\   )
weights_name	cache_dirforce_downloadproxieslocal_files_onlytokenrevision	subfolder
user_agentcommit_hash)r   r   r	   r   as_posixr   r   EnvironmentError)is_localpretrained_model_name_or_pathr   use_safetensorsr}   r>   r~   r   r   r   r   r   r   
index_fileindex_file_in_repor(   r(   r)   _fetch_index_file   sB   
r   )N)NNN).r0   r`   rE   collectionsr   pathlibr   typingr   r   r   rH   rI   huggingface_hub.utilsr   utilsr	   r
   r   r   r   r   r   r   
get_loggerr-   r4   r.   
accelerater   accelerate.utilsr   r   r   rx   ry   r*   r<   r"   PathLikerW   rA   r   rm   r{   r   r(   r(   r(   r)   <module>   sL   (
"&
 