
    [ThI                     @   S SK r S SKJr  S SKJrJrJr  S SKrS SKrS SK	J
r
  S SKJr  S SKJrJr  S SKJr  S SKJr   S SKrS	rS/rSSSSSS.r/ SQrSSSS.r\(       a  \
" S
S9 " S S5      5       rg\(       d  \
" S
S9 " S S5      5       rgg! \ a    S
rSr NXf = f)    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                   (   \ rS rSrSr      S#S\R                  R                  S\S\	S\	S\	S	\	S
\
\   S\	4S jjrS$S\R                  4S jjrS\R                  4S jrS\R                  4S jrS\\\R                  4   4S jrS\R                  R&                  S\\\4   4S jrS\R*                  R,                  S\R                  R&                  S\R*                  R,                  4S jrS\S\4S jr S%S\S\4S jjrS\R                  R                  S\R                  R&                  S\	S	\	S\4
S jrS\4S jrS\S\4S jrS\R@                  S\4S  jr!S\R                  R                  S\S\	S\	S\	S	\	S\R                  4S! jr"S"r#g)&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	           	         X l         Ub  UOSU l        Xl        U R                  [        S'   X R	                  UUUUUU5      0U l        UR                  R                   H  n	U	R                  S:w  a  M  U R                  X5      n
[        U
[        R                  R                  5      (       d  MQ  U R	                  U
U SU	R                   3UUUU5      U R
                  U SU	R                   3'   M     g )Nrecordshaper   _)_namer5   r6   _WEIGHT_TEMPLATE_to_dot_dot_graphsgraphnodesop_get_leaf_node
isinstancetorchfxGraphModuletarget)selfr/   r0   r1   r2   r3   r4   r5   r6   node	leaf_nodes              T/var/www/auris/envauris/lib/python3.13/site-packages/torch/fx/passes/graph_drawer.py__init__FxGraphDrawer.__init__G   s     J#2#>H   #1(,(<(<W% ll "1+%	 D %**0077m+ //C	!)UXX-A-ABB<@LLfAdkk]+"1+%=  D64;;-!89 1    returnc                 J    Uc  U R                  5       $ U R                  U5      $ )a9  
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rH   submod_names     rK   get_dot_graphFxGraphDrawer.get_dot_graphv   s*    0 "..0000==rN   c                 4    U R                   U R                     $ Nr>   r;   rH   s    rK   rQ    FxGraphDrawer.get_main_dot_graph   s    ##DJJ//rN   c                 >    U R                   U R                   SU 3   $ )Nr:   rY   rS   s     rK   rR   "FxGraphDrawer.get_submod_dot_graph   s"    ##tzzl!K=$ABBrN   c                     U R                   $ rX   )r>   rZ   s    rK   get_all_dot_graphs FxGraphDrawer.get_all_dot_graphs   s    ###rN   rI   c                 j   U R                   SSSS.nUR                  [        ;   a  [        UR                     US'   U$ UR                  UR                  5      n[        [        R                  " UR                  5       SS9R                  5       S S S	5      n[        U[        [        5      -     US'   U$ )
Nz#CAFFE3r'   r(   )r9   r)   r*   r+   r)   F)usedforsecurity      )r5   rA   
_COLOR_MAP_pretty_print_targetrG   inthashlibmd5encode	hexdigest_HASH_COLOR_MAPlen)rH   rI   templatetarget_nametarget_hashs        rK   _get_node_styleFxGraphDrawer._get_node_style   s    --&+&	H ww*$(2477(;% O #77D!KK#**,eik"1& 	 )8#o"66)% OrN   modulec                    Un[        UR                  [        5      (       d   eUR                  R                  S5      nU H;  n[	        X55      (       d  [        [        U5      S-   U-   S-   5      e[        X55      nM=     U$ )N.z does not have attribute !)rC   rG   strsplithasattrRuntimeErrorgetattr)rH   rs   rI   py_objatomsatoms         rK   rB   FxGraphDrawer._get_leaf_node   s     Fdkk3////KK%%c*Ev,,&F&AADH3N  !.  MrN   rG   c                    [        U[        R                  R                  5      (       a  [        R                  " U5      nO#[        U[
        5      (       a  UnO[        U5      nUR                  SS5      R                  SS5      $ )N{\{}\})rC   rD   nnModuletypenamerw   r
   replace)rH   rG   rets      rK   	_typenameFxGraphDrawer._typename   sa    &%((//22nnV,FC(()&1
 ;;sE*223>>rN   full_file_nametruncate_to_last_nc                 n    UR                  S5      n[        U5      U:  a  SR                  X2* S  5      $ U$ )N/)rx   rm   join)rH   r   r   splitss       rK   _shorten_file_name FxGraphDrawer._shorten_file_name   s?    
 $))#.F6{00xx':'; <==!!rN   c                 (  ^ U4S jnSSUR                    SUR                   S3-   nUR                  S:X  a  U R                  X5      nUSU R                  U5      -   S-   -  nS	n[	        US
5      (       a:  SR                  UR                   V	s/ s H  n	U	 S[        Xy5       3PM     sn	5      nXhS-   -  nOUSU R                  UR                  5       3S-   -  nU R                  (       a.   [        UR                  UR                  UR                  SS9u  pOUR                  UR                  p[        U
5      S:  a
  Xe" U
5      -  n[        U5      S:  a
  Xe" U5      -  nUS[        UR                  5       3S-   -  nUR                   R#                  S5      nX`R%                  U5      -  nUR                   R#                  SS 5      nUb*  USUR                    3S-   -  nUSUR&                   3S-   -  nU(       ab  UR(                  bU  [+        UR(                  5      nU R-                  UR.                  5      nUSU SUR0                   SUR2                   3S-   -  nUS-   $ s  sn	f ! [         a    UR                  UR                  p GN^f = f)Nc                 Z  > [        U [        5      (       a  Su  pU  Vs/ s H  n[        USS9PM     nnOL[        U [        5      (       a6  Su  pU R	                  5        VVs/ s H  u  pVU S[        USS9 3PM     nnnOgT(       a  U Vs/ s H  nSU;  d  M  UPM     nn[        U5      S:X  a  gUS	R                  U5      -   U-   n[        U5      S
:X  a"  UR                  SS5      R                  SS5      nUR                  SS5      R                  SS5      $ s  snf s  snnf s  snf )N)z	|args=(\lz,\n)\lrc   )max_list_len)z|kwargs={\lz,\n}\l:  %r   z,\n   z\l\nr   r   r   r   )rC   tupler	   dictitemsrm   r   r   )	argprefixsuffixaarg_strs_listkvarg_strsr3   s	           rK   _get_str_for_args_kwargs?FxGraphDrawer._get_node_label.<locals>._get_str_for_args_kwargs   s*   c5))%<NFMP$QS[%CSM$QMT**%>NFMPYY[%MXTQ1#RAA >?@[ " %M  +0=$N1AQM$N}%*!FKK$>>G}%*'//r:BB5"MH''U3;;CGG# %R% %Os   D"D"
D(D(r   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   )r0   rA   rB   r   ry   r   r   r{   rG   r6   r   argskwargs	Exceptionrm   usersmetaget_tensor_meta_to_labeln_originstack_tracer   r   filelinenocode)rH   rs   rI   r3   r4   r   labelleaf_moduleextracr   r   r   r   parsed_stack_tracefnames      `            rK   _get_node_labelFxGraphDrawer._get_node_label   s   H, F499+YtwwirBBEww-'"11&?!<<vEE;88!JJ &1%>%>%>  !cGK$;#<=%>E &8DNN4;;$?#@AEII&&
>'9 KK II KK9=	(f $(99dkk&t9q=5d;;Ev;?5f==E;s4::&785@@))--6K//<<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'.,I, ,!JJc                    Uc  g[        U[        5      (       a  U R                  U5      $ [        U[        5      (       a   SnU H  nX R	                  U5      -  nM     U$ [        U[
        5      (       a.  SnUR                  5        H  nX R	                  U5      -  nM     U$ [        U[        5      (       a   SnU H  nX R	                  U5      -  nM     U$ [        S[        U5       35      e)Nr   zUnsupported tensor meta type )
rC   r   _stringify_tensor_metalistr   r   valuesr   rz   type)rH   tmresultitemr   s        rK   r   #FxGraphDrawer._tensor_meta_to_label2  s    zB//22266B%%D88>>F B%%A88;;F %B&&D88>>F "%B48*#MNNrN   r   c                 $   Sn[        US5      (       d  [        SU5        US[        UR                  5      -   S-   -  nUS[        [	        UR
                  5      5      -   S-   -  nUS[        UR                  5      -   S-   -  nUS[        UR                  5      -   S-   -  nUR                  (       Ga]  UR                  c   eS	UR                  ;   d   eUR                  S	   nU[        R                  [        R                  1;   aC  US
[        UR                  S   5      -   S-   -  nUS[        UR                  S   5      -   S-   -  nOU[        R                  [        R                  [        R                  1;   ad  US[        UR                  S   5      -   S-   -  nUS[        UR                  S   5      -   S-   -  nUS[        UR                  S   5      -   S-   -  nO[!        SU 35      eUS[        UR                  S	   5      -   S-   -  nU$ )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=)ry   printrw   r   r   r9   requires_gradstrideis_quantizedqparamsrD   per_tensor_affineper_tensor_symmetricper_channel_affineper_channel_symmetric per_channel_affine_float_qparamsrz   )rH   r   r   r   s       rK   r   $FxGraphDrawer._stringify_tensor_metaI  sH   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MrN   tc                 p    [        UR                  5      [        [        UR                  5      5      -   S-   $ )Nr   )rw   r   r   r9   )rH   r   s     rK   _get_tensor_labelFxGraphDrawer._get_tensor_label  s'    qww<#d177m"44u<<rN   c                   ^ ^^^ [         R                  " USS9m0 nUR                  R                   GHF  mU(       a  TR                  S:X  a  M  T R                  T5      n[         R                  " TR                  4ST R                  UTXV5      0UD6n	Tn
TR                  R                  SS5      nUbH  UR                  S:  a8  UR                  nX;  a  [         R                  " XS9X|'   UR                  U5      n
U
R                  U	5        UUUU 4S	 jnTR                  S
:X  d  M  T R                  UT5      mU(       a  GM  [        T[         R"                  R$                  5      (       a  GM?  U" 5         GMI     UR'                  5        H8  nUR)                  SS5        UR)                  SS5        TR+                  U5        M:     UR                  R                   Hi  mU(       a  TR                  S:X  a  M  TR,                   H=  nTR/                  [         R0                  " TR                  UR                  5      5        M?     Mk     T$ )z
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R                  5       TR                  5       5       H  u  pTR                  S-   U -   n[	        U[
        R                  R                  5      (       a  US-   S-   OSn[        R                  " U4SSU-   TR                  U5      -   S-   0[        D6nTR                  U5        TR                  [        R                  " UTR                  5      5        M     g )Nru   z|op_code=get_	parameterzbuffer\lr   r   r   )r   named_parametersnamed_buffersr0   rC   rD   r   	ParameterpydotNoder   r<   add_nodeadd_edgeEdge)	pnameptensorpname1label1
dot_w_node	dot_graphr   rI   rH   s	        rK   get_module_params_or_buffers;FxGraphDrawer._to_dot.<locals>.get_module_params_or_buffers  s    */#4468Q8Q8S+ "&S5!8  *'5883E3EFF #_4{B!1 
 &+ZZ"&"%,1G1G1P"PSV"V& /&

 "**:6!**5::fdii+HI+rN   r   color	royalbluepenwidth2)r   Dotr?   r@   rA   rq   r   r0   r   r   r   r   Clusterr   rB   rC   rD   rE   rF   r   setadd_subgraphr   r   r   )rH   r/   r0   r1   r2   r3   r4   buf_name_to_subgraphr*   dot_nodecurrent_graphr   buf_namer   subgraphuserr   r   rI   s   `               @@@rK   r=   FxGraphDrawer._to_dot  s     		$5I#% $**00!dgg&;,,T2 ::II..$d,C
  !*99==T:'H,=,=,A'}}H;9>$:,6 %9$<$<X$FM&&x0J J$ 77m+"&"5"5lD"IK88#UXX%9%9B B 56e 1h 1779Wk2Z-&&x0 :
 %**00!dgg&; JJD&&uzz$))TYY'GH '	 1 rN   )r>   r;   r5   r6   FFTFNFrX   )   )$__name__
__module____qualname____firstlineno____doc__rD   rE   rF   rw   boolr   rL   r   r   rU   rQ   rR   r   r_   r   rq   r   r   rB   r   r   rg   r   r   r   r   r   Tensorr   r=   __static_attributes__ rN   rK   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	rN   c                   |    \ rS rSr      SS\R
                  R                  S\S\S\S\S\S	\	\   S
\4S jjr
Srg)r   i  Nr/   r0   r1   r2   r3   r4   r5   r6   c	                     [        S5      e)Nz|FXGraphDrawer requires the pydot package to be installed. Please install pydot through your favorite Python package manager.)rz   )	rH   r/   r0   r1   r2   r3   r4   r5   r6   s	            rK   rL   rM     s     #J rN   r  r  )r  r	  r
  r  rD   rE   rF   rw   r  r   rL   r  r  rN   rK   r   r     s|     (-6;04*/15',#hh22  !%	
 04 *. $( "*# !% rN   )rh   	itertoolsr   typingr   r   r   rD   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__re   rl   r<   r   r  rN   rK   <module>r     s      / /   1 - : 8 5I 
 !"
(   %0Z Z 1Zz 	e	4	 	 
5	 Q  IEs   B 
BB