
    ij_<                         d Z ddlmZ ddlmZ ddlmZ d Zd Z	d Z
d Zd	 Zd
 Z G d de      Z G d de      Zd Z G d de      Zd Zy)z=Classes and methods for processing debugger-decorated graphs.    )	graph_pb2)op_def_registry)
tf_loggingc                     d| v rI| j                  d      s8| d| j                  d       }t        | | j                  d      dz   d       }||fS | dfS )a  Get the node name from a string that can be node or tensor name.

  Args:
    name: An input node name (e.g., "node_a") or tensor name (e.g.,
      "node_a:0"), as a str.

  Returns:
    1) The node name, as a str. If the input name is a tensor name, i.e.,
      consists of a colon, the final colon and the following output slot
      will be stripped.
    2) If the input name is a tensor name, the output slot, as an int. If
      the input name is not a tensor name, None.
  :N   )endswithrfindint)name	node_nameoutput_slots      }/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/tensorflow/python/debug/lib/debug_graphs.pyparse_node_or_tensor_namer      s_     	D[s+%djjo&Id4::c?Q./01Kk!!:    c                 "    t        |       \  }}|S Nr   )element_namer   _s      r   get_node_namer   -   s    *<8,)Q	r   c                 *    t        |       \  }}||S dS )a  Get the output slot number from the name of a graph element.

  If element_name is a node name without output slot at the end, 0 will be
  assumed.

  Args:
    element_name: (`str`) name of the graph element in question.

  Returns:
    (`int`) output slot number.
  r   r   )r   r   r   s      r   get_output_slotr   2   s"     -\:.![#/6Q6r   c                 $    | j                  d      S )a=  Determine whether a node name is that of a debug Copy node.

  Such nodes are inserted by TensorFlow core upon request in
  RunOptions.debug_options.debug_tensor_watch_opts.

  Args:
    node_name: Name of the node.

  Returns:
    A bool indicating whether the input argument is the name of a debug Copy
    node.
  __copy_
startswithr   s    r   is_copy_noder   B   s     
		i	((r   c                 $    | j                  d      S )a/  Determine whether a node name is that of a debug node.

  Such nodes are inserted by TensorFlow core upon request in
  RunOptions.debug_options.debug_tensor_watch_opts.

  Args:
    node_name: Name of the node.

  Returns:
    A bool indicating whether the input argument is the name of a debug node.
  __dbg_r   r   s    r   is_debug_noder"   R   s     
		h	''r   c                    d}| }|j                  |      st        d| z        |t        |      d }|j                  d      dk  rt        d| z        ||j	                  d      dz   d }|d|j	                  d       }t        ||j	                  d      dz   d       }|d|j	                  d       }|j                  d      dk7  rt        d	| z        |d|j                  d       }t        ||j                  d      dz   d       }||||fS )
a~  Parse the name of a debug node.

  Args:
    node_name: Name of the debug node.

  Returns:
    1. Name of the watched node, as a str.
    2. Output slot index of the watched tensor, as an int.
    3. Index of the debug node, as an int.
    4. Name of the debug op, as a str, e.g, "DebugIdentity".

  Raises:
    ValueError: If the input node name is not a valid debug node name.
  r!   z'Invalid prefix in debug node name: '%s'Nr      zInvalid debug node name: '%s'r   r   z,Invalid tensor name in debug node name: '%s')r   
ValueErrorlencountrindexr   index)r   prefixr   debug_opdebug_op_indexwatched_node_namewatched_output_slots          r   parse_debug_node_namer/   a   s*    &	$		 
>J
KK	c&kl	$	ZZ_q
4y@
AA$++c"Q&'((	t{{3	 $tDKK,q0123.	t{{3	 $	ZZ_
CiO
PP+DJJsO,DC1!4!567	/	IIr   c                       e Zd Zy)GraphTracingReachedDestinationN)__name__
__module____qualname__ r   r   r1   r1      s    r   r1   c                   .    e Zd ZdZ	 	 ddZd Zd Zd Zy)DFSGraphTracerz,Graph input tracer using depth-first search.Nc                 f    || _         || _        g | _        g | _        d| _        g | _        || _        y)a  Constructor of _DFSGraphTracer.

    Args:
      input_lists: A list of dicts. Each dict is an adjacency (input) map from
        the recipient node name as the key and the list of input node names
        as the value.
      skip_node_names: Optional: a list of node names to skip tracing.
      destination_node_name: Optional: destination node name. If not `None`, it
        should be the name of a destination not as a str and the graph tracing
        will raise GraphTracingReachedDestination as soon as the node has been
        reached.

    Raises:
      GraphTracingReachedDestination: if stop_at_node_name is not None and
        the specified node is reached.
    r   N)_input_lists_skip_node_names_inputs_visited_nodes_depth_count_depth_list_destination_node_name)selfinput_listsskip_node_namesdestination_node_names       r   __init__zDFSGraphTracer.__init__   s;    * $D+DDLDDD"7Dr   c                 $   | xj                   dz  c_         t        |      }|| j                  k(  r
t               || j                  v ry|| j
                  v ry| j
                  j                  |       | j                  D ]z  }||vr||   D ]k  }t        |      | j
                  v r| j                  j                  |       | j                  j                  | j                          | j                  |       m | | xj                   dz  c_         y)a  Trace inputs.

    Args:
      graph_element_name: Name of the node or an output tensor of the node, as a
        str.

    Raises:
      GraphTracingReachedDestination: if destination_node_name of this tracer
        object is not None and the specified node is reached.
    r   N)r=   r   r?   r1   r:   r<   appendr9   r;   r>   trace)r@   graph_element_namer   
input_listinps        r   rG   zDFSGraphTracer.trace   s    	01ID///*,,D)))D'''y)'' 
	*	$I& #!4!44
C  1 12

3 	r   c                     | j                   S r   )r;   r@   s    r   inputszDFSGraphTracer.inputs   s    <<r   c                     | j                   S r   )r>   rL   s    r   
depth_listzDFSGraphTracer.depth_list   s    r   )NN)r2   r3   r4   __doc__rD   rG   rM   rO   r5   r   r   r7   r7      s"    4  $%)8>"Hr   r7   c                     d}| j                   D ]  }|j                  s|j                  } n |t        j                  d       |S )z,Infer device name from a partition GraphDef.NzsFailed to infer device name from partition GraphDef: none of the nodes of the GraphDef has a non-empty device name.)nodedeviceloggingwarn)	graph_defdevice_namerR   s      r   _infer_device_namerX      sN    +nn d{{KKk LL	=> 
r   c                       e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zy)
DebugGraphz&Represents a debugger-decorated graph.Nc                    || _         d | _        i | _        i | _        i | _        i | _        i | _        i | _        i | _        i | _	        g | _
        i | _        || _        | j                  st        |      | _        |j                  D ]  }| j                  |        | j!                          | j#                          | j%                  | j'                                | j)                          y r   )_debug_graph_def_non_debug_graph_def_node_attributes_node_inputs_node_reversed_ref_inputs_node_ctrl_inputs_node_recipients_node_ctrl_recipients_node_devices_node_op_types_copy_send_nodes	_ref_args_device_namerX   rR   _process_debug_graph_node%_prune_non_control_edges_of_debug_ops!_prune_control_edges_of_debug_ops*_prune_nodes_from_input_and_recipient_maps_get_copy_nodes_populate_recipient_maps)r@   debug_graph_defrW   rR   s       r   rD   zDebugGraph.__init__   s    +D $DDD%'D"DD!#DDDDDN#D,_=d$$ +
$$T*+ 	..0**,33D4H4H4JK!!#r   c                    t        |j                        ry|j                  | j                  v r&t        d| j                  d|j                  d      |j
                  | j                  |j                  <   g | j                  |j                  <   g | j                  |j                  <   g | j                  |j                  <   g | j                  |j                  <   |j                  | j                  vr!t               | j                  |j                  <   | j                  |j                     j                  |j                  r|j                  n| j                         |j                  | j                  |j                  <   | j!                  |      | j"                  |j                  <   |j$                  D ]  }t'        |      rC|j                  dk(  s|j                  dk(  r%| j(                  j+                  |j                         |j-                  d      r.|dd }| j                  |j                     j+                  |       | j                  |j                     j+                  |        y)	zProcess a node from the debug GraphDef.

    Args:
      node: (NodeDef) A partition-graph node to be processed.

    Raises:
      ValueError: If duplicate node names are encountered.
    NzDuplicate node name on device z: ''_Send_Retval^r   )r"   r   r_   r%   rh   attrr^   ra   rb   rc   rd   setaddrS   opre   _get_ref_argsrg   inputr   rf   rF   r   )r@   rR   rJ   cinps       r   ri   z$DebugGraph._process_debug_graph_node  s    TYY yyD%%%))4996 7 7 (,yyD$))$#%Ddii (*D499%')D$))$,.Dtyy)yy***&)ed#tyy!%%{{(9(9;%)WWD		" $ 2 24 8DNN499zz 1	c	7 2dgg6J$$TYY/		12wtyy)006$))$++C01r   c                 
   t        j                  |j                        }|g S g }t        |j                        D ]E  \  }}|j
                  s|dk(  r|j                  nd|j                  |fz  }|j                  |       G |S )zDetermine whether an input of an op is ref-type.

    Args:
      node: A `NodeDef`.

    Returns:
      A list of the arg names (as strs) that are ref-type.
    r   z%s:%d)r   getrx   	enumerate
output_argis_refr   rF   )r@   rR   op_defref_argsir   arg_names          r   ry   zDebugGraph._get_ref_args3  s       )F~iH"6#4#45 ":			 !Q499W		1~-E!" Or   c                 f    g }| j                   D ]  }t        |      s|j                  |       ! |S )z(Find all Copy nodes in the loaded graph.)r_   r   rF   )r@   
copy_nodesrR   s      r   rm   zDebugGraph._get_copy_nodesG  s:    J!!  	d	$  r   c                     | j                   D ]G  }| j                   |   }t        |      D ](  \  }}t        |      s| j                   |   d   }|||<   * I y)zPrune (non-control) edges related to debug ops.

    Prune the Copy ops and associated _Send ops inserted by the debugger out
    from the non-control inputs and output recipients map. Replace the inputs
    and recipients with original ones.
    r   N)r_   r~   r   )r@   rR   rM   r   rJ   orig_inps         r   rj   z0DebugGraph._prune_non_control_edges_of_debug_opsO  si     !!   &ff% &!S &&s+A.(&)r   c                     | j                   D ]O  }| j                   |   }g }|D ]  }t        |      s|j                  |       ! |D ]  }|j                  |        Q y)z-Prune control edges related to the debug ops.N)ra   r"   rF   remove)r@   rR   ctrl_inputsdebug_op_inputsctrl_inpdebug_op_inps         r   rk   z,DebugGraph._prune_control_edges_of_debug_ops`  sp    && )**40ko! +("

 
 
*+ * ),<())r   c                 T   | j                   D ]  }| j                   |   }|D ]  }t        |      }|| j                  vrg | j                  |<   | j                  |   j                  |       || j                  v sX|| j
                  vrg | j
                  |<   | j
                  |   j                  |         | j                  D ]b  }| j                  |   }|D ]L  }|| j                  v r|| j                  vrg | j                  |<   | j                  |   j                  |       N d y)zPopulate the map from node name to recipient(s) of its output(s).

    This method also populates the input map based on reversed ref edges.
    N)	r_   r   rb   rF   rg   r`   ra   rf   rc   )r@   rR   rM   rJ   r   r   s         r   rn   z#DebugGraph._populate_recipient_mapsk  s:   
 !! ;  &f 	;#C d+++')$


$c"))$/$.. 66624D**3/

(
(
-
4
4T
:	;; && :**40k! :(t,,,
455513$
$
$X
.""8,33D9::r   c                 z    |D ]6  }| j                   |= | j                  |= | j                  |= | j                  |= 8 y)zPrune nodes out of input and recipient maps.

    Args:
      nodes_to_prune: (`list` of `str`) Names of the nodes to be pruned.
    N)r_   ra   rb   rc   )r@   nodes_to_prunerR   s      r   rl   z5DebugGraph._prune_nodes_from_input_and_recipient_maps  sK      +


D
!

 
 
&



%

$
$T
*	+r   c                 N   | j                   ryt        j                         | _         | j                  j                  D ]  }t        |j                        st        |j                        r.| j                   j                  j                         }|j                  |       |j                  dd= | j                  |j                     D ]  }|j                  j                  |        | j                  |j                     D ]   }|j                  j                  d|z          "  y)zReconstruct non-debug GraphDef.

    Non-debug GraphDef means the original GraphDef without the Copy* and Debug
    nodes inserted by the debugger.
    Nrt   )r]   r   GraphDefr\   rR   r   r   r"   rw   CopyFromrz   r_   rF   ra   )r@   rR   new_noderJ   r   s        r    _reconstruct_non_debug_graph_defz+DebugGraph._reconstruct_non_debug_graph_def  s        ) 2 2 4D%%** .	dii	 M$))$<**//335h
 ..
""499- ##c"#,,TYY7 .(cHn-..r   c                     | j                   S r   )rh   rL   s    r   rW   zDebugGraph.device_name      r   c                     | j                   S )z The debugger-decorated GraphDef.)r\   rL   s    r   ro   zDebugGraph.debug_graph_def  s        r   c                 :    | j                          | j                  S )zFThe GraphDef without the Copy* and Debug* nodes added by the debugger.)r   r]   rL   s    r   non_debug_graph_defzDebugGraph.non_debug_graph_def  s     	))+$$$r   c                     | j                   S r   )rd   rL   s    r   node_deviceszDebugGraph.node_devices  s    r   c                     | j                   S r   )re   rL   s    r   node_op_typeszDebugGraph.node_op_types  s    r   c                     | j                   S r   )r^   rL   s    r   node_attributeszDebugGraph.node_attributes         r   c                     | j                   S r   )r_   rL   s    r   node_inputszDebugGraph.node_inputs  r   r   c                     | j                   S r   )ra   rL   s    r   node_ctrl_inputszDebugGraph.node_ctrl_inputs  s    !!!r   c                     | j                   S r   )r`   rL   s    r   node_reversed_ref_inputsz#DebugGraph.node_reversed_ref_inputs  s    )))r   c                     | j                   S r   )rb   rL   s    r   node_recipientszDebugGraph.node_recipients  r   r   c                     | j                   S r   )rc   rL   s    r   node_ctrl_recipientszDebugGraph.node_ctrl_recipients  s    %%%r   r   )r2   r3   r4   rP   rD   ri   ry   rm   rj   rk   rn   rl   r   propertyrW   ro   r   r   r   r   r   r   r   r   r   r5   r   r   rZ   rZ      s   .$8)1V("	):8
+.4   ! ! % %
     ! !   " " * * ! ! & &r   rZ   c                 ,    t        |       j                  S )a  Reconstruct original (non-debugger-decorated) partition GraphDef.

  This method strips the input `tf.compat.v1.GraphDef` of the Copy* and
  Debug*-type nodes inserted by the debugger.

  The reconstructed partition graph is identical to the original (i.e.,
    non-debugger-decorated) partition graph except in the following respects:
      1) The exact names of the runtime-inserted internal nodes may differ.
         These include _Send, _Recv, _HostSend, _HostRecv, _Retval ops.
      2) As a consequence of 1, the nodes that receive input directly from such
         send- and recv-type ops will have different input names.
      3) The parallel_iteration attribute of while-loop Enter ops are set to 1.

  Args:
    debug_graph_def: The debugger-decorated `tf.compat.v1.GraphDef`, with the
      debugger-inserted Copy* and Debug* nodes.

  Returns:
    The reconstructed `tf.compat.v1.GraphDef` stripped of the debugger-inserted
    nodes.
  )rZ   r   )ro   s    r   reconstruct_non_debug_graph_defr     s    , 
O	$	8	88r   N)rP   tensorflow.core.frameworkr   tensorflow.python.frameworkr   tensorflow.python.platformr   rT   r   r   r   r   r"   r/   	Exceptionr1   objectr7   rX   rZ   r   r5   r   r   <module>r      sh    D / 7 <0
7 ) (&JRY JV JZq& q&h9r   