U
    yh7                     @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlZz(d dlm	Z	 d dl
mZ d dlmZ W n ek
rz   eddY nX d	d
 ZdddZdd ZdddZdS )    N)defaultdict)partial)DefaultDict)	event_pb2)	graph_pb2)
FileWriterzPTensorBoard visualization of GraphExecutors requires having TensorFlow installedc              	   C   s@   t |.}t| }tjt | d}|| W 5 Q R X d S )N)Z	wall_timeZ	graph_def)r   	visualizer   EventtimeZSerializeToStringZ	add_event)Zgraph_executorZlogdirwpb_graphZevt r   P/var/www/html/venv/lib/python3.8/site-packages/torch/contrib/_tensorboard_vis.pydump_tensorboard_summary   s    
r    c           	      C   s   i }|pt  }t| tjjr8t| ||tt|d |S |j	j
d|d d}t|   D ] \}}|d t| || < q\t| |||| |j	j
d|d d}|   D ]}|j||   q|S )z5Visualizes an independent graph, or a graph executor.)r   inputopnamezinput:output)r   ZGraphDef
isinstancetorchZ_CZGraphExecutorStatevisualize_graph_executorr   r   nodeadd	enumerateZ
param_nodeoutputsstruniquevisualize_recreturn_nodeinputsr   append)	graphname_prefixr   executors_it	value_mapZ
input_nodeivaluer    r   r   r   r      s    
r   c           
      C   s   | j dk	r*t| j |d |t| j d t| j D ]|\}\}}|d| d }|jj	d|d}t
|d|jd	 _t|j||t|j  |jdk	r8|d
 }	t|j|	| q8|| j|d 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#   r$   r   r%   plan/Z
INPUT_KINDr   asciir!   zgrad/z	original/)Zautograd_fallback_graphr   iterZautograd_fallbackZ	executorsr   Zexecution_plansitemsr   r   reprencodeattrsr#   codeZgrad_executor)
stater$   r   inline_graphr'   Zarg_specr)   Zsubgraph_nameZinput_kindsZgrad_subgraph_namer   r   r   r   6   s    

r   c                    st   	fddt tfddfdd fdd 	fd	d
|  D ]}| qbdS )zTRecursive part of visualize (basically skips setting up the input and output nodes).c                    sd   fddt |  | D }t| || d t |  | D ]\}}||  | < qBd S )Nc                    s"   i | ]\}}|   |   qS r   )r   ).0Zinpval)r&   r   r   
<dictcomp>d   s    z7visualize_rec.<locals>.inline_graph.<locals>.<dictcomp>)r#   r&   r$   r   )zipr!   r   r   r   )Zsubgraphr   r   Zrec_value_mapoutr6   )r   r&   r   r   r4   c   s    
z#visualize_rec.<locals>.inline_graphc                    sJ   |   |   dd d  }|  d7  < | | d t|  fS )Nz::      _)kindindexr   )r   r=   )r$   op_id_counterr   r   name_foro   s    zvisualize_rec.<locals>.name_forc                    s&   | \}} |  d|d |  d S )NZSubgraphr*   )g)r   r   r   )r4   r@   r   r   add_fusion_groupt   s    z'visualize_rec.<locals>.add_fusion_groupc                    sD   | \}}d kr |  n"t }t||d t| d d S )Nr*   )r   )nextr   r   )r   r   r   ge)add_noder%   r4   r@   r   r   r   add_graph_executorx   s    

z)visualize_rec.<locals>.add_graph_executorc                    s   |   dkr | S |   dkr(| S | \}}jj||d}|  D ]}|j|   qLt|  D ] \}}|d t	| | < qrd S )Nzprim::FusionGroupzprim::GraphExecutorr   :)
r=   r   r   r!   r   r"   r   r   r   r   )r   r   r   Zpb_noder(   r'   )rB   rF   r@   r   r&   r   r   rE      s    zvisualize_rec.<locals>.add_nodeN)r   intZnodes)r#   r&   r$   r   r%   r   r   )
rB   rF   rE   r%   r4   r@   r$   r?   r   r&   r   r   a   s    
	r   )r   NN)N)r
   collectionsr   	functoolsr   typingr   r   Ztensorflow.core.utilr   Ztensorflow.core.frameworkr   Z'tensorflow.python.summary.writer.writerr   ImportErrorr   r   r   r   r   r   r   r   <module>   s    
+