
    7ib'              
       t   d dl Z 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
  ej                  d      ZdedefdZd$d	ee   defd
Zdeee
f   deee
f   fdZ ej                  d      ZdedefdZd Zd ZdddddddddZd Zd%dZe G d d             Z	 d&ded ed!ed"e j:                  dz  fd#Zy)'    N)OrderedDictdefaultdict)	dataclass)Anyz(?<=\.)(\d+)(?=\.|$)keyreturnc                 .    t         j                  d|       S )zBReplace every dot-delimited integer with '*' to get the structure.*)	_DIGIT_RXsub)r   s    ]/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/transformers/utils/loading_report.py_pattern_ofr      s    ==c""    valuesc                     t        |       dk(  rt        | d         S t        |       } t        |       |kD  r| d    d| d    S dj                  t	        t        |             S )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           	         d}t        | t              s| D ci c]  }|| } }d}t        t              }| j	                         D ]  \  }}t
        j                  |      }t        |      }t        |      D ]R  \  }}	t        ||         |k  r||   j                  t                      ||   |   j                  t        |	             T ||   j                  |        i }
|j	                         D ]  \  }}|dd |d   }}|j                  d      }|d   }t        dt        |            D ]h  }|dz
  t        |      k  rH||dz
     r@t!        t#        ||dz
                 }t        ||dz
           dkD  r|d|z   d	z   z  }n||z  }n|dz  }|||   z  }j ||
|<    t%        |
      }|r|j'                         S |S c c}w )
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$!()A1a4))4?4EFMMO !S  %3dO 	(DAq6$< A%t##CE*4LOA'	( 	tC ! I f3BKc

3aq#e*% 		A1us4y T!a%[%fT!a%[&9:tAE{#a'S6\C//EVOEU1XE		 	%  i
 CxxzJC *s   
F>z\x1b\[[0-9;]*msc                 @    t         j                  dt        |             S )N )_ansi_rer   r   )r;   s    r   _strip_ansir?   Y   s    <<CF##r   c           	      j    t        |       }t        d|t        t        |            z
        }|d|z  z   S )Nr    )r   maxr   r?   )textwidthtpads       r   _padrG   ]   s3    D	A
a[^,,
-CsSy=r   c           
         | rt        t        |g| z          n|g}|D cg c]  }t        d |D               }}dj                  d t        ||      D              }dj                  d |D              }| D cg c]$  }dj                  d t        ||      D              & }}dj                  ||g|z         S c c}w c c}w )Nc              3   D   K   | ]  }t        t        |              y wN)r   r?   ).0xs     r   	<genexpr>z_make_table.<locals>.<genexpr>f   s     3!#k!n%3s    z | c              3   :   K   | ]  \  }}t        ||        y wrJ   rG   )rK   hws      r   rM   z_make_table.<locals>.<genexpr>g   s     IDAqT!QZI   z-+-c              3   &   K   | ]	  }d |z    yw)-N )rK   rQ   s     r   rM   z_make_table.<locals>.<genexpr>h   s     2a#'2s   c              3   :   K   | ]  \  }}t        ||        y wrJ   rO   )rK   crQ   s      r   rM   z_make_table.<locals>.<genexpr>i   s     =datAqz=rR   
)r!   ziprB   r   )	rowsheaderscolscolwidthsheader_linesep_linerbodys	            r   _make_tablerc   c   s    -14wi$&()yD?CDc3s33DFD**IC4HIIKzz2622HGKL!EJJ=c!Vn==LDL99k8,t344	 E Ms   B98)B>z[0mz[31mz[33mz[38;5;208mz[35mz[1mz[3mz[2m)resetredyelloworangepurplebolditalicdimc                 n    t         j                  j                         rt        |    |  t        d    S | S )z^Return color-formatted input `s` if `sys.stdout` is interactive, e.g. connected to a terminal.rd   )sysstdoutisattyPALETTE)r;   colors     r   _colorrr   y   s5    
zz%.!!WW%5$677r   c                 b    	 t        j                         j                  S # t        $ r | cY S w xY wrJ   )shutilget_terminal_sizecolumns	Exception)defaults    r   _get_terminal_widthry      s0    '')111 s     ..c                       e Zd ZU dZee   ed<   ee   ed<   eeeee   ee   f      ed<   e	e   ed<   e
eef   ed<   d Zd Zd	ed
z  fdZy
)LoadStateDictInfoaj  
    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                 `    | j                   | j                  D ch c]  }|d   	 c}z  S c c}w )zMReturn all effective missing keys, including `missing` and `mismatched` keys.r   )r|   r~   )selfr-   s     r   missing_and_mismatchedz(LoadStateDictInfo.missing_and_mismatched   s+      $2F2F#GQAaD#GGG#Gs   +c                 `    | j                   | j                  | j                  | j                  dS )Nr|   r}   r~   r   r   )r   s    r   to_dictzLoadStateDictInfo.to_dict   s0     !--#33#33//	
 	
r   r   Nc           	         t               }g }d}| j                  rX|dt        dd      t        d   z    dz  }t	        | j                        D ]#  }t        dd      }|j                  ||ddg       % | j                  rW|dt        dd      t        d   z    d	z  }t	        | j                        D ]"  }t        dd      }|j                  ||dg       $ | j                  r|dt        d
d      t        d   z    dz  }| j                  D ci c]  \  }}}|||f }	}}}t	        |	      j                         D ]B  \  }
\  }}t        d
d      }|
|dt        |       dt        |       g}|j                  |       D | j                  rn|dt        dd      t        d   z    dz  }t	        | j                        j                         D ]+  \  }}t        dd      }d| d}|j                  |||g       - t        |      dk(  ryddg}|dkD  r|dgz  }n|ddgz  }t        ||      }dt        d    d| t        d    }||z   }|S c c}}}w )z/Generate the minimal table of a loading report.r=   z
- 
UNEXPECTEDrg   rj   zd	:can be ignored when loading from different task/architecture; not ok if you expect identical arch.MISSINGre   zu	:those params were newly initialized because missing from the checkpoint. Consider training on your downstream task.MISMATCHrf   zT	:ckpt weights were loaded, but they did not match the original empty weight shapes.z$Reinit due to size mismatch - ckpt: z
 vs model:
CONVERSIONrh   z&	:originate from the conversion schemez

r   NKeyStatus   Details)r[   zNotes:rd   )ry   r}   rr   rp   r:   r%   r|   r~   r"   r   r   r   rc   )r   term_wrZ   tipsr-   statusabrW   iteratorr   
shape_ckptshape_modeldatav_detailsr[   tablereports                      r   create_loading_reportz'LoadStateDictInfo.create_loading_report   s   $&vlH58IIJ KJ JD %T%9%9: 1h7QB/01 vi/'(2CCD ER RD %T%6%67 -	51QO,- vj(3gh6GGH I4 4D 261E1EFFgaAAq6	FHF2A(2K2Q2Q2S "..j+
H5:3z?:K:VYZeVfUgh
 D!" !!d6,9GH<MMNNuvvD'(>(>?EEG 31h7!!D>Q123 t9>(#C<	{"GBxGD'2gh'(tfWW5E4FG= Gs   I)__name__
__module____qualname____doc__r&   r   __annotations__tupler(   r!   r    r   r   r   rU   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                    |t        j                  t              }|j                  rIdj	                  |j                        }d|v r|dz  }t        d| j                  j                   d|       |j                         }|yt        d    | j                  j                   dt        d	    d
| d}|j                  ||z          |j                  rt        d      |s|j                  rt        d      yy)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:
	ri   z LOAD REPORTrd   z from: rX   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   rp   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 NN7V#$ %% 
 	
 #|'C'Cx
 	
 (D"r   )
   )P   rJ   )r   rert   rm   collectionsr   r   dataclassesr   typingr   compiler   r   r   r!   r(   r   r    r:   r>   r?   rG   rc   rp   rr   ry   r{   boolLoggerr   rU   r   r   <module>r      s.    	  
 0 !  BJJ./	#S #S #
'c '# '*T#s(^ *S#X *Z 2::'($3 $3 $5 	 ` ` `P %),
#&,
 ",
 $	,

 NNT!,
r   