
    9i0                     4   S SK r S SKJr  S SKJr  S SKrS SKrS SKJr  S SK	J
r
  S SKJrJrJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJrJrJr  S SKJr  S SKJr   " S S\5      r \ " 5       r!\!RE                  \5      S 5       r#\!RE                  \5      S 5       r$\!RJ                  S 5       r&\!RE                  \RN                  5      " \" \!SS95        \!RE                  \RP                  5      S 5       r)S r*\S 5       r+S r,S r-S\.S\.4S jr/S r0S r1S r2g)     N)contextmanager)wraps)DispatchKey)+_maybe_find_pre_dispatch_tf_mode_for_export)_ConstantFunction
flat_applyto_graphablestrict_mode)autograd_not_implemented)HigherOrderOperator)FakeTensorMode)PreDispatchTorchFunctionModeProxyTorchDispatchModetrack_tensor_tree)_pytree)"is_traceable_wrapper_subclass_typec                   4   ^  \ rS rSrU 4S jrU 4S jrSrU =r$ )ExportTracepoint   c                 $   > [         TU ]  S5        g )N_export_tracepoint)super__init__)self	__class__s    V/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/_export/wrappers.pyr   ExportTracepoint.__init__   s    -.    c                 $   > [         TU ]  " U0 UD6$ N)r   __call__)r   argskwargsr   s      r   r"   ExportTracepoint.__call__    s    w000r    )__name__
__module____qualname____firstlineno__r   r"   __static_attributes____classcell__)r   s   @r   r   r      s    /1 1r   r   c                     [         R                  " U R                  R                  X45      u  p4U R                  R	                  S[
        X45      n[        XS U R                  S9$ )Ncall_functionconstanttracer)pytreetree_mapr1   unwrap_proxycreate_proxyr   r   )moder#   r$   p_argsp_kwargsproxys         r   export_tracepoint_dispatch_moder:   '   sR    t{{'?'?$PFKK$$+VE T4LLr   c                 @    U    UsS S S 5        $ ! , (       d  f       g = fr!   r&   )r6   r#   r$   s      r   "export_tracepoint_fake_tensor_moder<   0   s    	 
s   
c                     U R                  U5      nU R                  U5      nU R                  5          [        U0 UD6  UsS S S 5        $ ! , (       d  f       g = fr!   )unwrap_tensorsredispatch_to_nextr   )ctxr#   r$   unwrapped_argsunwrapped_kwargss        r   export_tracepoint_functionalrC   6   sL    ''-N))&1				!N?.>? 
"	!	!s   A


AT)deferred_errorc                      U $ r!   r&   )r#   r$   s     r   export_tracepoint_cpurF   E   s    Kr   c                 :  ^^^^	 [        U [        R                  R                  5      (       d   eTS:w  d   e[        R                  R
                  R                  U T5      nU4S jm	S mUU4S jnUUU	4S jnUR                  USS9nUR                  USS9nXg4$ )N c                 X   > U T;   a  TU    S   U:X  d   eTU    S   U:X  d   eXS.TU '   g )Nin_specout_spec)rJ   rK   r&   )pathrJ   rK   module_call_specss      r   update_module_call_signatures6_wrap_submodule.<locals>.update_module_call_signaturesO   sJ    $$$T*95@@@$T*:6(BBB.5"L$r   c           	          U  HJ  n[        U[        R                  [        [        [
        [        45      (       a  M9  Uc  M>  [        SU 35      e   g )NzGOnly Tensors or scalars are supported as pytree flattened inputs, got: )
isinstancetorchTensorstrintfloatboolAssertionError)	flat_argsas     r   check_flattened(_wrap_submodule.<locals>.check_flattenedU   sE    Aq5<<c5$"GHHAI$]^_]`a  r   c                    > [         R                  " X45      u  p4T" U5        [        USTS.6n[         R                  " X45      u  pX4$ )Nmodule_call_inputskindrL   r2   tree_flattenr   tree_unflatten)moduler#   r$   rY   rJ   r[   rL   s        r   pre_hook!_wrap_submodule.<locals>.pre_hook\   sJ    #00$@		"&	8LSWX	,,Y@|r   c                    > [         R                  " X45      u  pE[         R                  " U5      u  pgT" U5        [        UST	S.6nT
" T	XW5        [         R                  " Xg5      $ )Nmodule_call_outputsr_   ra   )rd   r#   r$   res_rJ   flat_resrK   r[   rL   rN   s           r   	post_hook"_wrap_submodule.<locals>.post_hookc   s]    (($8
#005!%x6KRVW%dG>$$X88r   T)with_kwargs)	rQ   rR   nnModulefxgraph_module	_get_attrregister_forward_pre_hookregister_forward_hook)
modrL   rM   	submodulere   rl   
pre_handlepost_handler[   rN   s
    ``     @@r   _wrap_submodulerz   J   s    c588??++++2::%%//T:IM9 44X44PJ11)1NK""r   c              #      #    / n U H  nUR                  [        XU5      5        M      S v   U H  nUR                  5         M     g ! U H  nUR                  5         M     f = f7fr!   )extendrz   remove)fpreserve_signaturemodule_call_signatureshandlesrL   handles         r   _wrap_submodulesr   p   sV     G&DNN?14JKL 'FMMO gFMMO s   A&(A A&A##A&c                     S nXl         U $ )Nc                     [        X5      $ r!   r
   )r   r#   s     r   call'_mark_strict_experimental.<locals>.call~   s    4&&r   )r"   )clsr   s     r   _mark_strict_experimentalr   }   s    ' LJr   c                    US-   n[        U R                  U5      (       a0  [        U R                  U5      U:X  d   eU R                  SUS0 5      $ U R	                  U5      n[        U R                  XB5        U R                  SUS0 5      $ )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.
0get_attrr&   )hasattrrootgetattrr5   get_fresh_qualnamesetattr)r1   namespecfx_namequalnames        r   #_register_func_spec_proxy_in_tracerr      s     SjGv{{G$$v{{G,444"":wB??((.HFKK(z8R<<r   	spec_namecall_spec_cache_keyc                    [        U5      u  pgU R                  U5      n[        U R                  X5        U R	                  SUS0 5      n	[
        R                  " [        U5      5      u  p[        X S3U5      n[
        R                  " U R                  U5      nU R	                  S[        X/UQ70 5      n[        XNS U S9  g )Nr   r&   _const_func_specr.   r/   )r	   r   r   r   r5   r2   rb   r   r   r3   r4   r   r   )r1   r   const_target_for_applygraphable_argstrack_valuer   rY   rJ   r   
spec_proxyrj   	func_specfunc_spec_proxyflat_proxy_args	out_proxys                  r   _emit_flat_apply_callr      s     &n5I((3HFKK+$$Z2rBJ &&'89O'PQLA9''78)O
 oof&9&99EO ##o%TO%TVXI ktFKr   c                 D    [        U 5      =(       a    U R                  S:H  $ )Nr   )callabler'   )fns    r   _is_initr      s    B<5BKK:55r   c                 f   ^  [        T 5      (       d  [        ST R                   S35      eU 4S jnU$ )a  
Experimental decorator that makes subclass to be traceable in export
with pre-dispatch IR. To make your subclass traceble 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, ...):
        # ...
ztorch._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            	        > T	" U 0 UD6  [         R                  R                  5       (       d  g [        [	        U S   5      5      (       dJ  T	R
                  R                  S5      (       d   eT	R
                  S [        S5      *  n[        SU S35      e[        5       nUc  g [        U[        5      (       d   eUR                  nU S   n[        U SS  5      U4nSR                  T	R
                  R                  5       R!                  S5      5      n[	        U5      R"                  R                  5       n[%        UU[	        U5      UUUS9  g )	Nr   r   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.   rj   .r1   r   r   r   r   r   )rR   compileris_exportingr   typer)   endswithlenRuntimeErrorr   rQ   r   r1   tuplejoinlowersplitr'   r   )
r#   r$   obj_namer6   r1   subclass	graphabler   r   constructor_subclasss
            r   wrapperBmark_subclass_constructor_exportable_experimental.<locals>.wrapper   sA   d-f-~~**,,1$tAw-@@'44==jIIII+889KC
O;KLH"8* -} ~ 
 ;<<$ <====748_f-	HH1>>DDFLLSQR	"8n55;;=#'>$  3	
 	r   )r   r   r'   )r   r   s   ` r   1mark_subclass_constructor_exportable_experimentalr      sI    * ())))=)F)F(G H}~
 	
#J Nr   c                    ^  [        T 5      (       a  [        T 5      $ [        T 5      (       d)  T R                  S:X  d  [        ST R                   S35      e[	        T 5      U 4S j5       nU$ )ao  
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)

applyztorch._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            	        > [         R                  R                  5       (       d  T" U 0 UD6$ [        R                  " U S   5      (       d  T" U 0 UD6$ [        U S   [         R                  R                  5      (       d  T" U 0 UD6$ SSKJ	n  U" [         R                  R                  R                  5      nUc  T" U 0 UD6$ [         R                  R                  5       R                  [         R                  R                  R                   5      n[         R                  R#                  5       [         R                  R%                  [         R                  R                  R                   5      -  n[         R                  R'                  XE5         T" U 0 UD6nS S S 5        UR(                  (       d   S5       eUR*                  nU S   R,                   SU S   R.                   3nU/U SS  Q7U4n	SSKJn
  SR5                  UR7                  S5      5      n[9        U
5      R:                  R=                  5       n[?        UUU
U	WUS9  U$ ! , (       d  f       N= f)	Nr   )_get_dispatch_mode_pre_dispatchz"Should only do this in predispatchr   r   )._call_custom_autograd_function_in_pre_dispatchrj   r   ) rR   r   r   inspectisclass
issubclassautogradFunction
torch._opsr   _C_TorchDispatchModeKeyPROXY_dispatch_tls_local_include_setr}   r   PreDispatch_dispatch_tls_local_exclude_setDispatchKeySet_ForceDispatchKeyGuardpre_dispatchr1   r(   r)   torch.export.custom_opsr   r   r   r   r'   r   r   )r#   r$   r   r6   include_to_setexclude_to_setoutr1   function_cls_namer   r   r   r   funcs                r   r   ,allow_in_pre_dispatch_graph.<locals>.wrapper  s   ~~**,,(((tAw''((($q'5>>#:#:;;(((>.uxx/M/M/S/ST<((( AACJJHH  ,,
 HH446hh%%ehh&:&:&F&FGH 	
 XX,,^L''C M   F"FF #Aw112!DG4H4H3IJ'3$qr(3V<		
 HH.44S9:	":

(557 	 	#Q$ 3	
 
3 MLs   	I


I)r   r   r'   r   r   )r   r   s   ` r   allow_in_pre_dispatch_graphr      st    " ~~@FFTNNdmmw6))- 8de
 	
 4[3 3j Nr   )3r   
contextlibr   	functoolsr   rR   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   torch._subclasses.fake_tensorr   "torch.fx.experimental.proxy_tensorr   r   r   torch.utilsr   r2   torch.utils._python_dispatchr   r   r   py_implr:   r<   py_functionalize_implrC   AutogradCPUrF   rz   r   r   r   rT   r   r   r   r   r&   r   r   <module>r      sR    %      K 
 < B * 8 
 * K1* 1 &'  23M 4M N+ ,
 )) *   ;// 0/E
 KOO, -##L 	 	="L L L>6AHTr   