B
    ӻd                 @   s   d 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ae Zedg dG d	d
 d
ed
ddddgZedg ddddZedg ddddZdd Zedg ddd Zedg dG dd deZdS ) a.  TensorFlow 2.x Profiler.

The profiler has two modes:
- Programmatic Mode: start(logdir), stop(), and Profiler class. Profiling starts
                     when calling start(logdir) or create a Profiler class.
                     Profiling stops when calling stop() to save to
                     TensorBoard logdir or destroying the Profiler class.
- Sampling Mode: start_server(). It will perform profiling after receiving a
                 profiling request.

NOTE: Only one active profiler session is allowed. Use of simultaneous
Programmatic Mode and Sampling 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.
    N)errors)
tf_logging)_pywrap_profiler)	tf_exportz%profiler.experimental.ProfilerOptions)Zv1c                   s"   e Zd ZdZd fdd	Z  ZS )	ProfilerOptionsar  Options for finer control over the profiler.

  Use `tf.profiler.experimental.ProfilerOptions` to control `tf.profiler`
  behavior.

  Fields:
    host_tracer_level: Adjust CPU tracing level. Values are: `1` - critical info
      only, `2` - info, `3` - verbose. [default value is `2`]
    python_tracer_level: Toggle tracing of Python function calls. Values are:
      `1` - enabled, `0` - disabled [default value is `0`]
    device_tracer_level: Adjust device (TPU/GPU) tracing level. Values are:
      `1` - enabled, `0` - disabled [default value is `1`]
    delay_ms: Requests for all hosts to start profiling at a timestamp that is
      `delay_ms` away from the current time. `delay_ms` is in milliseconds. If
      zero, each host will start profiling immediately upon receiving the
      request. Default value is `None`, allowing the profiler guess the best
      value.
     r      Nc                s   t t| | ||||S )N)superr   __new__)clshost_tracer_levelpython_tracer_leveldevice_tracer_leveldelay_ms)	__class__ X/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/profiler/profiler_v2.pyr
   F   s    zProfilerOptions.__new__)r   r   r   N)__name__
__module____qualname____doc__r
   __classcell__r   r   )r   r   r   -   s
      r   r   r   r   r   zprofiler.experimental.startc          	   C   s   t  tdk	rtdddt ay(|dk	r:t| ni }t| | W nF tjk
rz   t	
d tdddY n tk
r   da Y nX W dQ R X dS )aO  Start profiling TensorFlow performance.

  Args:
    logdir: Profiling results log directory.
    options: `ProfilerOptions` namedtuple to specify miscellaneous profiler
      options. See example usage below.

  Raises:
    AlreadyExistsError: If a profiling session is already running.

  Example usage:
  ```python
  options = tf.profiler.experimental.ProfilerOptions(host_tracer_level = 3,
                                                     python_tracer_level = 1,
                                                     device_tracer_level = 1)
  tf.profiler.experimental.start('logdir_path', options = options)
  # Training code here
  tf.profiler.experimental.stop()
  ```

  To view the profiling results, launch TensorBoard and point it to `logdir`.
  Open your browser and go to `localhost:6006/#profile` to view profiling
  results.

  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   ZAlreadyExistsErrorr   ZProfilerSessiondict_asdictstartloggingwarning	Exception)logdiroptionsoptsr   r   r   r   P   s    

r   zprofiler.experimental.stopTc          	   C   sZ   t L tdkrtddd| rHyt  W n tk
rF   da Y nX daW dQ R X dS )a  Stops the current profiling session.

  The profiler session will be stopped and profile results can be saved.

  Args:
    save: An optional variable to save the results to TensorBoard. Default True.

  Raises:
    UnavailableError: If there is no active profiling session.
  Nz8Cannot export profiling results. No profiler is running.)r   r   r   ZUnavailableErrorZexport_to_tbr   )saver   r   r   stop   s    r$   c               C   s   t d tdd dS )zWarm-up the profiler session.

  The profiler session will set up profiling context, including loading CUPTI
  library for GPU profiling. This is used for improving the accuracy of
  the profiling results.

   F)r#   N)r   r$   r   r   r   r   warmup   s    r&   z"profiler.experimental.server.startc             C   s   t |  dS )a\  Start a profiler grpc server that listens to given port.

  The profiler server will exit when the process finishes. The service is
  defined in tensorflow/core/profiler/profiler_service.proto.

  Args:
    port: port profiler server listens to.
  Example usage: ```python tf.profiler.experimental.server.start(6009) # do
    your training here.
  N)r   start_server)portr   r   r   r'      s    r'   zprofiler.experimental.Profilec               @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
Profilea5  Context-manager profile API.

  Profiling will start when entering the scope, and stop and save the results to
  the logdir when exits the scope. Open TensorBoard profile tab to view results.

  Example usage:
  ```python
  with tf.profiler.experimental.Profile("/path/to/logdir"):
    # do some work
  ```
  Nc             C   s   || _ || _dS )zCreates a context manager object for profiler API.

    Args:
      logdir: profile data will save to this directory.
      options: An optional `tf.profiler.experimental.ProfilerOptions` can be
        provided to fine tune the profiler's behavior.
    N)_logdir_options)selfr    r!   r   r   r   __init__   s    zProfile.__init__c             C   s   t | j| j d S )N)r   r*   r+   )r,   r   r   r   	__enter__   s    zProfile.__enter__c             C   s
   t   d S )N)r$   )r,   typvaluetbr   r   r   __exit__   s    zProfile.__exit__)N)r   r   r   r   r-   r.   r2   r   r   r   r   r)      s   
r)   )N)T)r   collections	threadingZtensorflow.python.frameworkr   Ztensorflow.python.platformr   r   Z#tensorflow.python.profiler.internalr   Z tensorflow.python.util.tf_exportr   r   Lockr   
namedtupler   r   r$   r&   r'   objectr)   r   r   r   r   <module>   s(   
0
