
    sgm                     B   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 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!y)z#PyTorch - TF 2.0 general utilities.    N   )	ExplicitEnum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y)TransposeTypez
    Possible ...
    nosimpleconv1dconv2dN)__name__
__module____qualname____doc__NOSIMPLECONV1DCONV2D     Y/var/www/html/venv/lib/python3.12/site-packages/transformers/modeling_tf_pytorch_utils.pyr   r   ,   s     
BFFFr   r   c                    |F| j                  |      sd| vrt        d|  d| d      | t        |      d } | j                  d      } | j	                  dd      } t        j                  d	d
|       } | j	                  dd      } t        j                  dd|       } | j                  d      } t        |       dkD  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                  }n=t        | d   dv xs
 d| v xs 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   j	                  dd      | d<   dj                  |       } |r| j	                  |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:0 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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_namerB   7   s*   " !!*-2EW2Twi'G
| Tw w  #j/+,..%oodB'G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@Ir   r   c                 >   | 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        |      }n.t        |      t        |j                        kD  rt        |d      }t        |      t        |j                        k7  r	 t        ||      }|S |S # t        $ r}|xj                  ||fz  c_        |d}~ww xY w)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'   rD   r   r   )axes)rD   r   r   Nr   )axis)r   r   transpose_funcr   r   r5   shaper   r   r;   r
   AssertionErrorargs)r   r.   match_shapept_to_tfrE   es         r   apply_transposerN      s   
 M(((  (|\T2	m**	*  Y7	m**	*'
;#fll++	[	C-	-V!,KD..	V[1F
 M6M	  	FF{K00FG	s   %C5 5	D>DDc           	      <   	 ddl }ddl}ddlm}	 ddlm}
 t        |t              r|g}i }|D ]  }t        j                  j                  |      }t        j                  d|        |j                  d      r	 |	|      }n|
rd	d
ini } |j                   |fddi|}|j#                  |        t        j                  dt%        d |j'                         D              dd       t)        | ||||||      S # t        $ r t        j                  d        w xY w)*Load pytorch checkpoints in a TF 2.0 modelr   N)	load_filer   )#is_torch_greater_or_equal_than_1_13Loading 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weights_onlyTmap_locationcpuzPyTorch checkpoint contains c              3   <   K   | ]  }|j                           y wN)numel).0ts     r   	<genexpr>z7load_pytorch_checkpoint_in_tf2_model.<locals>.<genexpr>   s     2]17792]s   ,z parameters	tf_inputsallow_missing_keysoutput_loading_info_prefixtf_to_pt_weight_rename)
tensorflowtorchsafetensors.torchrQ   pytorch_utilsrR   ImportErrorloggererror
isinstancestrospathabspathinfoendswithloadupdatesumvalues!load_pytorch_weights_in_tf2_model)tf_modelpytorch_checkpoint_pathr_   r`   ra   rb   rc   tfre   safe_load_filerR   pt_state_dictrn   pt_path
state_dictweights_only_kwargs                   r   $load_pytorch_checkpoint_in_tf2_modelr      s1   AF )3/#:"; M' 	)''//$'3G9=>N+'0J;^.$!7df#GV%VCUVJZ(	) KK.s2]mFZFZF\2]/]^_.``klm,-/5 5  j	
 	s   C; ; Dc                 @    |j                         }t        | |||      S )rP   )r_   r`   )r}   rv   )rw   pt_modelr_   r`   r{   s        r   load_pytorch_model_in_tf2_modelr      s'    '')M,-9I[ r   c           	      Z   	 ddl }ddl}|j                         D 	
ci c]M  \  }	}
|	|
j                  |j                  k7  r|
j                         n|
j                         j                         O }}	}
t        | ||||||      S # t        $ r t        j	                  d        w xY wc c}
}	w )z*Load pytorch state_dict in a TF 2.0 model.r   NrS   r^   )rd   re   rh   ri   rj   itemsdtypebfloat16numpyfloat$load_pytorch_state_dict_in_tf2_model)rw   r{   r_   r`   ra   rb   rc   ry   re   kvs              r   rv   rv      s     VcUhUhUjMQQPQ5>>11779qwwy7HHM  0-/5   j	
 	s   B AB' B$c                    t        |      dkD  r#t        j                  d| d| d| d| d	       nt        j                  d| d       t        |       dkD  rt        j                  d	| d
|  d       nt        j                  d| d| d       t        |      dkD  rNdj                  |D cg c]  \  }}}d| d| d| d c}}}      }t        j                  d| d| d       y y c c}}}w )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:
)r5   ri   warningr=   )missing_keysunexpected_keysmismatched_keys
class_namekeyshape1shape2mismatched_warnings           r   _log_key_warningsr   
  sQ   
?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   1C/c	                 8   " ddl }	| j                  }|d}|r$|	j                  |      5    |d       ddd       i }
|j                         D ]  }d}d|v r|j	                  dd      }d|v r|j	                  dd	      }d
|v r|j	                  d
d      }d|v r|j	                  dd      }|j                  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j                  |      }||}||
|<    d}t         fd|
j                         D              s j                  dz   } j                   j                  z   }d}t        |
j                               }g }g }t        |d      }|D ]  }|j                  }t        |||j                  |      \  "}| |"      }|D ]
  }||
v s|" n |d   ""|
vrM|r|j!                  "       b j"                  t        "fd j"                  D              rt%        " d      |
"   }|r|j'                  |      }n||   }	 t)        |||j                        }|t1        |      z  }|j3                  |	j5                  ||j6                               ~|j9                  "        t:        j=                  d|dd       t?        |      } j"                  7 j"                  D ](  }|D  cg c]  } tA        jB                  ||       |  }} *  jD                  7 jD                  D ](  }|D  cg c]  } tA        jB                  ||       |  }} * |s#tG        ||| jH                  jJ                          |r
|||d!}! |!fS  S # 1 sw Y   TxY w# |	j*                  j,                  $ r`}|s+t/        |      }|dz  }|	j*                  j-                  |      |j!                  "|j                  |j                  f       Y d}~~d}~ww xY wc c} w c c} w )"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trainingr-   r.   r/   r0   running_varmoving_variancerunning_meanmoving_meanr2   rD   parametrizations	original0_g	original1_vc              3   T   K   | ]  }|j                  j                         ! y wrX   r3   base_model_prefix)rZ   srw   s     r   r\   z7load_pytorch_state_dict_in_tf2_model.<locals>.<genexpr>k  s      [Aq||H667[   %(
get_tensor)r?   r@   rA   c              3   N   K   | ]  }t        j                  |      d u  y wrX   )r8   search)rZ   patnames     r   r\   z7load_pytorch_state_dict_in_tf2_model.<locals>.<genexpr>  s!     lCryyd+47ls   "%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.r   r   r   r   )&rd   dummy_inputsrA   keysr7   r:   r=   anyr   trainable_weightsnon_trainable_weightssethasattrr   rB   rH   append_keys_to_ignore_on_load_missingAttributeErrorr   rN   errorsInvalidArgumentErrorrl   r   assigncastr   discardri   rp   r;   r8   r   "_keys_to_ignore_on_load_unexpectedr   	__class__r   )#rw   r{   r_   r`   ra   rb   rc   ignore_mismatched_sizesskip_logger_warningsry   tf_keys_to_pt_keysr   new_keykey_componentsr?   symbolic_weightstf_loaded_numelall_pytorch_weightsr   r   is_safetensor_archivesymbolic_weightsw_namer   aliasesaliasstate_dict_namearrayrM   	error_msgr   r   r   loading_infor   s#   `                                 @r   r   r   3  s    ))	]]7# 	0Y/	0 !!# *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*<  [ASAXAXAZ[[!)!;!;c!A11H4R4RRO05578LO#M<@+ 2*!&&B#9+11	
i "-,T2G  ".. D" qz ))!##D)99El8CkCkll D6)D!EFF,T2 !,,_=E!/2E	#Iuo6K6KLE 	;u--rwwuo.C.CDE##D)e2*h KK'/!,,LMN./O//;;; 	RC'3Q!ryya7H7PAQLQ	R22>>> 	XC*9WQRYYsA=N=VqWOW	X,U]UgUgUpUpq(..

 %%Ok	0 	0^ yy-- 		*F	w	 ii44Y??&&ekk?;P;P'QR		. R Xs<   N	N/PP2PP	NP/AP

Pc                    g }|D ]@  }	t        |	d      5 }
t        | |
||d|||d	      \  } }d d d        |j                         B t        t	        j
                  |D cg c]  }t	        |d          c}       }t        |D cg c]  }|d   	 c}g       }t        |D cg c]  }|d   	 c}g       }t        |||| j                  j                         |r
|||d	}| |fS | S # 1 sw Y   xY wc c}w c c}w c c}w )
Nry   )	frameworkT)r_   r`   ra   rb   rc   r   r   r   r   r   r   r   )
r   r   r   sortedr   intersectionrt   r   r   r   )rw   safetensors_shardsr_   r`   ra   rb   rc   r   all_loading_infosshardsafetensors_archiver   rp   r   r   r   s                   r   -load_sharded_pytorch_safetensors_in_tf2_modelr     s+    # /u- 	1D%I###5$('=(?%)
&"Hl	 	  ./  #**Sd,e4Sn1E-F,efgL?PQt4 12QSUVO?PQt4 12QSUVOlO_QYQcQcQlQlm(..

 %%O=	 	 -fQQs   C( C4C9#C>(C1	c                 p   	 ddl }ddl}ddl}ddlm} t        j                  d|        d| j                  j                  z   }	t        ||	      }
 |
| j                        }||j                  }|
 ||d	        |||       t        | |||
      S # t        $ r t        j	                  d        w xY w)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   r`   ra   )rd   re   rh   ri   rj   transformersmodeling_tf_utilsr   rp   r   r   getattrconfigr   load_tf2_model_in_pytorch_model)r   tf_checkpoint_pathr_   r`   ra   ry   re   r   r   tf_model_class_nametf_model_classrw   s               r   $load_tf2_checkpoint_in_pytorch_modelr     s     2
KK23E2FGH !3!3!<!<<\+>?Nhoo.H))	U+H01*(/AWj 5  j	
 	s   B  B5c                 8    |j                   }t        | |||      S )z$Load TF 2.0 model in a pytorch modelr   )weights!load_tf2_weights_in_pytorch_model)r   rw   r`   ra   r   s        r   r   r     s%    G,'.@Vi r   c                     	 ddl }ddl}|D ci c]  }|j
                  |j                          }}t        | |||      S # t        $ r t        j	                  d        w xY wc c}w )z.Load TF2.0 symbolic weights in a PyTorch modelr   Nr   r   )rd   re   rh   ri   rj   r   r   $load_tf2_state_dict_in_pytorch_model)r   
tf_weightsr`   ra   ry   re   	tf_weighttf_state_dicts           r   r   r   $  sy     ISS9Y^^Y__%66SMS/-4F\o   j	
 	 Ts   A "A$ A!c                 *    dd l }i }t         j                               }d}t         fd|j	                         D              s j
                  dz   }i }|j                         D ]'  \  }	}
t        |	||
j                        \  }}|
|f||<   ) t        |j	                               }i }g }|j                         D ]g  \  }}|j                         |v r||j                            ||<   0|}|j                  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j                  |      }||vr"|r|j                  |       t        | d      ||   \  }}t        |||j                  d      }t!        j"                  |      rt!        j$                  |      }t'        |      st)        |      s|j!                         }t)        |      r|j+                  |      }|||<   |||j                         <   |j-                  |       j  j/                  |d      \  }}||z  } j0                  7 j0                  D ](  }|D cg c]  }t3        j4                  ||      | }}*  j6                  7 j6                  D ](  }|D cg c]  }t3        j4                  ||      | }}* t9        |      dkD  r_t:        j=                  d j>                  j@                   d| d j>                  j@                   d j>                  j@                   d	       n-t:        j=                  d j>                  j@                   d       t9        |      dkD  r1t:        j=                  d j>                  j@                   d| d       nDt:        j=                  d j>                  j@                   d j>                  j@                   d       t:        jC                  d|        |r	||d } |fS  S c c}w c c}w )!Nr   r#   c              3   T   K   | ]  }|j                  j                         ! y wrX   r   )rZ   r   r   s     r   r\   z7load_tf2_state_dict_in_pytorch_model.<locals>.<genexpr>?  s      _Aq||H667_r   r2   )r?   r@   r   rD   r   r   r   r   r   r   z not found in TF 2.0 modelF)rL   )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   )"re   dictnamed_parametersr   r   r   r   rB   rH   r   data_ptrr:   r=   r   r   rN   r   isscalarr   r   r   
from_numpyr   load_state_dictr   r8   r   r   r5   ri   r   r   r   rp   )r   r   r`   ra   re   new_pt_params_dictcurrent_pt_params_dictr?   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   6  s   !(";";"=>  _AWA\A\A^__!)!;!;c!A N(..0 9iE)?QZQ`Q`
 $-i"8w	9 ,,./N!#O%;%A%A%C (/!	#==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KKME% $$U+E-2>*;@"9#5#5#78~.Q(/T %-$<$<=OX]$<$^!L/O#L //;;; 	RC'3Q!ryya7H7PAQLQ	R 22>>> 	XC*9WQRYYsA=N=VqWOW	X ?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   "P>P%PP)r#   NN)NT)NFFNN)NF)NFFNNFF)NFFNNF)NFF)FF)"r   rm   r8   r   utilsr   r   r   r   r   r	   r
   r   r   r   rG   safetensorsr   
get_loggerr   ri   r   rB   rN   r   r   rv   r   r   r   r   r   r   r   r   r   r   <module>r     s     * 	 	 
 
 
 / % 
		H	%L  JNEP"} "Z 2j  F&
X !J` !*f af&R$mr   