
    BVh6<              	       T   d Z ddlZddlZddlZddlZddlZddlmZ dZde de dZ	de	 d	Z
 ej                  e
ej                        Z ej                  d
ddg      Zej                   j#                  ej                   j#                  ej                   j#                  e                  Zej                   j)                  ed      ej*                  z   ej                   j)                  ed      ej*                  z   ej                   j)                  ej                   j#                  e      dd      ej*                  z   gZ ej                  d      gZ	 ej1                   ej2                          ej4                         gz   D  cg c]+  } ej                   j)                  | d      ej*                  z   - c}         ej                  d      gZd ZddZddZ ddZ!ddZ"d Z#d Z$d Z%d Z&d Z'd Z(d Z)yc c} w # e$ r$ ej9                   ej                  d             Y iw xY w) zFunction for interpolating formatted errors from the TensorFlow runtime.

Exposes the function `interpolate` to interpolate messages with tags of the form
{{type name}}.
    N)tf_stackz[A-Za-z0-9_.][A-Za-z0-9_.\-/]*?z{{(?P<type>z) (?P<name>z)}}z(?P<sep>.*?)(?P<tag>)	_ParseTagtypenamepythoncontribpykeras	<embeddedz
_test\.py$c                    g }g }g }d}t        j                  t        |       D ]  }t        |j	                  d      |j	                  d            }|j
                  dk(  r2|j                  |j	                  d             |j                  |       n0|j                  |j	                                |j                  |       |j                         } |j                  | |d        dj                  |      ||fS )aq  Extract function tags and node tags from a message.

  Tags are named tuples representing the string {{type name}}. For example,
  in "123{{node Foo}}456{{function_node Bar}}789", there are two tags: a node
  tag and a function tag.

  Args:
    message: An error message, possibly from an OpError.

  Returns:
    A tuple containing the original message with function nodes stripped,
    function tags, and node tags.

    For example, if message is "123{{node Foo}}456{{function_node Bar}}789"
    then this function returns ("123{{node Foo}}456789",
    [_ParseTag("function_node", "Bar")], [_ParseTag("node", "Foo")]).
  r   r   r   function_nodesepN )	refinditer_INTERPOLATION_PATTERNr   groupr   appendendjoin)messageerror_message	func_tags	node_tagsposmatch
parsed_tags          _/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/framework/error_interpolation.pyparse_messager    Q   s    $ -))	#{{17; e5;;v.F0CDJ/)5;;u-.z"5;;=)z"
))+C wst}%		I	55    c                 2   |sd}|| z  }||z   S g }|j                  |d| d       |D ][  }dj                  |j                  |j                        }|d|j                  |d}|j                   dj                  d
i |       ] d	j                  |      S )a  Return a summary of an op's device function stack.

  Args:
    name: The name of the op.
    device_assignment_list: The op._device_assignments list.
    prefix:  An optional string prefix used before each line of the multi-
        line string returned by this function.

  Returns:
    A multi-line string similar to:
        Device assignments active during op 'foo' creation:
          with tf.device(/cpu:0): <test_1.py:27>
          with tf.device(some_func<foo.py, 123>): <test_2.py:38>
    The first line will have no padding to its left by default.  Subsequent
    lines will have two spaces of left-padding.  Use the prefix argument
    to increase indentation.
  z:No device assignments were active during op '%s' creation.z%Device assignments active during op '' creation:<{file}:{line}>fileline  )prefixindentdev_namelocz1{prefix}{indent}with tf.device({dev_name}): {loc}
 )r   formatfilenamelinenoobjr   )r   device_assignment_listr)   r   str_listtraceable_objlocation_summarysubss           r   !_compute_device_summary_from_listr8   t   s    $ 
 JGtOGG(
//@FMO . 
Lm(//##-*>*> 0 @ !%%	D OOB;BBJTJL
L 
8	r!   c                 D    t        | j                  | j                  |      S )N)r8   r   _device_assignmentsopr)   s     r   *_compute_device_assignment_summary_from_opr=      s     	*277B4J4J+1
3 3r!   c                 @   |sd}|| z  }||z   S g }|j                  |d| d       |j                         D ]T  \  }}dj                  |j                  |j                        }|d||d}|j                   dj                  d
i |       V d	j                  |      S )a  Return a summary of an op's colocation stack.

  Args:
    name: The op name.
    colocation_dict: The op._colocation_dict.
    prefix:  An optional string prefix used before each line of the multi-
        line string returned by this function.

  Returns:
    A multi-line string similar to:
        Node-device colocations active during op creation:
          with tf.compat.v1.colocate_with(test_node_1): <test_1.py:27>
          with tf.compat.v1.colocate_with(test_node_2): <test_2.py:38>
    The first line will have no padding to its left by default.  Subsequent
    lines will have two spaces of left-padding.  Use the prefix argument
    to increase indentation.
  z?No node-device colocations were active during op '%s' creation.z*Node-device colocations active during op 'r#   r$   r%   r(   )r)   r*   r   r,   z4{prefix}{indent}with tf.colocate_with({name}): {loc}r-   r.   )r   itemsr/   r0   r1   r   )	r   colocation_dictr)   r   r4   
coloc_namelocationr6   r7   s	            r   %_compute_colocation_summary_from_dictrC      s    $ 
OGtOGG(
//4! " .335 
Oj((//X__ 0 6 	D OOE>EEMMO
O 
8	r!   c                 D    t        | j                  | j                  |      S )zEFetch colocation file, line, and nesting and return a summary string.)rC   r   _colocation_dictr;   s     r   #_compute_colocation_summary_from_oprF      s"     
/rww8K8K/5
7 7r!   c                     t         D ]  }|j                  |       s y t        D ]  }|j                  |       s y t        D ]  }| j	                  |      s y y)a  Returns whether a filename should be considered a part of the framework.

  A file is part of the framework if it does not match a pattern in
  _EXTERNAL_FILENAME_PATTERNS and it either matches a pattern in
  _FRAMEWORK_FILENAME_PATTERNS or starts with a _FRAMEWORK_PATH_PREFIXES prefix.

  Args:
    filename: A filename string.

  Returns:
    Whether the filename should be considered to be internal to the
    TensorFlow framework for the purposes of reporting errors.
  FT)_EXTERNAL_FILENAME_PATTERNSsearch_FRAMEWORK_FILENAME_PATTERNS_FRAMEWORK_PATH_PREFIXES
startswith)r0   patternr)   s      r   _is_framework_filenamerN      si     - g~~h . g~~h ) f6" 
r!   c                     t        |       }| D cg c]  }|j                   }}t        t        |            D ]  \  }}t	        |      }|r||z
  dz
  c S  yc c}w )aN  Return index in op.traceback with first 'useful' frame.

  This method reads through the stack stored in op.traceback looking for the
  innermost frame which (hopefully) belongs to the caller.  It accomplishes this
  by rejecting frames deemed to be part of the TensorFlow framework (by
  pattern matching the filename).

  Args:
    tb: A list of traceback frames (as from Operation.traceback).

  Returns:
    Integer index into op.traceback where the first non-TF file was found
    (innermost to outermost), or 0 (for the outermost stack frame) if all files
    came from TensorFlow.
     r   )lenr0   	enumeratereversedrN   )tbsizeframe	filenamesidxr0   is_frameworks          r   _find_index_of_defining_framerZ      sf    " 
R$+-.%u~~.). )!45 mc8)(3LCZ!^	
 
 /s   Ac                 v    t        |       }t        |dz   dz   t        |             }t        ||z
  d      }| || S )aw  Return a list of frames, which form a 'useful' stack.

  Starting from the defining frame to the outermost one, this method computes
  the contiguous portion of the 'useful' stack trace and returns the selected
  frames.

  Args:
    tb: A list of traceback frames (as from Operation.traceback).
    num: total number of frames to return.

  Returns:
    A list of frames.
     rP   r   )rZ   minrQ   max)rT   numdefining_frame_indexinnermost_excludedoutermost_includeds        r   _compute_useful_framesrc     sO     7r:
 /!3a7RA-3Q7	1	22r!   c           	          t        j                         }| D ]C  \  }}|j                  |j                  ||j                  t        |j                  d             E |j                         S )a  Construct and returns a `GraphDebugInfo` protocol buffer.

  Args:
    func_named_operations: An iterable of (func_name, op.Operation) tuples
      where the Operation instances have a _traceback members. The func_name
      should be the empty string for operations in the top-level Graph.

  Returns:
    GraphDebugInfo protocol buffer.

  Raises:
    TypeError: If the arguments are not of the correct proto buffer type.
  
   )r   GraphDebugInfoBuilder	tracebackAccumulateStackTracer   rc   Build)func_named_operationsbuilder	func_namer<   s       r   create_graph_debug_info_defrm   '  se     **,', mi	||  27722<<D 
r!   c           	         t        |       }t        |       }dj                  ||g      }| j                  d}d}d}d}d}ns| j                  j	                         }	|	j
                  }t        j                  | j                  j                               }|	j                  }|	j                  }| d|d}||||||||d}
|
S )a%  Return a dictionary mapping interpolation tokens to values.

  Args:
    op: op.Operation object.

  Returns:
    A dictionary mapping string tokens to string values.  The keys are shown
    below along with example values.
    {
      "file": "tool_utils.py",
      "lineno": "124",
      "line": "  source code line",
      "defined_at": " (defined at tool_utils.py:124)",
      "colocations":
          '''Node-device colocations active during op creation:
               with tf.compat.v1.colocate_with(test_node_1): <test_1.py:27>
               with tf.compat.v1.colocate_with(test_node_2): <test_2.py:38>'''
      "devices":
          '''Device assignments active during op 'foo' creation:
               with tf.device(/cpu:0): <test_1.py:27>
               with tf.device(some_func<foo.py, 123>): <test_2.py:38>'''
      "devs_and_colocs": A concatenation of colocations and devices, e.g.
          '''Node-device colocations active during op creation:
               with tf.compat.v1.colocate_with(test_node_1): <test_1.py:27>
               with tf.compat.v1.colocate_with(test_node_2): <test_2.py:38>'''
             Device assignments active during op 'foo' creation:
               with tf.device(/cpu:0): <test_1.py:27>
               with tf.device(some_func<foo.py, 123>): <test_2.py:38>'''
    }
  r-   z	<unknown>r   r   :d)colocationsdevicesdevs_and_colocs
defined_atr&   r1   r'   definition_traceback)
rF   r=   r   rg   last_user_framer0   format_listget_user_framesr1   r'   )r<   colocation_summarydevice_summarycombined_summaryr0   ru   r1   r'   rt   rV   
field_dicts              r   _compute_field_dictr}   @  s    B ;2>=bA.YY 2NCD\\ HFDJLL((*E~~H$001M1M1OP\\F::D:Qvaj)J ()2	* 
r!   c                 D   d| j                   dg}t        |       }|d   D ]I  }d|vs|j                  |j                  d      D cg c]  }|j	                         sd|  c}       K |j                  d| j                          dj                  |      S c c}w )zReturns the formatted error message for the given op.

  Args:
    op: The node.

  Returns:
    The formatted error message for the given op with traceback.
  zDetected at node z$ defined at (most recent call last):ru   r   r-   r(   zNode: )r   r}   extendsplitstripr   r   )r<   node_error_messager|   rV   r'   s        r   _build_node_error_messager     s     	"''$HI #2&* 01 Ge%#(;;t#4
E4

Rv;
EGG fRWWK01	%	&& Fs   B
B
c                     t        |       \  }}}ddg}|D ]8  }	 |j                  |j                        }|j                  t	        |             : |j                  |j                                dj                  |      S # t
        $ r Y vw xY w)a  Interpolates an error message.

  The error message can contain tags of form `{{node_type node_name}}`
  which will be parsed to identify the tf.Graph and op. If the op contains
  traceback, the traceback will be attached to the error message.

  Args:
    message: A string to interpolate.
    graph: ops.Graph object containing all nodes referenced in the error
        message.

  Returns:
    The error message string with node definition traceback.
  zGraph execution error:r   r-   )r    get_operation_by_namer   r   r   KeyErrorr   r   )r   graphparsed_messaged_r   r   tagr<   s           r   interpolate_graphr     s     #0"8/1i+R0- :c:&&sxx0b 4R89: ,,./	=	!!  s   B	BB)r   )*__doc__collectionsosr   siterg   tensorflow.python.utilr   _NAME_REGEX
_TAG_REGEX_INTERPOLATION_REGEXcompileDOTALLr   
namedtupler   pathdirname__file___FRAMEWORK_COMMON_PREFIXr   r   rK   rJ   r   getsitepackagesgetusersitepackagesAttributeErrorr   rH   r    r8   r=   rC   rF   rN   rZ   rc   rm   r}   r   r   )package_paths   0r   <module>r      s    	 	   +0k]+k]%H
.zl!< #$8"))D "K"";0@A	 77??GGOOBGGOOH-.0  GGLL)84rvv=GGLL)95>GGLL!9:w "$&&)  BJJ|  <!!.$..04LD4L4L4N3OO#
 	ggll<)BFF2#  BJJ}  6F'T3'T74<302?D'4"y
#  < %%jbjj&:;<s$   1G> 0G96G> 9G> >&H'&H'