
    VhI                     6   d dl Z d dlmZ d dlmZmZm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 d dlmZ 	 d dlZd	ZdgZddddddZg dZddddZer e
d
       G d d             Zyes e
d
       G d d             Zyy# e$ r d
ZdZY Sw xY w)    N)chain)AnyOptionalTYPE_CHECKING)compatibility)_parse_stack_trace)_format_arg_get_qualified_name)normalize_function)TensorMetadataTFFxGraphDrawerz"AliceBlue"LemonChiffon1Yellow2	LightGrey
PowderBlue)placeholdercall_module	get_paramget_attroutput)
CadetBlue1CoralDarkOliveGreen1DarkSeaGreen1
GhostWhiteKhaki1LavenderBlush1LightSkyBlue
MistyRose1
MistyRose2PaleTurquoise2
PeachPuff1SalmonThistle1Thistle3Wheat1r#   "filled,rounded"#000000)	fillcolorstyle	fontcolor)is_backward_compatiblec                      e Zd ZdZ	 	 	 	 	 	 d!dej
                  j                  dededededed	e	e   d
efdZ
d"dej                  fdZdej                  fdZdej                  fdZdeeej                  f   fdZdej
                  j$                  deeef   fdZdej(                  j*                  dej
                  j$                  dej(                  j*                  fdZdedefdZ	 d#dedefdZdej
                  j                  dej
                  j$                  dededef
dZdefdZdedefdZdej>                  defdZ dej
                  j                  dedededededej                  fd Z!y)$r   z
        Visualize a torch.fx.Graph with graphviz
        Basic usage:
            g = FxGraphDrawer(symbolic_traced, "resnet18")
            g.get_dot_graph().write_svg("a.svg")
        Ngraph_modulenameignore_getattrignore_parameters_and_buffersskip_node_names_in_argsparse_stack_tracedot_graph_shapenormalize_argsc	           	         || _         ||nd| _        || _        | j                  t        d<   || j	                  ||||||      i| _        |j                  j                  D ]  }	|	j                  dk7  r| j                  ||	      }
t        |
t        j                  j                        sJ| j	                  |
| d|	j                   ||||      | j
                  | d|	j                   <    y )Nrecordshaper   _)_namer4   r5   _WEIGHT_TEMPLATE_to_dot_dot_graphsgraphnodesop_get_leaf_node
isinstancetorchfxGraphModuletarget)selfr.   r/   r0   r1   r2   r3   r4   r5   node	leaf_nodes              L/home/dcms/DCMS/lib/python3.12/site-packages/torch/fx/passes/graph_drawer.py__init__zFxGraphDrawer.__init__G   s    DJ#2#>H   #1D(,(<(<W% dll "1+%	 D %**00 77m+ //dC	!)UXX-A-AB<@LLfAdkk]+"1+%=  D64;;-!89    returnc                 H    || j                         S | j                  |      S )aA  
            Visualize a torch.fx.Graph with graphviz
            Example:
                >>> # xdoctest: +REQUIRES(module:pydot)
                >>> # xdoctest: +REQUIRES(module:ubelt)
                >>> # define module
                >>> class MyModule(torch.nn.Module):
                >>>     def __init__(self) -> None:
                >>>         super().__init__()
                >>>         self.linear = torch.nn.Linear(4, 5)
                >>>     def forward(self, x):
                >>>         return self.linear(x).clamp(min=0.0, max=1.0)
                >>> module = MyModule()
                >>> # trace the module
                >>> symbolic_traced = torch.fx.symbolic_trace(module)
                >>> # setup output file
                >>> import ubelt as ub
                >>> dpath = ub.Path.appdir("torch/tests/FxGraphDrawer").ensuredir()
                >>> fpath = dpath / "linear.svg"
                >>> # draw the graph
                >>> g = FxGraphDrawer(symbolic_traced, "linear")
                >>> g.get_dot_graph().write_svg(fpath)
            )get_main_dot_graphget_submod_dot_graphrG   submod_names     rJ   get_dot_graphzFxGraphDrawer.get_dot_graphv   s*    0 "..0000==rL   c                 4    | j                   | j                     S Nr=   r:   rG   s    rJ   rO   z FxGraphDrawer.get_main_dot_graph   s    ##DJJ//rL   c                 >    | j                   | j                   d|    S )Nr9   rV   rQ   s     rJ   rP   z"FxGraphDrawer.get_submod_dot_graph   s"    ##tzzl!K=$ABBrL   c                     | j                   S rU   )r=   rW   s    rJ   get_all_dot_graphsz FxGraphDrawer.get_all_dot_graphs   s    ###rL   rH   c                 j   | j                   dddd}|j                  t        v rt        |j                     |d<   |S |j                  |j                        }t        t        j                  |j                         d      j                         d d d	      }t        |t        t              z     |d<   |S )
Nz#CAFFE3r'   r(   )r8   r)   r*   r+   r)   F)usedforsecurity      )r4   r@   
_COLOR_MAP_pretty_print_targetrF   inthashlibmd5encode	hexdigest_HASH_COLOR_MAPlen)rG   rH   templatetarget_nametarget_hashs        rJ   _get_node_stylezFxGraphDrawer._get_node_style   s    --&+&	H ww*$(2477(;% O #77D!KK#**,eik"1& 	 )8#o"66)% OrL   modulec                     |}t        |j                  t              sJ |j                  j                  d      }|D ]7  }t	        ||      st        t        |      dz   |z   dz         t        ||      }9 |S )N.z does not have attribute !)rB   rF   strsplithasattrRuntimeErrorgetattr)rG   rl   rH   py_objatomsatoms         rJ   rA   zFxGraphDrawer._get_leaf_node   s     Fdkk3///KK%%c*E /vt,&F&AADH3N  !./ MrL   rF   c                     t        |t        j                  j                        rt        j                  |      }nt        |t
              r|}nt        |      }|j                  dd      j                  dd      S )N{\{}\})rB   rC   nnModuletypenamerp   r
   replace)rG   rF   rets      rJ   	_typenamezFxGraphDrawer._typename   s[    &%((//2nnV,FC()&1
 ;;sE*223>>rL   full_file_nametruncate_to_last_nc                 n    |j                  d      }t        |      |k\  rdj                  || d        S |S )N/)rq   rg   join)rG   r   r   splitss       rJ   _shorten_file_namez FxGraphDrawer._shorten_file_name   sB    
 $))#.F6{00xx(:':'; <==!!rL   c                    fd}dd|j                    d|j                   dz   }|j                  dk(  r{| j                  ||      }|d| j                  |      z   dz   z  }d	}t	        |d
      r8dj                  |j                  D 	cg c]  }	|	 dt        ||	        c}	      }||dz   z  }n|d| j                  |j                         dz   z  }| j                  r2	 t        |j                  |j                  |j                  d      \  }
}n|j                  |j                  }}
t        |
      dkD  r| ||
      z  }t        |      dkD  r| ||      z  }|dt        |j                         dz   z  }|j                   j#                  d      }|| j%                  |      z  }|j                   j#                  dd       }|*|d|j                    dz   z  }|d|j&                   dz   z  }|ra|j(                  Ut+        |j(                        }| j-                  |j.                        }|d| d|j0                   d|j2                   dz   z  }|dz   S c c}	w # t        $ r |j                  |j                  }}
Y Xw xY w)Nc           
      2   t        | t              r d\  }}| D cg c]  }t        |d       }}nIt        | t              r8d\  }}| j	                         D cg c]  \  }}| dt        |d        }}}nyr|D cg c]	  }d|vs| }}t        |      dk(  ry|d	j                  |      z   |z   }t        |      d
k(  r"|j                  dd      j                  dd      }|j                  dd      j                  dd      S c c}w c c}}w c c}w )N)z	|args=(\lz,\n)\lr]   )max_list_len)z|kwargs={\lz,\n}\l:  %r   z,\n   z\l\nry   rz   r{   r|   )rB   tupler	   dictitemsrg   r   r   )	argprefixsuffixaarg_strs_listkvarg_strsr2   s	           rJ   _get_str_for_args_kwargsz?FxGraphDrawer._get_node_label.<locals>._get_str_for_args_kwargs   s"   c5)%<NFFMP$Q[%C$QM$QT*%>NFFMPYY[%EIQ1#RAA >?@%M %  +0=$N1AQ$NM$N}%*!FKK$>>G}%*'//r:BB5"MH''U3;;CGG# %R% %Os   D	D	DDry   zname=%z	|op_code=
r   r   z\n|r   __constants__r   z|target=T)normalize_to_only_use_kwargsr   z|num_users=tensor_metabuf_metaz|buf=z
|n_origin=z|file=: r{   )r/   r@   rA   r   rr   r   r   rt   rF   r5   r   argskwargs	Exceptionrg   usersmetaget_tensor_meta_to_labeln_originstack_tracer   r   filelinenocode)rG   rl   rH   r2   r3   r   labelleaf_moduleextracr   r   r   r   parsed_stack_tracefnames      `            rJ   _get_node_labelzFxGraphDrawer._get_node_label   s   H, F499+YtwwirBBEww-'"11&$?!<<vEE;8!JJ &1%>%> !  !cGK$;#<=E &8DNN4;;$?#@AEII&&
>'9 KK II KK9=	(f $(99dkk&Dt9q=5d;;Ev;?5f==E;s4::&785@@))--6KT//<<E yy}}Z6H#50588:h&7&7%89EAA !T%5%5%A%78H8H%I"//0B0G0GHUG1%7%>%>$?qASAXAX@YZ
 3;a  % > (,yy$++f>s   	I"0I !JJc                    |yt        |t              r| j                  |      S t        |t              rd}|D ]  }|| j	                  |      z  } |S t        |t
              r-d}|j                         D ]  }|| j	                  |      z  } |S t        |t              rd}|D ]  }|| j	                  |      z  } |S t        dt        |             )Nr   zUnsupported tensor meta type )
rB   r   _stringify_tensor_metalistr   r   valuesr   rs   type)rG   tmresultitemr   s        rJ   r   z#FxGraphDrawer._tensor_meta_to_label2  s    zB/22266B% ?Dd88>>F?B% <Ad88;;F<B& ?Dd88>>F?"%B48*#MNNrL   r   c                    d}t        |d      st        d|       |dt        |j                        z   dz   z  }|dt        t	        |j
                              z   dz   z  }|dt        |j                        z   dz   z  }|dt        |j                        z   dz   z  }|j                  rV|j                  J d	|j                  v sJ |j                  d	   }|t        j                  t        j                  hv rC|d
t        |j                  d         z   dz   z  }|dt        |j                  d         z   dz   z  }n|t        j                  t        j                  t        j                  hv rd|dt        |j                  d         z   dz   z  }|dt        |j                  d         z   dz   z  }|dt        |j                  d         z   dz   z  }nt!        d|       |dt        |j                  d	         z   dz   z  }|S )Nr   dtyper   z|dtype=r   z|shape=z|requires_grad=z|stride=qschemez	|q_scale=scalez|q_zero_point=
zero_pointz|q_per_channel_scale=z|q_per_channel_zero_point=z|q_per_channel_axis=axiszUnsupported qscheme: z	|qscheme=)rr   printrp   r   r   r8   requires_gradstrideis_quantizedqparamsrC   per_tensor_affineper_tensor_symmetricper_channel_affineper_channel_symmetric per_channel_affine_float_qparamsrs   )rG   r   r   r   s       rJ   r   z$FxGraphDrawer._stringify_tensor_metaI  sC   F2w'dB)CM9EAAF)Cbhh,@@5HHF1C8H8H4IIEQQF*S^;eCCFzz--- BJJ...**Y/++..  3c"**W:M6NNQVVVF bjj678  	 F ,,//::! 
  bjj123  	 F  bjj678  	 F  bjj012  	 F ')>wi'HII/#bjj6K2LLuTTMrL   tc                 p    t        |j                        t        t        |j                              z   dz   S )Nr   )rp   r   r   r8   )rG   r   s     rJ   _get_tensor_labelzFxGraphDrawer._get_tensor_label  s'    qww<#d177m"44u<<rL   c                 V    t        j                  |d      i }|j                  j                  D ]0  |rj                  dk(  r j                        }t        j                  j                  fd j                  |||      i|}	}
j                  j                  dd      }|J|j                  dkD  r;|j                  }||vrt        j                  ||      ||<   |j                  |      }
|
j                  |	        fd	}j                  d
k(  s j                  |      |rt        t         j"                  j$                        r* |        3 |j'                         D ]7  }|j)                  dd       |j)                  dd       j+                  |       9 |j                  j                  D ]^  |rj                  dk(  rj,                  D ];  }j/                  t        j0                  j                  |j                               = ` 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.
            TB)rankdirr   r   r   Nr   )r   c            	         t        j                         j                               D ]  \  } }j                  dz   | z   }t	        |t
        j                  j                        r|dz   dz   nd}t        j                  |fdd|z   j                  |      z   dz   it        }j                  |       j                  t        j                  |j                                y )Nrn   z|op_code=get_	parameterzbuffer\lr   ry   r{   )r   named_parametersnamed_buffersr/   rB   rC   r}   	ParameterpydotNoder   r;   add_nodeadd_edgeEdge)	pnameptensorpname1label1
dot_w_node	dot_graphr   rH   rG   s	        rJ   get_module_params_or_buffersz;FxGraphDrawer._to_dot.<locals>.get_module_params_or_buffers  s    */#4468Q8Q8S+ Jw "&S5!8  *'5883E3EF #_4{B!1 
 &+ZZ"&"%,1G1G1P"PSV"V& /&

 "**:6!**5::fdii+HIJrL   r   color	royalbluepenwidth2)r   Dotr>   r?   r@   rk   r   r/   r   r   r   r   Clusterr   rA   rB   rC   rD   rE   r   setadd_subgraphr   r   r   )rG   r.   r/   r0   r1   r2   r3   buf_name_to_subgraphr*   dot_nodecurrent_graphr   buf_namer   subgraphuserr   r   rH   s   `               @@@rJ   r<   zFxGraphDrawer._to_dot  s	     		$5I#% $**00 27!dgg&;,,T2 ::II..$d,CEV
  !*99==T:'H,=,=,A'}}H';;9>$H:,X6 %9$<$<X$FM&&x0J$ 77m+"&"5"5lD"IK8#UXX%9%9B 56e27h 1779 1Wk2Z-&&x01
 %**00 I!dgg&; JJ ID&&uzz$))TYY'GHI	I rL   FFTFNFrU   )   )"__name__
__module____qualname____doc__rC   rD   rE   rp   boolr   rK   r   r   rS   rO   rP   r   rZ   r   rk   r}   r~   rA   r   r   ra   r   r   r   r   r   Tensorr   r<    rL   rJ   r   r   >   s5   	 $)27,0&+-1#(-	((..-	 -	 !	-	
 ,0-	 &*-	  $-	 &c]-	 !-	^	>UYY 	>:	0		 	0	Cuyy 	C	$S%))^(< 	$	 	$sCx. 	.	((//	16	XX__		?C 	?C 	?& '(	"	" !$	"U	HH((U	 ((--U	 &*	U	
  $U	 U	n	Os 	O.5	^ 5	 5	n	=u|| 	= 	=
T	((..T	 T	 !	T	
 ,0T	 &*T	  $T	 YYT	rL   c                   p    e Zd Z	 	 	 	 	 	 ddej                  j
                  dedededededee   d	efd
Z	y)r   Nr.   r/   r0   r1   r2   r3   r4   r5   c	                     t        d      )Nz|FXGraphDrawer requires the pydot package to be installed. Please install pydot through your favorite Python package manager.)rs   )	rG   r.   r/   r0   r1   r2   r3   r4   r5   s	            rJ   rK   zFxGraphDrawer.__init__  s     #J rL   r   )
r   r   r   rC   rD   rE   rp   r   r   rK   r   rL   rJ   r   r     sw     (-6;04*/15',#hh22  !%	
 04 *. $( "*# !%rL   )rb   	itertoolsr   typingr   r   r   rC   torch.fxtorch.fx._compatibilityr   torch.fx.graphr   torch.fx.noder	   r
   torch.fx.operator_schemasr   torch.fx.passes.shape_propr   r   	HAS_PYDOTModuleNotFoundError__all__r_   rf   r;   r   r   rL   rJ   <module>r     s      / /   1 - : 8 5I 
 !"
(   %0Z Z 1Zz 	e	4	 	 
5	 Q  IEs   B 	BB