o
    Hh                     @   s   d dl 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y4   eddw dd	 ZdddZdd ZdddZdS )    N)defaultdict)partial)	graph_pb2)	event_pb2)
FileWriterzPTensorBoard visualization of GraphExecutors requires having TensorFlow installedc                 C   sV   t |}t| }tjt | d}|| W d    d S 1 s$w   Y  d S )N)	wall_time	graph_def)r   	visualizer   EventtimeSerializeToString	add_event)graph_executorlogdirwpb_graphevt r   R/var/www/vscode/kcb/lib/python3.10/site-packages/torch/contrib/_tensorboard_vis.pydump_tensorboard_summary   s   
"r    c                 C   s   i }|pt  }t| tjjrt| ||tt|d |S |j	j
d|d d t|   D ]\}}|d t| || < q.t| |||| |j	j
d|d d}|   D ]}|j||   qW|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*   r   r   r   r	       s   r	   c           
      C   s   | j du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urZ|d
 }	t|j|	| q|| 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/
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_graphr1   arg_specr3   subgraph_nameinput_kindsgrad_subgraph_namer   r   r   r!   :   s"   

r!   c                    st   	fddt tfddfdd fdd 	fd	d
|  D ]}| q1d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 ]\}}||  | < q!d S )Nc                    s"   i | ]\}}|   |   qS r   )r(   ).0inpval)r0   r   r   
<dictcomp>k   s    z7visualize_rec.<locals>.inline_graph.<locals>.<dictcomp>)r-   r0   r.   r   )zipr+   r)   r&   r(   )subgraphr   r"   rec_value_mapoutrK   )r   r0   r   r   rD   j   s   
z#visualize_rec.<locals>.inline_graphc                    sJ   |   |   dd d  }|  d7  < | | d t|  fS )Nz::      _)kindindexr'   )r"   rT   )r.   op_id_counterr   r   name_forw   s   zvisualize_rec.<locals>.name_forc                    s&   | \}} |  d|d |  d S )NSubgraphr4   )g)r"   r   r   )rD   rW   r   r   add_fusion_group|   s   z'visualize_rec.<locals>.add_fusion_groupc                    sF   | \}}d u r |  d S t }t||d t| d d S )Nr4   )r"   )nextr!   r   )r"   r   r   ge)add_noder/   rD   rW   r   r   r   add_graph_executor   s   z)visualize_rec.<locals>.add_graph_executorc                    s   |   dkr
 | S |   dkr| S | \}}jj||d}|  D ]}|j|   q&t|  D ]\}}|d t	| | < q9d S )Nzprim::FusionGroupzprim::GraphExecutorr   :)
rT   r"   r#   r+   r   r,   r(   r$   r&   r'   )r"   r   r   pb_noder2   r1   )rZ   r^   rW   r   r0   r   r   r]      s   zvisualize_rec.<locals>.add_nodeN)r   intnodes)r-   r0   r.   r   r/   r"   r   )
rZ   r^   r]   r/   rD   rW   r.   rV   r   r0   r   r)   g   s   

r)   )r   NN)N)r   collectionsr   	functoolsr   r   tensorflow.core.frameworkr   tensorflow.core.utilr   'tensorflow.python.summary.writer.writerr   ImportErrorr   r	   r!   r)   r   r   r   r   <module>   s&   
	-