§
    9²¸jw8  ã                  óN  — d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ dd	lmZmZ erdd
lmZ ddlmZ i Ze ed¬¦  «         G d„ d¦  «        ¦   «         ¦   «         Z G d„ d¦  «        Z ed¦  «        Z	 d-d.d„Zd/d$„Zd0d1d)„Z ej        ¦   «         Zd2d*„Zd0dd+œd,„ZdS )3z°
Contains the logic for automatic additional output capture with our forward decorators.
This mostly describe the hooks used and the logic to make capture thread/context safe.
é    )ÚannotationsN)Ú
ContextVar)Ú	dataclass©Úwraps)ÚTYPE_CHECKINGé   )Úis_torchdynamo_compilingÚrequires)Únné   ©ÚPreTrainedModel)Útorch)Úbackendsc                  óV   — e Zd ZU dZded<   dZded<   dZded	<   dZded
<   dZded<   dS )ÚOutputRecordera’  
    Configuration for recording outputs from a model via hooks.

    Attributes:
        target_class (Type): The class (e.g., nn.Module) to which the hook will be attached.
        index (Optional[int]): If the output is a tuple/list, optionally record only at a specific index.
        layer_name (Optional[str]): Name of the submodule to target (if needed), e.g., "transformer.layer.3.attn".
        class_name (Optional[str]): Name of the class to which the hook will be attached. Could be the suffix of class name in some cases.
        capture_initial_hidden_state  (bool): Whether to prepend the first module's input as the initial hidden state.
    ztype[nn.Module]Útarget_classr   ÚintÚindexNú
str | NoneÚ
layer_nameÚ
class_nameTÚboolÚcapture_initial_hidden_state)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r   r   r   © ó    ús/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/utils/output_capturing.pyr   r   '   sk   € € € € € € ð	ð 	ð "Ð!Ð!Ñ!Ø€E€N€N€N�NØ!€JÐ!Ð!Ð!Ñ!Ø!€JÐ!Ð!Ð!Ñ!Ø)-Ð Ð-Ð-Ð-Ñ-Ð-Ð-r"   r   c                  ó*   — e Zd ZdZd„ Zd„ Zd„ Zd„ ZdS )ÚCompileableContextVara¡  
    Convenience wrapper around a ContextVar for usage with `torch.compile`.
    This behaves exactly as a `ContextVar`, except when compilation is triggered in which case it behaves as a simple
    global variable. This is useful as `torch.compile` cannot trace the `get` method of `ContextVar`. This however means
    that the access to the underlying variable is not thread-safe when compilation is triggered.
    c                óN   — t          |d ¬¦  «        | _        d | _        d| _        d S )N)ÚdefaultF)r   Úcontext_varÚ
global_varÚ	compiling)ÚselfÚnames     r#   Ú__init__zCompileableContextVar.__init__D   s)   € Ý% d°DÐ9Ñ9Ô9ˆÔØˆŒØˆŒˆˆr"   c                óP   — | j         r| j        S | j                             ¦   «         S ©N)r*   r)   r(   Úget)r+   s    r#   r0   zCompileableContextVar.getI   s)   € àŒ>ð 	*Ø”?Ð"àÔ#×'Ò'Ñ)Ô)Ð)r"   c                ór   — t          ¦   «         r|| _        d| _        d S | j                             |¦  «        S )NT)r
   r)   r*   r(   Úset)r+   Úvalues     r#   r2   zCompileableContextVar.setP   s:   € Ý#Ñ%Ô%ð 	/Ø#ˆDŒOØ!ˆDŒNØ�4àÔ#×'Ò'¨Ñ.Ô.Ð.r"   c                ól   — | j         s|€d | _        d| _         d S | j                             |¦  «         d S )NF)r*   r)   r(   Úreset)r+   Útokens     r#   r5   zCompileableContextVar.resetX   s?   € ØŒ>ð 	*˜U˜]Ø"ˆDŒOØ"ˆDŒNˆNˆNàÔ×"Ò" 5Ñ)Ô)Ð)Ð)Ð)r"   N)r   r   r   r   r-   r0   r2   r5   r!   r"   r#   r%   r%   <   sZ   € € € € € ðð ðð ð ð
*ð *ð *ð/ð /ð /ð*ð *ð *ð *ð *r"   r%   Úoutput_collectorTÚmoduleú	nn.ModuleÚkeyÚstrr   r   r   r   ÚreturnÚNonec                óD   ‡‡‡— ˆˆˆfd„}|                       |¦  «         dS )zaInstall the forward hook needed to capture the output described by `key` and `index` in `module`.c                óV  •— t                                ¦   «         }|�‰|                     ¦   «         vrd S |                     d¦  «        }‰rB‰dk    r<t          |‰         ¦  «        dk    r#|€!|‰                              |d         ¦  «         ‰dk    r8|�6t          |‰         ¦  «        }||vr|‰                              d ¦  «         d S t          |t          ¦  «        s|‰                              |¦  «         d S |‰         �#|‰                              |‰         ¦  «         d S d S )NÚ_hidden_states_layersÚhidden_statesr   )Ú_active_collectorr0   ÚkeysÚlenÚappendÚ
isinstanceÚtuple)	r8   ÚargsÚoutputÚcollected_outputsÚhidden_states_layersÚcurrent_layer_idxr   r   r:   s	         €€€r#   Úoutput_capturing_hookz;install_output_capuring_hook.<locals>.output_capturing_hooki   sV  ø€ å-×1Ò1Ñ3Ô3ÐàÐ$¨Ð3D×3IÒ3IÑ3KÔ3KÐ(KÐ(KØˆFð  1×4Ò4Ð5LÑMÔMÐð )ð	3à�Ò&Ð&ÝÐ% cÔ*Ñ+Ô+¨qÒ0Ð0Ø$Ð,à˜cÔ"×)Ò)¨$¨q¬'Ñ2Ô2Ð2ð �/Ò!Ð!Ð&:Ð&FÝ #Ð$5°cÔ$:Ñ ;Ô ;ÐØ Ð(<Ð<Ð<Ø! #Ô&×-Ò-¨dÑ3Ô3Ð3Ø�å˜&¥%Ñ(Ô(ð 	9Ø˜cÔ"×)Ò)¨&Ñ1Ô1Ð1Ð1Ð1Ø�EŒ]Ð&Ø˜cÔ"×)Ò)¨&°¬-Ñ8Ô8Ð8Ð8Ð8ð 'Ð&r"   N)Úregister_forward_hook)r8   r:   r   r   rM   s    ``` r#   Úinstall_output_capuring_hookrO   d   sF   øøø€ ð
9ð 9ð 9ð 9ð 9ð 9ð 9ðB × Ò Ð!6Ñ7Ô7Ð7Ð7Ð7r"   Úparent_moduleÚmodule_nameÚcapture_tasksú list[tuple[str, OutputRecorder]]c                óø  — ddl m} |                      ¦   «         D ]B\  }}t          ||¦  «        st	          ||› d|› �|¦  «         Œ,t          ||› d|› �¬¦  «         ŒC|D ]™\  }}|j        duot          | |j        ¦  «        }|j        duo|                     |j        ¦  «        }	|s|	rO|j	        �,|j	         
                    d¦  «        }
d|
z   dz   }
|
|dz   v }|sŒ}t          | ||j        |j        ¦  «         ŒšdS )aÖ  
    Recursively install all output capturing hooks on all submodules of `parent_module`.
    Note that we need to use this recursive approach instead of simply iterating over all modules, because we want
    to respect the `capture_tasks` of all individual submodels (`PreTrainedModel` instances) in the graph. That is, once
    we reach a submodel in the graph, its children should use this submodel's `capture_tasks`, but other parts of the graph
    should not.
    r   r   ú.)ÚprefixN)Úmodeling_utilsr   Únamed_childrenrF   Úrecursively_install_hooksÚ"install_all_output_capturing_hooksr   r   Úendswithr   ÚstriprO   r   r   )rP   rQ   rR   r   r,   r8   r:   ÚspecsÚmatch_target_classÚmatch_class_nameÚtarget_layer_nameÚmatchess               r#   rY   rY   �   su  € ð 1Ð0Ð0Ð0Ð0Ð0ð &×4Ò4Ñ6Ô6ð Wð W‰ˆˆfå˜& /Ñ2Ô2ð 	WÝ% f°Ð.EÐ.E¸tÐ.EÐ.EÀ}ÑUÔUÐUÐUõ /¨vÀÐ>UÐ>UÈtÐ>UÐ>UÐVÑVÔVÐVÐVð $ð nð n‰
ˆˆUà"Ô/°tÐ;ÐmÅ
È=ÐZ_ÔZlÑ@mÔ@mÐà Ô+°4Ð7Ðb¸K×<PÒ<PÐQVÔQaÑ<bÔ<bÐàð 	nÐ!1ð 	nàÔÐ+à$)Ô$4×$:Ò$:¸3Ñ$?Ô$?Ð!Ø$'Ð*;Ñ$;¸cÑ$AÐ!à+¨{¸SÑ/@Ð@�Øð Øå(¨¸¸U¼[È%ÔJlÑmÔmÐmøð#nð nr"   Úmodelr   rV   r   c                ó  — t                                t          | j        ¦  «        ¦  «        pi }g }|                     ¦   «         D ]š\  }}t          |t          ¦  «        s|g}|D ]z}t          |t          ¦  «        sLd|v rdnd}t          |t          ¦  «        sdn|}t          |t          ¦  «        s|nd}	t          |	||¬¦  «        }|                     ||f¦  «         Œ{Œ›|�|nd}t          | ||¦  «         t          | dd¦  «         dS )	zÙ
    Install the output recording hooks on all the modules in `model`. This will take care of correctly dispatching
    the `_can_record_outputs` property of each individual submodels in case of composite models.
    rA   r   r	   N)r   r   r   Ú Ú!_output_capturing_hooks_installedT)Ú_CAN_RECORD_REGISTRYr0   r;   Ú	__class__ÚitemsrF   Úlistr   rE   rY   Úsetattr)
rb   rV   Úcapture_flagsrR   r:   Úlayer_specsr]   r   r   r   s
             r#   rZ   rZ   ·   s9  € õ )×,Ò,­S°´Ñ-AÔ-AÑBÔBÐHÀb€Mà€MØ)×/Ò/Ñ1Ô1ð 	/ð 	/Ñˆˆ[Ý˜+¥tÑ,Ô,ð 	(Ø&˜-ˆKØ ð 	/ð 	/ˆEÝ˜e¥^Ñ4Ô4ð fØ,°Ð3Ð3˜˜¸�Ý)3°E½3Ñ)?Ô)?ÐJ˜T˜TÀU�
Ý,6°u½cÑ,BÔ,BÐL˜u˜uÈ�Ý&°LÈÐZdÐeÑeÔe�Ø× Ò  # u Ñ.Ô.Ð.Ð.ð	/ð Ð)ˆVˆV¨r€FÝ˜e V¨]Ñ;Ô;Ð;åˆEÐ6¸Ñ=Ô=Ð=Ð=Ð=r"   c                óÈ   — t          | dd¦  «        rdS t          5  t          | dd¦  «        r	 ddd¦  «         dS t          | ¦  «         ddd¦  «         dS # 1 swxY w Y   dS )zà
    Check if the model already has output capturing hooks installed, and install them if it is not already the
    case.
    Note that this is thread-safe, in case 2 (or more) threads want to install them concurrently.
    re   FN)ÚgetattrÚ_hook_installation_lockrZ   )rb   s    r#   Úmaybe_install_capturing_hooksrp   ×   sÚ   € õ ˆuÐ9¸5ÑAÔAð Øˆå	 ð 2ð 2õ �5Ð=¸uÑEÔEð 	Øð	2ð 2ð 2ñ 2ô 2ð 2ð 2ð 2õ 	+¨5Ñ1Ô1Ð1ð2ð 2ð 2ñ 2ô 2ð 2ð 2ð 2ð 2ð 2ð 2ð 2øøøð 2ð 2ð 2ð 2ð 2ð 2s   ›A»AÁAÁA)Útie_last_hidden_statesc               ó,   ‡— ˆfd„}| � || ¦  «        S |S )aô  
    Decorator to intercept specific layer outputs through hooks. The hooks are installed only once and lazily,
    the first time output capture is requested with the `output_xxx` kwargs/config.
    The implementation is fully context/thread safe, except when using `torch.compile`, as dynamo is unable to trace
    through `ContextVar` methods.

    Args:
        tie_last_hidden_states (`bool`, *optional*, defaults to `True`):
            Whether to overwrite `out.hidden_states[-1]` with the `out.last_hidden_state`. This is true for all language models
            and should be toggled off only if `out.hidden_states[-1]` has to be the hidden state before last layer norm, which
            is needed for some vision models (e.g. CLIP, SigLIP).
    c                ó@   •‡ — t          ‰ ¦  «        ˆ ˆfd„¦   «         }|S )Nc                ó>  •‡ ‡— ‰                      dt          ‰ j        dd¦  «        ¦  «        }t                               t          ‰ j        ¦  «        ¦  «        pi }ˆˆ fd„|D ¦   «         }d|v r-‰                     dt          ‰ j        dd¦  «        ¦  «        |d<   d|v r-‰                     dt          ‰ j        dd¦  «        ¦  «        |d	<   d
„ |                     ¦   «         D ¦   «         }d|v r@t          |d         t          t          t          f¦  «        rt          |d         ¦  «        |d<   t          |¦  «        dk    rt          ‰ ¦  «         t                               |¦  «        }	  ‰‰ g|¢R i ‰¤Ž}t                               |¦  «         n# t                               |¦  «         w xY w|                      dd ¦  «        }	|D ]º}
|
dk    rš‰r|	�||
         d         €n‡t!          |d¦  «        r4||
         d d…         ||
<   ||
                              |j        ¦  «         nCt!          |d¦  «        r3||
         d d…         ||
<   ||
                              |j        ¦  «         t          ||
         ¦  «        ||
<   Œ»|du r|                     ¦   «         }|S )NÚreturn_dictTc                óx   •— i | ]6}d |› �‰                      d |› �t          ‰j        d |› �d¦  «        ¦  «        “Œ7S )Úoutput_F)r0   rn   Úconfig)Ú.0ÚkÚkwargsr+   s     €€r#   ú
<dictcomp>zHcapture_outputs.<locals>.wrapped_fn.<locals>.wrapper.<locals>.<dictcomp>   s[   ø€ ð ð ð àð ˜!��˜vŸzšz¨-°A¨-¨-½ÀÄÈmÐXYÈmÈmÐ]bÑ9cÔ9cÑdÔdðð ð r"   Úcross_attentionsÚoutput_attentionsFÚoutput_cross_attentionsÚmask_decoder_attentionsÚoutput_mask_decoder_attentionsc                óF   — i | ]\  }}|¯|                      d d¦  «        g “ŒS )rw   rd   )Úreplace)ry   rz   Úvs      r#   r|   zHcapture_outputs.<locals>.wrapped_fn.<locals>.wrapper.<locals>.<dictcomp>  s2   € Ð gÐ gÐ gÁ$À!ÀQÐefÐ g §¢¨9°bÑ!9Ô!9¸2Ð gÐ gÐ gr"   Úoutput_hidden_statesr@   r   rA   éÿÿÿÿÚvision_hidden_statesÚlast_hidden_state)Úpoprn   rx   rf   r0   r;   rg   rh   rF   ri   rG   r2   rD   rp   rB   r5   ÚhasattrrE   r‡   rˆ   Úto_tuple)r+   rH   r{   ru   Úcapturable_flagsÚrecordable_keysrJ   Úoutput_tokenÚoutputsrK   r:   Úfuncrq   s   ` `        €€r#   Úwrapperz4capture_outputs.<locals>.wrapped_fn.<locals>.wrapperù   s  øøø€ ð !Ÿ*š* ]µG¸D¼KÈÐX\Ñ4]Ô4]Ñ^Ô^ˆKõ  4×7Ò7½¸D¼NÑ8KÔ8KÑLÔLÐRÐPRÐðð ð ð ð à)ðñ ô ˆOð
 "Ð%5Ð5Ð5Ø=C¿ZºZØ'­°´Ð>QÐSXÑ)YÔ)Yñ>ô >�Ð 9Ñ:ð )Ð,<Ð<Ð<ØDJÇJÂJØ'­°´Ð>QÐSXÑ)YÔ)YñEô E�Ð @ÑAð !hÐ gÈ/×J_ÒJ_ÑJaÔJaÐ gÑ gÔ gÐð &¨Ð8Ð8½ZØÐ 6Ô7½$ÅÅsÐ9Kñ>ô >Ð8õ >AÀÐQgÔAhÑ=iÔ=iÐ!Ð"9Ñ:åÐ$Ñ%Ô%¨Ò)Ð)Ý-¨dÑ3Ô3Ð3å,×0Ò0Ð1BÑCÔCˆLð6Ø˜$˜tÐ5 dÐ5Ð5Ð5¨fÐ5Ð5�õ "×'Ò'¨Ñ5Ô5Ð5Ð5øÕ!×'Ò'¨Ñ5Ô5Ð5Ð5øøøà#4×#8Ò#8Ð9PÐRVÑ#WÔ#WÐ à(ð =ð =�Ø˜/Ò)Ð)à1ð 	QØ,Ð8Ð=NÈsÔ=SÐTVÔ=WÐ=_àÝ  Ð*@ÑAÔAð QØ1BÀ3Ô1GÈÈÈÔ1LÐ)¨#Ñ.Ø)¨#Ô.×5Ò5°gÔ6RÑSÔSÐSÐSÝ  Ð*=Ñ>Ô>ð QØ1BÀ3Ô1GÈÈÈÔ1LÐ)¨#Ñ.Ø)¨#Ô.×5Ò5°gÔ6OÑPÔPÐPå$Ð%6°sÔ%;Ñ<Ô<�˜‘�à˜eÐ#Ð#Ø!×*Ò*Ñ,Ô,�àˆNs   Å,F ÆF/r   )r�   r‘   rq   s   ` €r#   Ú
wrapped_fnz#capture_outputs.<locals>.wrapped_fnø   s:   øø€ Ý	ˆt‰Œð>	ð >	ð >	ð >	ð >	ñ 
Œð>	ð@ ˆr"   r!   )r�   rq   r’   s    ` r#   Úcapture_outputsr“   ê   s@   ø€ ðBð Bð Bð Bð BðH ÐØˆz˜$ÑÔÐØÐr"   )T)
r8   r9   r:   r;   r   r   r   r   r<   r=   )rP   r9   rQ   r;   rR   rS   r<   r=   r/   )rb   r   rV   r   r<   r=   )rb   r   r<   r=   )r   Ú
__future__r   Ú	threadingÚcontextvarsr   Údataclassesr   Ú	functoolsr   Útypingr   Úimport_utilsr
   r   r   r   rW   r   rf   r   r%   rB   rO   rY   rZ   ÚLockro   rp   r“   r!   r"   r#   ú<module>rœ      sù  ððð ð
 #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "Ø !Ð !Ð !Ð !Ð !Ð !Ø Ð Ð Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  à <Ð <Ð <Ð <Ð <Ð <Ð <Ð <ð ð 1ØÐÐÐÐÐà0Ð0Ð0Ð0Ð0Ð0ð Ð ð Ø	€�:ÐÑÔð.ð .ð .ð .ð .ñ .ô .ñ Ôñ „ð.ð&!*ð !*ð !*ð !*ð !*ñ !*ô !*ð !*ðJ *Ð)Ð*<Ñ=Ô=Ð ð SWð&8ð &8ð &8ð &8ð &8ðR'nð 'nð 'nð 'nðT>ð >ð >ð >ð >ð: )˜)œ.Ñ*Ô*Ð ð2ð 2ð 2ð 2ð&T¸ð Tð Tð Tð Tð Tð Tð Tr"   