o
    eib'                  
   @   s,  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dZdedefdd	Zd/dee defddZdeee
f deee
f fddZedZdedefddZdd Zdd Zdddddddd d!Zd"d# Zd0d%d&ZeG d'd( d(Z	d1d)ed*ed+ed,e jdB fd-d.ZdS )2    N)OrderedDictdefaultdict)	dataclass)Anyz(?<=\.)(\d+)(?=\.|$)keyreturnc                 C   s   t d| S )zBReplace every dot-delimited integer with '*' to get the structure.*)	_DIGIT_RXsub)r    r   _/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/transformers/utils/loading_report.py_pattern_of   s   r   
   valuesc                 C   sR   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   cutoffr   r   r   _fmt_indices   s   r   mappingc                 C   s  d}t | tsdd | D } d}tt}|  D ]8\}}t|}t|}t|D ]\}}t	|| |kr=|| 
t  || | t| q)|| 
| qi }	| D ]`\}}
|
dd |
d }}|d}|d }td	t	|D ]<}|d	 t	|k r||d	  rtt||d	  }t	||d	  d	kr|d
| d 7 }n	||7 }n|d7 }||| 7 }qv||	|< qWt|	}|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                 S   s   i | ]}||qS r   r   .0kr   r   r   
<dictcomp>2   s    z#update_key_name.<locals>.<dictcomp>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outr   r   r   update_key_name)   s>   




r:   z\x1b\[[0-9;]*msc                 C   s   t dt| S )N )_ansi_rer
   r   )r;   r   r   r   _strip_ansiY   s   r>   c                 C   s*   t | }td|tt| }|d|  S )Nr    )r   maxr   r>   )textwidthtpadr   r   r   _pad]   s   rE   c                    s|   | rt t|g|   n|g}dd |D  ddd t| D }ddd  D } fdd| D }d	||g| S )
Nc                 S   s   g | ]}t d d |D qS )c                 s   s    | ]	}t t|V  qd S N)r   r>   )r   xr   r   r   	<genexpr>f   s    )_make_table.<locals>.<listcomp>.<genexpr>)r@   )r   colr   r   r   
<listcomp>f   s    z_make_table.<locals>.<listcomp> | c                 s       | ]
\}}t ||V  qd S rF   rE   )r   hwr   r   r   rH   g       z_make_table.<locals>.<genexpr>z-+-c                 s   s    | ]}d | V  qdS )-Nr   )r   rP   r   r   r   rH   h   s    c                    s&   g | ]}d  dd t| D qS )rL   c                 s   rM   rF   rN   )r   crP   r   r   r   rH   i   rQ   rI   )r   zip)r   rwidthsr   r   rK   i   s   & 
)r"   rT   r   )rowsheaderscolsheader_linesep_linebodyr   rV   r   _make_tablec   s   r_   z[0mz[31mz[33mz[38;5;208mz[35mz[1mz[3mz[2m)resetredyelloworangepurplebolditalicdimc                 C   s&   t j rt|  |  td  S | S )z^Return color-formatted input `s` if `sys.stdout` is interactive, e.g. connected to a terminal.r`   )sysstdoutisattyPALETTE)r;   colorr   r   r   _colory   s   
rm   P   c                 C   s$   zt  jW S  ty   |  Y S w rF   )shutilget_terminal_sizecolumns	Exception)defaultr   r   r   _get_terminal_width   s
   rt   c                   @   s   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d Zd	d
 ZdedB fd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                 C   s   | j dd | jD B S )zMReturn all effective missing keys, including `missing` and `mismatched` keys.c                 S   s   h | ]}|d  qS )r   r   r   r   r   r   	<setcomp>   s    z;LoadStateDictInfo.missing_and_mismatched.<locals>.<setcomp>)rv   rx   selfr   r   r   missing_and_mismatched   s   z(LoadStateDictInfo.missing_and_mismatchedc                 C   s   | j | j| j| jdS )Nrv   rw   rx   ry   r   r|   r   r   r   to_dict   s
   zLoadStateDictInfo.to_dictr   Nc                 C   s  t  }g }d}| jr/|dtddtd   d7 }t| jD ]}tdd}|||ddg q| jrV|dtddtd   d	7 }t| jD ]}tdd}|||dg qF| jr|dtd
dtd   d7 }dd | jD }t| D ]\}\}}	td
d}||dt	| dt	|	 g}
||
 qv| j
r|dtddtd   d7 }t| j
 D ]\}}tdd}d| d}||||g qt|dkrdS ddg}|dkr|dg7 }n|ddg7 }t||d}dtd  d| td  }|| }|S )z/Generate the minimal table of a loading report.r<   z
- 
UNEXPECTEDrc   rf   zd	:can be ignored when loading from different task/architecture; not ok if you expect identical arch.MISSINGra   zu	:those params were newly initialized because missing from the checkpoint. Consider training on your downstream task.MISMATCHrb   zT	:ckpt weights were loaded, but they did not match the original empty weight shapes.c                 S   s   i | ]
\}}}|||fqS r   r   )r   abrS   r   r   r   r      s    z;LoadStateDictInfo.create_loading_report.<locals>.<dictcomp>z$Reinit due to size mismatch - ckpt: z
 vs model:
CONVERSIONrd   z&	:originate from the conversion schemez

r   NKeyStatus   Details)rZ   zNotes:r`   )rt   rw   rm   rk   r:   r&   rv   rx   r#   r   rz   r   r_   )r}   term_wrY   tipsr   statusiteratorr   
shape_ckptshape_modeldatav_detailsrZ   tablereportr   r   r   create_loading_report   sZ   



z'LoadStateDictInfo.create_loading_report)__name__
__module____qualname____doc__r'   r   __annotations__tupler)   r"   r!   r~   r   r   r   r   r   r   ru      s   
 	ru   pretrained_model_name_or_pathignore_mismatched_sizesloading_infologgerc                 C   s   |du r	t t}|jr&d|j}d|v r|d7 }td| jj d| | }|du r0dS td  | jj dtd	  d
| d}|	||  |j
rQtd|sZ|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:
	re   z LOAD REPORTr`   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   ry   r   RuntimeError	__class__r   rk   warningrz   rx   )modelr   r   r   r   	error_msgr   preluder   r   r   log_state_dict_report   s.   
&
r   )r   )rn   rF   )r   rero   rh   collectionsr   r   dataclassesr   typingr   compiler	   r   r   r"   r)   r   r!   r:   r=   r>   rE   r_   rk   rm   rt   ru   boolLoggerr   r   r   r   r   <module>   sL   
"

-
h