a
    /Sic9                     @   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                   @   s   e Zd ZdS )ProfilerAlreadyRunningErrorN__name__
__module____qualname__ r   r   \/var/www/html/django/DPS/env/lib/python3.9/site-packages/tensorflow/python/eager/profiler.pyr
   6   s   r
   c                   @   s   e Zd ZdS )ProfilerNotRunningErrorNr   r   r   r   r   r   :   s   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rF| ni  W n& t
jyt   td tdY n0 W d   n1 s0    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   AlreadyExistsErrorloggingwarning)optionsr   r   r   r   >   s    
r   z,use `tf.profiler.experimental.stop` instead.c                  C   sf   t N tdu rtdtjtjkr0t j  t } dat	d7 a	W d   n1 sX0    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   ListDirectoryendswith_EVENT_FILE_SUFFIXr   EventsWriterr   as_bytesospathjoinInitWithSuffix)logdir	file_nameevent_writerr   r   r   maybe_create_event_fileq   s    
r0   c                 C   st   t j| ddtj d}t| t|  t	t j|dd}|
| W d   n1 sf0    Y  dS )zSave profile result to TensorBoard logdir.

  Args:
    logdir: log directory read by TensorBoard.
    result: profiling result returned by stop().
  pluginsprofilez%Y-%m-%d_%H-%M-%Szlocal.tracewbN)r)   r*   r+   datetimenowstrftimer   MakeDirsr0   Openwrite)r-   r"   
plugin_dirfr   r   r   save   s    
r<   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    r?   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__   s    zProfiler.__init__c                 C   s
   t   d S rA   )r   )rC   r   r   r   	__enter__   s    zProfiler.__enter__c                 C   s   t  }t| j| d S rA   )r    r<   rB   )rC   typvaluetbr"   r   r   r   __exit__   s    zProfiler.__exit__N)r   r   r   __doc__rD   rE   rI   r   r   r   r   r@      s   	r@   )N)#rJ   r4   r)   	threadingtensorflow.python.clientr   tensorflow.python.eagerr   tensorflow.python.frameworkr   tensorflow.python.platformr   r   r   #tensorflow.python.profiler.internalr   tensorflow.python.utilr   "tensorflow.python.util.deprecationr	   r   Lockr   r!   r&   	Exceptionr
   r   r   r    r0   r<   r?   objectr@   r   r   r   r   <module>   sF   



