a
    d.0                     @   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Zd dlZd dl	Z	d dl
Z
d dlZd dlmZmZ d dlmZ d dlmZmZmZm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  d d	l!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+m,Z,m-Z-m.Z. ddl/m0Z0 e1e2Z3e4ddd Z5dddZ6ee* ej7dddZ8ej9dd Z:G dd dZ;ee Z<G dd dZ=dS )    N)AnyList)patch)config
draw_graphget_aot_graph_nameget_graph_being_compiled)fx)r   )save_graph_reprowrap_compiler_debug)get_debug_dirinit_logging)GraphModule)TensorMetadata)legalize_graph   )r   ir)BaseSchedulerNodeFusedSchedulerNodeNopKernelSchedulerNode
OutputNodeSchedulerNode)Vc                   C   s6   zt jddgt jd W dS  t jy0   Y dS 0 d S )Nwhichdot)stderrTF)
subprocesscheck_outputPIPESubprocessError r    r    ^/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torch/_inductor/debug.pyhas_dot-   s
    r"   Fc           	   	   C   s   t  std dS |du r"t }t| }|jD ]d}d|jvr@q0|jd j}t|t	r^|d }d}t|t
jrv|jj}t||ddddd}||jd< q0|rt| ti |}t| |j  t||dd dS )zR
    Draw a graph in fname.svg.
    nodes is a list of SchedulerNode objects.
    z*draw_buffers() requires `graphviz` packageNfusion_metar   Ztensor_metaF)Z
clear_meta)r"   logwarningr   create_fx_from_snodesnodesmetagroup
isinstancetupler   ZComputedBufferdatadtyper   printr   r   graphZlintr   )	r'   Zprint_graphfnamer/   noder)   r-   metadatagmr    r    r!   draw_buffers6   s.    





r4   )snodesreturnc              
      s  dd  t dg d} fdddD }i }tj }d}g }d}| D ]}| r`d	}	|	}nZ| rrd
}	|	}nHt|trd}	|	}n4t|t	rd}	|j
}nt|trd}	|j
}ntd||	 }
|j|
ddd}fdd|r|| | }||_|||g|	|jd< t|tr8|jD ]}||| < q$|||< |du rH|}qH| D ]}| }|jj}|| }g }|D ]j}|j|v r||j }nB||& ||j}|||j< W d   n1 s0    Y  || qvt||_qR|t|dkr
|d nt| |S )zB
    Creates a FX Graph from a list of SchedulerNode objects.
    c                 S   s   dd }| |_ |S )Nc                  W   s   dS )Nr   r    )argsr    r    r!   func1b   s    z;create_fx_from_snodes.<locals>.get_fake_func.<locals>.func1)__name__)namer8   r    r    r!   get_fake_funca   s    z,create_fx_from_snodes.<locals>.get_fake_func
FusionMeta)r)   r5   typec                    s   i | ]}| |qS r    r    ).0s)r;   r    r!   
<dictcomp>j       z)create_fx_from_snodes.<locals>.<dictcomp>)externnopcomputefusedNrB   templaterC   rD   rE   zUnknown node typer    r7   kwargsc                    s6   t | tr"t fdd| jD S tdd | jD S )Nc                    s   g | ]} |qS r    r    )r>   x	in_outputr    r!   
<listcomp>   rA   z<create_fx_from_snodes.<locals>.in_output.<locals>.<listcomp>c                 S   s   g | ]}t |jtqS r    )r*   r1   r   )r>   userr    r    r!   rL      rA   )r*   r   anyr5   Zusers)snoderJ   r    r!   rK      s    
z(create_fx_from_snodes.<locals>.in_outputr#   r   r   )collections
namedtupletorchr	   GraphZ	is_externZis_templater*   r   r   r)   r   RuntimeErrorcall_functionappendget_namer:   r(   r5   Zread_writesZreadsZinserting_beforeplaceholderr+   r7   outputlen)r5   r<   Z	func_dictZbuf_to_fx_noder/   Z
first_nodeoutputsr)   rO   Z	node_typeZ	node_funcZfx_noder:   rI   depsnew_argsdepZdep_noder    )r;   rK   r!   r&   \   sl    






*$r&   c               	   #   sx  t tjdd} tj}tj}dd l}t	|j
jj t }|tdtj |sX|rttj  | fdd | szd V  W |  n
|  0 d S |tdd |td	d |td
d tjt d}tj|st| ttj|dt  d}|tj | t!d  | zd V  W  "| |  n "| |  0 d S )NZTORCH_COMPILE_DEBUGFr   z"functorch.compile.config.log_levelc                      s
     S N)removeHandlerr    r$   Zstdout_handlerr    r!   <lambda>   rA   z$enable_aot_logging.<locals>.<lambda>z*functorch.compile.config.debug_partitionerTz%functorch.compile.config.debug_graphsz$functorch.compile.config.debug_jointaot_torchinductorZaot_z
_debug.log3[%(filename)s:%(lineno)d %(levelname)s] %(message)s)#boolosenvirongetfunctorch_configdebug_graphsZdebug_jointZtorch._functorch.aot_autogradlogging	getLoggerZ
_functorchZaot_autogradr9   
contextlib	ExitStackenter_contextr   DEBUGStreamHandlersysstdout
addHandlercallbackclosepathjoinr   existsmakedirsFileHandlerr   setLevelsetFormatter	Formatterr`   )Zcompile_debugrj   Zdebug_joint_graphsrR   stackrw   fhr    ra   r!   enable_aot_logging   sN    





r   c                   @   s   e Zd Ze Zedd Zedd Zdd Z	e
ddd	Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )DebugContextc                    s"   t   fdd}t|ddS )Nc                     s8   t    | i |W  d    S 1 s*0    Y  d S r_   )r   rG   fnr    r!   inner   s    z DebugContext.wrap.<locals>.innerinductor)Zcompiler_name)	functoolswrapsr   )r   r   r    r   r!   wrap   s    zDebugContext.wrapc                 C   sJ   t jD ]>}tjt d|  d| }tj|st| |  S qd S )Nrc   .)r   _counterrf   rw   rx   r   ry   rz   )Zfolder_namendirnamer    r    r!   create_debug_dir   s    

zDebugContext.create_debug_dirc                 C   s   d | _ d | _t | _d S r_   )_prof_pathrm   rn   _stack)selfr    r    r!   __init__   s    zDebugContext.__init__)new_pathc                 C   sb   | j s
d S |dsJ |tj|r2t| zt| j | || _ W n ty\   Y n0 d S )Nz.debug)	r   endswithrf   rw   ry   shutilrmtreerenameOSError)r   r   r    r    r!   r     s    

zDebugContext.renamec                 C   s    | j s
J ttj| j |dS )Nw)r   openrf   rw   rx   r   filenamer    r    r!   fopen  s    
zDebugContext.fopenc                 C   s   t j| j|S r_   )rf   rw   rx   r   )r   suffixr    r    r!   r     s    zDebugContext.filenamec                 C   s   t jjd urdd l}| jsJ tj| jtj| j d}|	|d*}|j
| jtj| jd W d    n1 sz0    Y  t j| d S )Nr   z.tar.gzzw:gz)arcname)r   trace
upload_tartarfiler   rf   rw   rx   basenamer   add)r   r   Ztar_filetarr    r    r!   r     s    
8zDebugContext.upload_tarc                 C   s   t d}|jst  tjr<dd }| j|tj	 t j
t_	| jt|  tjjsZd S | t | _tjjr~| dt j
 tjjr| dt j tjjrt | _| j  d S )Ntorch._inductorc                 S   s
   | t _d S r_   )dynamo_config	log_level)levelr    r    r!   reset_log_level+  s    z/DebugContext.__enter__.<locals>.reset_log_levelz	debug.logzinfo.log)rk   rl   handlersr   r   debugr   ru   r   r   rp   ro   r   Zset_debug_handlerr   enabledr   r   r   Z	debug_log_setup_log_captureZinfo_logINFOZcompile_profilecProfileZProfiler   enable)r   r$   r   r    r    r!   	__enter__$  s$    

zDebugContext.__enter__c                 C   sp   t d}| j| |}t |}|| |t d |	| |t
|j| | j|j| d S )Nr   rd   )rk   rl   r   ro   r   rq   r|   r}   r~   rt   minr   ru   r`   )r   r   r   r$   fdchr    r    r!   r   @  s    



zDebugContext._setup_log_capturec                 C   sF   | j r| j   |   | jr8|   tdt | j | j	  d S )Nz%s debug trace: %s)
r   disable_save_profile_datar   r   r$   r%   r   r   rv   )r   exc_typeexc_valexc_tbr    r    r!   __exit__L  s    
zDebugContext.__exit__c                 C   s   | j | d | dP}tj| j |d}|  |d |d |d |d W d    n1 sr0    Y  d S )Nzcompile.profzcompile.stats)streamZcumtimed   Ztottime)	r   Z
dump_statsr   r   pstatsZStatsZ
strip_dirsZ
sort_statsZprint_stats)r   r   statsr    r    r!   r   V  s    


zDebugContext._save_profile_datac                 C   sX   t jjrHtt j|rHztt| |W S  tyD   tjddd Y qT0 ndd }|S d S )Nz Ignoring exception in debug codeT)exc_infoc                  _   s   d S r_   r    rG   r    r    r!   ignoredh  s    z)DebugContext.__getattr__.<locals>.ignored)r   r   r   getattrDebugFormatter	Exceptionr$   r%   )r   r:   r   r    r    r!   __getattr__`  s    zDebugContext.__getattr__N)r9   
__module____qualname__	itertoolscountr   staticmethodr   r   r   strr   r   r   r   r   r   r   r   r   r    r    r    r!   r      s   



r   c                   @   s   e Zd Zdd Zejjeej dddZ	ejjeej dddZ
edd	d
ZedddZeedddZedddZdd ZdS )r   c                 C   s   |j | _ |j| _|| _d S r_   )r   r   handler)r   r   r    r    r!   r   r  s    zDebugFormatter.__init__)r3   inputsc                 C   sx   |  d}t|||d W d    n1 s.0    Y  |  d"}||jdd W d    n1 sj0    Y  d S )Nzfx_graph_runnable.pyr   zfx_graph_readable.pyFZprint_output)r   r
   writeprint_readabler   r3   r   r   r    r    r!   fx_graphw  s    ,zDebugFormatter.fx_graphc                 C   s@   |  d"}||jdd W d    n1 s20    Y  d S )Nzfx_graph_transformed.pyFr   )r   r   r   r   r    r    r!   fx_graph_transformed~  s    z#DebugFormatter.fx_graph_transformed)r'   c                 C   s   |  d| d S )Nzir_pre_fusion.txt	_write_irr   r'   r    r    r!   ir_pre_fusion  s    zDebugFormatter.ir_pre_fusionc                 C   s   |  d| d S )Nzir_post_fusion.txtr   r   r    r    r!   ir_post_fusion  s    zDebugFormatter.ir_post_fusion)r   r'   c                 C   sP   |  |2}|D ]}||  |d qW d    n1 sB0    Y  d S )Nz


)r   r   Z	debug_str)r   r   r'   r   r1   r    r    r!   r     s    zDebugFormatter._write_irc                 C   s   t || dd d S )Nzgraph_diagram.svg)r0   )r4   r   r   r    r    r!   graph_diagram  s    zDebugFormatter.graph_diagramc                 C   s   t || d d S )Nzoutput_code.py)r   copyr   r   r    r    r!   output_code  s    zDebugFormatter.output_codeN)r9   r   r   r   rR   r	   r   r   ZTensorr   r   SchedulerNodeListr   r   r   r   r   r   r    r    r    r!   r   q  s   r   )FN)>rP   rm   r   r   r   rk   os.pathrf   r   r   r   rr   typingr   r   Zunittest.mockr   Zfunctorch.compiler   ri   r   r   r   rR   r	   Ztorch._dynamor   Ztorch._dynamo.debug_utilsr
   r   Ztorch._dynamo.utilsr   r   Ztorch.fx.graph_moduler   Ztorch.fx.passes.shape_propr   Ztorch.fx.passes.tools_commonr    r   Z	schedulerr   r   r   r   r   Zvirtualizedr   rl   r9   r$   	lru_cacher"   r4   rS   r&   contextmanagerr   r   r   r   r    r    r    r!   <module>   sF   


&U
5 