
    biŻ              0          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m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 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) ddl*m+Z+ ddl,m-Z-m.Z.  e'       rd dl/Z/d dl/m0Z0m1Z1 d dl2mZ3 d dl2m4Z5 d dl2m"Z6  e%       rd dl7Z7d dl7m8Z8 d dl9m:Z: d dl;m<Z<m=Z= dZ>dZ?dZ@dZAdgZB e)j                  eD      ZEd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ZFi ZGeFD ]  ZHeGj                  eFeH           dnd eJfd!ZKd" ZLd# ZMdod ee
ej                     eOf   fd$ZPed%        ZQd& ZRd' ZSd( ZT	 dod)ZU	 	 	 	 	 dpd*ZV	 	 	 	 	 	 	 	 dqd+ZWd,eOd-eOd.e
e   d/ed0eJd1eOd2eeOej                  f   d3eeOej                  f   fd4ZY	 drd5e	eOeZf   d6e	eOeZf   d7eOfd8Z[d9 Z\	 dsd,eOd-eOd.e
e   d/ed0eJd:ed2ej                  d;ed<ed=eee	eOej                  f   eOf      d>ee	ee^eOf   ee^eOf   f      d?eeeOej                  f      d@eJdAe	eOeOf   d1eOdBeJdCeOdDeJd3eeOej                  f   dEeJdFee	eOef      dGedHee   f.dIZ_dJe`dKeOdLeJd efdMZadN ZbdOeOdCeOdPecd ecfdQZddR ZedSeOdPecfdTZfdUecdVecdWecdOeOd ecf
dXZg	 	 	 dndYeOdZe	eOef   d[ee
eO      d\ee
eO      dCeeO   f
d]Zh	 dsd^e
eO   d_e
eO   dEeJdBeJd`eJdaeJdbeJdCeeO   d e
eO   fdcZidYeOddeOdeeOdfeOdgeOdheJdieOdjeOfdkZjdl Zkdm Zly)t    N)Path)AnyCallableDictListOptionalUnion)	DDUFEntry	ModelCard
model_infosnapshot_download)OfflineModeIsEnabledvalidate_hf_hub_args)version)	HTTPError   )__version__)FLAX_WEIGHTS_NAMEONNX_EXTERNAL_WEIGHTS_NAMEONNX_WEIGHTS_NAMESAFETENSORS_WEIGHTS_NAMEWEIGHTS_NAME	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)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                    t         t        t        t        t        g}t               r|t        t        t        gz  }|D cg c]  }|j                  d      d    }}|D cg c]  }|j                  d      d    }}d}t        j                  ddj                  |       d| d| d| ddj                  |       d	      }	t        j                  ddj                  |       d
| ddj                  |       d	      }
|xs g }|r4| D ch c])  }t        j                  j                  |      d   |v s(|+ } }i }| D ]_  }t        |j                  d            dk(  s!|j                  d      \  }}||v r:|j!                  |g        ||   j#                  |       a t%               }|s.|t'        | |	      }|st'        | |
      }t)        d |D              S |j+                         D ]y  \  }}g }t%               }|t'        ||	      }|st'        ||
      }|D ]:  }t        j                  j-                  |      \  }}|dk(  }|j#                  |       < t)        |      ry y yc c}w c c}w c c}w )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   \d{5}-of-\d{5}(|)\.(-)$)(-)?\.(/r   c              3   $   K   | ]  }d |v  
 yw).safetensorsN ).0filenames     e/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/pipelines/pipeline_loading_utils.py	<genexpr>z,is_safetensors_compatible.<locals>.<genexpr>   s     Q(>X-Qs   rH   FT)r   r   r   r   r   r   TRANSFORMERS_WEIGHTS_NAMETRANSFORMERS_SAFE_WEIGHTS_NAMETRANSFORMERS_FLAX_WEIGHTS_NAMEsplitrecompilejoinospathlen
setdefaultappendsetfilter_with_regexanyitemssplitext)	filenamespassed_componentsfolder_namesvariantweight_nameswweight_prefixesweight_suffixstransformers_index_formatvariant_file_renon_variant_file_ref
componentsrK   	componentcomponent_filenamefiltered_filenamescomponent_filenamesmatchesfiltered_component_filenames	extensionmatch_existss                         rL   is_safetensors_compatiblert   _   s    	 "L !"24RTrss 1==1qwws|A=O=0<=1aggcl2&=N= 1jjSXXo&'tG9AgYa@Y?ZZ^_b_g_ghv_w^xxz{O **SXXo&'s+D*EU388TbKcJddfg */R )Q1RWW]]1-=a-@L-PQQ	Q J 	98>>#&'1,(0s(;%	%))i,9$$%78	9 !29o!N "!29>Q!RQ>PQQQ +5*:*:*< &	&'*u$+<=PRa+b( ,+<=PRe+f("> 	)"$''"2"23E"FHi$6LNN<(		) 7|#& } >= Rs   I'I,
)I14I1c                    t         t        t        t        t        g}t               r|t        t        t        gz  }|D cg c]  }|j                  d      d    }}| D cg c]  t        fd|D              s c}S c c}w c c}w )zIFilter model repo files for just files/folders that contain model weightsr<   r=   c              3   @   K   | ]  }j                  |        y wN)endswith)rJ   rr   rj   s     rL   rM   z%filter_model_files.<locals>.<genexpr>   s     'b)

9(='bs   )r   r   r   r   r   r   rN   rO   rP   rQ   r\   )r_   rc   wnallowed_extensionsrj   s       `rL   filter_model_filesr{      s     	 "L !"24RTrss6BC"((3-+CC c!C'bOa'b$bAcc Dcs   B!B;Bc                 t    | D ch c](  }|j                  |j                  d      d         '|* c}S c c}w )NrF   r=   )matchrQ   )r_   
pattern_rerj   s      rL   r[   r[      s3     S!J$4$4QWWS\"5E$F$RASSSs   (55c                 ~   t         t        t        t        t        g}t               r|t        t        t        gz  }|D cg c]  }|j                  d      d    }}|D cg c]  }|j                  d      d    }}d}|t        j                  ddj                  |       d| d| d| ddj                  |       d	      }t        j                  ddj                  |       ddj                  |       d
| d      }	t        j                  d| d| d      }
t        j                  ddj                  |       ddj                  |       d| d      }t        j                  ddj                  |       d| ddj                  |       d	      }t        j                  ddj                  |       ddj                  |       d      }d"d}i }| D ]v  }t        |j                  d            dk(  s"|j                  dg       j                  |       B|j                  d      \  }}|j                  |g       j                  |       x t!               }t!               }|j#                         D ]  \  }} |||      }t!               }t!               }t!               }|0t%        |      }t%        |	      }t%        |
      }t%        |      }|s|r|j'                  |r|z  n|z         t%        ||      }t%        ||      }|j'                  ||z          |j'                  |       t        |      dk(  r|d| d}t)        |      t        |      dkD  rN||k7  rIt*        j-                  d| d| d| ddj                  |       d | ddj                  ||z
         d!       ||fS c c}w c c}w )#Nr<   r   r=   r>   r?   r@   rA   rB   rC   z
)\.index\.z\.json$z.*-z\.z	\.[a-z]+$z)\.z\.index\.json$rD   rE   z)\.index\.jsonc                 b    |s| S | D ch c]  t        fd|D              r c}S c c}w )Nc              3   ^   K   | ]$  }j                  |j                  d              & yw)z*.N)rx   lstrip)rJ   patrj   s     rL   rM   zXvariant_compatible_siblings.<locals>.filter_for_compatible_extensions.<locals>.<genexpr>   s$     /hQT

3::d;K0L/hs   *-)r\   )r_   ignore_patternsrj   s     `rL    filter_for_compatible_extensionszEvariant_compatible_siblings.<locals>.filter_for_compatible_extensions   s0     %iaC/hXg/h,hiiis   ,,rF   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.rw   )r   r   r   r   r   r   rN   rO   rP   rQ   rR   rS   rT   rW   rX   rY   rZ   r]   r[   update
ValueErrorloggerwarning)r_   rb   r   rc   rd   re   rf   rg   rh   variant_index_relegacy_variant_file_relegacy_variant_index_reri   non_variant_index_rer   rk   rK   rl   _usable_filenamesvariant_filenamesro   component_variantscomponent_legacy_variantscomponent_non_variantscomponent_variant_index_files$component_legacy_variant_index_fileserror_messages                               rL   variant_compatible_siblingsr      s5    "L !"24RTrss 1==1qwws|A=O=0<=1aggcl2&=N= 1**/*+4y'!D]C^^bcfckcklzc{b||~
 ::/*+40H/IT[S\\cd
 "$s3L2MRPWyXa,b!c"$**/*+40H/IWIUcd#

 **SXXo&'s+D*EU388TbKcJddfg ::388O+D*ET#((SaJbIccq&rsj J >8>>#&'1,!!"b)00:~~c*	1i,33H=> u*4*:*:*< \&	&>?Rdst U$'E!!$!23F!X,=>QSc,d)(9:MOe(f%3DEXZq3r0!:$$% #%BB.1UU &77JL_%`",=>QSg,h)##$:=Z$Z[1\4 -.
"w':MgY  WD  E''
!&6:K&KgYiy8[\c[d e		+,-^G9 E		*->>?@ A<=	
 ...o >=s   N5N:c                 &   t        | |d       }|j                  D ch c]  }|j                   }}t        ||      \  }}	|D 
cg c]:  }
dj	                  |
j                  d      d d |
j                  d      dd  z         < }}
t        |      j                  t        |            r.t        j                  d| d|  d| d	| d
| d| dt               y t        j                  d| d|  d| d| d|  d|  d| dt               y c c}w c c}
w )Ntokenrevision)rb   r<   r!   r   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   siblings	rfilenamer   rT   rQ   rZ   issubsetwarningswarnFutureWarning)pretrained_model_name_or_pathr   rb   r   model_filenamesinfosiblingr_   comp_model_filenamesr   rj   s              rL   warn_deprecated_model_variantr   4  s   %D
 37--@w""@I@9)XV!SghaCHHQWWS\"1%5QR8H%HIhh
?$$S)=%>?*8*F;X:YYijris  ta  bj  ak  kT  U]  T^  ^i  jr  is  s~  	

 	*8*F;X:YYijris  tR  S[  R\  \A  B_  A`  `I  Jg  Ih  ht  u}  t~  ~t	  u		
 Ahs   D	?Dc                     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   s     rL   _unwrap_modelr   K  s<    % "eY'$$**EL    c           	         |st        j                  |       }t        ||      }|j                         D ci c]  }|t        ||d       }	}d}
|	j	                         D ]  \  }}|	t        ||      s|}
 ||   }t        |      }|j                  }t        ||
      st        ||    d| d|
       yt        j                  d||    d       yc c}w )zNSimple helper method to raise or warn in case incorrect module has been passedNz 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_modulegetattrkeysr]   
issubclassr   	__class__r   r   r   )library_namelibrary
class_nameimportable_classespassed_class_objnameis_pipeline_module	class_objcclass_candidatesexpected_class_objclass_candidate	sub_modelunwrapped_sub_model	model_clss                  rL   maybe_raise_or_warnr   Y  s    )),7GZ0	BTBYBYB[\QAww488\\!+;+A+A+C 	5'J*z)_/U%4"	5 %T*	+I6'11	)%78 0 67}YKO_`r_stuu 9 	45Ed5K4L M$ $	
! ]s   Cc                     ddl m} t        ||       }|rt        ||       }t        ||      }|S t	        j
                  |       }t        ||      }|S )Nr   	pipelines)r8   r   hasattrr   r   r   )r   r   r   r   pipeline_moduler   r   s          rL   simple_get_class_objr   w  sY    # L9!)\:OZ8	
  )),7GZ0	r   c           	      n   |r"|r t         j                  j                  ||      nd}|r@t        ||       }t        ||      }	t        j                  |j                         |	      }
|	|
fS |ryt         j                  j                  t         j                  j                  || dz               r9t        || dz   |      }	t        j                  |j                         |	      }
|	|
fS t        j                  |       }t        ||      }	|j                         D ci c]  }|t        ||d       }
}|	|
fS c c}w )zaSimple helper method to retrieve class object of module as well as potential parent class objectsN.py)module_filer   )rU   rV   rT   r   dictfromkeysr   isfiler   r   r   )r   r   r   r   r   component_name	cache_dircomponent_folderr   r   r   r   r   s                rL   get_class_obj_and_candidatesr     s-    CQU^rww||I~>dh!)\:OZ8	==);)@)@)BIN &&& 
bggnnRWW\\:JL[`L`-ab1,*>:
	  ==);)@)@)BIN &&& )),7GZ0	BTBYBYB[\QAww488\\&&& ]s   D2c                     | j                  d      r2t        |       }|j                  }|j                  j	                         } n||  d}|} nt
        }|||}t        | ||||      S )Nr   )r   r   r   r   )rx   r   r   parentabsoluteCUSTOM_PIPELINE_FILE_NAMEr   )custom_pipelinerepo_idhub_revisionr   r   r   rV   	file_names           rL   _get_custom_pipeline_classr     s     &O$II	++..0		&'s+	!-	|7  ( r   c	                 2   |t        ||||||      S | j                  dk7  r| j                  dk7  r| S t        j                  | j                  j                  d      d         }	|xs |d   }|st        d      |j                  d      r|d	d  n|}t        |	|      }
|rvd
dl	m
}  ||
      }|1t        j                  d|j                   d|
j                   d       n/t        j                  |
j                   d|
j                   d       |xs |
}
|
S )N)r   r   r   r   r   r0   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__rQ   r   
startswithr   auto_pipeliner   r   r   )r   configload_connected_pipeliner   r   r   r   r   r   diffusers_modulepipeline_clsr   connected_pipeline_clss                rL   _get_pipeline_classr     sI    ")%!
 	
 00Y5G5GK\5\ ..y/C/C/I/I#/Nq/QR4vm4J}
 	
 $.#8#8#@ABjJ+Z8L:!8!F!-KK-.D.M.M-Nl[g[p[pZq  rb  c KK<0011[\h\q\q[rrstu-=r   r   r   r   r   r   r   torch_dtypecached_folderc                 "   t        | ||||||      \  }	}
t               rAt        j                  t        j                  t        j
                        j                        }nd}t               xr* t        |	t              xr |t        j                  d      k\  }t        j                  t        j                  d      d         }t        |	|j                        }d }|}t
        ddd}|r|	j                  t        j                   j#                  ||      |d	d	|j%                  d
d      |j%                  dd       |j%                  dd      |j%                  dd       |j%                  dd       |j%                  dd       |      \  }}}t'        j(                         5   |	j*                  |fi |}d d d        n|rt-        |	dd       }|t/        d      |j1                  |||j%                  d
d      |j%                  dd       |j%                  dd      |j%                  dd       |j%                  dd       |      }t'        j(                         5   |	|      }d d d        ||j3                  |      }|S # 1 sw Y   xY w# 1 sw Y   +xY w)Nr   r   N/A4.20.0r<   r   r   pytorch)r8   	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   parser9   r   base_versionr   r$   r   r   r   rQ   r.   load_configrU   rV   rT   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  s                         rL   _load_empty_modelr    s    0LIq !"&}}W]]<;S;S-T-a-ab$ 	"# 	<y/2	< GMM($;; 
 !..x~~c/B1/EF#I/?/J/JKEK J -6-B-BGGLLd+#!%#!::&6>JJy$/#ZZ(:EB**Wd+ZZ
D1jjd3! .C .
*{ **, 	C)I))&BMBE	C 	C	y.$?\]]--!::&6>JJy$/#ZZ(:EB**Wd+ZZ
D1! . 	
 **, 	&f%E	& {+L-	C 	C"	& 	&s   I9	J9JJmodule_sizesdevice_memorydevice_mapping_strategyc                 4   t        |j                               }||d d d   z   }|j                         }i }d}| D ]Z  }||t        |      z     }| |   }	||   }
|	|
kD  r|g|d<   *||vr|g||<   n||   j	                  |       ||xx   |	z  cc<   |dz  }\ |S )Nr=   r   cpur!   )listr   copyrW   rY   )r  r  r  
device_idsdevice_cycledevice_id_component_mappingcurrent_device_indexrl   	device_idcomponent_memorycurr_device_memorys              rL   _assign_components_to_devicesr$  I  s     m((*+J
4R4 00L!&&(M"$! &	 !5L8I!IJ	'	2*95 002;'. ;;:C+I6+I6==iH )$(88$ A% !&$ '&r   c                    | dk7  r| S ddl m} |j                  dt        j                        }i }	|j                         D ]5  \  }
\  }}|j                  d      rt        d      t        ||      }t        }d }|
|v r4t        ||||||
|       t        j                         5  ||
   }d d d        nt        |t              r0|j                  |
|j                  dt        j                              n|}t        |||||||
||j                  dd       |j                  d	d       |j                  d
d       |j                  dd       |j                  dd       |j                  dd             }|1||	|
<   8 |	j                         D ci c]z  \  }}t        |t        j                   j"                        rQ|t%        |t        |t              r0|j                  ||j                  dt        j                              n|      d   | }}}t        t'        |j                         d d            }t)        |      }t        t'        |j                         d d            }|j                         D ci c]  \  }}|dk7  s|| }}}d }t+        |      dkD  r4t-        |||       }i }|j                         D ]  \  }}|D ]  }|||<   	  |S # 1 sw Y   qxY wc c}}w c c}}w )Nbalancedr   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   r  r   c                     | d   S Nr!   rI   items    rL   <lambda>z'_get_final_device_map.<locals>.<lambda>  s
    d1g r   T)keyreversec                     | d   S r*  rI   r+  s    rL   r-  z'_get_final_device_map.<locals>.<lambda>  s
    $q' r   r  )r  )r8   r   gettorchfloat32r]   r   r   r   ALL_IMPORTABLE_CLASSESr   r
  r  r   r   r  nnModuler)   sortedr*   rW   r$  )
device_mapr(  r   	init_dictr   
max_memoryr  r   r   init_empty_modulesr   r   r   r   r   loaded_sub_modelsub_model_dtypemodule_namemoduler  kvfinal_device_mapr  r!  rk   rl   s                              rL   _get_final_device_maprC  g  s   Z#**]EMM:K ,5OO,= 18((|Z  (RSS %Y=3 ##  " " ..0 :#3D#9 : : k40 kooi&OP  
  1)%#5##5-+$jj$?%zz*:DA

9d3!',>!Ejj$/J5 " ''7t$c18z $6#;#;#=	  Kfehhoo. 	)+t, //+{y%--/XY	

  		L 	 |1139MW[\]L  
+JfZ--/5ISWXYJ#-#3#3#5D41ae!Q$DJD 
:&C*j'
#
 %@%F%F%H 	8!Iz' 8	.7 +8	8 : :@	 Es   #KA?K!8K'K'K	r(  providersess_optionsr8  r:  offload_folderoffload_state_dictmodel_variants	from_flaxrb   low_cpu_mem_usageuse_safetensorsdduf_entriesprovider_optionsquantization_configc           	      L   ddl m} t        | ||||||      \  }}d}|j                         D ]  \  }}|	t	        ||      s||   d   } |^|j
                  }|j                  t              xs |j                  t              }|rd|v r |        t        d| d| d	t         d
      t        |||du      }t        j                  t        j                  d
      d         }i } t	        |t         j"                  j$                        r|| d<   t	        ||j&                        r|| d<   || d<   || d<   t	        ||j(                        }!t+               rAt-        j.                  t-        j.                  t0        j2                        j4                        }"nd}"t+               xr* t	        |t6              xr |"t-        j.                  d      k\  }#|!s|#r|	| d<   |
| d<   || d<   || d<   |j9                  |d      | d<   || d<   |rd| d<   |#r,| d   '|"t-        j.                  d      k  rt;        d| d      |#r| d   | j9                  d       |r|#s|| d<   nd| d<   |Jt=        ||      r>t	        |t         j"                  j$                        r|j?                  |!|       }$|$|$| d!<   |r|| d"<    ||fi | }%nnt@        jB                  jE                  t@        jB                  jG                  ||            r( |t@        jB                  jG                  ||      fi | }%n	 ||fi | }%t=        |%t         j"                  j$                        rUt=        |	tH              rEtK        |%d#       |	d$   d%k(  }&|&r tM        |%|%jO                         |	dd&       |%S tM        |%|	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   rD  rE  rM  r   r   r8  r:  rF  rG  rb   rK  TrI  z4.27.0zWhen passing `variant='zS'`, please make sure to upgrade your `transformers` version to at least 4.27.0.dev0rJ  F)is_diffusersr>  rN  rL  )recurser   r  )
state_dictr8  force_hooksmain_device)r8  rV  )(
quantizersrP  r   r]   r   r   r   DUMMY_MODULES_FOLDER!TRANSFORMERS_DUMMY_MODULES_FOLDERr   r4  _get_load_methodr   r   r   rQ   r2  r5  r6  r1   r.   r   r   r  r9   r   r  r$   r	  ImportErrorr   _resolve_quant_configrU   rV   isdirrT   r   r(   r'   rU  )'r   r   r   r   r   r(  r   rD  rE  r8  r:  rF  rG  rH  r   rI  rb   rJ  r   rK  rL  rM  rN  rP  r   r   load_method_namer   none_moduleis_dummy_pathload_methodr   loading_kwargsr  r  r  model_quant_configr<  needs_offloading_to_cpus'                                          rL   load_sub_modelrf    s   4 8 #?#I '7'='='? A#
O&:i+Q1*=a@A
 **#../CD 
H^H^-I
 W3KYKtN+; <66L5MQP
 	

 #9.>\`H`aK !..x~~c/B1/EFN)UXX__-(3}%)->>?%-z")5~&-=)*#I/?/J/JK "&}}W]]<;S;S-T-a-ab$ 	"# 	<y/2	< GMM($;;  2'1|$'1|$+9'(/A+,$2$6$6tT$By!,;()*.N;'
 "y)5$w}}X'>>)'  3F  G  #~i'@'Hy) 32CN./27N./ 	'*,FGy%((//20FF+ G 
 )4FN01 )5~&&t>~>	rww||M48	9&rww||M4'H[N[ '}GG"EHHOO4JPT9U 0$?",R.E"9" +668%   +
PTUr   r   r_  rR  c                 t     |r*t         t              r fdS t         t              r f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| i |S rw   )r"   argsr  r   s     rL   r-  z"_get_load_method.<locals>.<lambda>s  s    +DY+`QU+`Y_+` r   c                  "    t        g| i |S rw   )r#   ri  s     rL   r-  z"_get_load_method.<locals>.<lambda>u  s    +Mi+iZ^+ibh+i r   )r   r%   r$   r   )r   r_  rR  s   `  rL   r[  r[  i  s7     i!89``i1ii9.//r   c                 "   t        j                  t        j                  d      d         }t	        |d      }t        |       }|j                  j                  d      d   }|j                  j                  d      }t        |      dkD  r|d   nd }|j                  j                  d      }||v xr t        ||      }|r|}n|t        vr|j                  }t        |t              r|j                  }	||	fS |j                  j                  }	||	fS )Nr<   r   r   r   )r   r   r   rQ   r   r   r   rW   r   LOADABLE_CLASSESr   typer   )
r?  r   r   not_compiled_moduler   module_path_itemspipeline_dirrV   r   r   s
             rL   _fetch_class_library_tuplers  y  s    ..x~~c/B1/EF(+6I (/!,,2237:G ,66<<SA,/0A,BQ,F$R(DL))//4D%-R')\2R
 	(	(%00 %t,(11
 Z   )22;;
Z  r   folderr   c                 4   i }t        j                  |       D ]z  }t         j                  j                  | |      }t         j                  j	                  |      xr ||v }|xr' t        fdt        j                  |      D              }|sv||<   | |S )Nc              3   d   K   | ]'  }|j                  d       d   j                         ) yw)r<   r!   N)rQ   r   )rJ   prb   s     rL   rM   z+_identify_model_variants.<locals>.<genexpr>  s)     .tWXqwws|A/I/I'/R.ts   -0)rU   listdirrV   rT   r^  r\   )rt  rb   r   rH  
sub_folderfolder_path	is_foldervariant_existss    `      rL   _identify_model_variantsr}    s    N**V, 	5J'',,vz:Kk2KzV7KI&t3.t\^\f\fgr\s.t+tN-4z*	5 r   c                    d }t         j                  j                  t         j                  j                  | | d            r't         j                  j                  | | d      }||fS t	        |d   t
        t        f      rwt         j                  j                  t         j                  j                  | |d   d    d            r1t         j                  j                  | |d   d    d      }|d   d   }||fS )Nr   r   r   r!   )rU   rV   r   rT   r   r  tuple)rt  r   r   custom_class_names       rL    _resolve_custom_pipeline_and_clsr    s    	ww~~bggll6o->c+BCD'',,v/1B#/FG --- 
F=)D%=	9bggnn
V} 5a 89=>? '',,v&2G2J1K3/OP"=1!4---r   r   c                    | j                   dk(  rrt        j                  t        j                  |d         j                        t        j                  d      k  r&ddlm}m} |} d| d| d| d	}t        d
d|d       y y y )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  r8   r  r  r   )r(  r   r   r  r  deprecation_messages         rL   #_maybe_raise_warning_for_inpaintingr    s    "BBw}}f123@@H	w	H  	c=455GHfGg h 99 :l	l 	 	8'CVfkl!H Br   init_kwargspassed_pipe_kwargspassed_class_objsc                    ddl m} t        j                  t        j
                  j                  |d            }t        D ci c]  }|t        |j                  |d g      d     }}|j                         }	|	+t        |	      dk\  r|D ]  }
d|
v s|	j                  |
      } fd}|j                         D ci c]#  \  }}|| |j                  |fi |	 ||      % }}}|j                         D ]S  \  }}| j                  |j                   j                         D ci c]  \  }}dj                  ||g      | c}}       U | S c c}w c c}}w c c}}w )Nr!   )r0   z	README.mdr   	schedulerc                 d   j                         D ci c]3  \  }}|j                  d      d   | k(  s|j                  |  dd      |5 }}}j                         D ci c]3  \  }}|j                  d      d   | k(  s|j                  |  dd      |5 }}}i ||}|S c c}}w c c}}w )Nr   r   r   )r]   rQ   replace)prefixr@  rd   connected_passed_class_objconnected_passed_pipe_kwargsconnected_passed_kwargsr  r  s         rL   get_connected_passed_kwargszP_update_init_kwargs_with_connected_pipeline.<locals>.get_connected_passed_kwargs  s    7H7N7N7P&
/3q!TUT[T[\_T`abTcgmTmAIIlB'*&
" &
 8J7O7O7Q(
/3q!UVU\U\]`UabcUdhnUnAIIlB'*(
$ (
 #a%?"`C_"`&&&
(
s   B&B&#B,B,r   )pipeline_utilsr0   r   loadrU   rV   rT   CONNECTED_PIPES_KEYSr   datar  rW   r	  r]   r-   r   rk   )r  r  r  rt  pipeline_loading_kwargsr0   	modelcardr  connected_pipespipeline_loading_kwargs_cpr@  r   r  r   connected_piper   rl   s    ``              rL   +_update_init_kwargs_with_connected_pipeliner    s    2rww||FK@AIXlmfvwy~~vvFqIImOm "9!=!=!?!-#6P2QUV2V( 	6Aa.2215	6	'  /446	 FG	 	1!11
1
5PQW5X
 	
O  #2"7"7"9 
HVHaHaHgHgHij_T9SXXvtn%y0j	

 G n* ks   #E?(E(Epretrained_model_nameconfig_dictr_   r   c                    |j                         }|j                         D cg c]  \  }}t        |t              s|dk7  s|  }}}t	        j
                  t        j                  d      d         }t        |d      }	i }
|D ]U  }||   d   }|t        |t              s| d| d}||v r||
|<   0|t        vs9t        |	|      rFt        | d|  d       |
|fS c c}}w )	Nr   r<   r   r   rF   r   z4 as defined in `model_index.json` does not exist in z. and is not a module in 'diffusers/pipelines'.)r  r]   r   r  r   r   r   rQ   r   strrn  r   r   )r  r  r_   r   rb   r@  rA  ra   r   r   custom_componentsrl   module_candidatecandidate_files                 rL   "_get_custom_components_and_foldersr    s$    ""$K #."3"3"5d$!QAt9LQRVcQcAdLd ..x~~c/B1/EF(+6I ! 	&y1!4#:6F+L &;a(8'9=Y&+;i(%55giQa>b!""VWlVm  n\  ]   l**/ es   C!C!C!model_folder_namesr   allow_pickleuse_onnxis_onnxc	                     |r#|s!t        || ||      st        d| d| d      |rg d}	|	S |r$t        || ||      rddg}	||n|}|s|	dd	gz  }	|	S d
dg}	||n|}|s|	dd	gz  }	|	S )N)r`   ra   rb   z6Could not find the necessary `safetensors` weights in z
 (variant=))*.bin*.safetensors*.onnx*.pbr  z	*.msgpackr  r  r  )rt   EnvironmentError)
r`   r  r   rK  rI  r  r  r  rb   r   s
             rL   _get_ignore_patternsr    s     	)/@Oakr
 D_DUU_`g_hhij
 	
 F$ ! 
6+<K]gn #K0'38&11O  +K8'38&11Or   	dduf_filepipeline_class_namer   r   r   r   r   c           
         d }|s	 t        | ||      }	|s)|'|d 	j                  D        vrt        d| d|  d      	 |dd	}t        | ||||||g|
      }|S # t        t        t        j                  f$ r'}
t
        j                  d|
 d       d}|
}Y d }
~
d }
~
ww xY w# t        $ r | t        d|  d      |w xY w)Nr   zCouldn't connect to the Hub: z$.
Will try to load from local cache.Tc              3   4   K   | ]  }|j                     y wrw   )r   )rJ   r   s     rL   rM   z&_download_dduf_file.<locals>.<genexpr>Y  s     MGg//Ms   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ConnectionErrorr   r   r   r   r   FileNotFoundErrorr  )r  r  r  r   r   r   r   r   model_info_call_errorr   er  r   s                rL   _download_dduf_filer  C  s    !	&358TD !Mt}}MM:i[0JK`Jaabcdd)(;TJ
)!-%;!	
 1 /1I1IJ 	&NN:1#=bcd#$%!	&2  )
 !( #$%:$; <  )	))s#   A  B# B 9BB #C c                     d}| D ]%  }t        | |   t              s| |   d   dk(  }|s% n |rt        dd      st        d      y y )NFr   r9   >z4.47.1zRPlease upgrade your `transformers` installation to the latest version to use DDUF.)r   r  r   r   )r  has_transformers_componentr@  s      rL   -_maybe_raise_error_for_incorrect_transformersr  {  sc    !& k!nd+)4Q):n)L&)	
 "*A#x*Pmnn +Q!r   c                    |y t        | j                               }d}d }t        |dd       t        |j                        }nJt        |dd       =t	        |j
                  t              r#t        |j
                  j                               }|r|j                  |      s||z
  }|r,t        j                  d| ddj                  |       d       y y )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<   )rZ   r   r   r  r   r  r   r   r   r   rT   )pipe_init_dictquant_configactual_pipe_componentsmissingquant_componentss        rL   /_maybe_warn_for_wrong_component_in_quant_configr    s     !4!4!67G|5t<H|BBC		5	AjQ]QkQkmqFr|99>>@A 0 9 9:P Q"%;;B7) L#yy)?@AD	
 r   )NNN)NN)NNNNN)NFNNNNNN)r&  rw   )mr   rU   rR   r   pathlibr   typingr   r   r   r   r   r	   r  r2  huggingface_hubr
   r   r   r   huggingface_hub.utilsr   r   	packagingr   requests.exceptionsr   r   r   utilsr   r   r   r   r   r   r   r   r   r   r   r   utils.torch_utilsr    transformers_loading_utilsr"   r#   r9   r$   r%   transformers.utilsrP   r&   rO   rN   r
  r'   accelerate.hooksr(   accelerate.utilsr)   r*   
INDEX_FILEr   rY  rZ  r  
get_loggerr   r   rn  r4  r   r   boolrt   r{   r[   PathLiker  r   r   r   r   r   r   r   r   r  r  floatr$  rC  deviceintrf  objectr[  rs  r   r}  r  r  r  r  r  r  r  r  rI   r   rL   <module>r     s    	 	   = =   O O L  )     3 e EVVL)8E +
) ( $8 !y 			H	% )*;<,.?@/1BC.0AB	 !23DE$57H#I-/@A#46G"H,.?@!24E F 	'):; (   =G!!"27";<=Uei Upd$Td/RWX\]_]h]hXiknXnRo d/N 
 
,
<  qu': F !0fQQQ S	Q 	Q
 Q Q sEKK'(Q bkk)*Qj eo'sEz"'37U
3C'^a'<`t *./\\\ S	\ 	\
 \ \ \ \ \ tC$56;<=\ eCHouS#X>?@\ U3#345\ \ cN\ \  !\" #\$ %\& bkk)*'\( )\* 4Y/0+\, -\. "#/\~0 0# 0 0QY 0 !B	S 	3 	 	 	
.mWZ mdh m())+/)DH)RU)	)^ &*-1!!+!+c3h!+ S	"!+  S	*	!+
 c]!+Z ")S	) #Y) 	)
 ) ) ) ) c]) 
#Y)X5)5)5) 5) 	5)
 5) 5) 5) 5)po
r   