§
    ŠŠtj4  ã                   óZ  — d dl Z d dlmZ d dlmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZmZ d dlmZ d dlmZ  G d„ de¦  «        Z e¦   «         Z e  !                    e¦  «        d„ ¦   «         Z" ee d¬¦  «        d„ ¦   «         Z#e j$        d„ ¦   «         Z% e  !                    ej&        ¦  «         ee d¬¦  «        ¦  «         e  !                    ej'        ¦  «        d„ ¦   «         Z(d„ Z)ed„ ¦   «         Z*d„ Z+d„ Z,de-de-fd„Z.d„ Z/d„ Z0d„ Z1dS ) é    N)Úcontextmanager)Úwraps)ÚDispatchKey)Ú+_maybe_find_pre_dispatch_tf_mode_for_export)Ú_ConstantFunctionÚ
flat_applyÚto_graphable©Ústrict_mode)Úautograd_not_implementedÚregister_fake)ÚHigherOrderOperator)ÚPreDispatchTorchFunctionModeÚProxyTorchDispatchModeÚtrack_tensor_tree)Ú_pytree)Ú"is_traceable_wrapper_subclass_typec                   ó(   ‡ — e Zd Zˆ fd„Zˆ fd„Zˆ xZS )ÚExportTracepointc                 óJ   •— t          ¦   «                              d¦  «         d S )NÚ_export_tracepoint)ÚsuperÚ__init__)ÚselfÚ	__class__s    €úT/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/_export/wrappers.pyr   zExportTracepoint.__init__   s"   ø€ Ý‰Œ×ÒÐ-Ñ.Ô.Ð.Ð.Ð.ó    c                 ó6   •—  t          ¦   «         j        |i |¤ŽS ©N)r   Ú__call__)r   ÚargsÚkwargsr   s      €r   r    zExportTracepoint.__call__   s   ø€ à�u‰wŒwÔ Ð0¨Ð0Ð0Ð0r   )Ú__name__Ú
__module__Ú__qualname__r   r    Ú__classcell__)r   s   @r   r   r      sQ   ø€ € € € € ð/ð /ð /ð /ð /ð1ð 1ð 1ð 1ð 1ð 1ð 1ð 1ð 1r   r   c                 ó¾   — t          j        | j        j        ||f¦  «        \  }}| j                             dt
          ||¦  «        }t          ||d | j        ¬¦  «        S )NÚcall_function©ÚconstantÚtracer)ÚpytreeÚtree_mapr+   Úunwrap_proxyÚcreate_proxyr   r   )Úmoder!   r"   Úp_argsÚp_kwargsÚproxys         r   Úexport_tracepoint_dispatch_moder4   '   s]   € å” t¤{Ô'?À$ÈÀÑPÔPÑ€FˆHØŒK×$Ò$ØÕ+¨V°Xñô €Eõ ˜T 5°4ÀÄÐLÑLÔLÐLr   T)Ú
skip_cachec                  ó   — | S r   © ©r!   r"   s     r   Ú"export_tracepoint_fake_tensor_moder9   0   ó   € à€Kr   c                 óÎ   — |                       |¦  «        }|                       |¦  «        }|                      ¦   «         5  t          |i |¤Ž |cd d d ¦  «         S # 1 swxY w Y   d S r   )Úunwrap_tensorsÚredispatch_to_nextr   )Úctxr!   r"   Úunwrapped_argsÚunwrapped_kwargss        r   Úexport_tracepoint_functionalrA   5   s¶   € à×'Ò'¨Ñ-Ô-€NØ×)Ò)¨&Ñ1Ô1Ðà	×	Ò	Ñ	!Ô	!ð ð Ý˜NÐ?Ð.>Ð?Ð?Ð?Øðð ð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð ð ð s   ¿AÁAÁ!A)Údeferred_errorc                  ó   — | S r   r7   r8   s     r   Úexport_tracepoint_cpurD   D   r:   r   c                 óˆ  ‡‡‡‡	— t          | t          j        j        ¦  «        st	          dt          | ¦  «        › �¦  «        ‚‰dk    rt	          d¦  «        ‚t          j        j                             | ‰¦  «        }ˆfd„Š	d„ Šˆˆfd„}ˆˆˆ	fd„}| 	                    |d¬	¦  «        }| 
                    |d¬	¦  «        }||fS )
Nzexpected torch.nn.Module, got Ú zpath must not be emptyc                 óø   •— | ‰v rl‰|          d         |k    r$t          d| › d‰|          d         › d|› �¦  «        ‚‰|          d         |k    r$t          d| › d‰|          d         › d|› �¦  «        ‚||dœ‰| <   d S )NÚin_speczin_spec mismatch for z: z != Úout_speczout_spec mismatch for )rH   rI   )ÚAssertionError)ÚpathrH   rI   Úmodule_call_specss      €r   Úupdate_module_call_signaturesz6_wrap_submodule.<locals>.update_module_call_signaturesP   sÈ   ø€ ØÐ$Ð$Ð$Ø  Ô& yÔ1°WÒ<Ð<Ý$Øe¨DÐeÐeÐ4EÀdÔ4KÈIÔ4VÐeÐeÐ\cÐeÐeñô ð ð ! Ô& zÔ2°hÒ>Ð>Ý$Øh¨TÐhÐhÐ5FÀtÔ5LÈZÔ5XÐhÐhÐ^fÐhÐhñô ð ð /6À8Ð"LÐ"LÐ˜$ÑÐÐr   c           	      óž   — | D ]I}t          |t          j        t          t          t
          t          f¦  «        s|�t          d|› �¦  «        ‚ŒJd S )NzGOnly Tensors or scalars are supported as pytree flattened inputs, got: )Ú
isinstanceÚtorchÚTensorÚstrÚintÚfloatÚboolrJ   )Ú	flat_argsÚas     r   Úcheck_flattenedz(_wrap_submodule.<locals>.check_flattened\   s]   € Øð 	ð 	ˆAÝ˜q¥5¤<µµc½5Å$Ð"GÑHÔHð ÈAÈIÝ$ØaÐ^_ÐaÐañô ð øð	ð 	r   c                 óž   •— t          j        ||f¦  «        \  }} ‰|¦  «         t          |d‰dœŽ}t          j        ||¦  «        \  }}||fS )NÚmodule_call_inputs©ÚkindrK   ©r,   Útree_flattenr   Útree_unflatten)Úmoduler!   r"   rV   rH   rX   rK   s        €€r   Úpre_hookz!_wrap_submodule.<locals>.pre_hookc   sb   ø€ Ý#Ô0°$¸°Ñ@Ô@Ñˆ	�7Øˆ˜	Ñ"Ô"Ð"Ý&¨	Ð8LÐSWÐXÐXÐXˆ	ÝÔ,¨Y¸Ñ@Ô@‰ˆˆfØ�Vˆ|Ðr   c                 óØ   •— t          j        ||f¦  «        \  }}t          j        |¦  «        \  }} ‰|¦  «         t          |d‰	dœŽ} ‰
‰	||¦  «         t          j        ||¦  «        S )NÚmodule_call_outputsr[   r]   )r`   r!   r"   ÚresÚ_rH   Úflat_resrI   rX   rK   rM   s           €€€r   Ú	post_hookz"_wrap_submodule.<locals>.post_hookj   s~   ø€ ÝÔ(¨$°¨Ñ8Ô8‰
ˆˆ7Ý#Ô0°Ñ5Ô5Ñˆ�(Øˆ˜Ñ!Ô!Ð!Ý% xÐ6KÐRVÐWÐWÐWˆØ%Ð% d¨G°XÑ>Ô>Ð>ÝÔ$ X¨xÑ8Ô8Ð8r   T)Úwith_kwargs)rO   rP   ÚnnÚModulerJ   ÚtypeÚfxÚgraph_moduleÚ	_get_attrÚregister_forward_pre_hookÚregister_forward_hook)
ÚmodrK   rL   Ú	submodulera   rg   Ú
pre_handleÚpost_handlerX   rM   s
    ``     @@r   Ú_wrap_submoduleru   I   s  øøøø€ Ý�c�5œ8œ?Ñ+Ô+ð KÝÐI½dÀ3¹i¼iÐIÐIÑJÔJÐJØˆr‚z€zÝÐ5Ñ6Ô6Ð6Ý”Ô%×/Ò/°°TÑ:Ô:€Ið
Mð 
Mð 
Mð 
Mð 
Mðð ð ðð ð ð ð ð ð9ð 9ð 9ð 9ð 9ð 9ð 9ð ×4Ò4°XÈ4Ð4ÑPÔP€JØ×1Ò1°)ÈÐ1ÑNÔN€KØ�{Ð"Ð"r   c              #   óØ   K  — g }	 |D ]&}|                      t          | ||¦  «        ¦  «         Œ'd V — |D ]}|                     ¦   «          Œd S # |D ]}|                     ¦   «          Œw xY wr   )Úextendru   Úremove)ÚfÚpreserve_signatureÚmodule_call_signaturesÚhandlesrK   Úhandles         r   Ú_wrap_submodulesr~   w   sš   è è € à€GðØ&ð 	Mð 	MˆDØ�NŠN�?¨1¨dÐ4JÑKÔKÑLÔLÐLÐLØˆˆˆàð 	ð 	ˆFØ�MŠM‰OŒOˆOˆOð	ð 	ø�gð 	ð 	ˆFØ�MŠM‰OŒOˆOˆOð	øøøs   †-A ÁA)c                 ó   — d„ }|| _         | S )Nc                 ó"   — t          | |¦  «        S r   r
   )r   r!   s     r   Úcallz'_mark_strict_experimental.<locals>.call…   s   € Ý˜4 Ñ&Ô&Ð&r   )r    )Úclsr�   s     r   Ú_mark_strict_experimentalrƒ   „   s   € ð'ð 'ð 'ð €C„LØ€Jr   c                 óB  — |dz   }t          | j        |¦  «        rCt          | j        |¦  «        |k    rt          d|› �¦  «        ‚|                      d|di ¦  «        S |                      |¦  «        }t          | j        ||¦  «         |                      d|di ¦  «        S )a  
    This is a wrapper utility method on top of tracer to cache the
    already registered subclass spec attribute. This is useful because
    Subclass.__init__ will be same for each subclass. By default, fx will
    create multiple attributes/proxies for given attribute.
    Ú0zspec mismatch for Úget_attrr7   )ÚhasattrÚrootÚgetattrrJ   r/   Úget_fresh_qualnameÚsetattr)r+   ÚnameÚspecÚfx_nameÚqualnames        r   Ú#_register_func_spec_proxy_in_tracerr�   Œ   s«   € ð �S‰j€GÝˆvŒ{˜GÑ$Ô$ð @Ý�6”; Ñ(Ô(¨DÒ0Ð0Ý Ð!?°gÐ!?Ð!?Ñ@Ô@Ð@Ø×"Ò" :¨w¸¸BÑ?Ô?Ð?à×(Ò(¨Ñ.Ô.€HÝˆFŒK˜ 4Ñ(Ô(Ð(Ø×Ò˜z¨8°R¸Ñ<Ô<Ð<r   Ú	spec_nameÚcall_spec_cache_keyc                 ó¾  — t          |¦  «        \  }}|                      |¦  «        }t          | j        ||¦  «         |                      d|di ¦  «        }	t          j        t          |¦  «        ¦  «        \  }
}t          | |› d�|¦  «        }t          j	        | j
        |¦  «        }|                      dt          ||	g|¢R i ¦  «        }t          ||d | ¬¦  «         d S )Nr†   r7   Ú_const_func_specr(   r)   )r	   rŠ   r‹   rˆ   r/   r,   r^   r   r�   r-   r.   r   r   )r+   r‘   Úconst_target_for_applyÚgraphable_argsÚtrack_valuer’   rV   rH   r�   Ú
spec_proxyre   Ú	func_specÚfunc_spec_proxyÚflat_proxy_argsÚ	out_proxys                  r   Ú_emit_flat_apply_callr�   ž   sù   € õ & nÑ5Ô5Ñ€IˆwØ×(Ò(¨Ñ3Ô3€HÝˆFŒK˜ 7Ñ+Ô+Ð+Ø×$Ò$ Z°¸2¸rÑBÔB€Jõ Ô&Õ'8Ð9OÑ'PÔ'PÑQÔQ�L€A€yÝ9ØÐ&Ð8Ð8Ð8¸)ñô €Oõ
 ”o fÔ&9¸9ÑEÔE€Oð ×#Ò#Ø� o°zÐ%TÀOÐ%TÐ%TÐVXñô €Iõ �k 9°tÀFÐKÑKÔKÐKÐKÐKr   c                 ó6   — t          | ¦  «        o
| j        dk    S )Nr   )Úcallabler#   )Úfns    r   Ú_is_initr¡   ½   s   € Ý�B‰<Œ<Ð5˜BœK¨:Ò5Ð5r   c                 ó`   ‡ — t          ‰ ¦  «        st          d‰ j        › d�¦  «        ‚ˆ fd„}|S )a*  
    Experimental decorator that makes subclass to be traceable in export
    with pre-dispatch IR. To make your subclass traceable in export, you need to:
        1. Implement __init__ method for your subclass (Look at DTensor implementation)
        2. Decorate your __init__ method with _mark_constructor_exportable_experimental
        3. Put torch._dynamo_disable decorator to prevent dynamo from peeking into its' impl

    Example:

    class FooTensor(torch.Tensor):
        @staticmethod
        def __new__(cls, elem, *, requires_grad=False):
            # ...
            return torch.Tensor._make_subclass(cls, elem, requires_grad=requires_grad)

        @torch._dynamo_disable
        @mark_subclass_constructor_exportable_experimental
        def __init__(self, elem, ...):
            # ...
    z‰torch._export.wrappers.mark_constructor_exportable_experimental can only be applied on subclass tensor.__init__But, you are adding it on z‡ which is not supported. If __init__ doesn't exist on your subclass, please add it. Look at DTensor.__init__ implementation for examplec                  óH  •—  ‰	| i |¤Ž t           j                             ¦   «         sd S t          t	          | d         ¦  «        ¦  «        sa‰	j                             d¦  «        st          d‰	j        › �¦  «        ‚‰	j        d t          d¦  «         …         }t          d|› d�¦  «        ‚t          ¦   «         }|€d S t          |t          ¦  «        st          dt	          |¦  «        › �¦  «        ‚|j        }| d         }t          | dd …         ¦  «        |f}d                     ‰	j                             ¦   «                              d	¦  «        ¦  «        }t	          |¦  «        j                             ¦   «         }t'          ||t	          |¦  «        |||¬
¦  «         d S )Nr   r   z2expected __qualname__ to end with '__init__', got zCan't intercept zœ in export because this object is not a traceable tensor subclass. Please look at DTensor.__init__ implementation as an example of proper usage of this API.z+expected PreDispatchTorchFunctionMode, got é   re   ú.©r+   r‘   r•   r–   r—   r’   )rP   ÚcompilerÚis_exportingr   rk   r%   ÚendswithrJ   ÚlenÚRuntimeErrorr   rO   r   r+   ÚtupleÚjoinÚlowerÚsplitr#   r�   )
r!   r"   Úobj_namer0   r+   ÚsubclassÚ	graphabler‘   r’   Úconstructor_subclasss
            €r   ÚwrapperzBmark_subclass_constructor_exportable_experimental.<locals>.wrapperÝ   sÉ  ø€ ØÐ˜dÐ- fÐ-Ð-Ð-åŒ~×*Ò*Ñ,Ô,ð 	ØˆFå1µ$°t¸A´w±-´-Ñ@Ô@ð 		Ø'Ô4×=Ò=¸jÑIÔIð Ý$ØlÐI]ÔIjÐlÐlñô ð ð ,Ô8Ð9K½CÀ
¹O¼OÐ;KÐ9KÔLˆHÝð~ 8ð ~ð ~ð ~ñô ð õ
 ;Ñ<Ô<ˆØˆ<ØˆFå˜$Õ <Ñ=Ô=ð 	Ý ØJ½dÀ4¹j¼jÐJÐJñô ð ð ”ˆØ˜”7ˆÝ˜4   œ8‘_”_ fÐ-ˆ	à—H’HÐ1Ô>×DÒDÑFÔF×LÒLÈSÑQÔQÑRÔRˆ	Ý" 8™nœnÔ5×;Ò;Ñ=Ô=ÐåØØÝ#'¨¡>¤>Ø$Ø Ø 3ð	
ñ 	
ô 	
ð 	
ð 	ˆr   )r¡   r«   r#   )r³   r´   s   ` r   Ú1mark_subclass_constructor_exportable_experimentalrµ   Á   sg   ø€ õ* Ð(Ñ)Ô)ð 
Ýð~Ø)=Ô)Fð~ð ~ð ~ñ
ô 
ð 	
ð)ð )ð )ð )ð )ðV €Nr   c                 óÜ   ‡ — t          ‰ ¦  «        rt          ‰ ¦  «        S t          ‰ ¦  «        s#‰ j        dk    st          d‰ j        › d�¦  «        ‚t	          ‰ ¦  «        ˆ fd„¦   «         }|S )aŸ  
    Experimental decorator that adds user function to export pre-dispatch graph. Note that
    we only support custom autograd function/subclass constructors today. To use this function:
        1. For subclasses:
            1. refer to instructions in mark_subclass_constructor_exportable_experimental
        2. Define apply method on your custom autograd function and apply this decorator.

    Example:

    class MyCoolCustomAutogradFunc(autograd.Function):
        @classmethod
        @torch._export.wrappers.allow_in_pre_dispatch_graph
        def apply(cls, *args, **kwargs):
            return super(MyCoolCustomAutogradFunc, cls).apply(*args, **kwargs)

    ÚapplyzŸtorch._export.wrappers.allow_in_pre_dispatch_graph can only be applied on subclass tensor.__init_ or custom_autograd_function.apply. But, you are adding it on a/   which is not supported. If __init__ doesn't exist on your subclass, please add it. Look at DTensor.__init__ implementation for example. If you are adding it on custom autograd function, please add it on apply method. If anything else, file an issue on github and we may consider extending our support. c                  ód  •— t           j                             ¦   «         s ‰| i |¤ŽS t          j        | d         ¦  «        s ‰| i |¤ŽS t          | d         t           j        j        ¦  «        s ‰| i |¤ŽS ddlm	}  |t           j
        j        j        ¦  «        }|€ ‰| i |¤ŽS t           j
                             ¦   «                              t           j
        j        j        ¦  «        }t           j
                             ¦   «         t           j
                             t           j
        j        j        ¦  «        z  }t           j
                             ||¦  «        5   ‰| i |¤Ž}d d d ¦  «         n# 1 swxY w Y   |j        st+          d¦  «        ‚|j        }| d         j        › d| d         j        › �}|g| dd …         ¢R |f}	ddlm}
 d                     |                     d¦  «        ¦  «        }t;          |
¦  «        j                             ¦   «         }tA          |||
|	||¬¦  «         |S )	Nr   )Ú_get_dispatch_mode_pre_dispatchz"Should only do this in predispatchr¥   r¤   )Ú._call_custom_autograd_function_in_pre_dispatchre   r¦   )!rP   r§   r¨   ÚinspectÚisclassÚ
issubclassÚautogradÚFunctionÚ
torch._opsr¹   Ú_CÚ_TorchDispatchModeKeyÚPROXYÚ_dispatch_tls_local_include_setrx   r   ÚPreDispatchÚ_dispatch_tls_local_exclude_setÚDispatchKeySetÚ_ForceDispatchKeyGuardÚpre_dispatchrJ   r+   r$   r%   Útorch.export.custom_opsrº   r­   r¯   rk   r#   r®   r�   )r!   r"   r¹   r0   Úinclude_to_setÚexclude_to_setÚoutr+   Úfunction_cls_namer²   rº   r‘   r’   Úfuncs                €r   r´   z,allow_in_pre_dispatch_graph.<locals>.wrapper)  s®  ø€ åŒ~×*Ò*Ñ,Ô,ð 	)Ø�4˜Ð( Ð(Ð(Ð(åŒ˜t AœwÑ'Ô'ð 	)Ø�4˜Ð( Ð(Ð(Ð(å˜$˜qœ'¥5¤>Ô#:Ñ;Ô;ð 	)Ø�4˜Ð( Ð(Ð(Ð(à>Ð>Ð>Ð>Ð>Ð>à.Ð.­u¬xÔ/MÔ/SÑTÔTˆØˆ<Ø�4˜Ð( Ð(Ð(Ð(õ œ×AÒAÑCÔC×JÒJÝŒHÔ Ô,ñ
ô 
ˆõ ŒH×4Ò4Ñ6Ô6ÝŒh×%Ò%¥e¤hÔ&:Ô&FÑGÔGñHð 	õ
 ŒX×,Ò,¨^¸^ÑLÔLð 	(ð 	(Ø�$˜Ð' Ð'Ð'ˆCð	(ð 	(ð 	(ñ 	(ô 	(ð 	(ð 	(ð 	(ð 	(ð 	(ð 	(øøøð 	(ð 	(ð 	(ð 	(ð Ô ð 	GÝ Ð!EÑFÔFÐFØ”ˆà# AœwÔ1ÐJÐJ°D¸´GÔ4HÐJÐJÐØ'Ð3¨$¨q¨r¨r¬(Ð3Ð3°VÐ<ˆ	ð	
ð 	
ð 	
ð 	
ð 	
ð 	
ð —H’HÐ.×4Ò4°SÑ9Ô9Ñ:Ô:ˆ	Ý"Ø:ñ
ô 
ä
—5’5‘7”7ð 	õ 	ØØØ#QØ$ØØ 3ð	
ñ 	
ô 	
ð 	
ð ˆ
s   Å	E2Å2E6Å9E6)r¡   rµ   r#   r«   r   )rÏ   r´   s   ` r   Úallow_in_pre_dispatch_graphrÐ     sž   ø€ õ" ��~„~ð GÝ@ÀÑFÔFÐFå�T‰NŒNð 
˜dœm¨wÒ6Ð6Ýðeà)-¬ðeð eð eñ
ô 
ð 	
õ ˆ4�[„[ð4ð 4ð 4ð 4ñ „[ð4ðl €Nr   )2r»   Ú
contextlibr   Ú	functoolsr   rP   Útorch._custom_opsÚtorch._Cr   Útorch._export.utilsr   Ú"torch._higher_order_ops.flat_applyr   r   r	   Ú#torch._higher_order_ops.strict_moder   Útorch._higher_order_ops.utilsr   r   rÀ   r   Ú"torch.fx.experimental.proxy_tensorr   r   r   Útorch.utilsr   r,   Útorch.utils._python_dispatchr   r   r   Úpy_implr4   r9   Úpy_functionalize_implrA   ÚAutogradÚCPUrD   ru   r~   rƒ   r�   rR   r�   r¡   rµ   rÐ   r7   r   r   ú<module>rà      s  ðà €€€Ø %Ð %Ð %Ð %Ð %Ð %Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  Ø KÐ KÐ KÐ KÐ KÐ Kðð ð ð ð ð ð ð ð ð ð
 <Ð ;Ð ;Ð ;Ð ;Ð ;Ø QÐ QÐ QÐ QÐ QÐ QÐ QÐ QØ *Ð *Ð *Ð *Ð *Ð *ðð ð ð ð ð ð ð ð ð ð
 *Ð )Ð )Ð )Ð )Ð )Ø KÐ KÐ KÐ KÐ KÐ Kð1ð 1ð 1ð 1ð 1Ð*ñ 1ô 1ð 1ð &Ð%Ñ'Ô'Ð ð ×ÒÐ2Ñ3Ô3ðMð Mñ 4Ô3ðMð €Ð!¨dÐ3Ñ3Ô3ðð ñ 4Ô3ðð Ô)ðð ñ *Ô)ðð 1Ð × Ò ˜;Ô/Ñ 0Ô 0ØÐÐ/ÀÐEÑEÔEñô ð ð
 ×Ò˜KœOÑ,Ô,ðð ñ -Ô,ðð+#ð +#ð +#ð\ ð	ð 	ñ „ð	ðð ð ð=ð =ð =ð$Lð ðLð ðLð Lð Lð Lð>6ð 6ð 6ðGð Gð GðTUð Uð Uð Uð Ur   