o
    I&i)                  	   @  s  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Zd dlm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$m%  m&Z' d dl(Z"d dl"m)Z) d d	l*m+Z+ d d
l,m-Z- d dl.m/Z/m0Z0 d dl1m2Z2m3Z3m4Z4 d dl5m6Z6m7Z7 d dl8m9Z9 ddl:m;Z; ddl<m=Z=m>Z> ddl?m@Z@ erd dlAmBZBmCZCmDZDmEZEmFZF neGe"jHjIjJD ]ZKeKLdrqeMe"jHjIjJeKeN eK< qddlOmPZPmQZQmRZRmSZSm%Z% ddlTmTZT ddlUmVZVmWZWmXZX ddlYmZZZ ddlm[Z[m\Z\ ddl%m]Z] e^e_Z`d dlambZb d dlcmdZd e%e ZfejgZhd dliZiG dd deZjejjkZle
m Zne
o Zpd d! Zqd"d# Zrejsdd&d'Zteue;eeeevd(hZwdd-d.ZxG d/d0 d0e"jyjzZ{d1d2 Z|d3d4 Z}d5d6 Z~e
o ZeG d7d8 d8Zd9d: Zejsdd?d@ZddAdBZddDdEZG dFdG dGZG dHdI dIeZG dJdK dKeZG dLdM dMeZdNdO ZddRdSZehdddTdUfddVdWZdXdY ZG dZd[ d[ejgZd\d] Zd^d_ Z	`ddUdddUddTdadbdcZe!dddTdedf ZG dgdh dhe"j:jjZG didj djeZdkdl Zdmdn ZdUdUddoddUdTdUdpdddZe@dddUdddTdddZG dd dZdS )    )annotationsN)	dataclass)Enum)dirnamejoin)
AnyCallableDictList
NamedTupleOptionalSetTupleTYPE_CHECKINGUnion)patch)_guards)fake_tensor)
Constraint)make_fxmaybe_disable_fake_tensor_mode)ConstraintViolationError
DimDynamicStatelessSymbolicContext)_PyTreeCodeGen_PyTreeInfo)DistributedDataParallel   )GraphModule   )
CompilerFnlookup_backend)Hooks)
reset_codeset_eval_frameset_guard_error_hook	skip_codeunsupported__)configconvert_frameexternal_utils	skipfilesutils)code_context)CondOpArgsMismatchError	UserErrorUserErrorTypeinstall_generation_tagging_init)
CacheEntryDynamoCallback)compile_times)enable_python_dispatcher)_disable_current_modesc                   @  s   e Zd ZdZdS )Unsetr   N)__name__
__module____qualname__token r>   r>   CC:\wamp64\www\opt\env\Lib\site-packages\torch/_dynamo/eval_frame.pyr9   Y   s    r9   c                   C  s8   t tdsdt_t tdsd t_t tdsi t_d S d S )N$skip_backend_check_for_run_only_modeFcurrent_backendcached_backends)hasattrguarded_backend_cacher@   rA   rB   r>   r>   r>   r?   !_maybe_init_guarded_backend_cachec   s   



rE   c                  C  sJ   t   dt_d t_tj} |  D ]}t|dr|  q|   i t_d S )NFreset)	rE   rD   r@   rA   rB   valuesrC   rF   clear)rB   backendr>   r>   r?   _reset_guarded_backend_cachel   s   

rJ   callbackr5   c                 c  sr    t   | du rztj}dt_d V  W |t_d S |t_w t| }ddd}||}z
|V  W || d S || w )NFTrI   r    c                 S  s   t j}| t _| t jt| < |S N)rD   rA   rB   id)rI   prev_backendr>   r>   r?   _set_current_backend   s   z3backend_cache_wrapper.<locals>._set_current_backend)rI   r    )rE   rD   r@   innermost_fn)rK   Z	prev_skiprI   rO   rN   r>   r>   r?   backend_cache_wrapperx   s   
rQ   z+onnx/_internal/fx/dynamo_graph_extractor.pycode)Union[types.CodeType, Callable[..., Any]]returnList[CacheEntry]c                 C  sF   t | r| j} tjjj| }g }|dur!|| |j}|dus|S )zh
    Given a code object or a callable object, retrieve the cache entries
     stored in this code.
    N)	callable__code__torch_C_dynamo
eval_frame_debug_get_cache_entry_listappendnext)rR   Z
cache_headZ
cache_listr>   r>   r?   r\      s   
r\   c                      sh   e Zd ZU dZded< ded< d fdd	Zd
d Zdd Zdd Zdd Z	dd Z
 fddZ  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modtorch.nn.Modulec                   s"   t    || _|| _|   d S rL   )super__init__	_orig_mod
dynamo_ctx_initialize)selfrc   rh   	__class__r>   r?   rf      s   
zOptimizedModule.__init__c                 C  sh   t | jjtjrt| jjr| t	| j| _n| | jj
| _t| jdr2| j| _| j| _d S d S NZ_initialize_hook)
isinstancerg   forwardtypes
MethodTyper,   checkrh   r+   wrap_inline__call__rC   _forward_call_lazy_checkrj   r>   r>   r?   ri      s   zOptimizedModule._initializec                 C  s&   t | j}|dd  |dd  |S )Nro   rt   )dict__dict__poprj   stater>   r>   r?   __getstate__   s   
zOptimizedModule.__getstate__c                 C  s   || _ |   d S rL   )ry   ri   r{   r>   r>   r?   __setstate__   s   zOptimizedModule.__setstate__c                 C  s   |dkr	| j d S t| j|S )Nrg   )Z_modulesgetattrrg   )rj   namer>   r>   r?   __getattr__   s   
zOptimizedModule.__getattr__c                 O  s.   t | jdr| j| j|| | j|i |S rm   )rC   rg   Z_infer_parametersru   )rj   argskwargsr>   r>   r?   rv      s   z OptimizedModule._call_lazy_checkc                   s&   | j     fddt  D  S )Nc                   s   g | ]}| vr|qS r>   r>   ).0attrZorig_mod_attrsr>   r?   
<listcomp>   s    z+OptimizedModule.__dir__.<locals>.<listcomp>)rg   __dir__re   rw   rk   r   r?   r      s   
zOptimizedModule.__dir__)rc   rd   )r:   r;   r<   __doc____annotations__rf   ri   r}   r~   r   rv   r   __classcell__r>   r>   rk   r?   r_      s   
 	r_   c                 C  st   t | tjrt|  dS t| drt| j dS tt| dddr)t| jj dS ddlm	} |  t
d|  dS )zA
    Make sure f.__code__ is not cached to force a recompile
    rW   ro   Nr   rF   z#could not determine __code__ for %s)rn   rp   CodeTyper#   rC   rW   r   ro    rF   logwarning)frF   r>   r>   r?   remove_from_cache   s   
r   c                   C  s   d S rL   r>   r>   r>   r>   r?   nothing  s   r   c                 C  s.   | }t |dr|j}t|sJ t |ds|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.
    ra   )rC   ra   rV   )fnZunaltered_fnr>   r>   r?   rP     s   

rP   c                   @     e Zd ZU ded< ded< dS )ConfigAndHashzDict[str, Any]r)   byteshashNr:   r;   r<   r   r>   r>   r>   r?   r     s   
 r   c                   C  s   t tdsd t_d t_d S d S )Nsaved_config_and_hash)rC   config_cacher   nopythonr>   r>   r>   r?    _maybe_init_guarded_config_cache  s   

r   	first_ctxboolr   r   c                 c  s    t   d}z?| r'tjd u r'tjd u sJ d}|t_|t_td|j  ntd d V  W |rDtdtjj  d t_d t_d S d S |rXtdtjj  d t_d t_w )NFTz)Setting top-level compile config hash: %sz-Ignoring inner dynamo compile config and hashz+Unsetting top-level compile config hash: %s)r   r   r   r   r   debugr   hex)r   r   r   Zis_top_levelr>   r>   r?   restore_guarded_dynamo_config$  s:   



r   c                 C  s4   | d u ri }n| rddi}nddd}t t| S )Nassume_static_by_defaultFT)automatic_dynamic_shapesr   )r   r)   Z get_config_and_hash_with_updates)dynamicZupdatesr>   r>   r?   _get_config_and_hashB  s   

r   r   c                   C  s    t   tjd urtjjS t S rL   )r   r   r   r   r)   Zget_hashr>   r>   r>   r?   "get_saved_else_current_config_hashL  s   
r   c                      sT   e Zd Zeeedfdddddd fddZd	d
 Zdd Zdd Zdd Z	  Z
S )_TorchDynamoContextFNT)r   compiler_configsave_configr   rK   r5   c          
        sx   t    t|s|du s|d u sJ || _t| _|| _|| _|| _|| _	|| _
|o+|| _|	| _| jr7|   |  d S NF)re   rf   rV   rK   unsetprioron_enterextra_ctx_ctorr   r   r   r   r   save_and_hash_config)
rj   rK   r   backend_ctx_ctorpatch_fnr   r   r   r   r   rk   r>   r?   rf   U  s   


z_TorchDynamoContext.__init__c                 C  s$   t | j| _td| jj  d S )NzBSaving dynamo config and hash for new compiled object(s). Hash: %s)r   r   r   r   r   r   r   rw   r>   r>   r?   r   q  s
   
z(_TorchDynamoContext.save_and_hash_configc                 C  sx   t jrtd|   t| j| _t| j| _| j	  | 
 | _| j	  | jr:t| j| j| j| _| j	  d S d S )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)   Zraise_on_ctx_manager_usageRuntimeErrorr   r$   rK   r   rQ   backend_cache_manager	__enter__r   backend_ctxr   r   r   r   r   dynamo_config_ctxrw   r>   r>   r?   r   y  s    


z_TorchDynamoContext.__enter__c                 C  sX   | j tusJ t| j  t| _ | jr| j||| | j||| | j||| d S rL   )r   r   r$   r   r   __exit__r   r   )rj   exc_typeexc_valexc_tbr>   r>   r?   r     s   
z_TorchDynamoContext.__exit__c                   sf  fdd}t ttjjrtjjd< ttj	j
r8}t|}|j|_t|dr3J ||_|S ts>J zt}W n tyP   d }Y nw |d u sZtrktdddvrk|tvrktjjj t fdd	}ttrd
|_n|_ |_t|drJ ||_dvrtdst!t"#dd
t$j< |S )Nc                     s    j S rL   )r   r>   rw   r>   r?   rb     s   z9_TorchDynamoContext.__call__.<locals>.get_compiler_configZorig_graphmodulerb   r:   r   )Z
_call_implZ_wrapped_call_implc                    s*  t tstjj rtjrtd| i |S tj	
 r,tjr%td| i |S   t}tj}|    }|  jrStjjj}|  z$| i |W t| jri|d d d  |d d d  |d d d  S t| jr|d d d  |d d d  |d d d  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.)rn   DisableContextrX   fx_symbolic_traceZis_fx_tracingr)   Zerror_on_nested_fx_tracer   jit
is_tracingZerror_on_nested_jit_tracer$   rQ   rK   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   rK   r   r   rj   r>   r?   _fn  sL   


z)_TorchDynamoContext.__call__.<locals>._fnTr   rW   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):
                        >>         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)
                        )%rP   rn   rX   r   r   r.   Zget_contextro   rW   nnModuler_   ra   rC   rb   rV   inspectgetsourcefile	TypeErrorr,   rr   r   DONT_WRAP_FILESr+   rs   rK   r   r   	functoolswrapsr   Z_torchdynamo_disableZ_torchdynamo_inliner   textwrapdedentalways_optimize_code_objects)rj   r   rb   rc   new_modfilenamer   r>   r   r?   rt     sR   


+

!z_TorchDynamoContext.__call__rK   r5   )r:   r;   r<   r   null_contextrf   r   r   r   rt   r   r>   r>   rk   r?   r   T  s    
r   c                      s,   e Zd Z	dddddd fddZ  ZS )OptimizeContextFNT)r   r   r   r   c          	        s,   dd }t  j|||tj|||||d	 d S )Nc                   S  s
   t   d S rL   r2   r>   r>   r>   r?   r   6     
z*OptimizeContext.__init__.<locals>.on_enter)	rK   r   r   r   r   r   r   r   r   )re   rf   TorchPatcherr   )	rj   rK   r   r   r   r   r   r   r   rk   r>   r?   rf   +  s   
zOptimizeContext.__init__)Fr:   r;   r<   rf   r   r>   r>   rk   r?   r   *  s    r   c                         e Zd Z fddZ  ZS )RunOnlyContextc                   s   dd }t  jd|d d S )Nc                   S  s   t jjj jd7  _d S )Nr   )rX   rZ   mutation_guardZGenerationTrackerZ
generationr>   r>   r>   r?   r   I  s   z)RunOnlyContext.__init__.<locals>.on_enterF)rK   r   re   rf   )rj   r   rk   r>   r?   rf   G  s   zRunOnlyContext.__init__r   r>   r>   rk   r?   r   F      r   c                      r   )r   c                   s   t  jd d d S )N)rK   r   rw   rk   r>   r?   rf   P  s   zDisableContext.__init__r   r>   r>   rk   r?   r   O  r   r   c                 C  s>   t jdk rdS t| D ]}|jdkr|jd   S qtd)N)      r   RESUMEr   z$RESUME instruction not found in code)sysversion_infodisget_instructionsopnameoffsetr   )rR   instr>   r>   r?   first_real_inst_idxT  s   

r   hooksr"   c                   s"   t   fdd} |_|S )Nc           	   	     s  |d usJ t | j}| jt| jks|stjrFtt	j
rD| jt| jkr)dn	t | jr1dnd}|r8tjrDtd| jj|| jj d S | jjdkrT| jjdkrTd S tjrt }|rt, ddlm} ||j jd	}t d
swJ d |j}|| ||W  d    S 1 sw   Y  t* t   | ||W  d    W  d    S 1 sw   Y  W d    d S 1 sw   Y  d S )Nztraced frame alreadyzin skipfileszdynamo tracing is disabledz#skipping: %s (reason: %s, file: %s)z<string>__new__r   )DDPOptimizer)bucket_bytes_capZbackend_compile_fn_clone_with_backendzJDDPOptimizer only supports callback fns that know how to clone themselves.)r,   rr   f_codef_lastir   r)   disabler   isEnabledForloggingDEBUGverboser   co_nameco_filenameZoptimize_ddpr   Z_get_active_ddp_modulecompile_lockZ"torch._dynamo.backends.distributedr   r   ra   rC   r   
compile_fnr8   )	framecache_entryZframe_stateZis_skipfileskip_reasonZ
ddp_moduler   Zddp_optimizerZhijacked_callbackrK   r   r>   r?   catch_errors^  s\   

 Rz*catch_errors_wrapper.<locals>.catch_errors)r   r   ra   )rK   r   r   r>   r   r?   catch_errors_wrapper]  s   2r   TFc              	   C  s   t t| ||d||||dS )NT)r   r   r   r   r   r   )r   r   )r   r   r   r   r   r   r   r>   r>   r?   _optimize_catch_errors  s   	r   c                 C  sD   ddl m} t| dr| j}n
t| tr| }nd }t| } || |S )Nr   )wrap_backend_debugcompiler_name)Zrepro.after_dynamor   rC   r   rn   strr!   )Zcompiler_fnr   Zcompiler_strr>   r>   r?   get_compiler_fn  s   


r   c                   @  s   e Zd Zdd ZdS )_NullDecoratorc                 C  s   t |sJ |S rL   )rV   )rj   r   r>   r>   r?   rt     s   z_NullDecorator.__call__N)r:   r;   r<   rt   r>   r>   r>   r?   r    s    r  c                   C  s(   t jdkr	tdt jdkrtdd S )Nwin32z+Windows not yet supported for torch.compile)r      z0Python 3.12+ not yet supported for torch.compile)r   platformr   r   r>   r>   r>   r?   check_if_dynamo_supported  s
   

r  c                   C  s"   zt   W dS  ty   Y dS w )NTF)r  	Exceptionr>   r>   r>   r?   is_dynamo_supported  s   r  inductor)r   guard_export_fnguard_fail_fnr   r   r   c          	   	   C  s   t   t||d}tjd |stjdddkrt S t	| } t
| dt}|r1t| |||dS ttj| |d||||t| d	rG|  dS d
dS )an  
    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.
        save_config: If True, recompiling this function will first restore the dynamo config
            at the time when `optimize` was first called, for the duration of the compilation
            process.
    Example Usage::

        @torch._dynamo.optimize()
        def toy_example(a, b):
            ...
    r	  r
  ztorch._dynamo.optimizeZTORCHDYNAMO_DISABLEr   1r   )r   r   r   )r   rb   N)r   r   r   )r  r"   rX   rY   _log_api_usage_onceosenvirongetr  r   r   r   optimize_assertr   r*   rC   rb   )	rI   r   r	  r
  r   r   r   r   r   r>   r>   r?   optimize  s2   &r  z&torch._dynamo.symbolic_convert.explainc                   s0    fdd}|s
|rt d ||i |S |S )Nc               	     s  ddl m} |  g g  dg g d fdd}fdd	}t|d
|d}|| i | t}i } D ]}|jd }	||t|	< q;d}
t| D ]\}}dt	
|j}|d  d|j d| d}|
|7 }
qQ|d }tdd}|  ddlm} ||| |S )Nr   r   r   gmtorch.fx.GraphModulec                   s,   ddl m} ||  \}  | jS )Nr   )_explain_graph_detail)backends.debuggingr  ro   )r  example_inputsr  )break_reasonsgraphsop_countops_per_graphr>   r?   "dynamo_graph_accumulating_compiler#  s
   
zBexplain.<locals>.inner.<locals>.dynamo_graph_accumulating_compilerc                   s     |  d S rL   )extendguards
out_guardsr>   r?   guard_export_print3  s   z2explain.<locals>.inner.<locals>.guard_export_printF)r   r	  r   z
. Reason: z
   User Stack: 
r   )repr)ExplainOutputr  r  )r   rF   r  lenZ
user_stackr%  	enumeraterG   r   	tracebackformat_listreasonr6   r  r&  )r   r   rF   r  r"  opt_fZgraph_countZdeduped_reasonsr,  Zinnermost_frameZformatted_listidxZbreak_reasonZformatted_stackmsgZgraph_break_countZcompile_timer&  r   )r  r  r  r  r!  r?   inner  sR   


zexplain.<locals>.innerzexplain(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).warningswarn)r   
extra_argsextra_kwargsr1  r>   r0  r?   explain  s   Ir7  c                      sB   e Zd Z	dd fddZdd Z fddZ fddZ  ZS )FlattenInputOutputSignatureNmr  	flat_args
Tuple[Any] matched_input_elements_positions	List[int]!matched_output_elements_positionsexample_fake_inputsList[torch.Tensor]flat_args_dynamic_dimsList[Set[int]]	fake_mode$Optional[fake_tensor.FakeTensorMode]c           
   	     s  t  | tt|ksJ  fddt|D }g _tdt|D ]Tt  d di }	|v r?| |	jjd< n4|d urst	| t
jrs|j| tfddtt| jD d gt| j d	d
|	jjd< j|	 q%fdd|D _|_d S )Nc                   s   i | ]	\}}| | qS r>   r>   )r   Zixval)r?  r>   r?   
<dictcomp>y  s    z8FlattenInputOutputSignature.__init__.<locals>.<dictcomp>r   argr>   rE  c                   s$   g | ]}|  v rt jnt jqS r>   )r   ZDYNAMICZSTATIC)r   d)rA  ir>   r?   r     s    z8FlattenInputOutputSignature.__init__.<locals>.<listcomp>)Zdynamic_sizesZconstraint_sizes)Zsymbolic_contextc                 3  s    | ]} j | V  qd S rL   )new_argsr   rI  rw   r>   r?   	<genexpr>  s    z7FlattenInputOutputSignature.__init__.<locals>.<genexpr>)re   rf   r(  r)  rJ  rangeplaceholdernodemetarn   rX   Tensorfrom_tensorr   shaper]   old_args_genr>  )
rj   r9  r:  r<  r>  r?  rA  rC  Zmatched_input_elements_to_fakerG  rk   )r?  rA  rI  rj   r?   rf   l  s.   


z$FlattenInputOutputSignature.__init__c                 C  sn   t | j}d| jjv r| jjd |jjd< d| jjv r%| jjd |jjd< d| jjv r5| jjd |jjd< |S )NrE  Ztensor_dictexample_value)r^   rT  current_noderP  rO  )rj   targetr   r   rG  r>   r>   r?   rN    s   
z'FlattenInputOutputSignature.placeholderc                   s<   |d }g || j   fdd| jD }t ||fi S )Nr   c                   s   g | ]} | qS r>   r>   rK  lookupr>   r?   r     s    z6FlattenInputOutputSignature.output.<locals>.<listcomp>)rJ  r>  re   output)rj   rW  r   r   Zdynamo_result_flatZnew_result_flatrk   rX  r?   rZ    s   z"FlattenInputOutputSignature.outputc                   s|   || _ t |}d| j jv r| j jd |jjd< d| j jv r)| j jd |jjd< | j jdkr<|jt| j d|jj |S )NrE  rU  rZ  r   )	rV  re   run_noderP  rO  opZ_renamer   r   )rj   nZresult_proxyrk   r>   r?   r[    s   z$FlattenInputOutputSignature.run_noderL   )r9  r  r:  r;  r<  r=  r>  r=  r?  r@  rA  rB  rC  rD  )r:   r;   r<   rf   rN  rZ  r[  r   r>   r>   rk   r?   r8  k  s    	,
r8  c                   @  r   )ExportResultr  Zgraph_module_guards.GuardsSetr  Nr   r>   r>   r>   r?   r^    s   
 r^  c           
      C  s   g }| j jD ]j}|jdkrpt|dsJ |j}| j|}|d u r"qt|dks*J d }|D ]}t|dkr7q.|} |d u rF|  d}n%d	t
|}d}	t|dkr`dt|d  d}	|  d	| |	 }|| q|r~ttjd
d	| d S )NrN  _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nodesr\  rC   r`  _source_to_user_stacksr  r(  r   r   r*  r+  r]   r0   r1   ZINVALID_INPUT)
ra  Zinput_errorsrO  sourceZuser_stacksstacksr/  tbextrar>   r>   r?   check_signature_rewritable  s@   

ri  c
                   s   t ||\}
}tjtjtjtjffdd  fdd}|d|id|id}t |\}}|d us6J |t||dd	|id}t	||||||	|
 }ddd}tt|| |
||||j_|  |S )Nc                   s
   t |  S rL   )rn   )rE  )supported_typesr>   r?   is_supported_type  r   z,rewrite_signature.<locals>.is_supported_typec           
   
     s   d dd |  D }dd |  D }g }i }t|D ]
\}}||t|< q| D ]D\}}	t|	D ];\}} |r_t||v rL||t|  q4t| d|d  dt| d| t| d|d  d	| d
 q,|S )Nz or c                 S  s0   g | ]\}}|d  d dd |D  d qS )z of types: (z, c                 S  s   g | ]}t t|qS r>   )r   type)r   rE  r>   r>   r?   r     s    zJrewrite_signature.<locals>.produce_matching.<locals>.<listcomp>.<listcomp>))r   )r   descvalsr>   r>   r?   r     s    z?rewrite_signature.<locals>.produce_matching.<locals>.<listcomp>c                 S  s   g | ]	}|D ]}|qqS r>   r>   )r   ro  rE  r>   r>   r?   r     s    z #r   z
, of type z, is not among z is z., but only the following types are supported: )r   itemsrG   r)  rM   r]   AssertionErrorrl  )
sources
candidatesZsource_typesZsource_valsZmatched_elements_positionsZdict_of_source_valsrI  rE  Zcandidate_descZcandidate_valsrk  rj  r>   r?   produce_matching  s2   	 z+rewrite_signature.<locals>.produce_matchingoriginal inputszgraph-captured input)rr  rs  )zgraph-captured outputsrv  zoriginal outputrT   	List[str]c                   s  ddd}||   j d t| }t|t j kr9 jd us$J d| fddtdt|t| D 7 }n&t|t j k r_ j t|t jpLg   D ]}||v s^J d	| qQ|t| 7 } jD ]} jppi }||v s||v sJ d
| qj|S )Nsiginspect.Signaturec           
   	   S  s   t | j }dd |D }dd |D }tdd |D d }tdd |D d }tdd |D }dd	 |D }i }| jrCd
| ji}|D ]}	|	j||	j< qEt	|||||||S )Nc                 S      g | ]}|j tjjkr|jqS r>   )kindr   	ParameterPOSITIONAL_OR_KEYWORDr   r   pr>   r>   r?   r   =  s
    z_rewrite_signature.<locals>.argument_names.<locals>.signature_to_fullargspec.<locals>.<listcomp>c                 S  rz  r>   )r{  r   r|  KEYWORD_ONLYr   r~  r>   r>   r?   r   B  s    c                 s  $    | ]}|j tjjkr|jV  qd S rL   )r{  r   r|  VAR_POSITIONALr   r~  r>   r>   r?   rL  F     " z^rewrite_signature.<locals>.argument_names.<locals>.signature_to_fullargspec.<locals>.<genexpr>c                 s  r  rL   )r{  r   r|  VAR_KEYWORDr   r~  r>   r>   r?   rL  J  r  c                 s  s2    | ]}|j tjjkr|jtjjur|jV  qd S rL   )r{  r   r|  r}  defaultemptyr~  r>   r>   r?   rL  N  s    c                 S  s2   i | ]}|j tjjkr|jtjjur|j|jqS r>   )r{  r   r|  r  r  r  r   r~  r>   r>   r?   rF  T  s    z_rewrite_signature.<locals>.argument_names.<locals>.signature_to_fullargspec.<locals>.<dictcomp>rT   )
list
parametersrG   r^   tuplereturn_annotation
annotationr   r   FullArgSpec)
rx  paramsr   
kwonlyargsvarargsvarkwdefaultskwonlydefaultsr   Z	parameterr>   r>   r?   signature_to_fullargspec9  s:   
zKrewrite_signature.<locals>.argument_names.<locals>.signature_to_fullargspeczMore arguments than expectedc                   s   g | ]
} j  d | qS )_)r  rK  Zfullargspecr>   r?   r   n  s    z=rewrite_signature.<locals>.argument_names.<locals>.<listcomp>r   zMissing argument zMissing keyword only argument )rx  ry  )	r   r(  r  rM  r  r  keysr  r  )f_sigr   r   r  Z
input_strsZunprovided_argZ
kwonly_argr  r>   r  r?   argument_names8  s*   
,


z)rewrite_signature.<locals>.argument_names)rT   rw  )pytreeZtree_unflattenrX   rQ  ZSymIntZSymFloatZSymBooltree_flattenr  r8  Z	transformr   r   ra  Z_codegenZ	recompile)r  ra  rC  r:  in_specr?  graph_captured_inputZgraph_captured_outputZdynamo_traced_resultrA  	orig_argsZorig_kwargsru  r<  Zflat_results_tracedZout_spec_tracedr>  Z	new_graphr  r>   rt  r?   rewrite_signature  sJ   !
R
r  symbolic)
aten_graphpre_dispatchdecomposition_tabletracing_modeconstraintsr   same_signaturedisable_constraint_solverr   r`   r  r  r  9Optional[Dict[torch._ops.OpOverload, Callable[..., Any]]]r  r   r  Optional[List[Constraint]]r   r  r  Callable[..., ExportResult]c             	     sH   | |  f	dd}|	s|
r"t d ||	i |
S |S )aL  
    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".

        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.
    c                    s  }}t   tjd 
d ursJ drsJ dt|}t|tjjr-|jn|}t	
|}d d d d d d)fdd}g  d* fd
d}t| |f\}}	t| d }
dkrgd}tjd|dddd4 t|t|d dd	d|}z	|| i |}W n ty } z|}
W Y d }~nd }~ww W d    n1 sw   Y  t| stdd  }d ur|j }d urt|s|  |  | }|||
|}|
r|
jd | f|
_n|rt|}
ntd| |j D ]}t|t j!rtd"t#$|j%|  d| d}
q|
r |
d us)J dt&ds1J d us:J dd usAJ rHt' fdd D }rfdd}t( U t) A . zt*|
dddd | W n t+y } z
t,t-j.t/|d!d"d }~ww W d    n	1 sw   Y  W d    n	1 sw   Y  W d    n	1 sw   Y  j0j1D ]"}|j2d#krtt|j3tj4rj5t|j3dd$|j6d%< qr	fd&d|D }t7|||	|||
	rd'd 	D ng j6d(< t8S )+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=Truer  r_  c                   s    d u sJ d|  d S )Nz3whole graph export entails exactly one guard exportr>   r  r   r>   r?   r"    s   
z1export.<locals>.inner.<locals>.guard_export_printr  r  c                   s6   d u sJ d| t  | fdd}|S )Nz\Tried to emit a second graph during export. Tracing through 'f' must produce a single graph.c            	   	     s  | d usJ t jdd}t jdd}t| d ur$t| n }|R t 6 i t |t |}t  }| D ]\}}|j|dd||< q>t	|j| }t
j||W d    n1 sew   Y  W d    S W d    S 1 s}w   Y  S )NF)Zremove_duplicateTZstatic_shapes)rx   named_parametersnamed_buffersr   detect_fake_moder7   rp  rR  r  Ztree_maprX   funcZfunctional_call)	Zgraph_inputsr  r  Zambient_fake_modeZparams_and_buffersZfake_params_buffersr   valueZfake_graph_inputs)rC  ra  r  graph_captured_resultr>   r?   result_capturing_wrapper  s>   
(zhexport.<locals>.inner.<locals>.dynamo_normalization_capturing_compiler.<locals>.result_capturing_wrapper)r   r  )r  Zinner_example_inputsr  )r  rC  ra  r  r  r>   r?   'dynamo_normalization_capturing_compiler  s   
%zFexport.<locals>.inner.<locals>.dynamo_normalization_capturing_compilerr  TF)Zspecialize_intr   r   Z capture_dynamic_output_shape_opsZcapture_scalar_outputsr  )r   exportexport_constraints	shape_envr   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 z1. Scroll up to see where this constraint was set.zZFailed to produce a graph during tracing. Tracing through 'f' must produce a single graph.rc  z'Failed to produce guards during tracingc                   s   g | ]}  |qS r>   )rR  )r   t)rC  r>   r?   r   u  s    z)export.<locals>.inner.<locals>.<listcomp>c                    sB   t jj  t j j|  W  d    S 1 sw   Y  d S rL   )rX   r   r*  Zpreserve_node_metaZInterpreterrun)r   )ra  r>   r?   graph_with_interpretery  s   $z5export.<locals>.inner.<locals>.graph_with_interpreterreal)r  r  Z_allow_non_fake_inputsr  Z_allow_fake_constantZcond_operands)Z	case_nameZget_attrr  rE  c                   s"   g | ]  fd dpdD qS )c                   s   h | ]}|   u r|jqS r>   )Zw_tensordim)r   cxr>   r?   	<setcomp>  s    z3export.<locals>.inner.<locals>.<listcomp>.<setcomp>r>   r>   )r   )r  r  r?   r     s    c                 S  s   g | ]}|j qS r>   )Zserializable_spec)r   
constraintr>   r>   r?   r     s    Zinput_shape_constraints)r  r_  r'  )9r  rX   rY   r  rP   rn   r   r   ro   r   	signaturer  r  r   r)   r   r  r"   r   r   dim_constraintsr,   rr   ZsolveZ remove_redundant_dynamic_resultsforced_specializationsZprettify_resultsr   r   infoZvar_to_ranger  sympyIntegerr   r*  r+  Zvar_to_stackrC   ri  r   r7   r   r/   r0   r1   ZDYNAMIC_CONTROL_FLOWr   ra  rb  r\  rW  rQ  rR  rP  r  r^  )r   r   r   r   Zcall_to_inspectZoriginal_signaturer"  r  r:  r  Zconstraint_violation_errorr-  Zresult_traceder  r  r  r/  kr?  r  rO  rA  	Z_assume_static_by_defaultZ_fr  r  r  r  r  r  r  )r  rC  ra  r  r  r!  r?   r1    s2  
;

  

zexport.<locals>.innerzexport(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).r2  )r   r  r  r  r  r  r   r  r  r5  r6  r1  r>   r  r?   r    s   6 fr  )r   r  r  r   r   c                C  s4   t | } t| dt}ttj| ||d||||ddS )zF
    The same as `torch._dynamo.optimize(backend, nopython=True)`
    r   )r  r  T)r   r   r   )r   r   r   r   r*   Zconvert_frame_assert)rI   r   r  r  r   r   r   r>   r>   r?   r    s   r  c                   @  s.   e Zd Zeeddd Zedd ZdS )r   Nc                  C  s  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d ddlm}m}m}m}m}m}m}m}m}	m}
m}m}m} |||||||||	|
|||h}||	|h}|D ]8}|jdd }d| }d	| }t||r||v rt||| t|| t||rt||| t|| qed
d tjj ! D }tjj"tjj#tjj$h}|D ]0}||v r| |j%|_%t|dr| |j&|_&t|j%dd}|rt|j%dd }|r||_%d|j%_'qd S )Nr   )r   Fr   )adadeltaadagradadamadamaxadamwasgdlbfgsnadamradamrmsproprpropsgdsparse_adam.r#  Z_multi_tensor_Z_fused_c                 S  s(   g | ]}t |rt|tjjr|qS r>   )r   isclass
issubclassrX   optimZ	Optimizer)r   optr>   r>   r?   r   %  s    z&TorchPatcher.patch.<locals>.<listcomp>_init_grouphooked__wrapped__T)(Z
decoratorsr   rX   r   traceZtrace_moduleZ_get_trace_graphr   r   ZTracerdistributionsDistributionZset_default_validate_argsr  r  r  r  r  r  r  r  r  r  r  r  r  r  r:   splitrC   setattrr   ry   rG   Z
SparseAdamZRAdamZLBFGSstepr  r  )r   r  r  r  r  r  r  r  r  r  r  r  r  r  Zoptimizer_modulesZ!disabled_multi_tensor_opt_modulesZopt_modZopt_nameZmulti_tensor_fn_nameZfused_fn_nameZoptimizer_classesZexcluded_optimizer_classesr  r  Zunwrapped_stepr>   r>   r?   r     s~   
<





zTorchPatcher.patchc                   s    fdd}|S )Nc                    s   t jdtdd  | i |S )Nignoreztorch.distributed)categorymodule)r3  filterwarningsUserWarning)r   r   r   r>   r?   inner_fnF  s   zBTorchPatcher.suppress_torch_distributed_warnings.<locals>.inner_fnr>   )r   r  r>   r  r?   #suppress_torch_distributed_warningsD  s   z0TorchPatcher.suppress_torch_distributed_warnings)r:   r;   r<   staticmethodr   	lru_cacher   r  r>   r>   r>   r?   r     s    fr   r   )rR   rS   rT   rU   )r   r   r   r   r   r   rL   )rT   r   )r   r"   )r  )r   r`   r  r   r  r   r  r  r  r   r  r  r   r   r  r   r  r   rT   r  )
__future__r   
contextlibr   r   r   r   r  r   r   	threadingr*  rp   r3  dataclassesr   enumr   Zos.pathr   r   typingr   r   r	   r
   r   r   r   r   r   r   Zunittest.mockr   rX   Ztorch.fxZtorch.utils._pytreer-   Z_pytreer  Ztorch.utils.checkpointr   Ztorch._subclassesr   Ztorch.exportr   Z"torch.fx.experimental.proxy_tensorr   r   Z%torch.fx.experimental.symbolic_shapesr   r   r   Ztorch.fx.graphr   r   Ztorch.nn.parallel.distributedr   r   r   Zbackends.registryr    r!   r   r"   Ztorch._C._dynamo.eval_framer#   r$   r%   r&   r'   dirrY   rZ   r[   r   
startswithr   globalsr   r)   r*   r+   r,   r.   excr/   r0   r1   r   r3   r4   r5   r6   	getLoggerr:   r   Ztorch._dispatch.pythonr7   Ztorch.utils._python_dispatchr8   ZExactWeakKeyDictionaryr   nullcontextr   r  r9   r=   r   RLockr   localrD   rE   rJ   contextmanagerrQ   r   __file__r   r\   r   r   r_   r   r   rP   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r7  interpreterZTransformerr8  r^  ri  r  r  r  r   r>   r>   r>   r?   <module>   s   0

	!
@


 W	
	;	J
UM* 0  .