
    bCiNm                     h   S r SSKrSSKrSSKrSSKJrJrJrJrJ	r	J
r
JrJrJrJr  SSKJr  \	" 5       (       a  SSKJr  \R&                  " \5      r " S S\5      r SS	 jrSS
\4S jjr     SS jrSS jr     SS jrS r       SS jr      SS jr SS jrSS jr SS jr!SS jr"g)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                   (    \ rS rSrSrSrSrSrSrSr	g)	TransposeType,   z
Possible ...
nosimpleconv1dconv2d N)
__name__
__module____qualname____firstlineno____doc__NOSIMPLECONV1DCONV2D__static_attributes__r       `/home/james-whalen/.local/lib/python3.13/site-packages/transformers/modeling_tf_pytorch_utils.pyr   r   ,   s     
BFFFr"   r   c                 t   UbM  U R                  U5      (       d  SU ;  a  [        SU  SU S35      eU [        U5      S n U R                  S5      n U R	                  SS5      n [        U 5      S	:  a  S
U ;   d  U R                  S
5      S:  a  [        SU -   5      e[        R                  " SSU 5      n U R	                  SS5      n [        R                  " SSU 5      n U R                  S5      n [        U 5      S:  a  U SS n [        U5      nU S   S:X  a#  Ub   [        U5      S:X  a  [        R                  nO{U S   S:X  a#  Ub   [        U5      S:X  a  [        R                  nOO[        U S   S;   =(       d    SU ;   =(       d    SU ;   5      (       a  [        R                  nO[        R                  nU S   S:X  d  U S   S:X  d	  U S   S:X  a  SU S'   U S   S:X  a  SU S'   U S   S:X  d	  U S   S:X  a  U S   R	                  S S!5      U S'   S"R!                  U 5      n U(       a  U R	                  USS5      n X4$ )#a1  
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:0 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_namerI   7   se   " !!*--2EW2Twi'G
| Tw w  #j/+,..%oodB'GGt 0W]]55IB5N]`gghhffgwG oosG ffVS'*GmmC G
7|a!"+?+O r{h?#>3CW[\C\!((			 _%@SEY]^E^!((		II 	"'!	"'!
 

 "((	!$$	 r{h'"+"=PWAWr{f r{((GBK;M,Mbk)))Y? hhwG//"8"a@r"   r   c                 @   U [         R                  L a  U(       a  SOSn[        XS9nO<U [         R                  L a  [        USS9nOU [         R                  L a  [        U5      nUc  U$ [        U5      [        UR                  5      :  a  [        U5      nO,[        U5      [        UR                  5      :  a
  [        USS9n[        U5      [        UR                  5      :w  a   [        X5      nU$ U$ ! [         a  nU=R                  X"4-  sl        UeSnAff = f)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-   rK   r   r   )axes)rK   r   r   Nr   )axis)r   r    transpose_funcr   r   r;   shaper   r   rB   r   AssertionErrorargs)r   r4   match_shapept_to_tfrL   es         r#   apply_transposerU      s    
 M(((  (|\2	m**	*  Y7	m**	*'
;#fll++	[	C-	-V!,KD..	V1F
 M6M	  	FF{00FG	s   'C6 6
D DDc           
      @    SSK nSSKnSSKJn	  [        U[        5      (       a  U/n0 n
U H  n[        R                  R                  U5      n[
        R                  SU 35        UR                  S5      (       a	  U	" U5      nO[        5         UR                  USSS	9nU
R!                  U5        M     [
        R                  S
[#        S U
R%                  5        5       5      S S35        ['        U U
UUUUUS9$ ! [         a    [
        R                  S5        e f = f)*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   @   #    U  H  oR                  5       v   M     g 7fN)numel).0ts     r#   	<genexpr>7load_pytorch_checkpoint_in_tf2_model.<locals>.<genexpr>   s     2]F\7799F\s   ,z parameters	tf_inputsallow_missing_keysoutput_loading_info_prefixtf_to_pt_weight_rename)
tensorflowtorchsafetensors.torchrX   ImportErrorloggererror
isinstancestrospathabspathinfoendswithr   loadupdatesumvalues!load_pytorch_weights_in_tf2_model)tf_modelpytorch_checkpoint_pathrf   rg   rh   ri   rj   tfrl   safe_load_filept_state_dictrt   pt_path
state_dicts                 r#   $load_pytorch_checkpoint_in_tf2_modelr      s   	A )3//#:"; M'''//$'3G9=>N++'0J$&G%dSJZ( ( KK.s2]mFZFZF\2]/]^_.``klm,-/5 5  j	
 	s   C< <!Dc                 6    UR                  5       n[        XX#S9$ )rW   )rf   rg   )r   r|   )r}   pt_modelrf   rg   r   s        r#   load_pytorch_model_in_tf2_modelr      s"    '')M,9 r"   c           
      Z    SSK nSSKnUR                  5        V	V
s0 s HM  u  pXR                  UR                  :w  a  U
R                  5       OU
R                  5       R                  5       _MO     nn	n
[        U UUUUUUS9$ ! [         a    [        R	                  S5        e f = fs  sn
n	f )z*Load pytorch state_dict in a TF 2.0 model.r   NrY   re   )rk   rl   rn   ro   rp   itemsdtypebfloat16numpyfloat$load_pytorch_state_dict_in_tf2_model)r}   r   rf   rg   rh   ri   rj   r   rl   kvs              r#   r|   r|      s     VcUhUhUjUjTQ5>>11779qwwy7HHUj   0-/5   j	
 	s   B AB'!B$c                    [        U5      S:  a#  [        R                  SU SU SU SU S3	5        O[        R                  SU S35        [        U 5      S:  a  [        R                  S	U S
U  S35        O[        R                  SU SU S35        [        U5      S:  aP  SR                  U VVVs/ s H  u  pEnSU SU SU S3PM     snnn5      n[        R                  SU SU S35        g g s  snnnf )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.
z- z: found shape z in the checkpoint and z in the model instantiatedSome weights of zh were not initialized from the model checkpoint are newly initialized because the shapes did not match:
)r;   ro   warningrD   )missing_keysunexpected_keysmismatched_keys
class_namekeyshape1shape2mismatched_warnings           r#   _log_key_warningsr     sP   
?a|2o. /| 55?L A5	5	
 	OPZ|[^_`
<1::, G99E GTT	
 	!* .##-,.XZ	
 ?a!YY ,;+:'C SEx/FvhNhi+:
 	zl +*+ ,88	
  s   4C4c	                   ^ ^" SSK n	Uc  T R                  nUc  SnU(       a!  U	R                  U5         T " USS9  SSS5        0 n
U H  nSnSU;   a  UR                  SS5      nSU;   a  UR                  SS	5      nS
U;   a  UR                  S
S5      nSU;   a  UR                  SS5      nUR	                  S5      nSm"USSS2   SS/:X  a	  US   S-   m"OUSSS2   SS/:X  a  US   S-   m"T"b  USS T"/-   nSR                  U5      nUc  UnXU'   M     Sn[        U 4S jU
 5       5      (       d  T R                  S-   nT R                  T R                  -   nSn[        U
R                  5       5      n/ n/ n[        US5      nU GH3  nUR                  n[        UUUR                  US9u  m"nUb   U" T"5      nU H  nUU
;   d  M  Um"  O   US   m"T"U
;  a[  U(       a  UR!                  T"5        Ml  T R"                  b&  [        U"4S jT R"                   5       5      (       a  M  [%        T" S35      eU
T"   nU(       a  UR'                  U5      nOUU   n [)        UUUR                  5      nU[1        U5      -  nUR3                  U	R5                  UUR6                  5      5        AUR9                  T"5        GM6     [:        R=                  SUS S35        [?        U5      nT R"                  b?  T R"                   H/  nU V s/ s H  n [@        RB                  " UU 5      b  M  U PM!     nn M1     T RD                  b?  T RD                   H/  nU V s/ s H  n [@        RB                  " UU 5      b  M  U PM!     nn M1     U(       d   [G        UUUT RH                  RJ                  S 9  U(       a
  UUUS!.n!T U!4$ T $ ! , (       d  f       GN= f! U	R*                  R,                   af  nU(       d+  [/        U5      nUS-  nU	R*                  R-                  U5      eUR!                  T"UR                  UR                  45         SnAGM  SnAff = fs  sn f s  sn f )"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trainingr3   r4   r5   r6   running_varmoving_variancerunning_meanmoving_meanr8   rK   parametrizations	original0_g	original1_vc              3   X   >#    U  H  oR                  TR                  5      v   M!     g 7fr^   r9   base_model_prefix)r`   sr}   s     r#   rb   7load_pytorch_state_dict_in_tf2_model.<locals>.<genexpr>l  s#     TASA||H6677AS   '*
get_tensor)rF   rG   rH   c              3   V   >#    U  H  n[         R                  " UT5      S Lv   M      g 7fr^   )r?   search)r`   patnames     r#   rb   r     s#     lCkCryyd+47Cks   &)z not found in PyTorch modelz_
	You may consider adding `ignore_mismatched_sizes=True` in the model `from_pretrained` method.zLoaded rd   z  parameters in the TF 2.0 model.r   r   r   r   )&rk   dummy_inputsrH   r=   rA   rD   anyr   trainable_weightsnon_trainable_weightssetkeyshasattrr   rI   rO   append_keys_to_ignore_on_load_missingAttributeErrorr   rU   errorsInvalidArgumentErrorrr   r   assigncastr   discardro   rv   rB   r?   r   "_keys_to_ignore_on_load_unexpectedr   	__class__r   )#r}   r   rf   rg   rh   ri   rj   ignore_mismatched_sizesskip_logger_warningsr   tf_keys_to_pt_keysr   new_keykey_componentsrF   symbolic_weightstf_loaded_numelall_pytorch_weightsr   r   is_safetensor_archivesymbolic_weightsw_namer   aliasesaliasstate_dict_namearrayrT   	error_msgr   r   r   loading_infor   s#   `                                 @r#   r   r   4  sy    ))	]]7#Y/ $ c>kk'84GS=kk&&1GCkk-1BCGS kk.-@G 3"%a% %7$EE!"%,DBEE"'9;&GG!"%,D+CR0D69Nhh~.G?G&)7#1 <  TASTTT!)!;!;c!A11H4R4RRO05578LO#M<@+!&&B#9+11	
i "-,T2G .. D ! qz ))!##D)99El8CkCklll D6)D!EFF,T2 !,,_=E!/2E	#Iuo6K6KLE 	;u--rwwuo.C.CDE##D)e ,h KK'/!,,LMN./O//;;;C'3Q|!ryya7HA|LQL <22>>>C*9W/QRYYsA=Nq/OWO ?,U]UgUgUpUpq(..

 %%Ok $#^ yy-- 		*F	w	 ii44Y??&&ekk?;P;P'QR		. R Xs<   N6OQ$QQ0Q6
OQ"AQQc                    / nU H:  n	[        U	SS9 n
[        U U
UUSUUUSS9	u  pS S S 5        UR                  W5        M<     [        [        R
                  " U Vs/ s H  n[	        US   5      PM     sn6 5      n[        U Vs/ s H  oS   PM	     sn/ 5      n[        U Vs/ s H  oS   PM	     sn/ 5      n[        XXR                  R                  S9  U(       a	  UUUS	.nX4$ U $ ! , (       d  f       N= fs  snf s  snf s  snf )
Nr   )	frameworkT)rf   rg   rh   ri   rj   r   r   r   r   r   r   r   )
r   r   r   sortedr   intersectionrz   r   r   r   )r}   safetensors_shardsrf   rg   rh   ri   rj   r   all_loading_infosshardsafetensors_archiver   rv   r   r   r   s                   r#   -load_sharded_pytorch_safetensors_in_tf2_modelr     s$    #u-1D%I###5$('=(?%)
&"H . 	  . $  #**Sd,eSd4Sn1E-FSd,efgL?PQ?Pt 12?PQSUVO?PQ?Pt 12?PQSUVOl_QcQcQlQlm(..

 %%O= .- -fQQs   C*C;D $D*
C8	c                 b    SSK nSSKnSSKnSSKJn  [        R                  SU 35        SU R                  R                  -   n	[        Xy5      n
U
" U R                  5      nUc  UR                  nUb  U" USS	9  U" X5        [        XX4S
9$ ! [         a    [        R	                  S5        e f = f)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   rg   rh   )rk   rl   rn   ro   rp   transformersmodeling_tf_utilsr   rv   r   r   getattrconfigr   load_tf2_model_in_pytorch_model)r   tf_checkpoint_pathrf   rg   rh   r   rl   r   r   tf_model_class_nametf_model_classr}   s               r#   $load_tf2_checkpoint_in_pytorch_modelr     s     2
KK23E2FGH !3!3!<!<<\?Nhoo.H))	U+H1*/A 5  j	
 	s   B !B.c                 .    UR                   n[        XX#S9$ )z$Load TF 2.0 model in a pytorch modelr   )weights!load_tf2_weights_in_pytorch_model)r   r}   rg   rh   r   s        r#   r   r     s     G,.@ r"   c                      SSK nSSKnU Vs0 s H  ofR
                  UR                  5       _M     nn[        XX#S9$ ! [         a    [        R	                  S5        e f = fs  snf )z.Load TF2.0 symbolic weights in a PyTorch modelr   Nr   r   )rk   rl   rn   ro   rp   r   r   $load_tf2_state_dict_in_pytorch_model)r   
tf_weightsrg   rh   r   rl   	tf_weighttf_state_dicts           r#   r   r   %  ss     ISS
9^^Y__%66
MS/4F   j	
 	 Ts   > $A"!Ac                   ^  SS K n0 n[        T R                  5       5      nSn[        U 4S jU 5       5      (       d  T R                  S-   n0 nUR                  5        H   u  p[        XU
R                  S9u  pX4X'   M"     [        UR                  5       5      n0 n/ nUR                  5        GH  u  nnUR                  5       U;   a,  UR                  5       S:w  a  UUR                  5          UU'   MG  UnUR                  S5      nS n	USS S2   SS	/:X  a	  US
   S-   n	OUSS S2   SS/:X  a  US
   S-   n	U	b  US S U	/-   nSR                  U5      nUU;  a(  U(       a  UR                  U5        M  [        U S35      eUU   u  nn[        UUUR                  SS9n[         R"                  " U5      (       a  [         R$                  " U5      n['        U5      (       d   [)        U5      (       d  UR!                  5       n[)        U5      (       a  UR+                  U5      nUUU'   UUUR                  5       '   UR-                  U5        GM     T R/                  USS9u  nnUU-  nT R0                  b?  T R0                   H/  nU Vs/ s H  n[2        R4                  " UU5      b  M  UPM!     nnM1     T R6                  b?  T R6                   H/  nU Vs/ s H  n[2        R4                  " UU5      b  M  UPM!     nnM1     [9        U5      S:  a_  [:        R=                  ST R>                  R@                   SU ST R>                  R@                   ST R>                  R@                   S3	5        O-[:        R=                  ST R>                  R@                   S35        [9        U5      S:  a1  [:        R=                  ST R>                  R@                   SU S35        OD[:        R=                  ST R>                  R@                   ST R>                  R@                   S35        [:        RC                  SU 35        U(       a	  UUS .nT U4$ T $ s  snf s  snf )!Nr   r'   c              3   X   >#    U  H  oR                  TR                  5      v   M!     g 7fr^   r   )r`   r   r   s     r#   rb   7load_tf2_state_dict_in_pytorch_model.<locals>.<genexpr>@  s#     XAWA||H6677AWr   r8   )rF   rG   r   rK   r   r   r   r   r   r   z not found in TF 2.0 modelF)rS   )strictzSSome 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   )"rl   dictnamed_parametersr   r   r   rI   rO   r   r   data_ptrrA   rD   r   r   rU   r   isscalarr   r	   r   
from_numpyr   load_state_dictr   r?   r   r   r;   ro   r   r   r   rv   )r   r   rg   rh   rl   new_pt_params_dictcurrent_pt_params_dictrF   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   r   s   `                         r#   r   r   7  s|   !(";";"=>  XAWXXX!)!;!;c!A N(..0EQZQ`Q`
 $-"8	 1 ,,./N!#O%;%A%A%C!	#==)BTBTBVZ[B[1KIL^L^L`1a~."0'--c2"%a% %7$EE!"%,DBEE"'9;&GG!"%,D+CR0D69N&)hh~&># #.8!&&~6 N#33M!NOO)*ABy	5)//ER>>%  KK&Eu%%nU.C.CKKME%  $$U+E-2>*;@"9#5#5#78~.Q &DT %-$<$<=OX]$<$^!L/O#L //;;;C'3Q|!ryya7HA|LQL < 22>>>C*9W/QRYYsA=Nq/OWO ? ?a""++,B.? @""++, -55=5G5G5P5P4Q R7	7	
 	NxOaOaOjOjNkknop
<1x11::; <)N +55	
 	!("4"4"="=!> ?##+#5#5#>#>"??ik	
 KKCNCSTU(4Y%%OK R Xs   	Q
)Q
Q5Q)r'   NN)NT)NFFNN)NF)NFFNNFF)NFFNNF)NFF)FF)#r   rs   r?   r   utilsr   r   r   r   r   r	   r
   r   r   r   r   rN   safetensorsr   
get_loggerr   ro   r   rI   rU   r   r   r|   r   r   r   r   r   r   r   r   r"   r#   <module>r     s    * 	 	    / % 
		H	%L  JNHV"} "Z 0f  F&
X !J` !*f af&R$mr"   