
    BVh                         d Z ddlZddlZddlZddlmZ ddlmZ d Z G d dej                        Z
d Zd	 Z G d
 de
      Zy)z?Debugger wrapper session that sends debug data to file:// URLs.    N)common)	frameworkc                     ddl m} |j                  |kD  rJt        j                  ||      }|j                  | |t        j                         |d       |j                  S |S )a  Publish traceback and source code if graph version is new.

  `graph.version` is compared with `old_graph_version`. If the former is higher
  (i.e., newer), the graph traceback and the associated source code is sent to
  the debug server at the specified gRPC URLs.

  Args:
    debug_server_urls: A single gRPC debug server URL as a `str` or a `list` of
      debug server URLs.
    graph: A Python `tf.Graph` object.
    feed_dict: Feed dictionary given to the `Session.run()` call.
    fetches: Fetches from the `Session.run()` call.
    old_graph_version: Old graph version to compare to.

  Returns:
    If `graph.version > old_graph_version`, the new graph version as an `int`.
    Else, the `old_graph_version` is returned.
  r   )source_remoteT)send_source)tensorflow.python.debug.libr   versionr   get_run_keysend_graph_tracebacks	tracebackextract_stack)debug_server_urlsgraph	feed_dictfetchesold_graph_versionr   run_keys          ]/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/debug/wrappers/grpc_wrapper.pypublish_tracebackr      s`    4 8
]]&&  G4G''7I$;$;$=u (  ==    c                   (    e Zd ZdZ	 	 ddZd Zd Zy)GrpcDebugWrapperSessionz=Debug Session wrapper that send debug data to gRPC stream(s).Nc                    t         j                  j                  | |||       t        |t              r| j                  |      g| _        yt        |t              r`g | _        |D ]S  }t        |t              st        dt        |      z        | j                  j                  | j                  |             U yt        dt        |      z        )a  Constructor of DumpingDebugWrapperSession.

    Args:
      sess: The TensorFlow `Session` object being wrapped.
      grpc_debug_server_addresses: (`str` or `list` of `str`) Single or a list
        of the gRPC debug server addresses, in the format of
        <host:port>, with or without the "grpc://" prefix. For example:
          "localhost:7000",
          ["localhost:7000", "192.168.0.2:8000"]
      watch_fn: (`Callable`) A Callable that can be used to define per-run
        debug ops and watched tensors. See the doc of
        `NonInteractiveDebugWrapperSession.__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.

    Raises:
       TypeError: If `grpc_debug_server_addresses` is not a `str` or a `list`
         of `str`.
    watch_fnthread_name_filterzGExpected type str in list grpc_debug_server_addresses, received type %szJExpected type str or list in grpc_debug_server_addresses, received type %sN)r   !NonInteractiveDebugWrapperSession__init__
isinstancestr_normalize_grpc_url_grpc_debug_server_urlslist	TypeErrortypeappend)selfsessgrpc_debug_server_addressesr   r   addresss         r   r   z GrpcDebugWrapperSession.__init__A   s    2 //88dX:L 9 N -s3

"
"#>
?&Ad"	/	6%'d"0 O''3'!#'=12 2 	$$++D,D,DW,MNO #$?@AB Br   c                     | j                   S )a  Implementation of abstract method in superclass.

    See doc of `NonInteractiveDebugWrapperSession.prepare_run_debug_urls()`
    for details.

    Args:
      fetches: Same as the `fetches` argument to `Session.run()`
      feed_dict: Same as the `feed_dict` argument to `Session.run()`

    Returns:
      debug_urls: (`str` or `list` of `str`) file:// debug URLs to be used in
        this `Session.run()` call.
    )r"   )r'   r   r   s      r   prepare_run_debug_urlsz.GrpcDebugWrapperSession.prepare_run_debug_urlsm   s     '''r   c                 j    |j                  t        j                        st        j                  |z   S |S N)
startswithr   GRPC_URL_PREFIX)r'   r*   s     r   r!   z+GrpcDebugWrapperSession._normalize_grpc_url~   s5    %%f&<&<= ""W, LCJLr   )NN)__name__
__module____qualname____doc__r   r,   r!    r   r   r   r   >   s    E
 "&	*BX("Lr   r   c                 v    	 t        d      j                         }|dv rt        j                  d       n|dv ry 9)Nz'
SIGINT received. Quit program? (Y/n): ) Yyr   )Nn)inputstripsysexit)unused_signalunused_frameresponses      r   _signal_handlerrC      s<    ?@FFHH>!	hhqk	Z	 	r   c                  t    	 t        j                   t         j                  t               y # t        $ r Y y w xY wr.   )signalSIGINTrC   
ValueErrorr5   r   r   register_signal_handlerrH      s+    	
MM&--1	 		s   (+ 	77c                   @     e Zd ZdZ	 	 d fd	Z	 	 	 	 	 	 d fd	Z xZS )TensorBoardDebugWrapperSessiona  A tfdbg Session wrapper that can be used with TensorBoard Debugger Plugin.

  This wrapper is the same as `GrpcDebugWrapperSession`, 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                 b    d }t         |   ||||       || _        d| _        t	                y)a  Constructor of TensorBoardDebugWrapperSession.

    Args:
      sess: The `tf.compat.v1.Session` instance to be wrapped.
      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))	debug_ops)r   WatchOptions)r   feedss     r   _gated_grpc_watch_fnzETensorBoardDebugWrapperSession.__init__.<locals>._gated_grpc_watch_fn   s     
5##568 8r   r   N)superr   _send_traceback_and_source_code_sent_graph_versionrH   )r'   r(   r)   r   send_traceback_and_source_coderP   	__class__s         r   r   z'TensorBoardDebugWrapperSession.__init__   sC     8
 
G#%-	  / ,JD(  "Dr   c           	          | j                   r2t        | j                  | j                  ||| j                        | _        t
        |   |||||||      S )N)r   optionsrun_metadatacallable_runnercallable_runner_argscallable_options)rS   r   r"   r   rT   rR   run)	r'   r   r   rX   rY   rZ   r[   r\   rV   s	           r   r]   z"TensorBoardDebugWrapperSession.run   sd     ++!2

&
&

Iw

"
""$d 7;!'1)  + +r   )NT)NNNNNN)r1   r2   r3   r4   r   r]   __classcell__)rV   s   @r   rJ   rJ      s4    	 #'.2	 H #+ +r   rJ   )r4   rE   r>   r   r   r    tensorflow.python.debug.wrappersr   r   r   r   rC   rH   rJ   r5   r   r   <module>r`      sP    F  
  . 6#LBLiII BLJ	A+%< A+r   