
    `i                         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 n# e$ r  ed          dw xY wd Zdd
Zd ZddZdS )    N)defaultdict)partial)	graph_pb2)	event_pb2)
FileWriterzPTensorBoard visualization of GraphExecutors requires having TensorFlow installedc                    t          |          5 }t          |           }t          j        t	          j                    |                                          }|                    |           d d d            d S # 1 swxY w Y   d S )N)	wall_time	graph_def)r   	visualizer   EventtimeSerializeToString	add_event)graph_executorlogdirwpb_graphevts        r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/contrib/_tensorboard_vis.pydump_tensorboard_summaryr      s    	F		 q^,,oikkX-G-G-I-I
 
 
 	
C                 s   AA;;A?A? c           	         i }|pt          j                    }t          | t          j        j                  r(t          | ||t          t          |                     |S |j	        
                    d|dz              t          |                                                                           D ]/\  }}|dz   t          |          z   ||                                <   0t!          | ||||           |j	        
                    d|dz             }|                                                                 D ]4}|j                            ||                                                    5|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       sf   I/9-//H%455  ;')h*O*O*O	
 	
 	
  M{W'<===e..0088::;; D D5$/($:SVV$C	%,,..!!%K<HHH -##xkH6L#MMK""$$++-- < <  5<<>>!:;;;;O    c           
      |   | j         ?t          | j         |dz   |t          | j                                                             t          | j                                                  D ]\  }\  }}|d| dz   }|j        	                    d|          }t          |                              d          |j        d	         _        t          |j        ||t          |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.
    Nz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_graphr3   arg_specr7   subgraph_nameinput_kindsgrad_subgraph_names
             r   r#   r#   :   sH   $ $0/#&::e5??AABB		
 	
 	
 	
  ))>)D)D)F)FGG H HHd#kQkkk1
 m''<m'LL'+H~~'<'<W'E'E"$$*mXtDI<O<O<Q<Q7R7RSSS )!.!8d(*<hGGG<[;%>???r5   c                    	
 fd	t          t                    fd
	
fd	
fd
fd|                                 D ]} |           dS )zTRecursive part of visualize (basically skips setting up the input and output nodes).c                    fdt          |                                 |                                          D             }t          | ||           t          |                                 |                                          D ]4\  }}||                                         |                                <   5d S )Nc                 p    i | ]2\  }}|                                 |                                          3S  )r*   ).0inpvalr2   s      r   
<dictcomp>z7visualize_rec.<locals>.inline_graph.<locals>.<dictcomp>k   sD     
 
 
S JJLL)CJJLL1
 
 
r5   )r/   r2   r0   r   )zipr-   r+   r(   r*   )subgraphr   r$   rec_value_mapoutrS   r   r2   s         r   rH   z#visualize_rec.<locals>.inline_graphj   s    
 
 
 
 1 14;;==AA
 
 
 	mPX	
 	
 	
 	
 H,,..?? 	B 	BHC&3CJJLL&AIcjjll##	B 	Br5   c                     |                                  |                                                      d          dz   d          }|xx         dz  cc<   ||z   dz   t          |                   z   fS )Nz::      _)kindindexr)   )r$   r]   r0   op_id_counters     r   name_forzvisualize_rec.<locals>.name_forw   sy    yy{{499;;,,T22Q6889dq [4'#-M$4G0H0HHHHr5   c                 j     |           \  }} |                      d          |dz   |            d S )NSubgraphr8   )g)r$   r   r   rH   r`   s      r   add_fusion_groupz'visualize_rec.<locals>.add_fusion_group|   s>    8D>>DTVVJ''T:::::r5   c           	           |           \  }} |            d S t                    }t          ||dz   t          |                      d S )Nr8   )r$   )nextr#   r   )	r$   r   r   geadd_noder1   rH   r`   r   s	       r   add_graph_executorz)visualize_rec.<locals>.add_graph_executor   sn    8D>>DHTNNNNNl##B$D3J',T*J*J*J    r5   c                    |                                  dk    r |           S |                                  dk    r |           S  |           \  }}	j                            ||          }|                                 D ]4}|j                            
|                                                    5t          |                                           D ]/\  }}|dz   t          |          z   
|                                <   0d S )Nzprim::FusionGroupzprim::GraphExecutorr   :)
r]   r$   r%   r-   r   r.   r*   r&   r(   r)   )r$   r   r   pb_noder4   r3   rd   ri   r`   r   r2   s         r   rh   zvisualize_rec.<locals>.add_node   s   99;;---##D)))YY[[111%%d+++8D>>D-##r#55[[]] 	< 	<EM  5<<>>!:;;;;!$,,..11 	< 	<HAu(,s
SVV(;Iellnn%%	< 	<r5   N)r   intnodes)r/   r2   r0   r   r1   r$   rd   ri   rh   rH   r`   r_   s    ```` @@@@@@r   r+   r+   g   s   	B 	B 	B 	B 	B 	B ,7s+;+;MI I I I I I
; ; ; ; ; ;        < < < < < < < < <    r5   )r   NN)N)r   collectionsr   	functoolsr   r    tensorflow.core.frameworkr   tensorflow.core.utilr   'tensorflow.python.summary.writer.writerr   ImportErrorr   r   r#   r+   rP   r5   r   <module>ru      s    # # # # # #       333333......BBBBBBB   
+	       4*@ *@ *@Z1 1 1 1 1 1s   ) ;