
    }-j1'              
          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dS ))    N)OrderedDictdefaultdict)	dataclass)Anyz(?<=\.)(\d+)(?=\.|$)keyreturnc                 8    t                               d|           S )zBReplace every dot-delimited integer with '*' to get the structure.*)	_DIGIT_RXsub)r   s    a/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/transformers/utils/loading_report.py_pattern_ofr      s    ==c"""    
   valuesc                    t          |           dk    rt          | d                   S t          |           } t          |           |k    r| d          d| d          S d                    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      sw    
6{{a6!9~~F^^F
6{{V),,r
,,,99Sf%%&&&r   mappingc           	      l   d}t          | t                    sd | D             } d}t          t                    }|                                 D ]\  }}t
                              |          }t          |          }t          |          D ]s\  }}t          ||                   |k    r'||         
                    t                                 ||         |                             t          |                     t||         
                    |           i }	|                                D ]\  }}
|
dd         |
d         }}|                    d          }|d         }t          dt          |                    D ]}|dz
  t          |          k     r^||dz
           rSt!          t#          ||dz
                               }t          ||dz
                     dk    r|d	|z   d
z   z  }n||z  }n|dz  }|||         z  }||	|<   t%          |	          }|r|                                S |S )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}.
    Fc                     i | ]}||S  r   .0ks     r   
<dictcomp>z#update_key_name.<locals>.<dictcomp>2   s    )))A1a)))r   TNr   r
   r   r   {})
isinstancedictr   listitemsr   findallr   	enumerater   appendsetaddintsplitranger   r   r   keys)r   not_mappingbucketr   valdigspattid	out_itemsr   setspartsfinalinsertouts                   r   update_key_namer@   )   s;    Kgt$$ ))))).9$.?.?FMMOO ! !S  %%3dOO 	( 	(DAq6$<  A%%t##CEE***4LOA''''tC    I  f3B3Kc

3aq#e**%% 		 		A1us4yy  T!a%[ %fT!a%[&9&9::tAE{##a''S6\C//EEVOEEU1XEE	%
i
 
 C xxzzJr   z\x1b\[[0-9;]*msc                 R    t                               dt          |                     S )N )_ansi_rer   r   )rA   s    r   _strip_ansirE   Y   s    <<CFF###r   c           	          t          |           }t          d|t          t          |                    z
            }|d|z  z   S )Nr    )r   maxr   rE   )textwidthtpads       r   _padrM   ]   s=    D		A
a[^^,,,
-
-CsSy=r   c                 B   | rt          t          |g| z              n|g}d |D             d                    d t          |          D                       }d                    d D                       }fd| D             }d                    ||g|z             S )Nc                 @    g | ]}t          d  |D                       S )c              3   N   K   | ] }t          t          |                    V  !d S N)r   rE   )r!   xs     r   	<genexpr>z)_make_table.<locals>.<listcomp>.<genexpr>f   s0      33!#k!nn%%333333r   )rH   )r!   cols     r   
<listcomp>z_make_table.<locals>.<listcomp>f   s/    DDDc33s33333DDDr    | c              3   <   K   | ]\  }}t          ||          V  d S rQ   rM   )r!   hws      r   rS   z_make_table.<locals>.<genexpr>g   s.      IIDAqT!QZZIIIIIIr   z-+-c              3       K   | ]	}d |z  V  
dS )-Nr   )r!   rZ   s     r   rS   z_make_table.<locals>.<genexpr>h   s&      22a#'222222r   c           	      j    g | ]/}d                      d t          |          D                       0S )rV   c              3   <   K   | ]\  }}t          ||          V  d S rQ   rX   )r!   crZ   s      r   rS   z)_make_table.<locals>.<listcomp>.<genexpr>i   s.      ==datAqzz======r   )r   zip)r!   rwidthss     r   rU   z_make_table.<locals>.<listcomp>i   s<    LLL!EJJ==c!Vnn=====LLLr   
)r(   r`   r   )rowsheaderscolsheader_linesep_linebodyrb   s         @r   _make_tablerj   c   s    -1@4wi$&()))yDDDtDDDF**IIC4H4HIIIIIKzz22622222HLLLLtLLLD99k8,t3444r   z[0mz[31mz[33mz[38;5;208mz[35mz[1mz[3mz[2m)resetredyelloworangepurplebolditalicdimc                 ~    t           j                                        rt          |          |  t          d          S | S )zdReturn color/style-formatted input `s` if `sys.stdout` is interactive, e.g. connected to a terminal.rk   )sysstdoutisattyPALETTE)rA   colors     r   _stylery   y   s<    
z %.7!7WW%5777r   P   c                 X    	 t          j                    j        S # t          $ r | cY S w xY wrQ   )shutilget_terminal_sizecolumns	Exception)defaults    r   _get_terminal_widthr      s=    '))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d
S )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                 4    | j         d | j        D             z  S )zMReturn all effective missing keys, including `missing` and `mismatched` keys.c                     h | ]
}|d          S )r   r   r    s     r   	<setcomp>z;LoadStateDictInfo.missing_and_mismatched.<locals>.<setcomp>   s    #G#G#GQAaD#G#G#Gr   )r   r   selfs    r   missing_and_mismatchedz(LoadStateDictInfo.missing_and_mismatched   s"     #G#G$2F#G#G#GGGr   c                 8    | j         | j        | j        | j        dS )Nr   r   r   r   r   r   s    r   to_dictzLoadStateDictInfo.to_dict   s*     !-#3#3/	
 
 	
r   r   Nc           	         t                      }g }d}| j        rh|dt          dd           dt          dd          z   z  }t          | j                  D ]+}t          dd          }|                    ||d	d	g           ,| j        rg|dt          d
d           dt          dd          z   z  }t          | j                  D ]*}t          dd          }|                    ||d	g           +| j        r|dt          dd           dt          dd          z   z  }d | j        D             }t          |                                          D ]R\  }\  }}	t          dd          }||dt          |           dt          |	           g}
|                    |
           S| j	        r|dt          dd           dt          dd          z   z  }t          | j	                                                  D ]3\  }}t          dd          }d| d}|                    |||g           4t          |          dk    rdS ddg}|dk    r|dgz  }n|d	d	gz  }t          ||           }||z   }|S )!z/Generate the minimal table of a loading report.z

Notes:z
- zUNEXPECTED:rn   	zbcan be ignored when loading from different task/architecture; not ok if you expect identical arch.rq   
UNEXPECTEDrC   zMISSING:rl   zsthose params were newly initialized because missing from the checkpoint. Consider training on your downstream task.MISSINGz	MISMATCH:rm   zRckpt weights were loaded, but they did not match the original empty weight shapes.c                      i | ]\  }}}|||fS r   r   )r!   abr_   s       r   r#   z;LoadStateDictInfo.create_loading_report.<locals>.<dictcomp>   s$    FFFgaAAq6FFFr   MISMATCHz$Reinit due to size mismatch - ckpt: z
 vs model:zCONVERSION:ro   z$originate from the conversion scheme
CONVERSIONz

r   NKeyStatus   Details)re   )r   r   ry   r@   r,   r   r   r)   r   r   r   rj   )r   term_wrd   tipsr"   statusiteratorr   
shape_ckptshape_modeldatav_detailsre   tablereports                   r   create_loading_reportz'LoadStateDictInfo.create_loading_report   s#   $&& 	1>6-::>>>tB B  D %T%9:: 1 1h77QB/0000 	-86*e448886 F< <  D %T%677 - -	511QO,,,, 	"<6+x88<<<vdfn@ @  D GF1EFFFH2A(2K2K2Q2Q2S2S " "..j+
H55h3z??hhVYZeVfVfhh
 D!!!!! 	3>6-::>>>6B B  D ((>??EEGG 3 31h77)!>>>Q12222 t99>>4(#C<<	{"GGBxGD'222r   )__name__
__module____qualname____doc__r-   r   __annotations__tupler/   r(   r'   r   r   r   r   r   r   r   r      s          " c(XsE#Jc
:;<<<<S	CH~%%%H H H
 
 
;sTz ; ; ; ; ; ;r   r   pretrained_model_name_or_pathignore_mismatched_sizesloading_infologgerc                    |t          j        t                    }|j        rBd                    |j                  }d|v r|dz  }t          d| j        j         d|           |                                }|dS t          d          | j        j         dt          d	          d
| d}|	                    ||z              |j
        rt          d          |s|j        rt          d          dS dS )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:
	rp   z LOAD REPORTrk   z from: rc   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   rw   warningr   r   )modelr   r   r   r   	error_msgr   preludes           r   log_state_dict_reportr      s\    ~"8,,  mKK 788	i''vI kAYkk`ikklll //11F~  C%/":  C  CPWHX  C  Ca~  C  C  CG NN7V#$$$ % 
 
 
 	
 # 
|'C 
x
 
 	

 
 
 
r   )r   )rz   rQ   )r   rer|   rt   collectionsr   r   dataclassesr   typingr   compiler   r   r   r(   r/   r   r'   r@   rD   rE   rM   rj   rw   ry   r   r   boolLoggerr   r   r   r   <module>r      s*    				  



 0 0 0 0 0 0 0 0 ! ! ! ! ! !       BJ.//	#S #S # # # #
' 'c '# ' ' ' '*T#s(^ *S#X * * * *Z 2:'(($3 $3 $ $ $ $  5 5 5 	 	      ` ` ` ` ` ` ` `P %),
 ,
#&,
 ",
 $	,

 NT!,
 ,
 ,
 ,
 ,
 ,
r   