§
    ŠŠtjw#  ã                   óh  — d Z ddlZddlZddlmZ ddlmZmZmZ ddl	m
Z
mZ ddlZddlmc mZ 	 ddlZn# e$ r dZY nw xY w ed¦  «        Z ed¦  «        Zer e
de¬	¦  «        d
efd„¦   «         Znd
efd„Zdeeef         d
eeef         fd„Zdedej        dz  f         deded
ej        fd„Zdeeef         d
eeef         fd„Z G d„ d¦  «        Zdej        j        j         de!ej                 ded
ej        e"ej        df         z  fd„Z#ded
e!e         fd„Z$dej        d
e%fd„Z& G d„ d¦  «        Z'ded ed!e(ded
ef
d"„Z)d#ed$eded ed%e(d&e(d
efd'„Z*deeef         d
eeef         fd(„Z+d)ed*eeef         d
eeef         fd+„Z,dej        e%z  d
e%fd,„Z-	 d9d-ej        d.ej        dz  d/ej        ez  d0edz  d
ej        dz  f
d1„Z.deeef         d2ed
eeef         fd3„Z/d4e"edf         d5e!e         d
e"edf         fd6„Z0d4e"edf         d5e!e         d7e"edf         d
e"edf         fd8„Z1dS ):a«  
This module contains utility functions that are explicitly allowed to be called during
TorchDynamo compilation. These functions are carefully vetted to ensure they work
correctly within the TorchDynamo tracing and compilation process.

Key functionality groups:

- Compilation State:
  Functions for checking compilation state (is_compiling)

- Function Wrapping:
  Utilities for wrapping functions (wrap_inline, wrap_numpy) to work with
  TorchDynamo compilation

- Autograd Hooks:
  Functions and classes for handling autograd hooks and backward passes
  (call_hook, FakeBackwardCFunction, etc.)

- Tensor Operations:
  Utility functions for tensor operations and transformations
é    N)ÚCallable)ÚAnyÚTYPE_CHECKINGÚTypeVar)Ú
deprecatedÚ	ParamSpecÚ_PÚ_Rze`torch._dynamo.external_utils.is_compiling` is deprecated. Use `torch.compiler.is_compiling` instead.)ÚcategoryÚreturnc                  ó>   — t           j                             ¦   «         S ©N©ÚtorchÚcompilerÚis_compiling© ó    úZ/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/_dynamo/external_utils.pyr   r   ,   s   € õ
 Œ~×*Ò*Ñ,Ô,Ð,r   c                  ó>   — t           j                             ¦   «         S )zd
        Indicates whether we are tracing/compiling with torch.compile() or torch.export().
        r   r   r   r   r   r   5   s   € õ Œ~×*Ò*Ñ,Ô,Ð,r   Úfnc                 ó†   ‡ — t          j        ‰ ¦  «        dt          j        dt          j        dt
          fˆ fd„¦   «         }|S )zC
    Create an extra frame around fn that is not in skipfiles.
    ÚargsÚkwargsr   c                  ó   •—  ‰| i |¤ŽS r   r   ©r   r   r   s     €r   Úinnerzwrap_inline.<locals>.innerC   s   ø€ àˆr�4Ð"˜6Ð"Ð"Ð"r   ©Ú	functoolsÚwrapsr	   r   r   r
   )r   r   s   ` r   Úwrap_inliner!   >   sX   ø€ õ
 „_�RÑÔð#•R”Wð #­¬	ð #µbð #ð #ð #ð #ð #ñ Ôð#ð €Lr   Úhook.r   r   c                 ót   —  | |Ž }|€|d         S |                      d¦  «        dk    rt          d¦  «        ‚|S )zB
    Used by compiled autograd to handle hook returning None.
    Nr   Ú	hook_typeÚpost_acc_grad_hookz5Tensor post accumulate grad hooks should return None.)ÚgetÚRuntimeError)r"   r   r   Úresults       r   Ú	call_hookr)   J   sI   € ð ˆT�4ˆ[€FØ€~Ø�AŒwˆØ	�Š�KÑ	 Ô	 Ð$8Ò	8Ð	8ÝÐRÑSÔSÐSØ€Mr   Úfc                 ó¢   ‡ — t           s‰ S t          j        ‰ ¦  «        dt          j        dt          j        dt          j        fˆ fd„¦   «         }|S )z�Decorator that turns a function from ``np.ndarray``s to ``np.ndarray``s into a function
    from ``torch.Tensor``s to ``torch.Tensor``s.
    r   r   r   c                  ó¢   •— t          j        t          j        d„ | |f¦  «        \  } } ‰| i |¤Ž}t          j        t          j        d„ |¦  «        S )Nc                 ó*   — |                       ¦   «         S r   )Únumpy©Úxs    r   ú<lambda>z*wrap_numpy.<locals>.wrap.<locals>.<lambda>b   s   €  A§G¢G¡I¤I€ r   c                 ó*   — t          j        | ¦  «        S r   )r   Ú	as_tensorr/   s    r   r1   z*wrap_numpy.<locals>.wrap.<locals>.<lambda>f   s   € ½%¼/È!Ñ:LÔ:L€ r   )ÚpytreeÚtree_map_onlyr   ÚTensorÚnpÚndarray)r   r   Úoutr*   s      €r   Úwrapzwrap_numpy.<locals>.wrap_   s[   ø€ åÔ+ÝŒLÐ-Ð-°°f¨~ñ
ô 
‰ˆˆfð ˆa�Ð ˜Ð Ð ˆåÔ#¥B¤JÐ0LÐ0LÈcÑRÔRÐRr   )r7   r   r    r	   r   r   r4   ÚPyTree)r*   r:   s   ` r   Ú
wrap_numpyr<   X   sp   ø€ õ ð Øˆå„_�QÑÔðS•B”Gð S¥r¤yð SµV´]ð Sð Sð Sð Sð Sñ ÔðSð €Kr   c                   óf   — e Zd Zdej        j        j        deej                 ddfd„Z	de
defd„ZdS )ÚFakeBackwardCFunctionÚrealÚsaved_tensorsr   Nc                 ó"   — || _         || _        d S r   )r?   r@   )Úselfr?   r@   s      r   Ú__init__zFakeBackwardCFunction.__init__l   s   € ð
 ˆŒ	Ø*ˆÔÐÐr   Únamec                 óz   — |dk    r!t          j        dt          ¦  «         | j        S t	          | j        |¦  «        S )NÚsaved_variablesz4'saved_variables' is deprecated; use 'saved_tensors')ÚwarningsÚwarnÚDeprecationWarningr@   Úgetattrr?   )rB   rD   s     r   Ú__getattr__z!FakeBackwardCFunction.__getattr__t   sE   € ØÐ$Ò$Ð$ÝŒMØFÝ"ñô ð ð Ô%Ð%å�t”y $Ñ'Ô'Ð'r   )Ú__name__Ú
__module__Ú__qualname__r   ÚautogradÚfunctionÚBackwardCFunctionÚlistr6   rC   Ústrr   rK   r   r   r   r>   r>   k   su   € € € € € ð+àŒnÔ%Ô7ð+ð ˜EœLÔ)ð+ð 
ð	+ð +ð +ð +ð( ð (¨ð (ð (ð (ð (ð (ð (r   r>   Úbackward_c_functionr@   c                 ó|   — t          | |¦  «        } |j        j        |g|¢R Ž }t          |t          ¦  «        s|f}|S r   )r>   Ú_forward_clsÚbackwardÚ
isinstanceÚtuple)rT   r@   r   ÚfakeÚgradss        r   Úcall_backwardr\      sO   € õ
 !Ð!4°mÑDÔD€DØ&ˆDÔÔ& tÐ3¨dÐ3Ð3Ð3€Eå�e�UÑ#Ô#ð Ø�ˆà€Lr   r0   c                 ó~   — t          | t          ¦  «        rt          | ¦  «        S t          | t          ¦  «        r| S | gS r   )rX   rY   rR   r/   s    r   Únormalize_as_listr^   �   s=   € Ý�!•UÑÔð Ý�A‰wŒwˆÝ	�A•tÑ	Ô	ð ØˆØˆ3€Jr   c                 óN   — |                       ¦   «                              ¦   «         S r   )Úuntyped_storageÚsizer/   s    r   Úuntyped_storage_sizerb   •   s    € Ø×ÒÑÔ×#Ò#Ñ%Ô%Ð%r   c                   ó²   — e Zd Zedeeg df                  deg df         ddfd„¦   «         Zedeeg df                  ddfd„¦   «         Zedd„¦   «         ZdS )	ÚFakeCompiledAutogradEngineÚfinal_callbacksNÚcbr   c                 ó0   — |                       |¦  «         d S r   )Úappend)re   rf   s     r   Úqueue_callbackz)FakeCompiledAutogradEngine.queue_callbackš   s   € ð 	×Ò˜rÑ"Ô"Ð"Ð"Ð"r   c                 ó¬   — d}|t          | ¦  «        k     r*| |         } |¦   «          |dz  }|t          | ¦  «        k     °*|                      ¦   «          d S )Nr   é   )ÚlenÚclear)re   Úirf   s      r   Úexec_final_callbacksz/FakeCompiledAutogradEngine.exec_final_callbacks    sg   € àˆØ•#�oÑ&Ô&Ò&Ð&Ø  Ô#ˆBØˆB‰DŒDˆDØ�‰FˆAð •#�oÑ&Ô&Ò&Ð&ð 	×ÒÑÔÐÐÐr   c                  ó   — d S r   r   r   r   r   Ú_exec_final_callbacks_stubz5FakeCompiledAutogradEngine._exec_final_callbacks_stub©   s   € àˆr   )r   N)	rL   rM   rN   ÚstaticmethodrR   r   ri   ro   rq   r   r   r   rd   rd   ™   s½   € € € € € Øð#Ø˜h r¨4 xÔ0Ô1ð#Ø7?ÀÀDÀÔ7Ið#à	ð#ð #ð #ñ „\ð#ð
 ð ¨d°8¸BÀ¸HÔ3EÔ.Fð  È4ð  ð  ð  ñ „\ð ð ðð ð ñ „\ðð ð r   rd   Úbw_stateÚ	hook_namec                 ó.   —  t          | |¦  «        |i |¤ŽS r   ©rJ   )rs   rt   r   r   s       r   Úcall_hook_from_backward_staterw   ®   s#   € ð (�7�8˜YÑ'Ô'¨Ð8°Ð8Ð8Ð8r   Ú_r(   Ú
hooks_nameÚmodule_namec                ól   — t          ||¦  «        }t          ||¦  «        }|D ]} |||g|¢R Ž }	|	�|	}Œ|S r   rv   )
rx   r(   rs   ry   rz   r   ÚmoduleÚhooksr"   Ú
new_results
             r   Ú%call_module_hooks_from_backward_stater   ´   s[   € õ �X˜{Ñ+Ô+€FÝ�H˜jÑ)Ô)€EØð  ð  ˆØ�T˜& &Ð0¨4Ð0Ð0Ð0ˆ
ØÐ!ØˆFøØ€Mr   c                 ó†   ‡ — t          j        ‰ ¦  «        dt          j        dt          j        dt
          fˆ fd„¦   «         }|S )Nr   r   r   c                  ón   •— t           j                             ¦   «         rt          d¦  «        ‚ ‰| i |¤ŽS )NzHNon-recursive torch.compiler.disable is not supported with torch.export.)r   r   Úis_exportingr'   r   s     €r   Únonrecursive_disable_wrapperzFget_nonrecursive_disable_wrapper.<locals>.nonrecursive_disable_wrapperÄ   sE   ø€ åŒ>×&Ò&Ñ(Ô(ð 	ÝØZñô ð ð ˆr�4Ð"˜6Ð"Ð"Ð"r   r   )r   rƒ   s   ` r   Ú get_nonrecursive_disable_wrapperr„   Á   sY   ø€ õ „_�RÑÔð#­B¬Gð #½r¼yð #ÍRð #ð #ð #ð #ð #ñ Ôð#ð (Ð'r   rB   Úfuncc                 óV   ‡ ‡— dt           j        dt           j        dt          fˆˆ fd„}|S )z;
    Apply self as a ctx manager around a call to func
    r   r   r   c                  óL   •— ‰5   ‰| i |¤Žcd d d ¦  «         S # 1 swxY w Y   d S r   r   )r   r   r…   rB   s     €€r   r   z+wrap_dunder_call_ctx_manager.<locals>.innerÕ   s   ø€ Øð 	)ð 	)Ø�4˜Ð( Ð(Ð(ð	)ð 	)ð 	)ð 	)ñ 	)ô 	)ð 	)ð 	)ð 	)ð 	)ð 	)ð 	)øøøð 	)ð 	)ð 	)ð 	)ð 	)ð 	)s   „™ ©r	   r   r   r
   )rB   r…   r   s   `` r   Úwrap_dunder_call_ctx_managerr‰   Ï   sG   øø€ ð)•R”Wð )­¬	ð )µbð )ð )ð )ð )ð )ð )ð )ð €Lr   c                 ód   — t          | t          j        ¦  «        r|                      d¦  «        S | S )Nrk   )rX   r   r6   ra   r/   s    r   Úunwrap_maybe_dynamic_intr‹   Þ   s+   € Ý�!•U”\Ñ"Ô"ð à�vŠv�a‰yŒyÐØ€Hr   ÚvariableÚvariable_gradÚgradÚhas_post_hooksc                 ó¸   — |€t          |¦  «        }|}| j        }t          j        j        j                             |g| ||¦  «        }|d         | _        | j        S )Nr   )ÚboolrŽ   r   Ú_dynamoÚcompiled_autogradÚopsÚAccumulateGrad)rŒ   r�   rŽ   r�   Úupdated_grads        r   Úcall_accumulate_gradr—   å   s`   € ð Ðõ ˜d™œˆØˆØ œˆÝ”=Ô2Ô6×EÒEØ	ˆ�˜-¨ñô €Lð ! ”O€H„MØŒ=Ðr   Úerror_on_graph_breakc                 ó¢   ‡ — |r&dt           j        dt           j        dt          fˆ fd„}n%dt           j        dt           j        dt          fˆ fd„}|S )Nr   r   r   c                  ó†   •— t           j                             d¦  «        5   ‰| i |¤Žcd d d ¦  «         S # 1 swxY w Y   d S )NT©r   r’   r˜   r   s     €r   Úwrapperz6wrap_inline_with_error_on_graph_break.<locals>.wrapper   s�   ø€ Ý”×3Ò3°DÑ9Ô9ð +ð +Ø�r˜4Ð* 6Ð*Ð*ð+ð +ð +ð +ñ +ô +ð +ð +ð +ð +ð +ð +øøøð +ð +ð +ð +ð +ð +ó   ¡6¶:½:c                  ó†   •— t           j                             d¦  «        5   ‰| i |¤Žcd d d ¦  «         S # 1 swxY w Y   d S )NFr›   r   s     €r   rœ   z6wrap_inline_with_error_on_graph_break.<locals>.wrapper  s�   ø€ Ý”×3Ò3°EÑ:Ô:ð +ð +Ø�r˜4Ð* 6Ð*Ð*ð+ð +ð +ð +ñ +ô +ð +ð +ð +ð +ð +ð +øøøð +ð +ð +ð +ð +ð +r�   rˆ   )r   r˜   rœ   s   `  r   Ú%wrap_inline_with_error_on_graph_breakrŸ   ø   s‡   ø€ ð ð 
+ð	+�2œ7ð 	+­b¬ið 	+½Bð 	+ð 	+ð 	+ð 	+ð 	+ð 	+ð 	+ð	+�2œ7ð 	+­b¬ið 	+½Bð 	+ð 	+ð 	+ð 	+ð 	+ð 	+ð €Nr   ÚtupÚmasksc                 óˆ   — g }t          |¦  «        D ]"\  }}|s|                     | |         ¦  «         Œ#t          |¦  «        S )zˆ
    masks is a list of bools, where True means the corresponding element in tup
    is a const value. Filter out the const values.
    ©Ú	enumeraterh   rY   )r    r¡   r9   Úmask_idxÚmasks        r   Úfilter_out_const_valuesr§     sO   € ð
 €CÝ# EÑ*Ô*ð &ð &‰ˆ�$Øð 	&Ø�JŠJ�s˜8”}Ñ%Ô%Ð%øÝ�‰:Œ:Ðr   Úvaluesc                 óÎ   — g }d}t          |¦  «        D ]C\  }}|r|                     ||         ¦  «         Œ#|                     | |         ¦  «         |dz  }ŒDt          |¦  «        S )zw
    masks and values are of same length. For indices where the mask is True, use
    the const_values to fill in.
    r   rk   r£   )r    r¡   r¨   r9   Úidxr¥   r¦   s          r   Úinsert_const_values_with_maskr«     sx   € ð €CØ
€CÝ# EÑ*Ô*ð ð ‰ˆ�$Øð 	Ø�JŠJ�v˜hÔ'Ñ(Ô(Ð(Ð(à�JŠJ�s˜3”xÑ Ô Ð Ø�1‰HˆCˆCÝ�‰:Œ:Ðr   r   )2Ú__doc__r   rG   Úcollections.abcr   Útypingr   r   r   Útyping_extensionsr   r   r   Útorch.utils._pytreeÚutilsÚ_pytreer4   r.   r7   ÚModuleNotFoundErrorr	   r
   ÚFutureWarningr‘   r   r!   r6   r)   r<   r>   rO   rP   rQ   rR   rY   r\   r^   Úintrb   rd   rS   rw   r   r„   r‰   r‹   r—   rŸ   r§   r«   r   r   r   ú<module>r¶      sR  ððð ð, Ð Ð Ð Ø €€€Ø $Ð $Ð $Ð $Ð $Ð $Ø .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3à €€€Ø $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ð $ðØÐÐÐÐøØð ð ð Ø	€B€B€Bðøøøð €Yˆt�_„_€Ø€WˆT�]„]€àð -ð €ZØoØðñ ô ð-˜$ð -ð -ð -ñ	ô ð-ð -ð
-˜$ð -ð -ð -ð -ð	�H˜R ˜VÔ$ð 	¨°"°b°&Ô)9ð 	ð 	ð 	ð 	ðØ
�3˜œ tÑ+Ð+Ô
,ðØ58ðØDGðà
„\ðð ð ð ð�(˜2˜r˜6Ô"ð  x°°B°Ô'7ð ð ð ð ð&(ð (ð (ð (ð (ñ (ô (ð (ð(ØœÔ0ÔBðà˜œÔ%ðð ðð „\�E˜%œ,¨Ð+Ô,Ñ,ð	ð ð ð ð˜ð   c¤ð ð ð ð ð&˜EœLð &¨Sð &ð &ð &ð &ðð ð ð ð ñ ô ð ð*9Øð9Øð9Ø*-ð9Ø9<ð9àð9ð 9ð 9ð 9ð	Ø
ð	Øð	Ø #ð	Ø/2ð	Ø@Cð	ØRUð	àð	ð 	ð 	ð 	ð(¨°"°b°&Ô)9ð (¸hÀrÈ2ÀvÔ>Nð (ð (ð (ð (ð
 sð 
°(¸2¸r¸6Ô2Bð 
ÀxÐPRÐTVÐPVÔGWð 
ð 
ð 
ð 
ð ¤¨sÑ 2ð °sð ð ð ð ð #'ð	ð ØŒlðà”< $Ñ&ðð Œ,˜Ñ
ðð ˜4‘Kð	ð
 „\�DÑðð ð ð ð&Ø��R�ÔðØ04ðàˆb�"ˆfÔðð ð ð ð*	  s¨C x¤ð 	¸¸d¼ð 	ÈÈcÐSVÈhÌð 	ð 	ð 	ð 	ðØ	ˆs�CˆxŒðØ!% d¤ðØ5:¸3À¸8´_ðà
ˆ3�ˆ8„_ðð ð ð ð ð s   ±6 ¶A ¿A 