
    AVhW                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ  ej2                  ej4                        ej6                  dfdedej8                  fd       Z ej2                  ej:                        dej6                  fdedej8                  fd       Z ej>                  ej@                  e       ejB                  ddd      d3d              Z  ej>                  ejD                  e      d4d       Z" ej>                  ejF                  e      	 	 	 	 d5d       Z# ej>                  ejH                  e      d6de%fd       Z$ ej>                  ejL                  e      d7d       Z& ej>                  ejN                  e      d7d       Z' ej>                  ejP                  e      d7d       Z( ej>                  ejR                  e      d8d        Z) ej>                  ejT                  e      d3d!       Z* ej>                  ejV                  e      d8d"       Z+ ej>                  ejX                  e      d3d#       Z, ej>                  ejZ                  e      d4d$       Z-d4d%Z.d& Z/d4d'Z0d4d(Z1d) Z2d* Z3d+ Z4d, Z5d- Z6d. Z7d/ Z8d0 Z9d1 Z:d2 Z;y)9zStructuredTensor array ops.    )Sequence)flags)constant_op)dtypes)ops)tensor)	array_ops)math_ops)
random_ops)dynamic_ragged_shape)ragged_tensor)RowPartition)StructuredTensor)deprecation)dispatchNinputreturnc                 :    ~| j                   j                  |      S z?Returns a DynamicRaggedShape containing the shape of the input._ragged_shape
with_dtyper   out_typenames      e/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/structured/structured_array_ops.pyshape_v2r   #        				'	'	11    c                 :    ~| j                   j                  |      S r   r   )r   r   r   s      r   shape_v1r!   +   r   r   zUse the `axis` argument insteaddimc                 N    t        j                  d|d|      }t        | ||      S )a  Creates a StructuredTensor with a length 1 axis inserted at index `axis`.

  This is an implementation of tf.expand_dims for StructuredTensor. Note
  that the `axis` must be less than or equal to rank.

  >>> st = StructuredTensor.from_pyval([[{"x": 1}, {"x": 2}], [{"x": 3}]])
  >>> tf.expand_dims(st, 0).to_pyval()
  [[[{'x': 1}, {'x': 2}], [{'x': 3}]]]
  >>> tf.expand_dims(st, 1).to_pyval()
  [[[{'x': 1}, {'x': 2}]], [[{'x': 3}]]]
  >>> tf.expand_dims(st, 2).to_pyval()
  [[[{'x': 1}], [{'x': 2}]], [[{'x': 3}]]]
  >>> tf.expand_dims(st, -1).to_pyval()  # -1 is the same as 2
  [[[{'x': 1}], [{'x': 2}]], [[{'x': 3}]]]

  Args:
    input: the original StructuredTensor.
    axis: the axis to insert the dimension: `-(rank + 1) <= axis <= rank`
    name: the name of the op.
    dim: deprecated: use axis.

  Returns:
    a new structured tensor with larger rank.

  Raises:
    an error if `axis < -(rank + 1)` or `rank < axis`.
  axisr"   r   )r   deprecated_argument_lookup_expand_dims_impl)r   r$   r   r"   s       r   expand_dimsr(   3   s)    < 
	/	/eS	I$	5$T	22r   c                     t        | ||      S )a  Creates a StructuredTensor with a length 1 axis inserted at index `axis`.

  This is an implementation of tf.expand_dims for StructuredTensor. Note
  that the `axis` must be less than or equal to rank.

  >>> st = StructuredTensor.from_pyval([[{"x": 1}, {"x": 2}], [{"x": 3}]])
  >>> tf.expand_dims(st, 0).to_pyval()
  [[[{'x': 1}, {'x': 2}], [{'x': 3}]]]
  >>> tf.expand_dims(st, 1).to_pyval()
  [[[{'x': 1}, {'x': 2}]], [[{'x': 3}]]]
  >>> tf.expand_dims(st, 2).to_pyval()
  [[[{'x': 1}], [{'x': 2}]], [[{'x': 3}]]]
  >>> tf.expand_dims(st, -1).to_pyval()  # -1 is the same as 2
  [[[{'x': 1}], [{'x': 2}]], [[{'x': 3}]]]

  Args:
    input: the original StructuredTensor.
    axis: the axis to insert the dimension: `-(rank + 1) <= axis <= rank`
    name: the name of the op.

  Returns:
    a new structured tensor with larger rank.

  Raises:
    an error if `axis < -(rank + 1)` or `rank < axis`.
  r%   )r'   )r   r$   r   s      r   expand_dims_v2r*   U   s    8 
5$T	22r   c                    |d}t        j                  |      5  t        j                  | j                  j
                  d      t        j                  d      fd}t        | |      cddd       S # 1 sw Y   yxY w)a  tf.gather for structured tensors.

  Does not support (yet) checks on illegal axis values, et cetera.

  Indices must be a ragged or dense tensor.
  Args:
    params: a structured tensor to be gathered
    indices: a ragged tensor or tensor to gather by.
    validate_indices: whether to validate the indices
    name: the name of the op(s).
    axis: the axis in params to gather on.
    batch_dims: the number of batch dimensions.

  Returns:
    the params reorganized according to indices.
  Ngatherzparams.shape.rank)
ndims_nameindicesr%   c                 :    t        j                  | d       S )N)validate_indicesr$   
batch_dimsr   )r	   r,   )pr$   r1   r.   r0   s    r   leaf_opzgather.<locals>.leaf_op   s(    

+ r   )	r   
name_scoper	   get_positive_axisshaperankr   "convert_to_tensor_or_ragged_tensor_extend_op_single)paramsr.   r0   r   r$   r1   r3   s    `` `` r   r,   r,   t   s    . 
\D
~~d .|d&&tV\\->->2EGD>>i!G VW-#. . .s   ABBr   c                     |d}t        |        fd}t        j                  | d   j                        t	        j
                  |d|       5  t        | |      cddd       S # 1 sw Y   yxY w)a#  tf.concat for structured tensors.

  Does not support (yet) checks on illegal axis values, et cetera.

  Args:
    values: a sequence of StructuredTensors.
    axis: an axis to concatenate upon.
    name: the name of the op(s).

  Returns:
    the params reorganized according to indices.
  Nconcatc                 0    t        j                  |       S N)r	   r<   )valuesr$   s    r   r3   zconcat.<locals>.leaf_op   s    FD))r   r   StructuredConcat),_assert_concat_compatible_structured_tensorsr	   r5   r7   r   r4   
_extend_op)r?   r$   r   r3   s    `  r   r<   r<      sh     
\D.v6* 
	$	$T6!9>>	:$
~~d.7 'fg&' ' 's   A&&A/c                 $   t        j                  |d| |g      5  | j                  dk(  rt        d      | j	                         }t        j                  t        j                  |      |      }t        | |d      cddd       S # 1 sw Y   yxY w)zShuffle a structured tensor on the zeroth axis.

  Args:
    value: a structured tensor of rank at least one.
    seed: the seed for shuffling.
    name: the name for shuffle.

  Returns:
    The shuffled structured tensor.
  shuffler   z(Cannot shuffle a scalar StructuredTensor)seed)r$   N)
r   r4   r7   
ValueErrornrowsr   random_shuffler
   ranger,   )valuerE   r   first_dimensionindexs        r   rH   rH      sy     ~~dIt}5 (zzQABBkkmO%%hnn_&E+/1E%Q'( ( (s   A"BBc                     |Mt        j                         j                  j                         rt        j
                  }nt        j                  }t        | ||      S )Returns the size of a tensor.)r   r   )r   configtf_shape_default_int64rJ   r   int64int32sizer   s      r   size_v2rT      sC     ||~,,224hh	e$	22r   c                 R   |Mt        j                         j                  j                         rt        j
                  }nt        j                  }t        j                  |d| g      5 }| j                  s\| j                         -t        j                  | j                         |      cddd       S t        j                  d|      cddd       S | j                  d   j                         }|||cddd       S t        j                  ||      cddd       S # 1 sw Y   yxY w)rN   NrS      dtype)r   rO   rP   rJ   r   rQ   rR   r   r4   row_partitionsrG   r
   castnvals)r   r   r   r\   s       r   rS   rS      s     ||~,,224hh
~~dFUG, 
0		"}}U[[]H5
0 
0
 }}Q)
0 
0   $**,E}(
0 
0 ==h/
0 
0 
0s   (A D2D"D=DD&c                      ~t        | ||      S z<Implementation of zeros_like for StructuredTensor for TF v1.)rY   r   )zeros_like_v2r   rY   r   optimizes       r   
zeros_likerb      s     	vU	66r   c                 X   ||j                         st        d|       |t        j                  }t	        j
                  |d| g      5 }| j                  sa| j                         0t        j                  | j                         g||      cddd       S t        j                  g ||      cddd       S | j                  d   }t        j                  j                  t        j                  |j                         |      | j                        }|cddd       S # 1 sw Y   yxY w)ao  Replace every object with a zero.

  Example:
  >>> st = StructuredTensor.from_pyval([{"x":[3]}, {"x":[4,5]}])
  >>> tf.zeros_like(st)
  <tf.Tensor: shape=(2,), dtype=int32, numpy=array([0.0, 0.0], dtype=float32)>
  >>> st = StructuredTensor.from_pyval([[{"x":[3]}], [{"x":[4,5]}, {"x":[]}]])
  >>> tf.zeros_like(st, dtype=tf.int32)
  <tf.RaggedTensor [[0], [0, 0]]>

  Args:
    input: a structured tensor.
    dtype: the dtype of the resulting zeros. (default is tf.float32)
    name: a name for the op.
    layout: Optional Layout. Only supports replicated layout.

  Returns:
    a tensor of zeros of the same shape.
  N4StructuredTensor only allows replicated layout. got rb   layoutrW   rX   )is_fully_replicatedrF   r   float32r   r4   rZ   rG   r	   zerosr   RaggedTensor_from_nested_row_partitionsr\   r   rY   r   rf   last_row_partitionresults         r   r_   r_      s   *  : : <

>vhG  ]NNE
~~dL5'2 d		"fE 
 r58  --b1''CC*002%@F      AD D 9AD  D)c                      ~t        | ||      S r^   )ones_like_v2r`   s       r   	ones_likerr   &  s     	fE	55r   c                 X   ||j                         st        d|       |t        j                  }t	        j
                  |d| g      5 }| j                  sa| j                         0t        j                  | j                         g||      cddd       S t        j                  g ||      cddd       S | j                  d   }t        j                  j                  t        j                  |j                         |      | j                        }|cddd       S # 1 sw Y   yxY w)am  Replace every object with a zero.

  Example:
  >>> st = StructuredTensor.from_pyval([{"x":[3]}, {"x":[4,5]}])
  >>> tf.ones_like(st)
  <tf.Tensor: shape=(2,), dtype=int32, numpy=array([1.0, 1.0], dtype=float32)>
  >>> st = StructuredTensor.from_pyval([[{"x":[3]}], [{"x":[4,5]}, {"x":[]}]])
  >>> tf.ones_like(st, dtype=tf.int32)
  <tf.RaggedTensor [[1], [1, 1]]>

  Args:
    input: a structured tensor.
    dtype: the dtype of the resulting zeros. (default is tf.float32)
    name: a name for the op.
    layout: Optional Layout. Only supports replicated layout.

  Returns:
    a tensor of zeros of the same shape.
  Nrd   rr   re   rW   rX   )rg   rF   r   rh   r   r4   rZ   rG   r	   onesr   rj   rk   r\   rl   s         r   rq   rq   .  s   *  : : <

>vhG  ]NNE
~~dK%1 T		"~~u{{}ouVD 
 ~~b%7  --b1''CC)//1?F   ro   c                     t        j                  |d| g      5 }t        j                  | j                  t
        j                        cddd       S # 1 sw Y   yxY w)zReturns the rank of a tensor.r7   rX   N)r   r4   r   constantr7   r   rR   )r   r   s     r   r7   r7   Y  sI     ~~dFUG, @

&,,?@ @ @s   /AAc           
         t        j                  || j                  dz   dd      }t        j                  |d| |g      5  | j
                  j                         D ci c]  \  }}|t        j                  ||       }}}| j                  d| dz   | j                  |d z   }t        | |      }|dkD  r| j                         nd}t        j                  ||||	      cddd       S c c}}w # 1 sw Y   yxY w)
a  Creates a StructuredTensor with a length 1 axis inserted at index `axis`.

  This is an implementation of tf.expand_dims for StructuredTensor. Note
  that the `axis` must be less than or equal to rank.

  >>> st = StructuredTensor.from_pyval([[{"x": 1}, {"x": 2}], [{"x": 3}]])
  >>> tf.expand_dims(st, 0).to_pyval()
  [[[{'x': 1}, {'x': 2}], [{'x': 3}]]]
  >>> tf.expand_dims(st, 1).to_pyval()
  [[[{'x': 1}, {'x': 2}]], [[{'x': 3}]]]
  >>> tf.expand_dims(st, 2).to_pyval()
  [[[{'x': 1}], [{'x': 2}]], [[{'x': 3}]]]
  >>> tf.expand_dims(st, -1).to_pyval()  # -1 is the same as 2
  [[[{'x': 1}], [{'x': 2}]], [[{'x': 3}]]]

  Args:
    st: the original StructuredTensor.
    axis: the axis to insert the dimension: `-(rank + 1) <= axis <= rank`
    name: the name of the op.

  Returns:
    a new structured tensor with larger rank.

  Raises:
    an error if `axis < -(rank + 1)` or `rank < axis`.
  rV   r$   zrank(st))	axis_namer-   
ExpandDimsN)rV   r   r6   rZ   rG   )r	   r5   r7   r   r4   _fieldsitemsr(   r6   _expand_st_row_partitionsrG   r   from_fields)	str$   r   kv
new_fields	new_shapenew_row_partitions	new_nrowss	            r   r'   r'   a  s    6 
	$	$
BGGaK6j
B$
~~dL2t*5 8:

8H8H8J.4q!9  D))J  $$&$%8I22t<#ax
aI'')	  s    C/!C)?A C/)C//C8c                 ~   |dk(  rS| j                   j                  dk(  ry| j                         }t        j                  ||dd      }|f| j
                  z   S || j                  k(  r_|dz
  dk\  r | j
                  |dz
     j                         n| j                         }| j
                  t        j                  d||d      fz   S |dz
  dk\  r | j
                  |dz
     j                         n| j                         }| j
                  d|dz
   t        j                  d||d      fz   | j
                  |dz
  d z   S )z*Create the row_partitions for expand_dims.r    rV   F)rG   validate   N)r6   r7   rG   r   from_uniform_row_lengthrZ   r\   )r   r$   r\   new_partitions       r   r}   r}     sG   	QY	xx}}HHJE 88uA/Mb////rww04qA$(#))+BHHJ 
 D D	5!/  1 1 1 15qA$(#))+BHHJ 
YdQh'<+O+O	5,/ +1 1353D3DTAXY3OP Pr   c           	         t        | t              st        d      | st        d      |t        |      }| d   }t        |t              r ||       }|j                         s|S i }|j                         D ]/  }t        | D cg c]  }|j                  |       c}||      ||<   1 t	        j                  ||j                        S  ||       S c c}w )a  Extend an op from RaggedTensor and Tensor to StructuredTensor.

  Visits all children of the structured tensor, and children of children,
  applying leaf_op whenever it reaches a leaf, and empty_st_op whenever
  it reaches an internal node without children.

  Args:
    values: a list of structured tensors, ragged tensors, or tensors. All must
      have the same type. If they are structured tensors, they must have the
      same paths.
    leaf_op: an op for handling non-structured tensor.
    empty_st_op: op to create a structured tensor without fields.

  Returns:
    the result of the extended op (a StructuredTensor, RaggedTensor, or Tensor)

  Raises:
    ValueError:
      If values is not a Sequence or is empty.
  zExpected a listzList cannot be emptyr   r6   )

isinstancer   rF   empty_st_op_like_zerosr   field_namesrB   field_valuer~   r6   )r?   r3   empty_st_oprJ   empty_resultr   r   r   s           r   rB   rB     s    * 
FH	%
&
''	
+
,,(1K )%'(v&LJ  . F!Cq!--"2!CW!,.jm. ''
,:L:LMM6?	 "Ds   C
c                 <    d }t        | g ||       ||            S )z4Extend an op to a value instead of a list of values.c                       y  fd}|S )Nc                     | \  } |      S r>   r   )r?   rJ   
element_ops     r   list_opz6_extend_op_single.<locals>.to_list_op.<locals>.list_op  s    gur   r   )r   r   s   ` r   
to_list_opz%_extend_op_single.<locals>.to_list_op  s     Nr   )rB   )rJ   r3   r   r   s       r   r9   r9     s$     
UGZ0*[2I	JJr   c                       fd}|S )Nc                     | D cg c]  }t        |t        j                         }} |      }t        |      S c c}w )NrX   )r_   r   rR   _structured_tensor_like)r?   rJ   as_zerosrn   r3   s       r   r   z+empty_st_op_like_zeros.<locals>.empty_st_op  sD    >D5:e6<<0H  XF"6**	s   "=r   )r3   r   s   ` r   r   r     s    + 
r   c                    | j                   j                         r!t        j                  i | j                         S | j                   j                  t        d      | j                   j                  dk(  r8t        j                  i | j                   t        j                   |       d         S t        j                  j                  |       }t        | j                   |j                        S )z<Create a structured tensor with the shape of a dense tensor.r   z,Can't build StructuredTensor w/ unknown rankrV   r   )r6   rG   )r6   is_fully_definedr   r~   r7   rF   r	   r   rj   from_tensor&_structured_tensor_from_row_partitions_nested_row_partitions)trts     r   $_structured_tensor_from_dense_tensorr     s     WW''!''::ww||
C
DDww||q''!''.7ooa.@.CE E 
	#	#	/	/	2B1!''242K2KM Mr   c                 2    t        j                  i | |      S )Nr6   rZ   )r   r~   r   s     r   r   r     s    		%	%b,15C
E Er   c                    t        | t        j                        rE| j                  j                  dk  ryt
        j                  j                  |       }|j                  S t        | j                        }| j                  }||z   S )zHReturns all nested row partitions in rt, including for dense dimensions.rV   r   )r   
tensor_libTensorr6   r7   r   rj   r   r   _all_nested_row_partitionsflat_values)r   rt2tail_partitionshead_partitionss       r   r   r     sk    J%%&	xx}}&&2226c'''0@O//O_,,r   c                 J   t        | t        j                        rt        |       S t	        j
                  |       r/t        j                  i | j                         t        |             S t        j                  i | j                  | j                  | j                               S )zACreate a StructuredTensor with the shape of a (composite) tensor.r   rz   )r   r   r   r   r   	is_raggedr   r~   	get_shaper   r6   rZ   rG   )r   s    r   r   r     s    :$$%/22Q''
!++-0J10MO O 
	%	%b,-GG565E5E,-GGI
7 7r   c                    | j                         }dh}|D ]b  }| j                  |      }t        |t              r-|j	                  t        |      D cg c]  }|f|z   
 c}      }Q|j                  |f       d |S c c}w )z*Get all the paths from a StructuredTensor.r   )r   r   r   r   union_get_all_pathsadd)r   fields	all_pathsr   r   r2   s         r   r   r   !  s}    >>&d) a
qA!%&//^A5F"GA4!8"GHimmQD 
 #Hs   A=
c                     | j                         }d| j                  i}|D ]N  }| j                  |      }t        |t              s%t        |      j                         D ]  \  }}|||f|z   <    P |S )z3Get ranks of all submessages of a StructuredTensor.r   )r   r7   r   r   r   _get_all_ranksr|   )r   r   	all_ranksr   r   k2v2s          r   r   r   .  s|    >>&277m) "a
qA!%&$Q'--/ "(2r!	1$)""
 
r   c                     | D cg c]  }t        |       }}t               }|dd D ]%  }|j                  |d   j                  |            }' |rt	        d|      yc c}w )z/Raises an error if the paths are not identical.rV   Nr   zASome paths are present in some, but not all, structured tensors: )r   setr   symmetric_differencerF   )r?   r   paths	path_diffother_pathss        r   _assert_all_paths_matchr   :  sw    (.
/">"
/%
/e)12Y Lka = =k JKIL
		  	 0s   A!c                 x    | D cg c]  }t        |       }}|dd D ]  }||d   k7  st        d       yc c}w )z>Raises an error if the ranks of submessages are not identical.rV   Nr   z!Ranks of sub-message do not match)r   rF   )r?   r   ranksother_rankss       r   _assert_all_ranks_matchr   F  sN    (.
/">"
/%
/12Y <keAh :;;	< 0s   7c                     t        | t              st        d      | st        d      | D ]  }t        |t              rt        d       t	        |        t        |        y)a  Sometimes raises an error if concat doesn't make sense statically on values.

  values must be a sequence, and each element in values must be a structured
  tensor, and must have the same paths. Additionally, each path that is a
  submessage must have the same rank.

  These constraints are sufficient for concat on the fields to be the same
  as concat on structured tensors. This is meant to capture scenarios like
  paths that are not in the first structured tensor, but are in later
  structured tensors, which will just be ignored by the recursive algorithm.

  If the rank of a submessage was different for two structured tensors,
  then that is also a non-sensical merge.

  Note that all of these checks are static, as paths and submessage ranks
  are known.

  Args:
    values: a Sequence of StructuredTensors.

  Raises:
    ValueError: if there is any inconsistency as described above.
  z7values must be a list of StructuredTensors (not a list)z values must not be an empty listz*values must be a list of StructuredTensorsN)r   r   rF   r   r   r   )r?   r   s     r   rA   rA   P  sc    0 
FH	%
N
OO	
7
88 Ebb*+CDDE &!&!r   )NNNr>   )NNNr   )r<   )NN)NNT)<__doc__typingr   tensorflow.core.configr   tensorflow.python.frameworkr   r   r   r   r   tensorflow.python.opsr	   r
   r   tensorflow.python.ops.raggedr   r   *tensorflow.python.ops.ragged.row_partitionr   2tensorflow.python.ops.structured.structured_tensorr   tensorflow.python.utilr   r   dispatch_for_apir   rR   DynamicRaggedShaper6   r!   dispatch_for_typesr(   deprecated_argsr*   r,   r<   strrH   rT   rS   rb   r_   rr   rq   r7   r'   r}   rB   r9   r   r   r   r   r   r   r   r   r   rA   r   r   r   <module>r      sC   "  ( 3 . + < + * , = 6 C O . + 9--./5||2$ 2/BB2 /2 9??++/ll2$ 2';'N'N2 ,2 Y224DET#DeL3 M F3@ Y557GH3 I3< Y--/?@ !). A).X Y--/?@'s ' A'0 Z668HI( J(( Y..0@A3 B3 Y^^-=>0 ?0, Y113CD7 E7 Y446FG& H&T Y002BC6 D6 Y335EF' G'T Y^^-=>@ ?@(VP,,^K 	M"E-7
		< "r   