B
    ӻd@                 @   sD   d Z ddlZddlmZ ddlmZ edgdG dd deZdS )	z(Utilities for building profiler options.    N)tfprof_logger)	tf_exportzprofiler.ProfileOptionBuilder)Zv1c               @   s   e Zd ZdZd1ddZedd Zedd Zed2ddZdd Z	dd Z
d3ddZd4ddZdd Zdd Zdd Zdd Zd5ddZdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 ZdS )6ProfileOptionBuildera  Option Builder for Profiling API.

  For tutorial on the options, see
  https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/profiler/g3doc/options.md

  ```python
  # Users can use pre-built options:
  opts = (
      tf.profiler.ProfileOptionBuilder.trainable_variables_parameter())

  # Or, build your own options:
  opts = (tf.compat.v1.profiler.ProfileOptionBuilder()
      .with_max_depth(10)
      .with_min_micros(1000)
      .select(['accelerator_micros'])
      .with_stdout_output()
      .build()

  # Or customize the pre-built options:
  opts = (tf.compat.v1.profiler.ProfileOptionBuilder(
      tf.profiler.ProfileOptionBuilder.time_and_memory())
      .with_displaying_options(show_name_regexes=['.*rnn.*'])
      .build())

  # Finally, profiling with the options:
  _ = tf.compat.v1.profiler.profile(tf.compat.v1.get_default_graph(),
                          run_meta=run_meta,
                          cmd='scope',
                          options=opts)
  ```
  Nc             C   sJ   |dk	rt || _n0ddddddddgdgg dgg ddgdd	d
| _dS )zWConstructor.

    Args:
      options: Optional initial option dict to start with.
    Nd   r   namez.*Fmicrosstdout)	max_depth	min_bytes
min_micros
min_paramsmin_float_opsmin_occurrenceorder_byaccount_type_regexesstart_name_regexestrim_name_regexesshow_name_regexeshide_name_regexesaccount_displayed_op_onlyselectstepoutput)copydeepcopy_options)selfoptions r   [/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/profiler/option_builder.py__init__:   s$    zProfileOptionBuilder.__init__c               C   s0   dddddddt jgdgg dgg ddgddd	S )
zOptions used to profile trainable variable parameters.

    Normally used together with 'scope' view.

    Returns:
      A dict of profiling options.
    i'  r   r   z.*Tparamsr   r	   )r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   ZTRAINABLE_VARIABLESr   r   r   r    trainable_variables_parameterT   s     	z2ProfileOptionBuilder.trainable_variables_parameterc               C   s.   ddddddddgdgg dgg ddgddd	S )
a  Options used to profile float operations.

    Please see https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/profiler/g3doc/profile_model_architecture.md
    on the caveats of calculating float operations.

    Returns:
      A dict of profiling options.
    i'  r      Z	float_opsz.*Tr   r	   )r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    float_operationn   s     z$ProfileOptionBuilder.float_operationr$   r   c             C   s:   d||||| ||dddddgdgg dgg dddgddd	S )
a  Show operation time and memory consumptions.

    Args:
      min_micros: Only show profiler nodes with execution time
          no less than this. It sums accelerator and cpu times.
      min_bytes: Only show profiler nodes requested to allocate no less bytes
          than this.
      min_accelerator_micros: Only show profiler nodes spend no less than
          this time on accelerator (e.g. GPU).
      min_cpu_micros: Only show profiler nodes spend no less than
          this time on cpu.
      min_peak_bytes: Only show profiler nodes using no less than this bytes
          at peak (high watermark). For profiler nodes consist of multiple
          graph nodes, it sums the graph nodes' peak_bytes.
      min_residual_bytes: Only show profiler nodes have no less than
          this bytes not being de-allocated after Compute() ends. For
          profiler nodes consist of multiple graph nodes, it sums the
          graph nodes' residual_bytes.
      min_output_bytes: Only show profiler nodes have no less than this bytes
          output. The output are not necessarily allocated by this profiler
          nodes.
    Returns:
      A dict of profiling options.
    i'  r   r   z.*Tbytesr   r	   )r
   r   min_peak_bytesmin_residual_bytesmin_output_bytesr   min_accelerator_microsmin_cpu_microsr   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r*   r+   r'   r(   r)   r   r   r    time_and_memory   s*    z$ProfileOptionBuilder.time_and_memoryc             C   s   t | jS )zOBuild a profiling option.

    Returns:
      A dict of profiling options.
    )r   r   r   )r   r   r   r    build   s    zProfileOptionBuilder.buildc             C   s   || j d< | S )a<  Set the maximum depth of display.

    The depth depends on profiling view. For 'scope' view, it's the
    depth of name scope hierarchy (tree), for 'op' view, it's the number
    of operation types (list), etc.

    Args:
      max_depth: Maximum depth of the data structure to display.
    Returns:
      self
    r
   )r   )r   r
   r   r   r    with_max_depth   s    
z#ProfileOptionBuilder.with_max_depthc             C   s,   || j d< || j d< || j d< || j d< | S )a>  Only show profiler nodes consuming no less than 'min_bytes'.

    Args:
      min_bytes: Only show profiler nodes requested to allocate no less bytes
          than this.
      min_peak_bytes: Only show profiler nodes using no less than this bytes
          at peak (high watermark). For profiler nodes consist of multiple
          graph nodes, it sums the graph nodes' peak_bytes.
      min_residual_bytes: Only show profiler nodes have no less than
          this bytes not being de-allocated after Compute() ends. For
          profiler nodes consist of multiple graph nodes, it sums the
          graph nodes' residual_bytes.
      min_output_bytes: Only show profiler nodes have no less than this bytes
          output. The output are not necessarily allocated by this profiler
          nodes.
    Returns:
      self
    r   r'   r(   r)   )r   )r   r   r'   r(   r)   r   r   r    with_min_memory   s
    



z$ProfileOptionBuilder.with_min_memoryc             C   s"   || j d< || j d< || j d< | S )a  Only show profiler nodes consuming no less than 'min_micros'.

    Args:
      min_micros: Only show profiler nodes with execution time
          no less than this. It sums accelerator and cpu times.
      min_accelerator_micros: Only show profiler nodes spend no less than
          this time on accelerator (e.g. GPU).
      min_cpu_micros: Only show profiler nodes spend no less than
          this time on cpu.
    Returns:
      self
    r   r*   r+   )r   )r   r   r*   r+   r   r   r    with_min_execution_time   s    


z,ProfileOptionBuilder.with_min_execution_timec             C   s   || j d< | S )aD  Only show profiler nodes holding no less than 'min_params' parameters.

    'Parameters' normally refers the weights of in TensorFlow variables.
    It reflects the 'capacity' of models.

    Args:
      min_params: Only show profiler nodes holding number parameters
          no less than this.
    Returns:
      self
    r   )r   )r   r   r   r   r    with_min_parameters  s    
z(ProfileOptionBuilder.with_min_parametersc             C   s   || j d< | S )a  Only show profiler nodes including no less than 'min_occurrence' graph nodes.

    A "node" means a profiler output node, which can be a python line
    (code view), an operation type (op view), or a graph node
    (graph/scope view). A python line includes all graph nodes created by that
    line, while an operation type includes all graph nodes of that type.

    Args:
      min_occurrence: Only show nodes including no less than this.
    Returns:
      self
    r   )r   )r   r   r   r   r    with_min_occurrence  s    
z(ProfileOptionBuilder.with_min_occurrencec             C   s   || j d< | S )a  Only show profiler nodes consuming no less than 'min_float_ops'.

    Please see https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/profiler/g3doc/profile_model_architecture.md
    on the caveats of calculating float operations.

    Args:
      min_float_ops: Only show profiler nodes with float operations
          no less than this.
    Returns:
      self
    r   )r   )r   r   r   r   r    with_min_float_operations'  s    
z.ProfileOptionBuilder.with_min_float_operationsc             C   s   t  || jd< | S )a  Selectively counting statistics based on node types.

    Here, 'types' means the profiler nodes' properties. Profiler by default
    consider device name (e.g. /job:xx/.../device:GPU:0) and operation type
    (e.g. MatMul) as profiler nodes' properties. User can also associate
    customized 'types' to profiler nodes through OpLogProto proto.

    For example, user can select profiler nodes placed on gpu:0 with:
    `account_type_regexes=['.*gpu:0.*']`

    If none of a node's properties match the specified regexes, the node is
    not displayed nor accounted.

    Args:
      account_type_regexes: A list of regexes specifying the types.
    Returns:
      self.
    r   )r   r   )r   r   r   r   r    with_accounted_types8  s    z)ProfileOptionBuilder.with_accounted_typesc             C   sd   |dk	rt  || jd< |dk	r0t  || jd< |dk	rHt  || jd< |dk	r`t  || jd< | S )aM  Regular expressions used to select profiler nodes to display.

    After 'with_accounted_types' is evaluated, 'with_node_names' are
    evaluated as follows:

      For a profile data structure, profiler first finds the profiler
      nodes matching 'start_name_regexes', and starts displaying profiler
      nodes from there. Then, if a node matches 'show_name_regexes' and
      doesn't match 'hide_name_regexes', it's displayed. If a node matches
      'trim_name_regexes', profiler stops further searching that branch.

    Args:
      start_name_regexes: list of node name regexes to start displaying.
      show_name_regexes: list of node names regexes to display.
      hide_name_regexes: list of node_names regexes that should be hidden.
      trim_name_regexes: list of node name regexes from where to stop.
    Returns:
      self
    Nr   r   r   r   )r   r   )r   r   r   r   r   r   r   r    with_node_namesN  s    z$ProfileOptionBuilder.with_node_namesc             C   s   || j d< | S )a  Whether only account the statistics of displayed profiler nodes.

    Args:
      is_true: If true, only account statistics of nodes eventually
          displayed by the outputs.
          Otherwise, a node's statistics are accounted by its parents
          as long as it's types match 'account_type_regexes', even if
          it is hidden from the output, say, by hide_name_regexes.
    Returns:
      self
    r   )r   )r   Zis_truer   r   r    r   p  s    
z.ProfileOptionBuilder.account_displayed_op_onlyc             C   s   d| j d< | S )z$Do not generate side-effect outputs.noner   )r   )r   r   r   r    with_empty_output  s    
z&ProfileOptionBuilder.with_empty_outputc             C   s   d| j d< | S )zPrint the result to stdout.r	   r   )r   )r   r   r   r    with_stdout_output  s    
z'ProfileOptionBuilder.with_stdout_outputc             C   s   d| | j d< | S )zPrint the result to a file.zfile:outfile=%sr   )r   )r   outfiler   r   r    with_file_output  s    z%ProfileOptionBuilder.with_file_outputc             C   s   d| | j d< | S )zGenerate a timeline json file.ztimeline:outfile=%sr   )r   )r   Ztimeline_filer   r   r    with_timeline_output  s    z)ProfileOptionBuilder.with_timeline_outputc             C   s   d| | j d< | S )zGenerate a pprof profile gzip file.

    To use the pprof file:
      pprof -png --nodecount=100 --sample_index=1 <pprof_file>

    Args:
      pprof_file: filename for output, usually suffixed with .pb.gz.
    Returns:
      self.
    zpprof:outfile=%sr   )r   )r   Z
pprof_filer   r   r    with_pprof_output  s    z&ProfileOptionBuilder.with_pprof_outputc             C   s   || j d< | S )aB  Order the displayed profiler nodes based on a attribute.

    Supported attribute includes micros, bytes, occurrence, params, etc.
    https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/profiler/g3doc/options.md

    Args:
      attribute: An attribute the profiler node has.
    Returns:
      self
    r   )r   )r   	attributer   r   r    r     s    
zProfileOptionBuilder.order_byc             C   s   t  || jd< | S )a  Select the attributes to display.

    See https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/profiler/g3doc/options.md
    for supported attributes.

    Args:
      attributes: A list of attribute the profiler node has.
    Returns:
      self
    r   )r   r   )r   
attributesr   r   r    r     s    zProfileOptionBuilder.selectc             C   s   || j d< | S )a1  Which profile step to use for profiling.

    The 'step' here refers to the step defined by `Profiler.add_step()` API.

    Args:
      step: When multiple steps of profiles are available, select which step's
         profile to use. If -1, use average of all available steps.
    Returns:
      self
    r   )r   )r   r   r   r   r    	with_step  s    
zProfileOptionBuilder.with_step)N)r$   r$   r   r   r   r   r   )r   r   r   r   )r   r   r   )NNNN)__name__
__module____qualname____doc__r!   staticmethodr#   r%   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r   r7   r8   r:   r;   r<   r   r   r?   r   r   r   r    r      sD   !
  /   
  
   
r   )rC   r   Ztensorflow.python.profilerr   Z tensorflow.python.util.tf_exportr   objectr   r   r   r   r    <module>   s
   