o
    Zh                     @   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)Z	wall_timeZ	graph_def)r   	visualizer   EventtimeZSerializeToStringZ	add_event)Zgraph_executorZlogdirwpb_graphZevt r   M/var/www/auris/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   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_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/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   :   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)r%   r   r   
<dictcomp>k   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   r3   j   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_forw   s   zvisualize_rec.<locals>.name_forc                    s&   | \}} |  d|d |  d S )NZSubgraphr)   )g)r   r   r   )r3   r@   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 )Nr)   )r   )nextr   r   )r   r   r   ge)add_noder$   r3   r@   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   :)
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   intnodes)r"   r%   r#   r   r$   r   r   )
rB   rF   rE   r$   r3   r@   r#   r?   r   r%   r   r   g   s   

r   )r   NN)N)r	   collectionsr   	functoolsr   r   Ztensorflow.core.frameworkr   Ztensorflow.core.utilr   Z'tensorflow.python.summary.writer.writerr   ImportErrorr   r   r   r   r   r   r   r   <module>   s&   
	-