
    sg7                         d dl Z d dlmZ d dlmZ d dlmZ d dlZ	 d dlm	Z	 d dl
mZ d dlmZ d	 Zdd
Zd ZddZy# e$ r
  ed      dw xY w)    N)defaultdict)partial)DefaultDict)	event_pb2)	graph_pb2)
FileWriterzPTensorBoard visualization of GraphExecutors requires having TensorFlow installedc                     t        |      5 }t        |       }t        j                  t	        j                         |j                               }|j                  |       d d d        y # 1 sw Y   y xY w)N)	wall_time	graph_def)r   	visualizer   EventtimeSerializeToString	add_event)graph_executorlogdirwpb_graphevts        Q/var/www/html/venv/lib/python3.12/site-packages/torch/contrib/_tensorboard_vis.pydump_tensorboard_summaryr      sV    	F	 q^,oo		x?Y?Y?[\	C  s   AA))A2c           	         i }|xs t        j                         }t        | t        j                  j
                        rt        | ||t        t        |             |S |j                  j                  d|dz         }t        | j                         j                               D ]'  \  }}|dz   t        |      z   ||j                         <   ) t!        | ||||       |j                  j                  d|dz         }| j#                         j%                         D ].  }|j&                  j)                  ||j                                   0 |S )z5Visualizes an independent graph, or a graph executor.)r   inputopnamezinput:output)r   GraphDef
isinstancetorch_CGraphExecutorStatevisualize_graph_executorr   r   nodeadd	enumerate
param_nodeoutputsstruniquevisualize_recreturn_nodeinputsr   append)	graphname_prefixr   executors_it	value_map
input_nodeivaluer,   s	            r   r   r      s/   I/9--/H%445 X!(X!F	H ""gK'4I"JJe..088:; D5$/($:SV$C	%,,.!D %K<H --##xkH6L#MK""$++- <  5<<>!:;< O    c           
         | j                   =t        | j                   |dz   |t        | j                  j	                                      t        | j                  j                               D ]  \  }\  }}|d| dz   }|j                  j                  d|      }t        |      j                  d      |j                  d   _        t        |j                  ||t        |j                  j	                                      |j                   |d	z   }	t        |j                   |	|         || j                  |d
z         S )aT  Append the state of a given GraphExecutor to the graph protobuf.

    Args:
        state (GraphExecutor or GraphExecutorState): GraphExecutor to display.
        name_prefix (str): Name prefix of the containing subgraph.
        pb_graph (GraphDef): graph to append to.
        inline_graph (Callable): a function that handles setting up a value_map,
            so that some graphs in here can be inlined. This is necessary, because
            this will simply be `visualize` for the top-level GraphExecutor,
            or `inline_graph` for all nested ones.

            The signature should look like (Graph, name_prefix) -> ().
            It will be called exactly once.

    The strategy is to embed all different configurations as independent subgraphs,
    while inlining the original graph as the one that actually produces the values.
    zautograd_fallback/)r/   r0   r   r1   plan/
INPUT_KINDr   asciir-   zgrad/z	original/)autograd_fallback_graphr   iterautograd_fallback	executorsr&   execution_plansitemsr$   r%   reprencodeattrsr/   codegrad_executor)
stater0   r   inline_graphr4   arg_specr8   subgraph_nameinput_kindsgrad_subgraph_names
             r   r#   r#   6   s,   $ $$055),@@##E$;$;$E$E$GH	J
  ))>)>)D)D)FG HHd#QCqk1
 mm''<m'L'+H~'<'<W'E"$$**mXtDII<O<O<Q7RS )!.!8d((*<hGH  [;%>??r6   c                    	
 fd	t        t              fd
	
fd	
fd
fd| j                         D ]
  } |        y)zTRecursive part of visualize (basically skips setting up the input and output nodes).c                    t        | j                         |j                               D ci c]'  \  }}|j                         |j                            ) }}}t        | ||       t        | j	                         |j	                               D ])  \  }}||j                            |j                         <   + y c c}}w )N)r/   r2   r0   r   )zipr-   r*   r+   r(   )	subgraphr   r$   inpvalrec_value_mapoutr   r2   s	          r   rI   z#visualize_rec.<locals>.inline_graphc   s    ),X__->)NP%S# y'>> P PH -"&'	) H,,.? 	BHC&3CJJL&AIcjjl#	BPs   ,C c                     | j                         | j                         j                  d      dz   d  }|xx   dz  cc<   ||z   dz   t        |         z   fS )Nz::      _)kindindexr)   )r$   rZ   r0   op_id_counters     r   name_forzvisualize_rec.<locals>.name_foro   s`    yy{499;,,T2Q678dq [4'#-M$4G0HHHHr6   c                 T     |       \  }} | j                  d      |dz   |        y )NSubgraphr9   )g)r$   r   r   rI   r]   s      r   add_fusion_groupz'visualize_rec.<locals>.add_fusion_groupt   s)    D>DTVVJ'T:r6   c           	           |       \  }}	 |        y t              }t        ||dz   t        |              y )Nr9   )r$   )nextr#   r   )	r$   r   r   geadd_noder1   rI   r]   r   s	       r   add_graph_executorz)visualize_rec.<locals>.add_graph_executorx   sD    D>DTNl#B$RX%,\%EGr6   c                    | j                         dk(  r |       S | j                         dk(  r |       S  |       \  }}	j                  j                  ||      }| j                         D ].  }|j                  j                  
|j                                   0 t        | j                               D ]'  \  }}|dz   t        |      z   
|j                         <   ) y )Nzprim::FusionGroupzprim::GraphExecutorr   :)
rZ   r$   r%   r-   r   r.   r*   r&   r(   r)   )r$   r   r   pb_noder5   r4   ra   rf   r]   r   r2   s         r   re   zvisualize_rec.<locals>.add_node   s    99;--#D))YY[11%d++D>D--##r#5[[] 	<EMM  5<<>!:;	< "$,,.1 	<HAu(,s
SV(;Ielln%	<r6   N)r   intnodes)r/   r2   r0   r   r1   r$   ra   rf   re   rI   r]   r\   s    ```` @@@@@@r   r+   r+   a   sU    B ,7s+;MI
;G G< <  r6   ) NN)N)r   collectionsr   	functoolsr   typingr   r    tensorflow.core.utilr   tensorflow.core.frameworkr   'tensorflow.python.summary.writer.writerr   ImportErrorr   r   r#   r+    r6   r   <module>ru      s_     #   8.3B2(@V.a  8
 - .3788s	   = A