ó
    qyüij0  ã                  óJ  • S r SSKJr  SSKrSSKJr  SSKJr  SSKJ	r	  SSK
Jr  SS	KJrJr  \(       a  SS
KJr  SSKJr  0 r\\" SS9 " S S5      5       5       r " S S5      r\" S5      rSS jr        SS jrSSS jjr\R4                  " 5       rSS jrSSS.S jjrg) z°
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                  óN   • \ rS rSr% SrS\S'   SrS\S'   SrS	\S
'   SrS	\S'   Sr	g)ÚOutputRecorderé'   aÿ  
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.
ztype[nn.Module]Útarget_classr   ÚintÚindexNú
str | NoneÚ
layer_nameÚ
class_name© )
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__r   r   r   Ú__static_attributes__r   ó    Ú`/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/utils/output_capturing.pyr   r   '   s,   ‡ ñð "Ó!Ø€Eˆ3ƒNØ!€J�
Ó!Ø!€J�
Ö!r#   r   c                  ó0   • \ rS rSrSrS rS rS rS rSr	g)	ÚCompileableContextVaré:   a�  
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                ó>   • [        US S9U l        S U l        SU l        g )N)ÚdefaultF)r   Úcontext_varÚ
global_varÚ	compiling)ÚselfÚnames     r$   Ú__init__ÚCompileableContextVar.__init__B   s   € Ü% d°DÑ9ˆÔØˆŒØˆ�r#   c                óp   • U R                   (       a  U R                  $ U R                  R                  5       $ ©N)r,   r+   r*   Úget)r-   s    r$   r3   ÚCompileableContextVar.getG   s(   € à�>�>Ø—?‘?Ð"à×#Ñ#×'Ñ'Ó)Ð)r#   c                ór   • [        5       (       a  Xl        SU l        g U R                  R	                  U5      $ )NT)r
   r+   r,   r*   Úset)r-   Úvalues     r$   r6   ÚCompileableContextVar.setN   s1   € Ü#×%Ñ%Ø#ŒOØ!ˆDŒNØà×#Ñ#×'Ñ'¨Ó.Ð.r#   c                ó€   • U R                   (       d  Uc  S U l        SU l         g U R                  R                  U5        g )NF)r,   r+   r*   Úreset)r-   Útokens     r$   r:   ÚCompileableContextVar.resetV   s/   € Ø�>�>˜U™]Ø"ˆDŒOØ"ˆD�Nà×Ñ×"Ñ" 5Õ)r#   )r,   r*   r+   N)
r   r   r   r   r    r/   r3   r6   r:   r"   r   r#   r$   r&   r&   :   s   † ñòò
*ò/õ*r#   r&   Úoutput_collectorc                ó8   ^^• UU4S jnU R                  U5        g)zaInstall the forward hook needed to capture the output described by `key` and `index` in `module`.c                óL  >• [         R                  5       nUb  TUR                  5       ;  a  g TS:X  a)  [        UT   5      S:X  a  UT   R	                  US   5        [        U[        5      (       d  UT   R	                  U5        g UT   b  UT   R	                  UT   5        g g )NÚhidden_statesr   )Ú_active_collectorr3   ÚkeysÚlenÚappendÚ
isinstanceÚtuple)ÚmoduleÚargsÚoutputÚcollected_outputsr   Úkeys       €€r$   Úoutput_capturing_hookÚ<install_output_capturing_hook.<locals>.output_capturing_hooke   s£   ø€ ä-×1Ñ1Ó3ÐàÑ$¨Ð3D×3IÑ3IÓ3KÓ(KØà�/Ó!¤cÐ*;¸CÑ*@Ó&AÀQÓ&FØ˜cÑ"×)Ñ)¨$¨q©'Ô2Ü˜&¤%×(Ñ(Ø˜cÑ"×)Ñ)¨&Õ1Ø�E‰]Ñ&Ø˜cÑ"×)Ñ)¨&°©-Õ8ð 'r#   N)Úregister_forward_hook)rG   rK   r   rL   s    `` r$   Úinstall_output_capturing_hookrO   b   s   ù€ ö9ð × Ñ Ð!6Õ7r#   c                óÔ  • SSK Jn  U R                  5        H6  u  pE[        XS5      (       d  [	        XQ SU 3U5        M(  [        XQ SU 3S9  M8     U H’  u  pgUR                  b  [        XR                  5      (       d1  UR                  c  M;  UR                  UR                  5      (       d  M]  UR                  b  UR                  U;  a  M|  [        XUR                  5        M”     g)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_childrenrE   Úrecursively_install_hooksÚ"install_all_output_capturing_hooksr   r   Úendswithr   rO   r   )Úparent_moduleÚmodule_nameÚcapture_tasksr   r.   rG   rK   Úspecss           r$   rU   rU   v   sÉ   € õ 1ð &×4Ñ4Ö6‰ˆä˜&×2Ñ2Ü% f°¸Q¸t¸fÐ.EÀ}ÖUô /¨vÀÈQÈtÈfÐ>UÔVñ 7ó $‰
ˆà×ÑÑ*¬z¸-×I[ÑI[×/\Ñ/\Ø×ÑÓ(¨[×-AÑ-AÀ%×BRÑBR×-SÓ-Sà×ÑÑ+°×0@Ñ0@ÈÓ0SÙÜ)¨-¸e¿k¹kÖJò $r#   c                óø  • [         R                  [        U R                  5      5      =(       d    0 n/ nUR	                  5        H“  u  pE[        U[        5      (       d  U/nU Hp  n[        U[        5      (       dF  SU;   a  SOSn[        U[        5      (       d  SOUn[        U[        5      (       d  UOSn	[        X—US9nUR                  XF45        Mr     M•     Ub  UOSn[        XU5        [        U SS5        g)	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.
r@   r   r	   N)r   r   r   Ú Ú!_output_capturing_hooks_installedT)Ú_CAN_RECORD_REGISTRYr3   ÚstrÚ	__class__ÚitemsrE   Úlistr   rD   rU   Úsetattr)
ÚmodelrR   Úcapture_flagsrZ   rK   Úlayer_specsr[   r   r   r   s
             r$   rV   rV   –   sÞ   € ô )×,Ñ,¬S°·±Ó-AÓB×HÀb€Mà€MØ)×/Ñ/Ö1ÑˆÜ˜+¤t×,Ñ,Ø&˜-ˆKÛ ˆEÜ˜e¤^×4Ñ4Ø,°Ó3™¸�Ü)3°E¼3×)?Ñ)?™TÀU�
Ü,6°u¼c×,BÑ,B™uÈ�Ü&°LÐZdÑe�Ø× Ñ  # Ö.ó !ñ 2ð Ñ)‰V¨r€FÜ˜e¨]Ô;äˆEÐ6¸Õ=r#   c                ó¸   • [        U SS5      (       a  g[           [        U SS5      (       a
   SSS5        g[        U 5        SSS5        g! , (       d  f       g= f)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.
r^   FN)ÚgetattrÚ_hook_installation_lockrV   )re   s    r$   Úmaybe_install_capturing_hooksrk   ¶   sM   € ô ˆuÐ9¸5×AÑAØæ	 ô �5Ð=¸u×EÑEØ÷	 
!Ð	 ô 	+¨5Ô1÷ 
!×	 Ö	 ús   šA·AÁ
AT)Útie_last_hidden_statesc               ó*   ^• U4S jnU b  U" U 5      $ U$ )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                ó4   >^ • [        T 5      U U4S j5       nU$ )Nc                ó¼  >• UR                  S[        U R                  SS5      5      n[        R	                  [        U R                  5      5      =(       d    0 nU Vs0 s H4  nSU 3UR	                  SU 3[        U R                  SU 3S5      5      _M6     nnSU;   a*  UR	                  S[        U R                  SS5      5      US'   SU;   a*  UR	                  S[        U R                  SS5      5      US	'   UR                  5        VVs0 s H!  u  pWU(       d  M  UR                  SS
5      / _M#     nnn[        U5      S:”  a  [        U 5        [        R                  U5      n	 T" U /UQ70 UD6n
[        R                  U	5        U GH  nUS:X  aˆ  T(       d  Oo[        U
S5      (       a'  X‹   S S X‹'   X‹   R                  U
R                   5        O7[        U
S5      (       a&  X‹   S S X‹'   X‹   R                  U
R"                  5        [%        X‹   5      X«'   M’  US:X  aj  ['        XK   [(        5      (       aA  [        XK   5      S:X  a0  [%        X‹   SS S2   5      X«'   [%        X‹   SS S2   5      U
SU-   '   Mð  [%        X‹   5      X«'   GM  [%        X‹   5      X«'   GM     USL a  U
R+                  5       n
U
$ s  snf s  snnf ! [        R                  U	5        f = f)NÚreturn_dictTÚoutput_FÚcross_attentionsÚoutput_attentionsÚoutput_cross_attentionsÚmask_decoder_attentionsÚoutput_mask_decoder_attentionsr]   r   r@   Úvision_hidden_stateséÿÿÿÿÚlast_hidden_stateÚ
attentionsr   r	   Úcross_)Úpopri   Úconfigr_   r3   r`   ra   rb   ÚreplacerC   rk   rA   r6   r:   ÚhasattrrD   rw   ry   rF   rE   rc   Úto_tuple)r-   rH   Úkwargsrp   Úcapturable_flagsÚkÚrecordable_keysÚvrJ   Úoutput_tokenÚoutputsrK   Úfuncrl   s               €€r$   ÚwrapperÚ4capture_outputs.<locals>.wrapped_fn.<locals>.wrapperÙ   sê  ø€ ð !Ÿ*™* ]´G¸D¿K¹KÈÐX\Ó4]Ó^ˆKô  4×7Ñ7¼¸D¿N¹NÓ8KÓL×RÐPRÐñ *óâ)�Að ˜!˜�˜vŸz™z¨G°A°3¨-¼ÀÇÁÐPWÐXYÐWZÈmÐ]bÓ9cÓdÒdÙ)ð ð ð
 "Ð%5Ó5Ø=C¿Z¹ZØ'¬°·±Ð>QÐSXÓ)Yó>�Ð 9Ñ:ð )Ð,<Ó<ØDJÇJÁJØ'¬°·±Ð>QÐSXÓ)YóE�Ð @ÑAð KZ×J_ÑJ_ÔJaÔ gÒJaÁ$À!ÔefÓ!= §¡¨9°bÓ!9¸2Ò!=ÑJaÐÑ gäÐ$Ó%¨Ó)Ü-¨dÔ3ä,×0Ñ0Ð1BÓCˆLð6Ù˜tÐ5 dÒ5¨fÑ5�ô "×'Ñ'¨Ô5ô )�Ø˜/Ó)Þ1ØÜ  Ð*@×AÑAØ1BÑ1GÈÈÐ1LÐ)Ñ.Ø)Ñ.×5Ñ5°g×6RÑ6RÕSÜ  Ð*=×>Ñ>Ø1BÑ1GÈÈÐ1LÐ)Ñ.Ø)Ñ.×5Ñ5°g×6OÑ6OÔPä#(Ð):Ñ)?Ó#@�G“LØ˜LÓ(ä!Ð"2Ñ"7¼×>Ñ>Ä3ÐGWÑG\ÓC]ÐabÓCbÜ',Ð->Ñ-CÀAÀDÀqÀDÑ-IÓ'J˜™Ü27Ð8IÑ8NÈqÈtÐRSÈtÑ8TÓ2U˜ ¨3¡Ó/ä',Ð->Ñ-CÓ'D˜œä#(Ð):Ñ)?Ó#@�G”Lñ) )ð, ˜eÒ#Ø!×*Ñ*Ó,�àˆNùòoùó !høô "×'Ñ'¨Õ5ús   Á;J9ÄJ>ÄJ>Å)K ËKr   )rˆ   r‰   rl   s   ` €r$   Ú
wrapped_fnÚ#capture_outputs.<locals>.wrapped_fnØ   s!   ù€ Ü	ˆt‹õ=	ó 
ð=	ð~ ˆr#   r   )rˆ   rl   r‹   s    ` r$   Úcapture_outputsr�   É   s#   ø€ õAðF ÑÙ˜$ÓÐØÐr#   )rG   ú	nn.ModulerK   r`   r   r   ÚreturnÚNone)rX   rŽ   rY   r`   rZ   z list[tuple[str, OutputRecorder]]r�   r�   r2   )re   r   rR   r   r�   r�   )re   r   r�   r�   )r    Ú
__future__r   Ú	threadingÚcontextvarsr   Údataclassesr   Ú	functoolsr   Útypingr   Úimport_utilsr
   r   r   r   rS   r   r_   r   r&   rA   rO   rU   rV   ÚLockrj   rk   r�   r   r#   r$   Ú<module>r™      sÊ   ðñõ
 #ã Ý "Ý !Ý Ý  ç <ö Ýå0ð Ð ð Ù	�:Ñ÷"ð "ó ó ð"÷"!*ñ !*ñJ *Ð*<Ó=Ð ô8ð(KØðKØ+.ðKØ?_ðKà	ôKö@>ð: $Ÿ.š.Ó*Ð ô2ð&T¸÷ Tr#   