
    Vh                         d dl 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)	graph_pb2)	event_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        N/home/dcms/DCMS/lib/python3.12/site-packages/torch/contrib/_tensorboard_vis.pydump_tensorboard_summaryr      s[    	F	 q^,ooiikX-G-G-I
 	
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ivaluer+   s           r   r   r       s.   I/9--/H%445 ;')h*O	
  MM{W'<=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.   r/   r   r0   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)
stater/   r   inline_graphr2   arg_specr6   subgraph_nameinput_kindsgrad_subgraph_names
             r   r"   r"   :   s,   $ $$0//#&::e55??AB		
  ))>)>)D)D)FG HHd#QCqk1
 mm''<m'L'+H~'<'<W'E"$$**mXtDII<O<O<Q7RS )!.!8d((*<hGH  [;%>??r4   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.   r1   r/   r   )zipr,   r)   r*   r'   )	subgraphr   r#   inpvalrec_value_mapoutr   r1   s	          r   rG   z#visualize_rec.<locals>.inline_graphj   s       14;;=A
S JJL)CJJL11
 
 	mPX	
 H,,.? 	BHC&3CJJL&AIcjjl#	B
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#   rX   r/   op_id_counters     r   name_forzvisualize_rec.<locals>.name_forw   s`    yy{499;,,T2Q689dq [4'#-M$4G0HHHHr4   c                 T     |       \  }} | j                  d      |dz   |        y )NSubgraphr7   )g)r#   r   r   rG   r[   s      r   add_fusion_groupz'visualize_rec.<locals>.add_fusion_group|   s)    D>DTVVJ'T:r4   c           	           |       \  }}	 |        y t              }t        ||dz   t        |              y )Nr7   )r#   )nextr"   r   )	r#   r   r   geadd_noder0   rG   r[   r   s	       r   add_graph_executorz)visualize_rec.<locals>.add_graph_executor   sC    D>DTNl#B$D3J',T*Jr4   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   :)
rX   r#   r$   r,   r   r-   r)   r%   r'   r(   )r#   r   r   pb_noder3   r2   r_   rd   r[   r   r1   s         r   rc   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%	<r4   N)r   intnodes)r.   r1   r/   r   r0   r#   r_   rd   rc   rG   r[   rZ   s    ```` @@@@@@r   r*   r*   g   sS    	B ,7s+;MI
; < <  r4   ) NN)N)r   collectionsr   	functoolsr   r   tensorflow.core.frameworkr   tensorflow.core.utilr   'tensorflow.python.summary.writer.writerr   ImportErrorr   r   r"   r*    r4   r   <module>rr      s^     #  3.B4*@Z1o  
	 s	   7 A