+
    œ°³jw8  ã                  óP  € R t ^ RIHt ^ RIt^ RIHt ^ RIHt ^ RIH	t	 ^ RI
Ht ^RIHtHt ]'       d   ^ RIHt ^R	IHt / t]]! RR
7       ! R R4      4       4       t ! R R4      t]! R4      tRR R lltR R ltRR R llt]P4                  ! 4       tR R ltRRR/R lltR# )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)Úbackendsc                  ó^   € ] tR t^'t$ RtR]R&   ^ tR]R&   RtR]R&   RtR]R	&   R
t	R]R&   Rt
R# )ÚOutputRecorderar  
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_classÚintÚindexNú
str | NoneÚ
layer_nameÚ
class_nameTÚboolÚcapture_initial_hidden_state© )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__r   r   r   r   Ú__static_attributes__r   ó    Úd/home/luca/screen-checking/.venv/lib/python3.14/site-packages/transformers/utils/output_capturing.pyr   r   '   s7   ‡ ñ	ð "Ó!Ø€Eˆ3ƒNØ!€J�
Ó!Ø!€J�
Ó!Ø)-Ð  $×-r    r   c                  ó2   € ] tR t^<tRtR tR tR tR tRt	R# )Ú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                	óF   € \        VR R7      V n        R V n        RV n        R # )N)ÚdefaultF)r   Úcontext_varÚ
global_varÚ	compiling)ÚselfÚnames   &&r!   Ú__init__ÚCompileableContextVar.__init__D   s   € Ü% d°DÔ9ˆÔØˆŒØˆŽr    c                	ór   € V P                   '       d   V P                  # V P                  P                  4       # ©N)r(   r'   r&   Úget)r)   s   &r!   r/   ÚCompileableContextVar.getI   s*   € à�>�>ˆ>Ø—?‘?Ð"à×#Ñ#×'Ñ'Ó)Ð)r    c                	óv   € \        4       '       d   Wn        R V n        R# V P                  P	                  V4      # )TN)r   r'   r(   r&   Úset)r)   Úvalues   &&r!   r2   ÚCompileableContextVar.setP   s1   € Ü#×%Ò%Ø#ŒOØ!ˆDŒNÙà×#Ñ#×'Ñ'¨Ó.Ð.r    c                	óˆ   € V P                   '       g   Vf   R V n        RV n         R # V P                  P                  V4       R # )NF)r(   r'   r&   Úreset)r)   Útokens   &&r!   r6   ÚCompileableContextVar.resetX   s1   € Ø�>�>ˆ>˜Uš]Ø"ˆDŒOØ"ˆDŽNà×Ñ×"Ñ" 5Ö)r    )r(   r&   r'   N)
r   r   r   r   r   r+   r/   r2   r6   r   r   r    r!   r#   r#   <   s   † ñòò
*ò/ö*r    r#   Úoutput_collectorTc          
     ó,   € V ^8„  d   QhRRRRRRRRR	R
/# )é   Úmoduleú	nn.ModuleÚkeyÚstrr   r   r   r   ÚreturnÚNoner   )Úformats   "r!   Ú__annotate__rC   d   s4   € ÷ &8ñ &8Øð&8Øð&8Ø(+ð&8ØKOð&8à	ñ&8r    c                ó>   aaa€ VVV3R lpV P                  V4       R# )zaInstall the forward hook needed to capture the output described by `key` and `index` in `module`.c                óL  <€ \         P                  4       pVe   SVP                  4       9  d   R # VP                  R4      pS'       dB   SR8X  d;   \        VS,          4      ^ 8X  d$   Vf    VS,          P	                  V^ ,          4       SR8X  d7   Ve3   \        VS,          4      pWT9  d   VS,          P	                  R 4       R # \        V\        4      '       g   VS,          P	                  V4       R # VS,          e"   VS,          P	                  VS,          4       R # R # )NÚ_hidden_states_layersÚhidden_states)Ú_active_collectorr/   ÚkeysÚlenÚappendÚ
isinstanceÚtuple)	r<   ÚargsÚoutputÚcollected_outputsÚhidden_states_layersÚcurrent_layer_idxr   r   r>   s	   &&&   €€€r!   Úoutput_capturing_hookÚ;install_output_capuring_hook.<locals>.output_capturing_hooki   s   ø€ ä-×1Ñ1Ó3ÐàÒ$¨Ð3D×3IÑ3IÓ3KÔ(KÙð  1×4Ñ4Ð5LÓMÐ÷ )Ø�Ô&ÜÐ% cÕ*Ó+¨qÔ0Ø$Ò,à˜cÕ"×)Ñ)¨$¨q­'Ô2ð �/Ô!Ð&:Ò&FÜ #Ð$5°cÕ$:Ó ;ÐØ Ô<Ø! #Õ&×-Ñ-¨dÔ3Ùä˜&¤%×(Ò(Ø˜cÕ"×)Ñ)¨&Ö1Ø�E�]Ò&Ø˜cÕ"×)Ñ)¨&°­-Ö8ñ 'r    N)Úregister_forward_hook)r<   r>   r   r   rS   s   &fff r!   Úinstall_output_capuring_hookrV   d   s   ú€ ÷
9ðB × Ñ Ð!6Ö7r    c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r;   Úparent_moduler=   Úmodule_namer?   Úcapture_tasksz list[tuple[str, OutputRecorder]]r@   rA   r   )rB   s   "r!   rC   rC   �   s2   € ÷ 'nñ 'nØð'nØ+.ð'nØ?_ð'nà	ñ'nr    c                ó~  € ^RI Hp V P                  4        F:  w  rE\        WS4      '       g   \	        WQ RV 2V4       K)  \        WQ RV 2R7       K<  	  V Fâ  w  rgVP                  RJ;'       d    \        WP                  4      pVP                  RJ;'       d    VP                  VP                  4      p	V'       g   V	'       g   Ks  VP                  eA   VP                  P                  R4      p
RV
,           R,           p
W¡R,           9   pV'       g   KÁ  \        WVP                  VP                  4       Kä  	  R# )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   Ú.)ÚprefixN)Úmodeling_utilsr   Únamed_childrenrL   Úrecursively_install_hooksÚ"install_all_output_capturing_hooksr   r   Úendswithr   ÚstriprV   r   r   )rX   rY   rZ   r   r*   r<   r>   ÚspecsÚmatch_target_classÚmatch_class_nameÚtarget_layer_nameÚmatchess   &&&         r!   r`   r`   �   s  € õ 1ð &×4Ñ4Ö6‰ˆä˜&×2Ò2Ü% f°¸Q¸t¸fÐ.EÀ}ÖUô /¨vÀÈQÈtÈfÐ>U×Vñ 7ó $‰
ˆà"×/Ñ/°tÐ;×mÐmÄ
È=×ZlÑZlÓ@mÐà ×+Ñ+°4Ð7×bÐb¸K×<PÑ<PÐQV×QaÑQaÓ<bÐç×!1Ñ!1à×ÑÒ+à$)×$4Ñ$4×$:Ñ$:¸3Ó$?Ð!Ø$'Ð*;Õ$;¸cÕ$AÐ!à+¸SÕ/@Ñ@�ßÙä(¨¸U¿[¹[È%×JlÑJlÖmó# $r    c               ó$   € V ^8„  d   QhRRRRRR/# )r;   Úmodelr   r]   r   r@   rA   r   )rB   s   "r!   rC   rC   ·   s"   € ÷ >ñ >¨oð >Àzð >Ð]añ >r    c                ó  € \         P                  \        V P                  4      4      ;'       g    / p. pVP	                  4        F›  w  rE\        V\        4      '       g   V.pV Fw  p\        V\        4      '       gM   RV9   d   ^ M^p\        V\        4      '       g   RMTp\        V\        4      '       g   TMRp	\        W—VR7      pVP                  WF34       Ky  	  K�  	  Ve   TMRp\        WV4       \        V RR4       R# )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.
rG   N)r   r   r   Ú Ú!_output_capturing_hooks_installedT)Ú_CAN_RECORD_REGISTRYr/   r?   Ú	__class__ÚitemsrL   Úlistr   rK   r`   Úsetattr)
rj   r]   Úcapture_flagsrZ   r>   Úlayer_specsrd   r   r   r   s
   &&        r!   ra   ra   ·   sá   € ô )×,Ñ,¬S°·±Ó-AÓB×HÐ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               ó    € V ^8„  d   QhRRRR/# )r;   rj   r   r@   rA   r   )rB   s   "r!   rC   rC   ×   s   € ÷ 2ñ 2¨ð 2¸Tñ 2r    c                óØ   € \        V RR4      '       d   R# \        ;_uu_ 4        \        V RR4      '       d    RRR4       R# \        V 4       RRR4       R#   + '       g   i     R# ; i)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.
rm   FN)ÚgetattrÚ_hook_installation_lockra   )rj   s   &r!   Úmaybe_install_capturing_hooksry   ×   sS   € ô ˆuÐ9¸5×AÒAÙç	 Ö	 ô �5Ð=¸u×EÒEØ÷	 
!Ñ	 ô 	+¨5Ô1÷ 
!×	 ×	 Ò	 ús   ¤AÁAÁA)	Útie_last_hidden_statesc               ó,   a€ V3R lpV e	   V! V 4      # V# )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   <a € \        S 4      V V3R  l4       pV# )c                óÄ  <€ VP                  R \        V P                  R R4      4      p\        P	                  \        V P                  4      4      ;'       g    / pV Uu/ uF4  pRV 2VP	                  RV 2\        V P                  RV 2R4      4      bK6  	  ppRV9   d+   VP	                  R\        V P                  RR4      4      VR&   RV9   d+   VP	                  R\        V P                  RR4      4      VR&   VP                  4        UUu/ uF"  w  rWV'       g   K  VP                  RR	4      . bK$  	  pppR
V9   d>   \        VR
,          \        \        \        34      '       d   \        VR
,          4      VR&   \        V4      ^ 8”  d   \        V 4       \        P                  V4      p	 S! V .VO5/ VB p
\        P!                  V	4       VP                  RR4      pV F¼  pVR8X  d    S'       d   Ve   WŒ,          R,          f   M�\#        V
R4      '       d0   WŒ,          RR WŒ&   WŒ,          P%                  V
P&                  4       M@\#        V
R4      '       d/   WŒ,          RR WŒ&   WŒ,          P%                  V
P(                  4       \        WŒ,          4      W¬&   K¾  	  VRJ d   V
P+                  4       p
V
# u upi u uppi   \        P!                  T	4       i ; i)Úreturn_dictTÚoutput_FÚcross_attentionsÚoutput_attentionsÚoutput_cross_attentionsÚmask_decoder_attentionsÚoutput_mask_decoder_attentionsrl   Úoutput_hidden_statesrF   NrG   Úvision_hidden_statesÚlast_hidden_stateéÿÿÿÿ)Úpoprw   Úconfigrn   r/   r?   ro   rp   ÚreplacerL   rq   rM   r2   rJ   ry   rH   r6   ÚhasattrrK   r†   r‡   Úto_tuple)r)   rN   Úkwargsr~   Úcapturable_flagsÚkÚrecordable_keysÚvrP   Úoutput_tokenÚoutputsrQ   r>   Úfuncrz   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Ð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ð &¨Ô8¼ZØÐ 6Õ7¼$ÄÄsÐ9K÷>ò >ô >AÀÐQgÕAhÓ=iÐ!Ð"9Ñ:äÐ$Ó%¨Ô)Ü-¨dÔ3ä,×0Ñ0Ð1BÓCˆLð6Ù˜tÐ5 dÒ5¨fÑ5�ô "×'Ñ'¨Ô5à#4×#8Ñ#8Ð9PÐRVÓ#WÐ ã(�Ø˜/Ô)ç1Ø,Ò8Ð=NÕ=SÐTVÕ=WÒ=_àÜ  Ð*@×AÒAØ1BÕ1GÈÈÐ1LÐ)Ñ.Ø)Õ.×5Ñ5°g×6RÑ6RÕSÜ  Ð*=×>Ò>Ø1BÕ1GÈÈÐ1LÐ)Ñ.Ø)Õ.×5Ñ5°g×6OÑ6OÔPä$Ð%6Õ%;Ó<�“ñ )ð  ˜eÓ#Ø!×*Ñ*Ó,�àˆNùòqùó !høô$ "×'Ñ'¨Õ5ús   Á:J=ÄKÄ!KÆ0K ËKr   )r•   r–   rz   s   f €r!   Ú
wrapped_fnÚ#capture_outputs.<locals>.wrapped_fnø   s!   ù€ Ü	ˆt‹õ>	ó 
ð>	ð@ ˆr    r   )r•   rz   r˜   s   &d r!   Úcapture_outputsrš   ê   s#   ø€ õBðH ÒÙ˜$ÓÐØÐr    )Útorch)Tr.   )r   Ú
__future__r   Ú	threadingÚcontextvarsr   Údataclassesr   Ú	functoolsr   Útypingr   Úimport_utilsr   r	   r›   r
   r^   r   rn   r   r#   rH   rV   r`   ra   ÚLockrx   ry   rš   r   r    r!   Ú<module>r¤      s§   ðñõ
 #ã Ý "Ý !Ý Ý  ç <÷ Ýå0ð Ð ð Ù	�:Ô÷.ð .ó ó ð.÷&!*ñ !*ñJ *Ð*<Ó=Ð ÷&8õR'n÷T>ð: $Ÿ.š.Ó*Ð õ2ñ&T¸÷ Tr    