
    BVh4                         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
ej                        Z G d dej                        Z G d dej                        Z G d de      Zy)ztfdbg CLI as SessionRunHook.    )
config_pb2)debug_utils)dumping_wrapper)	framework)grpc_wrapper)local_cli_wrapper)session_run_hookc                   8    e Zd ZdZ	 	 	 	 ddZd Zd Zd Zd Zy)	LocalCLIDebugHookzlCommand-line-interface debugger hook.

  Can be used as a hook for `tf.compat.v1.train.MonitoredSession`.
  Nc                 X    || _         || _        || _        d| _        i | _        || _        y)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_paths        V/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/debug/wrappers/hooks.py__init__zLocalCLIDebugHook.__init__    s2    * DMDO1D D#%D -D    c                 t    | j                   r| j                   j                  ||       y|| j                  |<   y)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_filters      r   r   z#LocalCLIDebugHook.add_tensor_filter<   s3      
--k=I2?d"";/r   c                      y N r   s    r   beginzLocalCLIDebugHook.beginQ       r   c           	      x   | j                   st        j                  |j                  | j                  | j
                  | j                  | j                        | _         | j                  D ]+  }| j                   j                  || j                  |          - | j                   j                          t        j                  |j                  j                  |j                  j                  d d | j                   j                         }| j                   j#                  |      }|j$                  | _        t)        j*                  d d t-        j.                               }| j&                  t        j0                  j2                  k(  rj| j                   j5                  |j6                  |j8                  |j:                  |j<                  |j>                  |j@                  |jB                         |S | j&                  t        j0                  jD                  k(  r%| j                   jG                  |j6                         |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_argss         r   
before_runzLocalCLIDebugHook.before_runT   s     /KK


--OO!55113d 55 D+//55kB	DD
 	224 %66!!));+D+D+N+NdD))88: !11>>299D..j&;&;&=?H!;!;!E!EE
;;




*
*)33#==#;;#@@#GG < J$ O 
		9#=#=#I#I	I
==h>N>NO Or   c                     t        j                  | j                  |j                        }| j                  j                  |       y r    )r   OnRunEndRequestr7   run_metadatar   
on_run_end)r   r@   
run_valueson_run_end_requests       r   	after_runzLocalCLIDebugHook.after_run   s<     #2243I3I3=3J3JL$$%78r   )readlineNNN)	__name__
__module____qualname____doc__r   r   r#   rD   rK   r!   r   r   r   r      s0     ""& $	.8@*	2h9r   r   c                   .    e Zd ZdZ	 	 ddZd Zd Zd Zy)DumpingDebugHookz{A debugger hook that dumps debug data to filesystem.

  Can be used as a hook for `tf.compat.v1.train.MonitoredSession`.
  Nc                 <    || _         || _        || _        d| _        y)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   s       r   r   zDumpingDebugHook.__init__   s#      &DDN1D Dr   c                      y r    r!   r"   s    r   r#   zDumpingDebugHook.begin   r$   r   c                    d}| j                   sHt        j                  |j                  | j                  | j
                  | j                        | _         d}| j                   j                          | j                   j                  |j                  j                  |j                  j                        \  }}t        j                         }t        j                  ||j                  j                   ||j"                  |j$                  |j&                  |j(                  |j*                  |	       t-        j.                  d d |      }|S )NFrW   r   T)r=   r)   r*   r+   r,   r-   reset_disk_byte_usager&   )r   r   DumpingDebugWrapperSessionr/   rT   rU   r   r0   _prepare_run_watch_configr2   r3   r'   r   r9   r   watch_graphgraphr)   r*   r+   r,   r-   r	   r8   )r   r@   r[   r=   watch_optionsrun_optionsrC   s          r   rD   zDumpingDebugHook.before_run   s   !  -HH





>>!55	7d
 #224 !% 5 5 O O!!));+D+D+N+N!PJ '')K!!))"/"I"I - E E%2%O%O==3
5  ..k3HOr   c                      y r    r!   )r   r@   rI   s      r   rK   zDumpingDebugHook.after_run   r$   r   NN)rM   rN   rO   rP   r   r#   rD   rK   r!   r   r   rR   rR      s$     "&!*	B	r   rR   c                   "    e Zd ZdZ	 	 ddZd Zy)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                 b    d| _         || _        t        |t              r|n|g| _        || _        y)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_addressesrU   )r   grpc_debug_server_addressesrW   r   s       r   r   zGrpcDebugHook.__init__   s=    " (,D$1D 148 	$	$% 	%
 DNr   c           
      t   | j                   sFt        j                  |j                  | j                  | j
                  | j                        | _         |j                  j                  }|j                  j                  }| j                  ||      }t        j                         }t        j                  ||j                  j                  | j                   j                  ||      |j                   |j"                  |j$                  |j&                  |j(                         t+        j,                  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.
    rZ   )r=   r)   r*   r+   r,   r-   Nr&   )rg   r   GrpcDebugWrapperSessionr/   rj   rU   r   r2   r3   r'   r   r9   r   r^   r_   prepare_run_debug_urlsr)   r*   r+   r,   r-   r	   r8   )r   r@   r3   r'   r`   ra   s         r   rD   zGrpcDebugHook.before_run   s    ++)5)M)M




+
+>>!55	*7d& ''//G))33INN7I6M'')K!!33JJY ))"/"I"I - E E%2%O%O==
@ **k3 3r   rc   )rM   rN   rO   rP   r   rD   r!   r   r   re   re      s    
 "&4#3r   re   c                   2     e Zd ZdZ	 	 d fd	Z fdZ x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`.
  c                     d }t         t        |   |||       || _        || _        d| _        t        j                          y)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                 4    ~ ~t        j                  dg      S )NzDebugIdentity(gated_grpc=true))r)   )r   WatchOptions)r3   feedss     r   _gated_grpc_watch_fnz;TensorBoardDebugHook.__init__.<locals>._gated_grpc_watch_fn;  s     
5##568 8r   rZ   N)superrp   r   rj   _send_traceback_and_source_code_sent_graph_versionr   register_signal_handler)r   rk   r   send_traceback_and_source_coderu   	__class__s        r   r   zTensorBoardDebugHook.__init__,  sO    8
 

.#%- / /
 )DD%+ID(!D((*r   c                    | j                   rnt        j                  | j                  |j                  j
                  |j                  j                  |j                  j                  | j                        | _	        t        t        | 3  |      S r    )rx   r   publish_tracebackrj   r/   r_   r2   r'   r3   ry   rw   rp   rD   )r   r@   r|   s     r   rD   zTensorBoardDebugHook.before_runJ  sr    ++!-!?!?

+
+[-@-@-F-F

#
#
-
-

#
#
+
+T-E-E"Gd %t7DDr   )NT)rM   rN   rO   rP   r   rD   __classcell__)r|   s   @r   rp   rp      s!    	 #'.2+<E Er   rp   N)rP   tensorflow.core.protobufr   tensorflow.python.debug.libr    tensorflow.python.debug.wrappersr   r   r   r   tensorflow.python.trainingr	   SessionRunHookr   rR   re   rp   r!   r   r   <module>r      so    # / 3 < 6 9 > 7s9(77 s9l@	'66 @	FJ3$33 J3Z0E= 0Er   