§
    9²¸jB  ã            	       ó   — 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
 ddlmZ ddlmZmZmZ  e¦   «         rd dlZd dlmZ  e¦   «         rd dlZ ej        e¦  «        Zd	„ Z ej        d
¦  «        Zdedefd„Zd„ Z	 d"dedz  dededz  fd„Zd"dedz  dededz  fd„Z dej!        fd„Z"d„ Z# ej        d¦  «        Z$d„ Z%d„ Z&dedz  fd„Z'	 	 	 d#dededefd„Z( ed¬ ¦  «        e	 	 	 d$dedz  dedefd!„¦   «         ¦   «         Z)dS )%é    N)ÚcontextmanagerÚredirect_stdout)ÚStringIOé   ©Ú_get_torch_distributed_rank)Úlogging)Úis_torch_availableÚis_torch_distributed_availableÚrequires)Ú	save_filec                  ó&   — t          ¦   «         dk    S )z7Return True if rank=0 or we aren't running distributed.r   r   © ó    úr/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/model_debugging_utils.pyÚ_is_rank_zeror   (   s   € å&Ñ(Ô(¨AÒ-Ð-r   zobject at 0x[0-9A-Fa-f]+Úx_strÚreturnc                 ó8   — t                                d| ¦  «        S )z™
    Replace memory addresses in an object's repr with a stable placeholder
    so that beautiful JSON diffs won't be ruined by ephemeral addresses.
    zobject at 0xXXXXXXXX)ÚMEMORY_ADDRESS_REGEXÚsub)r   s    r   Ú_sanitize_repr_for_diffr   0   s   € õ
  ×#Ò#Ð$:¸EÑBÔBÐBr   c                 óP   — t          ¦   «         rdt          | j        ¦  «        › �S dS )z@Return a stable string representation for a DTensor-like object.zDTensor (rank0) -> zDTensor(non-rank0))r   ÚreprÚ_local_tensor)Úxs    r   Ú_dtensor_reprr   8   s,   € å�„ð =Ø<¥T¨!¬/Ñ%:Ô%:Ð<Ð<Ð<ØÐr   TÚ
debug_pathÚuse_reprÚpath_to_valuec                 óØ  — t          j        d¬¦  «         |rt          | ¦  «        }n¤|rŒ|                     d¦  «        s|dz  }|r t          j                             ||¦  «        n|}t          d|                      ¦   «          	                    ¦   «          
                    ¦   «         i|¦  «         d|› �}nt          d|›d|›d�¦  «        ‚t          | j        ¦  «        t          | j        ¦  «        |d	œ}| j        t           j        t           j        t           j        hv rÊ|                     t'          t          |                      ¦   «         ¦  «        ¦  «        t'          t          |                      ¦   «         ¦  «        ¦  «        t'          t          |                      ¦   «         ¦  «        ¦  «        t'          t          |                      ¦   «         ¦  «        ¦  «        d
œ¦  «         |S )a‘  
    Converts Tensors and DTensors to a JSON-serializable dictionary representation.

    Args:
        value: Any Python object, often including torch Tensors, lists, dicts, etc.
        debug_path (`str`, *optional*, defaults to `None`): Directory to dump debug JSON and SafeTensors files.
        use_repr (bool, *optional*, defaults to `True`): Whether to save a `repr()`-ized version of the tensor as the
            `value` property in the associated FULL_TENSORS.json file, or to store the full tensors in separate
            SafeTensors file and store the relative path to that file in the `value` property in the dictionary.
        path_to_value (`str`, *optional*, defaults to `None`): The file name for the SafeTensors file holding the full
            tensor value if `use_repr=False`.

    Returns:
        A nested Python structure (list, dict, or sanitized string) that is safe to json.dump.
    T)Úsci_modez.safetensorsÚdataz./z	use_repr=z and path_to_value=z cannot both be falsy.)ÚshapeÚdtypeÚvalue)ÚmeanÚstdÚminÚmax)ÚtorchÚset_printoptionsÚ_repr_to_listÚendswithÚosÚpathÚjoinr   Ú
contiguousÚdetachÚcpuÚ
ValueErrorr   r$   r%   Úfloat16Úfloat32Úbfloat16Úupdater   r'   r(   r)   r*   )r&   r   r   r    Ú	value_outÚfilepathÚouts          r   Ú_serialize_tensor_like_ior=   ?   s¼  € õ$ 
Ô DÐ)Ñ)Ô)Ð)àð 
TÝ! %Ñ(Ô(ˆ	ˆ	Ø	ð TØ×%Ò% nÑ5Ô5ð 	,Ø˜^Ñ+ˆMà>HÐ[•2”7—<’< 
¨MÑ:Ô:Ð:ÈmˆÝ�6˜5×+Ò+Ñ-Ô-×4Ò4Ñ6Ô6×:Ò:Ñ<Ô<Ð=¸xÑHÔHÐHØ(˜Ð(Ð(ˆ	ˆ	åÐR˜HÐRÐR¨MÐRÐRÐRÑSÔSÐSõ �e”kÑ"Ô"Ý�e”kÑ"Ô"Øðð €Cð
 „{•u”}¥e¤mµU´^ÐDÐDÐDØ�
Š
å/µ°U·Z²Z±\´\Ñ0BÔ0BÑCÔCÝ.­t°E·I²I±K´KÑ/@Ô/@ÑAÔAÝ.­t°E·I²I±K´KÑ/@Ô/@ÑAÔAÝ.­t°E·I²I±K´KÑ/@Ô/@ÑAÔAð	ð ñ	
ô 	
ð 	
ð €Jr   c                 óÊ  ‡‡‡— t          | t          t          f¦  «        rˆˆˆfd„t          | ¦  «        D ¦   «         S t          | t          ¦  «        r"ˆˆˆfd„|                      ¦   «         D ¦   «         S t          | d¦  «        rt          | j        ‰‰‰¬¦  «        S t          | t          j
        ¦  «        rt          | ‰‰‰¬¦  «        S t          t          | ¦  «        ¦  «        S )a’  
    Recursively build a JSON-serializable Python structure from `value`.
    Tensors and DTensors become either sanitized repr strings, or are saved to disk as SafeTensors files and their
    relative paths are recorded in the returned Python structure.
    Lists/tuples/dicts are recursed into.
    All memory addresses are replaced with a stable placeholder.

    Args:
        value: Any Python object, often including torch Tensors, lists, dicts, etc.
        debug_path (`str`, *optional*, defaults to `None`): Directory to dump debug JSON and SafeTensors files.
        use_repr (bool, *optional*, defaults to `True`): Whether to save a `repr()`-ized version of the tensors as the
            `value` property in the associated FULL_TENSORS.json file, or to store full tensors in separate SafeTensors
            files and store the relative path to that file in the `value` property.
        path_to_value (`str`, *optional*, defaults to `None`): The file name for the SafeTensors file holding the full
            tensor value if `use_repr=False`.

    Returns:
        A nested Python structure (list, dict, or sanitized string) that is safe to json.dump.
    c           
      óF   •— g | ]\  }}t          |‰‰‰› d |› �¬¦  «        ‘ŒS ©Ú_©r   r   r    ©Ú_serialize_io)Ú.0ÚiÚvr   r    r   s      €€€r   ú
<listcomp>z!_serialize_io.<locals>.<listcomp>…   sN   ø€ ð 
ð 
ð 
á��1õ ˜!¨
¸XÐXeÐUkÐUkÐhiÐUkÐUkÐlÑlÔlð
ð 
ð 
r   c                 óH   •— i | ]\  }}|t          |‰‰‰› d |› �¬¦  «        “ŒS r@   rC   )rE   ÚkrG   r   r    r   s      €€€r   ú
<dictcomp>z!_serialize_io.<locals>.<dictcomp>‹   sP   ø€ ð 
ð 
ð 
á��1ð �}˜Q¨:ÀÐ[hÐXnÐXnÐklÐXnÐXnÐoÑoÔoð
ð 
ð 
r   r   rB   )Ú
isinstanceÚlistÚtupleÚ	enumerateÚdictÚitemsÚhasattrr=   r   r+   ÚTensorr   r   )r&   r   r   r    s    ```r   rD   rD   p   s'  øøø€ õ( �%�$¥˜Ñ'Ô'ð 
ð
ð 
ð 
ð 
ð 
ð 
å! %Ñ(Ô(ð
ñ 
ô 
ð 	
õ
 �%�ÑÔð 
ð
ð 
ð 
ð 
ð 
ð 
àŸš™œð
ñ 
ô 
ð 	
õ
 ˆu�oÑ&Ô&ð 
Ý(ØÔ¨JÀÐYfð
ñ 
ô 
ð 	
õ �%�œÑ&Ô&ð wÝ(¨¸:ÐPXÐhuÐvÑvÔvÐvå"¥4¨¡;¤;Ñ/Ô/Ð/r   r&   c                 óP  — t          j        dd¬¦  «         t          ¦   «         5 }t          |¦  «        5  t	          | ¦  «         |                     ¦   «         }ddd¦  «         n# 1 swxY w Y   ddd¦  «         n# 1 swxY w Y   t          |¦  «                             ¦   «         S )zã
    Converts a tensor into a sanitized multi-line string representation.

    Args:
        value (`torch.Tensor`): The tensor to represent.

    Returns:
        `list[str]`: List of string lines representing the tensor.
    Téx   )r"   Ú	linewidthN)r+   r,   r   r   ÚprintÚgetvaluer   Ú
splitlines)r&   ÚbufÚraws      r   r-   r-   ›   s  € õ 
Ô D°CÐ8Ñ8Ô8Ð8Ý	‰Œð �s�O¨CÑ0Ô0ð ð Ýˆe‰ŒˆØ�lŠl‰nŒnˆðð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð ð ð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð õ # 3Ñ'Ô'×2Ò2Ñ4Ô4Ð4s4   ¥A<µ$A%ÁA<Á%A)	Á)A<Á,A)	Á-A<Á<B ÂB c                 ó”   — |                       d¦  «        r0|                      dd ¦  «         | d         D ]}t          |¦  «         Œd S d S )NÚchildrenÚoutputs)ÚgetÚpopÚprune_outputs_if_children)ÚnodeÚchilds     r   ra   ra   ¬   se   € ð ‡x‚x�
ÑÔð -Ø�Š�˜DÑ!Ô!Ð!Ø˜*Ô%ð 	-ð 	-ˆEÝ% eÑ,Ô,Ð,Ð,ð-ð -ð	-ð 	-r   z(.*)\.(\d+)$c                 óþ   ‡— t                                |                      dd¦  «        ¦  «        }|r|                      d¦  «        sdS |                     d¦  «        Št	          ˆfd„| d         D ¦   «         ¦  «        S )zÇ
    Checks whether a node represents a layer block with submodules.

    Args:
        node (`dict`): A node from the call tree.

    Returns:
        `bool`: Whether the node is a layer block.
    Úmodule_pathÚ r]   Fé   c              3   óP   •K  — | ] }d ‰› d �|                      dd¦  «        v V — Œ!dS )ú.re   rf   N©r_   )rE   rc   Únumbers     €r   ú	<genexpr>z!is_layer_block.<locals>.<genexpr>Æ   s>   øè è € Ð[Ð[Àˆ}�6ˆ}ˆ}ˆ} §	¢	¨-¸Ñ <Ô <Ð<Ð[Ð[Ð[Ð[Ð[Ð[r   )ÚLAYER_SUFFIX_REÚmatchr_   ÚgroupÚany)rb   rn   rk   s     @r   Úis_layer_blockrq   ¸   s}   ø€ õ ×!Ò! $§(¢(¨=¸"Ñ"=Ô"=Ñ>Ô>€EØð ˜Ÿš Ñ,Ô,ð ØˆuØ�[Š[˜‰^Œ^€FÝÐ[Ð[Ð[Ð[È$ÈzÔJZÐ[Ñ[Ô[Ñ[Ô[Ð[r   c                 ó>  ‡— |                       d¦  «        sdS d„ t          | d         ¦  «        D ¦   «         }t          |¦  «        dk    r8d„ |dd…         D ¦   «         Šˆfd„t          | d         ¦  «        D ¦   «         | d<   | d         D ]}t          |¦  «         ŒdS )	zø
    Recursively removes intermediate layers from the tree to improve readability.
    Keeps at least the first and last layers if many consecutive layers are present.

    Args:
        node (`dict`): The root or subnode to prune recursively.
    r]   Nc                 ó:   — g | ]\  }}t          |¦  «        ¯||f‘ŒS r   )rq   )rE   rF   rc   s      r   rH   z-prune_intermediate_layers.<locals>.<listcomp>Ó   s.   € ÐdÐdÐd¡8 1 eÍnÐ]bÑNcÔNcÐd�Q˜�JÐdÐdÐdr   rg   c                 ó   — g | ]\  }}|‘ŒS r   r   )rE   rF   rA   s      r   rH   z-prune_intermediate_layers.<locals>.<listcomp>Ö   s   € Ð6Ð6Ð6™4˜1˜a�QÐ6Ð6Ð6r   r   éÿÿÿÿc                 ó"   •— g | ]\  }}|‰v¯	|‘ŒS r   r   )rE   rF   rc   Ú	to_removes      €r   rH   z-prune_intermediate_layers.<locals>.<listcomp>×   s)   ø€ ÐdÐdÐd¡h a¨ÐQRÐZcÐQcÐQc˜EÐQcÐQcÐQcr   )r_   rO   ÚlenÚprune_intermediate_layers)rb   Úlayer_blocksrc   rw   s      @r   ry   ry   É   sÇ   ø€ ð �8Š8�JÑÔð ØˆØdÐd­y¸¸jÔ9IÑ/JÔ/JÐdÑdÔd€Lå
ˆ<ÑÔ˜1ÒÐØ6Ð6 <°°"°Ô#5Ð6Ñ6Ô6ˆ	ØdÐdÐdÐdµ)¸DÀÔ<LÑ2MÔ2MÐdÑdÔdˆˆZÑà�jÔ!ð )ð )ˆÝ! %Ñ(Ô(Ð(Ð(ð)ð )r   c                 óâ  ‡— | rf	 t          j        | d¬¦  «         t           j                             | |j        dz   ¦  «        }n0# t
          $ r}t          d| › d�¦  «        |‚d }~ww xY w|j        dz   }t                               d|› d�¦  «         |dz   }|d	z   }t          |j
        ¦  «         t          |d
¦  «        5 }t          j        |j
        |d¬¦  «         d d d ¦  «         n# 1 swxY w Y   ˆfd„Št          j        t          j        |j
        ¦  «        ¦  «        } ‰|¦  «         t          |d
¦  «        5 }t          j        ||d¬¦  «         d d d ¦  «         d S # 1 swxY w Y   d S )NT©Úexist_okÚ_debug_treeú"Unexpected or existing debug_path=ri   zWriting model trace at z.jsonz_FULL_TENSORS.jsonz_SUMMARY.jsonÚwrg   )Úindentc                 óØ   •‡— ˆfd„Š ‰|                       di ¦  «        ¦  «          ‰|                       di ¦  «        ¦  «         |                       dg ¦  «        D ]} ‰|¦  «         Œd S )Nc                 óô   •— t          | t          ¦  «        r:|                      dd ¦  «         |                      ¦   «         D ]} ‰|¦  «         Œd S t          | t          ¦  «        r| D ]} ‰|¦  «         Œd S d S )Nr&   )rL   rP   r`   ÚvaluesrM   )ÚvalrG   ÚitemÚcleans      €r   r‡   z:log_model_debug_trace.<locals>.strip_values.<locals>.cleanò   s�   ø€ Ý˜#�tÑ$Ô$ð  Ø—’˜ Ñ&Ô&Ð&ØŸš™œð ð �AØ�E˜!‘H”H�H�Hðð å˜C¥Ñ&Ô&ð  Øð  ð  �DØ�E˜$‘K”K�K�Kð ð  ð ð  r   Úinputsr^   r]   rj   )rb   rc   r‡   Ústrip_valuess     @€r   r‰   z+log_model_debug_trace.<locals>.strip_valuesñ   s•   øø€ ð	 ð 	 ð 	 ð 	 ð 	 ð 	ˆˆd�hŠh�x Ñ$Ô$Ñ%Ô%Ð%Øˆˆd�hŠh�y "Ñ%Ô%Ñ&Ô&Ð&à—X’X˜j¨"Ñ-Ô-ð 	 ð 	 ˆEØˆL˜ÑÔÐÐð	 ð 	 r   )r/   Úmakedirsr0   r1   Ú_debugger_module_dump_nameÚ	Exceptionr5   ÚloggerÚinfora   Ú
_call_treeÚopenÚjsonÚdumpÚloadsÚdumps)	r   ÚmodelÚbaseÚeÚ	full_pathÚsummary_pathÚfÚ	tree_copyr‰   s	           @r   Úlog_model_debug_tracerœ   Ý   s$  ø€ Øð @ð	XÝŒK˜
¨TÐ2Ñ2Ô2Ð2Ý”7—<’< 
¨EÔ,LÈ}Ñ,\Ñ]Ô]ˆDˆDøÝð 	Xð 	Xð 	XÝÐOÀ*ÐOÐOÐOÑPÔPÐVWÐWøøøøð	Xøøøð Ô/°-Ñ?ˆå
‡K‚KÐ5¨$Ð5Ð5Ð5Ñ6Ô6Ð6ØÐ+Ñ+€IØ˜/Ñ)€Lå˜eÔ.Ñ/Ô/Ð/å	ˆi˜Ñ	Ô	ð 1 ÝŒ	�%Ô" A¨aÐ0Ñ0Ô0Ð0ð1ð 1ð 1ñ 1ô 1ð 1ð 1ð 1ð 1ð 1ð 1øøøð 1ð 1ð 1ð 1ð ð  ð  ð  ð  õ  ”
�4œ: eÔ&6Ñ7Ô7Ñ8Ô8€IØ€L�ÑÔÐå	ˆl˜CÑ	 Ô	 ð * AÝŒ	�)˜Q qÐ)Ñ)Ô)Ð)ð*ð *ð *ñ *ô *ð *ð *ð *ð *ð *ð *ð *øøøð *ð *ð *ð *ð *ð *s;   …>A Á
A'ÁA"Á"A'Ã C)Ã)C-Ã0C-Ä?E$Å$E(Å+E(ri   Údo_prune_layersc                 ó´  ‡ ‡‡‡‡	‡
— ‰ j         j        Š	‰	ddg dœ‰ _        g ‰ _        ‰	‰ _        ‰r>	 t          j        ‰d¬¦  «         n&# t          $ r}t          d‰› d�¦  «        |‚d}~ww xY wˆˆ ˆfd„}‰  	                    ¦   «         D ]\  }}|dk    rŒ ||‰	› d|› �¦  «         Œ‰ j
        Š
t          j        ‰
¦  «        ˆ	ˆˆˆ ˆ
ˆfd	„¦   «         }|‰ _
        dS )
aÜ  
    Attaches a debugging wrapper to every module in the model.

    This records structured inputs and outputs during the forward pass into a call tree.

    Args:
        model (`PreTrainedModel`, `nn.Module`): Model to wrap.
        debug_path (`str`): Optional directory to dump debug JSON files.
        do_prune_layers (`bool`, *optional*, defaults to `True`): Whether to prune intermediate layers.
        use_repr (bool, *optional*, defaults to `True`): Whether to save a `repr()`-ized version of the tensors as the
            `value` property in the associated FULL_TENSORS.json file, or to store full tensors in separate SafeTensors
            files and store the relative path to that file in the `value` property.
    N©re   rˆ   r^   r]   Tr|   r   ri   c                 ór   •‡ ‡‡— ‰ j         Št          j        ‰¦  «        ˆˆˆˆ ˆˆfd„¦   «         }|‰ _         d S )Nc                  óš  •‡— t          ¦   «         rH| |dœŠˆfd„‰D ¦   «         Š‰t          ‰‰‰‰› d�¬¦  «        d g dœ}‰j                             |¦  «         t	          j        ¦   «         5   ‰
| i |¤Ž}d d d ¦  «         n# 1 swxY w Y   t          ¦   «         r±t          d„ ‰	                     ¦   «         D ¦   «         ¦  «        dk    rd |d<   nt          |‰‰‰› d	�¬¦  «        |d<   ‰j                             ¦   «         }|d
         s|                     d
¦  «         ‰j        r&‰j        d         d
                              |¦  «         |S )N©ÚargsÚkwargsc                 óT   •— i | ]$}t          ‰|         ¦  «        d k    ¯|‰|         “Œ%S )r   )rx   )rE   rJ   Údict_inputss     €r   rK   zY_attach_debugger_logic.<locals>.wrap_forward.<locals>.wrapped_forward.<locals>.<dictcomp>/  s7   ø€ ÐaÐaÐa°QÍÈ[ÐYZÌ^ÑI\ÔI\Ð_`ÒI`ÐI`˜q +¨a¤.ÐI`ÐI`ÐI`r   Ú_inputsrB   rŸ   c              3   ó   K  — | ]}d V — ŒdS )r   Nr   )rE   rA   s     r   rl   zX_attach_debugger_logic.<locals>.wrap_forward.<locals>.wrapped_forward.<locals>.<genexpr>@  s"   è è € Ð:Ð:˜Q�qÐ:Ð:Ð:Ð:Ð:Ð:r   r   r^   Ú_outputsr]   ru   )	r   rD   Ú_debugger_model_call_stackÚappendr+   Úno_gradÚsumÚnamed_childrenr`   )ÚinpsÚkwsrb   r<   Úfinishedr¦   r   r˜   r•   ÚmoduleÚorig_forwardr   s        @€€€€€€r   Úwrapped_forwardzE_attach_debugger_logic.<locals>.wrap_forward.<locals>.wrapped_forward+  sÞ  øø€ å‰Œð >Ø'+°sÐ;Ð;�ØaÐaÐaÐa¸+ÐaÑaÔa�à#,Ý+Ø#Ø#-Ø!)Ø)2Ð&;Ð&;Ð&;ð	ñ ô ð  $Ø "ð
ð 
�ð Ô0×7Ò7¸Ñ=Ô=Ð=Ý”‘”ð 1ð 1Ø"�l DÐ0¨CÐ0Ð0�ð1ð 1ð 1ñ 1ô 1ð 1ð 1ð 1ð 1ð 1ð 1øøøð 1ð 1ð 1ð 1õ ‰Œð VÝÐ:Ð: &×"7Ò"7Ñ"9Ô"9Ð:Ñ:Ô:Ñ:Ô:¸QÒ>Ð>Ø&*�D˜‘O�Oå&3ØØ#-Ø!)Ø)2Ð&<Ð&<Ð&<ð	'ñ 'ô '�D˜‘Oð !Ô;×?Ò?ÑAÔA�à 
Ô+ð -Ø—L’L Ñ,Ô,Ð,àÔ3ð VØÔ4°RÔ8¸ÔD×KÒKÈHÑUÔUÐUØˆJs   Á,	BÂBÂB)ÚforwardÚ	functoolsÚwraps)r²   r˜   r´   r³   r   r•   r   s   `` @€€€r   Úwrap_forwardz,_attach_debugger_logic.<locals>.wrap_forward(  sb   øøøø€ Ø”~ˆå	Œ˜Ñ	&Ô	&ð%	ð %	ð %	ð %	ð %	ð %	ð %	ð %	ð %	ñ 
'Ô	&ð%	ðN )ˆŒˆˆr   rf   c                  óJ  •— t          ¦   «         r;‰› d�t          | |dœ‰‰
‰› d�¬¦  «        d g dœ}‰j                             |¦  «          ‰	| i |¤Ž}t          ¦   «         rÂ‰j        r»t          |‰‰
‰› d�¬¦  «        |d<   ‰j                             ¦   «         }|d         ‰j        d<   |d         ‰j        d<   |d	         ‰j        d	<   ˆfd
„t          ‰j                             ¦   «         ¦  «        D ¦   «          ‰rt          ‰j        ¦  «         t          ‰‰¬¦  «         |S )Nz (top-level)r¢   r§   rB   rŸ   r©   r^   rˆ   r]   c                 ó`   •— g | ]*}‰j         |         °‰j                              |d ¦  «        ‘Œ+S )N)r�   r`   )rE   rJ   r•   s     €r   rH   zG_attach_debugger_logic.<locals>.top_wrapped_forward.<locals>.<listcomp>{  s;   ø€ ÐmÐmÐm¨qÐY^ÔYiÐjkÔYlÐmˆUÔ×!Ò! ! TÑ*Ô*ÐmÐmÐmr   )r   r•   )
r   rD   rª   r«   r`   r�   rM   Úkeysry   rœ   )r¯   r°   Útop_noder<   r±   Ú
class_namer   r�   r•   Úreal_top_forwardr   s        €€€€€€r   Útop_wrapped_forwardz3_attach_debugger_logic.<locals>.top_wrapped_forward^  s‚  ø€ å‰?Œ?ð 	>à",Ð:Ð:Ð:Ý'Ø!¨SÐ1Ð1Ø)Ø%Ø%/Ð"8Ð"8Ð"8ð	ñ ô ð  Øð
ð 
ˆHð Ô,×3Ò3°HÑ=Ô=Ð=àÐ Ð,¨Ð,Ð,ˆÝ‰?Œ?ð 	F˜uÔ?ð 	FÝ"/ØØ%Ø!Ø!+Ð5Ð5Ð5ð	#ñ #ô #ˆH�YÑð Ô7×;Ò;Ñ=Ô=ˆHØ)1°(Ô);ˆEÔ˜XÑ&Ø*2°9Ô*=ˆEÔ˜YÑ'Ø+3°JÔ+?ˆEÔ˜ZÑ(àmÐmÐmÐmµD¸Ô9I×9NÒ9NÑ9PÔ9PÑ4QÔ4QÐmÑmÔmÐmð ð <Ý)¨%Ô*:Ñ;Ô;Ð;å!¨Z¸uÐEÑEÔEÐEØˆ
r   )Ú	__class__Ú__name__r�   rª   r‹   r/   rŠ   rŒ   r5   Únamed_modulesrµ   r¶   r·   )r•   r   r�   r   r—   r¸   ÚnameÚ	submoduler¿   r½   r¾   s   ````     @@r   Ú_attach_debugger_logicrÅ     sv  øøøøøø€ ð& ”Ô)€Jð (2¸TÈdÐ`bÐcÐc€EÔØ')€EÔ$Ø'1€EÔ$àð Xð	XÝŒK˜
¨TÐ2Ñ2Ô2Ð2Ð2øÝð 	Xð 	Xð 	XÝÐOÀ*ÐOÐOÐOÑPÔPÐVWÐWøøøøð	Xøøøð+)ð +)ð +)ð +)ð +)ð +)ð +)ð\ !×.Ò.Ñ0Ô0ð 8ð 8‰ˆˆiØ�2Š:ˆ:ØØˆ�Y :Ð 6Ð 6°Ð 6Ð 6Ñ7Ô7Ð7Ð7ð ”}Ðå„_Ð%Ñ&Ô&ð#ð #ð #ð #ð #ð #ð #ð #ð #ñ 'Ô&ð#ðJ (€E„M€M€Ms   °A Á
A*ÁA%Á%A*)r+   )Úbackendsc              #   ó  K  — d„ |                       ¦   «         D ¦   «         }| j        || <   t          | |||¦  «         	 | V — |                     ¦   «         D ]\  }}||_        ŒdS # |                     ¦   «         D ]\  }}||_        Œw xY w)a  
    # Model addition debugger - context manager for model adders
    This context manager is a power user tool intended for model adders.

    It tracks all forward calls within a model forward and logs a slice of each input and output on a nested JSON file.
    If `use_repr=True` (the default), the JSON file will record a `repr()`-ized version of the tensors as a list of
    strings. If `use_repr=False`, the full tensors will be stored in separate SafeTensors files and the JSON file will
    provide a relative path to that file.

    To note, this context manager enforces `torch.no_grad()`.

    ## Usage

    add the context manager to a model to debug

    ```python
    import torch

    from PIL import Image
    from transformers import LlavaProcessor, LlavaForConditionalGeneration, model_addition_debugger_context

    torch.random.manual_seed(673)

    # load pretrained model and processor
    model_id = "llava-hf/llava-1.5-7b-hf"
    processor = LlavaProcessor.from_pretrained(model_id)
    model = LlavaForConditionalGeneration.from_pretrained(model_id)

    # create random image input
    random_image = Image.fromarray(torch.randint(0, 256, (224, 224, 3), dtype=torch.uint8).numpy())

    # prompt
    prompt = "<image>Describe this image."

    # process inputs
    inputs = processor(text=prompt, images=random_image, return_tensors="pt")

    # call forward method (not .generate!)
    with model_addition_debugger_context(model, debug_path="Your_debug_path", do_prune_layers=False):
        output = model.forward(**inputs)
    ```

    c                 ó$   — i | ]\  }}||j         “ŒS r   )rµ   )rE   rA   Úms      r   rK   z3model_addition_debugger_context.<locals>.<dictcomp>º  s    € ÐDÐDÐD¡d a¨�Q˜œ	ÐDÐDÐDr   N)rÂ   rµ   rÅ   rQ   )r•   r   r�   r   Úorig_forwardsÚmodule_instanceÚforward_methods          r   Úmodel_addition_debugger_contextrÍ   ‡  s¿   è è € ðf EÐD¨e×.AÒ.AÑ.CÔ.CÐDÑDÔD€MØ œ=€M�%ÑÝ˜5 *¨o¸xÑHÔHÐHð5Øˆˆˆà/<×/BÒ/BÑ/DÔ/Dð 	5ð 	5Ñ+ˆO˜^Ø&4ˆOÔ#Ð#ð	5ð 	5ø¨}×/BÒ/BÑ/DÔ/Dð 	5ð 	5Ñ+ˆO˜^Ø&4ˆOÔ#Ð#ð	5øøøs   ¾A% Á%#B)NTN)ri   TT)NTT)*r¶   r‘   r/   ÚreÚ
contextlibr   r   Úior   Údistributed.utilsr   Úutilsr	   Úutils.import_utilsr
   r   r   r+   Úsafetensors.torchr   Útorch.distributed.tensorÚ
get_loggerrÁ   r�   r   Úcompiler   Ústrr   r   Úboolr=   rD   rS   r-   ra   rm   rq   ry   rœ   rÅ   rÍ   r   r   r   ú<module>rÚ      s  ðð  Ð Ð Ð Ø €€€Ø 	€	€	€	Ø 	€	€	€	Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø Ð Ð Ð Ð Ð à :Ð :Ð :Ð :Ð :Ð :Ø Ð Ð Ð Ð Ð Ø \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \Ð \ð ÐÑÔð (Ø€L€L€LØ+Ð+Ð+Ð+Ð+Ð+ð &Ð%Ñ'Ô'ð (Ø'Ð'Ð'Ð'ð 
ˆÔ	˜HÑ	%Ô	%€ð.ð .ð .ð
 "�r”zÐ"=Ñ>Ô>Ð ðC 3ð C¨3ð Cð Cð Cð Cð ð  ð  ð ^bð.ð .Ø˜T‘zð.Ø48ð.ØPSÐVZÑPZð.ð .ð .ð .ðb(0ð (0 S¨4¡Zð (0À$ð (0Ð^aÐdhÑ^hð (0ð (0ð (0ð (0ðV5˜œð 5ð 5ð 5ð 5ð"-ð -ð -ð �"”*˜_Ñ-Ô-€ð\ð \ð \ð")ð )ð )ð((* c¨D¡jð (*ð (*ð (*ð (*ðZ Ø Øð	|(ð |(àð|(ð ð|(ð ð	|(ð |(ð |(ð |(ð~ 
€�:ÐÑÔØð "Ø Øð	85ð 85à�d‘
ð85ð ð85ð ð	85ð 85ð 85ñ „ñ Ôð85ð 85ð 85r   