
    sh                         S SK r S SKJr  S SKJr  S SKr S SKJr  S SKJ	r	  S SK
Jr  S rSS	 jrS
 rSS jrg! \ a
    \" S5      Sef = f)    N)defaultdict)partial)	graph_pb2)	event_pb2)
FileWriterzPTensorBoard visualization of GraphExecutors requires having TensorFlow installedc                     [        U5       n[        U 5      n[        R                  " [        R                  " 5       UR                  5       S9nUR                  U5        S S S 5        g ! , (       d  f       g = f)N)	wall_time	graph_def)r   	visualizer   EventtimeSerializeToString	add_event)graph_executorlogdirwpb_graphevts        r/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/contrib/_tensorboard_vis.pydump_tensorboard_summaryr      sR    	F	q^,ooiikX-G-G-I
 	
C 
		s   AA((
A6c           
         0 nU=(       d    [         R                  " 5       n[        U [        R                  R
                  5      (       a  [        XU[        [        US95        U$ UR                  R                  SUS-   S9  [        U R                  5       R                  5       5       H&  u  pVUS-   [        U5      -   XFR                  5       '   M(     [!        XXU5        UR                  R                  SUS-   S9nU R#                  5       R%                  5        H.  nUR&                  R)                  XFR                  5          5        M0     U$ )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5 ')h*O	
  MM{W'<=e..088:;$/($:SV$C	,,.! < %K<H --##xkH6L#MK""$++-  <<>!:; . O    c           
      |   U R                   b:  [        U R                   US-   U[        U R                  R	                  5       5      S9  [        U R                  R                  5       5       H  u  nu  pVUSU S3-   nUR                  R                  SUS9n[        U5      R                  S5      UR                  S   l        [        UR                  Xr[        UR                  R	                  5       5      5        UR                   c  M  US	-   n	[        UR                   X5        M     U" U R                  US
-   5      $ )a   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#QCqk1
 mm''<m'L'+H~'<'<W'E"$$**mtDII<O<O<Q7RS )!.!8d((*<G  H  [;%>??r4   c                    ^^^^^^^^	^
^ UU4S jm	[        [        5      mUU4S jm
U	U
4S jmUUU	U
U4S jmUUU
UU4S jmU R                  5        H  nT" U5        M     g)zTRecursive part of visualize (basically skips setting up the input and output nodes).c                   > [        U R                  5       UR                  5       5       VVs0 s H'  u  p4UR                  5       TUR                  5          _M)     nnn[        XUTS9  [        U R	                  5       UR	                  5       5       H(  u  pdXVR                  5          TUR                  5       '   M*     g s  snnf )N)r.   r1   r/   r   )zipr,   r)   r*   r'   )	subgraphr   r#   inpvalrec_value_mapoutr   r1   s	          r   rG   #visualize_rec.<locals>.inline_graphj   s       14;;=A
A JJL)CJJL11A 	 
 	PX	
 H,,.?HC&3JJL&AIcjjl# @
s   .B>c                    > U R                  5       U R                  5       R                  S5      S-   S  nTU==   S-  ss'   UTU-   S-   [        TU   5      -   4$ )Nz::      _)kindindexr(   )r#   rY   r/   op_id_counters     r   name_forvisualize_rec.<locals>.name_forw   s`    yy{499;,,T2Q689dq [4'#-M$4G0HHHHr4   c                 R   > T" U 5      u  pT" U R                  S5      US-   U 5        g )NSubgraphr7   )g)r#   r   r   rG   r\   s      r   add_fusion_group'visualize_rec.<locals>.add_fusion_group|   s'    D>TVVJ'T:r4   c           
      x   > T" U 5      u  pTc	  T" U 5        g [        T5      n[        X2S-   T[        TU S95        g )Nr7   )r#   )nextr"   r   )	r#   r   r   geadd_noder0   rG   r\   r   s	       r   add_graph_executor)visualize_rec.<locals>.add_graph_executor   s?    D>TNl#B$3J',T*Jr4   c                   > U R                  5       S:X  a  T" U 5      $ U R                  5       S:X  a  T" U 5      $ T" U 5      u  pT	R                  R                  XS9nU R                  5        H/  nUR                  R                  T
UR                  5          5        M1     [        U R                  5       5       H'  u  pTUS-   [        U5      -   T
UR                  5       '   M)     g )Nzprim::FusionGroupzprim::GraphExecutorr   :)
rY   r#   r$   r,   r   r-   r)   r%   r'   r(   )r#   r   r   pb_noder3   r2   ra   rg   r\   r   r1   s         r   rf   visualize_rec.<locals>.add_node   s    99;--#D))YY[11%d++D>--##r#5[[]EMM  5<<>!:; # "$,,.1HA(,s
SV(;Ielln% 2r4   N)r   intnodes)r.   r1   r/   r   r0   r#   ra   rg   rf   rG   r\   r[   s    ```` @@@@@@r   r*   r*   g   sN    	B ,7s+;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>rw      s^     #  3.B4*@Z1o  
	 s	   9 A	