U
    Jc 2                     @   s,  d dl mZmZmZm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mZmZ d dlmZ d dlmZ d dlmZ dgZzd dlZd	ZW n ek
r   d
ZY nX ddddddZdddddddddddddddd gZd!dd"d#d$Zer
ed
d%G d&d dZnes(ed
d%G d'd dZdS )(    )absolute_importdivisionprint_functionunicode_literalsN)DictAnyTYPE_CHECKING)_get_qualified_name_format_arg)TensorMetadata)compatibility)chainFxGraphDrawerTFz"AliceBlue"ZLemonChiffon1ZYellow2Z	LightGreyZ
PowderBlue)placeholdercall_module	get_paramget_attroutputZ
CadetBlue1ZCoralZDarkOliveGreen1ZDarkSeaGreen1Z
GhostWhiteZKhaki1ZLavenderBlush1ZLightSkyBlueZ
MistyRose1Z
MistyRose2ZPaleTurquoise2Z
PeachPuff1ZSalmonZThistle1ZThistle3ZWheat1record"filled,rounded"#000000shape	fillcolorstyleZ	fontcolor)Zis_backward_compatiblec                   @   s*  e Zd ZdZd(ejjeeeedddZ	d)e
jdd	d
Ze
jdddZe
jdddZeee
jf dddZejjeeef dddZejjejjejjdddZeedddZejjejjeedddZedddZeedd d!Zejed"d#d$Zejjeeeee
jd%d&d'ZdS )*r   z
        Visualize a torch.fx.Graph with graphviz
        Basic usage:
            g = FxGraphDrawer(symbolic_traced, "resnet18")
            with open("a.svg", "w") as f:
                f.write(g.get_dot_graph().create_svg())
        FT)graph_modulenameignore_getattrignore_parameters_and_buffersskip_node_names_in_argsc                 C   s   || _ || |||||i| _|jjD ]\}|jdkr6q&| ||}t|tj	j
sRq&| || d|j |||| j| d|j < q&d S )Nr   _)_name_to_dot_dot_graphsgraphnodesop_get_leaf_node
isinstancetorchfxGraphModuletarget)selfr   r   r   r   r   nodeZ	leaf_node r/   @/tmp/pip-unpacked-wheel-gikjz4vx/torch/fx/passes/graph_drawer.py__init__@   s.         
FxGraphDrawer.__init__N)returnc                 C   s   |d kr|   S | |S d S N)get_main_dot_graphget_submod_dot_graphr-   Zsubmod_namer/   r/   r0   get_dot_graph`   s    zFxGraphDrawer.get_dot_graphc                 C   s   | j | j S r4   r#   r!   r-   r/   r/   r0   r5   f   s    z FxGraphDrawer.get_main_dot_graphc                 C   s   | j | j d|  S )Nr    r9   r7   r/   r/   r0   r6   i   s    z"FxGraphDrawer.get_submod_dot_graphc                 C   s   | j S r4   )r#   r:   r/   r/   r0   get_all_dot_graphsl   s    z FxGraphDrawer.get_all_dot_graphs)r.   r3   c                 C   sl   ddddd}|j tkr(t|j  |d< n@||j}tt|  d d d}t	|t
t	  |d< |S )	Nr   z#CAFFE3r   r   r   r         )r&   
_COLOR_MAPZ_pretty_print_targetr,   inthashlibmd5encode	hexdigest_HASH_COLOR_MAPlen)r-   r.   templateZtarget_nameZtarget_hashr/   r/   r0   _get_node_styleo   s    
 zFxGraphDrawer._get_node_style)moduler.   r3   c                 C   sZ   |}t |jtst|jd}|D ]0}t||sJtt|d | d t||}q$|S )N.z does not have attribute !)r(   r,   strAssertionErrorsplithasattrRuntimeErrorgetattr)r-   rH   r.   Zpy_objZatomsZatomr/   r/   r0   r'      s    
zFxGraphDrawer._get_leaf_node)r,   r3   c                 C   sF   t |tjjrt|}nt |tr*|}nt|}|ddddS )N{\{}\})r(   r)   nnModuletypenamerK   r	   replace)r-   r,   retr/   r/   r0   	_typename   s    
zFxGraphDrawer._typename)rH   r.   r   r3   c                    s  fdd}dd|j  d|j d }|jdkr| || |d|   d	 7 }d
}t drzd fdd jD }||d 7 }nj|d| |j d 7 }t|j	dkr|||j	7 }t|j
dkr|||j
7 }|dt|j d 7 }|jd}|| |7 }|d S )Nc                    s   t | tr"d\}}dd | D }n*t | trHd\}}dd |  D }ndS  r^dd |D }t|dkrndS |d	| | }|d
dddS )N)z	|args=(\lz,\n)\lc                 S   s   g | ]}t |d dqS )r<   Zmax_list_lenr
   .0ar/   r/   r0   
<listcomp>   s     zSFxGraphDrawer._get_node_label.<locals>._get_str_for_args_kwargs.<locals>.<listcomp>)z|kwargs={\lz,\n}\lc                 S   s&   g | ]\}}| d t |dd qS ): r<   r[   r\   )r^   kvr/   r/   r0   r`      s    c                 S   s   g | ]}d |kr|qS )%r/   r]   r/   r/   r0   r`      s      r   z,\nrQ   rR   rS   rT   )r(   tupledictitemsrE   joinrX   )argprefixsuffixZarg_strs_listZarg_strs)r   r/   r0   _get_str_for_args_kwargs   s    

z?FxGraphDrawer._get_node_label.<locals>._get_str_for_args_kwargsrQ   zname=%z	|op_code=
r   \nz\n|rd   __constants__c                    s    g | ]}| d t  | qS )ra   )rP   )r^   c)leaf_moduler/   r0   r`      s     z1FxGraphDrawer._get_node_label.<locals>.<listcomp>z|target=r   z|num_users=tensor_metarS   )r   r&   r'   rZ   rN   ri   rp   r,   rE   argskwargsusersmetaget_tensor_meta_to_label)r-   rH   r.   r   rm   labelextrars   r/   )rr   r   r0   _get_node_label   s(    

zFxGraphDrawer._get_node_labelc                 C   s   |d krdS t |tr | |S t |trJd}|D ]}|| |7 }q2|S t |tr|d}| D ]\}}|| |7 }q`|S t |trd}|D ]}|| |7 }q|S tdt	| d S )Nrd   zUnsupported tensor meta type )
r(   r   _stringify_tensor_metalistry   rg   rh   rf   rO   type)r-   tmresultitemrb   rc   r/   r/   r0   ry      s(    




z#FxGraphDrawer._tensor_meta_to_label)r   r3   c                 C   sz  d}t |dstd| |dt|j d 7 }|dtt|j d 7 }|dt|j d 7 }|dt|j d 7 }|jrv|j	d k	st
d	|j	kst
|j	d	 }|tjtjhkr|d
t|j	d  d 7 }|dt|j	d  d 7 }nt|tjtjtjhkrN|dt|j	d  d 7 }|dt|j	d  d 7 }|dt|j	d  d 7 }ntd| |dt|j	d	  d 7 }|S )Nrd   dtyper   z|dtype=ro   z|shape=z|requires_grad=z|stride=qschemez	|q_scale=Zscalez|q_zero_point=Z
zero_pointz|q_per_channel_scale=z|q_per_channel_zero_point=z|q_per_channel_axis=ZaxiszUnsupported qscheme: z	|qscheme=)rN   printrK   r   rf   r   Zrequires_gradZstrideZis_quantizedZqparamsrL   r)   Zper_tensor_affineZper_tensor_symmetricZper_channel_affineZper_channel_symmetricZ per_channel_affine_float_qparamsrO   )r-   r   r   r   r/   r/   r0   r}      s8    


z$FxGraphDrawer._stringify_tensor_meta)tr3   c                 C   s   t |jt t|j d S )Nro   )rK   r   r~   r   )r-   r   r/   r/   r0   _get_tensor_label  s    zFxGraphDrawer._get_tensor_label)r   r   r   r   r   r3   c           
   	      s   t j|dd |jjD ]|r*jdkr*q}t jjfd||i|} 	|  fdd}jdkr
||sttjjs|  q|jjD ]6|rjdkrqjD ]}	 t j|	j qq S )a  
            Actual interface to visualize a fx.Graph. Note that it takes in the GraphModule instead of the Graph.
            If ignore_parameters_and_buffers is True, the parameters and buffers
            created with the module will not be added as nodes and edges.
            ZTB)Zrankdirr   rz   c                     s   t   D ]z\} }jd |  }t|tjjrB|d d nd}tj	|fdd| 
| d it} |  t|j qd S )NrI   z|op_code=get_Z	parameterzbuffer\lrz   rQ   rS   )r   Znamed_parametersZnamed_buffersr   r(   r)   rU   	ParameterpydotNoder   _WEIGHT_TEMPLATEadd_nodeadd_edgeEdge)ZpnameZptensorZpname1label1Z
dot_w_nodeZ	dot_graphrr   r.   r-   r/   r0   get_module_params_or_buffers   s$     
z;FxGraphDrawer._to_dot.<locals>.get_module_params_or_buffersr   )r   Dotr$   r%   r&   rG   r   r   r|   r   r'   r(   r)   r*   r+   rv   r   r   )
r-   r   r   r   r   r   r   Zdot_noder   userr/   r   r0   r"     s0    



zFxGraphDrawer._to_dot)FFT)N)__name__
__module____qualname____doc__r)   r*   r+   rK   boolr1   r   r   r8   r5   r6   r   r;   r   rG   rU   rV   r'   r   rZ   r|   ry   r   r}   ZTensorr   r"   r/   r/   r/   r0   r   6   sH        4c                   @   s$   e Zd ZdejjeedddZdS )r   F)r   r   r   c                 C   s   t dd S )Nz|FXGraphDrawer requires the pydot package to be installed. Please install pydot through your favorite Python package manager.)rO   )r-   r   r   r   r/   r/   r0   r1   E  s    r2   N)F)	r   r   r   r)   r*   r+   rK   r   r1   r/   r/   r/   r0   r   C  s   )
__future__r   r   r   r   r@   r)   Ztorch.fxtypingr   r   r   Ztorch.fx.noder	   r
   Ztorch.fx.passes.shape_propr   Ztorch.fx._compatibilityr   	itertoolsr   __all__r   Z	HAS_PYDOTImportErrorr>   rD   r   r   r/   r/   r/   r0   <module>   sd   
	  