
    AVh@                        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  ej"                  ej$                        	 	 	 	 ddej&                  fd       Z ej"                  ej(                        	 	 	 	 	 ddej&                  fd       Z ej"                  ej*                        	 	 	 	 	 	 	 ddej&                  fd       Zd Zd Zy)zEmbedding operations.    )dtypes)ops)	array_ops)array_ops_stack)embedding_ops)math_ops)resource_variable_ops)	variables)ragged_array_ops)ragged_functional_ops)ragged_tensor)dispatchNidsc                    | t        d      t        | t        t        f      r| st        d      |j                  t
        j                  k7  r?|j                  t
        j                  k7  r"t        dt        |j                         d      t        j                  |d      5 }t        j                  t        j                  | |||      }|cddd       S # 1 sw Y   yxY w)a  Look up the ragged ids in a list of embedding tensors.

  Args:
    params: A tensor representing the complete embedding tensor having the shape
      [e1, ...eM]
    ragged_ids: A 'RaggedTensor' with type 'int32' or 'int64' containing the ids
      to be looked up in 'params' of shape [r0, ..rN]. Values must be in the
      range '[0, params.shape[0]]'.
    partition_strategy: A string specifying the partitioning strategy.
    max_norm: If not `None`, each embedding is clipped if its l2-norm is larger
      than this value.
    name: A name for the operation (optional)

  Returns:
    A ragged tensor of shape [r0, r1, ...rN, e1, ...eM].

  Raises:
    ValueError: When params is empty or the type of the ids is not int32 or
      int64.
  Nzparams must be specified.zparams should not be empty.z-The values contained by the inputs have type zZ and cannot be processed. All values should be indices, either of type `int32` or `int64`.embedding_lookup_ragged)paramsr   partition_strategymax_norm)
ValueError
isinstancelisttupledtyper   int32int64strr   
name_scoper   map_flat_valuesr   embedding_lookup)r   r   r   namevalidate_indicesr   looked_up_raggeds          a/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/ragged/ragged_embedding_ops.pyr   r      s    : ^
0
11u&v
2
33YY&,,399#<
7syy>
	A	A  ~~d56 	$,<<&&- 	 	 	s   #*CC sp_idsc                    |}|}	|d}|dvrt        d|       t        | t        j                        rt	        |       } t        | t              s| g} |	du }
|
st        |	t
        j                        st        d      |j                  j                         j                  |	j                  j                                |j                         j                  |	j                                t        j                  |d| |gz         5 }|j                         }|j                  }t        j                   | |||||
||||
      cddd       S # 1 sw Y   yxY w)av  Looks up embeddings for the given ids and weights from a list of tensors.

  This op assumes that there is at least one id for each row in the dense tensor
  represented by sp_ids (i.e. there are no rows with empty features), and that
  all the indices of sp_ids are in canonical row-major order.

  `sp_ids` and `sp_weights` (if not None) are `RaggedTensor`s with rank of 2.
  Embeddings are always aggregated along the last dimension.

  It also assumes that all id values lie in the range [0, p0), where p0
  is the sum of the size of params along dimension 0.

  Args:
    params: A single tensor representing the complete embedding tensor, or a
      list tensors all of same shape except for the first dimension,
      representing sharded embedding tensors. Alternatively, a
      `PartitionedVariable`, created by partitioning along dimension 0. Each
      element must be appropriately sized for the given `partition_strategy`.
    sp_ids: `RaggedTensor` with rank 2. The rank is not verified for performance
      reasons.
    sparse_weights: `RaggedTensor` of same type and shape as `sparse_ids`,
      containing float / double weights corresponding to `sparse_ids`, or `None`
      if all weights are assumed to be 1.0.
    partition_strategy: A string specifying the partitioning strategy, relevant
      if `len(params) > 1`. Currently `"div"` and `"mod"` are supported. Default
      is `"mod"`. See `tf.nn.embedding_lookup` for more details.
    name: Optional name for the op.
    combiner: A string specifying the reduction op. Currently "mean", "sqrtn"
      and "sum" are supported. "sum" computes the weighted sum of the embedding
      results for each row. "mean" is the weighted sum divided by the total
      weight. "sqrtn" is the weighted sum divided by the square root of the sum
      of the squares of the weights. Defaults to `mean`.
    max_norm: If not `None`, each embedding is clipped if its l2-norm is larger
      than this value, before combining.
    allow_fast_lookup: An optional boolean specifying whether to allow
      simplified embedding lookups when `params` is a single tensor and
      `max_norm` is `None`. Setting this flag to `True` during training can
      cause the use of dense gradients with increased memory footprint.

  Returns:
    A dense tensor representing the combined embeddings for the
    sparse ids. For each row in the dense tensor represented by `sp_ids`, the op
    looks up the embeddings for all ids in that row, multiplies them by the
    corresponding weight, and combines these embeddings as specified.

    In other words, if

      `shape(combined params) = [p0, p1, ..., pm]`

    and

      `shape(sp_ids) = shape(sp_weights) = [d0, d1]`

    then

      `shape(output) = [d0, p1, ..., pm]`.

    For instance, if params is a 10x20 matrix, and sp_ids / sp_weights are

      ```python
      [0, 0]: id 1, weight 2.0
      [0, 1]: id 3, weight 0.5
      [1, 0]: id 0, weight 1.0
      [2, 3]: id 1, weight 3.0
      ```

    with `combiner`="mean", then the output will be a 3x20 matrix where

      ```python
      output[0, :] = (params[1, :] * 2.0 + params[3, :] * 0.5) / (2.0 + 0.5)
      output[1, :] = (params[0, :] * 1.0) / 1.0
      output[2, :] = (params[1, :] * 3.0) / 3.0
      ```

  Raises:
    TypeError: If `sp_weights` is neither `None` nor of the same type as
      `sp_ids`.
    ValueError: If `combiner` is not one of {"mean", "sqrtn", "sum"}.
  Nmean)r&   sqrtnsumz6combiner must be one of 'mean', 'sqrtn' or 'sum', got zsp_ids must be of the same type as sp_weights, received {type(sp_ids).__name__!r} for sp_ids and {type(sp_weights).__name__!r} for sp_weights.embedding_lookup_sparse)r   r   r
   PartitionedVariabler   r   RaggedTensor	TypeErrorvalues	get_shapeassert_is_compatible_withr   r   value_rowidsflat_valuesr   embedding_lookup_sparse_impl)r   r$   
sp_weightsr   r    combinerr   allow_fast_lookuprt_ids
rt_weightsignore_weightssegment_idsr   s                r#   r)   r)   T   s^   t &*H--

@
K  	556&\F	FD	!XF%.	j-"<"<=< 
 MM77##% 001E1E1GH
~~
%v'8 %%'K


C55  s   :EE
sparse_idsc	                    |}	|}
| t        d|  d      t        | t        j                        rt	        |       } t        | t              s| g} t        |       dk  rt        d|  d      |
|
j                  nd}| D cg c]@  }t        j                  |      r|d|j                  fv r|nt        j                  ||      B } }t        j                  |d| |	|
gz         5 }t        |	|
      \  }	}
|dk7  rt        |	|
      \  }	}
t        j                  |	|xs d      \  }	}|
t        j                  |
d	      \  }
}t!        | |	|
|||dn|||
      }|t#        j$                  t#        j&                  |ddg      t)        j*                  dt#        j,                  |      d   g            }t#        j.                  |t#        j0                  |      ||      }|cddd       S c c}w # 1 sw Y   yxY w)a  Lookup embedding results, accounting for invalid IDs and empty features.

  The partitioned embedding in `embedding_weights` must all be the same shape
  except for the first dimension. The first dimension is allowed to vary as the
  vocabulary size is not necessarily a multiple of `P`.  `embedding_weights`
  may be a `PartitionedVariable` as returned by using
  `tf.compat.v1.get_variable()` with a
  partitioner.

  Invalid IDs (< 0) are pruned from input IDs and weights, as well as any IDs
  with non-positive weight. For an entry with no features, the embedding vector
  for `default_id` is returned, or the 0-vector if `default_id` is not supplied.

  The ids and weights may be multi-dimensional `SparseTensor`s or
  `RaggedTensor`s with rank of 2. For `SpareTensor`s with left-aligned non-zero
  entries which can be described as `RaggedTensor`s, use of `RaggedTensor`s can
  yield higher performance. Embeddings are always aggregated along the last
  dimension.

  Args:
    embedding_weights: A single tensor representing the complete embedding
      tensor, or a list tensors all of same shape except for the first
      dimension, representing sharded embedding tensors. Alternatively, a
      `PartitionedVariable`, created by partitioning along dimension 0. Each
      element must be appropriately sized for the given `partition_strategy`.
    sp_ids: `RaggedTensor` with rank 2. The rank is not verified for performance
      reasons.
    sparse_weights: `RaggedTensor` of same type and shape as `sparse_ids`,
      containing float weights corresponding to `sparse_ids`, or `None` if all
      weights are assumed to be 1.0.
    combiner: A string specifying how to combine embedding results for each
      entry. Currently "mean", "sqrtn" and "sum" are supported, with "mean" the
      default.
    default_id: The id to use for an entry with no features.
    name: A name for this operation (optional).
    partition_strategy: A string specifying the partitioning strategy. Currently
      `"div"` and `"mod"` are supported. Default is `"div"`.
    max_norm: If not `None`, all embeddings are l2-normalized to max_norm before
      combining.
    allow_fast_lookup: An optional boolean specifying whether to allow
      simplified embedding lookups when `params` is a single tensor and
      `max_norm` is `None`. Setting this flag to `True` during training can
      cause the use of dense gradients with increased memory footprint.

  Returns:
    A dense tensor representing the combined embeddings for the
    sparse ids. For each row in the dense tensor represented by `sp_ids`, the op
    looks up the embeddings for all ids in that row, multiplies them by the
    corresponding weight, and combines these embeddings as specified.

    In other words, if

      `shape(combined embedding_weights) = [p0, p1, ..., pm]`

    and

      `shape(sparse_ids) = shape(sparse_weights) = [d0, d1, ..., dn]`

    then

      `shape(output) = [d0, d1, ... dn-1, p1, ..., pm]`.

    For instance, if params is a 10x20 matrix, and sp_ids / sp_weights are

      ```python
      [0, 0]: id 1, weight 2.0
      [0, 1]: id 3, weight 0.5
      [1, 0]: id -1, weight 1.0
      [2, 3]: id 1, weight 3.0
      ```

    `default_id` is 0.

    with `combiner`="mean", then the output will be a 3x20 matrix where

      ```python
      output[0, :] = (params[1, :] * 2.0 + params[3, :] * 0.5) / (2.0 + 0.5)
      output[1, :] = (params[0, :] * 1.0) / 1.0
      output[2, :] = (params[1, :] * 3.0) / 3.0
      ```

  Raises:
    ValueError: if `embedding_weights` is empty.
  NzMissing embedding_weights .   )r   r   r(   r   g      ?)r4   r   r    r   r5   )r    )r   r   r
   r*   r   lenr   r	   is_resource_variabler   convert_to_tensorr   _prune_invalid_ids_ragged_prune_invalid_weights_raggedr   fill_empty_rowsr)   r   tilereshaper   stackshapewhere
zeros_like)embedding_weightsr:   sparse_weightsr4   
default_idr    r   r   r5   
ragged_idsragged_weightsr   wscopeis_row_empty_results                    r#   safe_embedding_lookup_sparserU      sA   @ *!.
12C1DAF
GG!9#@#@A./	%t	,*+	a
12C1DAF
GG"0"<.

$% ! 	  
4
4Q
7agg& 
   %01  ~~
 1Z4P P (!:N"J 5#@
n$ j.  0??JO! J !*::>3Ona$-'TU+	F  ^^


L2q'
2


IOOF$;A$> ?
@l
 
	,,V4f5f Q( (( (s   ?AG%"C9G**G3c                    t        j                  | j                  d      }| j                         }t	        j
                  | j                  |      }t	        j
                  | j                         |      }t        j                  j                  |||d      } |Ct	        j
                  |j                  |      }t        j                  j                  |||d      }| |fS )z7Prune invalid IDs (< 0) from the input ids and weights.r   Fnrowsvalidate)
r   greater_equalr-   rX   r   boolean_mask_v2r0   r   r+   from_value_rowids)r   weightsis_id_validrX   pruned_valuespruned_value_rowidspruned_weights_valuess          r#   rB   rB   |  s    &&szz15+
))+% ++CJJD-!11	+ 	""44( 	5 	# %55 ((::2%% ; G 
g    c                    |t        j                  |j                  d      }| j                         }t	        j
                  | j                  |      }t	        j
                  | j                         |      }t        j                  j                  |||d      } t	        j
                  |j                  |      }t        j                  j                  |||d      }| |fS )z;Prune invalid weights (< 0) from the input ids and weights.r   FrW   )
r   greaterr-   rX   r   r[   r0   r   r+   r\   )r   r]   is_weights_validrX   r_   r`   ra   s          r#   rC   rC     s    '':IIKE --cjj:JKM#33, 
$
$
6
6*%% 7 C &55( ((::2%% ; G 
grb   )modNTN)rf   NNNF)Nr&   NNdivNF)__doc__tensorflow.python.frameworkr   r   tensorflow.python.opsr   r   r   r   r	   r
   tensorflow.python.ops.raggedr   r   r   tensorflow.python.utilr   dispatch_for_apir   Raggedr)   rU   rB   rC    rb   r#   <module>rp      s    . + + 1 / * 7 + 9 > 6 + =99: 	1			1 ;1h =@@A
 	C  C BCL =EEF 	]$$] G]@6rb   