Ë
    g^(hó  ã                   ó’   — 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        ú\/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/contrib/_tensorboard_vis.pyÚdump_tensorboard_summaryr      s[   € Ü	�FÓ	ð ˜qÜ˜^Ó,ˆÜ�o‰oÜ—i‘i“k¨X×-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ô	
ð ˆð ‡M�M×Ñ˜ {°WÑ'<ÐÔ=Ü˜e×.Ñ.Ó0×8Ñ8Ó:Ó;ò D‰ˆˆ5Ø$/°(Ñ$:¼SÀ»VÑ$Cˆ	�%—,‘,“.Ò!ðDô �%˜ K°¸<ÔHð —-‘-×#Ñ# x°kÀHÑ6LÐ#ÓM€KØ×"Ñ"Ó$×+Ñ+Ó-ò <ˆØ×Ñ× Ñ  ¨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ÑˆÑˆH�dØ#¨¨Q¨C¨q kÑ1ˆð
 —m‘m×'Ñ'¨<¸mÐ'ÓLˆÜ'+¨H£~×'<Ñ'<¸WÓ'Eˆ×Ñ˜Ñ"Ô$ä�$—*‘*˜m¨X´t¸D¿I¹I×<OÑ<OÓ<QÓ7RÔ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±   ø€ ô   §¡Ó 1°4·;±;³=ÓA÷
á��Sð �J‰J‹L˜) C§J¡J£LÑ1Ñ1ð
ˆñ 
ô 	Ø mÀÐPXõ	
ô ˜H×,Ñ,Ó.°·±³Ó?ò 	B‰HˆC�Ø&3°C·J±J³LÑ&AˆI�c—j‘j“lÒ#ñ	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`   ø€ Ø�y‰y‹{˜4Ÿ9™9›;×,Ñ,¨TÓ2°QÑ6Ð8Ð9ˆØ�dÓ˜qÑ ÓØ�[ 4Ñ'¨#Ñ-´°MÀ$Ñ4GÓ0HÑ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Ù�T—V‘V˜JÓ'¨°©°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ØÐÙ�T�Nä�lÓ#ˆBÜ$Ø�D˜3‘J ¬'°,À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Ì   ø€ Ø�9‰9‹;Ð-Ò-Ù# DÓ)Ð)Ø�Y‰Y‹[Ð1Ò1Ù% dÓ+Ð+Ù˜D“>‰ˆˆDØ—-‘-×#Ñ# r°Ð#Ó5ˆØ—[‘[“]ò 	<ˆEØ�M‰M× Ñ  ¨5¯<©<«>Ñ!:Õ;ð	<ô " $§,¡,£.Ó1ò 	<‰HˆAˆuØ(,¨s©
´S¸³VÑ(;ˆI�e—l‘l“nÒ%ñ	<r4   N)r   ÚintÚnodes)r.   r1   r/   r   r0   r#   r_   rd   rc   rG   r[   rZ   s    ```` @@@@@@r   r*   r*   g   sS   ÿù€ õ	Bô ,7´sÓ+;€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