B
    )`_                 @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlm	Z	 e
dZe
dZe
d	Zd
Zdd ZG dd deZdS )zJLibrary for arbitrary expression evaluation based on a debugger data dump.    )absolute_import)division)print_functionN)
debug_dataz`.*?`z0/job:(\w)+/replica:(\d)+/task:(\d)+/(\w)+:(\d)+:z
\[(\d)*\]$ZDebugIdentityc       	      C   s  t t| }|r:| | | d  }| | d } nd}| d}t|dkr`td|  t t	|d }|rt
|d | d | d  }|d d|  |d< nd}t|dkr|d }t
|d }t}n&| 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)	Zdebug_tensor_nameZdevice_prefix_matchdevice_nameZsplit_itemsZexec_index_match
exec_index	node_nameoutput_slotdebug_op r   T/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow/python/debug/cli/evaluator.py_parse_debug_tensor_name"   s4    #

r   c               @   s    e Zd ZdZdd Zdd ZdS )ExpressionEvaluatorzCEvaluates Python expressions using debug tensor values from a dump.c             C   s   || _ i | _dS )z^Constructor of ExpressionEvaluator.

    Args:
      dump: an instance of `DebugDumpDir`.
    N)_dump_cached_tensor_values)selfdumpr   r   r   __init__m   s    zExpressionEvaluator.__init__c          	   C   s   t t|}|}xtt|D ]}|ddd  }t|\}}}}	}
|| jkry| j	j
|||	|d|
 }W n& tjk
r   td||f Y nX || j|< |d|d d | d ||dd  }qW t|S )	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    Zget_tensorsr   Z'WatchKeyDoesNotExistInDebugDumpDirErrorr   r   r   eval)r"   Z
expressionZdump_tensors_iterZrewritten_expressionr   Ztensor_namer   r   r   r   r   valuer   r   r   evaluatev   s$    

zExpressionEvaluator.evaluateN)__name__
__module____qualname____doc__r$   r-   r   r   r   r   r   j   s   	r   )r1   
__future__r   r   r   r   numpynpZtensorflow.python.debug.libr   compiler&   r   r   r   r   objectr   r   r   r   r   <module>   s   

H