§
    ‚Štj€1  ã                  ó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   © ó    úa/var/www/html/CA-Chatbot/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                ó¬  •— t                                ¦   «         }|�‰|                     ¦   «         vrd S ‰r@‰dk    r:t          |‰         ¦  «        dk    r!|‰                              |d         ¦  «         t          |t          ¦  «        s|‰                              |¦  «         d S |‰         �#|‰                              |‰         ¦  «         d S d S )NÚhidden_statesr   )Ú_active_collectorr0   ÚkeysÚlenÚappendÚ
isinstanceÚtuple)r8   ÚargsÚoutputÚcollected_outputsr   r   r:   s       €€€r#   Úoutput_capturing_hookz;install_output_capuring_hook.<locals>.output_capturing_hooki   sæ   ø€ å-×1Ò1Ñ3Ô3ÐàÐ$¨Ð3D×3IÒ3IÑ3KÔ3KÐ(KÐ(KØˆFà'ð 	3¨C°?Ò,BÐ,BÅsÐK\Ð]`ÔKaÑGbÔGbÐfgÒGgÐGgØ˜cÔ"×)Ò)¨$¨q¬'Ñ2Ô2Ð2Ý˜&¥%Ñ(Ô(ð 	9Ø˜cÔ"×)Ò)¨&Ñ1Ô1Ð1Ð1Ð1Ø�EŒ]Ð&Ø˜cÔ"×)Ò)¨&°¬-Ñ8Ô8Ð8Ð8Ð8ð 'Ð&r"   N)Úregister_forward_hook)r8   r:   r   r   rJ   s    ``` r#   Úinstall_output_capuring_hookrL   d   sE   øøø€ ð
9ð 9ð 9ð 9ð 9ð 9ð 9ð × Ò Ð!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_childrenrE   Úrecursively_install_hooksÚ"install_all_output_capturing_hooksr   r   Úendswithr   ÚstriprL   r   r   )rM   rN   rO   r   r,   r8   r:   ÚspecsÚmatch_target_classÚmatch_class_nameÚtarget_layer_nameÚmatchess               r#   rV   rV   z   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   rS   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.
    r@   r   r	   N)r   r   r   Ú Ú!_output_capturing_hooks_installedT)Ú_CAN_RECORD_REGISTRYr0   r;   Ú	__class__ÚitemsrE   Úlistr   rD   rV   Úsetattr)
r_   rS   Úcapture_flagsrO   r:   Úlayer_specsrZ   r   r   r   s
             r#   rW   rW   ¤   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.
    rb   FN)ÚgetattrÚ_hook_installation_lockrW   )r_   s    r#   Úmaybe_install_capturing_hooksrm   Ä   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                ój  •‡ ‡— ‰                      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 ¦   «         }t          |¦  «        dk    rt          ‰ ¦  «         t                               |¦  «        }	  ‰
‰ g|¢R i ‰¤Ž}t                               |¦  «         n# t                               |¦  «         w xY w|D ]ª}	|	dk    rŠ‰s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   rk   Ú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 )rt   ra   )Úreplace)rv   rw   Úvs      r#   ry   zHcapture_outputs.<locals>.wrapped_fn.<locals>.wrapper.<locals>.<dictcomp>ý   s2   € Ð gÐ gÐ gÁ$À!ÀQÐefÐ g §¢¨9°bÑ!9Ô!9¸2Ð gÐ gÐ gr"   r   r@   Úvision_hidden_stateséÿÿÿÿÚlast_hidden_state)Úpoprk   ru   rc   r0   r;   rd   re   rC   rm   rA   r2   r5   ÚhasattrrD   r‚   r„   rF   Úto_tuple)r+   rG   rx   rr   Úcapturable_flagsÚrecordable_keysrI   Úoutput_tokenÚoutputsr:   Úfuncrn   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ÐåÐ$Ñ%Ô%¨Ò)Ð)Ý-¨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øøøð )ð =ð =�Ø˜/Ò)Ð)Ø1ð QØÝ  Ð*@Ñ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   Ä(E ÅE+r   )rŒ   r�   rn   s   ` €r#   Ú
wrapped_fnz#capture_outputs.<locals>.wrapped_fnæ   s:   øø€ Ý	ˆt‰Œð4	ð 4	ð 4	ð 4	ð 4	ñ 
Œð4	ðl ˆr"   r!   )rŒ   rn   rŽ   s    ` r#   Úcapture_outputsr�   ×   s;   ø€ ð8ð 8ð 8ð 8ð 8ðt ÐØˆz˜$ÑÔÐØÐr"   )T)
r8   r9   r:   r;   r   r   r   r   r<   r=   )rM   r9   rN   r;   rO   rP   r<   r=   r/   )r_   r   rS   r   r<   r=   )r_   r   r<   r=   )r   Ú
__future__r   Ú	threadingÚcontextvarsr   Údataclassesr   Ú	functoolsr   Útypingr   Úimport_utilsr
   r   r   r   rT   r   rc   r   r%   rA   rL   rV   rW   ÚLockrl   rm   r�   r!   r"   r#   ú<module>r˜      sø  ððð ð
 #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "Ø !Ð !Ð !Ð !Ð !Ð !Ø Ð Ð Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  à <Ð <Ð <Ð <Ð <Ð <Ð <Ð <ð ð 1ØÐÐÐÐÐà0Ð0Ð0Ð0Ð0Ð0ð Ð ð Ø	€�:ÐÑÔð.ð .ð .ð .ð .ñ .ô .ñ Ôñ „ð.ð&!*ð !*ð !*ð !*ð !*ñ !*ô !*ð !*ðJ *Ð)Ð*<Ñ=Ô=Ð ð SWð8ð 8ð 8ð 8ð 8ð,'nð 'nð 'nð 'nðT>ð >ð >ð >ð >ð: )˜)œ.Ñ*Ô*Ð ð2ð 2ð 2ð 2ð&K¸ð Kð Kð Kð Kð Kð Kð Kr"   