
    BVh                         d Z ddlZddlZddlmZ  ej                  d      Z ej                  d      Z ej                  d      Z	dZ
d Z G d	 d
e      Zy)zJLibrary for arbitrary expression evaluation based on a debugger data dump.    N)
debug_dataz`.*?`z0/job:(\w)+/replica:(\d)+/task:(\d)+/(\w)+:(\d)+:z
\[(\d)*\]$DebugIdentityc                    t        j                  t        |       }|r8| |j                         |j	                         dz
   }| |j	                         d } nd}| j                  d      }t        |      dvrt        d| z        t        j                  t        |d         }|rMt        |d   |j                         dz   |j	                         dz
         }|d   d|j                          |d<   nd}t        |      dk(  r|d   }t        |d         }t        }n)| j                  d      }|d   }t        |d         }|d   }|||||fS )	a}  Parse a debug tensor name in a to-be-evaluated expression.

  Args:
    debug_tensor_name: name of the debug tensor, with or without
      device name as a prefix, with or without debug op, with or
      without '[<exec_index>]' as a suffix.
      E.g., without device name prefix, without debug op suffix:
        "hidden_0/MatMul:0"
      E.g., with device name prefix:
        "/job:worker/replica:0/task:1/gpu:0:hidden_0/MatMul:0"
      E.g., with debug op suffix:
        "hidden_0/MatMul:0:DebugNumericSummary"
      E.g., with device name prefix and debug op suffix:
        "/job:worker/replica:0/task:1/gpu:0:hidden_0/MatMul:0:DebugNumericSummary"
      E.g., with device name prefix, debug op and an exec index:
        "/job:worker/replica:0/task:1/gpu:0:hidden_0/MatMul:0:DebugNumericSummary[1]"

  Returns:
    device_name: If device name prefix exists, the device name; otherwise,
      `None`.
    node_name: Name of the node.
    output_slot: Output slot index as an `int`.
    debug_op: If the debug op suffix exists, the debug op name; otherwise,
      `None`.
    exec_index: Execution index (applicable to cases in which a debug tensor
      is computed multiple times in a `tf.Session.run` call, e.g., due to
      `tf.while_loop`). If the exec_index suffix does not exist, this value
      defaults to `0`.

  Raises:
    ValueError: If the input `debug_tensor_name` is malformed.
     N:)      zJThe debug tensor name in the to-be-evaluated expression is malformed: '%s'r   r   )rematch_DEVICE_NAME_PREFIX_PATTERNstartendsplitlen
ValueErrorsearch_EXEC_INDEX_SUFFIX_PATTERNint_DEFAULT_DEBUG_OP)	debug_tensor_namedevice_prefix_matchdevice_namesplit_itemsexec_index_match
exec_index	node_nameoutput_slotdebug_ops	            U/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/debug/cli/evaluator.py_parse_debug_tensor_namer!      sy   F !<>OP#!!#&9&=&=&?!&CEK)*=*A*A*C*DEK!'',+V#
	"	#$ $ YY9;r?K[_ 1$'7';';'='AC DJ!"o&?'7'='='?@KOJAIk!n%K H#))#.KAIk!n%K1~H	ih
	BB    c                       e Zd ZdZd Zd Zy)ExpressionEvaluatorzCEvaluates Python expressions using debug tensor values from a dump.c                      || _         i | _        y)z^Constructor of ExpressionEvaluator.

    Args:
      dump: an instance of `DebugDumpDir`.
    N)_dump_cached_tensor_values)selfdumps     r    __init__zExpressionEvaluator.__init__i   s     DJ!#Dr"   c                    t        j                  t        |      }|}t        t	        |            D ]  }|j                  d      dd j                         }t        |      \  }}}}	}
|| j                  vr2	 | j                  j                  |||	|      |
   }|| j                  |<   |d|j                  d       dz   |z   dz   ||j                  d      d z   } t!        |      S # t        j                  $ r t        d||fz        w xY w)	a  Parse an expression.

    Args:
      expression: the expression to be parsed.

    Returns:
      The result of the evaluation.

    Raises:
      ValueError: If the value of one or more of the debug tensors in the
        expression are not available.
    r   r   r
   )r   zEEval failed due to the value of %s:%d:DebugIdentity being unavailableNzself._cached_tensor_values['z'])r   finditer_DUMP_TENSOR_PATTERNreversedlistgroupstripr!   r'   r&   get_tensorsr   'WatchKeyDoesNotExistInDebugDumpDirErrorr   r   r   eval)r(   
expressiondump_tensors_iterrewritten_expressionr   tensor_namer   r   r   r   r   values               r    evaluatezExpressionEvaluator.evaluater   sE    $8*E%$012 /KKN1R(..0k
";
/ @k9k8Z	D66	6	8**((h% ) ''13% 38"";/
A
/
()+679=>
uyy|}
-. /& $%% AA 	8(+678 8	8s   6"C&&$D
N)__name__
__module____qualname____doc__r*   r:    r"   r    r$   r$   f   s    K$"&r"   r$   )r>   r   numpynptensorflow.python.debug.libr   compiler-   r   r   r   r!   objectr$   r?   r"   r    <module>rE      sd    Q 	  2!rzz(+ (bjj79 'RZZ6 # ECP.&& .&r"   