a
    d8                     @   s.  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mZm	Z	 d dl
Z
d dlmZ ddlmZm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 dd
lmZmZmZ ddlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% ddl&m'Z'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z> e?e@ZAG dd dZBeB ZCeB ZDdaEe Fedd ZGdd ZHejIdd ZJd+ddZKd,eeLeLddd ZMe4d!d"d-ejNeeOePf eeOePf eeOePf eeLeLe*eejQ ee( d#
d$d%ZRee*d&d'd(ZSd)d* ZTdS ).    N)DictOptionalSet)_forward_from_src   )configexc)
is_allowed)
CompilerFn)remove_dead_coderemove_pointless_jumps)is_generatortransform_code_object)always_optimize_code_objects	skip_codeTorchPatcher)augment_exc_messageBackendCompilerFailedformat_error_msgInternalTorchDynamoErrorTorchRuntimeErrorunimplementedUnsupported)CheckFunctionManagerGuardedCode)Hooks)OutputGraph)ExecutionRecord)InstructionTranslator)CleanupManagercountersdynamo_timedformat_bytecodegen_record_file_nameguard_failuresincrement_frameinit_loggingis_namedtupleistypeorig_code_maptroubleshooting_urlwrite_record_to_filec                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
Trackerc                 C   s   g | _ t | _d S N)seensetseen_idsself r3   d/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/_dynamo/convert_frame.py__init__4   s    zTracker.__init__c                    sD   t |  jvr@t| fdd}j| j  d S )Nc                    s   j  S r-   )r0   remove)_idxr2   r3   r4   <lambda>;       zTracker.add.<locals>.<lambda>)idr0   weakrefrefr.   appendadd)r2   Z
strong_objobjr3   r8   r4   r@   8   s
    
zTracker.addc                 C   s   t || jv S r-   )r<   r0   )r2   itemr3   r3   r4   __contains__?   s    zTracker.__contains__c                 C   s   | j   | j  d S r-   )r.   clearr0   r1   r3   r3   r4   rD   B   s    
zTracker.clearN)__name__
__module____qualname__r5   r@   rC   rD   r3   r3   r3   r4   r,   3   s   r,   c                  O   s   t | i |}t|j |S r-   )original_forward_from_srcr   __code__)argskwargsresultr3   r3   r4   fx_forward_from_src_skip_resultN   s    
rM   c                    s    t   fdd} |_|S )z
    Context manager to:
        1) Save/restore torch random state
        2) Save/restore torch.is_grad_enabled() state
        3) Monkey patch torch.fx.graph_module._forward_from_src
    c               	      s   t  }t j }t j r&t j }t jjj}t	t jj_zT | i |W t j
| t j| t j rxt j| |t jj_S |t jj_n:t j
| t j| t j rt j| |t jj_0 d S r-   )torchis_grad_enabledrandomZget_rng_stateZcudaZis_availableZfxZgraph_moduler   rM   Z_CZ_set_grad_enabledZset_rng_state)rJ   rK   Zprior_grad_modeZ	rng_stateZcuda_rng_stateZprior_fwd_from_srcfnr3   r4   _fn_   s0    






    
z!wrap_convert_context.<locals>._fn)	functoolswraps_torchdynamo_orig_callable)rR   rS   r3   rQ   r4   wrap_convert_contextW   s    rW   c                    s   | j tv rdS | j jD ]"}|| jv rt| j| r dS qt  fdd | j D ]} |rX dS qXt	d| j j
 d| j j d| j j  dS )z+Check if the frame has torch.* related bitsTc                    s  t  }|v r| S d|< t tjtjjfrDd|< | S t ttfrtt	fdd D |< | S t t
rt  }t	fdd|D |< | S t ttttdtfrd|< | S t rt	 fdd jD |< | S dS dS )z)Recursively check if the obj has a tensorFTc                    s   g | ]} |qS r3   r3   .0v
has_tensorr3   r4   
<listcomp>   r;   z;has_tensor_in_frame.<locals>.has_tensor.<locals>.<listcomp>c                    s   g | ]} |qS r3   r3   rX   r[   r3   r4   r]      r;   Nc                    s   g | ]} t |qS r3   )getattrrX   )r\   rA   r3   r4   r]      r;   )r<   
isinstancerN   ZTensornnModuler(   listtupleanydictvaluesstrintfloattypeboolr'   _fields)rA   Zobj_idrf   r\   r0   )rA   r4   r\      s*    

z'has_tensor_in_frame.<locals>.has_tensorzskipping because no torch.* z              F)f_coder   co_names	f_globalsr	   re   f_localsrf   logdebugco_nameco_filenameco_firstlineno)frameru   valuer3   rm   r4   has_tensor_in_framet   s&    

"rz   c                 C   sP   d }t | dr*t| |}t|| j || _t|  tjrLt	t
| ||| d S )Nexec_record)hasattrr#   r+   r{   record_filenamer   r   suppress_errorsrs   errorr   )ecoderx   r}   r3   r3   r4   exception_handler   s    

r   TF)compiler_fn	one_graphexportc                    s0   t   tjttd fdd} |_t|S )z&Fully convert a frame into an FX graphrx   
cache_sizehooksc              
      s`  t   | j}t| |tv r"d S tjdrDtjd|jkrDd S |jdkr^|j	
dr^d S |jdkrld S |jdkr|j	dkrd S |jdkr|j	dkrt| jsd S t|rtd |tjkr*d	d
 }dd }|tv sJ dtdtj dd|| d d|| d dt d  td t| s8d S t at| j| j| j| j || 	S )NZTORCHDYNAMO_DEBUG_FUNCTIONz	<genexpr>)ztransformers/file_utils.pyztransformers/utils/generic.py__setattr__z<module>z<string>z<lambda>	generatorc                 S   s   d| j  d| j d| j dS )N'z' (:))ru   rv   rw   r   r3   r3   r4   format_func_info   s    zMconvert_frame_assert.<locals>._convert_frame_assert.<locals>.format_func_infoc                 S   s   t t|  d  S )N)rg   r$   r   r3   r3   r4   format_guard_failures   s    zRconvert_frame_assert.<locals>._convert_frame_assert.<locals>.format_guard_failuresz&TODO(whc) any other recompile reasons?z+torch._dynamo hit config.cache_size_limit (z)
z   function: 
z   reasons:  z&to diagnose recompilation issues, see .zcache_size_limit reached)r%   ro   input_codesr@   output_codesosenvirongetru   rv   endswithrk   
f_builtinsr   r   r   cache_size_limitr$   rs   warningr*   rz   rN   rO   initial_grad_state_compilerq   rr   )rx   r   r   r   r   r   r   r   r   r3   r4   _convert_frame_assert   sn    




z3convert_frame_assert.<locals>._convert_frame_assert)r&   types	FrameTyperh   r   rV   rW   )r   r   r   r   r3   r   r4   convert_frame_assert   s    Kr   Zentire_frame_compile)Z
phase_name)
r   globalslocalsbuiltinsr   r   r   r   rx   returnc	                    sH  d t   f	dd}	zt D ]}
zt|	}t|< W  qW q2 tjy   td |
dkr|t	d Y q2 tj
y } zHtd| dj dj dj  rtd	 W Y d }~ W d S d }~0 0 q2t| tjr.ttd
jjj ttdjjj| d us<J jd usLJ jtj|< t|rl|jnd }t||j}tjrd}|ddd tjD 7 }t| |jd ur|j |W S  tt t!t"fy
 } zt#||  W Y d }~nBd }~0  t$yB } z t#|| t% |W Y d }~n
d }~0 0 d S )Nc                    sv   t |  |
}|  |jd us4J js>J j| d d < |j tjrrtt	| | d d < d S r-   )
r   runoutputZoutput_instructionsupdatecode_optionsr   Zdead_code_eliminationr   r   )Zinstructionsr   Ztracer	r   r   r   r   r   r   Zmutated_closure_cell_contentsr   r   r3   r4   	transform)  s(    
z_compile.<locals>.transformzRestarting analysis ...d   z100+ RestartAnalysis() callszSkipping frame rn   z                     z%No graph captured with one_graph=TruezORIGINAL BYTECODEzMODIFIED BYTECODEzGUARDS:
r   c                 S   s   g | ]}d t | qS )z - )rg   )rY   Zguardr3   r3   r4   r]   x  r;   z_compile.<locals>.<listcomp>)&r/   	itertoolscountr   r)   r   ZRestartAnalysisrs   rt   r   Z	SkipFrameru   rv   rw   r   r@   r   Zoutput_codeinfor"   guardsZcleanupsr   instancer   Zguard_fail_fnr   check_fnjoinsortedZguard_export_fnr   r   r   AssertionErrorr   	Exceptionr   )r   r   r   r   r   r   r   r   rx   r   attemptZout_coder   r   Zguarded_codeZ	guard_strr3   r   r4   r     s    




	

r   )r   r   c                    s.   t | dd tjttd fdd}| |_|S )zHTry to convert a frame into an FX graph, if error leave frame unmodifiedF)r   r   c              	      s   t d d  d7  < z& | ||}t d d  d7  < |W S  ttfyZ   td Y n$ ty|   tjsn td Y n0 d S )Nframestotalr   okz;converting frame raised unsupported, leaving it unconvertedz0converting frame raised error, suppressing error)r    NotImplementedErrorr   rs   r   r   r   r~   )rx   r   r   rL   Zinner_convertr3   r4   _convert_frame  s    z%convert_frame.<locals>._convert_frame)r   r   r   rh   r   rV   )r   r   r   r3   r   r4   convert_frame  s    r   c                 C   s   ddl m} tj}dt_t  t| d}t|}W d    n1 sH0    Y  dd t	|j
 t
  D |_
zFz(t|j|j
|j|j|ddt d d	 W n ty   Y n0 W |t_n|t_0 d S )Nr   )eagerFrbc                 S   s   i | ]\}}||qS r3   r3   )rY   krZ   r3   r3   r4   
<dictcomp>  s   zreplay.<locals>.<dictcomp>)r   r   r   r   rx   )Zbackends.debuggingr   r   Zreplay_record_enabledr&   openr   loadr   chainr   itemsr   r   r   r   r   r   )filenamer   Zoriginal_replay_valin_filerecordr3   r3   r4   replay  s0    (
r   )N)TF)N)UrT   r   loggingr   r   r=   typingr   r   r   rN   Ztorch.fx.graph_moduler   rH    r   r   Zallowed_functionsr	   Zbackends.registryr
   Zbytecode_analysisr   r   Zbytecode_transformationr   r   Z
eval_framer   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zoutput_graphr   Zreplay_recordr   Zsymbolic_convertr   utilsr   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   	getLoggerrE   rs   r,   r   r   r   rU   rM   rW   Z#suppress_torch_distributed_warningsrz   r   rk   r   CodeTyperg   objectr   r   r   r   r3   r3   r3   r4   <module>   sn   $	<


<
  W
 


v