
    bi                         d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlmZ ddl	m
Z
mZmZ erd dlmZmZ  e       rd dlmZmZ  e
       rd dlZdd	d
edeeef   dd	fdZddd
edeeef   ddfdZy)    N)TYPE_CHECKINGDict)	DDUFEntry)tqdm   )is_safetensors_availableis_transformers_availableis_transformers_version)PreTrainedModelPreTrainedTokenizerclsr   namedduf_entriesreturnc           	         t        j                         5 }|j                         D ]  \  }}|j                  |dz         st	        j
                  j                  |g|j                  d       }t	        j                  t        j
                  j                  |      d       t        |d      5 }|j                         5 }	|j                  |	       ddd       ddd         | j                  t        j
                  j                        fi |cddd       S # 1 sw Y   LxY w# 1 sw Y   xY w# 1 sw Y   yxY w)a  
    Load a tokenizer from a DDUF archive.

    In practice, `transformers` do not provide a way to load a tokenizer from a DDUF archive. This function is a
    workaround by extracting the tokenizer files from the DDUF archive and loading the tokenizer from the extracted
    files. There is an extra cost of extracting the files, but of limited impact as the tokenizer files are usually
    small-ish.
    /T)exist_okwbN)tempfileTemporaryDirectoryitems
startswithospathjoinsplitmakedirsdirnameopenas_mmapwritefrom_pretrained)
r   r   r   kwargstmp_dir
entry_nameentrytmp_entry_pathfmms
             i/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/pipelines/transformers_loading_utils.py_load_tokenizer_from_ddufr+   $   s    
	$	$	& 	N'!-!3!3!5 	$J$$TCZ0!#g!N
8H8H8M!NBGGOON;dK.$/ $1 $B$$ $	$ #s""277??>#BMfM	N 	N$ $$ $	N 	NsB   +D7A/D70D*D	D*9D7D'#D**D4/D77E r   c           
      X   |j                  | d      }| t        d| d|j                          d      |j                  | dd      }|j                         D cg c],  \  }}|j	                  | d      r|j                  d      r|. }}}|s t        d	| d|j                          d      t               st        d
      t        dd      rt        d      t        j                         5 }	ddlm}
m} t        j                  j!                  |	d      }t#        |d      5 }|j%                  |j'                                ddd       |
j)                  |      }|et        j                  j!                  |	d      }t#        |d      5 }|j%                  |j'                                ddd       |j)                  |      }i }t+        j,                         5 }t/        |d      D ]Q  }|j1                  |j3                               }t4        j6                  j9                  |      }|j;                  |       S  | j(                  dd|||d|cddd       cddd       S c c}}w # 1 sw Y   )xY w# 1 sw Y   xY w# 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)a  
    Load a transformers model from a DDUF archive.

    In practice, `transformers` do not provide a way to load a model from a DDUF archive. This function is a workaround
    by instantiating a model from the config file and loading the weights from the DDUF archive directly.
    z/config.jsonNz0Could not find a config.json file for component z in DDUF file (contains z).z/generation_config.jsonr   z.safetensorsz-Could not find any weight file for component z\Safetensors is not available, cannot load model from DDUF. Please `pip install safetensors`.<z4.47.0zYou need to install `transformers>4.47.0` in order to load a transformers model from a DDUF file. You can install it with: `pip install --upgrade transformers`r   )
AutoConfigGenerationConfigzconfig.jsonwzgeneration_config.jsonzLoading state_dict)desc)pretrained_model_name_or_pathconfiggeneration_config
state_dict )getEnvironmentErrorkeysr   r   endswithr   r
   ImportErrorr   r   transformersr.   r/   r   r   r   r   r!   	read_textr"   
contextlib	ExitStackr   enter_contextr    safetensorstorchloadupdate)r   r   r   r#   config_filer4   r%   r&   weight_filesr$   r.   r/   tmp_config_filer(   r3   tmp_generation_config_filer5   stackmmaptensorss                       r*   "_load_transformers_model_from_ddufrL   ;   s    ""dV<#89K>tfD\]i]n]n]p\qqst
 	
 %((D61H)I4P ".!3!3!5J  D6,1D1D^1T 	L 
 ;D6AYZfZkZkZmYnnpq
 	
 $%j
 	
 sH-L
 	

 
	$	$	& '='',,w>/3' 	-1GGK))+,	-++O<()+g?W)X&0#6 7!)33567 0 @ @A[ \
!!# 	ul1EF +**5==?;%++006!!'*+ '3&& .2"3%	
 	 	 '.	- 	-
7 7	 	 	  s\   1I+55J * I1
AJ  I>1/J  A8J
	J 1I;	6J >J	J 
J	J  J))r>   r   r   typingr   r   huggingface_hubr   r   utilsr   r	   r
   r<   r   r   safetensors.torchrA   strr+   rL   r6       r*   <module>rS      s     	  & %  ` ` AAN	N&)N9=c9n9MNN.>	>"%>59#y.5I>>rR   