
    BVh-                     @   d 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Zd!dZd!dZd Zd Zd Zd Zd Z G d d ej(                  dd            Zd Zd Zd Z G d d ej(                  dd            Zd Zd Zd Zd Zy)"zDOperators specific to data structures: list append, subscripts, etc.    N)constant_op)dtypes)ops)tensor_util)	array_ops)cond)list_ops)tensor_array_opsc                 R    | rt        |       }nd}|rt        |      S t        |      S )zThe list constructor.

  Args:
    iterable: Optional elements to fill the list with.

  Returns:
    A list-like object. The exact return value depends on the initial elements.
   )tuple_py_list_newtf_tensor_list_new)iterableelementss     e/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/autograph/operators/data_structures.pynew_listr       s0     XHH !!	H	%%    c                    t        d | D              } t        d | D              }t        |      dk(  r0t        |      \  }|V||k7  rQt        dj	                  || |            t        |      dkD  rt        dj	                  |             |t        d      t        d | D              }t        |      dk(  r0t        |      \  }|K||k7  rFt        d	j	                  || |            t        |      dkD  rt        d
j	                  |             d}|}||}t        j                  |t        |       d|du |      }t        |       D ]  \  }}	|j                  ||	      } |S )8Overload of new_list that stages a Tensor list creation.c              3   F   K   | ]  }t        j                  |        y wNr   convert_to_tensor.0els     r   	<genexpr>z&tf_tensor_array_new.<locals>.<genexpr>7        @3((,@   !c              3   4   K   | ]  }|j                     y wr   dtyper   s     r   r   z&tf_tensor_array_new.<locals>.<genexpr>9        /288/      N7incompatible dtype; specified: {}, inferred from {}: {}z<TensorArray requires all elements to have the same dtype: {}z0dtype is required to create an empty TensorArrayc              3   b   K   | ]'  }t        |j                  j                                ) y wr   r   shapeas_listr   s     r   r   z&tf_tensor_array_new.<locals>.<genexpr>H   "     @5))+,@   -/7incompatible shape; specified: {}, inferred from {}: {}z<TensorArray requires all elements to have the same shape: {}T)r#   sizedynamic_sizeinfer_shapeelement_shape)	r   setlen
ValueErrorformatr
   TensorArray	enumeratewrite)
r   element_dtyper2   
all_dtypesinferred_dtype
all_shapesinferred_shapelir   s
             r   tf_tensor_array_newrA   5   s   @x@@(/h//*_J'ON ]n%D
C
J
JX~78 8 :
	fX    IJJ@x@@*_J'ON ]n%D
C
J
JX~78 8 :
	fX   
 N"M"M""x= D(!#! " ea	2A	
(r   c                    t        j                  |       r>|t        d      t        j                  |       dd }t        j                  | |      }|S t        d | D              } t        d | D              }t        |      dk(  r1t        |      d   }|a||k7  r\t        dj                  || |            |r.|t        d	j                  ||             t        j                  }nt        j                  }t        d
 | D              }t        |      dk(  r;t        j                  | d         }|k||k7  rft        dj                  || |            |r3|t        dj                  ||             t        j                  d      }nt        j                  d      }||}||}t        j                   |t        j"                        }t        j$                  ||      }| D ]  }t        j&                  ||      } |S )r   NzAelement shape may not be specified when creating list from tensorr&   )r2   c              3   F   K   | ]  }t        j                  |        y wr   r   r   s     r   r   z%tf_tensor_list_new.<locals>.<genexpr>q   r   r    c              3   4   K   | ]  }|j                     y wr   r"   r   s     r   r   z%tf_tensor_list_new.<locals>.<genexpr>s   r$   r%   r   r'   z;specified dtype {} is inconsistent with that of elements {}c              3   b   K   | ]'  }t        |j                  j                                ) y wr   r)   r   s     r   r   z%tf_tensor_list_new.<locals>.<genexpr>   r,   r-   r.   z;specified shape {} is inconsistent with that of elements {}r"   r2   r:   )r   
is_tf_typer5   r   r*   r	   tensor_list_from_tensorr   r3   r4   r6   r   variantr   constantr   r   int32empty_tensor_listtensor_list_push_back)	r   r:   r2   r?   r;   r<   r=   r>   r   s	            r   r   r   g   s   H% 
MO OOOH-ab1M((OAH@x@@(/h//*_:&q)N ]n%D
C
J
JX~78 8  
G
N
NX'( ( ^^N^^N@x@@*___Xa[1N ]n%D
C
J
JX~78 8  
G
N
NX'( ( !))"-N ))"-N"M"M''V\\J-  !@! .b&&q"-A.	
(r   c                     t        |       S )z0Overload of new_list that creates a Python list.)list)r   s    r   r   r      s    	hr   c                     t        | t        j                        rt        | |      S t	        j
                  |       r7| j                  t        j                  k(  rt        | |      S t        d| z        t        | |      S )a	  The list append function.

  Note: it is unspecified where list_ will be mutated or not. If list_ is
  a TensorFlow entity, it will not be typically mutated. If list_ is a plain
  list, it will be. In general, if the list is mutated then the return value
  should point to the original entity.

  Args:
    list_: An entity that supports append semantics.
    x: The element to append.

  Returns:
    Same as list_, after the append was performed.

  Raises:
    ValueError: if list_ is not of a known list-like type.
  Otensor lists are expected to be Tensors with dtype=tf.variant, instead found %s)
isinstancer
   r7   _tf_tensorarray_appendr   rH   r#   r   rJ   _tf_tensor_list_appendr5   _py_list_appendlist_xs     r   list_appendrZ      sv    $ '334!%++e${{fnn$#E1-- %&' ' 5!$$r   c                      fd}t        j                   t        j                         dkD   fd|       t        j                         S )z8Overload of list_append that stages a Tensor list write.c                      t        j                        } t        j                  t	        j
                  |       | j                        S )NrG   )r   r   r	   rM   r   r*   r#   )tensor_xrY   s    r   empty_list_of_elements_like_xz=_tf_tensor_list_append.<locals>.empty_list_of_elements_like_x   s9    $$Q'H%%ooh/nn& &r   r   c                       S r   r   rX   s   r   <lambda>z(_tf_tensor_list_append.<locals>.<lambda>   s    e r   )r   r	   tensor_list_lengthrN   )rX   rY   r^   s   `` r   rU   rU      sF    & ))!!%(1,#%
 
	'	'q	11r   c                 B    | j                  | j                         |      S )z8Overload of list_append that stages a TensorArray write.)r9   r/   rW   s     r   rT   rT      s    	UZZ\1	%%r   c                 (    | j                  |       | S )z;Overload of list_append that executes a Python list append.)appendrW   s     r   rV   rV      s     ,,q/	,r   c                       e Zd Zy)ListPopOptsN__name__
__module____qualname__r   r   r   rg   rg      s    r   rg   )r:   r2   c                 "   t        |t              sJ t        | t        j                        rt	        d      t        j                  |       r8| j                  t        j                  k(  rt        | ||      S t	        d| z        t        | |      S )a  The list pop function.

  Note: it is unspecified where list_ will be mutated or not. If list_ is
  a TensorFlow entity, it will not be typically mutated. If list_ is a plain
  list, it will be. In general, if the list is mutated then the return value
  should point to the original entity.

  Args:
    list_: An entity that supports pop semantics.
    i: Optional index to pop from. May be None.
    opts: A ListPopOpts.

  Returns:
    Tuple (x, out_list_):
      out_list_: same as list_, after the removal was performed.
      x: the removed element value.

  Raises:
    ValueError: if list_ is not of a known list-like type or the operation is
    not supported for that type.
  z)TensorArray does not support item removalrR   )rS   rg   r
   r7   r5   r   rH   r#   r   rJ   _tf_tensor_list_pop_py_list_pop)rX   r@   optss      r   list_poprp      s    , 
D+	&&	&'334
@
AAe${{fnn$ 400 %&' ' q!!r   c                     |t        d      |j                  t        d      |j                  t        d      t	        j
                  | |j                        \  }}|j                  |j                         ||fS )z3Overload of list_pop that stages a Tensor list pop.z/tensor lists only support removing from the endz\cannot pop from a list without knowing its element type; use set_element_type to annotate itz]cannot pop from a list without knowing its element shape; use set_element_type to annotate itr:   )NotImplementedErrorr:   r5   r2   r	   tensor_list_pop_back	set_shape)rX   r@   ro   list_outrY   s        r   rm   rm     s    ]
O
PP	
 A B B	
 B C C--4--/+(A++d  !	1r   c                 X    || j                         }| |fS | j                  |      }| |fS )z8Overload of list_pop that executes a Python list append.)pop)rX   r@   rY   s      r   rn   rn     s5    Y		A 
/ 			!A	/r   c                       e Zd Zy)ListStackOptsNrh   r   r   r   rz   rz   '  s     r   rz   )r:   original_callc                    t        |t              sJ t        | t        j                        rt	        |       S t        j                  |       r+| j                  t        j                  k(  rt        | |      S | S t        | |      S )a  The list stack function.

  This does not have a direct correspondent in Python. The closest idiom to
  this is tf.append or np.stack. It's different from those in the sense that it
  accepts a Tensor list, rather than a list of tensors. It can also accept
  TensorArray. When the target is anything else, the dispatcher will rely on
  ctx.original_call for fallback.

  Args:
    list_: An entity that supports append semantics.
    opts: A ListStackOpts object.

  Returns:
    The output of the stack operation, typically a Tensor.
  )rS   rz   r
   r7   _tf_tensorarray_stackr   rH   r#   r   rJ   _tf_tensor_list_stack_py_list_stackrX   ro   s     r   
list_stackr   -  so      
D-	((	('334 ''e${{fnn$"5$// l%&&r   c                 "    | j                         S )z7Overload of list_stack that stages a TensorArray stack.)stackr`   s    r   r}   r}   K  s    	r   c                 r    |j                   t        d      t        j                  | |j                         S )z7Overload of list_stack that stages a Tensor list write.zYcannot stack a list without knowing its element type; use set_element_type to annotate itrr   )r:   r5   r	   tensor_list_stackr   s     r   r~   r~   P  s9    	
 < = =		#	#E9K9K	LLr   c                 $    |j                  |       S )z:Overload of list_stack that executes a Python list append.)r{   r   s     r   r   r   X  s     
		E	""r   r   )NN)__doc__collectionstensorflow.python.frameworkr   r   r   r   tensorflow.python.opsr   r   r	   r
   r   rA   r   r   rZ   rU   rT   rV   
namedtuplerg   rp   rm   rn   rz   r   r}   r~   r   r   r   r   <module>r      s    K  3 . + 3 + & * 2&*/d8v
%>2 &
K=*LM
""J"K?=?'<
M#r   