a
    dFl                     @   s6  U 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mZmZ d dlmZ d dlZd dlm  mZ d dlmZ d dlmZmZ d dlmZ dd	l m!Z!m"Z" dd
l#m$Z$ erd dl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ n<e,ej-j.j/D ],Z0e01dr2qe2ej-j.j/e0e3 e0< qddl4m5Z5m6Z6m7Z7mZ ddl8m9Z9 ddl:m;Z; ddl	m<Z< ddlm=Z= e>e?Z@d dlAmBZB eC ZDe jEZFG dd deZGeGjHZIeJ ZKdaLee! eMd< G dd dejNjOZPdd ZQdd ZRdd ZSe jTdNeUdd d!ZVG d"d# d#ZWG d$d% d%eWZXG d&d' d'eWZYG d(d) d)eWZZe$d*d+d,Z[eFd-fe$d*d.d/Z\d0d1 Z]G d2d3 d3e jEZ^d4d5 Z_dOd-ddd-d-d7d8d9Z`ed:dd;d< Zad-dd=d>d?d@ZbdAdB Zce$ddd-d-dCdDdEZddPdFdGZedQdHdIZfdRdJdKZgG dLdM dMZhdS )S    N)Enum)OptionalTupleTYPE_CHECKINGUnion)patch)make_fx)_PyTreeCodeGen_PyTreeInfo)DistributedDataParallel   )
CompilerFnlookup_backend)Hooks)
reset_codeset_eval_frameset_guard_error_hookset_guard_fail_hook	skip_codeunsupported__)configconvert_frame	skipfilesutils)ResetRequired)install_generation_tagging_init)DynamoCallback)compile_times)proxy_tensorc                   @   s   e Zd ZdZdS )Unsetr   N)__name__
__module____qualname__token r%   r%   a/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/_dynamo/eval_frame.pyr    6   s   r    most_recent_backendc                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )OptimizedModulezx
    Wraps the original nn.Module object and later patches its
    forward method to optimized self.forward method.
    c                    s   t    || _|| _d S N)super__init__	_orig_mod
dynamo_ctx)selfmodr-   	__class__r%   r&   r+   F   s    
zOptimizedModule.__init__c                 C   s   |dkr| j d S t| j|S )Nr,   )Z_modulesgetattrr,   )r.   namer%   r%   r&   __getattr__L   s    
zOptimizedModule.__getattr__c                 O   s   |  | jj|i |S r)   )r-   r,   forward)r.   argskwargsr%   r%   r&   r5   Q   s    zOptimizedModule.forward)r!   r"   r#   __doc__r+   r4   r5   __classcell__r%   r%   r0   r&   r(   @   s   r(   c                 C   sn   t | tjrt|  nTt| dr,t| j n>tt| dddrLt| jj nddlm	} |  t
d|  dS )zA
    Make sure f.__code__ is not cached to force a recompile
    __code__r5   Nr   resetz#could not determine __code__ for %s)
isinstancetypesCodeTyper   hasattrr:   r2   r5    r<   logwarning)fr<   r%   r%   r&   remove_from_cacheU   s    

rE   c                   C   s   d S r)   r%   r%   r%   r%   r&   nothingf   s    rF   c                 C   s&   | }t |dr"|j}t|sJ q|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.
    _torchdynamo_orig_callable)r@   rG   callable)fnZunaltered_fnr%   r%   r&   innermost_fnj   s
    
rJ   Tenablec                 c   sF   | sd V  d S t jddd d V  W d    n1 s80    Y  d S )NTF)Zdynamic_shapesZspecialize_int_float)r   r   rK   r%   r%   r&   enable_dynamicw   s
    rM   c                       sJ   e Zd Zeeedfdded fddZdd Zdd	 Zd
d Z	  Z
S )_TorchDynamoContextFdynamiccallbackc                   sT   t    t|s&|du s&|d u s&J || _t| _|| _|| _|| _|| _	|  d S NF)
r*   r+   rH   rR   unsetprioron_enterextra_ctx_ctor	first_ctxrP   )r.   rR   rV   backend_ctx_ctorpatch_fnrX   rP   r0   r%   r&   r+      s    

z_TorchDynamoContext.__init__c                 C   sP   t jrtd|   t| j| _|  | _| j	  t
| j| _| j	  d S )Nztorch._dynamo.optimize(...) is used with a context manager. Please refer to https://github.com/pytorch/torchdynamo#usage-example to use torch._dynamo.optimize(...) as an annotation/decorator. )r   Zraise_on_ctx_manager_usageRuntimeErrorrV   r   rR   rU   rW   backend_ctx	__enter__rM   rP   dynamic_ctxr.   r%   r%   r&   r]      s    

z_TorchDynamoContext.__enter__c                 C   sB   | j tusJ t| j  t| _ | j||| | j||| d S r)   )rU   rT   r   r^   __exit__r\   )r.   exc_typeexc_valexc_tbr%   r%   r&   r`      s
    
z_TorchDynamoContext.__exit__c                    s   t ttjjr0}t|}|j|_|S ts<J j	j
j t fdd}ttr~d|_n|_|_dvrtdsttddtj< |S )Nc                     s   t ts4tjj r4tjr&tdn| i |S   t	}  }|
  tj}|
  z4| i |W t	| |d d d  |d d d  S t	| |d d d  |d d d  0 d S )NzvDetected that you are using FX to symbolically trace a dynamo-optimized function. This is not supported at the moment.)r=   DisableContexttorchfx_symbolic_traceZis_fx_tracingr   Zerror_on_nested_fx_tracer[   r   r]   rM   rP   r`   )r6   r7   rU   r\   r^   rY   rR   rI   rV   r.   r%   r&   _fn   s6    

   z)_TorchDynamoContext.__call__.<locals>._fnTrS   r:   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)
                        )rJ   r=   re   nnModuler(   r5   rG   rH   rR   rV   rW   	functoolswrapsrd   _torchdynamo_disableZ_torchdynamo_inliner@   r[   textwrapdedentalways_optimize_code_objectsr:   )r.   rI   r/   new_modri   r%   rh   r&   __call__   s2    


!
z_TorchDynamoContext.__call__)r!   r"   r#   rF   null_contextr   r+   r]   r`   rs   r9   r%   r%   r0   r&   rN      s   rN   c                       s0   e Zd Zedd Zddd fddZ  ZS )OptimizeContextc                 C   s   | |kp| d u  S r)   r%   )oldnewr%   r%   r&   _different_backend  s    z"OptimizeContext._different_backendFrO   c                   s2    fdd}t | t j|||tj||d d S )Nc                      s2   t t r$tjrt n
td  at  d S )Nz`changing options to `torch.compile()` may require calling `torch._dynamo.reset()` to take effect)	ru   rx   r'   r   Zraise_on_backend_changer   warningswarnr   r%   compiler_fnr%   r&   rV     s    z*OptimizeContext.__init__.<locals>.on_enter)rR   rV   rY   rZ   rX   rP   )rJ   r*   r+   TorchPatcherr   )r.   rR   rY   rX   rP   rV   r0   r{   r&   r+     s    zOptimizeContext.__init__)F)r!   r"   r#   staticmethodrx   r+   r9   r%   r%   r0   r&   ru     s   
ru   c                       s   e Zd Z fddZ  ZS )RunOnlyContextc                    s   t  jdd d S )NFrQ   r*   r+   r_   r0   r%   r&   r+   *  s    zRunOnlyContext.__init__r!   r"   r#   r+   r9   r%   r%   r0   r&   r   )  s   r   c                       s   e Zd Z fddZ  ZS )rd   c                    s   t  jd d d S )NrQ   r   r_   r0   r%   r&   r+   /  s    zDisableContext.__init__r   r%   r%   r0   r&   rd   .  s   rd   hooksc                    s"   t   fdd} |_|S )Nc                    s   | j dkst| jjstjr@td| jj	 d| jj  d S | jjdkr\| jj	dkr\d S tj
rt }|rtH ddlm} ||j jd}tj|jd}|| |W  d    S 1 s0    Y  t  | |W  d    S 1  s0    Y  d S )	Nr   z	skipping  z<string>__new__)DDPOptimizer)bucket_bytes_capZbackend_compile_fnr   )f_lastir   checkf_codeco_filenamer   disablerB   debugco_nameZoptimize_ddpr   Z_get_active_ddp_modulecompile_lockZ"torch._dynamo.backends.distributedr   r   rG   r   
compile_fn)frameZ
cache_sizeZ
ddp_moduler   Zddp_optimizerZhijacked_callbackrR   r   r%   r&   catch_errors4  s4    *z*catch_errors_wrapper.<locals>.catch_errors)rl   rm   rG   )rR   r   r   r%   r   r&   catch_errors_wrapper3  s    r   Fc                 C   s   t t| ||d|dS )NT)rY   rX   rP   )ru   r   )r   r   rY   rP   r%   r%   r&   _optimize_catch_errorsW  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debug_utilsr   r@   r   r=   strr   )r|   r   Zcompiler_strr%   r%   r&   get_compiler_fnb  s    

r   c                   @   s   e Zd Zdd ZdS )_NullDecoratorc                 C   s   t |sJ |S r)   )rH   )r.   rI   r%   r%   r&   rs   p  s    z_NullDecorator.__call__N)r!   r"   r#   rs   r%   r%   r%   r&   r   o  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)      z0Python 3.11+ not yet supported for torch.compile)sysplatformr[   version_infor%   r%   r%   r&   check_if_dynamo_supportedu  s    

r   inductor)nopythonguard_export_fnguard_fail_fnr   rP   c                C   sz   t   t||d}tjd |s4tjdddkr:t S t	| } t
| dt}|r`t| ||dS ttj| |d|||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, turn on dynamic shapes support

    Example Usage::

        @torch._dynamo.optimize()
        def toy_example(a, b):
            ...
    r   r   ztorch._dynamo.optimizeZTORCHDYNAMO_DISABLErA   1rY   )rP   r   r   rO   )r   r   re   _C_log_api_usage_onceosenvirongetr   r   r2   rt   optimize_assertr   r   )backendr   r   r   r   rP   r   rY   r%   r%   r&   optimize|  s&    !r   z&torch._dynamo.symbolic_convert.explainc                    sv  ddl m} |  g g g dg  tjjd fdd}fdd}tt d	d 0 t|d
|d| }||i | W d    n1 s0    Y  t}i } D ]}	|	j	d }
|	|t
|
< qd}t| D ]B\}}dt|j	}|j d| }||d  d| d7 }qd| d}|d|d  d d7 }|}|d| 7 }|t 7 }|  | |fS )Nr   r;   r   gmc                    sd    |  g }| jjD ]}|jdkr| |j qt|7  | | jd ur^  | j | jS )Ncall_function)appendgraphnodesoptargetlenZcompile_subgraph_reasonr5   )r   example_inputsopsnode)break_reasonsgraphsop_countops_per_graphr%   r&   "dynamo_graph_accumulating_compiler  s    



z3explain.<locals>.dynamo_graph_accumulating_compilerc                    s     |  d S r)   )r   Zguards
out_guardsr%   r&   guard_export_print  s    z#explain.<locals>.guard_export_print.most_recent_backendF)r   r   rA   
z. z 
zDynamo produced z graphs zwith z graph break and z opsz
 Break reasons: 

)rA   r<   re   rf   GraphModuler   r!   r   r   Z
user_stackrepr	enumeratevaluesjoin	tracebackformat_listreasonr   )rD   r6   r7   r<   r   r   opt_fZgraph_countZdeduped_reasonsr   Zinnermost_frameZformatted_listidxZbreak_reasonZformatted_stackmsgZexplanationZexplanation_verboser%   )r   r   r   r   r   r&   explain  sR    ,

r   real)
aten_graphdecomposition_tabletracing_modec                   s  t   tjd |d us"|dkr.|s.J dt| } d d d d dd }fdd}tjjdfd	d
}t||f\}	t	|  t
t dd > t|t|d dd|dkd| }
|
|i |}W d    n1 s0    Y  t	|  d usJ dd usJ d|t|\}}d us>J t }|||G  fdddtjjj |rfdd}t|||dd   }dd tt|D t|  }tt||	||j_|  |fS )Nztorch._dynamo.exportr   zaSpecifying a decomposition_table table or tracing mode is illegal without setting aten_graph=Truec                 S   s   g }t  }tdt| D ]}t| | }|||< qtdt|D ]}|| }t|tjrt|dkrt||v r||t|  qt|	 |v r||t|	   qt
dq@t||v sJ d||t|  q@|S )Nr   r   z>Dynamo input/output is not consistent with traced input/outputzADynamo input and output is a strict subset of traced input/output)dictranger   idr=   re   ZTensorZnumelr   itemAssertionError)Zsource_argsZcandidate_argsZmatched_elements_positionsZdict_of_source_argsiZ
element_idargr%   r%   r&   produce_matching  s.    

z export.<locals>.produce_matchingc                    s    d u sJ d|  d S )N3whole graph export entails exactly one guard exportr%   r   r   r%   r&   r   6  s    z"export.<locals>.guard_export_printr   c                    s(    d u sJ d|   fdd}|S )Nz,whole graph export entails exactly one graphc                     s   |  d usJ  |  S r)   r%   )Zgraph_inputsr   graph_captured_inputgraph_captured_resultr%   r&   result_capturing_wrapperC  s    zYexport.<locals>.dynamo_normalization_capturing_compiler.<locals>.result_capturing_wrapperr%   )r   r   r   r   r%   r&   'dynamo_normalization_capturing_compiler;  s    	z7export.<locals>.dynamo_normalization_capturing_compilerr   r   TZsymbolicr   exportrP   z+whole graph export entails exactly one callr   c                       sD   e Zd Z fddZdd Z fddZ fddZ  ZS )	z*export.<locals>.ChangeInputOutputSignaturec                    sJ   t  | t} fddtd|D  _ fddD  _d S )Nc                    s&   g | ]}t  d | di qS )r   r%   )r*   placeholder.0r   )ChangeInputOutputSignaturer1   r.   r%   r&   
<listcomp>n  s   zGexport.<locals>.ChangeInputOutputSignature.__init__.<locals>.<listcomp>r   c                 3   s   | ]} j | V  qd S r)   )new_argsr   r_   r%   r&   	<genexpr>r  s   zFexport.<locals>.ChangeInputOutputSignature.__init__.<locals>.<genexpr>)r*   r+   r   r   r   old_args_gen)r.   mZarg_len)r   r1   	flat_args matched_input_elements_positionsr_   r&   r+   h  s    
z3export.<locals>.ChangeInputOutputSignature.__init__c                 S   sN   t | j}d| jjv r*| jjd |jjd< d| jjv rJ| jjd |jjd< |S )NvalZtensor_dict)nextr   current_nodemetar   )r.   r   r6   r7   r   r%   r%   r&   r   v  s    
z6export.<locals>.ChangeInputOutputSignature.placeholderc                    s:   |d }g || j   fddD }t ||fi S )Nr   c                    s   g | ]} | qS r%   r%   r   lookupr%   r&   r         zEexport.<locals>.ChangeInputOutputSignature.output.<locals>.<listcomp>)r   r*   output)r.   r   r6   r7   Zdynamo_result_flatZnew_result_flat)r1   !matched_output_elements_positionsr   r&   r   ~  s    z1export.<locals>.ChangeInputOutputSignature.outputc                    s   || _ t |S r)   )r   r*   run_node)r.   nr0   r%   r&   r     s    z3export.<locals>.ChangeInputOutputSignature.run_node)r!   r"   r#   r+   r   r   r   r9   r%   )r   r   r   r   r0   r&   r   g  s   r   c                     sB   t jj " t j j|  W  d    S 1 s40    Y  d S r)   )re   rf   r   Zpreserve_node_metaZInterpreterrun)r6   )r   r%   r&   graph_with_interpreter  s    z&export.<locals>.graph_with_interpreter)r   r   Z_allow_non_fake_inputsc                 S   s   g | ]}d | qS )Z	orig_arg_r%   r   r%   r%   r&   r     r   zexport.<locals>.<listcomp>)r   re   r   r   rJ   rf   r   pytreeZtree_flattenrE   r   r!   r   r   listinterpreterZTransformerr   Z	transformr   r   keysr	   r
   r   Z_codegenZ	recompile)rD   r   r   r   r6   r7   r   r   r   Zin_specr   Zresult_tracedZflat_results_tracedZout_spec_tracedZ	flat_bothr   Z	new_graphZ
input_strsr%   )r   r   r   r   r   r   r   r   r&   r   	  sx    
,

 !"r   c                 C   s
   d| _ | S )NT)Z_dynamo_marked_constantrI   r%   r%   r&   assume_constant_result  s    r  r   c                C   s.   t | } t| dt}ttj| |d|||dS )zF
    The same as `torch._dynamo.optimize(backend, nopython=True)`
    rY   )r   rO   )r   r2   rt   r   r   Zconvert_frame_assert)r   r   r   rP   rY   r%   r%   r&   r     s    r   c                 C   s,   | dur&t | } t| sJ t | S t S )z;Don't do any dynamic compiles, just use prior optimizationsN)rJ   rH   r   r  r%   r%   r&   r     s
    
r   c                 C   s,   | dur&t | } t| sJ t | S t S )z4Decorator and context manager to disable TorchDynamoN)rJ   rH   rd   r  r%   r%   r&   r     s
    
r   c                 C   s4   | du rt S t| } t| s J t| j d| _| S )zi
    Skip frames associated with the function code, but still process recursively
    invoked frames
    NT)skiprJ   rH   r   r:   rn   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  t tjjtj_t tjjtj_t tjjtj_t tjjjjtjjj_t tj	j
tj	_
tjjd t tjt_dd tjj D } ttdrttjt_ddlm}m}m}m}m}m}m} |||||||fD ]:}d|jdd	  }	t||	rt ||	t t!||	 qtjj"tjj#tjj$h}
| D ]}||
v rDt |j%|_%t |j&|_&t |j'|_'t|d
rtt |j(|_(t!|j%dd}|rt!|j%dd }|r||_%d|j%_)q*d S )NFc                 S   s(   g | ] }t |rt|tjjr|qS r%   )inspectisclass
issubclassre   optimZ	Optimizer)r   optr%   r%   r&   r     s   z&TorchPatcher.patch.<locals>.<listcomp>_inside_ddp_forward   )adagradadamadamaxadamwasgdnadamsgdZ_multi_tensor_.r   _init_grouphooked__wrapped__T)*r   re   ZjittraceZtrace_moduleZ_get_trace_graphrf   rg   ZTracerZonnxZexport_to_pretty_stringdistributionsDistributionZset_default_validate_argsr   Zdispatch_tracer
  __dict__r   r@   r   r  r  r  r  r  r  r  r  r  r!   splitsetattrr2   Z
SparseAdamZRAdamZLBFGSstepZ _cuda_graph_capture_health_checkZ	zero_gradr  r  )Z
optimizersr  r  r  r  r  r  r  Zopt_modZmulti_tensor_fn_nameZexcluded_optsr  r  Zunwrapped_stepr%   r%   r&   r     sR    


$

zTorchPatcher.patchc                    s    fdd}|S )Nc                     s   t jdtdd  | i |S )Nignoreztorch.distributed)categorymodule)ry   filterwarningsUserWarning)r6   r7   r  r%   r&   inner_fn#  s    zBTorchPatcher.suppress_torch_distributed_warnings.<locals>.inner_fnr%   )rI   r%  r%   r  r&   #suppress_torch_distributed_warnings!  s    z0TorchPatcher.suppress_torch_distributed_warnings)r!   r"   r#   r~   rl   	lru_cacher   r&  r%   r%   r%   r&   r}     s
   @r}   )T)r   )N)N)N)i
contextlibrl   r  loggingr   r   ro   	threadingr   r>   ry   enumr   typingr   r   r   r   Zunittest.mockr   re   Ztorch.utils._pytreer   Z_pytreer   Z"torch.fx.experimental.proxy_tensorr   Ztorch.fx.graphr	   r
   Ztorch.nn.parallel.distributedr   Zbackends.registryr   r   r   r   Ztorch._C._dynamo.eval_framer   r   r   r   r   r   dirr   Z_dynamoZ
eval_framer3   
startswithr2   globalsrA   r   r   r   excr   Zmutation_guardr   r   r   	getLoggerr!   rB   Ztorch.fx.experimentalr   ZExactWeakKeyDictionaryrq   nullcontextrt   r    r$   rT   RLockr   r'   __annotations__rj   rk   r(   rE   rF   rJ   contextmanagerboolrM   rN   ru   r   rd   r   r   r   r   r   r   r   r   r  r   r   r   r  r}   r%   r%   r%   r&   <module>   s   
"	
 % @
M  
	
	
