+
    QV-jƒ1  ã                  ó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   ó    Út/Volumes/fast/ai/experiments/ui-tars-smoke/.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                ó¨  <€ \         P                  4       pVe   SVP                  4       9  d   R # S'       d>   SR8X  d7   \        VS,          4      ^ 8X  d    VS,          P	                  V^ ,          4       \        V\        4      '       g   VS,          P	                  V4       R # VS,          e"   VS,          P	                  VS,          4       R # R # )NÚhidden_states)Ú_active_collectorr/   ÚkeysÚlenÚappendÚ
isinstanceÚtuple)r<   ÚargsÚoutputÚcollected_outputsr   r   r>   s   &&& €€€r!   Úoutput_capturing_hookÚ;install_output_capuring_hook.<locals>.output_capturing_hooki   s§   ø€ ä-×1Ñ1Ó3ÐàÒ$¨Ð3D×3IÑ3IÓ3KÔ(KÙç'¨C°?Ô,BÄsÐK\Ð]`ÕKaÓGbÐfgÔGgØ˜cÕ"×)Ñ)¨$¨q­'Ô2Ü˜&¤%×(Ò(Ø˜cÕ"×)Ñ)¨&Ö1Ø�E�]Ò&Ø˜cÕ"×)Ñ)¨&°­-Ö8ñ 'r    N)Úregister_forward_hook)r<   r>   r   r   rP   s   &fff r!   Úinstall_output_capuring_hookrS   d   s   ú€ ÷
9ð × Ñ Ð!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   z   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                  e   \        WP                  4      '       g4   VP                  f   K>  VP                  VP                  4      '       g   Ka  VP                  e   VP                  V9  d   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_childrenrK   Úrecursively_install_hooksÚ"install_all_output_capturing_hooksr   r   Úendswithr   rS   r   r   )rU   rV   rW   r   r*   r<   r>   Úspecss   &&&     r!   r]   r]   z   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ÙÜ(¨¸U¿[¹[È%×JlÑJlÖmó $r    c               ó$   € V ^8„  d   QhRRRRRR/# )r;   Úmodelr   rZ   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.
rF   N)r   r   r   Ú Ú!_output_capturing_hooks_installedT)Ú_CAN_RECORD_REGISTRYr/   r?   Ú	__class__ÚitemsrK   Úlistr   rJ   r]   Úsetattr)
rb   rZ   Úcapture_flagsrW   r>   Úlayer_specsr`   r   r   r   s
   &&        r!   r^   r^   š   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;   rb   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.
re   FN)ÚgetattrÚ_hook_installation_lockr^   )rb   s   &r!   Úmaybe_install_capturing_hooksrq   º   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                ó8  <€ 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\        V4      ^ 8”  d   \        V 4       \        P                  V4      p	 S! V .VO5/ VB p
\        P                  V	4       V EFK  pVR
8X  d    S'       g   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8X  dˆ   \'        WK,          \(        4      '       dV   \        WK,          4      ^8X  d@   \%        W‹,          R,          4      W«&   \%        W‹,          R,          4      V
RV,           &   EK"  \%        W‹,          4      W«&   EK8  \%        W‹,          4      W«&   EKN  	  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_attentionsrd   rF   Úvision_hidden_statesNÚlast_hidden_stateÚ
attentions:é    Nr;   :é   Nr;   Úcross_éÿÿÿÿ)Úpopro   Úconfigrf   r/   r?   rg   rh   ÚreplacerI   rq   rG   r2   r6   ÚhasattrrJ   r}   r~   rL   rK   ri   Úto_tuple)r)   rM   Úkwargsrv   Úcapturable_flagsÚkÚrecordable_keysÚvrO   Úoutput_tokenÚoutputsr>   Úfuncrr   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äÐ$Ó%¨Ô)Ü-¨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ÀDÕ-IÓ'J˜™Ü27Ð8IÕ8NÈtÕ8TÓ2U˜ ¨3¥Ô/ä',Ð->Õ-CÓ'D˜œä#(Ð):Õ)?Ó#@�G”Lñ) )ð, ˜eÓ#Ø!×*Ñ*Ó,�àˆNùòoùó !høô "×'Ñ'¨Õ5ús   Á:K7ÄK<Ä!K<Å,L ÌLr   )r�   r‘   rr   s   f €r!   Ú
wrapped_fnÚ#capture_outputs.<locals>.wrapped_fnÜ   s!   ù€ Ü	ˆt‹õ=	ó 
ð=	ð~ ˆr    r   )r�   rr   r“   s   &d r!   Úcapture_outputsr•   Í   s#   ø€ õAðF ÒÙ˜$ÓÐØÐr    )Útorch)Tr.   )r   Ú
__future__r   Ú	threadingÚcontextvarsr   Údataclassesr   Ú	functoolsr   Útypingr   Úimport_utilsr   r	   r–   r
   r[   r   rf   r   r#   rG   rS   r]   r^   ÚLockrp   rq   r•   r   r    r!   Ú<module>rŸ      s¦   ðñõ
 #ã Ý "Ý !Ý Ý  ç <÷ Ýå0ð Ð ð Ù	�:Ô÷.ð .ó ó ð.÷&!*ñ !*ñJ *Ð*<Ó=Ð ÷8õ,n÷@>ð: $Ÿ.š.Ó*Ð õ2ñ&T¸÷ Tr    