
    BVh                     H    d Z ddlmZ ddlmZ ddlmZ d Z G d de      Z	y)	zMemory leak detection utility.    )_PythonMemoryChecker)trace)
tf_inspectc                      t        j                         D ]I  } | d   }|j                  d      s	 | d   j                  d   j                  j
                  }|dz   |z   c S  y#  Y QxY w)z>If available, return the current test name. Otherwise, `None`.   testr   self.N)r   stack
startswithf_locals	__class____name__)r   function_name
class_names      Z/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/framework/memory_checker.py_get_test_name_best_effortr      su    ! e!HM'1X&&v.88AA
C-// 
s   -A""A&c                       e Zd ZdZej
                  d        Zej
                  d        Zd Zej
                  d        Z	ej
                  d        Z
ej
                  d	d       Zy)
MemoryCheckera  Memory leak detection class.

  This is a utility class to detect Python and C++ memory leaks. It's intended
  for both testing and debugging. Basic usage:

  >>> # MemoryChecker() context manager tracks memory status inside its scope.
  >>> with MemoryChecker() as memory_checker:
  >>>   tensors = []
  >>>   for _ in range(10):
  >>>     # Simulating `tf.constant(1)` object leak every iteration.
  >>>     tensors.append(tf.constant(1))
  >>>
  >>>     # Take a memory snapshot for later analysis.
  >>>     memory_checker.record_snapshot()
  >>>
  >>> # `report()` generates a html graph file showing allocations over
  >>> # snapshots per every stack trace.
  >>> memory_checker.report()
  >>>
  >>> # This assertion will detect `tf.constant(1)` object leak.
  >>> memory_checker.assert_no_leak_if_all_possibly_except_one()

  `record_snapshot()` must be called once every iteration at the same location.
  This is because the detection algorithm relies on the assumption that if there
  is a leak, it's happening similarly on every snapshot.
  c                 $    t               | _        | S N)r   _python_memory_checkerr	   s    r   	__enter__zMemoryChecker.__enter__A   s    "6"8DK    c                      y r    )r	   exc_type	exc_value	tracebacks       r   __exit__zMemoryChecker.__exit__F   s    r   c                 8    | j                   j                          y)a  Take a memory snapshot for later analysis.

    `record_snapshot()` must be called once every iteration at the same
    location. This is because the detection algorithm relies on the assumption
    that if there is a leak, it's happening similarly on every snapshot.

    The recommended number of `record_snapshot()` call depends on the testing
    code complexity and the allcoation pattern.
    N)r   record_snapshotr   s    r   r#   zMemoryChecker.record_snapshotL   s     	//1r   c                 8    | j                   j                          y)a  Generates a html graph file showing allocations over snapshots.

    It create a temporary directory and put all the output files there.
    If this is running under Google internal testing infra, it will use the
    directory provided the infra instead.
    N)r   reportr   s    r   r%   zMemoryChecker.reportX   s     	&&(r   c                 8    | j                   j                          y)zRaises an exception if a leak is detected.

    This algorithm classifies a series of allocations as a leak if it's the same
    type(Python) or it happens at the same stack trace(C++) at every snapshot,
    but possibly except one snapshot.
    N)r   )assert_no_leak_if_all_possibly_except_oner   s    r   r'   z7MemoryChecker.assert_no_leak_if_all_possibly_except_oneb   s     	IIKr   Nc                 <    | j                   j                  |       y)aN  Raises an exception if there are new Python objects created.

    It computes the number of new Python objects per type using the first and
    the last snapshots.

    Args:
      threshold: A dictionary of [Type name string], [count] pair. It won't
        raise an exception if the new Python objects are under this threshold.
    )	thresholdN)r   assert_no_new_objects)r	   r)   s     r   assert_no_new_python_objectsz*MemoryChecker.assert_no_new_python_objectsm   s     	55	5Jr   r   )r   
__module____qualname____doc__r   trace_wrapperr   r!   r#   r%   r'   r+   r   r   r   r   r   %   s    6 	  		 	

2 	) ) 	L L 	
K 
Kr   r   N)
r.   1tensorflow.python.framework.python_memory_checkerr   tensorflow.python.profilerr   tensorflow.python.utilr   r   objectr   r   r   r   <module>r4      s(    % R , -SKF SKr   