
    ijS5                         d Z ddlZddlZddlZddlZddlmZ ddlZddl	m
Z
 ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  e                                    Zej                  j                  ej                        ZdZdZd Zd Zd	 Zd
 Zd Zd Zd Z	 	 	 	 ddZ	 	 ddZ	 	 	 	 ddZy)zJClasses and functions that help to inspect Python source w.r.t. TF graphs.    N)app)	profilingz.py)z.pycz.pyoc                 z    t         j                  j                  t         j                  j                  |             S N)ospathnormpathabspath)	file_paths    }/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/tensorflow/python/debug/lib/source_utils.py_norm_abs_pathr   '   s$    			"''//)4	55    c                 r    t         j                  j                  |       \  }}|j                         t        v S r   )r   r   splitextlowerUNCOMPILED_SOURCE_SUFFIXESr   _	extensions      r   %is_extension_uncompiled_python_sourcer   +   s-    !!),,!Y		8	88r   c                 r    t         j                  j                  |       \  }}|j                         t        v S r   )r   r   r   r   COMPILED_SOURCE_SUFFIXESr   s      r   #is_extension_compiled_python_sourcer   0   s-    !!),,!Y		6	66r   c                 *    | d | j                  d       S )N:)rfind)	watch_keys    r   !_convert_watch_key_to_tensor_namer   5   s    	(IOOC(	))r   c                 F   t        |       st        |       syt        |       } | j                  t              xs | j                  t
              xrR | j                  d       xr> t        j                  j                  d      t        j                  j                  |       vS )a  Guess whether a Python source file is a part of the tensorflow library.

  Special cases:
    1) Returns False for unit-test files in the library (*_test.py),
    2) Returns False for files under python/debug/examples.

  Args:
    py_file_path: full path of the Python source file in question.

  Returns:
    (`bool`) Whether the file is inferred to be a part of the tensorflow
      library.
  Fz_test.pyz tensorflow/python/debug/examples)
r   r   r   
startswith_TENSORFLOW_BASEDIR_ABSL_BASEDIRendswithr   r   r	   )py_file_paths    r   guess_is_tensorflow_py_libraryr%   9   s     0
=
-l
;-,""#67 2""=1+##J/
/+ 77>?77L)*,r   c           	         t         j                  j                  |       rFt        | d      5 }|j	                         j                  d      }ddd       j                  d      }nt        |       }|t        d| z        t        t        j                  t        j                  t        |                        dz   }||fS # 1 sw Y   wxY w)a  Load the content of a Python source code file.

  This function covers the following case:
    1. source_file_path points to an existing Python (.py) file on the
       file system.
    2. source_file_path is a path within a .par file (i.e., a zip-compressed,
       self-contained Python executable).

  Args:
    source_file_path: Path to the Python source file to read.

  Returns:
    A length-2 tuple:
      - Lines of the source file, as a `list` of `str`s.
      - The width of the string needed to show the line number in the file.
        This is calculated based on the number of lines in the source file.

  Raises:
    IOError: if loading is unsuccessful.
  rbutf-8N
z?Source path neither exists nor can be loaded as a .par file: %s   )r   r   isfileopenreaddecodesplit_try_load_par_sourceIOErrorintnpceillog10len)source_file_pathfsource_textsource_linesline_num_widths        r   load_sourcer<   R   s    * WW^^$%		% -FFHOOG,k-$$T*L ((89L
K
  rwwrxxL(9:;<q@.	~	%%- -s    CCc                    | }	 t         j                  j                  |      \  }}|syt         j                  j                  t         j                  j	                  | |            }|j                  d      rt         j                  j                  |      rt        j                  |      5 }|j                         D cg c]!  }t         j                  j                  |      # }}||v rt|j                  |j                         |j                  |               5 }|j                         j                  d      }|j                  d      cddd       cddd       S ddd       ec c}w # 1 sw Y   xY w# 1 sw Y   xY w)a[  Try loading the source code inside a .par file.

  A .par file is a zip-compressed, self-contained Python executable.
  It contains the content of individual Python source files that can
  be read only through extracting from the zip file.

  Args:
    source_file_path: The full path to the file inside the .par file. This
      path should include the path to the .par file itself, followed by the
      intra-par path, e.g.,
      "/tmp/my_executable.par/org-tensorflow/tensorflow/python/foo/bar.py".

  Returns:
    If successful, lines of the source file as a `list` of `str`s.
    Else, `None`.
  )startz.parr(   r)   N)r   r   r/   r	   relpathr#   r+   zipfileZipFilenamelistr,   indexr-   r.   )	r7   prefix_pathbasenamesuffix_pathzname
norm_nameszfr9   s	            r   r0   r0   w   s9   " !+GGMM+6K''""
(<>KF#{(C??;' +19:Fbgg&&t,F
F*$vvajjl:#3#3K#@AB +b'')**73K$$T*+ ++ + + 	 G+ ++ +s6   ,E:?&E)%7E:0E.	E:)E:.E7	3E::Fc           
         | j                   }|st        d      t        |      }i }|j                         D ]  }t	        | j                  |j                              D ]  \  }	}
}}||
|k  s||
|k\  rt        |	      |k7  r&|r>| j                  |j                        }t        t        t        t        |                  }n|j                  g}|
|v r||
   j                  |       n|||
<   |s   |S )a  Annotate a Python source file with a list of ops created at each line.

  (The annotation doesn't change the source file itself.)

  Args:
    dump: (`DebugDumpDir`) A `DebugDumpDir` object of which the Python graph
      has been loaded.
    source_file_path: (`str`) Path to the source file being annotated.
    do_dumped_tensors: (`str`) Whether dumped Tensors, instead of ops are to be
      used to annotate the source file.
    file_stack_top: (`bool`) Whether only the top stack trace in the
      specified source file is to be annotated.
    min_line: (`None` or `int`) The 1-based line to start annotate the source
      file from (inclusive).
    max_line: (`None` or `int`) The 1-based line number to end the annotation
      at (exclusive).

  Returns:
    A `dict` mapping 1-based line number to a list of op name(s) created at
      that line, or tensor names if `do_dumped_tensors` is True.

  Raises:
    ValueError: If the dump object does not have a Python graph set.
  zUCannot perform source annotation due to a lack of set Python graph in the dump object)python_graph
ValueErrorr   get_operationsreversednode_tracebackrH   debug_watch_keyslistsetmapr   extend)dumpr7   do_dumped_tensorsfile_stack_topmin_linemax_linepy_graphline_to_op_namesopr   line_numberr   
watch_keysitems_to_appends                 r   annotate_sourcera      s   > (	
 7 8 8 $$45##% b(01D1DRWW1M(N $	;1

;#9

;(#:			"&6	6	**2773
5zBCE 77)	(	(%,,_=(7%	-2 
r   c                 X   | j                   }|st        d      t        j                  t              }t        j                  t              }i }i |rt        j                  |      nd}|rt        j                  |      nd}t	               }	|j                         D ]l  }
|r|j                  |
j                        s"| j                  |
j                        D ],  \  }}}}t        |      }||	v s2|r|j                  |      rt        j                  j                  |      s|	j                  |       \||   j                  |
j                         ||v r||   |kD  r|||<   n|||<   |
j                   D ]"  }|j                  }||   j                  |       $ | j#                  |
j                        }|D ]K  }|j%                  d      \  }}}|d|}|vs$t'        | j)                  |t+        |      |            |<   M / o i }|D ]  }t-        fd||   D              ||<    g }|D ]g  }|j/                  |t1        |      t'        |j3                  |i             t'        |j3                  |i             |j3                  |d      ||   f       i t5        |d       S )a@  Generate a list of source files with information regarding ops and tensors.

  Args:
    dump: (`DebugDumpDir`) A `DebugDumpDir` object of which the Python graph
      has been loaded.
    path_regex_allowlist: A regular-expression filter for source file path.
    node_name_regex_allowlist: A regular-expression filter for node names.

  Returns:
    A list of tuples regarding the Python source files involved in constructing
    the ops and tensors contained in `dump`. Each tuple is:
      (source_file_path, is_tf_library, num_nodes, num_tensors, num_dumps,
       first_line)

      is_tf_library: (`bool`) A guess of whether the file belongs to the
        TensorFlow Python library.
      num_nodes: How many nodes were created by lines of this source file.
        These include nodes with dumps and those without.
      num_tensors: How many Tensors were created by lines of this source file.
        These include Tensors with dumps and those without.
      num_dumps: How many debug Tensor dumps were from nodes (and Tensors)
        that were created by this source file.
      first_line: The first line number (1-based) that created any nodes or
        Tensors in this source file.

    The list is sorted by ascending order of source_file_path.

  Raises:
    ValueError: If the dump object does not have a Python graph set.
  zPCannot generate source list due to a lack of set Python graph in the dump objectNr   c              3   B   K   | ]  }j                  |d         yw)r   N)get).0tensor_nametensor_name_to_num_dumpss     r   	<genexpr>z1list_source_files_against_dump.<locals>.<genexpr>/  s&      "7 	!$$[!4"7s   r   c                     | d   S )Nr    )xs    r   <lambda>z0list_source_files_against_dump.<locals>.<lambda>=  s
    ad r   )key)rL   rM   collectionsdefaultdictrS   recompilerN   matchrH   rP   r   r   r   r+   addoutputsrQ   r/   r6   get_tensorsr2   sumappendr%   rd   sorted)rV   path_regex_allowlistnode_name_regex_allowlistr[   path_to_node_namespath_to_tensor_namespath_to_first_line
path_regexnode_name_regexto_skip_file_pathsr]   r   r^   r   output_tensorrf   r_   r   	node_nameoutput_slotdebug_oppath_to_num_dumpsr   outputrg   s                           @r   list_source_files_against_dumpr      s   D (	
 7 8 8 #..s3$005 +?bjj%&D  
# 	jj*+(,  u##% Gb44RWW=(,(;(;BGG(D G$	;1 +i
)
)
Z--i8ggnnY'y)#''0	(	(i(;6*5
Y
'(39%:: 9-#((Y'++K89 ((1j! G)+4??3+?(	;!*K86625y#k*:HE3G
";
/	G)G	G> " 7d! "7/5"7 7d7
 &% (i
MM&y1""9b12 $$Y34i+9%' (( 
N	++r   c                 N   t        |      }|rt        j                  |      nd}|rt        j                  |      nd}i }| D ]  }	|	j                  st        |	j                        |k7  r)||	j                  |k  s||	j                  |k\  rL|r&|j                  |	j                  j                        st|r|j                  |	j                        s|	j                  |vr#t        j                  |	      ||	j                  <   ||	j                     j                  |	        |S )a  Annotate a Python source file with profiling information at each line.

  (The annotation doesn't change the source file itself.)

  Args:
    profile_data: (`list` of `ProfileDatum`) A list of `ProfileDatum`.
    source_file_path: (`str`) Path to the source file being annotated.
    node_name_filter: Regular expression to filter by node name.
    op_type_filter: Regular expression to filter by op type.
    min_line: (`None` or `int`) The 1-based line to start annotate the source
      file from (inclusive).
    max_line: (`None` or `int`) The 1-based line number to end the annotation
      at (exclusive).

  Returns:
    A `dict` mapping 1-based line number to a the namedtuple
      `profiling.LineOrFuncProfileSummary`.
  N)r   rp   rq   r   r^   rr   node_exec_statsr   op_typer   AggregateProfilers   )
profile_datar7   node_name_filterop_type_filterrY   rZ   r   op_type_regexline_to_profile_summaryprofile_datums
             r   annotate_source_against_profiler   @  s!   2 $$454DBJJ/0$/0>"**^,D-# Lm""m--.2BB!:!:X!E!:!:h!F!!-"?"?"I"IJ]001F1FG  (??

$
$]
3 m778 m778<<]K-L0 
! r   )FFNN)NN)NNNN)__doc__rn   r   rp   r@   abslr   numpyr3   tensorflow.python.debug.libr   r   dirnamer	   r
   __file__r!   r"   r   r   r   r   r   r   r%   r<   r0   ra   r   r   rj   r   r   <module>r      s   Q  	 	    1 ggooGGOOBGGOOBGGOO
23%5 6 78  - $ + 69
7
*,2"&J+F ',#(!!@H 9==Ab,N 6:37-1-17!r   