
    Z j1'              
          S SK r S SKrS SKrS SKrS SKJrJr  S SKJr  S SK	J
r
  \R                  " S5      rS\S\4S jrS$S	\\   S\4S
 jjrS\\\
4   S\\\
4   4S jr\R                  " S5      rS\S\4S jrS rS rSSSSSSSSS.rS rS%S jr\ " S S5      5       r S&S\S \S!\S"\ R:                  S-  4S# jjrg)'    N)OrderedDictdefaultdict)	dataclass)Anyz(?<=\.)(\d+)(?=\.|$)keyreturnc                 .    [         R                  SU 5      $ )zBReplace every dot-delimited integer with '*' to get the structure.*)	_DIGIT_RXsub)r   s    r/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/utils/loading_report.py_pattern_ofr      s    ==c""    valuesc                     [        U 5      S:X  a  [        U S   5      $ [        U 5      n [        U 5      U:  a  U S    SU S    3$ SR                  [	        [        U 5      5      $ )zEFormat a list of ints as single number, {a, ..., b}, or first...last.   r   z...z, )lenstrsortedjoinmap)r   cutoffs     r   _fmt_indicesr      s`    
6{a6!9~F^F
6{V)Cr
|,,99Sf%&&r   mappingc           	         Sn[        U [        5      (       d  U  Vs0 s H  o"U_M     n nSn[        [        5      nU R	                  5        H  u  pE[
        R                  U5      n[        U5      n[        U5       HP  u  p[        X7   5      U::  a  X7   R                  [        5       5        X7   U   R                  [        U	5      5        MR     X7   R                  U5        M     0 n
UR	                  5        H  u  p{USS US   p\UR                  S5      nUS   n[        S[        U5      5       Hk  nUS-
  [        U5      :  aJ  XS-
     (       a>  [!        [#        XS-
     5      5      n[        XS-
     5      S:  a  USU-   S	-   -  nO
X-  nOUS-  nXU   -  nMm     XZU'   M     [%        U
5      nU(       a  UR'                  5       $ U$ s  snf )
z
Merge keys like 'layers.0.x', 'layers.1.x' into 'layers.{0, 1}.x'
BUT only merge together keys that have the exact same value.
Returns a new dict {merged_key: value}.
FTNr   r
   r   r   {})
isinstancedictr   listitemsr   findallr   	enumerater   appendsetaddintsplitranger   r   r   keys)r   not_mappingkbucketr   valdigspattid	out_itemsr   setspartsfinalinsertouts                    r   update_key_namer:   )   s    Kgt$$!()Aa4).9$.?FMMO  %3dODA6< A%##CE*LOA' $ 	C  $ I3BKc

3aq#e*%A1us4y Ta%[%fTa%[&9:tE{#a'S6\C//EOE1XE & % '  i
 CxxzJC *s   Gz\x1b\[[0-9;]*msc                 @    [         R                  S[        U 5      5      $ )N )_ansi_rer   r   )r;   s    r   _strip_ansir?   Y   s    <<CF##r   c           	      j    [        U 5      n[        SU[        [        U5      5      -
  5      nUSU-  -   $ )Nr    )r   maxr   r?   )textwidthtpads       r   _padrG   ]   s3    D	A
a[^,,
-CsSy=r   c           
         U (       a  [        [        U/U -   6 5      OU/nU Vs/ s H  n[        S U 5       5      PM     nnSR                  S [        X5       5       5      nSR                  S U 5       5      nU  Vs/ s H$  nSR                  S [        Xt5       5       5      PM&     nnSR                  XV/U-   5      $ s  snf s  snf )Nc              3   J   #    U  H  n[        [        U5      5      v   M     g 7fN)r   r?   ).0xs     r   	<genexpr>_make_table.<locals>.<genexpr>f   s     3s!#k!n%%ss   !#z | c              3   <   #    U  H  u  p[        X5      v   M     g 7frJ   rG   )rK   hws      r   rM   rN   g   s     I4HDAT!ZZ4H   z-+-c              3   ,   #    U  H
  nS U-  v   M     g7f)-N )rK   rR   s     r   rM   rN   h   s     26a#'6s   c              3   <   #    U  H  u  p[        X5      v   M     g 7frJ   rP   )rK   crR   s      r   rM   rN   i   s     =ndatAzznrS   
)r!   ziprB   r   )	rowsheaderscolscolwidthsheader_linesep_linerbodys	            r   _make_tablerd   c   s    -14wi$&()yD?CDtc3s33tFD**IC4HIIKzz2622HGKLt!EJJ=c!n==tDL99k,t344	 E Ms   C?+Cz[0mz[31mz[33mz[38;5;208mz[35mz[1mz[3mz[2m)resetredyelloworangepurplebolditalicdimc                 x    [         R                  R                  5       (       a  [        U    U  [        S    3$ U $ )zdReturn color/style-formatted input `s` if `sys.stdout` is interactive, e.g. connected to a terminal.re   )sysstdoutisattyPALETTE)r;   colors     r   _stylers   y   s8    
zz%.!!WW%5$677r   c                 f     [         R                  " 5       R                  $ ! [         a    U s $ f = frJ   )shutilget_terminal_sizecolumns	Exception)defaults    r   _get_terminal_widthrz      s0    '')111 s   ! 00c                       \ rS rSr% Sr\\   \S'   \\   \S'   \\\\\	   \\	   4      \S'   \
\   \S'   \\\4   \S'   S rS	 rS
\S-  4S jrSrg)LoadStateDictInfo   a2  
Mutable container for state-dict loading results and diagnostics. Each entry in this structure is mutable,
and will usually be mutated in-place during the loading pipeline.

Attributes:
    missing_keys (`set[str]`):
        Keys that are missing from the loaded checkpoints but expected in the model's architecture.
    unexpected_keys (`set[str]`):
        Keys that are found in the checkpoints, but not expected in the model's architecture.
    mismatched_keys (`set[tuple[str, tuple[int], tuple[int]]]`):
        Keys that are found in the checkpoints and are expected in the model's architecture, but with a different shape.
    error_msgs ( `list[str]`):
        Some potential error messages.
    conversion_errors (`dict[str, str]`):
        Errors happening during the on-the-fly weight conversion process.
missing_keysunexpected_keysmismatched_keys
error_msgsconversion_errorsc                 d    U R                   U R                   Vs1 s H  oS   iM	     sn-  $ s  snf )zMReturn all effective missing keys, including `missing` and `mismatched` keys.r   )r~   r   )selfr-   s     r   missing_and_mismatched(LoadStateDictInfo.missing_and_mismatched   s/      $2F2F#G2FQaD2F#GGG#Gs   -c                 `    U R                   U R                  U R                  U R                  S.$ )Nr~   r   r   r   r   )r   s    r   to_dictLoadStateDictInfo.to_dict   s0     !--#33#33//	
 	
r   r   Nc           	         [        5       n/ nSnU R                  (       a\  US[        SS5       S3[        SS5      -   -  n[        U R                  5       H#  n[        SS5      nUR	                  XES	S	/5        M%     U R
                  (       a[  US[        S
S5       S3[        SS5      -   -  n[        U R
                  5       H"  n[        SS5      nUR	                  XES	/5        M$     U R                  (       a  US[        SS5       S3[        SS5      -   -  nU R                   VVVs0 s H	  u  pgoXx4_M     n	nnn[        U	5      R                  5        HB  u  n
u  p[        SS5      nU
US[        U5       S[        U5       3/nUR	                  U5        MD     U R                  (       aq  US[        SS5       S3[        SS5      -   -  n[        U R                  5      R                  5        H*  u  pN[        SS5      nSU S3nUR	                  XEU/5        M,     [        U5      S:X  a  gSS/nUS:  a  US/-  nOUS	S	/-  n[        UUS9nUU-   nU$ s  snnnf ) z/Generate the minimal table of a loading report.z

Notes:z
- zUNEXPECTED:rh   	zbcan be ignored when loading from different task/architecture; not ok if you expect identical arch.rk   
UNEXPECTEDr=   zMISSING:rf   zsthose params were newly initialized because missing from the checkpoint. Consider training on your downstream task.MISSINGz	MISMATCH:rg   zRckpt weights were loaded, but they did not match the original empty weight shapes.MISMATCHz$Reinit due to size mismatch - ckpt: z
 vs model:zCONVERSION:ri   z$originate from the conversion scheme
CONVERSIONz

r   NKeyStatus   Details)r\   )rz   r   rs   r:   r%   r~   r   r"   r   r   r   rd   )r   term_wr[   tipsr-   statusabrX   iteratorr   
shape_ckptshape_modeldatav_detailsr\   tablereports                      r   create_loading_report'LoadStateDictInfo.create_loading_report   s   $&d6-:;2>tB  D %T%9%9:h7QB/0 ; d6*e45R86 F<  D %T%6%67	51QO, 8 d6+x89<vdfn@  D 261E1EF1EgaAA6	1EHF2A(2K2Q2Q2S..j
H5:3z?:K:VYZeVfUgh
 D! 3T !!d6-:;2>6B  D ((>(>?EEGh7!!D>Q12 H t9>(#C<	{"GBxGD'2? Gs   )IrV   )__name__
__module____qualname____firstlineno____doc__r&   r   __annotations__tupler(   r!   r    r   r   r   __static_attributes__rV   r   r   r|   r|      so    " c(XsE#Jc
:;<<S	CH~%H
;sTz ;r   r|   pretrained_model_name_or_pathignore_mismatched_sizesloading_infologgerc                 "   Uc  [         R                  " [        5      nUR                  (       aK  SR	                  UR                  5      nSU;   a  US-  n[        SU R                  R                   SU 35      eUR                  5       nUc  g[        S    U R                  R                   S[        S	    S
U S3nUR                  Xv-   5        UR                  (       a  [        S5      eU(       d  UR                  (       a  [        S5      egg)z
Log a readable report about state_dict loading issues.

This version is terminal-size aware: for very small terminals it falls back to a compact
Key | Status view so output doesn't wrap badly.
Nz
	zsize mismatchzb
	You may consider adding `ignore_mismatched_sizes=True` to `from_pretrained(...)` if appropriate.z#Error(s) in loading state_dict for z:
	rj   z LOAD REPORTre   z from: rY   zWe encountered some issues during automatic conversion of the weights. For details look at the `CONVERSION` entries of the above report!zjYou set `ignore_mismatched_sizes` to `False`, thus raising an error. For details look at the above report!)logging	getLoggerr   r   r   RuntimeError	__class__r   rq   warningr   r   )modelr   r   r   r   	error_msgr   preludes           r   log_state_dict_reportr      s     ~""8, KK 7 78	i'vI @AYAY@ZZ_`i_jkll //1F~!%//":":!;<PWHXGYY`a~`  @B  CG NN7#$ %% 
 	
 #|'C'Cx
 	
 (D"r   )
   )P   rJ   )r   reru   rn   collectionsr   r   dataclassesr   typingr   compiler   r   r   r!   r(   r   r    r:   r>   r?   rG   rd   rq   rs   rz   r|   boolLoggerr   rV   r   r   <module>r      s*    	  
 0 !  JJ./	#S #S #
'c '# '*T#s(^ *S#X *Z ::'($3 $3 $5 	 ` ` `P %),
#&,
 ",
 $	,

 NNT!,
r   