B
    һd7                 @   s   d 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 G d	d
 d
ejZG dd dejZG dd dejZG dd deZdS )ztfdbg CLI as SessionRunHook.    )
config_pb2)debug_utils)dumping_wrapper)	framework)grpc_wrapper)local_cli_wrapper)session_run_hookc               @   s:   e Zd ZdZdddZdd Zdd	 Zd
d Zdd ZdS )LocalCLIDebugHooka  Command-line-interface debugger hook.

  Can be used as a hook for `tf.compat.v1.train.MonitoredSession`s and
  `tf.estimator.Estimator`s. Provides a substitute for
  `tfdbg.LocalCLIDebugWrapperSession` in cases where the session is not directly
  available.
  cursesNc             C   s(   || _ || _|| _d| _i | _|| _dS )a  Create a local debugger command-line interface (CLI) hook.

    Args:
      ui_type: (`str`) requested user-interface type. Currently supported:
        (curses | readline).
      dump_root: (`str`) optional path to the dump root directory. Must be a
        directory that does not exist or an empty directory. If the directory
        does not exist, it will be created by the debugger core during debug
        `run()` calls and removed afterwards.
      thread_name_filter: Regular-expression white list for threads on which the
        wrapper session will be active. See doc of `BaseDebugWrapperSession` for
        more details.
      config_file_path: Optional override to the default configuration file
        path, which is at `${HOME}/.tfdbg_config`.
    N)_ui_type
_dump_root_thread_name_filter_session_wrapper_pending_tensor_filters_config_file_path)selfui_type	dump_rootthread_name_filterconfig_file_path r   X/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/debug/wrappers/hooks.py__init__#   s    zLocalCLIDebugHook.__init__c             C   s$   | j r| j || n
|| j|< dS )a  Add a tensor filter.

    See doc of `LocalCLIDebugWrapperSession.add_tensor_filter()` for details.
    Override default behavior to accommodate the possibility of this method
    being
    called prior to the initialization of the underlying
    `LocalCLIDebugWrapperSession` object.

    Args:
      filter_name: See doc of `LocalCLIDebugWrapperSession.add_tensor_filter()`
        for details.
      tensor_filter: See doc of
        `LocalCLIDebugWrapperSession.add_tensor_filter()` for details.
    N)r   add_tensor_filterr   )r   filter_nameZtensor_filterr   r   r   r   ?   s    z#LocalCLIDebugHook.add_tensor_filterc             C   s   d S )Nr   )r   r   r   r   beginT   s    zLocalCLIDebugHook.beginc          	   C   s   | j sJtj|j| j| j| j| jd| _ x"| jD ]}| j 	|| j|  q.W | j 
  t|jj|jjd d | j j}| j |}|j| _tjd d t d}| jtjjkr| j j|j|j|j|j|j|j |j!d n| jtjj"kr| j #|j |S )N)r   r   r   r   )	feed_dictoptions)	debug_opsnode_name_regex_allowlistop_type_regex_allowlisttensor_dtype_regex_allowlist#tolerate_debug_op_creation_failures)$r   r   ZLocalCLIDebugWrapperSessionsessionr   r   r   r   r   r   increment_run_call_countr   ZOnRunStartRequestoriginal_argsfetchesr   Zrun_call_countZon_run_startaction_performed_actionr   SessionRunArgsr   
RunOptionsZOnRunStartActionZ	DEBUG_RUNZ_decorate_run_options_for_debugr   
debug_urlsr   r   r    r!   r"   ZPROFILE_RUNZ!_decorate_run_options_for_profile)r   run_contextr   Zon_run_start_requestZon_run_start_responserun_argsr   r   r   
before_runW   s>    
zLocalCLIDebugHook.before_runc             C   s    t | j|j}| j| d S )N)r   ZOnRunEndRequestr(   Zrun_metadatar   Z
on_run_end)r   r,   
run_valuesZon_run_end_requestr   r   r   	after_run   s    zLocalCLIDebugHook.after_run)r
   NNN)	__name__
__module____qualname____doc__r   r   r   r.   r0   r   r   r   r   r	      s      
4r	   c               @   s2   e Zd ZdZdddZdd Zdd	 Zd
d ZdS )DumpingDebugHookzA debugger hook that dumps debug data to filesystem.

  Can be used as a hook for `tf.compat.v1.train.MonitoredSession`s and
  `tf.estimator.Estimator`s.
  NTc             C   s"   || _ || _|| _|| _d| _dS )a  Create a local debugger command-line interface (CLI) hook.

    Args:
      session_root: See doc of
        `dumping_wrapper.DumpingDebugWrapperSession.__init__`.
      watch_fn: See doc of
        `dumping_wrapper.DumpingDebugWrapperSession.__init__`.
      thread_name_filter: Regular-expression white list for threads on which the
        wrapper session will be active. See doc of `BaseDebugWrapperSession` for
        more details.
      log_usage: (bool) Whether usage is to be logged.
    N)_session_root	_watch_fnr   
_log_usager   )r   Zsession_rootwatch_fnr   	log_usager   r   r   r      s
    zDumpingDebugHook.__init__c             C   s   d S )Nr   )r   r   r   r   r      s    zDumpingDebugHook.beginc             C   s   d}| j s.tj|j| j| j| j| jd| _ d}| j   | j 	|j
j|j
j\}}t }tj||jj||j|j|j|j|j|d	 tjd d |d}|S )NF)r9   r   r:   T)r+   r   r   r    r!   r"   reset_disk_byte_usage)r   r   )r   r   ZDumpingDebugWrapperSessionr#   r6   r7   r   r8   r$   Z_prepare_run_watch_configr%   r&   r   r   r*   r   watch_graphgraphr   r   r    r!   r"   r   r)   )r   r,   r;   r+   watch_optionsrun_optionsr-   r   r   r   r.      s4    
zDumpingDebugHook.before_runc             C   s   d S )Nr   )r   r,   r/   r   r   r   r0      s    zDumpingDebugHook.after_run)NNT)r1   r2   r3   r4   r   r   r.   r0   r   r   r   r   r5      s     
"r5   c               @   s"   e Zd ZdZdddZdd ZdS )	GrpcDebugHooka*  A hook that streams debugger-related events to any grpc_debug_server.

  For example, the debugger data server is a grpc_debug_server. The debugger
  data server writes debugger-related events it receives via GRPC to logdir.
  This enables debugging features in Tensorboard such as health pills.

  When the arguments of debug_utils.watch_graph changes, strongly consider
  changing arguments here too so that features are available to tflearn users.

  Can be used as a hook for `tf.compat.v1.train.MonitoredSession`s and
  `tf.estimator.Estimator`s.
  NTc             C   s2   d| _ || _t|tr|n|g| _|| _|| _dS )a  Constructs a GrpcDebugHook.

    Args:
      grpc_debug_server_addresses: (`list` of `str`) A list of the gRPC debug
        server addresses, in the format of <host:port>, with or without the
        "grpc://" prefix. For example: ["localhost:7000", "192.168.0.2:8000"]
      watch_fn: A function that allows for customizing which ops to watch at
        which specific steps. See doc of
        `dumping_wrapper.DumpingDebugWrapperSession.__init__` for details.
      thread_name_filter: Regular-expression white list for threads on which the
        wrapper session will be active. See doc of `BaseDebugWrapperSession` for
        more details.
      log_usage: (bool) Whether usage is to be logged.
    N)_grpc_debug_wrapper_sessionr   
isinstancelist_grpc_debug_server_addressesr7   r8   )r   grpc_debug_server_addressesr9   r   r:   r   r   r   r      s    zGrpcDebugHook.__init__c          
   C   s   | j s&tj|j| j| j| j| jd| _ |jj	}|jj
}| ||}t }tj||jj| j |||j|j|j|j|jd tjdd|dS )zCalled right before a session is run.

    Args:
      run_context: A session_run_hook.SessionRunContext. Encapsulates
        information on the run.

    Returns:
      A session_run_hook.SessionRunArgs object.
    )r9   r   r:   )r+   r   r   r    r!   r"   N)r   r   )rA   r   ZGrpcDebugWrapperSessionr#   rD   r7   r   r8   r%   r&   r   r   r*   r   r<   r=   Zprepare_run_debug_urlsr   r   r    r!   r"   r   r)   )r   r,   r&   r   r>   r?   r   r   r   r.     s.    
zGrpcDebugHook.before_run)NNT)r1   r2   r3   r4   r   r.   r   r   r   r   r@      s
     
r@   c                   s.   e Zd ZdZd fdd	Z fddZ  ZS )	TensorBoardDebugHooka  A tfdbg hook that can be used with TensorBoard Debugger Plugin.

  This hook is the same as `GrpcDebugHook`, except that it uses a predefined
    `watch_fn` that
    1) uses `DebugIdentity` debug ops with the `gated_grpc` attribute set to
        `True`, to allow the interactive enabling and disabling of tensor
       breakpoints.
    2) watches all tensors in the graph.
  This saves the need for the user to define a `watch_fn`.
  NTc                s>   dd }t t| j||||d || _|| _d| _t  dS )a,  Constructor of TensorBoardDebugHook.

    Args:
      grpc_debug_server_addresses: gRPC address(es) of debug server(s), as a
        `str` or a `list` of `str`s. E.g., "localhost:2333",
        "grpc://localhost:2333", ["192.168.0.7:2333", "192.168.0.8:2333"].
      thread_name_filter: Optional filter for thread names.
      send_traceback_and_source_code: Whether traceback of graph elements and
        the source code are to be sent to the debug server(s).
      log_usage: Whether the usage of this class is to be logged (if
        applicable).
    c             S   s   ~ ~t jdgdS )NzDebugIdentity(gated_grpc=true))r   )r   ZWatchOptions)r&   Zfeedsr   r   r   _gated_grpc_watch_fnK  s    z;TensorBoardDebugHook.__init__.<locals>._gated_grpc_watch_fn)r9   r   r:   N)superrF   r   rD   _send_traceback_and_source_code_sent_graph_versionr   Zregister_signal_handler)r   rE   r   Zsend_traceback_and_source_coder:   rG   )	__class__r   r   r   9  s    
zTensorBoardDebugHook.__init__c                s:   | j r*t| j|jj|jj|jj| j	| _	t
t| |S )N)rJ   r   Zpublish_tracebackrD   r#   r=   r%   r   r&   rK   rI   rF   r.   )r   r,   )rL   r   r   r.   [  s    
zTensorBoardDebugHook.before_run)NTT)r1   r2   r3   r4   r   r.   __classcell__r   r   )rL   r   rF   -  s
   
  rF   N)r4   Ztensorflow.core.protobufr   Ztensorflow.python.debug.libr   Z tensorflow.python.debug.wrappersr   r   r   r   Ztensorflow.python.trainingr   ZSessionRunHookr	   r5   r@   rF   r   r   r   r   <module>   s   yHR