
    AVha                        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mZ ddlmZ  ed      ej4                  ddddej6                  fd              Zd Z ej<                  d       dZ	 	 dkdZ  ejB                  ejD                        	 dldejF                  dejF                  fd       Z$ ejB                  ejJ                        	 dldejF                  dejF                  fd       Z& ejB                  ejN                        	 dldejF                  dejF                  fd       Z( ejB                  ejR                        	 dldejF                  dejF                  fd       Z* ejB                  ejV                        	 dldejF                  dejF                  fd        Z, ejB                  ejZ                        	 dldejF                  dejF                  fd!       Z.d" Z/ e/e$d#d$        e/e&d%d&        e/e(d'd(        e/e*d)d*        e/e,d+d,        e/e.d-d$       d.Z0d/Z1d0Z2d1Z3d2Z4d3Z5d4Z6d5Z7d6Z8d7Z9	 	 dkd8Z: ejB                  ejv                        	 	 	 dmd9ejx                  fd:       Z; ejB                  ejz                        	 	 	 dmd9ejx                  fd;       Z= ejB                  ej|                        	 	 	 dmd9ejx                  fd<       Z> ejB                  ej~                        	 	 	 dmd9ejx                  fd=       Z? ejB                  ej                        	 	 	 dmd9ejx                  fd>       Z@ ejB                  ej                        	 	 	 dnd9ejx                  fd?       ZA ejB                  ej                        	 	 	 dnd9ejx                  fd@       ZBdA ZC ejB                  ej                        	 	 	 dmd9ejx                  fdB       ZD ejB                  ej                        	 	 	 dmd9ejx                  fdC       ZEdD ZF eFe;d#d$dEe1        eFe=d%d&dFe2        eFe>d'd(dGe3        eFe?d)d*dHe4        eFe@d+d,dIe5        eFeAdJd,dIe6        eFeBdKd,dIe7        eFeDdLdMdNe8        eFeEdOdPdQe9        ejB                  ej                        	 	 	 	 	 	 	 	 	 	 dodRejF                  dSejF                  fdT       ZGdU ZHdV ZIdW ZJ ejB                  ej                        dkdXejx                  fdY       ZL ejB                  ej                        dldZej                  ejF                     fd[       ZM ejB                  ej                        	 	 	 	 	 dpd\ejx                  fd]       ZP ejB                  ej                        	 	 	 dmd\ejx                  fd^       ZQ ejB                  ej                        	 	 	 dmd\ejx                  fd_       ZR ejB                  ej                        d`ejF                  daejF                  fdb       ZS ejB                  ej                        d`ejF                  daejF                  fdc       ZTdd ZU	 	 dqdeZV ejB                  ej                        	 	 	 	 drd\ejx                  dfeXdgeYdheYdiej                  e[   f
dj       Z\y)szSupport for ragged tensors.    N)dtypes)errors)ops)tensor)tensor_util)	array_ops)	check_ops)gen_ragged_math_ops)map_fn)math_ops)nn_ops)ragged_functional_ops)ragged_tensor)segment_id_ops)dispatch)	tf_exportzragged.range   c                 j   t        j                  |      }|d| }} t        j                  |d| ||g      5 }t        j                  | |d      } t        j                  ||d      }t        j                  ||d      }|Ot        | ||gt         j                  t         j                  t         j                  t         j                  g      \  } }}t        j                  | ||||      }t        j                  j                  |j                  |j                   d	
      cddd       S # 1 sw Y   yxY w)a'  Returns a `RaggedTensor` containing the specified sequences of numbers.

  Each row of the returned `RaggedTensor` contains a single sequence:

  ```python
  ragged.range(starts, limits, deltas)[i] ==
      tf.range(starts[i], limits[i], deltas[i])
  ```

  If `start[i] < limits[i] and deltas[i] > 0`, then `output[i]` will be an
  empty list.  Similarly, if `start[i] > limits[i] and deltas[i] < 0`, then
  `output[i]` will be an empty list.  This behavior is consistent with the
  Python `range` function, but differs from the `tf.range` op, which returns
  an error for these cases.

  Examples:

  >>> tf.ragged.range([3, 5, 2]).to_list()
  [[0, 1, 2], [0, 1, 2, 3, 4], [0, 1]]
  >>> tf.ragged.range([0, 5, 8], [3, 3, 12]).to_list()
  [[0, 1, 2], [], [8, 9, 10, 11]]
  >>> tf.ragged.range([0, 5, 8], [3, 3, 12], 2).to_list()
  [[0, 2], [], [8, 10]]

  The input tensors `starts`, `limits`, and `deltas` may be scalars or vectors.
  The vector inputs must all have the same size.  Scalar inputs are broadcast
  to match the size of the vector inputs.

  Args:
    starts: Vector or scalar `Tensor`.  Specifies the first entry for each range
      if `limits` is not `None`; otherwise, specifies the range limits, and the
      first entries default to `0`.
    limits: Vector or scalar `Tensor`.  Specifies the exclusive upper limits for
      each range.
    deltas: Vector or scalar `Tensor`.  Specifies the increment for each range.
      Defaults to `1`.
    dtype: The type of the elements of the resulting tensor.  If not specified,
      then a value is chosen based on the other args.
    name: A name for the operation.
    row_splits_dtype: `dtype` for the returned `RaggedTensor`'s `row_splits`
      tensor.  One of `tf.int32` or `tf.int64`.

  Returns:
    A `RaggedTensor` of type `dtype` with `ragged_rank=1`.
  Nr   RaggedRangestarts)dtypenamelimitsdeltas)Tsplitsr   Fvalidate)r   as_dtyper   
name_scopeconvert_to_tensor_infer_matching_dtypeint32int64float32float64r
   ragged_ranger   RaggedTensorfrom_row_splitsrt_dense_valuesrt_nested_splits)r   r   r   r   r   row_splits_dtyperesults          \/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/ragged/ragged_math_ops.pyranger.   ,   s   j __%56^FF
~~dMFFF+CD I""6XFF""6XFF""6XFF }466
"<<v~~v~~
F Hfff !--(8tEF%%55 7 7% 6 II I Is   C)D))D2c                     t        fd| D              sJ t        | D cg c]  }|j                   c}j                        }| D cg c]  }t	        j
                  ||       c}S c c}w c c}w )zBInfers a matching dtype for tensors, and casts them to that dtype.c              3   :   K   | ]  }|j                   v   y wN)r   ).0tdtype_hierarchys     r-   	<genexpr>z(_infer_matching_dtype.<locals>.<genexpr>x   s     9AQWW'9s   )key)allmaxr   indexr   cast)tensorsr4   r3   inferred_dtypes    `  r-   r!   r!   v   s\    	99	99	91A17L7LM.4;	<q(-->
*	<< 2	<s   A+A0r   a  Computes the %(combination)s along segments of a RaggedTensor.

  Returns a RaggedTensor `output` with `num_segments` rows, where the row
  `output[i]` is formed by taking the %(combination)s of all rows of `data`
  whose corresponding `segment_id` is `i`.

  The length of the row `output[i]` will be the maximum of the lengths of
  all rows of `data` whose corresponding `segment_id` is `i`.  If no `data`
  rows correspond to a given segment ID, then the output row for that segment
  ID will be empty.

  Args:
    data: A `RaggedTensor` containing the values to combine.
    segment_ids: A `Tensor` or `RaggedTensor`.  Must have type `int64` or
      `int32`.  `segment_ids.shape` must be a prefix of `data.shape`.
      Must be greater than or equal to zero, and less than `num_segments`.
      `segment_ids` is not required to be sorted.
    num_segments: An `int32` or `int64` scalar specifying the number of
      distinct segment ids.
    name: A name prefix for the returned tensor (optional).
  Returns:
    A `RaggedTensor` containing the %(combined)s values.  The returned tensor
    has the same dtype as `data`, and its shape is
    `[num_segments] + data.shape[segment_ids.rank:]`.
  Raises:
    ValueError: If `segment_ids.shape` is not a prefix of `data.shape`.
c           	      `   t        j                  |      s.t        j                  |      s| | |||||      S  | ||||      S t        j                  |d|||g      5 }t        j                  |d      }t        j                  |d      }t        j
                  ||      \  }}|j                  t        j                  t        j                  fvrt        d      t        j                  |      rt        j                  |      st        d      t        j                  |j                  |j                  d	      }t        j                  |g      5  t        | |j                   |j                   ||      cddd       cddd       S |j                  d
d |j                  dd z
  }t#        j$                  t#        j&                  |||      d      }t)        j*                  t)        j,                  d
g|j                        t#        j.                  |      gd      }	t)        j0                  |	|      }
|
|z   }t3        |
|      j                   }t        | |j                   ||	d   |      }t         j4                  j7                  ||	d      cddd       S # 1 sw Y   xY w# 1 sw Y   yxY w)a  Aggregates along segments of a RaggedTensor using `unsorted_segment_op`.

  Returns a RaggedTensor `output` with `num_segments` rows, where the row
  `output[i]` is formed by combining all rows of `data` whose corresponding
  `segment_id` is `i`.  The values in each row are combined using
  `unsorted_segment_op`.

  The length of the row `output[i]` will be the maximum of the lengths of
  all rows of `data` whose corresponding `segment_id` is `i`.  If no `data`
  rows correspond to a given segment ID, then the output row for that segment
  ID will be empty.

  Args:
    unsorted_segment_op: The tensorflow `op` that should be used to combine
      values in each row.  Must have the same signature and basic behavior as
      `unsorted_segment_sum`, `unsorted_segment_max`, etc.
    data: A `RaggedTensor` containing the values to be combined.
    segment_ids: A `Tensor` or `RaggedTensor`.  Must have type `int64` or
      `int32`.  `segment_ids.shape` must be a prefix of `data.shape`.
      `segment_ids` is not required to be sorted.
    num_segments: An `int32` or `int64` scalar.
    separator: An optional string. Defaults to None. The separator to use when
      joining. Only used for string types.
    name: A name prefix for the returned tensor (optional).

  Returns:
    A `RaggedTensor` containing the aggregated values.  The returned tensor
    has the same dtype as `data`, and its shape is
    `[num_segments] + data.shape[segment_ids.rank:]`.
  Raises:
    ValueError: If segment_ids.shape is not a prefix of data.shape.
  NRaggedSegmentdatar   segment_idsz+segment_ids must have dtype int32 or int64.z\segment_ids.shape must be a prefix of data.shape, but segment_ids is ragged and data is not.z0segment_ids.shape must be a prefix of data.shapemessager   r   axisFr   )r   	is_raggedr   r   "convert_to_tensor_or_ragged_tensormatch_row_splits_dtypesr   r   r"   r#   
ValueErrorr	   assert_equal
row_splitscontrol_dependencies_ragged_segment_aggregatevaluesr   maximumunsorted_segment_maxr   concatzeroscumsumgatherr.   r'   r(   )unsorted_segment_opr?   rA   num_segments	separatorr   check_splitsdata_row_lengthsoutput_row_lengthsoutput_splitsdata_row_to_out_row_startdata_row_to_out_row_limitdata_val_to_out_val_indexoutput_valuess                 r-   rN   rN      s   L 
!
!$
'

!
!+
. {L)!%' ' !{L$GG
~~dO[,79 76<@;;DvNDBB-)K%==dKPD+v|| <<DEE{+$$T* F G 	G++

 
 
//DFl ##\N3 4()<dkk)4););\)244 4#76 76. qr*T__Sb-AA
 "))%%&6&2	4568
 $$/556*+& +,	-M !* 0 0 L 9<L L
 !&&?&?!AAG  ..A4;;.G.;B.?LM %%55}u 6 6m76 76"4 4#76 76s,   C6J$#J7	J$
DJ$J!	J$$J-r?   rA   c                 F    t        t        j                  | |||xs d      S )NRaggedSegmentSumr?   rA   rW   r   )rN   r   unsorted_segment_sumrc   s       r-   segment_sumre     ,     
###&&
) )    c                 F    t        t        j                  | |||xs d      S )NRaggedSegmentProdrc   )rN   r   unsorted_segment_prodrc   s       r-   segment_prodrk     s,     
#$$''
* *rg   c                 F    t        t        j                  | |||xs d      S )NRaggedSegmentMinrc   )rN   r   unsorted_segment_minrc   s       r-   segment_minro   '  rf   rg   c                 F    t        t        j                  | |||xs d      S )NRaggedSegmentMaxrc   )rN   r   rQ   rc   s       r-   segment_maxrr   5  rf   rg   c                    t        j                  |d| ||g      5  t        | ||      }t        j                  j                  t        j                  | j                        | j                  d      }t        |||      }t        j                  |      r1|j                  |j                  |j                  z        cddd       S ||z  cddd       S # 1 sw Y   yxY w))For docs, see: _RAGGED_SEGMENT_DOCSTRING.RaggedSegmentMeanFr   N)r   r   re   r   r'   from_nested_row_splitsr   	ones_likeflat_valuesnested_row_splitsrG   with_flat_valuesr?   rA   rW   r   totalonescounts          r-   segment_meanr   C  s     ~~d/[,79 k<8E%%<<D,,- = D k<8Eu%##E$5$58I8I$IJ  U]  s   B CCCc                 
   t        j                  |d| ||g      5  t        | ||      }t        j                  j                  t        j                  | j                        | j                  d      }t        |||      }t        j                  |      rD|j                  |j                  t        j                  |j                        z        cddd       S |t        j                  |      z  cddd       S # 1 sw Y   yxY w)rt   RaggedSegmentSqrtNFr   N)r   r   re   r   r'   rv   r   rw   rx   ry   rG   rz   r   sqrtr{   s          r-   segment_sqrt_nr   W  s     ~~d0[,79 *k<8E%%<<D,,- = D k<8Eu%##E$5$5$,MM%2C2C$D%E F* * X]]5))* * *s   B3C9C99Dc                 6    t         t        ||      z  | _        y )N)combinationcombined)_RAGGED_SEGMENT_DOCSTRINGdict__doc__)funcr   r   s      r-   _set_ragged_segment_docstringr   l  s    *T.2 2$,rg   sumsummedproduct
multipliedminimum	minimizedrP   	maximizedmeanaveragedzsum divided by sqrt(N)a\  Computes the %(combination)s of elements across dimensions of a `RaggedTensor`.

  Reduces `input_tensor` along the dimensions given in `axis` by taking the
  %(combination)s of values.  If a reduced dimension has no elements for
  some index, then the value for that index will be %(default)s.

  The rank of the tensor is reduced by `1` for each entry in `axis`.  If
  `axis` is not specified, then all dimensions are reduced, and a scalar
  value is returned.
  Args:
    input_tensor: A `RaggedTensor` containing the values to be %(combined)s.
    axis: The dimensions to reduce.  May be `None` (to reduce all axes), an
      `int` (to reduce a single axis), a `list` or `tuple` of `int` (to reduce
      a given set of axes), or a `Tensor` with a constant value.  Must be in
      the range `[0, input_tensor.rank]`.
    name: A name prefix for the returned tensor (optional).
  Returns:
    A `RaggedTensor` containing the %(combined)s values.  The returned tensor
    has the same dtype as `data`, and its shape is given by removing the
    dimensions specified in `axis` from `input_tensor.shape`.  The `ragged_rank`
    of the returned tensor is given by substracting any ragged dimensions
    specified in `axis` from `input_tensor.ragged_rank`.
  Raises:
    ValueError: If `axis` contains a `Tensor` whose value is not constant.
  ####Example:
    %(example)s
a  
    >>> rt = tf.ragged.constant([[3, 1, 4], [1, 5], [9], [2, 6]])
    >>> tf.reduce_sum(rt, axis=0).numpy()  # = [3+1+9+2, 1+5+6, 4]
    array([15, 12, 4], dtype=int32)
    >>> tf.reduce_sum(rt, axis=1).numpy()  # = [3+1+4, 1+5, 9, 2+6]
    array([8, 6, 9, 8], dtype=int32)
a  
    >>> rt = tf.ragged.constant([[3, 1, 4], [1, 5], [9], [2, 6]])
    >>> tf.reduce_prod(rt, axis=0).numpy()  # = [3*1*9*2, 1*5*6, 4]
    array([54, 30, 4], dtype=int32)
    >>> tf.reduce_prod(rt, axis=1).numpy()  # = [3*1*4, 1*5, 9, 2*6]
    array([12, 5, 9, 12], dtype=int32)
z
    >>> rt = tf.ragged.constant([[3, 1, 4], [1, 5], [9], [2, 6]])
    >>> tf.reduce_min(rt, axis=0).numpy()
    array([1, 1, 4], dtype=int32)
    >>> tf.reduce_min(rt, axis=1).numpy()
    array([1, 1, 9, 2], dtype=int32)
z
    >>> rt = tf.ragged.constant([[3, 1, 4], [1, 5], [9], [2, 6]])
    >>> tf.reduce_max(rt, axis=0).numpy()
    array([9, 6, 4], dtype=int32)
    >>> tf.reduce_max(rt, axis=1).numpy()
    array([4, 5, 9, 6], dtype=int32)
z
    >>> rt = tf.ragged.constant([[3, 1, 4], [1, 5], [9], [2, 6]])
    >>> tf.reduce_mean(rt, axis=0).numpy()
    array([3.75, 4.  , 4. ])
    >>> tf.reduce_mean(rt, axis=1).numpy()
    array([2.66666667, 3.  , 9.  , 4.  ])
a  
    >>> rt = tf.ragged.constant([[1, 1, 4], [2, 1], [3], [4, 1]],
    ...                         dtype=tf.float64)
    >>> tf.math.reduce_variance(rt, axis=0).numpy()
    array([1.25, 0., 0.])
    >>> tf.math.reduce_variance(rt, axis=1).numpy()
    array([2., 0.25, 0., 2.25])
a  
    >>> rt = tf.ragged.constant([[1, 0], [2, 1], [3], [4, 1]],
    ...                         dtype=tf.float64)
    >>> tf.math.reduce_std(rt, axis=0).numpy()
    array([1.11803399, 0.47140452])
    >>> tf.math.reduce_std(rt, axis=1).numpy()
    array([0.5, 0.5, 0., 1.5])
z
    >>> rt = tf.ragged.constant([[True, True], [True, True, False, True], [False, True]])
    >>> tf.reduce_all(rt, axis=0).numpy()
    array([False,  True, False,  True])
    >>> tf.reduce_all(rt, axis=1).numpy()
    array([ True, False, False])
z
    >>> rt = tf.ragged.constant([[True, True], [True, True, False, True], [False, True]])
    >>> tf.reduce_any(rt, axis=0).numpy()
    array([ True,  True, False,  True])
    >>> tf.reduce_any(rt, axis=1).numpy()
    array([ True,  True,  True])
c                    |i }nd|i}t        j                  |      s | ||f||d|S t        |t        j                        rLt        j                  |      }|t        d      t        |t        j                        r|j                         }|F | |j                  df||d|}|r+|j                  dd D ]  }	t        j                  |d      } |S t        j                   |d||g      5  t        |t"        t$        f      r|s|cddd       S t'        |      dk(  r|d   }nt)        |      D 
cg c]4  \  }
}t        j*                  ||j                  j,                  d	|
z  d
      6 }}
}t/        |      }t1        | |||d   ||      }t1        | |||dd ||      cddd       S t        j2                  |d      }t        j*                  ||j                  j,                  d
      }|dk(  r|j4                  dd |j4                  dd z
  }t7        j8                  t7        j:                  |      d      }t=        |      j>                  }tA        ||j>                  |||      }|rt        j                  |d      }|cddd       S |dk(  rt        j                  |j4                        d   dz
  }tC        jD                  |j4                        }tA        ||j>                  |||      }|rt        j                  |d      }|cddd       S |jG                  t1        | ||j>                  |dz
  ||            cddd       S c c}}
w # 1 sw Y   yxY w)a?  Aggregates across axes of a RaggedTensor using the given `Tensor` ops.

  Reduces `rt_input` along the dimensions given in `axis`.  The rank of the
  tensor is reduced by 1 for each entry in `axis`.  If `axis` is not specified,
  then all dimensions are reduced, and a scalar value is returned.

  This op assumes that `reduce_op` and `unsorted_segment_op` are associative;
  if not, then reducing multiple axes will return incorrect results.  (In
  particular, reducing multiple axes is currently implemented by reducing the
  axes one at a time.)

  Args:
    reduce_op: The tensorflow `op` that should be used to reduce values in
      uniform dimensions.  Must have the same signature and basic behavior as
      `reduce_sum`, `reduce_max`, etc.
    unsorted_segment_op: The tensorflow `op` that should be used to combine
      values in ragged dimensions.  Must have the same signature and basic
      behavior as `unsorted_segment_sum`, `unsorted_segment_max`, etc.
    rt_input: A `Tensor` or `RaggedTensor` containing the values to be reduced.
    axis: The axis or axes to reduce.  May be `None` (to reduce all axes), an
      `int` (to reduce a single axis), a `list` or `tuple` of `int` (to reduce a
      given set of axes), or a `Tensor` with a constant value.  Must be in the
      range `[0, rt_input.rank)`.
    keepdims: If true, retains reduced dimensions with length 1.
    separator: An optional string. Defaults to None. The separator to use when
      joining. The separator must not be set for non-string data types. (i.e. if
      separator is not None then it uses string ops)
    name: A name prefix for the returned tensor (optional).

  Returns:
    A `RaggedTensor` containing the reduced values.  The returned tensor
    has the same dtype as `data`, and its shape is given by removing the
    dimensions specified in `axis` from `rt_input.shape`.  The `ragged_rank`
    of the returned tensor is given by substracting any ragged dimensions
    specified in `axis` from `rt_input.ragged_rank`.
  Raises:
    ValueError: If `axis` contains a `Tensor` whose value is not constant.
  NrX   )keepdimsr   z.axis must be known at graph construction time.r   r   rE   RaggedReducezaxis[%s]zrank(input_tensor)rD   rt_inputr@   
ndims_name)$r   rG   
isinstancer   Tensorr   constant_valuerJ   npndarraytolistrx   shaper   expand_dimsr   r   tuplelistlen	enumerateget_positive_axisndimssortedragged_reduce_aggregaterH   rL   r   rP   
reduce_maxr.   rO   rN   r   row_splits_to_segment_idswith_values)	reduce_oprV   r   rF   r   rX   r   maybe_separatorr,   _iainner_reducedrow_lengthsrW   rA   s                   r-   r   r     s   ^ O"I.O		 	 	*$I!)I8GI I fmm$%%d+D|GHH$

#[[]d 
\x++T 5H 5$35F~~ab! 7!&&vA67M
~~dNXt,<= <.$&<. <. t9>Aw "$
 1 ''8>>+?+?a(<>
 
 d|/	;N08$r(H09; 'y2E'4d3Bi'021<. <.8 ??z#H &&hnn""/CED qy''+h.A.A#2.FFk%%h&9&9+&FJl+&--k()<hoo)4lIOf	&&vA6W<. <.X 
__X%8%89!<q@l"<<


k()<hoo)4lIOf	&&vA6k<. <.r !!
!)-@"*//4!8X"+-.s<. <.
<. <.s8   3M"M99M23M/CMA<M+MMMinput_tensorc           	      d    t        t        j                  t        j                  | |||xs d      S )(For docs, see: _RAGGED_REDUCE_DOCSTRING.RaggedReduceSumr   rV   r   rF   r   r   )r   r   
reduce_sumrd   r   rF   r   r   s       r-   r   r   e  s5     
!##"77%%
( (rg   c           	      d    t        t        j                  t        j                  | |||xs d      S )r   RaggedReduceProdr   )r   r   reduce_prodrj   r   s       r-   r   r   u  s5     
!$$"88&&
) )rg   c           	      d    t        t        j                  t        j                  | |||xs d      S )r   RaggedReduceMinr   )r   r   
reduce_minrn   r   s       r-   r   r     5     
!##"77%%
( (rg   c           	      d    t        t        j                  t        j                  | |||xs d      S )r   RaggedReduceMaxr   )r   r   r   rQ   r   s       r-   r   r     r   rg   c                 H   t        j                  |d| |g      5  t        | ||      }t        j                  |       rJt        j
                  j                  t        j                  | j                        | j                  d      }nt        j                  |       }t        |||      }t        j                  |      rLt        j
                  j                  |j                  |j                  z  |j                  d      cddd       S ||z  cddd       S # 1 sw Y   yxY w)r   RaggedReduceMeanFr   N)r   r   r   r   rG   r'   rv   r   rw   rx   ry   )r   rF   r   r   r|   r}   r~   s          r-   reduce_meanr     s     ~~d.t0DE |T84E|,''>>


l66
7

(
( ? d
   .dtT8,Eu%''>>


e//
/

!
! ?    U]!  s   C&D
DD!c                    t        j                  |d| |g      5  t        j                  | d      } | j                  j
                  rt        d      t        j                  |       }t        |||      }t        | ||      }t        j                  |      }t        j                  ||z
  d      cddd       S # 1 sw Y   yxY w)r   RaggedReduceVariancer   r@   zGreduce_variance is not supported for RaggedTensors with complex dtypes.rF   r   r   N)r   r   r   rH   r   
is_complexrJ   r   squarer   rP   )r   rF   r   r   square_of_inputmean_of_squarer   square_of_means           r-   reduce_variancer     s     ~~d2\44HI @ CC>+L$$  ool3O thON|$BD__T*N N^;Q?@ @ @s   BB;;Cc                     t        j                  |d| |g      5  t        | ||      }t        j                  |      cddd       S # 1 sw Y   yxY w)r   RaggedReduceStdr   N)r   r   r   r   r   )r   rF   r   r   variances        r-   
reduce_stdr     sI     ~~d-d/CD #|$JH=="# # #s   #AAc                 L    t        j                  t        j                  | |      S r1   )r   map_flat_valuesr   r:   )r   r   s     r-   _castr     s     		.	.x}}l/4
6 6rg   c           	          t        j                  |d| |g      5  t        t        t        | t        j
                        ||      t        j                        cddd       S # 1 sw Y   yxY w)r   RaggedReduceAllN)r   r   r   r   r   r"   boolr   s       r-   
reduce_allr     sU     ~~d-d/CD E,5tXF     =A!!A*c           	          t        j                  |d| |g      5  t        t        t        | t        j
                        ||      t        j                        cddd       S # 1 sw Y   yxY w)r   RaggedReduceAnyN)r   r   r   r   r   r"   r   r   s       r-   
reduce_anyr     sU     ~~d-d/CD 5v||4dHE  r   c                 :    t         t        ||||      z  | _        y )N)r   r   defaultexample)_RAGGED_REDUCE_DOCSTRINGr   r   )r   r   r   r   r   s        r-   _set_ragged_reduce_docstringr     s!    )D	- $,rg   01z`input_tensor.dtype.min`z`input_tensor.dtype.max`NaNr   stdzlogical andzand-edTruez
logical orzor-edFalser   bc           	         ~	~
|r|rt        d      |r|rt        d      t        |||||||      }t        j                  |d| |g      5 }t	        j
                  | d      } t	        j
                  |d      }t        | t        j                        }t        |t        j                        }|s"|s t        j                  | |fi |cddd       S | j                  |j                  k7  rt        d	      | j                  j                  8|j                  j                  t        d
      |j                  j                  }nd|j                  j                  8| j                  j                  |j                  j                  k7  rt        d      | j                  j                  }|dk  rt        d      |dkD  rd}|s!t        j                  j                  | d      } |s!t        j                  j                  |d      }t        j                  t        j                   | j"                  |j"                  |      g      5  t        | j$                  |j$                  fi |}| j'                  |      cddd       cddd       S |dk(  rt)        | |fi |cddd       S |dk(  sJ |r| j*                  nd}|dk(  r|s|s|st-        | |fi |cddd       S t/        | |fi |cddd       S # 1 sw Y   rxY w# 1 sw Y   yxY w)a  Multiplies matrix `a` by matrix `b`.

  If all transpose or adjoint attributes are `False` then:

  ```
  output[..., i, j] = sum_k (a[..., i, k] * b[..., k, j]), for all indices i, j.
  ```

  The inputs `a` and `b` must have `rank >= 2`, where the outermost `rank - 2`
  dimensions are batch dimensions.  The inputs must have the same dtype.  See
  `tf.matmul` for more information.

  Args:
    a: `tf.Tensor` or `RaggedTensor` with `rank > 1`.
    b: `tf.Tensor` or `RaggedTensor` with same type and rank as `a`.
    transpose_a: If `True`, `a` is transposed before multiplication.
    transpose_b: If `True`, `b` is transposed before multiplication.
    adjoint_a: If `True`, `a` is conjugated & transposed before multiplication.
    adjoint_b: If `True`, `b` is conjugated & transposed before multiplication.
    a_is_sparse: If `True`, optimize assuming `a` is mostly zero.
    b_is_sparse: If `True`, optimize assuming `b` is mostly zero.
    output_type: The output datatype (optional).
    grad_a: Unused.
    grad_b: Unused.
    name: Name for the operation (optional).

  Returns:
    A `Tensor` or `RaggedTensor` with the same rank and shape as `a`, where
    each inner-most matrix is the product of the corresponding matrices in `a`
    and `b`.
  z2Only one of transpose_a and adjoint_a can be True.z2Only one of transpose_b and adjoint_b can be True.)transpose_atranspose_b	adjoint_a	adjoint_ba_is_sparseb_is_sparseoutput_typeRaggedMatMulr   r@   r   Nz%`a` and `b` must have the same dtype.zPmatmul requires at least one input to have known rank if either input is ragged.z$`a` and `b` must have the same rank.      z:Batch dimensions of `a` and `b` do not have the same size.r   )ragged_rankrB   r   )rJ   r   r   r   r   rH   r   r'   r   matmulr   r   rankfrom_tensorrM   r	   rK   rL   rO   r   
_matmul_2dr   !_matmul_3d_with_batch_dim_folding_matmul_3d_with_map_fn)r   r   r   r   r   r   r   r   r   grad_agrad_br   kwargsa_is_raggedb_is_raggedr   	shape_errflat_resulta_ragged_ranks                      r-   r   r     s   \ Y
I
JJY
I
JJ& ~~dNQF3 >4t88EA88EAQ : :;KQ : :;K;__Q,V,>4 >4 	ww!''>?? 	ww||	
	 ; < 	<WW\\d	
	!aggllaggll&B?@@WW\\d ax =>>
 axNi&&221!2D&&221!2D##

 
 q||Y
O% 	 * QXXqxx:6:}}[)	* *Y>4 >4d qy1''g>4 >4j 199%0AMMaM;+ /q!>v>w>4 >4| $Aq3F3}>4 >4X* *Y>4 >4s>   A=L E3L 2K48	L L &.L L 4K=	9L  L	c                    d}g }t        | t        j                        rit        j                  | j
                        }| j                         } |j                  t        j                  |t        j                  |       |             t        |t        j                        rit        j                  |j
                        }|j                         }|j                  t        j                  |t        j                  |      |             t        j                  |      5  t        j                  | |fi |cddd       S # 1 sw Y   yxY w)a  Multiplies potentially ragged 2D tensors.

  Args:
    a: A 2D Tensor or RaggedTensor with `shape=[I, J]`
    b: A 2D Tensor or RaggedTensor with `shape=[J, K]`
    **kwargs: Additional arguments for `tf.matmul` (e.g. transpose_a).

  Returns:
    A 2D Tensor with `shape=[I, K]`.
  zKThe matrices in `a` and `b` may not be ragged in their innermost dimension.rB   N)r   r   r'   r   sizerx   	to_tensorappendr	   rK   r   rM   r   r   )r   r   r   
ragged_errchecksoriginal_sizes         r-   r   r     s    7*&=--.NN1==1M	A
MM9>>!,j	BC =--.NN1==1M	A
MM9>>!,j	BC ' +??1a*6*+ + +s    EE
c                   	 t        |t        j                        r4|j                  dk(  s"j	                  d      sj	                  d      rd	nd		fd}d}t        | t        j                        r| j
                  j                  n|j
                  j                  }d   }|| j                  }t        j                  ||	dz
  |      }t        j                  || |f|	      }j	                  d
      sj	                  d      r3|j                  | j                  dd | j                  dd z   dgz          n2|j                  | j                  dd | j                  dd z   dgz          j	                  d      sj	                  d      r4|j                  |j                  dd dgz   |j                  dd z          |S |j                  |j                  dd dgz   |j                  dd z          |S )a7  Multiplies batches of 2D matrices using map_fn.

  `output[n, i, k]` = sum_j (a[n, i, j] * b[n, j, k])` (for all `n`, `i`, `k`).

  Requires that `a[n, i].nrows()` == `b[n].nrows()` (for all `n` and `i`).

  Args:
    a: A 3D Tensor or RaggedTensor with `shape=[B, I, J]`, where dimensions `I`
      and `J` may be ragged.
    b: A 3D Tensor or RaggedTensor with `shape=[B, J, K]`, where dimensions `J`
      and `K` may be ragged.
    **kwargs: Additional arguments for `tf.matmul` (e.g. transpose_a).

  Returns:
    A 3D RaggedTensor with `shape=[B, (I), (K)]`.
  r   r   r   r   c                 v    t        | d   | d   fi }dk(  rt        j                  j                  |      }|S )Nr   r   r   )r   r   r'   r   )xoutr   output_ragged_ranks     r-   single_batch_matmulz3_matmul_3d_with_map_fn.<locals>.single_batch_matmul  s?    
QqT1Q4
*6
*CQ&&2237cJrg   Nr   )r   r   r   r+   )elemsfn_output_signaturer   r   rD   )r   r   r'   r   getrL   r   RaggedTensorSpecr   
_set_shaper   )
r   r   r   r  fn_out_shaper+   r   specr,   r  s
     `      @r-   r   r     s   , M../}}VZZ6zz+ , 
A}11	2 ll898J8J  }%+''K		'	'$q('	
)$
 ==!QTC&
 ZZ&**["9
aggcrlQWWRS\1TF:;
aggcrlQWWR^3tf<=ZZ&**["9
aggcrldV+aggbn<= 
- aggcrldV+aggbcl:;	-rg   c                    t        j                  | j                  d      }t        j                  || j	                         d      }t        j                  ||fi |}| j                  t        j                  |d            S )ap  Multiply batches of 2D matrices where only `a.shape[1]` is ragged.

  Args:
    a: A RaggedTensor with `shape=[B, (I), J]`.  (ragged_rank must be 1.)
    b: A Tensor with `shape=[B, J, K]`
    **kwargs: Additional arguments for `tf.matmul` (e.g. transpose_a).
      transpose_a and adjoint_a must not be true.

  Returns:
    A RaggedTensor with `shape=[B, (I), K].
  r   r   rE   )	r   r   rO   repeatr   r   r   r   squeeze)r   r   r   
reshaped_a
reshaped_br  s         r-   r   r     se     $$QXXq1*1==?;*
JA&A+	
y((1=	>>rg   logitsc                    |d}t        j                  |d| g      5 }t        | |d      }t        j                  t        j
                  | |            }t        ||d      }t        j                  ||      cddd       S # 1 sw Y   yxY w)a  Computes softmax activations.

  Used for multi-class predictions. The sum of all outputs generated by softmax
  is 1.

  This function performs the equivalent of

      softmax = tf.exp(logits) / tf.reduce_sum(tf.exp(logits), axis)

  Example usage:

  >>> softmax = tf.nn.softmax([-1, 0., 1.])
  >>> softmax
  <tf.Tensor: shape=(3,), dtype=float32,
  numpy=array([0.09003057, 0.24472848, 0.66524094], dtype=float32)>
  >>> sum(softmax)
  <tf.Tensor: shape=(), dtype=float32, numpy=1.0>

  Args:
    logits: A non-empty `Tensor`. Must be one of the following types: `half`,
      `float32`, `float64`.
    axis: The dimension softmax would be performed on. The default is -1 which
      indicates the last dimension.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type and shape as `logits`.

  Raises:
    InvalidArgumentError: if `logits` is empty or `axis` is beyond the last
      dimension of `logits`.
  NrD   RaggedSoftmaxTr   )r   r   r   r   expsubtractr   divide)r   rF   r   	max_input
logits_expdenominators         r-   softmaxr)    s}    D 
\D
~~dOfX6 4$6t<Ih//	BCJZdTBK??:{3	4 4 4s   ABBinputsc                     t        |       dk  rt        d      t        j                  |d|       5  t	        j
                  t        j                  |       cddd       S # 1 sw Y   yxY w)z.RaggedTensor implementation for tf.math.add_n.r   z&tf.add_n: expected at least one input.
RaggedAddNN)r   rJ   r   r   r   r   r   add_n)r*  r   s     r-   r-  r-  C  sX     	[1_
=
>>
~~dL&1 I 00HI I Is   $AA(r  c           	         |t        d      t        j                  |d| |g      5  t        j                  | d      } | j                  t        j                  | j                  |||            cddd       S # 1 sw Y   yxY w))Ragged dispatch target for tf.nn.dropout.N3noise_shape is not supported yet for RaggedTensor xRaggedNNDropoutr  r@   )	keep_probseedrate)	rJ   r   r   r   rH   rz   r   dropoutrx   )r  r2  noise_shaper3  r   r4  s         r-   
dropout_v1r7  O  s     
J
KK
~~d-4y9 G88EAMMYT	FGG G G   A	A::Bc                 
   |t        d      t        j                  |d| |g      5  t        j                  | d      } | j                  t        j                  | j                  ||            cddd       S # 1 sw Y   yxY w)r/  Nr0  r1  r  r@   )r4  r3  )	rJ   r   r   r   rH   rz   r   
dropout_v2rx   )r  r4  r6  r3  r   s        r-   r:  r:  `  s{     
J
KK
~~d-4y9 @88EA!--d>@@ @ @s   AA99Bc           	         |t        d      t        j                  |d| |g      5  t        j                  | d      } | j                  t        j                  | j                  |||            cddd       S # 1 sw Y   yxY w)z@Ragged dispatch target for tf.nn.experimental.stateless_dropout.Nr0  RaggedNNStatelessDropoutr  r@   )r4  r3  rng_alg)	rJ   r   r   r   rH   rz   r   stateless_dropoutrx   )r  r4  r3  r=  r6  r   s         r-   r>  r>  o  s     
J
KK
~~d6D	B C88EA  MM4	BCC C Cr8  selfotherc                     |yt        |       r| |u S 	 t        j                  | |      S # t        j                  t
        f$ r Y yw xY w)z;Ragged version of the operation invoked by `Tensor.__eq__`.F)$_use_legacy_mode_for_tensor_equalityr   equalr   InvalidArgumentErrorrJ   r?  r@  s     r-   tensor_equalsrF    sQ     ]+D15=^^D%((''4    * AAc                     |yt        |       r| |uS 	 t        j                  | |      S # t        j                  t
        f$ r Y yw xY w)z;Ragged version of the operation invoked by `Tensor.__ne__`.FT)rB  r   	not_equalr   rD  rJ   rE  s     r-   tensor_not_equalsrJ    sT     ]+D1ue,,''4 rG  c                     t        | dd       }t        j                  j                  xr( t	        j
                         xr |d u xs |j                   S )Ngraph)getattrr   r   _USE_EQUALITYr   #executing_eagerly_outside_functionsbuilding_function)r?  gs     r-   rB  rB    sO    dGT"!mm)) 15571Dy/A//
2 2rg   c                    |st        | |d|      }||z   S |rmt        j                  | j                         | j	                         dz         dz
  }t        j                  |dd      }t        j                  ||      }||z
  S t        j                  | j                         | j	                               }t        j                  |d      }t        j                  ||      }||z
  S )zACalculate flat_values for math_ops.cumsum when axis==ragged_rank.T	exclusivereverser   )paramsindices)rT  )"_cumsum_flat_values_at_ragged_rankr   rU   rL   value_rowidsr   rT   )	last_rprx   rT  rU  partialyoungest_siblingnew_flat_valuesinitial_valueseldest_siblings	            r-   rX  rX    s     
0g?G[   ''!!#W-A-A-Ca-GIKLMookT4PO%%_.>@N ^++%%!!#W-A-A-CENookTBO%%_.<>N^++rg   rF   rT  rU  r   c           	         t        j                  |d| |||g      5  t        j                  || j                  j
                  d      }|| j                  k(  r@| j                  d   }| j                  t        || j                  ||            cddd       S || j                  kD  rU|| j                  z
  }t        j                  t        j                  |||      }t        j                   ||       cddd       S | j#                         }t        j                  |||||      }	t$        j&                  j)                  |	| j+                         	      cddd       S # 1 sw Y   yxY w)
a  Calculate math_ops.cumsum for a RaggedTensor.

  Given a ragged tensor `x`, the `result` is a ragged tensor with the same
  shape. One can calculate the value of `result[i_1...i_k]` as follows:
  ```
  dense_result=tf.math.cumsum(rt.to_tensor(), axis=axis, exclusive=exclusive,
                              reverse=reverse)
  result[i_1...i_k]=dense_result[i_1...i_k]
  ```

  Args:
    x: the original ragged tensor to sum.
    axis: the axis along which to sum, can range -rank<=axis<rank.
    exclusive: is the sum exclusive or inclusive? If True, then result[0]=0.
        If False, then result[0]=x[0].
    reverse: If True, sum from back to front.
    name: the name of the op.
  Returns:
    the cumulative sum.
  RaggedCumSumr   r   rD   rS  N)rF   rT  rU  )rT  rU  r   )lengths)r   r   r   r   r   r   r   _nested_row_partitionsrz   rX  rx   	functoolsr[  r   rT   r   r   r  r   r'   r   nested_row_lengths)
r  rF   rT  rU  r   rZ  new_axiscumsum_bounddense_versionr,   s
             r-   ragged_cumsumri    s:   4 ~~dNQi,IJ 2&&tQWW\\fMDq}}((,g
,Wamm7@5<>?	2 2 
	%h&&
//IwPl"22<C2 2 kkmm
G$Pf''33
!..0 4 2#2 2 2s   A2EAE<AEE')NNr1   )NNN)NFN)
FFFFFFNFFN)NNNNN)FF)r   FFN)]r   rd  typingnumpyr   tensorflow.python.frameworkr   r   r   r   r   tensorflow.python.opsr   r	   r
   r   r   r   tensorflow.python.ops.raggedr   r   r   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   add_dispatch_supportr#   r.   r!   no_gradientr   rN   dispatch_for_apird   RaggedOrDensere   rj   rk   rn   ro   rQ   rr   unsorted_segment_meanr   unsorted_segment_sqrt_nr   r   r   _RAGGED_REDUCE_SUM_EXAMPLE_RAGGED_REDUCE_PROD_EXAMPLE_RAGGED_REDUCE_MIN_EXAMPLE_RAGGED_REDUCE_MAX_EXAMPLE_RAGGED_REDUCE_MEAN_EXAMPLE_RAGGED_REDUCE_VARIANCE_EXAMPLE_RAGGED_REDUCE_STD_EXAMPLE_RAGGED_REDUCE_ALL_EXAMPLE_RAGGED_REDUCE_ANY_EXAMPLEr   r   Raggedr   r   r   r   r   r   r   r   r   r   r   r   r   r   
softmax_v2r)  r-  Listr5  r7  r:  r>  rF  rJ  rB  rX  rT   intr   Optionalstrri   rg   r-   <module>r     s   "    . . + . 3 + + 5 ( * ( > 6 7 + 6 >	!<<EI  EIP=   D )-#'f6R 8889 
)m11 
)*88
) :
) 899: 
*}22 
*+99
* ;
* 8889 
)m11 
)*88
) :
) 8889 
)m11 
)*88
) :
) 899: }22 +99 ;& 8;;< *44 * - ; ;* =*(2
 k5( ; lI| D k9k B k9k B lFJ ? n.F&( 8     #     '+!%E.P 8../(]11 ( 0( 8//0)m22 ) 1) 8../(]11 ( 0( 8../(]11 ( 0( 8//0m22  10 8334"@-"6"6 @ 5@, 8../#]11 # 0#6
 8../]11  0 8../]11  0 Z#79 [)\38: ZK779 ZK779 [&*e8: _j*e<> Z
E79 Z&79 Zw79 8??+ 	{4""{4""{4 ,{4|+B=@?2 6,,-(4M(( (4 .(4\ 8>>*I&++m99: I +I 6>>*G-&& G +G  6,,-  	@-&& @ .@ 6334 #"&C-- C 5C& 811233 &44 3 8556M77 *88 72 HM/4,2 8??+$)"'/3	+2])) +2+2!+2  +2 ,	+2 ,+2rg   