o
    2h4                     @   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 )LocalCLIDebugHookzlCommand-line-interface debugger hook.

  Can be used as a hook for `tf.compat.v1.train.MonitoredSession`.
  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   a/var/www/html/chatgem/venv/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_nametensor_filterr   r   r   r   <   s   z#LocalCLIDebugHook.add_tensor_filterc                 C      d S Nr   r   r   r   r   beginQ      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   LocalCLIDebugWrapperSessionsessionr   r   r   r   r   r   increment_run_call_countr   OnRunStartRequestoriginal_argsfetchesr"   run_call_counton_run_startaction_performed_actionr   SessionRunArgsr   
RunOptionsOnRunStartAction	DEBUG_RUN_decorate_run_options_for_debugr#   
debug_urlsr$   r%   r&   r'   r(   PROFILE_RUN!_decorate_run_options_for_profile)r   run_contextr   on_run_start_requeston_run_start_responserun_argsr   r   r   
before_runT   sL   
	



zLocalCLIDebugHook.before_runc                 C   s    t | j|j}| j| d S r   )r   OnRunEndRequestr2   run_metadatar   
on_run_end)r   r;   
run_valueson_run_end_requestr   r   r   	after_run   s   zLocalCLIDebugHook.after_run)r
   NNN)	__name__
__module____qualname____doc__r   r   r   r?   rE   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 )DumpingDebugHookz{A debugger hook that dumps debug data to filesystem.

  Can be used as a hook for `tf.compat.v1.train.MonitoredSession`.
  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   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rN   r   T)r8   r$   r%   r&   r'   r(   reset_disk_byte_usager!   )r   r   DumpingDebugWrapperSessionr*   rK   rL   r   r+   _prepare_run_watch_configr-   r.   r"   r   r4   r   watch_graphgraphr$   r%   r&   r'   r(   r   r3   )r   r;   rP   r8   watch_optionsrun_optionsr>   r   r   r   r?      s:   
zDumpingDebugHook.before_runc                 C   r   r   r   )r   r;   rC   r   r   r   rE      r    zDumpingDebugHook.after_runNN)rF   rG   rH   rI   r   r   r?   rE   r   r   r   r   rJ      s    
!rJ   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`.
  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_addressesrL   )r   grpc_debug_server_addressesrN   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.
    rO   )r8   r$   r%   r&   r'   r(   Nr!   )rY   r   GrpcDebugWrapperSessionr*   r\   rL   r   r-   r.   r"   r   r4   r   rS   rT   prepare_run_debug_urlsr$   r%   r&   r'   r(   r   r3   )r   r;   r.   r"   rU   rV   r   r   r   r?      s4   zGrpcDebugHook.before_runrW   )rF   rG   rH   rI   r   r?   r   r   r   r   rX      s    
rX   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   WatchOptions)r.   feedsr   r   r   _gated_grpc_watch_fn;  s   z;TensorBoardDebugHook.__init__.<locals>._gated_grpc_watch_fnrO   N)superr`   r   r\   _send_traceback_and_source_code_sent_graph_versionr   register_signal_handler)r   r]   r   send_traceback_and_source_coderc   	__class__r   r   r   ,  s   
zTensorBoardDebugHook.__init__c                    s:   | j rt| j|jj|jj|jj| j	| _	t
t| |S r   )rf   r   publish_tracebackr\   r*   rT   r-   r"   r.   rg   re   r`   r?   )r   r;   rj   r   r   r?   J  s   

zTensorBoardDebugHook.before_run)NT)rF   rG   rH   rI   r   r?   __classcell__r   r   rj   r   r`      s    r`   N)rI   tensorflow.core.protobufr   tensorflow.python.debug.libr    tensorflow.python.debug.wrappersr   r   r   r   tensorflow.python.trainingr   SessionRunHookr	   rJ   rX   r`   r   r   r   r   <module>   s   vCM