
    BVh9                        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j*                         ZdadZ G d de      Z G d de      Z edd      dd       Z edd      d        Z edd      d        Z edd      d        Z edd      d        Z  edd       G d de!             Z"y)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y)ProfilerAlreadyRunningErrorN__name__
__module____qualname__     P/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/eager/profiler.pyr   r   6       r   r   c                       e Zd Zy)ProfilerNotRunningErrorNr   r   r   r   r   r   :   r   r   r   z
2020-07-01z-use `tf.profiler.experimental.start` instead.c                    t         5  t        t        d      t        j                  t        j
                  k(  rt        j                          t        j                         a	 t        j                  d| | ni        	 ddd       y# t        j                  $ r! t        j                  d       t        d      w xY w# 1 sw Y   yxY w)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   ProfilerSessionstartr   AlreadyExistsErrorloggingwarning)optionss    r   r   r   >   s      H'(FGG%%););;  " 002IHoobW%8'bAH H $$ Hoo C D ((FGG	HH Hs   AB>#B4B;;B>>Cz,use `tf.profiler.experimental.stop` instead.c                  8   t         5  t        t        d      t        j                  t        j
                  k(  r,t        j                         j                  j                          t        j                         } dat        dz  a	ddd       | S # 1 sw Y    S xY w)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)results    r   r(   r(   X   s      #
:< <%%););;oo  %%'^^FIMH 
- 
-s   A>BBzE`tf.python.eager.profiler` has deprecated, use `tf.profiler` instead.c                 B   t        j                  |       D ]  }|j                  t              s y t	        j
                  t        j                  t        j                  j                  | d                  }|j                  t        j                  t                     y)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_writers      r   maybe_create_event_filer9   q   su     &&v. i,- '33oobggll68457,foo.@ABr   c                    t         j                  j                  | ddt        j                  j	                         j                  d            }t        j                  |       t        |        t        j                  t         j                  j                  |d      d      5 }|j                  |       ddd       y# 1 sw Y   yxY w)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)r2   r3   r4   datetimenowstrftimer   MakeDirsr9   Openwrite)r6   r*   
plugin_dirfs       r   saverF      s     ww||i&&':;=* ..&!zz"'',,z=94@ AGGFO  s   "B==Cz,use `tf.profiler.experimental.server.start`.c                     t         j                  t         j                  k(  rt        j                          t	        j
                  |        y)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)ports    r   start_profiler_serverrJ      s2     ##w'9'99 %r   z/use `tf.profiler.experimental.Profile` instead.c                   "    e Zd ZdZd Zd Zd Zy)ProfilerzContext-manager eager profiler api.

  Example usage:
  ```python
  with Profiler("/path/to/logdir"):
    # do some work
  ```
  c                     || _         y N)_logdir)selfr6   s     r   __init__zProfiler.__init__   s	    DLr   c                     t                y rN   )r   )rP   s    r   	__enter__zProfiler.__enter__   s    	Gr   c                 D    t               }t        | j                  |       y rN   )r(   rF   rO   )rP   typvaluetbr*   s        r   __exit__zProfiler.__exit__   s    VFvr   N)r   r   r   __doc__rQ   rS   rX   r   r   r   rL   rL      s    r   rL   rN   )#rY   r>   r2   	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(   r9   rF   rJ   objectrL   r   r   r   <module>re      s"  $  	  : + . , < @ ) 9	! & ) i  LIJH KH2 LHI J0 KC	C$ K	  LHI& J&$ LKLv  Mr   