
    ɯwg                        U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlmZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZmZ ddlmZ ddl Z ddl!Z!ddl"Z!ddl#m$c m%Z& ddl'Z!ddl!m(Z( dd	l)m*Z*m+Z+m,Z,m-Z- dd
l.m/Z/ ddl0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6m7Z7 ddl"m8Z8 ddl9m:Z: ddl;m<Z<m=Z=m>Z>m?Z? ddl@mAZAmBZB ddlCmDZDmEZEmFZFmGZGm$Z$ ddlHmIZImJZJ ddlKmKZK ddlLmMZMmNZNmOZO ddlPmQZQ ddlRmSZS ddl$mTZTmUZU erddlVmWZW ddlmXZXmYZY  ej                  e[      Z\ e$j                         Z^ej                  Z` G d de      Zai abdecd<   eaj                  ZedOd Zfd! Zg ej                  e8       e e eei            d"      hZj	 	 	 	 dPd#Zk G d$ d%e!j                  j                        Znd& Zod' Zpd( Zqd) ZrdQd*Zs G d+ d,      Zt G d- d.et      Zu G d/ d0et      Zv G d1 d2et      Zwe`d3dddf	 dRd4Zxd5 Zy G d6 d7ej                        Zzd8 Z{d9 Z|d: Z}d; Z~d< Z	 dSd3ddd3dd=	 	 	 dTd>Z ed?d@      dA        Z G dB dCe!j                  j                  j                        Z G dD dEe      ZdF ZdG Zd3d3ddHdd3d@d3d3d3d@dI	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dUdJZ eQdd      d3ddddKdLZ G dM dN      Zy)Vz
Functions in this file are responsible for modifying the eval frame
handler at RUNTIME.  Therefore, all functions in this file are hot.
Functions that only execute at compile time should be placed
in torch._dynamo.convert_frame.
    )annotationsN)Enum)dirnamejoin)
AnyCallableDictList
NamedTupleOptionalSetTupleTYPE_CHECKINGUnion)patch)_guards)
reset_codeset_guard_error_hook	skip_codeunsupported)enable_python_dispatcher)unset_fake_temporarily)justknobs_checklog_export_usage)_combine_args_process_dynamic_shapes)GraphModule)make_fx)ConstraintViolationError
DimDynamicShapeEnvStatelessSymbolicContext)_PyTreeCodeGen_PyTreeInfo   )configconvert_frameexternal_utilstrace_rulesutils)
CompilerFnlookup_backend)code_context)CondOpArgsMismatchError	UserErrorUserErrorType)Hooksinstall_generation_tagging_init)common_constant_typescompile_times)fake_tensor)
CacheEntryDynamoCallbackc                      e Zd ZdZy)Unsetr   N)__name__
__module____qualname__token     ]/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/_dynamo/eval_frame.pyr:   r:   \   s    Er@   r:   zDict[int, CompilerFn]cached_backendsc                    ddl m} t        d      s+t        j                  j
                  j                  d       | S  ||       S )Nr   )set_eval_framez/pytorch/compiler:enable_compiler_set_eval_framezTDynamo disabled by Justknob: enable_compiler_set_eval_frame, skipping set_eval_frame)torch._C._dynamo.eval_framerD   r   torch_dynamor*   	warn_once)callbackrD   s     rA   _maybe_set_eval_framerJ   e   s=     ;LM%%b	
 h''r@   c                     t         j                         D ]  } t        | d      s| j                          ! t         j	                          y )Nreset)rB   valueshasattrrL   clear)backends    rA   _reset_guarded_backend_cacherQ   s   s;    "))+ 7G$MMO r@   z+onnx/_internal/fx/dynamo_graph_extractor.pyc                    t        |       r| j                  } t        j                  j                  j
                  j                  |       S )zh
    Given a code object or a callable object, retrieve the cache entries
     stored in this code.
    )callable__code__rF   _CrG   
eval_frame_debug_get_cache_entry_list)codes    rA   rW   rW      s5     ~}}88&&BB4HHr@   c                       e Zd ZU dZded<   ded<   h dZd fdZd Zd	 Zd
 Z	d Z
ed        Zej                   fd       Zd Zd fdZd Z fdZ xZS )OptimizedModulezx
    Wraps the original nn.Module object and later patches its
    forward method to optimized self.forward method.
    Callable[..., Any]_torchdynamo_orig_callablezCallable[[], Any]get_compiler_config>   forward__dict___forward	_orig_mod
dynamo_ctxr]   named_children_walkr\   c                    t         |           || _        || _        | j	                          | j                  j
                  | _        y N)super__init__ra   rb   _initializetraining)selfmodrb   	__class__s      rA   rg   zOptimizedModule.__init__   s9    $//r@   c                   t        | j                  t              r+| j                  | j                  j                        | _        nt        | j                  j
                  t        j                        rtt        j                  | j                  j
                        st        | j                  dd      r4| j                  t        j                  | j                              | _        n*| j                  | j                  j                        | _        t        | j                  d      r#| j
                  | _        | j                  | _        y y )N_is_fsdp_managed_moduleF_initialize_hook)
isinstancerb   DisableContextra   __call__r^   types
MethodTyper)   checkgetattrr(   wrap_inlinerN   r`   _call_lazy_checkrj   s    rA   rh   zOptimizedModule._initialize   s    doo~6??4>>+B+BCDL..0@0@Adnn445t~~'@%H
  ??>+E+Ednn+UVDL  ??4>>+B+BCDL4>>#56 LLDM00DL 7r@   c                J    | j                   | j                  | j                  ffS re   )rl   ra   rb   ry   s    rA   
__reduce__zOptimizedModule.__reduce__   s     ABBr@   c                x    t        | j                        }|j                  dd        |j                  dd        |S )Nr^   rr   )dictr_   poprj   states     rA   __getstate__zOptimizedModule.__getstate__   s1    T]]#		)T"		*d#r@   c                2    || _         | j                          y re   )r_   rh   r   s     rA   __setstate__zOptimizedModule.__setstate__   s    r@   c                .    | j                   j                  S re   )ra   ri   ry   s    rA   ri   zOptimizedModule.training   s    ~~&&&r@   c                f    	 t         |   d       || j                  _        y # t        $ r Y y w xY wNra   )rf   __getattr__ra   ri   AttributeError)rj   valuerl   s     rA   ri   zOptimizedModule.training   s3    	G,&+DNN# 		s    $ 	00c                V    |dk(  r| j                   d   S t        | j                  |      S r   )_modulesrv   ra   )rj   names     rA   r   zOptimizedModule.__getattr__   s*    ;==--t~~t,,r@   c                    t        t        |       |      rt        |   ||      S |t        j
                  v rt        |   ||      S t        | j                  ||      S re   )rN   typerf   __setattr__rZ   _opt_mod_attributessetattrra   )rj   r   valrl   s      rA   r   zOptimizedModule.__setattr__   sV    4:t$7&tS11?6667&tS11t~~tS11r@   c                    t        | j                  d      r'| j                  j                  | j                  ||        | j                  |i |S )Nro   )rN   ra   _infer_parametersr`   )rj   argskwargss      rA   rx   z OptimizedModule._call_lazy_check   sC    4>>#56
 NN,,T^^T6Jt}}d-f--r@   c                    | j                   j                         }|t        |          D cg c]	  }||vs| c}z   S c c}w re   )ra   __dir__rf   )rj   orig_mod_attrsattrrl   s      rA   r   zOptimizedModule.__dir__   sH    //1"W_.!
$n2LD!
 
 	
 !
s
   	A A )rk   ztorch.nn.ModulereturnNoner   r   )r;   r<   r=   __doc____annotations__r   rg   rh   r{   r   r   propertyri   setterr   r   rx   r   __classcell__rl   s   @rA   rZ   rZ      s}    
 !32**	01*C ' ' __ -
2.
 
r@   rZ   c                H   t        | t        j                        rt        |        yt	        | d      rt        | j
                         yt	        t        | dd      d      r t        | j                  j
                         yddlm	}  |        t        j                  d|        y)zA
    Make sure f.__code__ is not cached to force a recompile
    rT   r^   Nr%   rL   z#could not determine __code__ for %s)rp   rs   CodeTyper   rN   rT   rv   r^    rL   logwarning)frL   s     rA   remove_from_cacher      sk     !U^^$1	J	1::	It,j	9199%%&91=r@   c                      y re   r?   r?   r@   rA   nothingr     s    r@   c                      yNFr?   r?   r@   rA   always_falser     s    r@   c                n    | }t        |d      r&|j                  }t        |      sJ t        |d      r&|S )z
    In case of nesting of _TorchDynamoContext calls, find the innermost
    function. TorchDynamo caches on fn.__code__ object, so its necessary to find
    the innermost function to pass on the optimize, run, disable etc.
    r\   )rN   r\   rS   )fnunaltered_fns     rA   innermost_fnr     sB     L
, <
=#>>%%% , <
= r@   c                    t        | t              sJ | rt        j                  d      S t        j                  dd      S )NF)assume_static_by_defaultT)automatic_dynamic_shapesr   )rp   boolr&   _make_closure_patcher)enables    rA   make_set_enable_dynamicr     s>    fd###++UKK++%*T
 	
r@   c                  L     e Zd Zeeedfdddd	 	 	 d fdZd Zd Zd Z xZ	S )	_TorchDynamoContextFNexportdynamiccompiler_configc               6   t         |           t        |      s|du s|J || _        | _        t
        | _        || _        || _        || _	        || _
        g | _        g | _         |        t        |      }	t        j                  t!        |	      |	       |$| j                  j#                  t%        |             t&        ur fd}
| j                  j#                  |
       t(        j*                  ur!fd}| j                  j#                  |       y y )NFc                               t         S re   )r   )on_enters   rA   call_on_enterz3_TorchDynamoContext.__init__.<locals>.call_on_enterK  s    
r@   c                 v            } | j                          t        j                  | j                  d d d       S re   )	__enter__	functoolspartial__exit__)ctxbackend_ctx_ctors    rA   call_backend_ctxz6_TorchDynamoContext.__init__.<locals>.call_backend_ctxS  s/    &( ((tT4HHr@   )rf   rg   rS   rI   _backend_ctx_ctorunsetprior	first_ctxr   _dynamicr   cleanup_fnsenter_exit_hooksr   rB   
setdefaultidappendr   r   
contextlibnullcontext)rj   rI   r   r   patch_fnr   r   r   r   rP   r   r   rl   s     ``        rA   rg   z_TorchDynamoContext.__init__)  s    	!X%6(:JJJ(0!138
".46 "
 x(""2g;8!!(()@)IJ7" !!((7:#9#99I
 !!(()9: :r@   c                    t         j                  rt        d      | j                  D cg c]	  } |        c}| _        t        | j                        | _        y c c}w )Nztorch._dynamo.optimize(...) is used with a context manager. Please refer to https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html to use torch._dynamo.optimize(...) as an annotation/decorator. )r&   raise_on_ctx_manager_usageRuntimeErrorr   r   rJ   rI   r   )rj   enters     rA   r   z_TorchDynamoContext.__enter__Z  sQ    ,,R 
 261F1FGEGG*4==9
 Hs   Ac                    | j                   t        usJ t        | j                          t        | _         | j                  D ]	  } |         | j                  j	                          y re   )r   r   rJ   r   rO   )rj   exc_typeexc_valexc_tbcleanups        rA   r   z_TorchDynamoContext.__exit__d  sS    zz&&&djj)
'' 	GI	 r@   c                
   	
  fd}t              t        t              r?t        j                        t        j                  j                  j                        d<   t        t        j                  j                        r6}t        |       }|j                  |_        t        |d      rJ ||_        |S t!        j"                        rV}  |j$                        |_        t'        |t        j                  j                        r  |j(                        |_        |S t+              sJ 	 t!        j,                        }|t1        j2                        r,t5        dd      dvr|t6        vrt9        j:                        d }t         d      r j<                  	n|	t        j>                  j@                  t        jB                  jD                  jF                  
tI        jJ                        	
 fd	       }|_&        |_        t        |d      rJ ||_        	d
vr=t        d      stO        tQ        jR                  d            dtT        j                  <   |S # t.        $ r d }Y 4w xY w)Nc                      j                   S re   )r   ry   s   rA   r]   z9_TorchDynamoContext.__call__.<locals>.get_compiler_confign  s    '''r@   orig_graphmoduler]   r;   r   )
_call_impl_wrapped_call_impl_lazy_forwardc                      y re   r?   )argr   s     rA   
do_nothingz0_TorchDynamoContext.__call__.<locals>.do_nothing  s    r@   rI   c                 |    	       r#t         j                  rt        d       | i |S  
       r#t         j                  rt        d       | i |S j                  D cg c]	  } |        }}t              }t        j                  j                  j                         }	  | i |t        j                  j                  j                  |       t        |       |D ]	  } |         S c c}w # t        j                  j                  j                  |       t        |       |D ]	  } |         w xY w)NzvDetected that you are using FX to symbolically trace a dynamo-optimized function. This is not supported at the moment.zsDetected that you are using FX to torch.jit.trace a dynamo-optimized function. This is not supported at the moment.)r&   error_on_nested_fx_tracer   error_on_nested_jit_tracer   rJ   rF   rU   
_functorchget_dynamic_layer_stack_depth)pop_dynamic_layer_stack_and_undo_to_depth)r   r   r   cleanupsr   saved_dynamic_layer_stack_depthr   rI   r   is_fx_tracingis_jit_tracingrj   s          rA   _fnz)_TorchDynamoContext.__call__.<locals>._fn  s<   22&\ 
 t.v..33&\ 
 t.v..-1-B-BCECHC)(3E ##AAC ,
4*6* ##MM3 &e,' GI- D" ##MM3 &e,' GIs   $C2(C7 7AD;r   rT   a  

                        torch._dynamo.optimize is called on a non function object.
                        If this is a callable class, please wrap the relevant code into a function and optimize the
                        wrapper function.

                        >> class CallableClass:
                        >>     def __init__(self) -> None:
                        >>         super().__init__()
                        >>         self.relu = torch.nn.ReLU()
                        >>
                        >>     def __call__(self, x):
                        >>         return self.relu(torch.sin(x))
                        >>
                        >>     def print_hello(self):
                        >>         print("Hello world")
                        >>
                        >> mod = CallableClass()

                        If you want to optimize the __call__ function and other code, wrap that up in a function

                        >> def wrapper_fn(x):
                        >>     y = mod(x)
                        >>     return y.sum()

                        and then optimize the wrapper_fn

                        >> opt_wrapper_fn = torch._dynamo.optimize(wrapper_fn)
                        T)+r   rp   r   weakrefrefr-   get_contextr^   rT   rF   nnModulerZ   r\   rN   r]   inspectisclassrr   
issubclassr   rS   getsourcefile	TypeErrorr)   ru   rv   DONT_WRAP_FILESr(   rw   rI   rU   _is_tracingfx_symbolic_tracer   r   wraps_torchdynamo_inliner   textwrapdedentalways_optimize_code_objects)rj   r   r]   rk   new_modcls_objfilenamer   r   rI   r   r   s   ``       @@@rA   rr   z_TorchDynamoContext.__call__l  sG   	( " b+& B $$RZZ%8%89"
 b%((//*C%c40G 25G. w(=>>>*=G'N??2 G#G$4$45G'588??3%)'*<*<%="N||	,,R0H !2!22!6J+LM /  ++B/B	 4$}}H!H--00>>		*	 
*	Z #% *,& 3 5666"5 =(2z*"OO   B 9=(5
q  	H	s   8I3 3JJ)rI   r8   r   r   )
r;   r<   r=   r   null_contextrg   r   r   rr   r   r   s   @rA   r   r   (  sI     %/; /; /; 
/;b:!cr@   r   c                  <     e Zd Z	 dddddd	 	 	 d fdZd Z xZS )OptimizeContextFN)r   r   r   rebuild_ctxc          
         d }t         
|   |||t        j                  ||||       t        j
                  r!fd}	| j                  j                  |	       y y )Nc                     t                y re   r2   r?   r@   rA   r   z*OptimizeContext.__init__.<locals>.on_enter   s    +-r@   )rI   r   r   r   r   r   r   r   c                     J         } t         j                  j                  j                  |       }|j	                          t        j                  |j                  d d d       S re   )rF   rG   compiled_autogradr   r   r   r   r   )compiler_fnr   r  s     rA   call_compiled_autogradz8OptimizeContext.__init__.<locals>.call_compiled_autograd0  sT    "...)mmm55<<[I ((tT4HHr@   )rf   rg   TorchPatcherr   r&   r  r   r   )rj   rI   r   r   r   r   r   r  r   r  rl   s          `  rA   rg   zOptimizeContext.__init__  sg    	. 	-!''+ 	 		
 ##I !!(()?@ $r@   c                    | j                   | j                  | j                  | j                  f| j                  | j
                  | j                  dfS )Nr   )rl   rI   r   r   r   r   r   ry   s    rA   r{   zOptimizeContext.__reduce__9  sG    NN]]D22DNNC++==#'#7#7
 	
r@   )F)r  z>Optional[Callable[[], Union[OptimizeContext, _NullDecorator]]]r   r   r;   r<   r=   rg   r{   r   r   s   @rA   r  r    s@    
 	$A  $A
$A 
$AL	
r@   r  c                  &     e Zd Zd fdZd Z xZS )RunOnlyContextc                .    d }t         |   d|       y )Nc                 r    t         j                  j                  j                  xj                  dz  c_        y )Nr%   )rF   rG   mutation_guardGenerationTracker
generationr?   r@   rA   r   z)RunOnlyContext.__init__.<locals>.on_enterH  s#    MM((::EEJEr@   F)rI   r   rf   rg   )rj   r   rl   s     rA   rg   zRunOnlyContext.__init__F  s    	K 	%(;r@   c                    | j                   dfS Nr?   r   ry   s    rA   r{   zRunOnlyContext.__reduce__M      ##r@   r   r  r   s   @rA   r  r  E  s    <$r@   r  c                  ,     e Zd Zd fdZd Zd Z xZS )rq   c                &    t         |   d        y )N)rI   r"  )rj   rl   s    rA   rg   zDisableContext.__init__R  s    $'r@   c                8   t              t        t        j                  j                        r!}t        ||       }|j                  |_        |S t        j                        rm} | |j                        |_
         | |j                        |_        t        |t        j                  j                        r | |j                        |_        |S t              sJ | j                  t!        j"                        fd       }d|_        |_        |S )Nc                 b    t              }	  | i |t        |       S # t        |       w xY wre   )rJ   )r   r   r   rI   r   s      rA   r   z$DisableContext.__call__.<locals>._fnt  s1    )(3E-4*6*%e,%e,s   ! .T)r   rp   rF   r   r   rZ   r^   r\   r   r   rg   rr   r   r   rS   rI   r   r  _torchdynamo_disable)rj   r   rk   r  r  r   rI   s    `    @rA   rr   zDisableContext.__call__U  s    
 "b%((//*C%c40G14G.N??2 G  $G$4$45G#G$4$45G'588??3%)'*<*<%="N||==			- 
	- $(  *,&
r@   c                    | j                   dfS r$  r   ry   s    rA   r{   zDisableContext.__reduce__  r%  r@   r   )r;   r<   r=   rg   rr   r{   r   r   s   @rA   rq   rq   Q  s    (-^$r@   rq   Fc           	     N    t        t        j                  | |      |d||||      S )NT)r   r   r   r   r   r  )r  r'   catch_errors_wrapper)
compile_fnhooksr   r   r   r   r  s          rA   _optimize_catch_errorsr0    s3     **:u=)' r@   c                    ddl m} t        | d      r| j                  }nt	        | t
              r| }nd }t        |       }  || |      S )Nr%   )wrap_backend_debugcompiler_name)repro.after_dynamor2  rN   r3  rp   strr,   )r  r2  compiler_strs      rA   get_compiler_fnr7    sE    6{O,"00	K	%" -Kk<88r@   c                      e Zd Zd Zy)_NullDecoratorc                     t        |      sJ |S re   )rS   )rj   r   s     rA   rr   z_NullDecorator.__call__  s    ||	r@   N)r;   r<   r=   rr   r?   r@   rA   r9  r9    s    r@   r9  c                 @    t         j                  dk\  rt        d      y )N)      z0Python 3.13+ not yet supported for torch.compile)sysversion_infor   r?   r@   rA   check_if_dynamo_supportedr@    s!    
7"MNN #r@   c                 8    	 t                y# t        $ r Y yw xY wNTF)r@  	Exceptionr?   r@   rA   is_dynamo_supportedrD    s"    !#    
 	c                     t                y re   )r@  r?   r@   rA   check_if_inductor_supportedrG    s    r@   c                 8    	 t                y# t        $ r Y yw xY wrB  )rG  rC  r?   r@   rA   is_inductor_supportedrI    s"    #% rE  c                 0      fd}t        |g i S )Nc                     t         i S re   )optimize)r   r   s   rA   r  zoptimize.<locals>.rebuild_ctx  s    (((r@   )	_optimize)r   r   r  s   `` rA   rL  rL    s    ) [242622r@   )nopythonguard_export_fnguard_fail_fndisabler   c          
        t                t        ||      }t        j                  j	                  d       |s.t
        j                  j                  dd      dk(  st        d      s
t               S t        |      }t        |dt              }|rt        ||||       S t        t        j                  ||	      |||t!        |d
      r|j#                         |       S d|       S )a  
    The main entrypoint of TorchDynamo.  Do graph capture and call
    backend() to optimize extracted graphs.

    Args:
        backend: One of the two things:
            - Either, a function/callable taking a torch.fx.GraphModule and
            example_inputs and returning a python callable that runs the
            graph faster.
            One can also provide additional context for the backend, like
            torch.jit.fuser("fuser2"), by setting the backend_ctx_ctor attribute.
            See AOTAutogradMemoryEfficientFusionWithContext for the usage.
            - Or, a string backend name in `torch._dynamo.list_backends()`
        nopython: If True, graph breaks will be errors and there will
            be a single whole-program graph.
        disable: If True, turn this decorator into a no-op
        dynamic: If True, upfront compile as dynamic a kernel as possible.  If False,
            disable all dynamic shapes support (always specialize).  If None, automatically
            detect when sizes vary and generate dynamic kernels upon recompile.

    Example Usage::

        @torch._dynamo.optimize()
        def toy_example(a, b):
            ...
    rO  rP  ztorch._dynamo.optimizeTORCHDYNAMO_DISABLEr   1zpytorch/compiler:enable_dynamor   )r   r/  r  )r/  r]   N)r   r   r  )r@  r1   rF   rU   _log_api_usage_onceosenvirongetr   r9  r7  rv   r  optimize_assertr0  r'   rN   r]   )	r  rP   rN  rO  rP  rQ  r   r/  r   s	            rA   rM  rM    s    H  /OE	HH  !9:::>>/4; @Ag&G w(:LI#	
 	
 "##G59712  335  	 	 	 	r@   z&torch._dynamo.symbolic_convert.explainTc                b      fd}|s|r$t        j                  dt        d        ||i |S |S )Nc            
       
 ddl m}  |        g g 
dg g 	 	 d
fd}fd} t        |d|            } || i | t              }|dz
  }t	        d	      } |        dd
lm}	  |	||
|      S )Nr%   r   r   c                L    ddl m}  ||       \  } | j                  S )Nr%   )_explain_graph_detail)backends.debuggingr^  r^   )gmexample_inputsr^  break_reasonsgraphsop_countops_per_graphs      rA   "dynamo_graph_accumulating_compilerzBexplain.<locals>.inner.<locals>.dynamo_graph_accumulating_compiler,  s:     B BWFHm]B>B- ::r@   c                (    j                  |        y re   )extendguards
out_guardss    rA   guard_export_printz2explain.<locals>.inner.<locals>.guard_export_print<  s    f%r@   F)rN  rO  r5  )repr)ExplainOutputr`  torch.fx.GraphModule)r   rL   rL  lenr5   r_  rn  )r   r   rL   rf  rl  opt_fgraph_countgraph_break_countcompile_timern  rb  rc  rd  re  rk  r   s             @@@@@rA   innerzexplain.<locals>.inner   s    -/#%-/*,
	$	 	 	&
..
 	 	tv&k'!O$%0 	5	
 		
r@   zexplain(f, *args, **kwargs) is deprecated, use explain(f)(*args, **kwargs) instead.  If you don't migrate, we may break your explain call in the future if your user defined kwargs conflict with future kwargs added to explain(f).   
stacklevel)warningswarnFutureWarning)r   
extra_argsextra_kwargsrv  s   `   rA   explainr    s@    9
v \? 	
 j1L11r@   c                  j     e Zd Z	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZd Z fdZ fdZ fdZ xZS )FlattenInputOutputSignaturec	                X    t            |       t        |      t        |      k(  sJ t        |      D 	
ci c]  \  }	}
|
||	    }}	}
g  _        t        dt        |            D ]  }t            d| di       }||v r||   |j                  j                  d<   n|t        ||   t        j                        r|j                  ||   t        t        t        ||   j                              D cg c])  }|||   v rt        j                   nt        j"                  + c}d gt        ||   j                        z              |j                  j                  d<    j                  j%                  |         fd|D         _        | _        | _        y c c}
}	w c c}w )Nr   r   r?   r   )dynamic_sizesconstraint_sizes)symbolic_contextc              3  <   K   | ]  }j                   |     y wre   )new_args).0irj   s     rA   	<genexpr>z7FlattenInputOutputSignature.__init__.<locals>.<genexpr>  s     X!T]]1-Xs   )rf   rg   rq  	enumerater  rangeplaceholdernodemetarp   rF   Tensorfrom_tensorr"   shaper    DYNAMICSTATICr   old_args_gen!matched_output_elements_positionsflat_results)rj   m	flat_args matched_input_elements_positionsr  r  example_fake_inputsflat_args_dynamic_dims	fake_modeixr   matched_input_elements_to_faker  r   drl   s   `              rA   rg   z$FlattenInputOutputSignature.__init__i  s    	)*c)n<<< %%EF*
C $R((*
& *

 q#i.) 	&A'%A3iR8C22'Ea'He$ (Z	!ell-S+4+@+@!!)A
 */s9Q<3E3E/F)G	+ %& $%(>q(A#A !+ 2 2%/%6%6!7+ /3Vc)A,:L:L6M-M* ,A ,CHHMM%( MM  %+	&, Y7WX1R.(=*
$+s   F!7.F'c                p   t        | j                        }d| j                  j                  v r0| j                  j                  d   |j                  j                  d<   d| j                  j                  v r0| j                  j                  d   |j                  j                  d<   d| j                  j                  v r0| j                  j                  d   |j                  j                  d<   d| j                  j                  v r0| j                  j                  d   |j                  j                  d<   |S )Nr   tensor_dictexample_valueunbacked_bindings)nextr  current_noder  r  )rj   targetr   r   r   s        rA   r  z'FlattenInputOutputSignature.placeholder  s    4$$%D%%***#'#4#4#9#9%#@CHHMM% D--222+/+<+<+A+A-+PCHHMM-(d//444-1->->-C-CO-TCHHMM/*$"3"3"8"88151B1B1G1G#2CHHMM-. 
r@   c                t   |d   }g || j                   }g }t        t        | j                              D ]n  }| j                  |   "|j                  || j                  |             4| j                  |   }t        |t        t                    sJ |j                  |       p t        	| )  ||fi       S )Nr   )r  r  rq  r  r  r   rp   tupler4   rf   output)
rj   r  r   r   dynamo_result_flatlookupnew_results_flatr  	const_valrl   s
            rA   r  z"FlattenInputOutputSignature.output  s    !!W6%66s4,,-. 	3A55a8D ''4AA!DE !--a0	!)U3H-IJJJ ''	2	3 w~f'7&92>>r@   c                   || _         t        | 	  |      }d| j                   j                  v r0| j                   j                  d   |j                  j                  d<   d| j                   j                  v r0| j                   j                  d   |j                  j                  d<   d| j                   j                  v r0| j                   j                  d   |j                  j                  d<   | j                   j
                  dk7  rD|j                  j                  t        | j                   d|j                  j                               |S )Nr   r  r  r  r   )	r  rf   run_noder  r  op_renamerv   r   )rj   nresult_proxyrl   s      rA   r  z$FlattenInputOutputSignature.run_node  s$   w'*D%%***,0,=,=,B,B5,IL""5)d//4446:6G6G6L6L7L""?3 $"3"3"8"88:>:K:K:P:P#;L""#67 8+%%))6<3D3D3I3IJ r@   c                    t         |          }d| j                  j                  v r&| j                  j                  d   |j                  d<   |S )N dynamo_flat_name_to_original_fqn)rf   	transformmoduler  )rj   	result_gmrl   s     rA   r  z%FlattenInputOutputSignature.transform  sL    G%'	-1A1AAAEAQAQ2BINN=> r@   re   )r  rp  r  z
Tuple[Any]r  	List[int]r  z	List[Any]r  r  r  zList[torch.Tensor]r  zList[Set[int]]r  z$Optional[fake_tensor.FakeTensorMode]r   r   )	r;   r<   r=   rg   r  r  r  r  r   r   s   @rA   r  r  h  s     ;?,),) ,) +4	,)
  ,) ,5,) 0,) !/,) 8,) 
,)\?( r@   r  c                  "    e Zd ZU ded<   ded<   y)ExportResultrp  graph_module_guards.GuardsSetrj  N)r;   r<   r=   r   r?   r@   rA   r  r    s    &&r@   r  c                t   g }| j                   j                  d      D ]  }t        |d      sJ |j                  }| j                  j                  |      }|;t        |      dkD  sJ d }|D ]  }t        |      dk(  r|} n ||j                          d}n]dj                  t        j                  |            }d}	t        |      dkD  rdt        |      dz
   d	}	|j                          d
| |	 }|j                  |        |r,t        t        j                  ddj                  |      z         y )Nr  r  _dynamo_sourcer   z, a closed over free variabler   r%   z(elided z more accesses)z, accessed at:
a  Cannot export model which references tensors that are neither buffers/parameters/constants nor are direct inputs.  For each tensor, if you'd like this tensor to be an explicit input, add it as a dummy argument to the top-level model definition you are exporting; if you would like its value to be embedded as an exported constant, wrap its access in a function marked with @assume_constant_result.

z

)graph
find_nodesrN   r  _source_to_user_stacksrY  rq  r   r   	tracebackformat_listr   r/   r0   INVALID_INPUT)
r  input_errorsr  sourceuser_stacksstacksmsgtbextras
             rA   check_signature_rewritabler    sb   L&&-&8 !t-...$$2266v>;!###  	A1v{E		
 =[[]O#@AC..u56BE;!#"3{#3a#7"8H[[]O#3B4w?CC 1!4 ''E kk,'(	
 		
 r@   c
           
         t        j                  ||      \  }
}d } ||t        j                         t        j                  |      \  }} ||t        j
                         dd fd} |d||      }|J  |dt        |      |z   |      }t        |||||||	|      j                         }dd}t        t         | |
|      ||            |j                  _        |j                          |S )	Nc                   t         j                  t         j                  t         j                  t         j                  t         j
                  j                  gt        t              z   fd}|t        j                  k(  rdnd}| D ]B  } ||      r|t        j                  k(  r|"t        |d| dt        |       d| d d	       y )	Nc                .    t        | t                    S re   )rp   r  )r   supported_typess    rA   is_supported_typezMrewrite_signature.<locals>.check_user_input_output.<locals>.is_supported_type  s    c5#9::r@   inputr  zIt looks like one of the zs with type `z;` is not supported or pytree-flattenable. 
Exported graphs z2s can only contain the following supported types: z. 
If you are using a custom class object, please register a pytree_flatten/unflatten function using `torch.utils._pytree.register_pytree_node` or `torch.export.register_dataclass`.)rF   r  SymIntSymFloatSymBoolrU   ScriptObjectlistr4   r0   r  r/   r   )flat_values
error_typer  
value_typevr  s        @rA   check_user_input_outputz2rewrite_signature.<locals>.check_user_input_output  s    LLLLNNMMHH!!
 &'(	; !+m.I.I IWx
 	A$Q'!<!<</
|=a	 R''1l 322A1B C99
 
	r@   c                    | j                   j                         D ]\  \  }}|j                  t        j                  j
                  us-ddlm} t        j                  d||j                          |dd       y )Nr   )Unsupportedz3Parameter %s is optional with a default value of %sz3Tracing through optional input is not supported yetoptional_input	case_name)

parametersitemsdefaultr   	Parameteremptytorch._dynamo.excr  r   error)f_sigr   paramr  s       rA   check_optional_input_and_errorz9rewrite_signature.<locals>.check_optional_input_and_error3  sm     ++113 	KD%}}G$5$5$;$;;9		IMM
 "I. 	r@   c                r   g }i }t        |      D ]  \  }}||t        |      <    t        |      D ]  \  }}t        |t        t                    r|j                  d        1t        |      |vr(| dk(  r        t        dt        |       d|  d      |j                  |t        |                 |S )NinputszUnexpectedly found a z in the zO.
Please file an issue along with a paste of the logs from TORCH_LOGS="+export")r  r   rp   r  r4   r   AssertionErrorr   )	
debug_typesources
candidatesmatched_elements_positionsdict_of_source_valsr  r   r  r  s	          rA   produce_matchingz+rewrite_signature.<locals>.produce_matchingC  s    :<" ( 	-FAs+,3(	-  
+ 	PFAs#u%:;<*11$7C 33)259$+DI;hzl Kd d 
 +112Ebg2NO	P *)r@   r  outputsc                   dd} ||       }|j                   d t        |       }t        |      t        |j                         kD  rU|j                  J d       |t        dt        |      t        |      z
        D cg c]  }|j                   d|  c}z  }ndt        |      t        |j                         k  rC|j                   t        |      t        |j                  xs g         D ]  }||v rJ d|         |t        |j                               z  }|j                  D ]%  }|j                  xs i }	||v r||	v rJ d|         |S c c}w )Nc           	     h   t        | j                  j                               }|D cg c]5  }|j                  t        j
                  j                  k(  r|j                  7 }}|D cg c]6  }|j                  t        j
                  j                  k(  s+|j                  8 }}t        d |D        d       }t        d |D        d       }t        d |D              }|D ci c]f  }|j                  t        j
                  j                  k(  r=|j                  t        j
                  j                  ur|j                  |j                  h }}i }	| j                  rd| j                  i}	|D ]  }
|
j                  |	|
j                  <    t	        j                  |||||||	      S c c}w c c}w c c}w )Nc              3     K   | ]8  }|j                   t        j                  j                  k(  s+|j                   : y wre   )kindr   r  VAR_POSITIONALr   r  ps     rA   r  z^rewrite_signature.<locals>.argument_names.<locals>.signature_to_fullargspec.<locals>.<genexpr>{  s+     VA166W5F5F5U5U+UV
   ,A A c              3     K   | ]8  }|j                   t        j                  j                  k(  s+|j                   : y wre   )r  r   r  VAR_KEYWORDr   r  s     rA   r  z^rewrite_signature.<locals>.argument_names.<locals>.signature_to_fullargspec.<locals>.<genexpr>  s+     SA166W5F5F5R5R+RSr  c              3     K   | ]]  }|j                   t        j                  j                  k(  r4|j                  t        j                  j
                  ur|j                   _ y wre   )r  r   r  POSITIONAL_OR_KEYWORDr  r  r  s     rA   r  z^rewrite_signature.<locals>.argument_names.<locals>.signature_to_fullargspec.<locals>.<genexpr>  sL      66W..DDDIIW%6%6%<%<< 		s   A#A%r   )r  r  rM   r  r   r  r  r   KEYWORD_ONLYr  r  r  r  return_annotation
annotationFullArgSpec)sigparamsr   r   
kwonlyargsvarargsvarkwdefaultskwonlydefaultsr   	parameters              rA   signature_to_fullargspeczKrewrite_signature.<locals>.argument_names.<locals>.signature_to_fullargspecn  s   #..//12F  66W..DDD D  !'!&&G4E4E4R4R*RJ  VVG SSE
   H  66W..;;;IIW%6%6%<%<< 		!N  K$$')>)>?# C	.7.B.BINN+C &&guh
NK I
$s   :F%(,F*F*!A+F/zMore arguments than expectedr   _zMissing argument zMissing keyword only argument )r
  inspect.Signature)	r   rq  r  r  r  r  keysr  r  )
r  r   r   r  fullargspec
input_strsr  unprovided_arg
kwonly_argr  s
             rA   argument_namesz)rewrite_signature.<locals>.argument_namesm  s   *	X /u5 !%%kD	2
t9s;++,, &&2R4RR2q#d)c*o"=> &&'q, J Y[--.. #."2"2D	S!5!5!;<<# V &/U3D^DT1UU/V 	d6;;=))
 &00 	=J(77=2Nf$
n(D=/
|<=D	= 5s   =E)r  r  )r   z	List[str])pytreetree_unflattenr0   r  tree_flattenINVALID_OUTPUTr  r  r  r#   r$   r  _codegen	recompile)r  r  r  r  in_specr  graph_captured_inputgraph_captured_outputdynamo_traced_resultr  	orig_argsorig_kwargsr  flat_results_tracedout_spec_tracedr  r  r  	new_graphr  r  s   `                   @rA   rewrite_signaturer*    s    $229gFI{> I}'B'BC+1+>+>?S+T(/1M1MN ** (8)1($ !,,,(84-.:<O)% ,()	 ik Pd  .5)[9	
 IOO r@   symbolic)
aten_graphpre_dispatchdecomposition_tabletracing_modedynamic_shapesr   same_signaturedisable_constraint_solver+prefer_deferred_runtime_asserts_over_guards'allow_complex_guards_as_runtime_asserts_log_export_usagec                  	
 |rt        ddh       | |
	fd}|s|r$t        j                  dt        d        ||i |S |S )ac  
    Export an input function f to a format that can be executed outside of PyTorch using the FX graph.

    Args:
        f (callable): A PyTorch function to be exported.

        aten_graph (bool): If True, exports a graph with ATen operators.
        If False, exports a graph with Python operators. Default is False.

        pre_dispatch (bool): If True, exports a graph with ATen operators,
        but before any logic in the PyTorch dispatcher has run.
        This can be useful if you want to apply further transformations on a graph before running it
        through autograd, autocast, or any other functionalities that are integrated into the dispatcher.
        This flag is only valid if aten_graph=True is set.
        Default is False.

        decomposition_table (dict): A dictionary that maps operators to their decomposition functions.
        Required if aten_graph or tracing_mode is specified. Default is None.

        tracing_mode (str): If "symbolic", turn on dynamic shapes support. Default is "symbolic".

        dynamic_shapes:
         An optional argument where the type should either be:
         1) a dict from argument names of ``f`` to their dynamic shape specifications,
         2) a tuple that specifies dynamic shape specifications for each input in original order.
         If you are specifying dynamism on keyword args, you will need to pass them in the order that
         is defined in the original function signature.

         The dynamic shape of a tensor argument can be specified as either
         (1) a dict from dynamic dimension indices to :func:`Dim` types, where it is
         not required to include static dimension indices in this dict, but when they are,
         they should be mapped to None; or (2) a tuple / list of :func:`Dim` types or None,
         where the :func:`Dim` types correspond to dynamic dimensions, and static dimensions
         are denoted by None. Arguments that are dicts or tuples / lists of tensors are
         recursively specified by using mappings or sequences of contained specifications.

        same_signature (bool): If True, rewrite the returned graph's signature to be the same as f.

        disable_constraint_solver (bool): Whether the dim constraint solver must be disabled.

    Returns:
        A function that given args and kwargs, returns a tuple of (graph, guards)
        Graph: An FX graph representing the execution of the input PyTorch function with the provided arguments and options.
        Guards: The guards we accumulated during tracing f above

    Raises:
        AssertionError: If decomposition_table is specified without setting aten_graph=True,
        or if graph breaks during tracing in export.

        AssertionError: If Dynamo input and output is not consistent with traced input/output.

    Note - this headerdoc was authored by ChatGPT, with slight modifications by the author.
    zexport.private_apirG   )eventflagsc                   !"#$%& t        (| |      }t        |-      }(}'}t                t        j                  j                  d       +	*sJ d       .r	*sJ d       t        |      }t        |t        j                  j                        r|j                  n|}t        j                  |      }d #d &d $d %d "d }d#&fd}	g !	 	 d$!"#$%fd}
t        j                  | |f      \  }}t        |       d }1dk7  rd}t!        j"                  d|ddd/)	      5   t%        |
t'        |	d 
      d|      |      }	  || i |}d d d        t        |       ,s`t+        "dd       x}P|j,                  x}At        |t        j.                  j0                  t        j.                  j2                  f      st5        j6                  |      s|j9                          |j;                         }|j=                  |-||      }|r|j>                  d   |z   f|_        n$|rt)        |      }nt@        jC                  d|       |jD                  jG                         D ]]  }t        |tH        jJ                        st)        djM                  tO        jP                  |jR                  |                d| d      }_ |r|#0sJ d       g !d$d%t        jT                  jW                  tY               d      "&t[        j\                         &&J t_        |j`                  jG                               }t        jb                  je                         }tg        |      D ]o  \  }}t        jh                  ||         s|jk                  |      }"jm                  ||   d      |jn                  d<   $||   fz   $!jq                  ||          q |js                  %       t        j                  j                         }t        jb                  ju                  ||      #t@        jC                  d#jw                  dd             nOty        #d      sJ &J d       "J t@        jC                  d#jw                  dd             0rt{        #       !D cg c]  }"jm                  |       }}*rԈ#fd}t}               5  t               5  "5  	  t        |+dd.d      | #	 d d d        d d d        d d d        #J #j                  j                  d!"      D ]e  }t        t+        #|j                        t        j                        s2"jm                  t+        #|j                        d      |jn                  d<   g 0r|D cg c]y  }|xs dD ch c]g  }|j                  t        |      k(  rM|j                  j                  j                  |j                  j                  j                  k7  r|j                  i c}{ } }}t        |#"|||$%|| 
      #t        #&      S # t(        $ r}|}Y d }~ d }~ww xY w# 1 sw Y   
xY wc c}w # t        $ r*}t        t        j                  t        |      d       d }~ww xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY wc c}w c c}}w )%Nztorch._dynamo.exportzaSpecifying a decomposition_table table or tracing mode is illegal without setting aten_graph=Truez7pre_dispatch=True can only be used when aten_graph=Truec                    J d       | y )Nz3whole graph export entails exactly one guard exportr?   ri  s    rA   rl  z1export.<locals>.inner.<locals>.guard_export_print/  s"     "EDE"Jr@   c                Z    J d       | t        j                         |fd}|S )Nz\Tried to emit a second graph during export. Tracing through 'f' must produce a single graph.c                    | J t        j                  d            }t        j                  d            }t        j                  |       t        j                  |       n}t               }|j                  x}r|j                         }|5  t               5  |5  i ||}i }|j                         D ]  \  }}	|j                  |	d      ||<    t        j                  |j                  |       }
t        j                  j                  ||
      d d d        d d d        d d d        S # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   S xY w)NF)remove_duplicateTstatic_shapes)r}   named_parametersnamed_buffersr   detect_fake_moder  	shape_envignore_fresh_unbacked_symbolsr   r  r  r  tree_maprF   funcfunctional_call)graph_inputsr@  rA  ambient_fake_modeignore_fresh_unbackedrC  params_and_buffersfake_params_buffersr   r   fake_graph_inputsr  r  r"  graph_captured_results              rA   result_capturing_wrapperzhexport.<locals>.inner.<locals>.dynamo_normalization_capturing_compiler.<locals>.result_capturing_wrapperJ  s    (4$(((#'(>(>PU(>(V#W  $U%8%8%%8%P Q //=I ,,\:" " )5% 1 ; ;;9;,5,S,S,U) &+-/D***'*& +-''9'?'?'A e4E4Q4Q! 5R 5+D1
 )/)55|)% -2JJ,F,F24E-)#  * -,+    * -,s=   EE "A8D4E "E4D=9E  E		EE)r   rB  )r`  inner_example_inputsrO  ra  r  r  r"  rN  s      rA   'dynamo_normalization_capturing_compilerzFexport.<locals>.inner.<locals>.dynamo_normalization_capturing_compiler8  sD    
 nmnE  002I1N.-` ,+r@   r+  TF)specialize_intr   r    capture_dynamic_output_shape_opscapture_scalar_outputsr3  r4  rS  )r/  r   export_constraintsrC  r   z#Summary of dimension constraints:%sr   zk
It appears that you're trying to set a constraint on a value which we evaluated to have a static value of z0. Set TORCH_LOGS="+export" for more information.zhFailed to produce a graph during tracing as no tensor operations were found and same_signature is False.r?   )rC  r   r>  r   zQFailed to capture a graph during tracing as no tensor operations were found.:

%s)print_outputcoloredr  z'Failed to produce guards during tracingzDynamo captured graph:

%sc                     t         j                  j                  j                         5  t        j                  j	                        j
                  |  cd d d        S # 1 sw Y   y xY wre   )rF   r  r  preserve_node_metaInterpreterrun)r   r  s    rA   graph_with_interpreterz5export.<locals>.inner.<locals>.graph_with_interpreter  sO    XX''::< B 88//6::DAB B Bs   +AA(real)r.  r/  _allow_non_fake_inputsr-  _allow_fake_constantcond_operandsr  get_attrr  )rj  r  ro  )Sr   r   r@  rF   rU   rV  r   rp   r   r   r^   r   	signaturer  r  r   r&   r   rZ  r1   r   rv   dim_constraints_opsOpOverloadPacket
OpOverloadr)   ru   solveforced_specializationsprettify_resultsr   r   infovar_to_ranger  sympyIntegerr   r  r  var_to_stack_subclassesFakeTensorModer!   r   	GuardsSetr  r  r  Graphr  	is_tensorr  r  r  r   r  r   print_readablerN   r  r   r   r   r.   r/   r0   DYNAMIC_CONTROL_FLOWr5  r  r  r  r  t_idr   constraint_rangevrlowerupperdimr*  r  )2r   r   combined_argsconstraintsr   r   call_to_inspectoriginal_signatureresult_tracedrl  rQ  r  r!  constraint_violation_errorrr  erC  rc  rh  r  kparameter_namesfx_graphr  r   r  r  tr  r\  xcr  ra  r  r  r"  rN  rk  _assume_static_by_default_fr4  r,  r.  r2  r0  r-  r3  r1  r/  s2                                    @@@@@@rA   rv  zexport.<locals>.inner  s   %b$7-m^L#< !#$$%;<*srsXXX:O'1!UXX__'E!))1$..?
#DH		  B	,$B	, B	,L $00$@	7!%)":%'+$\\%=%*-1#'8c4[
 	/O7$6"& #. E/ %t 6v 6)	/. 	! *%idCCP$-$=$==J%**"="=uzz?T?T!U  %%o6!!#%4%K%K%M"!22"*&	C *.33A6<3*/ *1I#1N.HH= ++002 a/1I779#8#89O9OPQ9R#STU VNNOS QII2. &,,=zyz  N#% $&!))88"*T 9 I !$..0
)))"#5#@#@#E#E#GHOxx~~'H$_5 84??9Q<0#//5D'0'<'<!!D (= (DIIe$ ,@9Q</+Q(")))A,78 OO12XX__&FHH((:EHHe$$%$F
 5":;;;)T+TT)(((HH.$$%$F *51 BPPAy44Q7PPB () +C+E y G.,?%+/3%1-2 +,E  $ $$$..*.= geT[[95<<H'0'<'<t{{34 (= (DIIe$  #
&  */R"Q%..11771;M;M;P;P;V;VV	 EE
&" 
& &"#$%&E E:..u , /-.*/+	/ 	/l Q$ / #%::A"1      6
&s    !YX9!YZ4Z'ZY$1Z'9Z4[%A,[[9	YY
Y
YYY$	Z	-%Z	Z	ZZ$Z''Z1	,Z44Z>[zexport(f, *args, **kwargs) is deprecated, use export(f)(*args, **kwargs) instead.  If you don't migrate, we may break your export call in the future if your user defined kwargs conflict with future kwargs added to export(f).rw  rx  )r   rz  r{  r|  )r   r,  r-  r.  r/  r0  r   r1  r2  r3  r4  r5  r}  r~  rv  r  r  s    ````` ````    @@rA   r   r     sh    N 3I;G 
B 8[/ [/z \> 	
 j1L11r@   )r/  r   rU  r   r  c                   t        |       } t        | dt              }t        t	        j
                  | ||      |||||      S )zF
    The same as `torch._dynamo.optimize(backend, nopython=True)`
    r   )r   rU  )r   r   r  )r7  rv   r  r0  r'   convert_frame_assert)rP   r/  r   rU  r   r  r   s          rA   rZ  rZ  B  sT     g&G w(:LI!**F7I	
 		 	r@   c                  V    e Zd Ze ej
                  d      d               Zed        Zy)r  Nc                 0   ddl m}   | t        j                  j                        t        j                  _         | t        j                  j
                        t        j                  _         | t        j                  j                        t        j                  _         | t        j                  j                  j                  j                        t        j                  j                  j                  _        t        j                  j                  j                  d       ddlm}m}m}m}m}m}m}m}m}	m}
m}m}m} |||||||||	|
|||h}|D ]T  }|j6                  j9                  d      d   }d| }d	| }t;        ||      s8t=        || | t?        ||                   V t        j@                  jB                  jE                         D cg c]=  }tG        jH                  |      r&tK        |t        j@                  jL                        r|? }}t        j@                  jN                  t        j@                  jP                  h}|D ]A  }||v r | |jR                        |_)        t;        |d
      s+ | |jT                        |_*        C y c c}w )Nr%   )rQ  Fr   )adadeltaadagradadamadamaxadamwasgdlbfgsnadamradamrmsproprpropsgdsparse_adam._fused__single_tensor__init_group)+
decoratorsrQ  rF   jittracetrace_module_get_trace_graphr  r  TracerdistributionsDistributionset_default_validate_argstorch.optimr  r  r  r  r  r  r  r  r  r  r  r  r  r;   splitrN   r   rv   optimr_   rM   r   r   r   	Optimizer
SparseAdamLBFGSstepr  )rQ  r  r  r  r  r  r  r  r  r  r  r  r  r  optimizer_modulesopt_modopt_namefused_fn_namesingle_tensor_fn_nameoptoptimizer_classesexcluded_optimizer_classess                         rA   r   zTorchPatcher.patch`  s    	(!%))//2		!()?)?!@		%,UYY-G-G%H		"07HH$$++111
  ''- 	((BB5I	
 	
 	
 	
" 
  ) 	G''--c226H%hZ0M&5hZ$@!w.]GGG]4S,T	 {{++224
s#
38M8M(N 
 
 KK""KK&
"
 % 	;C00"388,sM*")#//":	;
s   AJc                      fd}|S )Nc                 L    t        j                  dt        d        | i |S )Nignoreztorch.distributed)categoryr  )rz  filterwarningsUserWarning)r   r   r   s     rA   inner_fnzBTorchPatcher.suppress_torch_distributed_warnings.<locals>.inner_fn  s+    ##;7J t&v&&r@   r?   )r   r  s   ` rA   #suppress_torch_distributed_warningsz0TorchPatcher.suppress_torch_distributed_warnings  s    	' r@   )r;   r<   r=   staticmethodr   	lru_cacher   r  r?   r@   rA   r  r  _  s@    YI;  I;V  r@   r  )rI   r8   )rX   z)Union[types.CodeType, Callable[..., Any]]r   zList[CacheEntry])r   r   )r/  r1   )inductor)r  z4Callable[[], Union[OptimizeContext, _NullDecorator]]r   z&Union[OptimizeContext, _NullDecorator])r   r[   r,  r   r-  r   r.  z9Optional[Dict[torch._ops.OpOverload, Callable[..., Any]]]r/  r5  r0  z6Optional[Union[Dict[str, Any], Tuple[Any], List[Any]]]r   r   r1  r   r2  r   r3  r   r4  r   r5  r   r   zCallable[..., ExportResult])r   
__future__r   r   r   r   loggingrW  r>  r  r  rs   rz  r   enumr   os.pathr   r   typingr   r   r	   r
   r   r   r   r   r   r   unittest.mockr   rl  rF   torch.fxtorch.utils._pytreer*   _pytreer  torch.utils.checkpointr   rE   r   r   r   r   torch._dispatch.pythonr   torch._subclasses.fake_tensorr   torch._utils_internalr   r   torch.export.dynamic_shapesr   r   r   "torch.fx.experimental.proxy_tensorr   %torch.fx.experimental.symbolic_shapesr   r    r!   r"   torch.fx.graphr#   r$   r   r&   r'   r(   r)   backends.registryr+   r,   r-   excr.   r/   r0   r/  r1   r  r3   r4   r5   torch._subclassesr6   r7   r8   	getLoggerr;   r   ExactWeakKeyDictionaryr
  r   r  r:   rB   r   r>   r   rJ   rQ   r   __file__r  rW   r   r   rZ   r   r   r   r   r   r   r  r  rq   r0  r7  r9  r@  rD  rG  rI  rL  rM  r  r  interpreterTransformerr  r  r  r*  r   rZ  r  r?   r@   rA   <module>r     s|   #     	 
       !        $ $    < @ C N   6  7 G G 9 & B B  ; 7 -1 g!  <u;;= %%D  *,& +( G+&"	#%RS	I
3	I	If
ehhoo f
R>"	

g gT0
) 0
f	$( 	$4$( 4$t "(
9Z++ O
 3 K KEK ,K^ /6F 7FRg%(("6"6"B"B gT: &
REV  	"MQ%*&+8=49"uu u 	u
u u Ku #u u  $u 26u .2u u" !#uv d
:V Vr@   