
    AVhu                       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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 ddlmZ ddlmZ ddlmZ d Z	 	 dEdZd Z d Z! edg d       ejD                  ddg       G d d                    Z#d Z$ ed g d!       ejD                  d"d#g       G d$ d#e#                    Z% ed%d%d&g       ejD                  d&       G d' d&e#                    Z& G d( d)e#      Z' ed*g d+       ejD                  d,d-g       G d. d-e#                    Z( ed/g d0       ejD                  d1d2g       G d3 d2e#                    Z) G d4 d5      Z* ed6g       G d7 d6             Z+ ed8g       G d9 d8e+             Z, ed:d;g       G d< d;e+             Z- G d= d>      Z. G d? d@e.      Z/ G dA dBe.      Z0 G dC dD      Z1y)FzData Flow Operations.    N)contextdtypes)indexed_slices)ops)random_seed)tensor_shape)tensor_util)	python_io)	array_ops)array_ops_stack)control_flow_ops)gen_data_flow_ops)math_ops)resource_variable_ops)*)deprecation)collections_abc)	tf_exportc                 f    | J t        | t              st        | t              s| gS t        |       S )z"Convert dtypes to a list of types.)
isinstancelisttupler   s    S/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/data_flow_ops.py_as_type_listr   -   s4    			
VT
"j&?8O <    c                    ~|r9t        | t        j                        r| rt        d | D              rt	        d      | yt        | t
        j                        r| g} t        | t        t        f      st        dt        |        d      t        d | D              r| g} | D cg c]  }t        j                  |       } }|s t        d | D              rt	        d|        |s t        d	 | D              rt	        d
|        | S c c}w )z4Convert shapes to a list of tuples of int (or None).c              3   H   K   | ]  }|d u xs t        |t                y wNr   int.0shapes     r   	<genexpr>z!_as_shape_list.<locals>.<genexpr>@   s#     HETM3Zs33H    "zAWhen providing partial shapes, a list of shapes must be provided.NzEShapes must be a TensorShape or a list or tuple of TensorShapes, got z	 instead.c              3   H   K   | ]  }|d u xs t        |t                y wr   r    r"   s     r   r%   z!_as_shape_list.<locals>.<genexpr>K   s#     EU$	0*UC0	0Er&   c              3   >   K   | ]  }|j                            y wr   )is_fully_definedr"   s     r   r%   z!_as_shape_list.<locals>.<genexpr>P   s     
<Eu%%''
<s   z"All shapes must be fully defined: c              3   8   K   | ]  }|j                   d u   y wr   )dimsr"   s     r   r%   z!_as_shape_list.<locals>.<genexpr>S   s     
2%5::
2s   z%All shapes must have a defined rank: )r   r   Sequenceany
ValueErrorr	   TensorShaper   r   	TypeErrortypeallas_shape)shapesr   unknown_dim_allowedunknown_rank_allowedr$   s        r   _as_shape_listr7   8   s   
 v778HHH
MO O^001XF	FUDM	*
	F|nI	'( ( 	EfEEXF6<=UL!!%(=&=	

<V
<<;F8DEE	

26
22>vhGHH	- >s   $Dc                     | y t        | t        t        f      s| g} t        |       t        |      k7  r#t	        dt        |        dt        |             t        |       S )NzSList of names must have the same length as the list of dtypes, received len(names)=z,len(dtypes)=)r   r   r   lenr.   )namesr   s     r   _as_name_listr;   Y   sj    
]	ED%=	)GEZ3v;
 77:5zl C$$'K=2 3 3 
er   c                    t        j                  |       } t        j                  |      }| j                  %|j                  | j                  |j                  k7  rt        j                         S t	        | j                         |j                               D cg c]  \  }}|||k(  r|nd }}}t        j                  |      S c c}}w )z>The greatest lower bound (ordered by specificity) TensorShape.N)r	   r/   ndimsunknown_shapezipas_list)s1s2d1d2ds        r   _shape_commonrF   e   s    #"#"XX)RXX-A%%'' "**,

5
2r NrRxbT1!  
	!	!!	$$	s   Cqueue.QueueBase)rG   io.QueueBase	QueueBase)v1rH   rI   c                       e Zd ZdZd Zed        Zed        Zed        Z	ed        Z
ed        Zed        Zd	 Zd
 ZddZddZd ZddZddZddZddZddZddZy)rI   a  Base class for queue implementations.

  A queue is a TensorFlow data structure that stores tensors across
  multiple steps, and exposes operations that enqueue and dequeue
  tensors.

  Each queue element is a tuple of one or more tensors, where each
  tuple component has a static dtype, and may have a static shape. The
  queue implementations support versions of enqueue and dequeue that
  handle single elements, versions that support enqueuing and
  dequeuing a batch of elements at once.

  See `tf.queue.FIFOQueue` and
  `tf.queue.RandomShuffleQueue` for concrete
  implementations of this class, and instructions on how to create
  them.
  c                 b   || _         |ct        |      t        |      k7  r#t        dt        |       dt        |             |D cg c]  }t        j                  |       c}| _        n1| j                   D cg c]  }t        j                          c}| _        |Bt        |      t        |      k7  r#t        dt        |       dt        |             || _        nd| _        || _        t        |t        j                        ret        j                         j                  r$t        j                         j                  | _        nd| _        t        j                   |d      | _        y| j                  j$                  j&                  j)                  d      d   | _        yc c}w c c}w )	a$  Constructs a queue object from a queue reference.

    The two optional lists, `shapes` and `names`, must be of the same length
    as `dtypes` if provided.  The values at a given index `i` indicate the
    shape and name to use for the corresponding queue component in `dtypes`.

    Args:
      dtypes:  A list of types.  The length of dtypes must equal the number
        of tensors in each element.
      shapes: Constraints on the shapes of tensors in an element:
        A list of shape tuples or None. This list is the same length
        as dtypes.  If the shape of any tensors in the element are constrained,
        all must be; shapes can be None if the shapes should not be constrained.
      names: Optional list of names.  If provided, the `enqueue()` and
        `dequeue()` methods will use dictionaries with these names as keys.
        Must be None or a list or tuple of the same length as `dtypes`.
      queue_ref: The queue reference, i.e. the output of the queue op.

    Raises:
      ValueError: If one of the arguments is invalid.
    NzGQueue shapes must have the same length as dtypes, received len(shapes)=z, len(dtypes)=zDQueue names must have the same length as dtypes,received len(names)=z,len Empty/)_dtypesr9   r.   r	   r/   _shapesr>   _names
_queue_refr   r   EagerTensorr   
scope_name_namer   EagerResourceDeleter_resource_deleteropnamesplit)selfr   r4   r:   	queue_refs_s          r   __init__zQueueBase.__init__   sl   , DL	VF	# 114V >((+F}6 7 	7 <BBal..q1Bdl<@LLIql002Idl	Us6{	" 003E
| <  #F}. / 	/ dkdkDO)S__-			%	%__&11

4II
T d ??%%**005b9dj) CIs   F';F,c                 v  	
 |r"t        |t              rt        d |D              st        d      |d   j                  	t        	fd|dd D              st        d      |d   j
                  
t        
fd|dd D              st        d	      |D cg c]  }|j                   }}t        | D cg c]  }t        j                  t        |       }}t        j                  |D cg c]  }|j                   c}      }t        j                  ||       }t!        	|
|
      S c c}w c c}w c c}w )a  Create a queue using the queue reference from `queues[index]`.

    Args:
      index: An integer scalar tensor that determines the input that gets
        selected.
      queues: A list of `QueueBase` objects.

    Returns:
      A `QueueBase` object.

    Raises:
      TypeError: When `queues` is not a list of `QueueBase` objects,
        or when the data types of `queues` are not all the same.
    c              3   <   K   | ]  }t        |t                y wr   )r   rI   )r#   xs     r   r%   z&QueueBase.from_list.<locals>.<genexpr>   s     :aAy):   zA list of queues expectedr   c              3   <   K   | ]  }|j                   k(    y wr   r   )r#   qr   s     r   r%   z&QueueBase.from_list.<locals>.<genexpr>   s     6av!6      Nz-Queues do not have matching component dtypes.c              3   <   K   | ]  }|j                   k(    y wr   )r:   )r#   rf   r:   s     r   r%   z&QueueBase.from_list.<locals>.<genexpr>   s     4Au4rg   z,Queues do not have matching component names.)r   r4   r:   r]   )r   r   r2   r0   r   r:   r4   r?   	functoolsreducerF   r   stackr]   r   gatherrI   )indexqueuesrf   queue_shapesr^   reduced_shapesrc   
queue_refsselected_queuer   r:   s            @@r   	from_listzQueueBase.from_list   s&     Z5:6::122AYF66!":66EFF1IOOE444DEE&,-AHH-L-474F/0	*N  !&&V'D'DEJ%%j%8N 	" " . (Es   D,7!D1-D6c                     | j                   S )zThe underlying queue reference.)rS   r\   s    r   r]   zQueueBase.queue_ref   s     ??r   c                     t        j                         r| j                  S | j                  j                  j
                  S )z!The name of the underlying queue.)r   executing_eagerlyrV   rS   rY   rZ   rv   s    r   rZ   zQueueBase.name   s0       "ZZ??"""r   c                     | j                   S )z9The list of dtypes for each component of a queue element.rP   rv   s    r   r   zQueueBase.dtypes        <<r   c                     | j                   S )z9The list of shapes for each component of a queue element.rQ   rv   s    r   r4   zQueueBase.shapes   r{   r   c                     | j                   S )z8The list of names for each component of a queue element.rR   rv   s    r   r:   zQueueBase.names        ;;r   c           	         t        |t              r| j                  st        d      t	        | j                  t
              t	        |j                         t
              k7  r;t        dt	        |j                                dt	        | j                               | j                  D cg c]  }||   	 }}n0| j                  rt        d      t        |t        t        f      s|g}g }t        t        || j                              D ]2  \  }\  }}|j                  t        j                  ||d|z               4 |S c c}w )aH  Validate and convert `vals` to a list of `Tensor`s.

    The `vals` argument can be a Tensor, a list or tuple of tensors, or a
    dictionary with tensor values.

    If it is a dictionary, the queue must have been constructed with a
    `names` attribute and the dictionary keys must match the queue names.
    If the queue was constructed with a `names` attribute, `vals` must
    be a dictionary.

    Args:
      vals: A tensor, a list or tuple of tensors, or a dictionary..

    Returns:
      A list of `Tensor` objects.

    Raises:
      ValueError: If `vals` is invalid.
    z-Queue must have names to enqueue a dictionary)keyzHKeys in dictionary to enqueue do not match names of Queue.  Dictionary: z,Queue: z3You must enqueue a dictionary in a Queue with namescomponent_%ddtyperZ   )r   dictrR   r.   sortedstrkeysr   r   	enumerater?   rP   appendr   convert_to_tensor)r\   valsktensorsivalr   s          r   _check_enqueue_dtypeszQueueBase._check_enqueue_dtypes   s!   ( $[[HII		%		)E	E 99?		9L8M N##)$++#6"79 : 	:
  ${{+!d1g+d+	NOOtUm,vG$St||%<= L<Cnn


5~7I
JLL N ,s   ,Ec                 x    t        |t        t        f      r|S t        |t              r|j	                         S |gS zReturn a list of values to pass to `name_scope()`.

    Args:
      vals: A tensor, a list or tuple of tensors, or a dictionary.

    Returns:
      The values in vals as a list.
    r   r   r   r   valuesr\   r   s     r   _scope_valszQueueBase._scope_vals*  4     $u&k	D$	[[]Vmr   Nc                    t        j                  |d| j                  z  | j                  |            5 }| j	                  |      }t        || j                        D ]$  \  }}|j                         j                  |       & | j                  j                  t        j                  k(  r+t        j                  | j                  ||      cddd       S t        j                  | j                  ||      cddd       S # 1 sw Y   yxY w)a  Enqueues one element to this queue.

    If the queue is full when this operation executes, it will block
    until the element has been enqueued.

    At runtime, this operation may raise an error if the queue is
    `tf.QueueBase.close` before or during its execution. If the
    queue is closed before this operation runs,
    `tf.errors.CancelledError` will be raised. If this operation is
    blocked, and either (i) the queue is closed by a close operation
    with `cancel_pending_enqueues=True`, or (ii) the session is
    `tf.Session.close`,
    `tf.errors.CancelledError` will be raised.

    >>> q = tf.queue.FIFOQueue(capacity=3, dtypes=tf.int32)
    >>> q.enqueue(1)
    >>> q.enqueue(2)
    >>> q.size()
    <tf.Tensor: shape=(), dtype=int32, numpy=2>

    >>> q = tf.queue.FIFOQueue(2, tf.int32, shapes=tf.TensorShape(4))
    >>> q.enqueue(tf.constant([1, 2, 3, 4], dtype=tf.int32))
    >>> q.size()
    <tf.Tensor: shape=(), dtype=int32, numpy=1>

    Args:
      vals: A tensor, a list or tuple of tensors, or a dictionary containing
        the values to enqueue.
      name: A name for the operation (optional).

    Returns:
      The operation that enqueues a new tuple of tensors to the queue.
    z
%s_enqueuerZ   N)r   
name_scoperV   r   r   r?   rQ   	get_shapeassert_is_compatible_withrS   r   rP   resourcer   queue_enqueue_v2queue_enqueue)r\   r   rZ   scoper   r$   s         r   enqueuezQueueBase.enqueue:  s    D 
lTZZ7((.
0 /38''-d D$,,/ 9*#u11%89 
		'"2"2	2 11OOT// / !..OOT// / /s   BD !D  D	c                    t        j                  |d| j                  z  | j                  |            5 }| j	                  |      }t        j                  |d   j                         j                  d      d         }t        j                  |      }t        || j                        D ]  \  }}t        j                  |j                         j                  d      d         }t        j                  |      }|j                  |      }|j                         dd j                  |        t        j                  | j                   ||      cddd       S # 1 sw Y   yxY w)a  Enqueues zero or more elements to this queue.

    This operation slices each component tensor along the 0th dimension to
    make multiple queue elements. All of the tensors in `vals` must have the
    same size in the 0th dimension.

    If the queue is full when this operation executes, it will block
    until all of the elements have been enqueued.

    At runtime, this operation may raise an error if the queue is
    `tf.QueueBase.close` before or during its execution. If the
    queue is closed before this operation runs,
    `tf.errors.CancelledError` will be raised. If this operation is
    blocked, and either (i) the queue is closed by a close operation
    with `cancel_pending_enqueues=True`, or (ii) the session is
    `tf.Session.close`,
    `tf.errors.CancelledError` will be raised.

    >>> q = tf.queue.FIFOQueue(capacity=10, dtypes=tf.int32)
    >>> q.enqueue_many(tf.constant([1, 2, 3, 4, 5], dtype=tf.int32))
    >>> q.size()
    <tf.Tensor: shape=(), dtype=int32, numpy=5>

    Args:
      vals: A tensor, a list or tuple of tensors, or a dictionary
        from which the queue elements are taken.
      name: A name for the operation (optional).

    Returns:
      The operation that enqueues a batch of tuples of tensors to the queue.
    z%s_EnqueueManyr   rh   Nr   )r   r   rV   r   r   r	   dimension_valuer   with_rank_at_least	Dimensionr?   rQ   
merge_withr   r   queue_enqueue_many_v2rS   )r\   r   rZ   r   	batch_dimr   r$   val_batch_dims           r   enqueue_manyzQueueBase.enqueue_manyl  s4   @ 
.;((.
0 -38''-d ..
q'



0
0
3A
68i((3iD$,,/ =*#u$44MMO..q1!46$..}=((7	55e<= 44
//4e-%- - -s   DEE"c                     | j                   r,t        | j                         D ci c]  \  }}|||    c}}S t        |      dk(  r|d   S |S c c}}w )a  Return the value to return from a dequeue op.

    If the queue has names, return a dictionary with the
    names as keys.  Otherwise return either a single tensor
    or a list of tensors depending on the length of `tensors`.

    Args:
      tensors: List of tensors from the dequeue op.

    Returns:
      A single tensor, a list of tensors, or a dictionary
      of tensors.
    rh   r   )rR   r   r9   )r\   r   r   ns       r   _dequeue_return_valuezQueueBase._dequeue_return_value  sT     {{ )2$++(>?1am??	W	QZn	 @s   Ac                    |d| j                   z  }| j                  j                  t        j                  k(  r-t        j                  | j                  | j                  |      }n,t        j                  | j                  | j                  |      }t        j                         sL|d   j                  }t        |j                         | j                        D ]  \  }}|j                  |        | j                  |      S )a  Dequeues one element from this queue.

    If the queue is empty when this operation executes, it will block
    until there is an element to dequeue.

    At runtime, this operation may raise an error if the queue is
    `tf.QueueBase.close` before or during its execution. If the
    queue is closed, the queue is empty, and there are no pending
    enqueue operations that can fulfill this request,
    `tf.errors.OutOfRangeError` will be raised. If the session is
    `tf.Session.close`,
    `tf.errors.CancelledError` will be raised.

    >>> q = tf.queue.FIFOQueue(capacity=2, dtypes=tf.int32)
    >>> q.enqueue(1)
    >>> q.enqueue(2)
    >>> q.dequeue()
    <tf.Tensor: shape=(), dtype=int32, numpy=1>
    >>> q.dequeue()
    <tf.Tensor: shape=(), dtype=int32, numpy=2>

    Args:
      name: A name for the operation (optional).

    Returns:
      The tuple of tensors that was dequeued.
    z
%s_Dequeuer   r   )rV   rS   r   rP   r   r   queue_dequeue_v2queue_dequeuer   rx   rY   r?   r   rQ   	set_shaper   )r\   rZ   retrY   outputr$   s         r   dequeuezQueueBase.dequeue  s    8 |DJJ&d 0 00..
//4<<d4c ++
//4<<d4c
 $$&q699bryy{DLL9  -&%  %%c**r   c                    |d| j                   z  }t        j                  | j                  || j                  |      }t        j                         s|d   j                  }t        j                  t        j                  |j                  d               }t        |j                         | j                        D ]9  \  }}|j!                  t        j"                  |g      j%                  |             ; | j'                  |      S )a  Dequeues and concatenates `n` elements from this queue.

    This operation concatenates queue-element component tensors along
    the 0th dimension to make a single component tensor.  All of the
    components in the dequeued tuple will have size `n` in the 0th dimension.

    If the queue is closed and there are less than `n` elements left, then an
    `OutOfRange` exception is raised.

    At runtime, this operation may raise an error if the queue is
    `tf.QueueBase.close` before or during its execution. If the
    queue is closed, the queue contains fewer than `n` elements, and
    there are no pending enqueue operations that can fulfill this
    request, `tf.errors.OutOfRangeError` will be raised. If the
    session is `tf.Session.close`,
    `tf.errors.CancelledError` will be raised.

    >>> q = tf.queue.FIFOQueue(10, tf.int32, shapes=tf.TensorShape(2))
    >>> q.enqueue(tf.constant([1, 2], dtype=tf.int32, shape=(2)))
    >>> q.enqueue(tf.constant([3, 4], dtype=tf.int32, shape=(2)))
    >>> q.enqueue(tf.constant([5, 6], dtype=tf.int32, shape=(2)))
    >>> q.enqueue(tf.constant([7, 8], dtype=tf.int32, shape=(2)))
    >>> q.dequeue_many(3)
    <tf.Tensor: shape=(3, 2), dtype=int32, numpy=
    array([[1, 2],
       [3, 4],
       [5, 6]], dtype=int32)>

    Args:
      n: A scalar `Tensor` containing the number of elements to dequeue.
      name: A name for the operation (optional).

    Returns:
      The list of concatenated tensors that was dequeued.
    z%s_DequeueManyr   component_typesrZ   r   rh   )rV   r   queue_dequeue_many_v2rS   rP   r   rx   rY   r	   r   r
   constant_valueinputsr?   r   rQ   r   r/   concatenater   )r\   r   rZ   r   rY   r   r   r$   s           r   dequeue_manyzQueueBase.dequeue_many  s    H |

*d

1
11dllGC
 $$&q699b((

$
$RYYq\
24iryy{DLL9 F-&%$$i[1==eD	FF %%c**r   c                    |d| j                   z  }t        j                  | j                  || j                  |      }t        j                         so|d   j                  }t        |j                         | j                        D ]9  \  }}|j                  t        j                  dg      j                  |             ; | j                  |      S )a  Dequeues and concatenates `n` elements from this queue.

    **Note** This operation is not supported by all queues.  If a queue does not
    support DequeueUpTo, then a `tf.errors.UnimplementedError` is raised.

    This operation concatenates queue-element component tensors along
    the 0th dimension to make a single component tensor. If the queue
    has not been closed, all of the components in the dequeued tuple
    will have size `n` in the 0th dimension.

    If the queue is closed and there are more than `0` but fewer than
    `n` elements remaining, then instead of raising a
    `tf.errors.OutOfRangeError` like `tf.QueueBase.dequeue_many`,
    less than `n` elements are returned immediately.  If the queue is
    closed and there are `0` elements left in the queue, then a
    `tf.errors.OutOfRangeError` is raised just like in `dequeue_many`.
    Otherwise the behavior is identical to `dequeue_many`.

    >>> q = tf.queue.FIFOQueue(10, tf.int32, shapes=tf.TensorShape(2))
    >>> q.enqueue(tf.constant([1, 2], dtype=tf.int32, shape=(2)))
    >>> q.enqueue(tf.constant([3, 4], dtype=tf.int32, shape=(2)))
    >>> q.close()
    >>> q.dequeue_up_to(5)
    <tf.Tensor: shape=(2, 2), dtype=int32, numpy=
    array([[1, 2],
       [3, 4]], dtype=int32)>

    Args:
      n: A scalar `Tensor` containing the number of elements to dequeue.
      name: A name for the operation (optional).

    Returns:
      The tuple of concatenated tensors that was dequeued.
    Nz%s_DequeueUpTor   r   )rV   r   queue_dequeue_up_to_v2rS   rP   r   rx   rY   r?   r   rQ   r   r	   r/   r   r   )r\   r   rZ   r   rY   r   r$   s          r   dequeue_up_tozQueueBase.dequeue_up_to  s    F |

*d

2
21dllGC
 $$&q699bryy{DLL9 N-&%114&9EEeLMN %%c**r   c                     |d| j                   z  }| j                  j                  t        j                  k(  r"t        j                  | j                  ||      S t        j                  | j                  ||      S )a  Closes this queue.

    This operation signals that no more elements will be enqueued in
    the given queue. Subsequent `enqueue` and `enqueue_many`
    operations will fail. Subsequent `dequeue` and `dequeue_many`
    operations will continue to succeed if sufficient elements remain
    in the queue. Subsequently, dequeue and dequeue_many operations
    that would otherwise block waiting for more elements (if close
    hadn't been called) will now fail immediately.

    If `cancel_pending_enqueues` is `True`, all pending requests will also
    be canceled.

    >>> q = tf.queue.FIFOQueue(capacity=3, dtypes=tf.int32)
    >>> q.is_closed()
    <tf.Tensor: shape=(), dtype=bool, numpy=False>
    >>> q.close()
    >>> q.is_closed()
    <tf.Tensor: shape=(), dtype=bool, numpy=True>

    Args:
      cancel_pending_enqueues: (Optional.) A boolean, defaulting to `False`
        (described above).
      name: A name for the operation (optional).

    Returns:
      The operation that closes the queue.
    z%s_Closecancel_pending_enqueuesrZ   )rV   rS   r   rP   r   r   queue_close_v2queue_closer\   r   rZ   s      r   closezQueueBase.closeN  sq    : |$**$d 0 00--
//"9 
 **
//"9 r   c                     |d| j                   z  }| j                  j                  t        j                  k(  r!t        j                  | j                  |      S t        j                  | j                  |      S )a  Returns true if queue is closed.

    This operation returns true if the queue is closed and false if the queue
    is open.

    >>> q = tf.queue.FIFOQueue(capacity=3, dtypes=tf.int32)
    >>> q.is_closed()
    <tf.Tensor: shape=(), dtype=bool, numpy=False>

    Args:
      name: A name for the operation (optional).

    Returns:
      True if the queue is closed and false if the queue is open.
    z%s_Is_Closedr   )rV   rS   r   rP   r   r   queue_is_closed_v2queue_is_closed_r\   rZ   s     r   	is_closedzQueueBase.is_closedx  s]      |djj(d 0 0011$//MM//dKKr   c                     |d| j                   z  }| j                  j                  t        j                  k(  r!t        j                  | j                  |      S t        j                  | j                  |      S )a  Compute the number of elements in this queue.

    >>> q = tf.queue.FIFOQueue(capacity=10, dtypes=tf.int32)
    >>> q.enqueue_many(tf.constant([1, 2, 3, 4], dtype=tf.int32))
    >>> q.size()
    <tf.Tensor: shape=(), dtype=int32, numpy=4>

    Args:
      name: A name for the operation (optional).

    Returns:
      A scalar tensor containing the number of elements in this queue.
    z%s_Sizer   )rV   rS   r   rP   r   r   queue_size_v2
queue_sizer   s     r   sizezQueueBase.size  s]     |#d 0 00,,T__4HH))$//EEr   r   FN)__name__
__module____qualname____doc__r`   staticmethodrt   propertyr]   rZ   r   r4   r:   r   r   r   r   r   r   r   r   r   r   r    r   r   rI   rI   s   s    $0:d &" &"P   # #      )V 0/d3-j.,+\4+l0+d(TL.Fr   c                 h    t        j                         rt        t        j                               S | S r   )r   rx   r   r   uid)shared_names    r   _shared_namer     s$     swwy>	r   queue.RandomShuffleQueue)r   io.RandomShuffleQueueRandomShuffleQueuer   r   c                   .     e Zd ZdZ	 	 	 	 	 d fd	Z xZS )r   zA queue implementation that dequeues elements in a random order.

  See `tf.queue.QueueBase` for a description of the methods on
  this class.
  c	                    t        |      }t        ||      }t        ||      }t        j                  |      \  }	}
|	|
d\  }	}
nT|R|Pt        |	      |z   j                  d      }t        t        j                  |      j                         dd d      dz  }
t        j                  |||||	|
t        |      |      }t        t        | C  ||||       y)a"  Create a queue that dequeues elements in a random order.

    A `RandomShuffleQueue` has bounded capacity; supports multiple
    concurrent producers and consumers; and provides exactly-once
    delivery.

    A `RandomShuffleQueue` holds a list of up to `capacity`
    elements. Each element is a fixed-length tuple of tensors whose
    dtypes are described by `dtypes`, and whose shapes are optionally
    described by the `shapes` argument.

    If the `shapes` argument is specified, each component of a queue
    element must have the respective fixed shape. If it is
    unspecified, different queue elements may have different shapes,
    but the use of `dequeue_many` is disallowed.

    The `min_after_dequeue` argument allows the caller to specify a
    minimum number of elements that will remain in the queue after a
    `dequeue` or `dequeue_many` operation completes, to ensure a
    minimum level of mixing of elements. This invariant is maintained
    by blocking those operations until sufficient elements have been
    enqueued. The `min_after_dequeue` argument is ignored after the
    queue has been closed.

    Args:
      capacity: An integer. The upper bound on the number of elements
        that may be stored in this queue.
      min_after_dequeue: An integer (described above).
      dtypes:  A list of `DType` objects. The length of `dtypes` must equal
        the number of tensors in each queue element.
      shapes: (Optional.) A list of fully-defined `TensorShape` objects
        with the same length as `dtypes`, or `None`.
      names: (Optional.) A list of string naming the components in the queue
        with the same length as `dtypes`, or `None`.  If specified the dequeue
        methods return a dictionary with the names as keys.
      seed: A Python integer. Used to create a random seed. See
        `tf.compat.v1.set_random_seed`
        for behavior.
      shared_name: (Optional.) If non-empty, this queue will be shared under
        the given name across multiple sessions.
      name: Optional name for the queue operation.
    N)r   r   zutf-8      i)r   r4   capacitymin_after_dequeueseedseed2r   rZ   )r   r7   r;   r   get_seedr   encoder!   hashlibmd5	hexdigestr   random_shuffle_queue_v2r   superr   r`   )r\   r   r   r   r4   r:   r   r   rZ   seed1r   stringr]   	__class__s                r   r`   zRandomShuffleQueue.__init__  s    f 6"FFF+F%(E''-LE5}leU	+1
 E
[(009f'++f%//1"15r:ZGe!99+ -I 

d,VVUINr   )NNNNrandom_shuffle_queuer   r   r   r   r`   __classcell__r   s   @r   r   r     s'     *JO JOr   zqueue.FIFOQueue	FIFOQueuec                   ,     e Zd ZdZ	 	 	 	 d fd	Z xZS )r   zA queue implementation that dequeues elements in first-in first-out order.

  See `tf.queue.QueueBase` for a description of the methods on
  this class.
  c           
      d   t        |      }t        ||      }t        ||      }t        j                         5  t        j
                  d      5  t        j                  |||t        |      |      }ddd       ddd       t        t        | /  |||       y# 1 sw Y   (xY w# 1 sw Y   ,xY w)a/  Creates a queue that dequeues elements in a first-in first-out order.

    A `FIFOQueue` has bounded capacity; supports multiple concurrent
    producers and consumers; and provides exactly-once delivery.

    A `FIFOQueue` holds a list of up to `capacity` elements. Each
    element is a fixed-length tuple of tensors whose dtypes are
    described by `dtypes`, and whose shapes are optionally described
    by the `shapes` argument.

    If the `shapes` argument is specified, each component of a queue
    element must have the respective fixed shape. If it is
    unspecified, different queue elements may have different shapes,
    but the use of `dequeue_many` is disallowed.

    Args:
      capacity: An integer. The upper bound on the number of elements
        that may be stored in this queue.
      dtypes:  A list of `DType` objects. The length of `dtypes` must equal
        the number of tensors in each queue element.
      shapes: (Optional.) A list of fully-defined `TensorShape` objects
        with the same length as `dtypes`, or `None`.
      names: (Optional.) A list of string naming the components in the queue
        with the same length as `dtypes`, or `None`.  If specified the dequeue
        methods return a dictionary with the names as keys.
      shared_name: (Optional.) If non-empty, this queue will be shared under
        the given name across multiple sessions.
      name: Optional name for the queue operation.

    >>> q = tf.queue.FIFOQueue(capacity=10, dtypes=tf.int32)
    >>> q.size()
    <tf.Tensor: shape=(), dtype=int32, numpy=0>
    CPUr   r4   r   r   rZ   N)r   r7   r;   r   
init_scopedevicer   fifo_queue_v2r   r   r   r`   	r\   r   r   r4   r:   r   rZ   r]   r   s	           r   r`   zFIFOQueue.__init__  s    P 6"FFF+F%(E		 3::e, #11 ";/i  
)T#FFE9E   s#   B&$B3B&B#	B&&B/NNN
fifo_queuer   r   s   @r   r   r     s"      3F 3Fr   c                   <     e Zd ZdZ	 	 	 	 d fd	ZddZddZ xZS )GPUCompatibleFIFOQueuea  A queue implementation that dequeues elements in first-in first-out order.

  GPUCompatibleFIFOQueue is like FIFOQueue, but the queue resource may be placed
  either on a CPU or on a GPU. It is not cross-device: enqueues and dequeues
  will be colocated with the queue resource. GPUCompatibleFIFOQueue only
  supports enqueue and dequeue at the moment, not enqueue_many or dequeue_many.

  See `tf.queue.QueueBase` for a description of the methods in this class.
  c           	         t        |      }t        ||      }t        ||      }t        j                         5  t        j                  |||t        |      |      }ddd       t        t        | +  |||       y# 1 sw Y    xY w)a  Creates a queue that dequeues elements in a first-in first-out order.

    A `FIFOQueue` has bounded capacity; supports multiple concurrent
    producers and consumers; and provides exactly-once delivery.

    A `FIFOQueue` holds a list of up to `capacity` elements. Each
    element is a fixed-length tuple of tensors whose dtypes are
    described by `dtypes`, and whose shapes are optionally described
    by the `shapes` argument.

    If the `shapes` argument is specified, each component of a queue
    element must have the respective fixed shape. If it is
    unspecified, different queue elements may have different shapes,
    but the use of `dequeue_many` is disallowed.

    Args:
      capacity: An integer. The upper bound on the number of elements that may
        be stored in this queue.
      dtypes:  A list of `DType` objects. The length of `dtypes` must equal the
        number of tensors in each queue element.
      shapes: (Optional.) A list of fully-defined `TensorShape` objects with the
        same length as `dtypes`, or `None`.
      names: (Optional.) A list of strings naming the components in the queue
        with the same length as `dtypes`, or `None`.  If specified the dequeue
        methods return a dictionary with the names as keys.
      shared_name: (Optional.) If non-empty, this queue will be shared under the
        given name across multiple sessions.
      name: Optional name for the queue operation.
    r   N)r   r7   r;   r   r   r   r   r   r   r  r`   r   s	           r   r`   zGPUCompatibleFIFOQueue.__init__Q  s    H 6"FFF+F%(E		 #11 ";/i 

 $0y* s   $A<<Bc                     t        d      )z8enqueue_many is not supported on GPUCompatibleFIFOQueue._GPUCompatibleFIFOQueue does not support enqueue_many or dequeue_many, only enqueue and dequeue.NotImplementedError)r\   r   rZ   s      r   r   z#GPUCompatibleFIFOQueue.enqueue_many      
	$% %r   c                     t        d      )z8dequeue_many is not supported on GPUCompatibleFIFOQueue.r  r  )r\   r   rZ   s      r   r   z#GPUCompatibleFIFOQueue.dequeue_many  r	  r   r   r   )r   r   r   r   r`   r   r   r   r   s   @r   r  r  F  s&      0*d%%r   r  queue.PaddingFIFOQueue)r  io.PaddingFIFOQueuePaddingFIFOQueuer  r  c                   *     e Zd ZdZ	 	 	 d fd	Z xZS )r  a(  A FIFOQueue that supports batching variable-sized tensors by padding.

  A `PaddingFIFOQueue` may contain components with dynamic shape, while also
  supporting `dequeue_many`.  See the constructor for more details.

  See `tf.queue.QueueBase` for a description of the methods on
  this class.
  c                 8   t        |      }t        ||d      }t        ||      }t        |      t        |      k7  r$t	        dt        |       dt        |       d      t        j                  |||t        |      |      }t        t        | +  ||||       y)a  Creates a queue that dequeues elements in a first-in first-out order.

    A `PaddingFIFOQueue` has bounded capacity; supports multiple concurrent
    producers and consumers; and provides exactly-once delivery.

    A `PaddingFIFOQueue` holds a list of up to `capacity` elements. Each
    element is a fixed-length tuple of tensors whose dtypes are
    described by `dtypes`, and whose shapes are described by the `shapes`
    argument.

    The `shapes` argument must be specified; each component of a queue
    element must have the respective shape.  Shapes of fixed
    rank but variable size are allowed by setting any shape dimension to None.
    In this case, the inputs' shape may vary along the given dimension, and
    `dequeue_many` will pad the given dimension with zeros up to the maximum
    shape of all elements in the given batch.

    Args:
      capacity: An integer. The upper bound on the number of elements
        that may be stored in this queue.
      dtypes:  A list of `DType` objects. The length of `dtypes` must equal
        the number of tensors in each queue element.
      shapes: A list of `TensorShape` objects, with the same length as
        `dtypes`.  Any dimension in the `TensorShape` containing value
        `None` is dynamic and allows values to be enqueued with
         variable size in that dimension.
      names: (Optional.) A list of string naming the components in the queue
        with the same length as `dtypes`, or `None`.  If specified the dequeue
        methods return a dictionary with the names as keys.
      shared_name: (Optional.) If non-empty, this queue will be shared under
        the given name across multiple sessions.
      name: Optional name for the queue operation.

    Raises:
      ValueError: If shapes is not a list of shapes, or the lengths of dtypes
        and shapes do not match, or if names is specified and the lengths of
        dtypes and names do not match.
    T)r5   z9Shapes must be provided for all components, but received z dtypes and z shapes.r   N)r   r7   r;   r9   r.   r   padding_fifo_queue_v2r   r   r  r`   r   s	           r   r`   zPaddingFIFOQueue.__init__  s    Z 6"FFFEF%(E
6{c&k! ''*6{m<f+h0 1 1 "77 -I 

D*665)Lr   )NNpadding_fifo_queuer   r   s   @r   r  r    s    
 (;M ;Mr   queue.PriorityQueue)r  io.PriorityQueuePriorityQueuer  r  c                   ,     e Zd ZdZ	 	 	 	 d fd	Z xZS )r  zA queue implementation that dequeues elements in prioritized order.

  See `tf.queue.QueueBase` for a description of the methods on
  this class.
  c                     t        |      }t        ||      }t        j                  |||t	        |      |      }t
        j                  g|z   }|rdg|z   n|}	t        t        | '  ||	||       y)a  Creates a queue that dequeues elements in a first-in first-out order.

    A `PriorityQueue` has bounded capacity; supports multiple concurrent
    producers and consumers; and provides exactly-once delivery.

    A `PriorityQueue` holds a list of up to `capacity` elements. Each
    element is a fixed-length tuple of tensors whose dtypes are
    described by `types`, and whose shapes are optionally described
    by the `shapes` argument.

    If the `shapes` argument is specified, each component of a queue
    element must have the respective fixed shape. If it is
    unspecified, different queue elements may have different shapes,
    but the use of `dequeue_many` is disallowed.

    Enqueues and Dequeues to the `PriorityQueue` must include an additional
    tuple entry at the beginning: the `priority`.  The priority must be
    an int64 scalar (for `enqueue`) or an int64 vector (for `enqueue_many`).

    Args:
      capacity: An integer. The upper bound on the number of elements
        that may be stored in this queue.
      types:  A list of `DType` objects. The length of `types` must equal
        the number of tensors in each queue element, except the first priority
        element.  The first tensor in each element is the priority,
        which must be type int64.
      shapes: (Optional.) A list of fully-defined `TensorShape` objects,
        with the same length as `types`, or `None`.
      names: (Optional.) A list of strings naming the components in the queue
        with the same length as `dtypes`, or `None`.  If specified, the dequeue
        methods return a dictionary with the names as keys.
      shared_name: (Optional.) If non-empty, this queue will be shared under
        the given name across multiple sessions.
      name: Optional name for the queue operation.
    r   r   N)
r   r7   r   priority_queue_v2r   rP   int64r   r  r`   )r\   r   typesr4   r:   r   rZ   r]   priority_dtypespriority_shapesr   s             r   r`   zPriorityQueue.__init__  sy    T % EFE*F!33 -I }}o-O'-rdVm6O	-'%(13r   )NNNpriority_queuer   r   s   @r   r  r    s      $83 83r   c                   f    e Zd ZdZddZed        Zed        ZddZ	 	 	 ddZ	ddZ
dd	Zdd
Zy)BarrierzARepresents a key-value map that persists across graph executions.Nc                    t        |      | _        |t        || j                        }|D cg c]  }t        j                  |       c}| _        t        | j
                        D ])  \  }}|j                         dk(  st        d| d|        n1| j                  D cg c]  }t        j                          c}| _        t        j                  | j                  | j
                  ||      | _        t        j                         r$t        j                         j                  | _        y| j                  j"                  j$                  j'                  d      d   | _        yc c}w c c}w )a
  Creates a barrier that persists across different graph executions.

    A barrier represents a key-value map, where each key is a string, and
    each value is a tuple of tensors.

    At runtime, the barrier contains 'complete' and 'incomplete'
    elements. A complete element has defined tensors for all
    components of its value tuple, and may be accessed using
    take_many. An incomplete element has some undefined components in
    its value tuple, and may be updated using insert_many.

    The barrier call `take_many` outputs values in a particular order.
    First, it only outputs completed values.  Second, the order in which
    completed values are returned matches the order in which their very
    first component was inserted into the barrier.  So, for example, for this
    sequence of insertions and removals:

      barrier = Barrier((tf.string, tf.int32), shapes=((), ()))
      barrier.insert_many(0, keys=["k1", "k2"], values=["a", "b"]).run()
      barrier.insert_many(1, keys=["k1"], values=[1]).run()
      barrier.insert_many(0, keys=["k3"], values=["c"]).run()
      barrier.insert_many(1, keys=["k3"], values=[3]).run()
      barrier.insert_many(1, keys=["k2"], values=[2]).run()

      (indices, keys, values) = barrier.take_many(2)
      (indices_val, keys_val, values0_val, values1_val) =
         session.run([indices, keys, values[0], values[1]])

    The output will be (up to permutation of "k1" and "k2"):

      indices_val == (-2**63, -2**63)
      keys_val == ("k1", "k2")
      values0_val == ("a", "b")
      values1_val == (1, 2)

    Note the key "k2" was inserted into the barrier before "k3".  Even though
    "k3" was completed first, both are complete by the time
    take_many is called.  As a result, "k2" is prioritized and "k1" and "k2"
    are returned first.  "k3" remains in the barrier until the next execution
    of `take_many`.  Since "k1" and "k2" had their first insertions into
    the barrier together, their indices are the same (-2**63).  The index
    of "k3" will be -2**63 + 1, because it was the next new inserted key.

    Args:
      types: A single dtype or a tuple of dtypes, corresponding to the
        dtypes of the tensor elements that comprise a value in this barrier.
      shapes: Optional. Constraints on the shapes of tensors in the values:
        a single tensor shape tuple; a tuple of tensor shape tuples
        for each barrier-element tuple component; or None if the shape should
        not be constrained.
      shared_name: Optional. If non-empty, this barrier will be shared under
        the given name across multiple sessions.
      name: Optional name for the barrier op.

    Raises:
      ValueError: If one of the `shapes` indicate no elements.
    Nr   z5Empty tensors are not supported, but received shape 'z' at index )r   r4   r   rZ   rN   rO   )r   _typesr7   r	   r/   rQ   r   num_elementsr.   r>   r   barrier_barrier_refr   rx   rU   rV   rY   rZ   r[   )	r\   r  r4   r   rZ   r^   r   r$   r_   s	            r   r`   zBarrier.__init__'  s3   t  &DKfdkk2f;ABal..q1Bdl- <(!U1$ %%*G;qc; < <<
 =AKKHql002Hdl)11||	D
   "??$//dj$$'',,2237;dj! C Is   E&!E+c                     | j                   S )z%Get the underlying barrier reference.)r#  rv   s    r   barrier_refzBarrier.barrier_refw  s     r   c                     t        j                         r| j                  S | j                  j                  j
                  S )z#The name of the underlying barrier.)r   rx   rV   r#  rY   rZ   rv   s    r   rZ   zBarrier.name|  s2       "ZZ$$$r   c                 l    |d| j                   z  }t        j                  | j                  ||||      S )aY  For each key, assigns the respective value to the specified component.

    This operation updates each element at component_index.

    Args:
      component_index: The component of the value that is being assigned.
      keys: A vector of keys, with length n.
      values: An any-dimensional tensor of values, which are associated with the
        respective keys. The first dimension must have length n.
      name: Optional name for the op.

    Returns:
      The operation that performs the insertion.
    Raises:
      InvalidArgumentsError: If inserting keys and values without elements.
    z%s_BarrierInsertManyr   )rV   r   barrier_insert_manyr#  )r\   component_indexr   r   rZ   s        r   insert_manyzBarrier.insert_many  s=    " |#djj0d004tE Er   c                    |d| j                   z  }t        j                  | j                  || j                  |||      }t        j                         s|d   j                  }|rd}n5t        j                  t        j                  |j                  d               }|j                  d   j                  t        j                  |g             |j                  d   j                  t        j                  |g             t!        |j                  dd | j"                        D ]9  \  }}	|j                  t        j                  |g      j%                  |	             ; |S )a  Takes the given number of completed elements from this barrier.

    This operation concatenates completed-element component tensors along
    the 0th dimension to make a single component tensor.

    If barrier has no completed elements, this operation will block
    until there are 'num_elements' elements to take.

    TODO(b/25743580): the semantics of `allow_small_batch` are experimental
    and may be extended to other cases in the future.

    TODO(ebrevdo): If a take_many(allow_small_batch=True) is blocking
    already when the barrier is closed, it will block for ever. Fix this
    by using asynchronous operations.

    Args:
      num_elements: The number of elements to take.
      allow_small_batch: If the barrier is closed, don't block if there are less
        completed elements than requested, but instead return all available
        completed elements.
      timeout: This specifies the number of milliseconds to block
        before returning with DEADLINE_EXCEEDED. (This option is not
        supported yet.)
      name: A name for the operation (optional).

    Returns:
      A tuple of (index, key, value_list).
      "index" is a int64 tensor of length num_elements containing the
        index of the insert_many call for which the very first component of
        the given element was inserted into the Barrier, starting with
        the value -2**63.  Note, this value is different from the
        index of the insert_many call for which the element was completed.
      "key" is a string tensor of length num_elements containing the keys.
      "value_list" is a tuple of tensors, each one with size num_elements
        in the 0th dimension for each component in the barrier's values.

    Nz%s_BarrierTakeManyr   r   rh      )rV   r   barrier_take_manyr#  r   r   rx   rY   r	   r   r
   r   r   outputsr   r/   r?   rQ   r   )
r\   r!  allow_small_batchtimeoutrZ   r   rY   r   r   r$   s
             r   	take_manyzBarrier.take_many  s.   T |!DJJ.d

-
-C $$&q699b		 **&&ryy|46	jjml66	{CDjjml66	{CDrzz!"~t||< F-&%$$i[1==eD	FF Jr   c                 h    |d| j                   z  }t        j                  | j                  ||      S )az  Closes this barrier.

    This operation signals that no more new key values will be inserted in the
    given barrier. Subsequent InsertMany operations with new keys will fail.
    InsertMany operations that just complement already existing keys with other
    components, will continue to succeed. Subsequent TakeMany operations will
    continue to succeed if sufficient elements remain in the barrier. Subsequent
    TakeMany operations that would block will fail immediately.

    If `cancel_pending_enqueues` is `True`, all pending requests to the
    underlying queue will also be canceled, and completing of already
    started values is also not acceptable anymore.

    Args:
      cancel_pending_enqueues: (Optional.) A boolean, defaulting to
        `False` (described above).
      name: Optional name for the op.

    Returns:
      The operation that closes the barrier.
    z%s_BarrierCloser   )rV   r   barrier_closer#  r   s      r   r   zBarrier.close  s9    , |+d** 7 r   c                 f    |d| j                   z  }t        j                  | j                  |      S )zCompute the number of complete elements in the given barrier.

    Args:
      name: A name for the operation (optional).

    Returns:
      A single-element tensor containing the number of complete elements in the
      given barrier.
    z%s_BarrierReadySizer   )rV   r   barrier_ready_sizer#  r   s     r   
ready_sizezBarrier.ready_size  s0     |"TZZ/d//0A0AMMr   c                 f    |d| j                   z  }t        j                  | j                  |      S )zCompute the number of incomplete elements in the given barrier.

    Args:
      name: A name for the operation (optional).

    Returns:
      A single-element tensor containing the number of incomplete elements in
      the given barrier.
    z%s_BarrierIncompleteSizer   )rV   r   barrier_incomplete_sizer#  r   s     r   incomplete_sizezBarrier.incomplete_size	  s5     |'$**4d44& &r   )NNr"  r   )FNNr   )r   r   r   r   r`   r   r%  rZ   r*  r1  r   r6  r9  r   r   r   r  r  $  s\    IN<`   % %E0 #(	CJ:N&r   r  ConditionalAccumulatorBasec                   V    e Zd ZdZd Zed        Zed        Zed        Zd	dZ	d	dZ
y)
r:  8  A conditional accumulator for aggregating gradients.

  Up-to-date gradients (i.e., time step at which gradient was computed is
  equal to the accumulator's time step) are added to the accumulator.

  Extraction of the average gradient is blocked until the required number of
  gradients has been accumulated.
  c                 j   || _         |t        j                  |      | _        nt        j                         | _        || _        t        j                         r$t        j                         j                  | _	        y| j
                  j                  j                  j                  d      d   | _	        y)zCreates a new ConditionalAccumulator.

    Args:
      dtype: Datatype of the accumulated gradients.
      shape: Shape of the accumulated gradients.
      accumulator_ref: A handle to the conditional accumulator, created by sub-
        classes
    NrN   rO   )_dtyper	   r/   _shaper>   _accumulator_refr   rx   rU   rV   rY   rZ   r[   )r\   r   r$   accumulator_refs       r   r`   z#ConditionalAccumulatorBase.__init__$  s     DK ,,U3dk ..0dk+D  "??$//dj((++0066s;B?djr   c                     | j                   S )z%The underlying accumulator reference.)r@  rv   s    r   rA  z*ConditionalAccumulatorBase.accumulator_ref8  s        r   c                     | j                   S )z'The name of the underlying accumulator.rV   rv   s    r   rZ   zConditionalAccumulatorBase.name=       ::r   c                     | j                   S )z>The datatype of the gradients accumulated by this accumulator.)r>  rv   s    r   r   z ConditionalAccumulatorBase.dtypeB  r   r   Nc                 f    |d| j                   z  }t        j                  | j                  |      S zNumber of gradients that have currently been aggregated in accumulator.

    Args:
      name: Optional name for the operation.

    Returns:
      Number of accumulated gradients currently in accumulator.
    z%s_NumAccumulatedr   )rV   r   $resource_accumulator_num_accumulatedr@  r   s     r   num_accumulatedz*ConditionalAccumulatorBase.num_accumulatedG  s5     | 4::-dAAD* *r   c                     t        j                  | j                  t        j                  t        j                  |      t        j                        |      S a|  Sets the global time step of the accumulator.

    The operation logs a warning if we attempt to set to a time step that is
    lower than the accumulator's own time step.

    Args:
      new_global_step: Value of new time step. Can be a variable or a constant
      name: Optional name for the operation.

    Returns:
      Operation that sets the accumulator's time step.
    r   )	r   $resource_accumulator_set_global_stepr@  r   castr   r   rP   r  r\   new_global_steprZ   s      r   set_global_stepz*ConditionalAccumulatorBase.set_global_stepV  s@     AAc++O<gmmL r   r   )r   r   r   r   r`   r   rA  rZ   r   rJ  rQ  r   r   r   r:  r:    sS    @( ! !    *r   ConditionalAccumulatorc                   <     e Zd ZdZ	 	 	 	 d fd	ZddZddZ xZS )rR  r<  c                     t        j                  |||||      }t        j                         r8t	        j
                  |t        j                         j                        | _        t        t        | +  |||       y)a  Creates a new ConditionalAccumulator.

    Args:
      dtype: Datatype of the accumulated gradients.
      shape: Shape of the accumulated gradients.
      shared_name: Optional. If non-empty, this accumulator will be shared under
        the given name across multiple sessions.
      name: Optional name for the accumulator.
      reduction_type: Reduction type to use when taking the gradient.
    r   r$   r   rZ   reduction_type)handlehandle_deviceN)r    resource_conditional_accumulatorr   rx   r   rW   device_namerX   r   rR  r`   r\   r   r$   r   rZ   rV  rA  r   s          r   r`   zConditionalAccumulator.__init__t  sn      (HH%'O   "4II 0A0M0M Od 

 $0Or   c                 H   t        j                  || j                        }|j                         j	                  | j
                         t        j                  t        j                  |      t        j                        }t        j                  | j                  |||      S )a  Attempts to apply a gradient to the accumulator.

    The attempt is silently dropped if the gradient is stale, i.e., local_step
    is less than the accumulator's global time step.

    Args:
      grad: The gradient tensor to be applied.
      local_step: Time step at which the gradient was computed.
      name: Optional name for the operation.

    Returns:
      The operation that (conditionally) applies a gradient to the accumulator.

    Raises:
      ValueError: If grad is of the wrong shape
    )
local_stepgradientrZ   )r   r   r>  r   r   r?  r   rN  rP   r  r   #resource_accumulator_apply_gradientr@  r\   gradr]  rZ   s       r   
apply_gradz!ConditionalAccumulator.apply_grad  sv    "   t{{3DNN..t{{;s44Z@'--PJ@@*t$P Pr   c                     t        j                  | j                  || j                  |      }|j	                  | j
                         |S )a  Attempts to extract the average gradient from the accumulator.

    The operation blocks until sufficient number of gradients have been
    successfully applied to the accumulator.

    Once successful, the following actions are also triggered:

    - Counter of accumulated gradients is reset to 0.
    - Aggregated gradient is reset to 0 tensor.
    - Accumulator's internal time step is incremented by 1.

    Args:
      num_required: Number of gradients that needs to have been aggregated
      name: Optional name for the operation

    Returns:
      A tensor holding the value of the average gradient.

    Raises:
      InvalidArgumentError: If num_required < 1
    r   )r   "resource_accumulator_take_gradientr@  r>  r   r?  )r\   num_requiredrZ   outs       r   	take_gradz ConditionalAccumulator.take_grad  s=    , 
>
>|4;;TKCMM$++Jr   )NNconditional_accumulatorMEANr   Nr   )r   r   r   r   r`   rb  rg  r   r   s   @r   rR  rR  i  s'     -$P8P0r   z#sparse.SparseConditionalAccumulatorSparseConditionalAccumulatorc                   b     e Zd ZdZ	 	 	 	 d	 fd	Zd
dZ	 	 	 ddZddZddZddZ	ddZ
 xZS )rk  a  A conditional accumulator for aggregating sparse gradients.

  Sparse gradients are represented by `IndexedSlices`.

  Up-to-date gradients (i.e., time step at which gradient was computed is
  equal to the accumulator's time step) are added to the accumulator.

  Extraction of the average gradient is blocked until the required number of
  gradients has been accumulated.

  Args:
    dtype: Datatype of the accumulated gradients.
    shape: Shape of the accumulated gradients.
    shared_name: Optional. If non-empty, this accumulator will be shared under
      the given name across multiple sessions.
    name: Optional name for the accumulator.
    reduction_type: Reduction type to use when taking the gradient.
  c                 d    t        j                  |||||      }t        t        |   |||       y )NrU  )r   sparse_conditional_accumulatorr   rk  r`   r[  s          r   r`   z%SparseConditionalAccumulator.__init__  s>     (FF%'O 

&6ue7FHr   c                 j    | j                  |j                  |j                  |j                  ||      S )a  Attempts to apply a gradient to the accumulator.

    The attempt is silently dropped if the gradient is stale, i.e., `local_step`
    is less than the accumulator's global time step.

    Args:
      grad: The gradient `IndexedSlices` to be applied.
      local_step: Time step at which the gradient was computed.
      name: Optional name for the operation.

    Returns:
      The operation that (conditionally) applies a gradient to the accumulator.

    Raises:
      InvalidArgumentError: If grad is of the wrong shape
    )grad_indicesgrad_values
grad_shaper]  rZ   )rb  indicesr   dense_shaper`  s       r   apply_indexed_slices_gradz6SparseConditionalAccumulator.apply_indexed_slices_grad  s7    " ??\\KK##   r   c           
      P   t        j                  t        j                  |      t        j
                        }t        j                  | j                  |t        j                  |t        j
                        |t        j                  |g n|t        j
                        |du|      S )a  Attempts to apply a sparse gradient to the accumulator.

    The attempt is silently dropped if the gradient is stale, i.e., `local_step`
    is less than the accumulator's global time step.

    A sparse gradient is represented by its indices, values and possibly empty
    or None shape. Indices must be a vector representing the locations of
    non-zero entries in the tensor. Values are the non-zero slices of the
    gradient, and must have the same first dimension as indices, i.e., the nnz
    represented by indices and values must be consistent. Shape, if not empty or
    None, must be consistent with the accumulator's shape (if also provided).

    Example:
      A tensor [[0, 0], [0, 1], [2, 3]] can be represented
        indices: [1,2]
        values: [[0,1],[2,3]]
        shape: [3, 2]

    Args:
      grad_indices: Indices of the sparse gradient to be applied.
      grad_values: Values of the sparse gradient to be applied.
      grad_shape: Shape of the sparse gradient to be applied.
      local_step: Time step at which the gradient was computed.
      name: Optional name for the operation.

    Returns:
      The operation that (conditionally) applies a gradient to the accumulator.

    Raises:
      InvalidArgumentError: If grad is of the wrong shape
    N)r]  gradient_indicesgradient_valuesgradient_shapehas_known_shaperZ   )	r   rN  r   r   rP   r  r   !sparse_accumulator_apply_gradientr@  )r\   rp  rq  rr  r]  rZ   s         r   rb  z'SparseConditionalAccumulator.apply_grad  s    J s44Z@'--PJ>>!|W]]C#}}$B*gmmE#4/ r   c                 \    t        j                  | j                  || j                  |      S )a  Attempts to extract the average gradient from the accumulator.

    The operation blocks until sufficient number of gradients have been
    successfully applied to the accumulator.

    Once successful, the following actions are also triggered:
    - Counter of accumulated gradients is reset to 0.
    - Aggregated gradient is reset to 0 tensor.
    - Accumulator's internal time step is incremented by 1.

    Args:
      num_required: Number of gradients that needs to have been aggregated
      name: Optional name for the operation

    Returns:
      A tuple of indices, values, and shape representing the average gradient.

    Raises:
      InvalidArgumentError: If `num_required` < 1
    r   )r    sparse_accumulator_take_gradientr@  r>  )r\   re  rZ   s      r   rg  z&SparseConditionalAccumulator.take_grad1  s,    * ==|4;;TK Kr   c                     t        j                  | j                  || j                  |      }t	        j
                  |j                  |j                  |j                        S )a  Attempts to extract the average gradient from the accumulator.

    The operation blocks until sufficient number of gradients have been
    successfully applied to the accumulator.

    Once successful, the following actions are also triggered:
    - Counter of accumulated gradients is reset to 0.
    - Aggregated gradient is reset to 0 tensor.
    - Accumulator's internal time step is incremented by 1.

    Args:
      num_required: Number of gradients that needs to have been aggregated
      name: Optional name for the operation

    Returns:
      An `IndexedSlices` holding the value of the average gradient.

    Raises:
      InvalidArgumentError: If `num_required` < 1
    r   )rs  r   rt  )	r   r}  r@  r>  r   IndexedSlicesrs  r   r$   )r\   re  rZ   
return_vals       r   take_indexed_slices_gradz5SparseConditionalAccumulator.take_indexed_slices_gradI  sV    * #CC|4;;TKJ''""  $$& &r   c                 f    |d| j                   z  }t        j                  | j                  |      S rH  )rV   r   accumulator_num_accumulatedr@  r   s     r   rJ  z,SparseConditionalAccumulator.num_accumulatedf  s5     | 4::-d88D* *r   c                     t        j                  | j                  t        j                  t        j                  |      t        j                        |      S rL  )	r   accumulator_set_global_stepr@  r   rN  r   r   rP   r  rO  s      r   rQ  z,SparseConditionalAccumulator.set_global_stepu  s@     88c++O<gmmL r   )NNrn  ri  rj  )Nr   Nr   )r   r   r   r   r`   ru  rb  rg  r  rJ  rQ  r   r   s   @r   rk  rk    sG    * 4$H6 !.`K0&:*r   c                       e Zd ZdZdZ ej                         Z	 	 	 	 	 ddZe	d        Z
e	d        Ze	d        Ze	d        Ze	d	        Ze	d
        ZddZd Zd Zd Zy)BaseStagingAreazBase class for Staging Areas.r   Nc                    |=t        j                         j                  | j                  j                        | _        n&t        |t              r|| _        nt        d|       || _	        |Kt        |      t        |      k7  rt        d      |D cg c]  }t        j                  |       c}| _        n1| j                  D cg c]  }t        j                          c}| _        |*t        |      t        |      k7  rt        d      || _        nd | _        || _        || _        t        j$                  d| j
                  z        5  t'        j(                         | _        d d d        y c c}w c c}w # 1 sw Y   y xY w)Nz"shared_name must be a string, got z4StagingArea shapes must be the same length as dtypesz3StagingArea names must be the same length as dtypesz%s_root)r   get_default_graphunique_namer   r   rV   r   r   r.   rP   r9   r	   r/   rQ   r>   rR   	_capacity_memory_limitr   r   no_op	_coloc_op)	r\   r   r4   r:   r   r   memory_limitr^   r_   s	            r   r`   zBaseStagingArea.__init__  sD    



!
-
-dnn.E.E
F j	K	%dj;K=IJJDL	VF	#OPP;ABal..q1Bdl<@LLIql002Idl	Us6{	"NOOdkdkDN%D 
	DJJ.	/ 0'--/dn0 0 CI0 0s   E1E6E;;Fc                     | j                   S )zThe name of the staging area.rD  rv   s    r   rZ   zBaseStagingArea.name  rE  r   c                     | j                   S )z@The list of dtypes for each component of a staging area element.rz   rv   s    r   r   zBaseStagingArea.dtypes  r{   r   c                     | j                   S )z@The list of shapes for each component of a staging area element.r}   rv   s    r   r4   zBaseStagingArea.shapes  r{   r   c                     | j                   S )z?The list of names for each component of a staging area element.r   rv   s    r   r:   zBaseStagingArea.names  r   r   c                     | j                   S )z4The maximum number of elements of this staging area.)r  rv   s    r   r   zBaseStagingArea.capacity  s     >>r   c                     | j                   S )z1The maximum number of bytes of this staging area.)r  rv   s    r   r  zBaseStagingArea.memory_limit  s     r   c           	         t        |t              r| j                  st        d      t	        |j                               j                  | j                        s;t        dt        |j                                dt        | j                               t        t        | j                        D cg c]  \  }}||v r||   ||f c}} \  }}}nz| j                  rt        d      |t        d      t        |      t        |      k7  r#t        dt        |       dt        |             t        |t        t        f      s|g}dg}t        |      t        | j                        k  s-t        d	t        |       d
t        | j                               g }t        ||      D ]  \  }}| j                  |   | j                  |   }	}|j                  |k7  r-t        dt!        |j                         dt!        |             |j#                         j%                  |	       |j'                  t)        j*                  ||d|z                ||fS c c}}w )a  Validate and convert `vals` to a list of `Tensor`s.

    The `vals` argument can be a Tensor, a list or tuple of tensors, or a
    dictionary with tensor values.

    If `vals` is a list, then the appropriate indices associated with the
    values must be provided.

    If it is a dictionary, the staging area must have been constructed with a
    `names` attribute and the dictionary keys must match the staging area names.
    `indices` will be inferred from the dictionary keys.
    If the staging area was constructed with a `names` attribute, `vals` must
    be a dictionary.

    Checks that the dtype and shape of each value matches that
    of the staging area.

    Args:
      vals: A tensor, a list or tuple of tensors, or a dictionary.

    Returns:
      A (tensors, indices) tuple where `tensors` is a list of `Tensor` objects
      and `indices` is a list of indices associated with the tensors.

    Raises:
      ValueError: If `vals` or `indices` is invalid.
    z5Staging areas must have names to enqueue a dictionaryzJKeys in dictionary to put do not match names of staging area. Dictionary: zQueue: z:You must enqueue a dictionary in a staging area with namesz9Indices must be supplied when inserting a list of tensorszNumber of indices z  doesn't match number of values r   zUnexpected number of inputs z vs z+Datatypes do not match. Received val.dtype z and dtype r   r   )r   r   rR   r.   setr   issubsetr   r?   r   r9   r   r   rP   rQ   r   r   r   r   r   r   r   )
r\   r   rs  r   r   r_   r   r   r   r$   s
             r   _check_put_dtypesz!BaseStagingArea._check_put_dtypes  s`   8 $[[CE 	E&&t{{3 99?		9L8M"6$++#6"79 : 	:
 +4T[[+A-#'1a"#t) !%QA - .dGQ 
 & ' 	' 
 & ' 	' 
WT	"-c'l^ <--0YK9 : 	: tUm,v# t9DLL))5c$i[dll+,. / / GdG$ LQ\\!_dll1oUe	e	 //2399~.> ?""%e*/ 0 	0 
mmo//6nn


5~7I
JLL GQ-s   2I
c                     t        |t        t        f      s|g}t        j                         j
                  }|| j                  j
                  k7  r"|D cg c]  }t        j                  |       }}|S c c}w )zjEncode inter-device transfers if the current device
    is not the same as the Staging Area's device.
    )	r   r   r   r   r  r   r  r   identity)r\   r   curr_device_scopets       r   _create_device_transfersz(BaseStagingArea._create_device_transfers!  sh    
 gt}-	g(..077DNN1110781##A&8g8N 9s   A5c                 
   | j                  |      }t        ||      D ]#  \  }}|j                  | j                  |          % | j                  r-t        ||      D ci c]  \  }}| j                  |   | c}}S |S c c}}w )a  Return the value to return from a get op.

    If the staging area has names, return a dictionary with the
    names as keys.  Otherwise return either a single tensor
    or a list of tensors depending on the length of `tensors`.

    Args:
      tensors: List of tensors from the get op.
      indices: Indices of associated names and shapes

    Returns:
      A single tensor, a list of tensors, or a dictionary
      of tensors.
    )r  r?   r   rQ   rR   )r\   r   rs  r   r   r  s         r   _get_return_valuez!BaseStagingArea._get_return_value0  s      ++G4G '* (	t||A'( {{ -0,ABDAqdkk!naBBN Cs   A?c                 x    t        |t        t        f      r|S t        |t              r|j	                         S |gS r   r   r   s     r   r   zBaseStagingArea._scope_valsL  r   r   NNNr   r   r   )r   r   r   r   _identifier	threadingLock_lockr`   r   rZ   r   r4   r:   r   r  r  r  r  r   r   r   r   r  r    s    %+
)..
% $0L            N`8r   r  c                   \     e Zd ZdZ	 	 	 	 	 d	 fd	Zd
dZd Zd
dZd
dZd
dZ	d
dZ
 xZS )StagingAreaa  Class for staging inputs. No ordering guarantees.

  A `StagingArea` is a TensorFlow data structure that stores tensors across
  multiple steps, and exposes operations that can put and get tensors.

  Each `StagingArea` element is a tuple of one or more tensors, where each
  tuple component has a static dtype, and may have a static shape.

  The capacity of a `StagingArea` may be bounded or unbounded.
  It supports multiple concurrent producers and consumers; and
  provides exactly-once delivery.

  Each element of a `StagingArea` is a fixed-length tuple of tensors whose
  dtypes are described by `dtypes`, and whose shapes are optionally described
  by the `shapes` argument.

  If the `shapes` argument is specified, each component of a staging area
  element must have the respective fixed shape. If it is
  unspecified, different elements may have different shapes,

  It can be configured with a capacity in which case
  put(values) will block until space becomes available.

  Similarly, it can be configured with a memory limit which
  will block put(values) until space is available.
  This is mostly useful for limiting the number of tensors on
  devices such as GPUs.

  All get() and peek() commands block if the requested data
  is not present in the Staging Area.

  c                 6    t         t        |   ||||||       y)a  Constructs a staging area object.

    The two optional lists, `shapes` and `names`, must be of the same length
    as `dtypes` if provided.  The values at a given index `i` indicate the
    shape and name to use for the corresponding queue component in `dtypes`.

    The device scope at the time of object creation determines where the
    storage for the `StagingArea` will reside.  Calls to `put` will incur a copy
    to this memory space, if necessary.  Tensors returned by `get` will be
    placed according to the device scope when `get` is called.

    Args:
      dtypes:  A list of types.  The length of dtypes must equal the number
        of tensors in each element.
      shapes: (Optional.) Constraints on the shapes of tensors in an element.
        A list of shape tuples or None. This list is the same length
        as dtypes.  If the shape of any tensors in the element are constrained,
        all must be; shapes can be None if the shapes should not be constrained.
      names: (Optional.) If provided, the `get()` and
        `put()` methods will use dictionaries with these names as keys.
        Must be None or a list or tuple of the same length as `dtypes`.
      shared_name: (Optional.) A name to be used for the shared object. By
        passing the same name to two different python objects they will share
        the underlying staging area. Must be a string.
      capacity: (Optional.) Maximum number of elements.
        An integer. If zero, the Staging Area is unbounded
      memory_limit: (Optional.) Maximum number of bytes of all tensors
        in the Staging Area.
        An integer. If zero, the Staging Area is unbounded

    Raises:
      ValueError: If one of the arguments is invalid.
    N)r   r  r`   )r\   r   r4   r:   r   r   r  r   s          r   r`   zStagingArea.__init__  s"    R 
+t%ffe[&.>r   c           	         t        j                  |d| j                  z  | j                  |            5 }t	        |t
        t        t        f      s|g}t        t        t        |                  }| j                  ||      \  }}t        j                  | j                        5  t        j                  || j                  || j                  | j                         }ddd       cddd       S # 1 sw Y   xY w# 1 sw Y   yxY w)a4  Create an op that places a value into the staging area.

    This operation will block if the `StagingArea` has reached
    its capacity.

    Args:
      values: A single tensor, a list or tuple of tensors, or a dictionary with
        tensor values. The number of elements must match the length of the
        list provided to the dtypes argument when creating the StagingArea.
      name: A name for the operation (optional).

    Returns:
        The created op.

    Raises:
      ValueError: If the number or type of inputs don't match the staging area.
    %s_put)r   r   rZ   r   r  N)r   r   rV   r   r   r   r   r   ranger9   r  colocate_withr  r   stager  r  )r\   r   rZ   r   rs  r   r_   rY   s           r   putzStagingArea.put  s    $ 
h3((0
2 5:ud 34 U3v;'(g&&vw7gdAT^^, -$$

^^++-- % - - s$   A0C<$9C0	C<0C9	5C<<Dc                     t        j                  | j                        5   |       }d d d        t        t	        t        | j                                    }| j                  |      S # 1 sw Y   BxY wr   )r   r  r  r   r  r9   rP   r  )r\   get_fnrZ   r   rs  s        r   __internal_getzStagingArea.__internal_get  s]    			4>>	* Hc 5T\\*+,G!!#w//	 s   A))A2c                 X     d j                   z   fd} j                  |      S )aF  Gets one element from this staging area.

    If the staging area is empty when this operation executes, it will block
    until there is an element to dequeue.

    Note that unlike others ops that can block, like the queue Dequeue
    operations, this can stop other work from happening.  To avoid this, the
    intended use is for this to be called only when there will be an element
    already available.  One method for doing this in a training loop would be to
    run a `put()` call during a warmup session.run call, and then call both
    `get()` and `put()` in each subsequent step.

    The placement of the returned tensor will be determined by the current
    device scope when this function is called.

    Args:
      name: A name for the operation (optional).

    Returns:
      The tuple of tensors that was gotten.
    %s_getc                      t        j                  j                  j                   j                  j
                        S Nr   r   rZ   r   r  )r   unstagerP   rV   r  r  )rZ   r\   s   r   <lambda>z!StagingArea.get.<locals>.<lambda>  s2    "**$,, $

!^^!%!3!35 r   rV   _StagingArea__internal_get)r\   rZ   fns   `` r   getzStagingArea.get  s4    , |

"d
5B r4((r   c                 \     d j                   z   fd} j                  |      S )a  Peeks at an element in the staging area.

    If the staging area is too small to contain the element at
    the specified index, it will block until enough elements
    are inserted to complete the operation.

    The placement of the returned tensor will be determined by
    the current device scope when this function is called.

    Args:
      index: The index of the tensor within the staging area
              to look up.
      name: A name for the operation (optional).

    Returns:
      The tuple of tensors that was gotten.
    z%s_peekc                      t        j                   j                  j                  j                  j
                        S r  )r   
stage_peekrP   rV   r  r  )rn   rZ   r\   s   r   r  z"StagingArea.peek.<locals>.<lambda>  s4    "--e<<TZZ!%!3!35 r   r  )r\   rn   rZ   r  s   ``` r   peekzStagingArea.peek  s4    $ |#d
5B r4((r   c                     |d| j                   z  }t        j                  || j                   | j                  | j                  | j
                        S )Returns the number of elements in the staging area.

    Args:
        name: A name for the operation (optional)

    Returns:
        The created op
    %s_sizerZ   r   r   r   r  )rV   r   
stage_sizerP   r  r  r   s     r   r   zStagingArea.size  sK     |#d''JJ||'') )r   c                     |d| j                   z  }t        j                  || j                   | j                  | j                  | j
                        S )Clears the staging area.

    Args:
        name: A name for the operation (optional)

    Returns:
        The created op
    %s_clearr  )rV   r   stage_clearrP   r  r  r   s     r   clearzStagingArea.clear+  sK     |$**$d((JJ||'') )r   r  r   )r   r   r   r   r`   r  r  r  r  r   r  r   r   s   @r   r  r  ]  s@    F *>X$L0 )D)<)&)r   r  c                   x     e Zd ZdZ	 	 	 	 	 	 d fd	ZddZddZddZddZddZ	ddZ
dd	Zdd
ZddZ xZS )MapStagingAreaa 	  A `MapStagingArea` is a TensorFlow data structure that stores tensors
  across multiple steps, and exposes operations that can put and get tensors.

  Each `MapStagingArea` element is a (key, value) pair.
  Only int64 keys are supported, other types should be
  hashed to produce a key.
  Values are a tuple of one or more tensors.
  Each tuple component has a static dtype,
  and may have a static shape.

  The capacity of a `MapStagingArea` may be bounded or unbounded.
  It supports multiple concurrent producers and consumers; and
  provides exactly-once delivery.

  Each value tuple of a `MapStagingArea` is a fixed-length tuple of tensors
  whose
  dtypes are described by `dtypes`, and whose shapes are optionally described
  by the `shapes` argument.

  If the `shapes` argument is specified, each component of a staging area
  element must have the respective fixed shape. If it is
  unspecified, different elements may have different shapes,

  It behaves like an associative container with support for:

   - put(key, values)
   - peek(key)         like dict.get(key)
   - get(key)          like dict.pop(key)
   - get(key=None)     like dict.popitem()
   - size()
   - clear()

  If ordered a tree structure ordered by key will be used and
  get(key=None) will remove (key, value) pairs in increasing key order.
  Otherwise a hashtable

  It can be configured with a capacity in which case
  put(key, values) will block until space becomes available.

  Similarly, it can be configured with a memory limit which
  will block put(key, values) until space is available.
  This is mostly useful for limiting the number of tensors on
  devices such as GPUs.

  All get() and peek() commands block if the requested
  (key, value) pair is not present in the staging area.

  Partial puts are supported and will be placed in an incomplete
  map until such time as all values associated with the key have
  been inserted. Once completed, this (key, value) pair will be
  inserted into the map. Data in the incomplete map
  counts towards the memory limit, but not towards capacity limit.

  Partial gets from the map are also supported.
  This removes the partially requested tensors from the entry,
  but the entry is only removed from the map once all tensors
  associated with it are removed.
  c                    t         t        |   ||||||       || _        |rt        j
                  | _        t        j                  | _        t        j                  | _
        t        j                  | _        t        j                  | _        t        j                  | _        t        j"                  | _        yt        j&                  | _        t        j(                  | _        t        j*                  | _
        t        j,                  | _        t        j.                  | _        t        j0                  | _        t        j2                  | _        y)a-  Args:

      dtypes:  A list of types.  The length of dtypes must equal the number
        of tensors in each element.
      capacity: (Optional.) Maximum number of elements.
        An integer. If zero, the Staging Area is unbounded
      memory_limit: (Optional.) Maximum number of bytes of all tensors
        in the Staging Area (excluding keys).
        An integer. If zero, the Staging Area is unbounded
      ordered: (Optional.) If True the underlying data structure
        is a tree ordered on key. Otherwise assume a hashtable.
      shapes: (Optional.) Constraints on the shapes of tensors in an element.
        A list of shape tuples or None. This list is the same length
        as dtypes.  If the shape of any tensors in the element are constrained,
        all must be; shapes can be None if the shapes should not be constrained.
      names: (Optional.) If provided, the `get()` and
        `put()` methods will use dictionaries with these names as keys.
        Must be None or a list or tuple of the same length as `dtypes`.
      shared_name: (Optional.) A name to be used for the shared object. By
        passing the same name to two different python objects they will share
        the underlying staging area. Must be a string.

    Raises:
      ValueError: If one of the arguments is invalid.

    N)r   r  r`   _orderedr   ordered_map_stage_put_fnordered_map_unstage_pop_fnordered_map_unstage_no_key_popitem_fnordered_map_peek_peek_fnordered_map_size_size_fnordered_map_incomplete_size_incomplete_size_fnordered_map_clear	_clear_fn	map_stagemap_unstagemap_unstage_no_keymap_peekmap_sizemap_incomplete_size	map_clear)	r\   r   r4   r:   r   orderedr   r  r   s	           r   r`   zMapStagingArea.__init__{  s    F 
.$()1<A DM&88dl&::dl*EEd'88dm'88dm!2!N!Nd(::dn&00dl&22dl*==d'00dm'00dm!2!F!Fd(22dnr   c                    t        j                  |d| j                  z  | j                  |            5 }| j	                  ||      \  }}t        j
                  | j                        5  | j                  |||| j                  | j                  || j                  | j                        }ddd       ddd       S # 1 sw Y   xY w# 1 sw Y   S xY w)a  Create an op that stores the (key, vals) pair in the staging area.

    Incomplete puts are possible, preferably using a dictionary for vals
    as the appropriate dtypes and shapes can be inferred from the value names
    dictionary key values. If vals is a list or tuple, indices must
    also be specified so that the op knows at which element position
    to perform the insert.

    This operation will block if the capacity or memory limit of this
    container is reached.

    Args:
        key: Key associated with the data
        vals: Tensor (or a dict/tuple of Tensors) to place
                into the staging area.
        indices: (Optional) if vals is a tuple/list, this is required.
        name: A name for the operation (optional)

    Returns:
        The created op

    Raises:
        ValueError: If the number or type of inputs don't match the staging
        area.
    r  r  N)r   r   rV   r   r  r  r  r  rP   r  r  )r\   r   r   rs  rZ   r   rY   s          r   r  zMapStagingArea.put  s    6 
h3((.
0 -38,,T7;mdGT^^, 	-\\<<

^^++  -	-- I	- 	-- Is$   5C	)AB=+C	=C	C		Cc                 p   |'t        t        t        | j                                    }t	        |t
        t         f      st        dt        |             t        |      dk(  rt        d      t        d |D              rE| j                  t        d| d      	 |D cg c]  }| j                  j                  |       }}n"t        d |D              rnt        d	| d
      |D cg c]  }| j                  |    }}||fS c c}w # t        $ r t        d| j                         w xY wc c}w )NzInvalid indices type r   zEmpty indicesc              3   <   K   | ]  }t        |t                y wr   )r   r   r#   r   s     r   r%   z9MapStagingArea._get_indices_and_dtypes.<locals>.<genexpr>  s     
/!:a
/rd   zString indices provided z3, but this Staging Area was not created with names.z&Named index not in Staging Area names c              3   <   K   | ]  }t        |t                y wr   r    r  s     r   r%   z9MapStagingArea._get_indices_and_dtypes.<locals>.<genexpr>  s     1AZ31rd   zMixed types in indices z. May only be str or int)r   r  r9   rP   r   r   r0   r1   r.   r2   rR   rn   )r\   rs  r   r   r   s        r   _get_indices_and_dtypesz&MapStagingArea._get_indices_and_dtypes  sL   U3t||,-.ggt}--d7m_=>>
7|q''

/w
//		3G9 =I I J 	J>189A4;;$$Q'99 
11	1
/y 9/ / 0 0 (//!dll1o/F/F? : > //3{{m= > 	>> 0s$   D ""D	D -D3	D "D0c           
      D   |d| j                   z  }| j                  |      \  }}t        j                  | j                        5  | j                  || j                   |||| j                  | j                        }ddd       | j                  |      S # 1 sw Y   xY w)a   Peeks at staging area data associated with the key.

    If the key is not in the staging area, it will block
    until the associated (key, value) is inserted.

    Args:
        key: Key associated with the required data
        indices: Partial list of tensors to retrieve (optional).
                A list of integer or string indices.
                String indices are only valid if the Staging Area
                has names associated with it.
        name: A name for the operation (optional)

    Returns:
        The created op
    Nz%s_popr   rs  r   rZ   r   r  )	rV   r  r   r  r  r  r  r  r  r\   r   rs  rZ   r   results         r   r  zMapStagingArea.peek  s    $ |

"d227;OGV			4>>	* +}}
jj>>))  +f+ !!&'22+ +s   7BBc                 T    || j                  ||      S | j                  |||      S )a~  If the key is provided, the associated (key, value) is returned from the staging area.

    If the key is not in the staging area, this method will block until
    the associated (key, value) is inserted.
    If no key is provided and the staging area is ordered,
    the (key, value) with the smallest key will be returned.
    Otherwise, a random (key, value) will be returned.

    If the staging area is empty when this operation executes,
    it will block until there is an element to dequeue.

    Args:
        key: Key associated with the required data (Optional)
        indices: Partial list of tensors to retrieve (optional).
                A list of integer or string indices.
                String indices are only valid if the Staging Area
                has names associated with it.
        name: A name for the operation (optional)

    Returns:
        The created op
    )rs  rZ   )_popitem_pop)r\   r   rs  rZ   s       r   r  zMapStagingArea.get"	  s1    . {]]7]66YYsG$Y77r   c           
      H   |d| j                   z  }| j                  |      \  }}t        j                  | j                        5  | j                  || j                   |||| j                  | j                        }ddd       || j                  |      fS # 1 sw Y   xY w)aE  Remove and return the associated (key, value) is returned from the staging area.

    If the key is not in the staging area, this method will block until
    the associated (key, value) is inserted.
    Args:
        key: Key associated with the required data
        indices: Partial list of tensors to retrieve (optional).
                A list of integer or string indices.
                String indices are only valid if the Staging Area
                has names associated with it.
        name: A name for the operation (optional)

    Returns:
        The created op
    Nr  r  )	rV   r  r   r  r  r  r  r  r  r  s         r   r  zMapStagingArea._pop>	  s      |

"d227;OGV			4>>	* +||
jj>>))  +f+ &&vw777+ +s   7BB!c           	      x   |d| j                   z  }| j                  |      \  }}t        j                  | j                        5  | j                  | j                   |||| j                  | j                        \  }}ddd       | j                        d   }| j                  |      }||fS # 1 sw Y   3xY w)a  If the staging area is ordered, the (key, value) with the smallest key will be returned.

    Otherwise, a random (key, value) will be returned.
    If the staging area is empty when this operation executes,
    it will block until there is an element to dequeue.

    Args:
        key: Key associated with the required data
        indices: Partial list of tensors to retrieve (optional).
                A list of integer or string indices.
                String indices are only valid if the Staging Area
                has names associated with it.
        name: A name for the operation (optional)

    Returns:
        The created op
    Nz%s_get_nokeyr  r   )
rV   r  r   r  r  r  r  r  r  r  )r\   rs  rZ   r   r   r  s         r   r  zMapStagingArea._popitem_	  s    $ |djj(d227;OGV			4>>	* +$$jj>>)) % +kc6+ 
'
'
,Q
/C##FG4F;+ +s   9B00B9c                     |d| j                   z  }| j                  | j                   || j                  | j                  | j                        S )r  r  r   rZ   r   r   r  )rV   r  rP   r  r  r   s     r   r   zMapStagingArea.size	  sN     |#d==JJ||''  ) )r   c                     |d| j                   z  }| j                  | j                   || j                  | j                  | j                        S )zReturns the number of incomplete elements in the staging area.

    Args:
        name: A name for the operation (optional)

    Returns:
        The created op
    z%s_incomplete_sizer  )rV   r  rP   r  r  r   s     r   r9  zMapStagingArea.incomplete_size	  sP     |!DJJ.d##JJ||'' $ ) )r   c                     |d| j                   z  }| j                  | j                   || j                  | j                  | j                        S )r  r  r  )rV   r  rP   r  r  r   s     r   r  zMapStagingArea.clear	  sN     |$**$d>>JJ||''  ) )r   )NNNFr   r   )NNr   )NNN)r   r   r   r   r`   r  r  r  r  r  r  r   r9  r  r   r   s   @r   r  r  ?  sT    9z 83t*X<!3F888B%N)&)&)r   r  c                   .    e Zd ZdZ	 	 	 	 	 	 	 	 ddZd Zy)RecordInputa&  RecordInput asynchronously reads and randomly yields TFRecords.

  A RecordInput Op will continuously read a batch of records asynchronously
  into a buffer of some fixed capacity. It can also asynchronously yield
  random records from this buffer.

  It will not start yielding until at least `buffer_size / 2` elements have been
  placed into the buffer so that sufficient randomization can take place.

  The order the files are read will be shifted each epoch by `shift_amount` so
  that the data is presented in a different order every epoch.
  Nc
                     || _         || xj                   |z  c_         || _        || _        || _        || _        || _        || _        || _        t        j                  j                  | _        |	|	| _        yy)aP  Constructs a RecordInput Op.

    Args:
      file_pattern: File path to the dataset, possibly containing wildcards.
        All matching files will be iterated over each epoch.
      batch_size: How many records to return at a time.
      buffer_size: The maximum number of records the buffer will contain.
      parallelism: How many reader threads to use for reading from files.
      shift_ratio: What percentage of the total number files to move the start
        file forward by each epoch.
      seed: Specify the random number seed used by generator that randomizes
        records.
      name: Optional name for the operation.
      batches: None by default, creating a single batch op. Otherwise specifies
        how many batches to create, which are returned as a list when
        `get_yield_op()` is called. An example use case is to split processing
        between devices on one computer.
      compression_type: The type of compression for the file. Currently ZLIB and
        GZIP are supported. Defaults to none.

    Raises:
      ValueError: If one of the arguments is invalid.
    N)_batch_size_batches_file_pattern_buffer_size_parallelism_shift_ratio_seedrV   r   TFRecordCompressionTypeNONE_compression_type)
r\   file_pattern
batch_sizebuffer_sizeparallelismshift_ratior   rZ   batchescompression_types
             r   r`   zRecordInput.__init__	  s    B "D
'!DM%D#D#D#DDJDJ&>>CCD#/d $r   c           
         t         j                  j                  t        j                  | j                              }t	        j
                  | j                  | j                  | j                  | j                  | j                  | j                  || j                        }| j                  |S t        j                  | j                        5  t!        | j                        D cg c]  }g  }}t#        j$                  || j                  d      }t'        |      D ]<  \  }}|| j                  z  }||   j)                  t#        j*                  |g              > |cddd       S c c}w # 1 sw Y   yxY w)zAdds a node that yields a group of records every time it is executed.
    If RecordInput `batches` parameter is not None, it yields a list of
    record batches with the specified `batch_size`.
    )r  file_buffer_sizefile_parallelismfile_shuffle_shift_ratior  file_random_seedr  rZ   Nr   )r   TFRecordOptionsget_compression_type_stringr  r   record_inputr  r  r   r  r  r  rV   r  r   r   r  r   r[   r   r   reshape)r\   r  recordsr_   
batch_listrn   protobufbatch_indexs           r   get_yield_opzRecordInput.get_yield_op	  s<   
 !00LL!!$"8"89;,,''****!%!2!2##)ZZG }}n>>$**% "'"67Qb7
7//'4+;+;Q?(1 	JOE8-+
[
!
(
():):8R)H
I	J  7 s   E+%	E&.A.E+&E++E4)rh   rh   rh   r   r   NNN)r   r   r   r   r`   r  r   r   r   r  r  	  s,      $-0^r   r  )FF)2r   rj   r   r  tensorflow.python.eagerr   tensorflow.python.frameworkr   rP   r   r   r   r	   r
   tensorflow.python.lib.ior   tensorflow.python.opsr   r   r   r   r   r   'tensorflow.python.ops.gen_data_flow_opstensorflow.python.utilr   tensorflow.python.util.compatr    tensorflow.python.util.tf_exportr   r   r7   r;   rF   deprecated_endpointsrI   r   r   r   r  r  r  r  r:  rR  rk  r  r  r  r  r   r   r   <module>r$     s       + 9 6 + 3 4 3 . + 1 2 3 * 7 6 . 9 6
 (-(-B	
% >@!!!>;"?@lF lF A@lF\ 78 "!!235QO QO5	8QOh "3[!AB!!!+.:F	 :F / C:F@G%Y G%T LN "!!#8:L"MNEMy EM ONEMP  JL!!!#5"GH?3I ?3 IL?3Jr& r&j +,-L L .L^ '()W7 W *Wt -/MNP#= PDR Rj_)/ _)D~)_ ~)BV Vr   