
    wi!                        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j        e          Zdd	d
diZ e            rd dlmZ d dlmZm Z m!Z! dej"        j#        fdZ$d Z%dde
e&ej'        f         de	e&         f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Z+dedee&         fdZ,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                     t          |t                    rX|                     |          }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_kwargss         t/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/models/model_loading_utils.py_determine_device_mapr+   8   s    *c"" Z 66zBB68HI%%,!$(88%	 
 $ JJ (
33J*4,'*5YYYGXYY
    c           	      P   |j         }t                              |                              | d         d           }|rht          j        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_classs         r*   _fetch_remapped_cls_from_configr?   N   s    #,/334GHHLLVT_M`bfgg  %3HNN34G4G4JKK !24GHH1/B 1 1Qd 1 1 31 1 1	
 	
 	

 r,   checkpoint_filevariantc           	      t   	 t           j                            |                               d          d         }|t          k    r!t
          j                            | d          S t          dd          rddini }t          j	        | fd	di|S # t          $ r}	 t          |           5 }|                                                    d
          rt          d          t          d|  d          |# 1 swxY w Y   n+# t           t          f$ r t          d|  d|  d          w xY wY d}~dS d}~ww xY 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basenamer5   r   safetensorstorch	load_filer   load	Exceptionopenread
startswithOSError
ValueErrorUnicodeDecodeError)r@   rA   file_extensionweights_only_kwargefs         r*   load_state_dictr[   b   s   ))/::@@EEbI777$..u.MMM;KDRX;Y;Y!a.$!7!7_a: " %  
    	o&& !6688&&y11 
!&   %N_ N N N           #J/ 	 	 	sOss_nsss  	     sO   A#B &)B 
D7D*AC55C9	9D<C9	=D D2(D))D22D7
state_dictrE   r    model_name_or_pathreturnc                 @   |pt          j        d          }|pt           j        }dt          t	          j        t                    j                                                  v }g }| 	                                }|
                                D ]\  }}	||vr|                    |           ||         j        |	j        k    r0|| dnd}
t          d|
 d| d||          d|	j         d		          |rt          | |||	|
           |t          | |||	           |S )NrD   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    )rb   )rM   rE   float32setinspect	signaturer   
parameterskeysr\   itemsappendshaperU   )r   r\   rE   r    r]   accepts_dtypeunexpected_keysempty_state_dict
param_nameparammodel_name_or_path_strs              r*   load_model_dict_into_metarr      s    *u|E**F"U]Es7#45P#Q#Q#\#a#a#c#cdddMO''))'--// P P
E---"":...J'-<<ASA_(:%=%=%=%=eg" i5  i  iz  i  i[klv[w  i  i  DI  DO  i  i  i    	P'z6V[\\\\\'z6OOOOOr,   c                                                      g ddt          j        j        dt          ffd |            S )Nra   moduleprefixc                     |i dg g f} | j         |  | j                                        D ]\  }}| |||z   dz              d S )NTr/   )_load_from_state_dict_modulesri   )rt   ru   argsnamechild
error_msgsrO   r\   s        r*   rO   z)_load_state_dict_into_model.<locals>.load   st    FBb"jA$$d++!?0022 	1 	1KD% UFTMC/000	1 	1r,   )ra   )copyrM   nnModuler$   )model_to_loadr\   r|   rO   s    `@@r*   _load_state_dict_into_modelr      sr     ""JJ1 1UX_ 1c 1 1 1 1 1 1 1 1 	Dr,   c                 t   | r0t          ||pdt          |rt          nt          |                    }nt          |pdt          |rt          nt          |                                                    }	 t          |||||||	|
||||          }t          |          }n# t          t          f$ r d }Y nw xY w|S )Nra   )weights_name	cache_dirforce_downloadresume_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   rA   r   r   r   r   r   r   r   r   
index_fileindex_file_in_repos                   r*   _fetch_index_filer      s      )OO[00I[]dee
 


 "OO[00I[]dee
 
 (** 		(-/#- /!1!#%'  J j))JJ"$45 	 	 	JJJ	 s   4*B B54B5)N)NNN).r3   re   rI   collectionsr   pathlibr   typingr   r   r   rL   rM   huggingface_hub.utilsr   utilsr
   r   r   r   r   r   r   r   
get_loggerr0   r7   r1   
accelerater   accelerate.utilsr   r   r   r~   r   r+   r?   r$   PathLiker[   rE   r    rr   r   r    r,   r*   <module>r      s  "      				 # # # # # #       ( ( ( ( ( ( ( ( ( (      4 4 4 4 4 4	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
	H	%	% 05    b000000aaaaaaaaaa    ,  (   U3+;%<  xPS}        L 26/3(,  U3,-. E#u{*+,	
 ! 
#Y   @; 4PS9    *. . . . .r,   