a
    /Sic                    @   st  d Z ddlZddlZddlZddl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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 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" ddl#m$Z$ ddl%m&Z& e'ddZ(G dd de)Z*dd Z+dd Z,ej-dd dd d d fej.d!d e,d"d fe)d#d d$d d%d fgZ/d&d' Z0dGd(d)Z1d*d+ Z2d,d- Z3G d.d/ d/e)Z4G d0d1 d1e4Z5d2d3 Z6G d4d5 d5e4Z7G d6d7 d7e4Z8G d8d9 d9e4Z9G d:d; d;e)Z:d<d= Z;G d>d? d?e)Z<G d@dA dAe*Z=e&dBgdCG dDdB dBe=Z>e&dEgdCG dFdE dEe=Z?dS )Hz"A client interface for TensorFlow.    N)
config_pb2)rewriter_config_pb2)pywrap_tf_session)context)
monitoring)device)error_interpolation)errors)indexed_slices)ops)sparse_tensor)session_ops)
tf_logging)mixed_precision_global_state)compat)nest)collections_abc)	tf_exportz-/tensorflow/api/python/session_create_counterz1Counter for number of sessions created in Python.c                   @   sF   e Zd ZdZedd Zedd ZdddZdd	d
ZdddZ	dS )SessionInterfacez=Base class for implementations of TensorFlow client sessions.c                 C   s   t ddS )zCThe underlying TensorFlow graph, to be used in building Operations.graphNNotImplementedErrorself r   \/var/www/html/django/DPS/env/lib/python3.9/site-packages/tensorflow/python/client/session.pyr   5   s    zSessionInterface.graphc                 C   s   t ddS )z:The TensorFlow process to which this session will connect.sess_strNr   r   r   r   r   r   :   s    zSessionInterface.sess_strNc                 C   s   t ddS )zDRuns operations in the session. See `BaseSession.run()` for details.runNr   )r   fetches	feed_dictoptionsrun_metadatar   r   r   r   ?   s    zSessionInterface.runc                 C   s   t ddS )z>Sets up the feeds and fetches for partial runs in the session.partial_run_setupNr   )r   r   feedsr   r   r   r"   C   s    z"SessionInterface.partial_run_setupc                 C   s   t ddS )z:Continues the execution with additional feeds and fetches.partial_runNr   r   handler   r   r   r   r   r$   G   s    zSessionInterface.partial_run)NNN)N)N)
__name__
__module____qualname____doc__propertyr   r   r   r"   r$   r   r   r   r   r   2   s   



r   c                 C   s*   t | d | d t| dkr$| d nd S )Nr            )r
   IndexedSlicesValuelenZfetched_valsr   r   r   &_get_indexed_slices_value_from_fetchesL   s    r2   c                 C   s0   t t| jd u r| j| jgn| j| j| jg|S N)listzipdense_shapevaluesindicesfeedfeed_valr   r   r   _get_feeds_for_indexed_slicesR   s
    r<   c                 C   s   | j | j| jgdd fS )Nc                 S   s
   t j|  S r3   )r   SparseTensorValuer1   r   r   r   <lambda>u       <lambda>.<locals>.<lambda>r8   r7   r6   fetchr   r   r   r>   s   s    r>   c                 C   s   t t| j| j| jg|S r3   )r4   r5   r8   r7   r6   r9   r   r   r   r>   v   s   c                 C   s   | j | j| jgS r3   rA   r:   r   r   r   r>   x   r?   c                 C   s*   | j d u r| j| jgn| j| j| j gtfS r3   )r6   r7   r8   r2   rB   r   r   r   r>   |   s   c                 C   s&   | j d u r| j| jgS | j| j| j gS r3   )r6   r7   r8   rD   r   r   r   r>      s   c                 C   s   | gdd fS )Nc                 S   s   | d S Nr   r   r1   r   r   r   r>      r?   r@   r   rB   r   r   r   r>      r?   c                 C   s
   | |fgS r3   r   r9   r   r   r   r>      r?   c                 C   s   | gS r3   r   rD   r   r   r   r>      r?   c                 C   s   | t ur| |S t|S )zBExplicitly convert obj based on numpy type except for string type.)objectstr)numpy_dtypeobjr   r   r   _convert_to_numpy_obj   s    rJ   c                 C   s>   t D ] }t|d | rt|  dqt d| |||f dS )a  Register fetch and feed conversion functions for `tf.Session.run()`.

  This function registers a triple of conversion functions for fetching and/or
  feeding values of user-defined types in a call to tf.Session.run().

  An example

  ```python
     class SquaredTensor(object):
       def __init__(self, tensor):
         self.sq = tf.square(tensor)
     #you can define conversion functions as follows:
     fetch_function = lambda squared_tensor:([squared_tensor.sq],
                                             lambda val: val[0])
     feed_function = lambda feed, feed_val: [(feed.sq, feed_val)]
     feed_function_for_partial_run = lambda feed: [feed.sq]
     #then after invoking this register function, you can use as follows:
     session.run(squared_tensor1,
                 feed_dict = {squared_tensor2 : some_numpy_array})
  ```

  Args:
    tensor_type: The type for which you want to register a conversion function.
    fetch_function: A callable that takes an object of type `tensor_type` and
      returns a tuple, where the first element is a list of `tf.Tensor` objects,
      and the second element is a callable that takes a list of ndarrays and
      returns an object of some value type that corresponds to `tensor_type`.
      fetch_function describes how to expand fetch into its component Tensors
      and how to contract the fetched results back into a single return value.
    feed_function: A callable that takes feed_key and feed_value as input, and
      returns a list of tuples (feed_tensor, feed_val), feed_key must have type
      `tensor_type`, and feed_tensor must have type `tf.Tensor`. Each feed
      function describes how to unpack a single fed value and map it to feeds of
      one or more tensors and their corresponding values.
    feed_function_for_partial_run: A callable for specifying tensor values to
      feed when setting up a partial run, which takes a `tensor_type` type
      object as input, and returns a list of Tensors.

  Raises:
    ValueError: If `tensor_type` has already been registered.
  r   z* has already been registered so ignore it.N)_REGISTERED_EXPANSIONS
issubclass
ValueErrorinsert)tensor_typeZfetch_functionZfeed_functionZfeed_function_for_partial_runZconversion_functionr   r   r   )register_session_run_conversion_functions   s    .rP   c                 C   s   t | jddduS )zFReturns True if the given obj is an instance of attrs-decorated class.__attrs_attrs__Ngetattr	__class__rI   r   r   r   _is_attrs_instance   s    rV   c                    s   t  jd} fdd|D S )z2Returns the list of values from an attrs instance.rQ   c                    s   g | ]}t  |jqS r   )rS   name).0arU   r   r   
<listcomp>   r?   z%_get_attrs_values.<locals>.<listcomp>rR   )rI   attrsr   rU   r   _get_attrs_values   s    r\   c                   @   s,   e Zd ZdZdd Zdd Zedd ZdS )	_FetchMapperav  Definition of the interface provided by fetch mappers.

  Fetch mappers are utility classes used by the _FetchHandler to handle
  arbitrary structures for the `fetch` argument to `Session.run()`.

  The `fetch` argument can be of various shapes: single tensor or op, list of
  fetches, tuple of fetches, namedtuple of fetches, or dict of fetches.  The
  structures can be arbitrarily nested.

  The low level run() API only wants a list of tensor or op names.  The various
  `_FetchMapper` subclasses below take care of handling the different shapes:
  uniquifying the fetches, and constructing results with the original shape.
  c                 C   s   t ddS )zxReturn the list of unique tensors or ops needed by this fetch mapper.

    Returns:
      A list of tensors or ops.
    z0unique_fetches must be implemented by subclassesNr   r   r   r   r   unique_fetches   s    z_FetchMapper.unique_fetchesc                 C   s   t ddS )a  Build results that match the original shape of the fetch.

    Args:
      values: List of values returned by run(). The values correspond exactly to
        the list tensors or ops returned by unique_fetches().

    Returns:
      A struct of the same shape as the original fetch object handled by
      this fetch mapper.  In the returned struct, the original fetches are
      replaced by their fetched values.
    z/build_results must be implemented by subclassesNr   r   r7   r   r   r   build_results   s    z_FetchMapper.build_resultsc                 C   s   | du r&t d|  dt| j dnpt| ttfr<t| S t| tjrPt	| S t
| r`t| S tD ]0\}}}}t| |rd|| \}}t||  S qdt d|  dt| j ddS )ao  Creates fetch mapper that handles the structure of `fetch`.

    The default graph must be the one from which we want to fetch values when
    this function is called.

    Args:
      fetch: An arbitrary fetch structure: singleton, list, tuple, namedtuple,
        or dict.

    Returns:
      An instance of a subclass of `_FetchMapper` that handles the shape.
    NArgument `fetch` =  has invalid type "z". Cannot be None")	TypeErrortyper'   
isinstancer4   tuple_ListFetchMapperr   Mapping_DictFetchMapperrV   _AttrsFetchMapperrK   _ElementFetchMapper)rC   rO   Zfetch_fn_r   contraction_fnr   r   r   	for_fetch   s"    


z_FetchMapper.for_fetchN)r'   r(   r)   r*   r^   r`   staticmethodro   r   r   r   r   r]      s
   	r]   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	rl   z+Fetch mapper for singleton tensors and ops.c                 C   s   g | _ |D ]}z | j t j|ddd W q
 tyx } z2td| dt|j dt| dW Y d}~q
d}~0  t	y } z&t	d| dt| dW Y d}~q
d}~0  t
y } z&t	d| dt| dW Y d}~q
d}~0 0 q
|| _dS )	a  Creates an _ElementFetchMapper.

    This is the fetch mapper used for leaves in the fetch struct.  Because of
    the expansions mechanism, a leaf can actually fetch more than one tensor.

    Also note that the fetches here can be just strings (tensor or op names) or
    any other object that the graph knows how to convert to a tensor, such as a
    Variable.  So we have to run each fetch through `as_graph_element()` to get
    the corresponding tensor or op.

    Args:
      fetches: List of objects, as returned by a fetch_fn defined in
        _REGISTERED_EXPANSIONS.
      contraction_fn: Callable as returned by a fetch_fn.
    Tallow_tensorallow_operationra   rb   z" must be a string or Tensor. ()Nz% cannot be interpreted as a Tensor. ()_unique_fetchesappendr   get_default_graphas_graph_elementrd   re   r'   rG   rM   KeyError_contraction_fn)r   r   rn   rC   er   r   r   __init__  s*    


"z_ElementFetchMapper.__init__c                 C   s   | j S r3   ru   r   r   r   r   r^   >  s    z"_ElementFetchMapper.unique_fetchesc                 C   s   |sd S |  |S d S r3   )rz   r_   r   r   r   r`   A  s    z!_ElementFetchMapper.build_resultsNr'   r(   r)   r*   r|   r^   r`   r   r   r   r   rl     s   !rl   c                 C   sx   g }g }i }| D ]^}g }|  D ]B}|t|}|du rXt|}||t|< || || q || q||fS )a{  Uniquifies fetches from a list of fetch_mappers.

  This is a utility function used by _ListFetchMapper and _DictFetchMapper.  It
  gathers all the unique fetches from a list of mappers and builds a list
  containing all of them but without duplicates (unique_fetches).

  It also returns a 2-D list of integers (values_indices) indicating at which
  index in unique_fetches the fetches of the mappers are located.

  This list is as follows:
    values_indices[mapper_index][mapper_fetch_index] = unique_fetches_index

  Args:
    fetch_mappers: list of fetch mappers.

  Returns:
    A list of fetches.
    A 2-D list of integers.
  N)r^   getidr0   rv   )Zfetch_mappersr^   Zvalue_indicesZseen_fetchesmZm_value_indicesfjr   r   r   _uniquify_fetchesI  s    
r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	rh   z0Fetch mapper for lists, tuples, and namedtuples.c                 C   sJ   t |tjrt|j| _n
t|| _dd |D | _t| j\| _| _	dS )zaCreates a _ListFetchMapper.

    Args:
      fetches: List, tuple, or namedtuple of fetches.
    c                 S   s   g | ]}t |qS r   r]   ro   rX   rC   r   r   r   rZ   z  r?   z-_ListFetchMapper.__init__.<locals>.<listcomp>N)
rf   wraptObjectProxyre   __wrapped___fetch_type_mappersr   ru   _value_indicesr   r   r   r   r   r|   p  s
    
z_ListFetchMapper.__init__c                 C   s   | j S r3   r}   r   r   r   r   r^   }  s    z_ListFetchMapper.unique_fetchesc                    sj   g }t | j| jD ]&\}}|| fdd|D  qt| jtrJ|S | jtkr\t|S | j| S d S )Nc                    s   g | ]} | qS r   r   rX   r   r7   r   r   rZ     r?   z2_ListFetchMapper.build_results.<locals>.<listcomp>)	r5   r   r   rv   r`   rL   r   r4   rg   r   r7   resultsr   vir   r   r   r`     s     
z_ListFetchMapper.build_resultsNr~   r   r   r   r   rh   m  s   rh   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	rj   zFetch mapper for dicts.c                 C   sf   t || _t|tjr*ttj|j| _n| j| _|	 | _
dd | D | _t| j\| _| _dS )zKCreates a _DictFetchMapper.

    Args:
      fetches: Dict of fetches.
    c                 S   s   g | ]}t |qS r   r   r   r   r   r   rZ     s   z-_DictFetchMapper.__init__.<locals>.<listcomp>N)re   r   rf   collectionsdefaultdict	functoolspartialdefault_factory
_type_ctorkeys_keysr7   r   r   ru   r   r   r   r   r   r|     s    

z_DictFetchMapper.__init__c                 C   s   | j S r3   r}   r   r   r   r   r^     s    z_DictFetchMapper.unique_fetchesc                    s    fdd}  | S )Nc                  3   s@   t  j j jD ](\} }}| |fdd|D fV  qd S )Nc                    s   g | ]} | qS r   r   r   r   r   r   rZ     r?   zF_DictFetchMapper.build_results.<locals>._generator.<locals>.<listcomp>)r5   r   r   r   r`   )kr   r   r_   r   r   
_generator  s    z2_DictFetchMapper.build_results.<locals>._generator)r   )r   r7   r   r   r_   r   r`     s    z_DictFetchMapper.build_resultsNr~   r   r   r   r   rj     s   rj   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	rk   z)Fetch mapper for attrs decorated classes.c                 C   s8   t |}t|| _dd |D | _t| j\| _| _dS )zdCreates a _AttrsFetchMapper.

    Args:
      fetches: An instance of an attrs decorated class.
    c                 S   s   g | ]}t |qS r   r   r   r   r   r   rZ     r?   z._AttrsFetchMapper.__init__.<locals>.<listcomp>N)r\   re   r   r   r   ru   r   )r   r   r7   r   r   r   r|     s    
z_AttrsFetchMapper.__init__c                 C   s   | j S r3   r}   r   r   r   r   r^     s    z _AttrsFetchMapper.unique_fetchesc                    sD   g }t | j| jD ]&\}}|| fdd|D  q| j| S )Nc                    s   g | ]} | qS r   r   r   r   r   r   rZ     r?   z3_AttrsFetchMapper.build_results.<locals>.<listcomp>)r5   r   r   rv   r`   r   r   r   r   r   r`     s     z_AttrsFetchMapper.build_resultsNr~   r   r   r   r   rk     s   rk   c                   @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )_FetchHandlera  Handler for structured fetches.

  Given a graph, a user-provided structure for fetches, and a feed dict, this
  class takes care of generating a list of tensor names to fetch and op names
  to run for a low level `run()` call.

  Given the results of the low level run call, this class can also rebuild a
  result structure matching the user-provided structure for fetches, but
  containing the corresponding results.
  Nc                    s  |   t|| _W d   n1 s*0    Y  g | _g | _ | _|pLi | _g | _i | _	| j
 D ]}t|tjr| || | j| | jd n&| ||j | j| | jd t|tjrf|jjdks|jjdkrf|jjd j| j	| < qf fdd| jD | _dS )	a  Creates a fetch handler.

    Args:
      graph: Graph of the fetches.   Used to check for fetchability and to
        convert all fetches to tensors or ops as needed.
      fetches: An arbitrary fetch structure: singleton, list, tuple, namedtuple,
        or dict.
      feeds: A feed dict where keys are Tensors.
      feed_handles: A dict from feed Tensors to TensorHandle objects used as
        direct feeds.
    NTFGetSessionHandleGetSessionHandleV2r   c                    s   g | ]}|   vr|qS r   )ref)rX   xr#   r   r   rZ     r?   z*_FetchHandler.__init__.<locals>.<listcomp>)
as_defaultr]   ro   _fetch_mapper_fetches_targets_feeds_feed_handles_ops_fetch_handlesr^   rf   r   	Operation_assert_fetchablerv   opTensorre   inputsdtyper   _final_fetches)r   r   r   r#   feed_handlesrC   r   r   r   r|     s.    
*


z_FetchHandler.__init__c                 C   s"   | |std|j dd S )Nz
Operation z has been marked as not fetchable. Typically this happens when it is defined in another function or code block. Use return values, explicit Python locals or TensorFlow collections to access it.)is_fetchabler	   InaccessibleTensorErrorrW   )r   r   r   r   r   r   r     s    
z_FetchHandler._assert_fetchablec                 C   s   | j S )zXReturn the unique names of tensors to fetch.

    Returns:
      A list of strings.
    )r   r   r   r   r   r     s    z_FetchHandler.fetchesc                 C   s   | j S )zRReturn the unique names of ops to run.

    Returns:
      A list of strings.
    )r   r   r   r   r   targets  s    z_FetchHandler.targetsc           	      C   s   g }t | jt |ksJ d}d}| jD ]}|r<|d q(| j|  | jv rj| j| j|    }n| j	| j|  }|du r|| }|d7 }| j
	| j|  }|r|t||| n
|| |d7 }q(|t |ksJ | j|S )a  Build results matching the original fetch shape.

    `tensor_values` must be a list of the same length as
    the one returned by `fetches()`, and holding the requested
    fetch values.

    This method builds a struct with the same shape as the original `fetches`
    passed to the constructor, in which the fetches are replaced by their
    fetched value.

    Args:
      session: The enclosing session.  Used for tensor handles.
      tensor_values: List of values matching the list returned by fetches().

    Returns:
      A structure of the same shape as the original `fetches` argument but
        containing tensors or None (for fetched ops).
    r   Nr,   )r0   r   r   rv   r   r   r   evalr   r   r   r   TensorHandler   r`   )	r   sessionZtensor_valuesZfull_valuesir   Zis_opvaluer   r   r   r   r`     s(    


z_FetchHandler.build_results)N)	r'   r(   r)   r*   r|   r   r   r   r`   r   r   r   r   r     s   
$r   c                 C   s   dd | D S )zUtility function for transitioning to the new session API.

  Args:
    tensor_list: a list of `Tensor`s.

  Returns:
    A list of each `Tensor`s name (as byte arrays).
  c                 S   s   g | ]}t |jqS r   )r   as_bytesrW   rX   tr   r   r   rZ   M  r?   z_name_list.<locals>.<listcomp>r   )tensor_listr   r   r   
_name_listD  s    	r   c                   @   sP   e Zd ZdZdd Zedd Zedd Zedd	 Zed
d Z	dd Z
dS )_DeviceAttributesa  Struct-like object describing a device's attributes.

  Each device has 3 key properties:
   - name: the fully-qualified TensorFlow path to the device. For
        example: /job:worker/replica:0/task:3/device:CPU:0
   - device_type: the type of the device (e.g. CPU, GPU, TPU, etc.)
   - memory_limit_bytes: the maximum amount of memory available on the device
        (in bytes).
  c                 C   s"   t || _|| _|| _|| _d S r3   )r   canonical_name_name_device_type_memory_limit_bytes_incarnation)r   rW   device_typememory_limit_bytesincarnationr   r   r   r|   [  s    z_DeviceAttributes.__init__c                 C   s   | j S r3   )r   r   r   r   r   rW   a  s    z_DeviceAttributes.namec                 C   s   | j S r3   )r   r   r   r   r   r   e  s    z_DeviceAttributes.device_typec                 C   s   | j S r3   )r   r   r   r   r   r   i  s    z$_DeviceAttributes.memory_limit_bytesc                 C   s   | j S r3   )r   r   r   r   r   r   m  s    z_DeviceAttributes.incarnationc                 C   s   d| j | j| j| jf S )Nz!_DeviceAttributes(%s, %s, %d, %d))rW   r   r   r   r   r   r   r   __repr__q  s    z_DeviceAttributes.__repr__N)r'   r(   r)   r*   r|   r+   rW   r   r   r   r   r   r   r   r   r   P  s   




r   c                   @   s   e Zd ZdZd3ddZdd Zdd	 Zd
d Zedd Z	edd Z
edd Zdd Zd4ddZd5ddZd6ddZdd Zd7ddZedZd d! Zd"d# Zd$d% Zd&Zd'd( Zd)d* Zd+d, Zd-d. ZG d/d0 d0eZd1d2 ZdS )8BaseSessionzA class for interacting with a TensorFlow computation.

  The BaseSession enables incremental graph building with inline
  execution of Operations and evaluation of Tensors.
   Nc              	   C   s  t  d |du r"t | _n(t|tjsDtdt	|j
 d|| _d| _|durzt|| _W q ty   t|tjrtdt	|j
 dtdt	|j
 dY q0 nd| _t | _g | _|du rt j}t|tjstdt	|j
 dt rF|jjjtjjkrFt }|| tjj |jj_|}n|jjjtjj krdt!d	 || _"|jj#| _$d| _%t&j'| j|d
}zJ| jj() }t&*||| _%W d   n1 s0    Y  W t&+| nt&+| 0 dS )a}  Constructs a new TensorFlow session.

    Args:
      target: (Optional) The TensorFlow execution engine to connect to.
      graph: (Optional) The graph to be used. If this argument is None, the
        default graph will be used.
      config: (Optional) ConfigProto proto used to configure the session. If no
        config is specified, the global default will be used. The global default
        can be configured via the tf.config APIs.

    Raises:
      tf.errors.OpError: Or one of its subclasses if an error occurs while
        creating the TensorFlow session.
      TypeError: If one of the arguments has the wrong type.
    r,   Nz.Argument `graph` must be a tf.Graph, but got "rc   Fz-Argument `target` must be a string, but got "zD". Did you do "Session(config)" instead of "Session(config=config)"?z5Argument `config` must be a tf.ConfigProto, but got "T)targetconfig),_python_session_create_counterget_cellincrease_byr   rw   _graphrf   Graphrd   re   r'   _closedr   r   _targetr   ConfigProto	threadingLock_delete_lock_dead_handlesr   r   r   Z(is_mixed_precision_graph_rewrite_enabledgraph_optionsrewrite_optionsauto_mixed_precisionr   RewriterConfigOFFCopyFromONZ'set_non_mixed_precision_session_created_configinfer_shapes_add_shapes_session
tf_sessionTF_NewSessionOptions_c_graphr   TF_NewSessionRefTF_DeleteSessionOptions)r   r   r   r   Z
new_configoptsc_graphr   r   r   r|     sf    








0zBaseSession.__init__c           	      C   sz   t | j}g }t |}t|D ]H}t ||}t ||}t ||}t ||}|	t
|||| q"t | |S )a  Lists available devices in this session.

    ```python
    devices = sess.list_devices()
    for d in devices:
      print(d.name)
    ```

    Where:
      Each element in the list has the following properties
      name: A string with the full name of the device. ex:
          `/job:worker/replica:0/task:3/device:CPU:0`
      device_type: The type of the device (e.g. `CPU`, `GPU`, `TPU`.)
      memory_limit: The maximum amount of memory available on the device.
          Note: depending on the device, it is possible the usable memory could
          be substantially less.

    Raises:
      tf.errors.OpError: If it encounters an error (e.g. session is in an
      invalid state, or network errors occur).

    Returns:
      A list of devices in the session.
    )r   TF_SessionListDevicesr   TF_DeviceListCountrangeTF_DeviceListNameTF_DeviceListTypeTF_DeviceListMemoryBytesTF_DeviceListIncarnationrv   r   TF_DeleteDeviceList)	r   Zraw_device_listdevice_listsizer   rW   r   memoryr   r   r   r   list_devices  s    

zBaseSession.list_devicesc                 C   s"   | j r| jsd| _t| j  dS )zCloses this session.

    Calling this method frees all resources associated with the session.

    Raises:
      tf.errors.OpError: Or one of its subclasses if an error occurs while
        closing the TensorFlow session.
    TN)r   r   r   TF_CloseSessionr   r   r   r   close  s    	zBaseSession.closec              	   C   s\   z|    W n ty   Y n0 | jd urXzt| j W n ttfyP   Y n0 d | _d S r3   )r   	Exceptionr   r   TF_DeleteSessionAttributeErrorrd   r   r   r   r   __del__   s    
zBaseSession.__del__c                 C   s   | j S )z,The graph that was launched in this session.)r   r   r   r   r   r     s    zBaseSession.graphc                 C   s   | j j| jdS )zA serializable version of the underlying TensorFlow graph.

    Returns:
      A graph_pb2.GraphDef proto containing nodes for all of the Operations in
      the underlying TensorFlow graph.
    )
add_shapes)r   as_graph_defr   r   r   r   r   	graph_def  s    zBaseSession.graph_defc                 C   s   | j S r3   )r   r   r   r   r   r      s    zBaseSession.sess_strc                 C   s
   t | S )a  Returns a context manager that makes this object the default session.

    Use with the `with` keyword to specify that calls to
    `tf.Operation.run` or `tf.Tensor.eval` should be executed in
    this session.

    ```python
    c = tf.constant(..)
    sess = tf.compat.v1.Session()

    with sess.as_default():
      assert tf.compat.v1.get_default_session() is sess
      print(c.eval())
    ```

    To get the current default session, use `tf.compat.v1.get_default_session`.

    *N.B.* The `as_default` context manager *does not* close the
    session when you exit the context, and you must close the session
    explicitly.

    ```python
    c = tf.constant(...)
    sess = tf.compat.v1.Session()
    with sess.as_default():
      print(c.eval())
    # ...
    with sess.as_default():
      print(c.eval())

    sess.close()
    ```

    Alternatively, you can use `with tf.compat.v1.Session():` to create a
    session that is automatically closed on exiting the context,
    including when an uncaught exception is raised.

    *N.B.* The default session is a property of the current thread. If you
    create a new thread, and wish to use the default session in that
    thread, you must explicitly add a `with sess.as_default():` in that
    thread's function.

    *N.B.* Entering a `with sess.as_default():` block does not affect
    the current default graph. If you are using multiple graphs, and
    `sess.graph` is different from the value of
    `tf.compat.v1.get_default_graph`, you must explicitly enter a
    `with sess.graph.as_default():` block to make `sess.graph` the default
    graph.

    Returns:
      A context manager using this session as the default session.
    )r   default_sessionr   r   r   r   r   $  s    5zBaseSession.as_defaultc           	   	   C   s   |rt t| nd}|r(t  nd}zP| d||||}|r^t |}|t| W |rnt 	| |rt 	| n|rt 	| |rt 	| 0 |S )a  Runs operations and evaluates tensors in `fetches`.

    This method runs one "step" of TensorFlow computation, by
    running the necessary graph fragment to execute every `Operation`
    and evaluate every `Tensor` in `fetches`, substituting the values in
    `feed_dict` for the corresponding input values.

    The `fetches` argument may be a single graph element, or an arbitrarily
    nested list, tuple, namedtuple, dict, or OrderedDict containing graph
    elements at its leaves.  A graph element can be one of the following types:

    * A `tf.Operation`.
      The corresponding fetched value will be `None`.
    * A `tf.Tensor`.
      The corresponding fetched value will be a numpy ndarray containing the
      value of that tensor.
    * A `tf.sparse.SparseTensor`.
      The corresponding fetched value will be a
      `tf.compat.v1.SparseTensorValue`
      containing the value of that sparse tensor.
    * A `get_tensor_handle` op.  The corresponding fetched value will be a
      numpy ndarray containing the handle of that tensor.
    * A `string` which is the name of a tensor or operation in the graph.

    The value returned by `run()` has the same shape as the `fetches` argument,
    where the leaves are replaced by the corresponding values returned by
    TensorFlow.

    Example:

    ```python
       a = tf.constant([10, 20])
       b = tf.constant([1.0, 2.0])
       # 'fetches' can be a singleton
       v = session.run(a)
       # v is the numpy array [10, 20]
       # 'fetches' can be a list.
       v = session.run([a, b])
       # v is a Python list with 2 numpy arrays: the 1-D array [10, 20] and the
       # 1-D array [1.0, 2.0]
       # 'fetches' can be arbitrary lists, tuples, namedtuple, dicts:
       MyData = collections.namedtuple('MyData', ['a', 'b'])
       v = session.run({'k1': MyData(a, b), 'k2': [b, a]})
       # v is a dict with
       # v['k1'] is a MyData namedtuple with 'a' (the numpy array [10, 20]) and
       # 'b' (the numpy array [1.0, 2.0])
       # v['k2'] is a list with the numpy array [1.0, 2.0] and the numpy array
       # [10, 20].
    ```

    The optional `feed_dict` argument allows the caller to override
    the value of tensors in the graph. Each key in `feed_dict` can be
    one of the following types:

    * If the key is a `tf.Tensor`, the
      value may be a Python scalar, string, list, or numpy ndarray
      that can be converted to the same `dtype` as that
      tensor. Additionally, if the key is a
      `tf.compat.v1.placeholder`, the shape of
      the value will be checked for compatibility with the placeholder.
    * If the key is a
      `tf.sparse.SparseTensor`,
      the value should be a
      `tf.compat.v1.SparseTensorValue`.
    * If the key is a nested tuple of `Tensor`s or `SparseTensor`s, the value
      should be a nested tuple with the same structure that maps to their
      corresponding values as above.

    Each value in `feed_dict` must be convertible to a numpy array of the dtype
    of the corresponding key.

    The optional `options` argument expects a [`RunOptions`] proto. The options
    allow controlling the behavior of this particular step (e.g. turning tracing
    on).

    The optional `run_metadata` argument expects a [`RunMetadata`] proto. When
    appropriate, the non-Tensor output of this step will be collected there. For
    example, when users turn on tracing in `options`, the profiled info will be
    collected into this argument and passed back.

    Args:
      fetches: A single graph element, a list of graph elements, or a dictionary
        whose values are graph elements or lists of graph elements (described
        above).
      feed_dict: A dictionary that maps graph elements to values (described
        above).
      options: A [`RunOptions`] protocol buffer
      run_metadata: A [`RunMetadata`] protocol buffer

    Returns:
      Either a single value if `fetches` is a single graph element, or
      a list of values if `fetches` is a list, or a dictionary with the
      same keys as `fetches` if that is a dictionary (described above).
      Order in which `fetches` operations are evaluated inside the call
      is undefined.

    Raises:
      RuntimeError: If this `Session` is in an invalid state (e.g. has been
        closed).
      TypeError: If `fetches` or `feed_dict` keys are of an inappropriate type.
      ValueError: If `fetches` or `feed_dict` keys are invalid or refer to a
        `Tensor` that doesn't exist.
    N)
r   TF_NewBufferFromStringr   r   SerializeToStringTF_NewBuffer_runTF_GetBufferParseFromStringTF_DeleteBuffer)	r   r   r   r    r!   options_ptrrun_metadata_ptrresult
proto_datar   r   r   r   [  s.    i


zBaseSession.runc                 C   s   |  |||ddS )aB  Continues the execution with more feeds and fetches.

    This is EXPERIMENTAL and subject to change.

    To use partial execution, a user first calls `partial_run_setup()` and
    then a sequence of `partial_run()`. `partial_run_setup` specifies the
    list of feeds and fetches that will be used in the subsequent
    `partial_run` calls.

    The optional `feed_dict` argument allows the caller to override
    the value of tensors in the graph. See run() for more information.

    Below is a simple example:

    ```python
    a = array_ops.placeholder(dtypes.float32, shape=[])
    b = array_ops.placeholder(dtypes.float32, shape=[])
    c = array_ops.placeholder(dtypes.float32, shape=[])
    r1 = math_ops.add(a, b)
    r2 = math_ops.multiply(r1, c)

    h = sess.partial_run_setup([r1, r2], [a, b, c])
    res = sess.partial_run(h, r1, feed_dict={a: 1, b: 2})
    res = sess.partial_run(h, r2, feed_dict={c: res})
    ```

    Args:
      handle: A handle for a sequence of partial runs.
      fetches: A single graph element, a list of graph elements, or a dictionary
        whose values are graph elements or lists of graph elements (see
        documentation for `run`).
      feed_dict: A dictionary that maps graph elements to values (described
        above).

    Returns:
      Either a single value if `fetches` is a single graph element, or
      a list of values if `fetches` is a list, or a dictionary with the
      same keys as `fetches` if that is a dictionary
      (see documentation for `run`).

    Raises:
      tf.errors.OpError: Or one of its subclasses on error.
    N)r  r%   r   r   r   r$     s    -zBaseSession.partial_runc                    s"  dd } j rtd jjdkr*td|du r6g }g }t|ttf}|sR|g}|D ]x}||D ]j}z$ jj|ddd	}||	  W qb t
y }	 z(d
|	j |	_|	jf|	_|	W Y d}	~	qbd}	~	0 0 qbqVt j|i }
 fdd}dd |
 D }dd |
 D } | j|||S )a  Sets up a graph with feeds and fetches for partial run.

    This is EXPERIMENTAL and subject to change.

    Note that contrary to `run`, `feeds` only specifies the graph elements.
    The tensors will be supplied by the subsequent `partial_run` calls.

    Args:
      fetches: A single graph element, or a list of graph elements.
      feeds: A single graph element, or a list of graph elements.

    Returns:
      A handle for partial run.

    Raises:
      RuntimeError: If this `Session` is in an invalid state (e.g. has been
        closed).
      TypeError: If `fetches` or `feed_dict` keys are of an inappropriate type.
      tf.errors.OpError: Or one of its subclasses if a TensorFlow error happens.
    c                 S   sH   t D ]"\}}}}t| |r||   S qtd|  dt| j dd S )NzFeed argument rb   rc   rK   rf   rd   re   r'   )r:   rO   rm   feed_fnr   r   r   _feed_fn  s    

z/BaseSession.partial_run_setup.<locals>._feed_fn"Attempted to use a closed Session.r   MThe Session graph is empty. Add operations to the graph before calling run().NTFrq   z0Cannot interpret argument `feed` key as Tensor: c                    s       t| |||S r3   )_extend_graphr   TF_SessionPRunSetup_wrapper)r   	feed_list
fetch_listtarget_listr   r   r   	_setup_fnC  s    z0BaseSession.partial_run_setup.<locals>._setup_fnc                 S   s   g | ]}|  qS r   _as_tf_outputr   r   r   r   rZ   I  r?   z1BaseSession.partial_run_setup.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r   _c_oprX   r   r   r   r   rZ   J  r?   )r   RuntimeErrorr   versionrf   r4   rg   rx   rv   r  r   messageargsr   r   r   r   _do_callr   )r   r   r#   r  r  Zis_list_feedr:   subfeed	subfeed_tr{   fetch_handlerr  final_fetchesfinal_targetsr   r   r   r"     s>    
zBaseSession.partial_run_setupc                 C   sb  dd }| j rtd| jjdkr*tdi }i }i }	|rt|}| D ]\}
}||
|D ]\}}z| jj|ddd}W n8 ty } z t	d	|j
d  W Y d
}~n
d
}~0 0 t|tjrt	dt| dt|
 d|jj}t|tr:t|||kr:t	dt| dtt| dt| d t|tj}|rb| }||	| < ntj||d}|s| |jstdt|j d|j dt|  | j|std|j d||| < ||f|t !|j< qbqNt"| j#|||	d}| $||}|% }|& }|s<|s<|rR|rR| '||||||}ng }|(| |S )zFPerform either run or partial_run, depending the presence of `handle`.c                 S   sH   t D ]$\}}}}t| |r|| |  S qt|  dt| j dd S )Nz+ in argument `feed_dict` has invalid type "rc   r  )r:   r;   rO   rm   r  r   r   r   r  S  s    
z"BaseSession._run.<locals>._feed_fnr  r   r  TFrq   z*Cannot interpret feed_dict key as Tensor: NzThe value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles. For reference, the tensor object was z7 which was passed to the argument `feed_dict` with key .zType of feed value z with type z$ is not compatible with Tensor type zS. Try explicitly setting the type of the feed tensor to a larger type (e.g. int64).r   zCannot feed value of shape z for Tensor z, which has shape zTensor z may not be fed.)r   ))r   r  r   r  r   flatten_dict_itemsitemsrx   r   rd   r   rf   r   r   rG   r   as_numpy_dtypeintrJ   re   r   r   Zto_numpy_arrayr   npasarray	get_shapeis_compatible_withshaperM   rW   is_feedabler   r   r   r   _update_with_moversr   r   _do_runr`   )r   r&   r   r   r    r!   r  Zfeed_dict_tensorfeed_mapr   r:   r;   r"  Zsubfeed_valr#  r{   Zsubfeed_dtypeZis_tensor_handle_feednp_valr$  rm   r%  r&  r   r   r   r   r  P  s    



	
zBaseSession._runFc           	         s   dur8t  ttfs$td   fdd}|S   tji dd  D dd  D dfdd		}|rt	
|S t tjrʈrJ td
ksJ fdd}|S t tjrtd
ksJ rJ fdd}|S fdd}|S dS )a  Returns a Python callable that runs a particular step.

    The returned callable will take `len(feed_list)` arguments whose types
    must be compatible feed values for the respective elements of `feed_list`.
    For example, if element `i` of `feed_list` is a `tf.Tensor`, the `i`th
    argument to the returned callable must be a numpy ndarray (or something
    convertible to an ndarray) with matching element type and shape. See
    `tf.Session.run` for details of the allowable feed key and value types.

    The returned callable will have the same return type as
    `tf.Session.run(fetches, ...)`. For example, if `fetches` is a `tf.Tensor`,
    the callable will return a numpy ndarray; if `fetches` is a `tf.Operation`,
    it will return `None`.

    Args:
      fetches: A value or list of values to fetch. See `tf.Session.run` for
        details of the allowable fetch types.
      feed_list: (Optional.) A list of `feed_dict` keys. See `tf.Session.run`
        for details of the allowable feed key types.
      accept_options: (Optional.) If `True`, the returned `Callable` will be
        able to accept `tf.compat.v1.RunOptions` and `tf.compat.v1.RunMetadata`
        as optional keyword arguments `options` and `run_metadata`,
        respectively, with the same syntax and semantics as `tf.Session.run`,
        which is useful for certain use cases (profiling and debugging) but will
        result in measurable slowdown of the `Callable`'s
        performance. Default: `False`.

    Returns:
      A function that when called will execute the step defined by
      `feed_list` and `fetches` in this session.

    Raises:
      TypeError: If `fetches` or `feed_list` cannot be interpreted
        as arguments to `tf.Session.run`.
    NzBArgument `feed_list` must be a list or tuple. Received: feed_list=c                     s*   dd t  | D }jfd|i|S )Nc                 S   s   i | ]\}}||qS r   r   )rX   r:   r;   r   r   r   
<dictcomp>  s   zCBaseSession.make_callable.<locals>._generic_run.<locals>.<dictcomp>r   )r5   r   )Z	feed_argskwargsr   )r  r   r   r   r   _generic_run  s    z/BaseSession.make_callable.<locals>._generic_runc                 S   s   g | ]}|  qS r   r  r   r   r   r   rZ     r?   z-BaseSession.make_callable.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r   r  r  r   r   r   rZ     r?   c           	   	      s   |rt t| nd}|r(t  nd}zr |i | ||}|rR| |}n|r^|d nd}|rt |}|	t| W |rt 
| |rt 
| n|rt 
| |rt 
| 0 |S )z:Template callable that accepts RunOptions and RunMetadata.Nr   )r   r  r   r   r  r  _call_tf_sessionrunr`   r  r  r  )	r  r  r$  r    r!   r	  r
  r   r  r   r   r   ,_callable_template_with_options_and_metadata  s4    



zOBaseSession.make_callable.<locals>._callable_template_with_options_and_metadatar,   c                      s     d i g d  d S r3   r:  r   r   r  r   r   _single_operation_run  s    z8BaseSession.make_callable.<locals>._single_operation_runc                     s    d i  g d } | d S rE   r<  r   )r  r   r   r   _single_tensor_run  s    z5BaseSession.make_callable.<locals>._single_tensor_runc                     s    d i d }  | S r3   )r:  r`   r?  )r$  r  r   r  r   r   _fetch_handler_run$  s    z5BaseSession.make_callable.<locals>._fetch_handler_run)NN)rf   r4   rg   rd   r  r   r   r   r   r   r   r   r   r0   r   )	r   r   r  Zaccept_optionsr9  r;  r>  r@  rA  r   )r  r$  r  r   r   r  r   make_callable  s<    $  zBaseSession.make_callablez.\[\[(Node: )?(\{\{node )?([^\} ]*)(\}\})?\s*=*c                    s|   t dd | D }dd |D }dd D }	 fdd}
 fdd	}|d
u rh |
|||	||S  ||||S d
S )a+  Runs a step based on the given fetches and feeds.

    Args:
      handle: a handle for partial_run. None if this is just a call to run().
      target_list: A list of operations to be run, but not fetched.
      fetch_list: A list of tensors to be fetched.
      feed_dict: A dictionary that maps tensors to numpy ndarrays.
      options: A (pointer to a) [`RunOptions`] protocol buffer, or None
      run_metadata: A (pointer to a) [`RunMetadata`] protocol buffer, or None

    Returns:
      A list of numpy ndarrays, corresponding to the elements of
      `fetch_list`.  If the ith element of `fetch_list` contains the
      name of an operation, the first Tensor output of that operation
      will be returned for that element.

    Raises:
      tf.errors.OpError: Or one of its subclasses on error.
    c                 s   s"   | ]\}}|   |fV  qd S r3   )derefr  )rX   r   vr   r   r   	<genexpr>H  r?   z&BaseSession._do_run.<locals>.<genexpr>c                 S   s   g | ]}|  qS r   r  r   r   r   r   rZ   I  r?   z'BaseSession._do_run.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r   r  r  r   r   r   rZ   J  r?   c                    s        || |||S r3   )r  r:  )r   r  r  r    r!   r   r   r   _run_fnN  s    
z$BaseSession._do_run.<locals>._run_fnc                    s"   rt d d | ||S )NzBpartial_run() requires empty `target_list`. Received: target_list=z (non-empty))r  _call_tf_sessionprun)r&   r   r  r=  r   r   _prun_fnT  s
    
z%BaseSession._do_run.<locals>._prun_fnN)dictr*  r!  )r   r&   r  r  r   r    r!   r#   r   r   rF  rH  r   r=  r   r4  2  s    zBaseSession._do_runc           	      G   s   z
|| W S  t jy } zt|j}tj|}d }d }|d urx|d}z| j	
|}|j}W n tyv   Y n0 t|| j	}d|v r|d7 }t||||W Y d }~n
d }~0 0 d S )Nr-   z only supports NHWC tensor formatz
A possible workaround: Try disabling Grappler optimizer
by modifying the config for creating the session eg.
session_config.graph_options.rewrite_options.disable_meta_optimizer = True)r	   OpErrorr   as_textr  r   _NODEDEF_NAME_REsearchgroupr   get_operation_by_namenode_defry   r   interpolatere   )	r   fnr   r{   r  r   rP  r   	node_namer   r   r   r!  `  s$    


zBaseSession._do_callc                 C   s:   | j   t| j W d    n1 s,0    Y  d S r3   )r   _session_run_lockr   ExtendSessionr   r   r   r   r   r  w  s    zBaseSession._extend_graph
   c           	      C   s   d }| j 8 | j| t| jtjkr4| j}g | _W d    n1 sH0    Y  |ri }g }t|D ].\}}t| j	||\}}|||< || qf| j
||d d S )Nr   )r   r   rv   r0   r   _DEAD_HANDLES_THRESHOLD	enumerater   Z_get_handle_deleterr   r   )	r   r&   Ztensors_to_deleter#   r   Zdeleter_keyZtensor_handleholderdeleterr   r   r   _register_dead_handle~  s     $z!BaseSession._register_dead_handlec                 C   s   g }|  D ]4\}}tj| jg|R  }|r|||d |f q|sJg S i }g }|D ]$\}	}
}|
||d < ||d  qV| j||d}t||D ]:\}}
tj|
j	tj
d}|d }|| d }||| < q|S d S )Nr,   r   rW  r(  )r*  r   Z_get_handle_moverr   rv   r   r5   r-  arrayr&   object_r   )r   r   r5  Zhandle_moversZ	feed_namevalZmoverr#   r   rm   r&   handlesZhandle_moverr6  Zfeed_tensorr   r   r   r3    s&    zBaseSession._update_with_moversc                 C   s   t | j|||||S r3   )r   TF_SessionRun_wrapperr   )r   r    r   r  r  r!   r   r   r   r:    s    zBaseSession._call_tf_sessionrunc                 C   s   t | j|||S r3   )r   TF_SessionPRun_wrapperr   )r   r&   r   r  r   r   r   rG    s    z BaseSession._call_tf_sessionprunc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	zBaseSession._Callablez?Experimental wrapper for the C++ `Session::MakeCallable()` API.c              	   C   sP   || _ d | _tt| }zt|j || _W t| nt| 0 d S r3   )	r   _handler   r  r   r   r  TF_SessionMakeCallabler  )r   r   callable_optionsr	  r   r   r   r|     s    zBaseSession._Callable.__init__c              	   O   sx   | dd }zV|rt nd }t| jj| j||}|rRt|}|t	| W |rtt
| n|rrt
| 0 |S )Nr!   )r   r   r  TF_SessionRunCallabler   rc  r  r  r   r   r  )r   r   r8  r!   r
  retr  r   r   r   __call__  s    

zBaseSession._Callable.__call__c                 C   s4   | j d ur0| jjd ur0| jjs0t| jj| j  d S r3   )rc  r   r   r   TF_SessionReleaseCallabler   r   r   r   r     s    
zBaseSession._Callable.__del__N)r'   r(   r)   r*   r|   rh  r   r   r   r   r   	_Callable  s   rj  c                 C   s   |    t| |S )a	  Returns a handle to a "callable" with the given options.

    Args:
      callable_options: A `CallableOptions` protocol buffer message describing
        the computation that will be performed by the callable.

    Returns:
      A handle to the new callable.
    )r  r   rj  )r   re  r   r   r   _make_callable_from_options  s    
z'BaseSession._make_callable_from_options)r   NN)NNN)N)N)NF) r'   r(   r)   r*   r|   r   r   r   r+   r   r   r   r   r   r$   r"   r  rB  recompilerL  r4  r!  r  rX  r\  r3  r:  rG  rF   rj  rk  r   r   r   r   r   z  s>   
L&

	
7
y
/
M]
 .(r   Session)v1c                       s@   e Zd ZdZd fdd	Zdd Zdd	 Zedd
dZ  Z	S )rn  a  A class for running TensorFlow operations.

  A `Session` object encapsulates the environment in which `Operation`
  objects are executed, and `Tensor` objects are evaluated. For
  example:

  ```python
  tf.compat.v1.disable_eager_execution() # need to disable eager in TF2.x
  # Build a graph.
  a = tf.constant(5.0)
  b = tf.constant(6.0)
  c = a * b

  # Launch the graph in a session.
  sess = tf.compat.v1.Session()

  # Evaluate the tensor `c`.
  print(sess.run(c)) # prints 30.0
  ```

  A session may own resources, such as
  `tf.Variable`, `tf.queue.QueueBase`,
  and `tf.compat.v1.ReaderBase`. It is important to release
  these resources when they are no longer required. To do this, either
  invoke the `tf.Session.close` method on the session, or use
  the session as a context manager. The following two examples are
  equivalent:

  ```python
  # Using the `close()` method.
  sess = tf.compat.v1.Session()
  sess.run(...)
  sess.close()

  # Using the context manager.
  with tf.compat.v1.Session() as sess:
    sess.run(...)
  ```

  The
  [`ConfigProto`](https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto)
  protocol buffer exposes various configuration options for a
  session. For example, to create a session that uses soft constraints
  for device placement, and log the resulting placement decisions,
  create a session as follows:

  ```python
  # Launch the graph in a session that allows soft device placement and
  # logs the placement decisions.
  sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(
      allow_soft_placement=True,
      log_device_placement=True))
  ```

  @compatibility(TF2)
  `Session` does not work with either eager execution or `tf.function`, and you
  should not invoke it directly. To migrate code that uses sessions to TF2,
  rewrite the code without it. See the
  [migration
  guide](https://www.tensorflow.org/guide/migrate#1_replace_v1sessionrun_calls)
  on replacing `Session.run` calls.
  @end_compatibility
  r   Nc                    s&   t t| j|||d d| _d| _dS )a  Creates a new TensorFlow session.

    If no `graph` argument is specified when constructing the session,
    the default graph will be launched in the session. If you are
    using more than one graph (created with `tf.Graph()`) in the same
    process, you will have to use different sessions for each graph,
    but each graph can be used in multiple sessions. In this case, it
    is often clearer to pass the graph to be launched explicitly to
    the session constructor.

    Args:
      target: (Optional.) The execution engine to connect to. Defaults to using
        an in-process engine. See
        [Distributed TensorFlow](https://tensorflow.org/deploy/distributed) for
          more examples.
      graph: (Optional.) The `Graph` to be launched (described above).
      config: (Optional.) A
        [`ConfigProto`](https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto)
          protocol buffer with configuration options for the session.
    )r   N)superrn  r|   _default_graph_context_manager _default_session_context_manager)r   r   r   r   rT   r   r   r|   /  s    zSession.__init__c                 C   sH   | j d u r| j | _ ntd| jd u r4|  | _| j   | j S )NzvSession context managers are not re-entrant. Use `Session.as_default()` if you want to enter a session multiple times.)rq  r   r   r  rr  	__enter__r   r   r   r   rt  I  s    



zSession.__enter__c              
   C   s   |t ju rtd|f z| j||| W n0 ty\ } z||krFn W Y d }~n
d }~0 0 | j||| d | _d | _|rtj	d| j
d}d|_|  |d | rtd n| 
  d S )Nz"Session closing due to OpError: %sZSessionCloseThread)rW   r   Tg      >@zsSession failed to close after 30 seconds. Continuing after this point may leave your program in an undefined state.)r	   rJ  loggingerrorrr  __exit__r  rq  r   Threadr   daemonstartjoinis_alive)r   	exec_typeZ
exec_valueZexec_tbrv  Zclose_threadr   r   r   rw  U  s2    

	
zSession.__exit__c                 C   s@   | durt | } |dur*dd |D }ng }t| || dS )a  Resets resource containers on `target`, and close all connected sessions.

    A resource container is distributed across all workers in the
    same cluster as `target`.  When a resource container on `target`
    is reset, resources associated with that container will be cleared.
    In particular, all Variables in the container will become undefined:
    they lose their values and shapes.

    NOTE:
    (i) reset() is currently only implemented for distributed sessions.
    (ii) Any sessions on the master named by `target` will be closed.

    If no resource containers are provided, all containers are reset.

    Args:
      target: The execution engine to connect to.
      containers: A list of resource container name strings, or `None` if all of
        all the containers are to be reset.
      config: (Optional.) Protocol buffer with configuration options.

    Raises:
      tf.errors.OpError: Or one of its subclasses if an error occurs while
        resetting containers.
    Nc                 S   s   g | ]}t |qS r   )r   r   )rX   cr   r   r   rZ     r?   z!Session.reset.<locals>.<listcomp>)r   r   r   TF_Reset)r   
containersr   r   r   r   reset}  s    
zSession.reset)r   NN)NN)
r'   r(   r)   r*   r|   rt  rw  rp   r  __classcell__r   r   rs  r   rn    s   @(InteractiveSessionc                       s:   e Zd ZdZe ZdZd	 fdd	Z fddZ	  Z
S )
r  a  A TensorFlow `Session` for use in interactive contexts, such as a shell.

  The only difference with a regular `Session` is that an `InteractiveSession`
  installs itself as the default session on construction.
  The methods `tf.Tensor.eval`
  and `tf.Operation.run`
  will use that session to run ops.

  This is convenient in interactive shells and [IPython
  notebooks](http://ipython.org), as it avoids having to pass an explicit
  `Session` object to run ops.

  For example:

  ```python
  sess = tf.compat.v1.InteractiveSession()
  a = tf.constant(5.0)
  b = tf.constant(6.0)
  c = a * b
  # We can just use 'c.eval()' without passing 'sess'
  print(c.eval())
  sess.close()
  ```

  Note that a regular session installs itself as the default session when it
  is created in a `with` statement.  The common usage in non-interactive
  programs is to follow that pattern:

  ```python
  a = tf.constant(5.0)
  b = tf.constant(6.0)
  c = a * b
  with tf.compat.v1.Session():
    # We can also use 'c.eval()' here.
    print(c.eval())
  ```
  r   r   Nc                    s   |st jdd}t j|d}d|j_tt| ||| tj2 tj	dkrTt
d t j	d7  _	W d   n1 sv0    Y  d| _|  | _d| j_| j  || _| jdur| | _d| j_| j  dS )	a  Creates a new interactive TensorFlow session.

    If no `graph` argument is specified when constructing the session,
    the default graph will be launched in the session. If you are
    using more than one graph (created with `tf.Graph()`) in the same
    process, you will have to use different sessions for each graph,
    but each graph can be used in multiple sessions. In this case, it
    is often clearer to pass the graph to be launched explicitly to
    the session constructor.

    Args:
      target: (Optional.) The execution engine to connect to. Defaults to using
        an in-process engine.
      graph: (Optional.) The `Graph` to be launched (described above).
      config: (Optional) `ConfigProto` proto used to configure the session.
    T)allow_growth)gpu_optionsr   zAn interactive session is already active. This can cause out-of-memory errors in some cases. You must explicitly call `InteractiveSession.close()` to release resources held by the other session(s).r,   NF)r   
GPUOptionsr   r   place_pruned_graphrp  r  r|   _count_lock_active_session_countwarningswarn_explicitly_closedr   _default_sessionenforce_nestingrt  _explicit_graph_default_graph)r   r   r   r   r  rs  r   r   r|     s$    

,



zInteractiveSession.__init__c                    s   t t|   tj< | js2t jd8  _d| _nW d   dS W d   n1 sV0    Y  | jdur| jddd d| _| j	ddd d| _	dS )zCloses an `InteractiveSession`.r,   TN)
rp  r  r   r  r  r  r  r  rw  r  r   rs  r   r   r     s    .
zInteractiveSession.close)r   NN)r'   r(   r)   r*   r   r   r  r  r|   r   r  r   r   rs  r   r    s
   &2)NN)@r*   r   r   rl  r   r  numpyr-  r   tensorflow.core.protobufr   r   tensorflow.python.clientr   r   tensorflow.python.eagerr   r   tensorflow.python.frameworkr   r   r	   r
   r   r   tensorflow.python.opsr   tensorflow.python.platformr   ru  Z'tensorflow.python.training.experimentalr   tensorflow.python.utilr   r   tensorflow.python.util.compatr    tensorflow.python.util.tf_exportr   Counterr   rF   r   r2   r<   SparseTensorIndexedSlicesrK   rJ   rP   rV   r\   r]   rl   r   rh   rj   rk   r   r   r   r   rn  r  r   r   r   r   <module>   s   !
  
7I/$""{*      y
 3
