o
    ?e                     @   sr   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d ZG dd dej	Z
d	d
 Zdd ZG dd de
ZdS )z?Debugger wrapper session that sends debug data to file:// URLs.    N)common)	frameworkc                 C   sD   ddl m} |j|kr t||}|j| |t |d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)Zsend_source)tensorflow.python.debug.libr   versionr   Zget_run_keyZsend_graph_tracebacks	tracebackextract_stack)Zdebug_server_urlsgraph	feed_dictfetchesZold_graph_versionr   Zrun_key r   n/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/debug/wrappers/grpc_wrapper.pypublish_traceback   s   
r   c                   @   s.   e Zd ZdZ		d	ddZdd Zdd ZdS )
GrpcDebugWrapperSessionz=Debug Session wrapper that send debug data to gRPC stream(s).Nc                 C   s   t jj| |||d t|tr| |g| _dS t|tr=g | _|D ]}t|ts1tdt	| | j
| | q"dS tdt	| )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   addressr   r   r   r   A   s,   



z GrpcDebugWrapperSession.__init__c                 C   s   | 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
   r   r   r   prepare_run_debug_urlsm   s   z.GrpcDebugWrapperSession.prepare_run_debug_urlsc                 C   s   | tjstj| S |S N)
startswithr   ZGRPC_URL_PREFIX)r   r    r   r   r   r   ~   s   
z+GrpcDebugWrapperSession._normalize_grpc_url)NN)__name__
__module____qualname____doc__r   r!   r   r   r   r   r   r   >   s    
,r   c                 C   s0   	 t d }|dv rtd n|dv rd S q)NTz'
SIGINT received. Quit program? (Y/n): ) Yyr   )Nn)inputstripsysexit)Zunused_signalZunused_frameresponser   r   r   _signal_handler   s   r2   c                   C   s*   z
t  t jt W d S  ty   Y d S w r"   )signalSIGINTr2   
ValueErrorr   r   r   r   register_signal_handler   s
   r6   c                       s@   e Zd ZdZ		d fdd	Z						d	 fdd	Z  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`.
  NTc                    s2   dd }t  j||||d || _d| _t  dS )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                 S   s   ~ ~t jdgdS )NzDebugIdentity(gated_grpc=true))Z	debug_ops)r   ZWatchOptions)r   Zfeedsr   r   r   _gated_grpc_watch_fn   s   zETensorBoardDebugWrapperSession.__init__.<locals>._gated_grpc_watch_fnr   N)superr   _send_traceback_and_source_code_sent_graph_versionr6   )r   r   r   r   Zsend_traceback_and_source_coder8   	__class__r   r   r      s   
z'TensorBoardDebugWrapperSession.__init__c              	      s8   | j rt| j| j||| j| _t j|||||||dS )N)r
   optionsrun_metadatacallable_runnercallable_runner_argscallable_options)r;   r   r   r	   r<   r:   run)r   r   r
   r?   r@   rA   rB   rC   r=   r   r   rD      s   z"TensorBoardDebugWrapperSession.run)NT)NNNNNN)r$   r%   r&   r'   r   rD   __classcell__r   r   r=   r   r7      s    $r7   )r'   r3   r/   r   r   r   Z tensorflow.python.debug.wrappersr   r   r   r   r2   r6   r7   r   r   r   r   <module>   s   &E	