
     `iNm                     P   d Z ddlZddlZddlZddl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  e	            rddlmZ  ej        e          Z G d de          Z	 dd
ZddefdZ	 	 	 	 	 ddZddZ	 	 	 	 	 ddZd Z	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 ddZd dZ d dZ!d dZ"dS )!z#PyTorch - TF 2.0 general utilities.    N   )
ExplicitEnumcheck_torch_load_is_safeexpand_dimsis_numpy_arrayis_safetensors_availableis_torch_tensorloggingreshapesqueezetensor_size)	transpose)	safe_openc                   "    e Zd ZdZdZdZdZdZdS )TransposeTypez
    Possible ...
    nosimpleconv1dconv2dN)__name__
__module____qualname____doc__NOSIMPLECONV1DCONV2D     z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/modeling_tf_pytorch_utils.pyr   r   ,   s.          
BFFFFFr   r    c                    |[|                      |          sd| vrt          d|  d| d          | t          |          d         } |                     d          } |                     dd          } t          |           d	k    rd
| v s|                     d
          dk    rt          d| z             t          j        dd|           } |                     dd          } t          j        dd|           } |                     d          } t          |           dk    r
| dd         } t          |          }| d         dk    r"| t          |          dk    rt          j        }nf| d         dk    r"| t          |          dk    rt          j        }n8t          | d         dv pd| v pd| v           rt          j        }nt          j        }| d         dk    s| d         dk    s| d         dk    rd| d<   | d         dk    rd| d<   | d         dk    s| d         dk    r| d                             d d!          | d<   d"                    |           } |r|                     |dd          } | |fS )#aU  
    Convert a TF 2.0 model variable name in a pytorch model weight name.

    Conventions for TF2.0 scopes -> PyTorch attribute names conversions:

        - '$1___$2' is replaced by $2 (can be used to duplicate or remove layers in TF2.0 vs PyTorch)
        - '_._' is replaced by a new level separation (can be used to convert TF2.0 lists in PyTorch nn.ModulesList)

    return tuple with:

        - pytorch model weight name
        - transpose: `TransposeType` member indicating whether and how TF2.0 and PyTorch weights matrices should be
          transposed with regards to each other
    Nfinal_logits_biaszWeight name z  does not start with name_scope z. This is an internal error in Transformers, so (unless you were doing something really evil) please open an issue to report it!/z:0r!   i   ___
   zBTF variable name is too long or contains too many ___ separators: z/[^/]*___([^/]*)/z/\1/z_._z//+r   kernel      )r(   pointwise_kerneldepthwise_kernel	emb_projs	out_projs
embeddingsgammaweightbetabiasr+   r,   _kernelz.weight.)
startswith
ValueErrorlenlstripreplacecountresubsplitlistr   r   r   boolr   r   join)tf_namestart_prefix_to_removetf_weight_shape
name_scoper   s        r    (convert_tf_weight_name_to_pt_weight_namerF   7   s   " !!*-- 	2EW2T2Tww w w
 w w w   #j//++,..%%oodB''GGt 0 0W]]55I5IB5N5N]`gghhhfgw G oos G fVS'**GmmC  G
7||a!""+?++O r{h?#>3CWCW[\C\C\!(				 	 _%@SEYEY]^E^E^!(			II 	"'!	"'!
 
 %
 "(		!$	 r{h'"+"="=PWAWAWr{f r{(((GBK;M,M,Mbk)))Y?? hhwG A//"8"a@@Ir   Tr   c                 d   | t           j        u r|rdnd}t          ||          }n=| t           j        u rt          |d          }n| t           j        u rt          |          }||S t          |          t          |j                  k     rt          |          }n6t          |          t          |j                  k    rt          |d          }t          |          t          |j                  k    r8	 t          ||          }n&# t          $ r}|xj        ||fz  c_        |d}~ww xY w|S )z
    Apply a transpose to some weight then tries to reshape the weight to the same shape as a given shape, all in a
    framework agnostic way.
    )   r*   r   r   )r*   rH   r   r   )axes)rH   r   r   Nr   )axis)r   r   transpose_funcr   r   r8   shaper   r   r?   r   AssertionErrorargs)r   r1   match_shapept_to_tfrI   es         r    apply_transposerR      sL   
 M(((  (9||\T222	m*	*	*  Y777	m*	*	*''
;#fl++++	[		C--	-	-V!,,,KD....	V[11FF 	 	 	FF{K00FFG	 Ms   9D
 

D-D((D-Fc           	         	 ddl }ddl}ddlm}	 n)# t          $ r t
                              d            w xY wt          |t                    r|g}i }
|D ]}t          j
                            |          }t
                              d|            |                    d          r |	|          }n&t                       |                    |dd	          }|
                    |           t
                              d
t#          d |
                                D                       dd           t'          | |
|||||          S )*Load pytorch checkpoints in a TF 2.0 modelr   N)	load_fileLoading a PyTorch model in TensorFlow, requires both PyTorch and TensorFlow to be installed. Please see https://pytorch.org/ and https://www.tensorflow.org/install/ for installation instructions.zLoading PyTorch weights from z.safetensorscpuT)map_locationweights_onlyzPyTorch checkpoint contains c              3   >   K   | ]}|                                 V  d S N)numel).0ts     r    	<genexpr>z7load_pytorch_checkpoint_in_tf2_model.<locals>.<genexpr>   s*      2]2]177992]2]2]2]2]2]r   ,z parameters	tf_inputsallow_missing_keysoutput_loading_info_prefixtf_to_pt_weight_rename)
tensorflowtorchsafetensors.torchrU   ImportErrorloggererror
isinstancestrospathabspathinfoendswithr   loadupdatesumvalues!load_pytorch_weights_in_tf2_model)tf_modelpytorch_checkpoint_pathrb   rc   rd   re   rf   tfrh   safe_load_filept_state_dictrp   pt_path
state_dicts                 r    $load_pytorch_checkpoint_in_tf2_modelr      s   	AAAAAAA   j	
 	
 	
 	 )3// <#:"; M' 	) 	)'//$''=G==>>>N++ 	T'00JJ$&&&G%dSSJZ((((
KKls2]2]mFZFZF\F\2]2]2]/]/]llllmmm,-/5   s    &7c                 P    |                                 }t          | |||          S )rT   )rb   rc   )r   rx   )ry   pt_modelrb   rc   r}   s        r    load_pytorch_model_in_tf2_modelr      s4    ''))M,-9I[   r   c           	          	 ddl }ddln)# t          $ r t                              d            w xY wfd|                                D             }t          | ||||||          S )z*Load pytorch state_dict in a TF 2.0 model.r   NrV   c                     i | ]P\  }}||j         j        k    r|                                n%|                                                                QS r   )dtypebfloat16numpyfloat)r]   kvrh   s      r    
<dictcomp>z5load_pytorch_weights_in_tf2_model.<locals>.<dictcomp>   sY       MQQPQ5>11177999qwwyy7H7H  r   ra   )rg   rh   rj   rk   rl   items$load_pytorch_state_dict_in_tf2_model)	ry   r}   rb   rc   rd   re   rf   r{   rh   s	           @r    rx   rx      s       j	
 	
 	
 	   UbUhUhUjUj  M 0-/5   s    &2c                    t          |          dk    r(t                              d| d| d| d| d	           nt                              d| d           t          |           dk    r"t                              d	| d
|  d           n!t                              d| d| d           t          |          dk    rBd                    d |D                       }t                              d| d| d           d S d S )Nr   zSSome weights of the PyTorch model were not used when initializing the TF 2.0 model : ,
- This IS expected if you are initializing z from a PyTorch model trained on another task or with another architecture (e.g. initializing a TFBertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing z from a PyTorch model that you expect to be exactly identical (e.g. initializing a TFBertForSequenceClassification model from a BertForSequenceClassification model).z6All PyTorch model weights were used when initializing .
z,Some weights or buffers of the TF 2.0 model zH were not initialized from the PyTorch model and are newly initialized: o
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.All the weights of z were initialized from the PyTorch model.
If your task is similar to the task the model of the checkpoint was trained on, you can already use * for predictions without further training.
c           	      .    g | ]\  }}}d | d| d| dS )z- z: found shape z in the checkpoint and z in the model instantiatedr   )r]   keyshape1shape2s       r    
<listcomp>z%_log_key_warnings.<locals>.<listcomp>'  sJ       'C jSiiiiviii  r   Some weights of zh were not initialized from the model checkpoint are newly initialized because the shapes did not match:
)r8   rk   warningrA   )missing_keysunexpected_keysmismatched_keys
class_namemismatched_warnings        r    _log_key_warningsr     s   
?a55 5-5 55 5 6@	5 5 5	
 	
 	
 	
 	_PZ___```
<1T: T T9ET T T	
 	
 	
 	
 	Z* Z Z#-Z Z Z	
 	
 	
 ?a!YY +:  
 
 	8z 8 8*8 8 8	
 	
 	
 	
 	
  r   c	                     ! ddl }	| j        }|d}|r:|	                    |          5    |d           ddd           n# 1 swxY w Y   i }
|D ]}d}d|v r|                    dd          }d|v r|                    dd	          }d
|v r|                    d
d          }d|v r|                    dd          }|                    d          }d |ddd         ddgk    r|d         dz    n|ddd         ddgk    r|d         dz     #|dd          gz   }d                    |          }||}||
|<   d}t           fd|
D                       s
 j        dz   } j         j	        z   }d}t          |
                                          }g }g }t          |d          }|D ]}|j        }t          |||j        |          \   }|  |           }|D ]
}||
v r|  n	|d           |
vrR|r|                                c j        !t           fd j        D                       rt%            d          |
          }|r|                    |          }n||         }	 t)          |||j                  }no# |	j        j        $ r]}|s.t/          |          }|dz  }|	j                            |          |                     |j        |j        f           Y d}~Dd}~ww xY w|t1          |          z  }|                    |	                    ||j                             ~|                                t:                              d|dd           t?          |          } j         j        D ]!!fd |D             } j          j         D ]!!fd!|D             }|stC          ||| j"        j#        "           |r
|||d#} |fS  S )$zLoad a pytorch state_dict in a TF 2.0 model. pt_state_dict can be either an actual dict or a lazy-loading
    safetensors archive created with the safe_open() function.r   Nr!   Ftrainingr0   r1   r2   r3   running_varmoving_variancerunning_meanmoving_meanr5   rH   parametrizations	original0_g	original1_vc              3   L   K   | ]}|                     j                  V  d S r[   r6   base_model_prefix)r]   sry   s     r    r_   z7load_pytorch_state_dict_in_tf2_model.<locals>.<genexpr>l  s2      TTAq||H677TTTTTTr   
get_tensor)rC   rD   rE   c              3   F   K   | ]}t          j        |          d uV  d S r[   r<   search)r]   patnames     r    r_   z7load_pytorch_state_dict_in_tf2_model.<locals>.<genexpr>  s4      llCryd++47llllllr   z not found in PyTorch modelz_
	You may consider adding `ignore_mismatched_sizes=True` in the model `from_pretrained` method.zLoaded r`   z  parameters in the TF 2.0 model.c                 >    g | ]}t          j        |          |S r[   r   r]   r   r   s     r    r   z8load_pytorch_state_dict_in_tf2_model.<locals>.<listcomp>  *    QQQ!rya7H7H7PA7P7P7Pr   c                 >    g | ]}t          j        |          |S r[   r   r   s     r    r   z8load_pytorch_state_dict_in_tf2_model.<locals>.<listcomp>  *    WWWQRYsA=N=N=Vq=V=V=Vr   r   r   r   r   )$rg   dummy_inputsrE   r:   r>   rA   anyr   trainable_weightsnon_trainable_weightssetkeyshasattrr   rF   rL   append_keys_to_ignore_on_load_missingAttributeErrorr   rR   errorsInvalidArgumentErrorrn   r   assigncastr   discardrk   rr   r?   "_keys_to_ignore_on_load_unexpectedr   	__class__r   )"ry   r}   rb   rc   rd   re   rf   ignore_mismatched_sizesskip_logger_warningsr{   tf_keys_to_pt_keysr   new_keykey_componentsrC   symbolic_weightstf_loaded_numelall_pytorch_weightsr   r   is_safetensor_archivesymbolic_weightsw_namer   aliasesaliasstate_dict_namearrayrQ   	error_msgr   loading_infor   r   s"   `                               @@r    r   r   4  s    )	 0]]7## 	0 	0HY////	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0  * *c>>kk'844GS==kk&&11GCkk-1BCCGS  kk.-@@G 3"%a% %7$EEE!"%,DDBEE"'9;&GGG!"%,D+CRC0D69Nhh~..G?G&)7##  TTTTASTTTTT B!)!;c!A1H4RRO0557788LO#M<@@+ 2* 2*!&B#9+1	
 
 
i "-,,T22G  " "... DE /
 qz )))! ##D)))9Ellll8Cklllll  D!E!E!EFFF,T2  	3!,,_==EE!/2E	#Iuo6KLLEEy- 		 		 		* FF	w	 i44Y???&&ek?;P'QRRR		 	;u---rwwuo.CDDEEE##D))))
KKM/MMMMNNN.//O/;; 	R 	RCQQQQ|QQQLL2>> 	X 	XCWWWW/WWWOO r,U]UgUpqqqq &(..
 

 %%Os*   AA
A
5JK8AK33K8c                    g }|D ][}	t          |	d          5 }
t          | |
||d|||d	  	        \  } }d d d            n# 1 swxY w Y   |                    |           \t          t	          j        d |D                        }t          d |D             g           }t          d |D             g           }t          |||| j        j	                   |r
|||d	}| |fS | S )
Nr{   )	frameworkT)rb   rc   rd   re   rf   r   r   c                 8    g | ]}t          |d                    S )r   )r   r]   rr   s     r    r   zAload_sharded_pytorch_safetensors_in_tf2_model.<locals>.<listcomp>  s%    ,e,e,e4Sn1E-F-F,e,e,er   c                     g | ]
}|d          S )r   r   r   s     r    r   zAload_sharded_pytorch_safetensors_in_tf2_model.<locals>.<listcomp>      QQQt4 12QQQr   c                     g | ]
}|d          S )r   r   r   s     r    r   zAload_sharded_pytorch_safetensors_in_tf2_model.<locals>.<listcomp>  r   r   r   r   )
r   r   r   sortedr   intersectionrv   r   r   r   )ry   safetensors_shardsrb   rc   rd   re   rf   r   all_loading_infosshardsafetensors_archiver   r   r   r   s                  r    -load_sharded_pytorch_safetensors_in_tf2_modelr     s    # / /u--- 	1D%I###5$('=(?%)
& 
& 
&"Hl	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  .... #*,e,eSd,e,e,efggLQQ?PQQQSUVVOQQ?PQQQSUVVOlO_QYQcQlmmmm &(..
 

 %%Os   A  A	A	c                    	 ddl }ddl}n)# t          $ r t                              d            w xY wddl}ddlm} t                              d|            d| j	        j
        z   }	t          ||	          }
 |
| j                  }||j        }| ||d	            |||           t          | |||
          S )z
    Load TF 2.0 HDF5 checkpoint in a PyTorch model We use HDF5 to easily do transfer learning (see
    https://github.com/tensorflow/tensorflow/blob/ee16fcac960ae660e0e4496658a366e2f745e1f0/tensorflow/python/keras/engine/network.py#L1352-L1357).
    r   NLoading a TensorFlow model in PyTorch, requires both PyTorch and TensorFlow to be installed. Please see https://pytorch.org/ and https://www.tensorflow.org/install/ for installation instructions.r   )load_tf_weightsz Loading TensorFlow weights from TFFr   rc   rd   )rg   rh   rj   rk   rl   transformersmodeling_tf_utilsr   rr   r   r   getattrconfigr   load_tf2_model_in_pytorch_model)r   tf_checkpoint_pathrb   rc   rd   r{   rh   r   r   tf_model_class_nametf_model_classry   s               r    $load_tf2_checkpoint_in_pytorch_modelr     s(      j	
 	
 	
 	 222222
KKG3EGGHHH !3!<<\+>??N~ho..H)	U++++OH0111*(/AWj       &1c                 6    |j         }t          | |||          S )z$Load TF 2.0 model in a pytorch modelr   )weights!load_tf2_weights_in_pytorch_model)r   ry   rc   rd   r  s        r    r   r     s,    G,'.@Vi   r   c                     	 ddl }ddl}n)# t          $ r t                              d            w xY wd |D             }t          | |||          S )z.Load TF2.0 symbolic weights in a PyTorch modelr   Nr   c                 B    i | ]}|j         |                                S r   )r   r   )r]   	tf_weights     r    r   z5load_tf2_weights_in_pytorch_model.<locals>.<dictcomp>1  s&    SSS9Y^Y__%6%6SSSr   r   )rg   rh   rj   rk   rl   $load_tf2_state_dict_in_pytorch_model)r   
tf_weightsrc   rd   r{   rh   tf_state_dicts          r    r  r  %  s       j	
 	
 	
 	 TS
SSSM/-4F\o   r  c                     dd l }i }t                                                     }d}t           fd|D                       s
 j        dz   }i }|                                D ]&\  }	}
t          |	||
j                  \  }}|
|f||<   't          |	                                          }i }g }|                                D ]\  }}|
                                |v r6|
                                dk    r||
                                         ||<   R|}|                    d          }d }	|dd d         dd	gk    r|d
         dz   }	n|dd d         ddgk    r|d
         dz   }	|	#|d d         |	gz   }d                    |          }||vr*|r|                    |           t          | d          ||         \  }}t          |||j        d          }t!          j        |          rt!          j        |          }t'          |          s#t)          |          s|                                }t)          |          r|                    |          }|||<   |||
                                <   |                    |           ˉ                     |d          \  }}||z  } j         j        D ]fd|D             } j         j        D ]fd|D             }t5          |          dk    rFt6                              d j        j         d| d j        j         d j        j         d	           n(t6                              d j        j         d           t5          |          dk    r,t6                              d j        j         d| d           n5t6                              d j        j         d j        j         d            t6                              d!|            |r	||d"} |fS  S )#Nr   r!   c              3   L   K   | ]}|                     j                  V  d S r[   r   )r]   r   r   s     r    r_   z7load_tf2_state_dict_in_pytorch_model.<locals>.<genexpr>@  s2      XXAq||H677XXXXXXr   r5   )rC   rD   r   rH   r   r   r   r   r   r   z not found in TF 2.0 modelF)rP   )strictc                 >    g | ]}t          j        |          |S r[   r   r   s     r    r   z8load_tf2_state_dict_in_pytorch_model.<locals>.<listcomp>  r   r   c                 >    g | ]}t          j        |          |S r[   r   r   s     r    r   z8load_tf2_state_dict_in_pytorch_model.<locals>.<listcomp>  r   r   zSSome weights of the TF 2.0 model were not used when initializing the PyTorch model r   r   z from a TF 2.0 model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a TFBertForPreTraining model).
- This IS NOT expected if you are initializing z from a TF 2.0 model that you expect to be exactly identical (e.g. initializing a BertForSequenceClassification model from a TFBertForSequenceClassification model).z5All TF 2.0 model weights were used when initializing r   r   zG were not initialized from the TF 2.0 model and are newly initialized: r   r   z were initialized from the TF 2.0 model.
If your task is similar to the task the model of the checkpoint was trained on, you can already use r   z1Weights or buffers not loaded from TF 2.0 model: )r   r   ) rh   dictnamed_parametersr   r   r   rF   rL   r   r   data_ptrr>   rA   r   r   rR   r   isscalarr   r	   r   
from_numpyr   load_state_dictr   r   r8   rk   r   r   r   rr   )r   r
  rc   rd   rh   new_pt_params_dictcurrent_pt_params_dictrC   tf_weights_mapr   r  pt_namer   all_tf_weightsloaded_pt_weights_data_ptrmissing_keys_ptpt_weight_name	pt_weightpt_weight_name_to_checkr   r   r   r   r   r   s   `                       @r    r  r  7  s9   LLL!(";";"="=>>  XXXXAWXXXXX B!)!;c!A N(..00 9 9iE)?QZQ`
 
 
 $-i"8w,,..//N!#O%;%A%A%C%C (/ (/!	#===)BTBTBVBVZ[B[B[1KIL^L^L`L`1a~."0'--c22"%a% %7$EEE!"%,DDBEE"'9;&GGG!"%,D+CRC0D69N&)hh~&>&># #.88! &&~666 N!N!N!NOOO)*ABy	5)/ERRR>%   	'K&&Eu%% 	"nU.C.C 	"KKMME%   	,$$U++E-2>*;@"9#5#5#7#78~....$,$<$<=OX]$<$^$^!L/O#L /;; 	R 	RCQQQQ|QQQLL2>> 	X 	XCWWWW/WWWOO
?a7"+7 7/>7 7"+7 7 6>5G5P	7 7 7	
 	
 	
 	
 	oxOaOjoooppp
<15x1: 5 5)5 5 5	
 	
 	
 	
 	k("4"= k k#+#5#>k k k	
 	
 	
 KKTNTTUUU &(4YY%%Or   )r!   NN)NT)NFFNN)NF)NFFNNFF)NFFNNF)NFF)FF)#r   ro   r<   r   utilsr   r   r   r   r   r	   r
   r   r   r   r   rK   safetensorsr   
get_loggerr   rk   r   rF   rR   r   r   rx   r   r   r   r   r   r  r  r   r   r    <module>r#     s   * ) 				 				                         / . . . . .  &%%%%%% 
	H	%	%    L    JNH H H HV" "} " " " "Z 0 0 0 0f           F&
 &
 &
X !J J J J` !* * * *f af& & & &R      $m m m m m mr   