
    biav                     x   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m	Z	m
Z
 d dlmZ d dlmZmZmZmZ d dlm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!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*  e*jV                  e,      Z-ddddiZ. e%       rd dl/m0Z0 d dl1m2Z2m3Z3m4Z4m5Z5 g dfdejl                  jn                  fdZ8d Z9de:deee:ee;e:ejx                  f   f      fdZ=	 	 	 d8dee:ej|                  f   deee:ef      de?dee:ejx                  f   fdZ@	 	 	 	 	 	 	 	 	 	 d9dedeee:ej                  f      d ee:   d!ee   d"ee   deee:ee;e:ejx                  f   f      d#eee:      d$eee:ej|                  f      d%ee   d&ee   d'eee:ej|                  f      d(ee:   fd)ZBd:d*ZC	 	 	 	 	 	 	 	 	 	 	 	 	 d;d+ZD	 	 	 	 	 	 	 	 	 	 	 	 	 d;d,ZEd- ZF	 d<ded.e?d(ee:   fd/ZG	 d=deee:ef      fd0ZH	 d=deee:ef      fd1ZId2 ZJd<d3ZKd4 ZFd5 ZLd6ee:ejx                  f   dej                  d!ee   d(dfd7ZMy)>    N)array)OrderedDictdefaultdict)ThreadPoolExecutoras_completed)Path)DictListOptionalUnion)
is_zipfile)	DDUFEntry)EntryNotFoundError   )DiffusersQuantizer)#DEFAULT_HF_PARALLEL_LOADING_WORKERSGGUF_FILE_EXTENSIONSAFE_WEIGHTS_INDEX_NAMESAFETENSORS_FILE_EXTENSIONWEIGHTS_INDEX_NAME_add_variant_get_model_file	deprecateis_accelerate_availableis_accelerate_versionis_gguf_availableis_torch_availableis_torch_versionloggingTransformer2DModelDiTTransformer2DModelPixArtTransformer2DModel)ada_norm_zeroada_norm_single)infer_auto_device_map)get_balanced_memoryget_max_memoryoffload_weightset_module_tensor_to_devicemodelc           
         t        |t              rQi }|!|j                  |j                  | |             |j                  | j	                         D ci c]*  \  }t        fd|D              rt        j                  , c}}       |}	||j                  |	      }	| j                  |      }
d|
i}dt        j                  t              j                  v r||d<   n#t        |      dkD  rt        j!                  d       |dk7  rt#        | f||dk(  |d|}nt%        |      }||j'                  |      }||d	<   t        | fd
|	i|}||j)                  |       |S c c}}w )Nc              3   &   K   | ]  }|v  
 y wN ).0mnames     _/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/models/model_loading_utils.py	<genexpr>z(_determine_device_map.<locals>.<genexpr>O   s     ?QqDy?s   no_split_module_classesspecial_dtypesr   zThis model has some weights that should be kept in higher precision, you need to upgrade `accelerate` to properly deal with them (`pip install --upgrade accelerate`).
sequentialbalanced_low_0)dtypelow_zero
max_memoryr:   r8   )
device_map)
isinstancestrupdateget_special_dtypes_updatenamed_parametersanytorchfloat32adjust_target_dtype_get_no_split_modulesinspect	signaturer%   
parameterslenloggerwarningr&   r'   adjust_max_memoryvalidate_environment)r*   r;   r:   torch_dtypekeep_in_fp32_moduleshf_quantizerr5   r1   _target_dtypeno_split_modulesdevice_map_kwargss          `    r2   _determine_device_maprU   D   s    *c"#!!,"H"HP["\]  %557 D!?*>?? emm#	
 ##';;LIL 66zB68HIw001FGRRR2@./ 1$NN`
 %,!$(88%	
 $J (
3J#%77
CJ*4,'*5ZZHYZ
#---DUs   /E&
c           	      "   |j                   }t        j                  |      j                  | d   d       }|rXt        j                  t         j                  d      d         }t        ||      }t        j                  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getattrrJ   info)config	old_classprevious_class_nameremapped_class_namediffusers_libraryremapped_classs         r2   _fetch_remapped_cls_from_configrg   y   s    #,,/334GHLLVT_M`bfg %33HNN34G4JK !24GH./B.C=QdPe f  34 511	

     
param_namer;   c                     |y| }t        |      dkD  r:||vr6dj                  |j                  d      dd       }t        |      dkD  r||vr6|dk(  rd|vrt        |  d      ||   S )z<
    Find the device of param_name from the device_map.
    Ncpur   rX    z doesn't have any device set.)rI   joinr^   
ValueError)ri   r;   module_names      r2   _determine_param_devicerq      s       +"{*'D((;#4#4S#9#2#>?K +"{*'D":!5
|+HIJJ+&&rh   checkpoint_filedduf_entriesdisable_mmapmap_locationc           	         t        | t              r| S 	 t        j                  j	                  |       j                  d      d   }|t        k(  r|r<||    j                         5 }t        j                  j                  |      cddd       S |r7t        j                  j                  t        | d      j                               S t        j                  j                  | |      S |t        k(  rt        |       S i }t!        dd      rdd	ini }t        | t"              r"|d
k7  rt!        dd      rt%        |       r|sdd	i}t        j                  | fd|i||S # 1 sw Y   xY w# t&        $ r}	 t        |       5 }	|	j                         j)                  d      rt+        d      t-        d|  d      |# 1 sw Y   nxY wn%# t.        t,        f$ r t+        d|  d|  d      w xY wY d}~yd}~ww xY w)zU
    Reads a checkpoint file, returning properly formatted errors if they arise.
    rX   rl   Nrb)device>=z1.13weights_onlyTmetaz2.1.0mmapru   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.z1Unable to load weights from checkpoint file for 'z' at 'z'. )r<   dictospathbasenamer^   r   as_mmapsafetensorsrB   loadopenread	load_filer   load_gguf_checkpointr   r=   r   	Exception
startswithOSErrorro   UnicodeDecodeError)
rr   rs   rt   ru   file_extensionmm
extra_argsweights_only_kwargefs
             r2   load_state_dictr      s    /4(+))/:@@EbI77!/2::< 6&,,11"56 6"((--d?D.I.N.N.PQQ"((22?<2XX22'88J;KDRX;Y.$!7_a ?C0 F*$T73/$$d^
::omLmL^mblmm)6 6*  	o& !668&&y1!&  %4_4E FN N    #J/ 	COCTTZ[jZkkno 	sm   AE& #E	E& 8E&  E& &E& :AE& E#E& &	G20G;;F66F?	;GG-"G%%G--G2
state_dictr8   model_name_or_pathrP   rO   unexpected_keysoffload_folderoffload_indexstate_dict_indexstate_dict_folderreturnc                    |du}| j                         }|j                         D ]^  \  }|vri }|t        j                  |      r|Gt	        fd|D              r3|j                  t        j                        }t        j                  |d<   n7||j                  t        t        dd      k(  rn|j                  |      }||d<   t        dd      r
d|d<   d	|d
<   | }j                  d      }|D ]  }t        ||      } t        |t        j                  j                  t        j                  f      sd}|=||j                  |j                        }|j                         r|j!                         }t#        |      }|   j$                  |j$                  k7  rs|r9|j&                  r-|j)                  | |||      r|j+                  |   |       n8|| dnd}t-        d| d d|   j$                   d|j$                   d	      |dk(  rt/        |||	      }	|dk(  r|
t/        |||
      }
|r0|j)                  | |||      r|j1                  | |||||       Ot3        | |fd|i| a |	|
fS )z
    This is somewhat similar to `_load_state_dict_into_model`, but deals with a model that has some or all of its
    params on a `meta` device. It replaces the model params with the data from the `state_dict`
    Nc              3   D   K   | ]  }|j                  d       v   yw)rX   N)r^   )r/   module_to_keep_in_fp32ri   s     r2   r3   z,load_model_dict_into_meta.<locals>.<genexpr>   s'      8DZ&**:*:3*??8s    r8   float8_e4m3fn>z1.8.1Tnon_blockingFclear_cacherX   )param_device rm   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.diskrk   )r8   value)r   itemsrB   is_floating_pointrA   torC   r8   r_   r   r^   r<   nn	ParameterTensoris_contiguous
contiguousrq   shapepre_quantizedcheck_if_quantized_paramcheck_quantized_param_shapero   r(   create_quantized_paramr)   )r*   r   r8   r   rP   rO   r;   r   r   r   r   r   is_quantizedempty_state_dictparamset_module_kwargs	old_paramsplitsr^   r   model_name_or_path_strri   s                        @r2   load_model_dict_into_metar      s   &  t+L'')'--/ Hk
E-- !8!8!?#/C 8^r8 5 /-2]]!'*)ekkWUO]a=b.b-2!'* g.04n-/4m,
 	!!#& 	2E	51I	2 )ehh&8&8%,,%GHI }1&&(((*.z:F J'--< .. 995*j| :  88EUV`EachiEWEc,>+?q)Aik& "#9":)J<O_`pq{`|  aC  aC  `D  DN  OT  OZ  OZ  N[  [s  t  6!*5*nm\MU"'7'C-eZARTde11%
Jeq1r//uj,
O[` 0  (z<jujXijQHkT ***rh   c                    | j                   j                  dk(  ryt        |D cg c]  }|j                  |      s| c}      dk(  ryt	        | dd      s-t
        j                  | j                  j                   d       yt        t        | j                         j                                     }||z   |v r0|||z      j                  | j                         |   j                  k(  S yc c}w )z
    Checks if `model_to_load` supports param buffer assignment (such as when loading in empty weights) by first
    checking if the model explicitly disables it, then by ensuring that the state dict keys are a subset of the model's
    parameters.

    r{   Fr   !_supports_param_buffer_assignmentTzA does not support param buffer assignment, loading will be slower)rx   typerI   r   r_   rJ   debug	__class__rY   nextiterr   keysr8   )model_to_loadr   start_prefixkey	first_keys        r2   %check_support_param_buffer_assignmentr   ;  s       F*
:FC)ECFG1L ="EtL&&//00qr	
  T-22499;<=Ii:-,2399]=U=U=WXa=b=h=hhh Gs
   C,C,c                     t        | |      }t        ||||      }g }|rt        |||||||	||
||      \  }
}nt        ||      }|t	        |||      z  }|
|||fS )N)rs   )	r;   r8   rP   rO   r   r   r   r   r   )r   _find_mismatched_keysr   r   _load_state_dict_into_model)
shard_filer*   model_state_dictr;   r8   rP   rO   rs   loaded_keysr   r   r   r   r   ignore_mismatched_sizeslow_cpu_mem_usager   mismatched_keys
error_msgsassign_to_params_bufferss                       r2   _load_shard_filer   W  s    $ !,GJ+	O J*C!%!5+)'-/+
'' $IPZ#[ 1%E]^^
*OZGGrh   c                 D   t        t        |       t              }t        j	                  d| d       g }g }t        j                  t        f|||||||||	|
|||||d}t        |      5 }t        j                  t        |       d      5 }| D cg c]  }|j                  ||       }}t        |      D ]4  }|j                         }|\  }
}}}||z  }||z  }|j                  d       6 	 d d d        d d d        |
|||fS c c}w # 1 sw Y   xY w# 1 sw Y    xY w)Nz'Loading model weights in parallel with z workers...)r*   r   r;   r8   rP   rO   rs   r   r   r   r   r   r   r   r   )max_workerszLoading checkpoint shards)totaldesc   )minrI   r   rJ   r`   	functoolspartialr   r   r   tqdmsubmitr   resultr>   )shard_filesr*   r   r;   r8   rP   rO   rs   r   r   r   r   r   r   r   r   num_workersr   r   load_oneexecutorpbarr   futuresfuturer   _mismatched_keys_error_msgss                               r2   !_load_shard_files_with_threadpoolr     sM   & c+&(KLK
KK9+kRSJO  )!1!'#%)+ 7+!H& 
	4 \\K 07RS 	W[OZ[xx<[G[&w/ QWN/1A;k)
#33A	 *OZGG \	 	 s7   +!DD
D*AD
/DD

D	DDc                     g }|rd|D ]_  }|}|| vr
||v s| |   j                   ||   j                   k7  s/|j                  || |   j                   ||   j                   f       | |= a |S r-   r   appendr   r   r   r   r   checkpoint_key	model_keys          r2   r   r     s     O) 
	/N&IZ/,,N1K1Q1QUefoUpUvUv1v&&#Z%?%E%EGWXaGbGhGhi ~.
	/ rh   r   c                     j                         g ddt        j                  j                  dt        dt
        ffd | |       S )Nmoduleprefixr   c                     i }||d<   |r!t        dd      st        j                  d       	||dg g f} | j                  |  | j                  j                         D ]  \  }}|	 |||z   dz   |        y )Nr   ry   z2.1zYYou need to have torch>=2.1 in order to load the model with assign_to_params_buffers=TrueTrX   )r   rJ   r`   _load_from_state_dict_modulesr   )
r   r   r   local_metadataargsr1   childr   r   r   s
          r2   r   z)_load_state_dict_into_model.<locals>.load  s    5M12#,<T5,IKKstFND"b*M$$$d+!??002 	KKD% UFTMC/1IJ	Krh   )r   )rm   F)copyrB   r   Moduler=   bool)r   r   r   r   r   s    ` @@r2   r   r     sP    
 "JJ
KUXX__ 
Kc 
KRV 
K 	1IJrh   c                 >   | r)t        ||xs dt        |rt        nt        |            }|S t        |xs dt        |rt        nt        |            j	                         }	 t        |||||||	|
d |||      }|st        |      }|S # t        t        f$ r d }Y |S w xY w)Nrm   weights_name	cache_dirforce_downloadproxieslocal_files_onlytokenrevision	subfolder
user_agentcommit_hashrs   )r   r   r   r   as_posixr   r   EnvironmentError)is_localpretrained_model_name_or_pathr  use_safetensorsr   variantr   r  r  r  r  r  r  rs   
index_fileindex_file_in_repos                   r2   _fetch_index_filer    s      )OO0I[]de

< 1 "OO0I[]de
 (* 		(-/#-!1!%')J  !*-
  #$45 	J	s   !$B BBc                    | rt        ||xs d|rt        nt              j                         }|j	                  d      }d|v rdnd}|d |  |gz   || d  z   }dj                  |      }t        j                  j                  |      r"d| d}t        dd	|d
       t        |      }|S d }|S |t        |xs d|rt        nt              j                         }|j	                  d      }d}|d |  |gz   || d  z   }dj                  |      }	 t        |||||||	|
d |||      }t        |      }d| d}t        dd	|d
       |S S # t        t        f$ r d }Y |S w xY w)Nrm   rX   z.cachezThis serialization format is now deprecated to standardize the serialization format between `transformers` and `diffusers`. We recommend you to remove the existing files associated with the current variant (z6) and re-obtain them by running a `save_pretrained()`.!legacy_sharded_ckpts_with_variantz1.0.0F)standard_warnr   )r   r   r   r  r^   rn   r   r   existsr   r   r   r	  )r
  r  r  r  r   r  r   r  r  r  r  r  r  rs   r  r   split_indexdeprecation_messager  s                      r2   _fetch_index_file_legacyr  #  s     )O'6#<N
 (*	 	
 !!#&$
2b+&'2V[LM5JJXXf%
77>>*% %t  u|  t}  }s  #t97DWglmj)JD A J@ = !%R+:'@R" hj  (--c2FKMk\*gY69NNF!$&!1",1!3'#1#%5%") +!-
 "*-
 )x  y@  xA  Aw  'x#=wH[kpq : '(89 "!
"s   7E E E c                    t        |t              s|g}t        |      dk(  r|d   }d }n|d   dk7  rt        d      |\  }}|dv rt	        | d         } | S |dv rt        | d         } | S |dv rt        | d         } | S |dv r3t        d	t        |             j                         j                         } | S |d
v rt        | |      } | S )Nr   r   	   zPReceived multiple types, therefore expected the first type to indicate an array.)r   r   r            
      )      )   )   B)r  )r<   listrI   ro   intfloatr   r   tobytesdecode_gguf_parse_value)_value	data_typearray_data_types      r2   r+  r+  e  s    i&K	
9~aL	Q<1opp%."	?..VAY M 
g	vay! M 
c	fQi
 M	 
c	sDL)113::< M 
c	"6?;Mrh   c           	      r   t               rt               rddl}ddlm} ddlm}m} n t        j                  d       t        d       ||       }i }|j                  D ]  }|j                  }	|j                  }
|
|j                  j                  |j                  j                  fv}|rI|
|vrEdj!                  |D cg c]  }t#        |       c}      }t%        |	 d	t#        |
       d
| d      t'        j(                  |j*                  j-                               }|r
 |||
      n|||	<    |S c c}w )a  
    Load a GGUF file and return a dictionary of parsed parameters containing tensors, the parsed tokenizer and config
    attributes.

    Args:
        gguf_checkpoint_path (`str`):
            The path the to GGUF file to load
        return_tensors (`bool`, defaults to `True`):
            Whether to read the tensors from the file and return them. Not doing so is faster and only loads the
            metadata in memory.
    r   N)
GGUFReaderr   )SUPPORTED_GGUF_QUANT_TYPESGGUFParameterzLoading a GGUF checkpoint in PyTorch, requires both PyTorch and GGUF>=0.10.0 to be installed. Please see https://pytorch.org/ and https://github.com/ggerganov/llama.cpp/tree/master/gguf-py for installation instructions.zKPlease install torch and gguf>=0.10.0 to load a GGUF checkpoint in PyTorch.
z has a quantization type: zT which is unsupported.

Currently the following quantization types are supported: 

zs

To request support for this quantization type please open an issue here: https://github.com/huggingface/diffusers)
quant_type)r   r   ggufr0  quantizers.gguf.utilsr1  r2  rJ   errorImportErrortensorsr1   tensor_typeGGMLQuantizationTypeF32F16rn   r=   ro   rB   
from_numpydatar   )gguf_checkpoint_pathreturn_tensorsr5  r0  r1  r2  readerparsed_parameterstensorr1   r4  is_gguf_quantr   _supported_quants_strweightss                  r2   r   r   }  sD    13#UUA	
 ghh,-F.. n{{''
 #4+D+D+H+H$JcJcJgJg*hhZ/II$(IIE_.`Ts4y.`$a!f6s:6G HY,- LL  ""6;;#3#3#56S`-J"Ofm$%n(  /as   <D4
c                     g }|s|S |D ]_  }|}|| vr
||v s| |   j                   ||   j                   k7  s/|j                  || |   j                   ||   j                   f       | |= a |S r-   r   r   s          r2   r   r     s    O"% 
+"	+((Z-G-M-MQabkQlQrQr-r""N!;!A!ACST]C^CdCde >*
+ rh   c           
          i }| j                         D ]D  \  }}|j                  |D ci c]$  }||k(  s|j                  | d      s|dk(  s"||& c}       F |S c c}w )zT
    Expand a device map to return the correspondence parameter name to device.
    rX   rm   )r   r>   r   )r;   param_namesnew_device_mapr   rx   ps         r2   _expand_device_maprM    st     N$**, 
 +i1qF{allfXUV<>X\bfh\hQYi	

  js   #A
A
expanded_device_mapc                 h   |dn|j                         }|j                         D ci c](  \  }}t        |      dvr|t        j                  |      * }}}|syt        t              }|j                         D ]2  \  }	}	 | j                  |	      }
||xx   |
j                         z  cc<   4 |j                         D ]-  \  }}t        d||z        }t        j                  |||d      }/ yc c}}w # t        $ r2 	 | j                  |	      }
n# t        $ r t        d|	 d      w xY wY w xY w)	ay  
    This function warm-ups the caching allocator based on the size of the model tensors that will reside on each
    device. It allows to have one large call to Malloc, instead of recursively calling it later when loading the model,
    which is actually the loading speed bottleneck. Calling this function allows to cut the model loading time by a
    very large margin.
    Nr   )rk   r   zParameter or buffer with name=z not found in modelr   F)r8   rx   requires_grad)get_cuda_warm_up_factorr   r=   rB   rx   r   r'  get_parameterAttributeError
get_buffernumelmaxempty)r*   rN  r8   rP   factorr   rx   accelerator_device_mapelements_per_deviceri   rL  
elem_countwarmup_elemsrQ   s                 r2   _caching_allocator_warmupr]    s_    &QL,P,P,RF
 1668E6v;o- 	u||F## 
 "%c*4::< 	1
F	g##J/A 	F#qwwy0#	1 2779 W
1jF23KKE&PUVW+  	gg$$Z0! g$'Ej\Qd%effg 	gs/   -C0C66	D1 DD1D++D10D1)NFrk   )
NNNNNNNNNN)rm   )NNNNNNNNNNNFF)Fr-   )Nr   r\   rF   r   r   collectionsr   r   concurrent.futuresr   r   pathlibr   typingr	   r
   r   r   zipfiler   r   rB   huggingface_hubr   huggingface_hub.utilsr   
quantizersr   utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   
get_loggerrY   rJ   rZ   
accelerater%   accelerate.utilsr&   r'   r(   r)   r   r   rU   rg   r=   r'  rx   rq   PathLiker   r   r8   r   r   r   r   r   r   r  r  r+  r   rM  r]  r.   rh   r2   <module>rk     s  "    	  0 ?  . .    % 4 +   $ 
		H	% 05  0qq
 WYgk288??2j(' '$sERUWZ\a\h\hRhLiGiBj9k '& 48-2	73+,74Y/07 7 U\\)*	7z 04(,15+/EI+/8<$('+;?`+`+ E#u{{*+,`+ !	`+
 -.`+ #4.`+ c5c5<<)?#@@AB`+ d3i(`+ U3#345`+ D>`+ tn`+  c2;;&6 78`+ 
#Y`+F@ 
!!,Hf 
!!7Ht. NS*FJ	#YR 48/ 4Y/0/@ 48? 4Y/0?D00f$	#W $S%,,%6 7#W@E#W[cdv[w#W	#Wrh   