o
    ?e9                     @   s*  d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddl
mZ ddlmZ dd	lmZ dd
lmZ dae ZdadZG dd deZG dd deZeddd"ddZedddd Zedddd Zedddd Zedddd Z eddG d d! d!e!Z"dS )#a!  TensorFlow 2.0 Profiler for both Eager Mode and Graph Mode.

The profiler has two mode:
- Programmatic Mode: start(), stop() and Profiler class. It will perform
                    when calling start() or create Profiler class and will stop
                    when calling stop() or destroying Profiler class.
- On-demand Mode: start_profiler_server(). It will perform profiling when
                  receive profiling request.

NOTE: Only one active profiler session is allowed. Use of simultaneous
Programmatic Mode and On-demand Mode is undefined and will likely fail.

NOTE: The Keras TensorBoard callback will automatically perform sampled
profiling. Before enabling customized profiling, set the callback flag
"profile_batches=[]" to disable automatic sampled profiling.
customized profiling.
    N)_pywrap_events_writer)context)errors)gfile)
tf_logging)_pywrap_profiler)compat)
deprecatedz.profile-emptyc                   @      e Zd ZdS )ProfilerAlreadyRunningErrorN__name__
__module____qualname__ r   r   a/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/eager/profiler.pyr   6       r   c                   @   r
   )ProfilerNotRunningErrorNr   r   r   r   r   r   :   r   r   z
2020-07-01z-use `tf.profiler.experimental.start` instead.c              	   C   s   t ? tdurtdtjtjkrt  t azt	d| dur#| ni  W n t
jy8   td tdw W d   dS 1 sDw   Y  dS )zStart profiling.

  Args:
    options: profiler options.

  Raises:
    ProfilerAlreadyRunningError: If another profiling session is running.
  NzAnother profiler is running. zAnother profiler session is running which is probably created by profiler server. Please avoid using profiler server and profiler APIs at the same time.)_profiler_lock	_profilerr   r   default_execution_mode
EAGER_MODEensure_initializedr   ZProfilerSessionstartr   ZAlreadyExistsErrorloggingwarning)optionsr   r   r   r   >   s   
"r   z,use `tf.profiler.experimental.stop` instead.c                  C   sh   t ( tdu rtdtjtjkrt j  t } dat	d7 a	W d   | S 1 s-w   Y  | S )a  Stop current profiling session and return its result.

  Returns:
    A binary string of tensorflow.tpu.Trace. User can write the string
    to file for offline analysis by tensorboard.

  Raises:
    ProfilerNotRunningError: If there is no active profiling session.
  Nz.Cannot stop profiling. No profiler is running.   )
r   r   r   r   r   r   executorwaitstop_run_num)resultr   r   r   r!   X   s   

		r!   zE`tf.python.eager.profiler` has deprecated, use `tf.profiler` instead.c                 C   sN   t | D ]
}|tr dS qtttj	
| d}|tt dS )zCreate an empty event file if not already exists.

  This event file indicates that we have a plugins/profile/ directory in the
  current logdir.

  Args:
    logdir: log directory.
  Nevents)r   ZListDirectoryendswith_EVENT_FILE_SUFFIXr   ZEventsWriterr   as_bytesospathjoinZInitWithSuffix)logdir	file_nameZevent_writerr   r   r   maybe_create_event_fileq   s   
r-   c                 C   sv   t j| ddtj d}t| t|  t	t j|dd}|
| W d   dS 1 s4w   Y  dS )zSave profile result to TensorBoard logdir.

  Args:
    logdir: log directory read by TensorBoard.
    result: profiling result returned by stop().
  pluginsZprofilez%Y-%m-%d_%H-%M-%Szlocal.tracewbN)r(   r)   r*   datetimenowstrftimer   ZMakeDirsr-   ZOpenwrite)r+   r#   Z
plugin_dirfr   r   r   save   s   
"r5   z,use `tf.profiler.experimental.server.start`.c                 C   s"   t jt jkr
t   t|  dS )a  Start a profiler grpc server that listens to given port.

  The profiler server will keep the program running even the training finishes.
  Please shutdown the server with CTRL-C. It can be used in both eager mode and
  graph mode. The service defined in
  tensorflow/core/profiler/profiler_service.proto. Please use
  tensorflow/contrib/tpu/profiler/capture_tpu_profile to capture tracable
  file following https://cloud.google.com/tpu/docs/cloud-tpu-tools#capture_trace

  Args:
    port: port profiler server listens to.
  N)r   r   r   r   r   start_server)portr   r   r   start_profiler_server   s   r8   z/use `tf.profiler.experimental.Profile` instead.c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	ProfilerzContext-manager eager profiler api.

  Example usage:
  ```python
  with Profiler("/path/to/logdir"):
    # do some work
  ```
  c                 C   s
   || _ d S N)_logdir)selfr+   r   r   r   __init__      
zProfiler.__init__c                 C   s
   t   d S r:   )r   )r<   r   r   r   	__enter__   r>   zProfiler.__enter__c                 C   s   t  }t| j| d S r:   )r!   r5   r;   )r<   typvaluetbr#   r   r   r   __exit__   s   zProfiler.__exit__N)r   r   r   __doc__r=   r?   rC   r   r   r   r   r9      s
    	r9   r:   )#rD   r0   r(   	threadingZtensorflow.python.clientr   Ztensorflow.python.eagerr   Ztensorflow.python.frameworkr   Ztensorflow.python.platformr   r   r   Z#tensorflow.python.profiler.internalr   Ztensorflow.python.utilr   Z"tensorflow.python.util.deprecationr	   r   Lockr   r"   r&   	Exceptionr   r   r   r!   r-   r5   r8   objectr9   r   r   r   r   <module>   sH   



