o
    ?e5                     @   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                   @   sB   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.
  readlineNc                 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:
        (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   g/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/debug/wrappers/hooks.py__init__#   s   
zLocalCLIDebugHook.__init__c                 C   s&   | j r| j || dS || 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      d S Nr   r   r   r   r   beginT      zLocalCLIDebugHook.beginc              	   C   s   | j s#tj|j| j| j| j| jd| _ | jD ]}| j 	|| j|  q| j 
  t|jj|jjd d | j j}| j |}|j| _tjd d t d}| jtjjkrh| j j|j|j|j|j|j|j |j!d |S | jtjj"krv| 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   sL   
	



zLocalCLIDebugHook.before_runc                 C   s    t | j|j}| j| d S r   )r   ZOnRunEndRequestr-   Zrun_metadatar   Z
on_run_end)r   r1   
run_valuesZon_run_end_requestr   r   r   	after_run   s   zLocalCLIDebugHook.after_run)r
   NNN)	__name__
__module____qualname____doc__r   r   r   r3   r5   r   r   r   r   r	      s    	
4r	   c                   @   s6   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.
  Nc                 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.
    N)_session_root	_watch_fnr   r   )r   Zsession_rootwatch_fnr   r   r   r   r      s   
zDumpingDebugHook.__init__c                 C   r   r   r   r   r   r   r   r      r   zDumpingDebugHook.beginc                 C   s   d}| j st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r=   r   T)r0   r#   r$   r%   r&   r'   reset_disk_byte_usager    )r   r   ZDumpingDebugWrapperSessionr(   r;   r<   r   r)   Z_prepare_run_watch_configr*   r+   r!   r   r/   r   watch_graphgraphr#   r$   r%   r&   r'   r   r.   )r   r1   r?   r0   watch_optionsrun_optionsr2   r   r   r   r3      s:   
zDumpingDebugHook.before_runc                 C   r   r   r   )r   r1   r4   r   r   r   r5      r   zDumpingDebugHook.after_runNN)r6   r7   r8   r9   r   r   r3   r5   r   r   r   r   r:      s    
!r:   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.
  Nc                 C   s,   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.
    N)_grpc_debug_wrapper_sessionr   
isinstancelist_grpc_debug_server_addressesr<   )r   grpc_debug_server_addressesr=   r   r   r   r   r      s   
zGrpcDebugHook.__init__c              
   C   s   | j st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.
    r>   )r0   r#   r$   r%   r&   r'   Nr    )rF   r   ZGrpcDebugWrapperSessionr(   rI   r<   r   r*   r+   r!   r   r/   r   r@   rA   Zprepare_run_debug_urlsr#   r$   r%   r&   r'   r   r.   )r   r1   r+   r!   rB   rC   r   r   r   r3      s4   zGrpcDebugHook.before_runrD   )r6   r7   r8   r9   r   r3   r   r   r   r   rE      s    
rE   c                       s2   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).
    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_fn@  s   z;TensorBoardDebugHook.__init__.<locals>._gated_grpc_watch_fnr>   N)superrK   r   rI   _send_traceback_and_source_code_sent_graph_versionr   Zregister_signal_handler)r   rJ   r   Zsend_traceback_and_source_coderL   	__class__r   r   r   1  s   
zTensorBoardDebugHook.__init__c                    s:   | j rt| j|jj|jj|jj| j	| _	t
t| |S r   )rO   r   Zpublish_tracebackrI   r(   rA   r*   r!   r+   rP   rN   rK   r3   )r   r1   rQ   r   r   r3   O  s   

zTensorBoardDebugHook.before_run)NT)r6   r7   r8   r9   r   r3   __classcell__r   r   rQ   r   rK   %  s    rK   N)r9   Ztensorflow.core.protobufr   Ztensorflow.python.debug.libr   Z tensorflow.python.debug.wrappersr   r   r   r   Ztensorflow.python.trainingr   ZSessionRunHookr	   r:   rE   rK   r   r   r   r   <module>   s   yDN