
    BVhX                     2   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 ddlmZ ddlmZ g dZdZ G d dej@                        Z! edg      	 	 	 	 	 	 	 d*d       Z" edg      	 	 	 	 	 	 	 	 d+d       Z# G d deejH                        Z% G d  d!eejL                        Z'd" Z(	 	 d,d#Z)d$ Z*d% Z+ G d& d'e%      Z, G d( d)e'      Z-y)-zTPU Feature Column Library.    N)feature_column)feature_column_lib)dtypes)ops)	array_ops)embedding_ops)init_ops)math_ops)
sparse_ops)variable_scope)tpu)tpu_replication)_is_running_on_cpu)_record_variable_scope_and_name)!_SUPPORTED_CATEGORICAL_COLUMNS_V2)_SUPPORTED_SEQUENCE_COLUMNS)_TPUBaseEmbeddingColumn)	tf_export)cputpu_tensor_coretpu_embedding_core__TENSOR_CORE_MASKc                       e Zd ZdZdZdZy)EmbeddingDevice         N)__name__
__module____qualname__CPUTPU_TENSOR_CORETPU_EMBEDDING_CORE     W/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/tpu/feature_column_v2.pyr   r   +   s    	#/r%   r   z!tpu.experimental.embedding_column)v1c	                    t        | t              sOt        dj                  dj	                  t        D 	cg c]  }	|	j
                   c}	      t        |                   ||dk  rt        dj                  |            |r(t        |      dk7  rt        dj                  |            |/t        |      s$t        dj                  | j                              |-t        j                  ddt        j                  |      z  	      }|r|t        vrt        d
t               |dk(  rt         j"                  }n+|dk(  rt         j$                  }n|dk(  rt         j&                  }|t         j$                  k(  r(|st        d      t        | t(              rt        d      |st+        | ||||||      S t-        | ||||||||	      S c c}	w )a  TPU version of `tf.compat.v1.feature_column.embedding_column`.

  Note that the interface for `tf.tpu.experimental.embedding_column` is
  different from that of `tf.compat.v1.feature_column.embedding_column`: The
  following arguments are NOT supported: `ckpt_to_load_from`,
  `tensor_name_in_ckpt`, `max_norm` and `trainable`.

  Use this function in place of `tf.compat.v1.feature_column.embedding_column`
  when you want to use the TPU to accelerate your embedding lookups via TPU
  embeddings.

  ```
  column = tf.feature_column.categorical_column_with_identity(...)
  tpu_column = tf.tpu.experimental.embedding_column(column, 10)
  ...
  def model_fn(features):
    dense_feature = tf.keras.layers.DenseFeature(tpu_column)
    embedded_feature = dense_feature(features)
    ...

  estimator = tf.estimator.tpu.TPUEstimator(
      model_fn=model_fn,
      ...
      embedding_config_spec=tf.estimator.tpu.experimental.EmbeddingConfigSpec(
        column=[tpu_column],
        ...))
  ```

  Args:
    categorical_column: A categorical column returned from
        `categorical_column_with_identity`, `weighted_categorical_column`,
        `categorical_column_with_vocabulary_file`,
        `categorical_column_with_vocabulary_list`,
        `sequence_categorical_column_with_identity`,
        `sequence_categorical_column_with_vocabulary_file`,
        `sequence_categorical_column_with_vocabulary_list`
    dimension: An integer specifying dimension of the embedding, must be > 0.
    combiner: A string specifying how to reduce if there are multiple entries
      in a single row for a non-sequence column. For more information, see
      `tf.feature_column.embedding_column`.
    initializer: A variable initializer function to be used in embedding
      variable initialization. If not specified, defaults to
      `tf.compat.v1.truncated_normal_initializer` with mean `0.0` and
      standard deviation `1/sqrt(dimension)`.
    max_sequence_length: An non-negative integer specifying the max sequence
      length. Any sequence shorter then this will be padded with 0 embeddings
      and any sequence longer will be truncated. This must be positive for
      sequence features and 0 for non-sequence features.
    learning_rate_fn: A function that takes global step and returns learning
      rate for the embedding table. If you intend to use the same learning rate
      for multiple embedding tables, please ensure that you pass the exact same
      python function to all calls of embedding_column, otherwise performence
      may suffer.
    embedding_lookup_device: The device on which to run the embedding lookup.
      Valid options are "cpu", "tpu_tensor_core", and "tpu_embedding_core".
      If specifying "tpu_tensor_core", a tensor_core_shape must be supplied.
      If not specified, the default behavior is embedding lookup on
      "tpu_embedding_core" for training and "cpu" for inference.
      Valid options for training : ["tpu_embedding_core", "tpu_tensor_core"]
      Valid options for serving :  ["cpu", "tpu_tensor_core"]
      For training, tpu_embedding_core is good for large embedding vocab (>1M),
      otherwise, tpu_tensor_core is often sufficient.
      For serving, doing embedding lookup on tpu_tensor_core during serving is
      a way to reduce host cpu usage in cases where that is a bottleneck.
    tensor_core_shape: If supplied, a list of integers which specifies
      the intended dense shape to run embedding lookup for this feature on
      TensorCore. The batch dimension can be left None or -1 to indicate
      a dynamic shape. Only rank 2 shapes currently supported.
    use_safe_embedding_lookup: If true, uses safe_embedding_lookup_sparse
      instead of embedding_lookup_sparse. safe_embedding_lookup_sparse ensures
      there are no empty rows and all weights and ids are positive at the
      expense of extra compute cost. This only applies to rank 2 (NxM) shaped
      input tensors. Defaults to true, consider turning off if the above checks
      are not needed. Note that having empty rows will not trigger any error
      though the output result might be 0 or omitted.

  Returns:
    A  `_TPUEmbeddingColumnV2`.

  Raises:
    ValueError: if `dimension` not > 0.
    ValueError: if `initializer` is specified but not callable.
  zDcategorical_column for tpu embedding_column must be type {}, got {}. or r   Invalid dimension {}.r   )tensor_core_shape must be size 2. Got {}.zGinitializer must be callable if specified. Embedding of column_name: {}        meanstddev+If set, embedding_lookup_device must be in r   r   r   SUsing embedding_lookup_device=tpu_tensor_core requires tensor_core_shape to be set.Tembedding_lookup_device=tpu_tensor_core currently does not support sequence columns.)categorical_column	dimensioncombinerinitializermax_sequence_lengthlearning_rate_fnuse_safe_embedding_lookup)	r3   r4   r5   r6   r7   r8   embedding_lookup_devicetensor_core_shaper9   )
isinstancer   	TypeErrorformatjoinr   type
ValueErrorlencallablenamer	   truncated_normal_initializermathsqrt_ALLOWED_DEVICESr   r!   r"   r#   r   _TPUEmbeddingColumnV2#_TPUDeviceSpecificEmbeddingColumnV2)
r3   r4   r5   r6   r7   r8   r:   r;   r9   ccs
             r&   embedding_column_v2rL   1   s   | 
&(I	J
	44:F6;;"CH
BKKH
 <#$5&' '
 Y]
,33I>
??301Q6
3::;LMO O (=
 44:F+00523 3 77TYYy113K %55

56F5GHJ J %-11"33-=="66-@@ ? ?? 6 7 7$&AB 7 8 8 
! -/)";= = /-/) 7+";	= 	=_H
s   Gz)tpu.experimental.shared_embedding_columnsc
                    | D ]a  }
t        |
t              rt        dj                  dj	                  t        D cg c]  }|j
                   c}      t        |
                   |sdgt        |       z  }t        |      t        |       k7  r-t        dj                  t        |      t        |                   ||dk  rt        dj                  |            |r(t        |      dk7  rt        d	j                  |            |t        |      st        d
      |-t        j                  ddt        j                  |      z        }t        | d       }|d   j                  }|dd D ];  }||j                  k7  st        dj                  |d   |||j                               |sdj	                  d |D              }|dz  }g }t!        j"                  ||dd|d|      }|r|t$        vrt        dt$               |dk(  rt&        j(                  }n+|dk(  rt&        j*                  }n|dk(  rt&        j,                  }|t&        j*                  k(  r/|st        d      |D ]  }t        |t.              st        d       t1        | |      D ]A  \  }
}|st3        |
|||||||	      }nt5        |
|||||||||	
      }|j7                  |       C |S c c}w )a  TPU version of `tf.compat.v1.feature_column.shared_embedding_columns`.

  Note that the interface for `tf.tpu.experimental.shared_embedding_columns` is
  different from that of `tf.compat.v1.feature_column.shared_embedding_columns`:
  The following arguments are NOT supported: `ckpt_to_load_from`,
  `tensor_name_in_ckpt`, `max_norm` and `trainable`.

  Use this function in place of
  tf.compat.v1.feature_column.shared_embedding_columns` when you want to use the
  TPU to accelerate your embedding lookups via TPU embeddings.

  ```
  column_a = tf.feature_column.categorical_column_with_identity(...)
  column_b = tf.feature_column.categorical_column_with_identity(...)
  tpu_columns = tf.tpu.experimental.shared_embedding_columns(
      [column_a, column_b], 10)
  ...
  def model_fn(features):
    dense_feature = tf.keras.layers.DenseFeature(tpu_columns)
    embedded_feature = dense_feature(features)
    ...

  estimator = tf.estimator.tpu.TPUEstimator(
      model_fn=model_fn,
      ...
      embedding_config_spec=tf.estimator.tpu.experimental.EmbeddingConfigSpec(
          column=tpu_columns,
          ...))
  ```

  Args:
    categorical_columns: A list of categorical columns returned from
      `categorical_column_with_identity`, `weighted_categorical_column`,
      `categorical_column_with_vocabulary_file`,
      `categorical_column_with_vocabulary_list`,
      `sequence_categorical_column_with_identity`,
      `sequence_categorical_column_with_vocabulary_file`,
      `sequence_categorical_column_with_vocabulary_list`
    dimension: An integer specifying dimension of the embedding, must be > 0.
    combiner: A string specifying how to reduce if there are multiple entries in
      a single row for a non-sequence column. For more information, see
      `tf.feature_column.embedding_column`.
    initializer: A variable initializer function to be used in embedding
      variable initialization. If not specified, defaults to
      `tf.truncated_normal_initializer` with mean `0.0` and standard deviation
      `1/sqrt(dimension)`.
    shared_embedding_collection_name: Optional name of the collection where
      shared embedding weights are added. If not given, a reasonable name will
      be chosen based on the names of `categorical_columns`. This is also used
      in `variable_scope` when creating shared embedding weights.
    max_sequence_lengths: An list of non-negative integers, either None or empty
      or the same length as the argument categorical_columns. Entries
      corresponding to non-sequence columns must be 0 and entries corresponding
      to sequence columns specify the max sequence length for the column. Any
      sequence shorter then this will be padded with 0 embeddings and any
      sequence longer will be truncated.
    learning_rate_fn: A function that takes global step and returns learning
      rate for the embedding table. If you intend to use the same learning rate
      for multiple embedding tables, please ensure that you pass the exact same
      python function to all calls of shared_embedding_columns, otherwise
      performence may suffer.
    embedding_lookup_device: The device on which to run the embedding lookup.
      Valid options are "cpu", "tpu_tensor_core", and "tpu_embedding_core". If
      specifying "tpu_tensor_core", a tensor_core_shape must be supplied.
      Defaults to "cpu". If not specified, the default behavior is embedding
      lookup on "tpu_embedding_core" for training and "cpu" for inference.
      Valid options for training : ["tpu_embedding_core", "tpu_tensor_core"]
      Valid options for serving :  ["cpu", "tpu_tensor_core"]
      For training, tpu_embedding_core is good for large embedding vocab (>1M),
      otherwise, tpu_tensor_core is often sufficient.
      For serving, doing embedding lookup on tpu_tensor_core during serving is
      a way to reduce host cpu usage in cases where that is a bottleneck.
    tensor_core_shape: If supplied, a list of integers which specifies the
      intended dense shape to run embedding lookup for this feature on
      TensorCore. The batch dimension can be left None or -1 to indicate a
      dynamic shape. Only rank 2 shapes currently supported.
    use_safe_embedding_lookup: If true, uses safe_embedding_lookup_sparse
      instead of embedding_lookup_sparse. safe_embedding_lookup_sparse ensures
      there are no empty rows and all weights and ids are positive at the
      expense of extra compute cost. This only applies to rank 2 (NxM) shaped
      input tensors. Defaults to true, consider turning off if the above checks
      are not needed. Note that having empty rows will not trigger any error
      though the output result might be 0 or omitted.

  Returns:
    A  list of `_TPUSharedEmbeddingColumnV2`.

  Raises:
    ValueError: if `dimension` not > 0.
    ValueError: if `initializer` is specified but not callable.
    ValueError: if `max_sequence_lengths` is specified and not the same length
      as `categorical_columns`.
    ValueError: if `max_sequence_lengths` is positive for a non sequence column
      or 0 for a sequence column.
  zMcategorical_column for tpu  shared_embedding_columns must be type {}, got {}.r)   r   zmax_sequence_lengths and categorical_columns must be of the same length. len(max_sequence_lengths)={} len(categorical_columns)={}.Nr   r*   r   r+   z+initializer must be callable if specified. r,   r-   c                     | j                   S NrD   )xs    r&   <lambda>z-shared_embedding_columns_v2.<locals>.<lambda>W  s
    QVV r%   )keyzTo use shared_embedding_column, all categorical_columns must have the same number of buckets. Given column: {} with buckets: {} does  not match column: {} with buckets: {}_c              3   4   K   | ]  }|j                     y wrO   rP   ).0cs     r&   	<genexpr>z.shared_embedding_columns_v2.<locals>.<genexpr>c  s     /O1/Os   _shared_embedding)r4   r6   ckpt_to_load_fromtensor_name_in_ckptnum_buckets	trainablerD   r0   r   r   r   r1   r2   )r3   shared_embedding_column_creatorr5   r6    shared_embedding_collection_namer7   r8   r9   )
r3   r^   r5   r6   r_   r7   r8   r:   r;   r9   )r<   r   r=   r>   r?   r   r@   rB   rA   rC   r	   rE   rF   rG   sorted_num_bucketsfc_libSharedEmbeddingColumnCreatorrH   r   r!   r"   r#   r   zip_TPUSharedEmbeddingColumnV2)_TPUSharedDeviceSpecificEmbeddingColumnV2append)categorical_columnsr4   r5   r6   r_   max_sequence_lengthsr8   r:   r;   r9   r3   rK   sorted_columnsr\   rW   tpu_columnscolumn_creatorr7   columns                      r&   shared_embedding_columns_v2rn      sc   V 0 )(*KL??Evkk)JK22;;KM%&@() )) 
3%8!99	#&9"::
 44:F12C8K4L5NO O
 Y]
,33I>
??301Q6
3::;LMO O (=
B
CC77TYYy113K
 -3CD.q!..+!" Baann$228&Qa3AB BB 
*'*xx/O/O'O$$(;;$+66{dK4+-.
 %55

56F5GHJ J %-11"33-=="66-@@ ? ?? 6 7 7 ?	A2	3 > ? 	?? 25/21 --"*/*8!+K1+$=?f 9/*8!+K1+"9-$=
?f v14 
 Ls   Kc                       e Zd ZdZ	 	 	 	 	 	 ddZd Zd Z	 	 	 	 	 	 ddZd Zd Z	d	 Z
d
 Zd Zd Zd ZddZd Zd Z	 ddZd Zy)rI   zCore Embedding Column.Nc	                 V    ~t         j                  j                  | ||||d d d d|
      S )NT)r5   r6   rZ   r[   max_normr]   r9   )rb   EmbeddingColumn__new__)	clsr3   r4   r5   r6   r7   r8   r9   bypass_scope_validations	            r&   rs   z_TPUEmbeddingColumnV2.__new__  sC     	 !!)) "; * 
= 
=r%   c                     | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  fS rO   )_tpu_categorical_columnr4   r5   r6   _max_sequence_length_learning_rate_fnr9   _bypass_scope_validationselfs    r&   __getnewargs__z$_TPUEmbeddingColumnV2.__getnewargs__  sM    (($..$--d779O9O**D,I,IK Kr%   c                 B    t        fd| j                         D         S )Nc              3   J   K   | ]  }t        j                  |        y wrO   copydeepcopyrV   amemos     r&   rX   z5_TPUEmbeddingColumnV2.__deepcopy__.<locals>.<genexpr>       	@Q$--4
 	@    #)rI   r}   r|   r   s    `r&   __deepcopy__z"_TPUEmbeddingColumnV2.__deepcopy__  s#     	@$*=*=*?	@B Br%   c	                 R    t        j                  | |||       d | _        || _        y N)r7   r8   )r   __init___keyrz   )	r|   r3   r4   r5   r6   r7   r8   r9   ru   s	            r&   r   z_TPUEmbeddingColumnV2.__init__  s1     $$/)	+
 DI %<D!r%   c                     | j                   S rO   r5   r{   s    r&   get_combinerz"_TPUEmbeddingColumnV2.get_combiner      ==r%   c                 F    | j                   j                  | j                  fS zReturns num_ids and width.)r3   ra   r4   r{   s    r&   get_embedding_table_sizez._TPUEmbeddingColumnV2.get_embedding_table_size  s    ##00$..AAr%   c                     | j                         r | j                  j                  j                  S | j                  j                  S zget_feature_key_name.is_categorical_column_weightedr3   rD   r{   s    r&   get_feature_key_namez*_TPUEmbeddingColumnV2.get_feature_key_name  8    **,$$77<<<""'''r%   c                 P    | j                         r| j                  j                  S yzget_weight_key_name.Nr   r3   weight_feature_keyr{   s    r&   get_weight_key_namez)_TPUEmbeddingColumnV2.get_weight_key_name  #    **,$$777r%   c                 .    | j                   j                  S zget_embedding_var_name.)r3   rD   r{   s    r&   get_embedding_var_namez,_TPUEmbeddingColumnV2.get_embedding_var_name  s    ""'''r%   c                     | j                   S rO   )r6   r{   s    r&   get_initializerz%_TPUEmbeddingColumnV2.get_initializer  s    r%   c                 n    t        | j                  t        j                  t        j
                  f      ryyzDCheck if the categorical column of the embedding column is weighted.TFr<   r3   fc_WeightedCategoricalColumnrb   WeightedCategoricalColumnr{   s    r&   r   z4_TPUEmbeddingColumnV2.is_categorical_column_weighted  2    )),,	./
 r%   c                 R    t        j                         r fd}t        j                  |      S t	               r"t
        j                  j                         S j                   j                               }t         j                         d j                         |S )Nc                  H    t         j                  j                         S rO   )rb   rr   _get_dense_tensorinputsr|   r]   weight_collectionss   r&   host_computationzA_TPUEmbeddingColumnV2._get_dense_tensor.<locals>.host_computation  s&    %%77&,i9 	9r%   embedding_weightsru   )r   under_tpu_inference_contextr   outside_compilationr   rb   rr   r   getr   r   r   rz   )r|   r   r   r]   r   tensors   ````  r&   r   z'_TPUEmbeddingColumnV2._get_dense_tensor  s    
&&(9 001ABB##55
*I7 7
 ZZ1134F $##% $ = =?
 Mr%   c                     t               r t        j                  j                  | |      S t	        | j                         d| j                         y )Nr   r   )r   rb   rr   create_stater   r   rz   )r|   state_managers     r&   r   z"_TPUEmbeddingColumnV2.create_state  sH    ##00
  $##% $ = =?r%   c                     t        j                         r fd}t        j                  |      S t	               r!t
        j                  j                         S j                   j                               }|S )Nc                  F    t         j                  j                         S rO   )rb   rr   get_dense_tensorr|   r   transformation_caches   r&   r   z@_TPUEmbeddingColumnV2.get_dense_tensor.<locals>.host_computation"  s$    %%66&7 	7r%   )
r   r   r   r   r   rb   rr   r   r   r   r|   r   r   r   r   s   ```  r&   r   z&_TPUEmbeddingColumnV2.get_dense_tensor   sv    
&&(7 001ABB##44
$m5 5
 "%%d&?&?&A&35F Mr%   c                     t        j                         r fd}t        j                  |      S t	               r"t
        j                  j                         S j                   j                               }j                   j                               }t        j                  |d      }t         j                         d j                         t
        j                   j#                  ||      S )Nc                  H    t         j                  j                         S rO   )rb   rr   _get_sequence_dense_tensorr   s   r&   r   zJ_TPUEmbeddingColumnV2._get_sequence_dense_tensor.<locals>.host_computation6  s&    %%@@&,i9 	9r%   r   r   dense_tensorsequence_length)r   r   r   r   r   rb   rr   r   r   r   $get_sequence_length_feature_key_namer   squeezer   r   rz   SequenceDenseColumnTensorSequenceLengthPair)r|   r   r   r]   r   r   tensor_lengthss   ````   r&   r   z0_TPUEmbeddingColumnV2._get_sequence_dense_tensor3  s    
&&(9 001ABB##>>
*I7 7 ZZ1134FZZ I I KLN &&~r:N $##% $ = =?
 %%>>^ ? = =r%   c                     t        j                         r fd}t        j                  |      S t	               r!t
        j                  j                         S j                   j                               }j                   j                               }t        j                  |d      }t
        j                  j                  ||      S )Nc                  F    t         j                  j                         S rO   )rb   rr   get_sequence_dense_tensorr   s   r&   r   zI_TPUEmbeddingColumnV2.get_sequence_dense_tensor.<locals>.host_computationR  s$    %%??&7 	7r%   r   r   )r   r   r   r   r   rb   rr   r   r   r   r   r   r   r   r   r|   r   r   r   r   r   s   ```   r&   r   z/_TPUEmbeddingColumnV2.get_sequence_dense_tensorP  s    
&&(7 001ABB##==
$m5 5 "%%d&?&?&A&35F)--113N &&~r:N%%>>^ ? = =r%   )r.   Nr   NTFNN)r   r   r    __doc__rs   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r%   r&   rI   rI     s    
 "##(,&+=.K
B #$ $)-',<(B((0?( 8<=:=r%   rI   c                   z    e Zd ZdZ	 	 	 	 	 	 ddZd Zd Z	 	 	 	 	 	 ddZd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zy)re   zCore Shared Embedding Column.Nc	                 L    t         j                  j                  | |||d |      S )N)r5   r^   rq   r9   )rb   SharedEmbeddingColumnrs   )	rt   r3   r^   r5   r6   r_   r7   r8   r9   s	            r&   rs   z#_TPUSharedEmbeddingColumnV2.__new__o  s4     ''//(G"; 0 = =r%   c                     | j                   | j                  | j                  | j                  | j                  | j
                  | j                  fS rO   )rw   r^   r5   _initializer!_shared_embedding_collection_namerx   ry   r{   s    r&   r}   z*_TPUSharedEmbeddingColumnV2.__getnewargs__  sF    (($*N*NMM4,,22D4M4M""$ $r%   c                 B    t        fd| j                         D         S )Nc              3   J   K   | ]  }t        j                  |        y wrO   r   r   s     r&   rX   z;_TPUSharedEmbeddingColumnV2.__deepcopy__.<locals>.<genexpr>  r   r   )re   r}   r   s    `r&   r   z(_TPUSharedEmbeddingColumnV2.__deepcopy__  s#    &	@$*=*=*?	@B Br%   c	                 R    t        j                  | |||       || _        || _        y r   )r   r   r   r   )	r|   r3   r^   r5   r6   r_   r7   r8   r9   s	            r&   r   z$_TPUSharedEmbeddingColumnV2.__init__  s0     $$/)	+
 $D-MD*r%   c                     | j                   S rO   r   r{   s    r&   r   z(_TPUSharedEmbeddingColumnV2.get_combiner  r   r%   c                 Z    | j                   j                  | j                  j                  fS r   )r3   ra   r^   r4   r{   s    r&   r   z4_TPUSharedEmbeddingColumnV2.get_embedding_table_size  s*    ##0000::< <r%   c                     | j                         r | j                  j                  j                  S | j                  j                  S r   r   r{   s    r&   r   z0_TPUSharedEmbeddingColumnV2.get_feature_key_name  r   r%   c                 P    | j                         r| j                  j                  S yr   r   r{   s    r&   r   z/_TPUSharedEmbeddingColumnV2.get_weight_key_name  r   r%   c                     | j                   S r   )r   r{   s    r&   r   z2_TPUSharedEmbeddingColumnV2.get_embedding_var_name  s    111r%   c                     | j                   S rO   )r   r{   s    r&   r   z+_TPUSharedEmbeddingColumnV2.get_initializer  s    r%   c                 n    t        | j                  t        j                  t        j
                  f      ryyr   r   r{   s    r&   r   z:_TPUSharedEmbeddingColumnV2.is_categorical_column_weighted  r   r%   c                 b    t        j                         r fd}t        j                  |      S t	               r!t
        j                  j                         S j                   j                               }t         j                          j                  j                  d       |S )Nc                  F    t         j                  j                         S rO   )rb   r   _get_dense_tensor_internalr   s   r&   r   zP_TPUSharedEmbeddingColumnV2._get_dense_tensor_internal.<locals>.host_computation  s$    ++FF&7 	7r%   T)is_shared_embedding)r   r   r   r   r   rb   r   r   r   r   r   r   r^   _namer   s   ```  r&   r   z6_TPUSharedEmbeddingColumnV2._get_dense_tensor_internal  s    
&&(7 001ABB))DD
$m5 5
 "%%d&?&?&A&35F
 $##%,,22 " Mr%   c                     t        j                         r fd}t        j                  |      S t	               r!t
        j                  j                         S  j                        }j                   j                               }t        j                  |d      }t
        j                  j                  ||      S )Nc                  F    t         j                  j                         S rO   )rb   r   r   r   s   r&   r   zO_TPUSharedEmbeddingColumnV2.get_sequence_dense_tensor.<locals>.host_computation  s$    ++EE&7 	7r%   r   r   )r   r   r   r   r   rb   r   r   r   r   r   r   r   r   r   r   s   ```   r&   r   z5_TPUSharedEmbeddingColumnV2.get_sequence_dense_tensor  s    
&&(7 001ABB))CC
$m5 5 ,,m-F)--113N &&~r:N%%>>^ ? = =r%   )r.   NNr   NT)r   r   r    r   rs   r}   r   r   r   r   r   r   r   r   r   r   r   r$   r%   r&   re   re   k  sp    %
 /3"##(,=$$B 04#$ $)-N$<
(24=r%   re   c                     g }g }| D ]c  }t        |t        t        f      st        dt	        |       d      |j                         r|j                  |       S|j                  |       e ||fS )a  Split a list of _TPUEmbeddingColumn into sequence and non-sequence columns.

  For use in a TPUEstimator model_fn function. E.g.

  def model_fn(features):
    sequence_columns, feature_columns = (
        tf.tpu.feature_column.split_sequence_columns(feature_columns))
    input = tf.feature_column.input_layer(
        features=features, feature_columns=feature_columns)
    sequence_features, sequence_lengths = (
        tf.contrib.feature_column.sequence_input_layer(
            features=features, feature_columns=sequence_columns))

  Args:
    feature_columns: A list of _TPUEmbeddingColumns to split.

  Returns:
    Two lists of _TPUEmbeddingColumns, the first is the sequence columns and the
    second is the non-sequence columns.
  zNcolumn must be a _TPUEmbeddingColumnV2 or _TPUSharedEmbeddingColumnV2 but got z	 instead.)r<   rI   re   r=   r@   is_sequence_columnrg   )feature_columnssequence_columnsnon_sequence_columnsrm   s       r&   split_sequence_columns_v2r     s    *  	*ff4:< =115fiIJ J   "f%!!&)	* 
/	//r%   c                 v   |\  }}t        j                  |      5  | j                         j                         \  }}|j                         j                         \  }}	|sd}t	        j
                  t        j                  | t	        j
                  |||	g            ||	|g      }
t	        j
                  |||	dg      }t        j                  |
|z  d      }|dk(  r|cddd       S |dk(  r7|t        j                  t        j                  |d      d      z  cddd       S t        d      # 1 sw Y   yxY w)	a  Uses XLA's dynamic slice operations to perform embedding lookups.

  From third_party/cloud_tpu/models/movielens/tpu_embedding.py

  Args:
    params: Tensor of embedding table. Rank 2 (table_size x embedding dim)
    values_and_values_mask: is a two-tuple that contains: values - Tensor of
      embedding indices. Rank 2 (batch x n_indices) values_mask - Tensor of mask
      / weights. Rank 2 (batch x n_indices)
    combiner: The combiner to use for the embedding lookup. Currently supports
      'sum' and 'mean'.
    name: Optional name scope for created ops

  Returns:
    Rank 2 tensor of aggregated (per batch element) embedding vectors.

  Raises:
    ValueError: Combiner is not supported.
  r   r   )axissumNr.   g      ?zFDense TPU Embedding does not support combiner other than sum and mean.)r   
name_scope	get_shapeas_listr   reshaper   embedding_lookupr
   
reduce_summaximumrA   )paramsvalues_and_values_maskr5   rD   valuesvalues_maskrT   embedding_dimensionn_batchn_indices_padded
emb_lookupvalues_mask_broadcastaggregate_embs                r&    sparse_embedding_aggregate_slicer    sT   . /&+
~~d 3#--/779A & 0 0 2 : : <Gg""&&I%%fw8H.IJ	L	"$78:J
 &--k/68H!.LN''**4M5!3 3" 
V	 X--


3!
<cC C/3 34  2 3 353 3s   CD/(2D/$D//D8c                    | j                   d   }t        j                  | ddg||g      } | j                  | j                  }}t        j                  |t        j                  |t        j                        ||f      }t        j                  |t        j                        }t        j                  ||||f      }||fS )ad  Creates statically-sized Tensors containing indices and weights.

  From third_party/cloud_tpu/models/movielens/tpu_embedding.py

  Also computes sparse_indices.values % embedding_table_size, for equivalent
  functionality to sparse_column_with_integerized_feature. The returned
  padded weight Tensor also doubles as a mask indicating which values in
  the returned padded indices Tensor are indices versus padded zeros.

  Args:
    sparse_indices: SparseTensor of embedding lookup indices.
    padded_size: Number of columns of the returned Tensors. Indices which fall
      out of bounds will be truncated to the padded size.

  Returns:
    (sparse_indices.values padded to the specified size,
     a mask the same size as the returned padded values in which 0s
     indicate padded locations and 1s (or values from sparse_weights)
     indicate actual values)
  r   )shape)dtype)dense_shaper   sparse_sliceindicesr   r   
scatter_ndr
   castr   int32	ones_likefloat32)sparse_indicespadded_size
batch_sizer  r   padded_valuesweightspadded_masks           r&   #pad_sparse_embedding_lookup_indicesr  R  s    * ))!,***>Aq6,6+DF."**N,A,A6'&&mmFFLL)%'-
 fnn='$$wz;79+ 
	##r%   c                     t        j                         r| t        j                  k(  rt	        d      | t        j
                  k(  r t        j                         st	        d      yy)z:Checks for invalid embedding_lookup_device configurations.zSUsing embedding_lookup_device=tpu_embedding_core during inference is not supported.z]Using TPUEmbeddingColumn with embedding_lookup_device="cpu" during training is not supported.N)r   r   r   r#   rA   r!   )r:   s    r&   _check_invalid_casesr  x  sf    	%%'!C!CC
	   3 33**,./ / - 4r%   c                   J     e Zd ZdZd Zd Zd Z fdZ fdZd fd	Z	 xZ
S )	rJ   z6TPUEmbeddingColumn which allows serving on TensorCore.c                 x    d|v r|d   | _         |d= d|v r|d   | _        |d= t        j                  | g|i |S Nr;   r:   )_tensor_core_shape_embedding_lookup_devicerI   rs   rt   argskwargss      r&   rs   z+_TPUDeviceSpecificEmbeddingColumnV2.__new__  s[    f$%&9:c
$
% F*%+,E%Fc"
*
+ ((>t>v>>r%   c                 z    d|v r|d   | _         |d= d|v r|d   | _        |d= t        j                  | g|i | y r  )r  r  rI   r   r|   r!  r"  s      r&   r   z,_TPUDeviceSpecificEmbeddingColumnV2.__init__  sX    f$ &': ;d
$
% F*&,-F&Gd#
*
+""49$9&9r%   c                 r    t        fd| j                         D        | j                  | j                  dS )Nc              3   J   K   | ]  }t        j                  |        y wrO   r   r   s     r&   rX   zC_TPUDeviceSpecificEmbeddingColumnV2.__deepcopy__.<locals>.<genexpr>  r   r   r;   r:   )rJ   r}   r  r  r   s    `r&   r   z0_TPUDeviceSpecificEmbeddingColumnV2.__deepcopy__  s3    .	@$*=*=*?	@11 $ = =? ?r%   c                 h   t        | j                         | j                  t        j                  k(  }|xs
 t	               }|r t
        j                  j                  | |      S | j                  t        j                  k(  rt        t        |   |      S t
        j                  j                  | |      S rO   )r  r  r   r!   r   rb   rr   r   r#   superrJ   )r|   r   is_cpu	__class__s      r&   r   z0_TPUDeviceSpecificEmbeddingColumnV2.create_state  s    667**o.A.AAF+)+F##00}EE		&	&/*L*L	L6&&345 !!..t]CCr%   c                     t         j                          j                  t        j                  k(  }|xs
 t	               }|rt
        t           ||      S  j                  t        j                  k(  rt
        t           ||      S t        j                         rE|j                   j                  j                  |       fd}t        j                  |      \  }}nS|j                   j                  j                  |      }|j                   j                  j                  t         z   |      }|j#                   d      }t%        |||f j'                               S )z-Private method that follows get_dense_tensor.c                  6    t         j                  d         S Nr   r  r  r|   sparse_tensors   r&   r   zN_TPUDeviceSpecificEmbeddingColumnV2.get_dense_tensor.<locals>.host_computation  "    2=373J3J13MO 	Or%   r   rP   )r  r  r   r!   r   r)  rJ   r   r#   r   r   r   r3   rD   r   r   _TENSOR_CORE_MASK_KEY_SUFFIXget_variabler  r   
r|   r   r   r*  r   r   maskr   r1  r+  s
   `       @r&   r   z4_TPUDeviceSpecificEmbeddingColumnV2.get_dense_tensor  s_   667**o.A.AAF+)+F6**>NO 
	&	&/*L*L	L6**>NO &&(*..t/F/F/K/K/<>mO %889IJlfd $''(?(?(D(D(57f!%%

!
!
&
&)E
E
d &22& 3 (+,=~,0,=,=,?A Ar%   c                    
 t         j                          j                  t        j                  k(  }|xs
 t	               }|rt
        t           |||      S  j                  t        j                  k(  rt
        t           |||      S t        j                         r>|j                   j                               
 
fd}t        j                  |      \  }}nE|j                   j                               }|j                   j                         t        z         } j                   j"                   j$                  f}|rEt&        j(                  j*                  |vr)|j-                  t&        j(                  j*                         t/        j0                  d|t2        j4                   j6                   j8                  xr ||      }	t;        |	||f j=                               S )Nc                  6    t         j                  d         S r.  r/  r0  s   r&   r   zO_TPUDeviceSpecificEmbeddingColumnV2._get_dense_tensor.<locals>.host_computation  r2  r%   r   )rD   r  r	  r6   r]   collections)r  r  r   r!   r   r)  rJ   r   r#   r   r   r   r   r   r   r3  r3   ra   r4   r   	GraphKeysGLOBAL_VARIABLESrg   r   r4  r   r  r6   r]   r  r   )r|   r   r   r]   r*  r   r   r6  embedding_shaper   r1  r+  s   `         @r&   r   z5_TPUDeviceSpecificEmbeddingColumnV2._get_dense_tensor  s   667**o.A.AAF+)+F6++13E+466 
	&	&/*L*L	L6++13E+466
 &&(jj!:!:!<=mO %889IJlfd zz$3356fZZ11345 6d ..;;T^^LO&&.@@ > >?&33 nn$$...Y&( ,,=~,0,=,=,?A Ar%   r   )r   r   r    r   rs   r   r   r   r   r   __classcell__r+  s   @r&   rJ   rJ     s.    >?:?D#AJ+A +Ar%   rJ   c                   4     e Zd ZdZd Zd Zd Z fdZ xZS )rf   z>TPUSharedEmbeddingColumnV2 which allows serving on TensorCore.c                 x    d|v r|d   | _         |d= d|v r|d   | _        |d= t        j                  | g|i |S r  )r  r  re   rs   r   s      r&   rs   z1_TPUSharedDeviceSpecificEmbeddingColumnV2.__new__  s[    f$%&9:c
$
% F*%+,E%Fc"
*
+&..sDTDVDDr%   c                 z    d|v r|d   | _         |d= d|v r|d   | _        |d= t        j                  | g|i | y r  )r  r  re   r   r$  s      r&   r   z2_TPUSharedDeviceSpecificEmbeddingColumnV2.__init__  sX    f$ &': ;d
$
% F*&,-F&Gd#
*
+((???r%   c                 r    t        fd| j                         D        | j                  | j                  dS )Nc              3   J   K   | ]  }t        j                  |        y wrO   r   r   s     r&   rX   zI_TPUSharedDeviceSpecificEmbeddingColumnV2.__deepcopy__.<locals>.<genexpr>  r   r   r'  )rf   r}   r  r  r   s    `r&   r   z6_TPUSharedDeviceSpecificEmbeddingColumnV2.__deepcopy__  s3    4	@$*=*=*?	@11 $ = =? ?r%   c                     t         j                          j                  t        j                  k(  }|xs
 t	               }|rt
        t           ||      S  j                  t        j                  k(  rt
        t           ||      S t        j                         rE|j                   j                  j                  |       fd}t        j                  |      \  }}nS|j                   j                  j                  |      }|j                   j                  j                  t         z   |      } j"                  j$                  }t'        |||f j)                               S )z7Private method that follows _get_dense_tensor_internal.c                  6    t         j                  d         S r.  r/  r0  s   r&   r   z^_TPUSharedDeviceSpecificEmbeddingColumnV2._get_dense_tensor_internal.<locals>.host_computation9  r2  r%   )r  r  r   r!   r   r)  rf   r   r#   r   r   r   r3   rD   r   r   r3  r^   r   r  r   r5  s
   `       @r&   r   zD_TPUSharedDeviceSpecificEmbeddingColumnV2._get_dense_tensor_internal#  s]   667**o.A.AAF+)+F<44H4ACC $$(J(JJ<44H4ACC
 &&(*..t/F/F/K/K/<>mO %889IJlfd $''(?(?(D(D(57f!%%

!
!
&
&)E
E
d
 <<NN+,=~,0,=,=,?A Ar%   )	r   r   r    r   rs   r   r   r   r=  r>  s   @r&   rf   rf     s#    F	E@?&A &Ar%   rf   )r.   Nr   NNNT)r.   NNNNNNT)r.   r  ).r   r   enumrF    tensorflow.python.feature_columnr   r   r   rb   tensorflow.python.frameworkr   r   tensorflow.python.opsr   r   r	   r
   r   r   tensorflow.python.tpur   r   $tensorflow.python.tpu.feature_columnr   r   r   r   r    tensorflow.python.util.tf_exportr   rH   r3  Enumr   rL   rn   rr   rI   r   re   r   r  r  r  rJ   rf   r$   r%   r&   <module>rN     sL   "    A I . + + / * * , 0 % 1 C P R L H 6 D 3 dii  234 "($(,-)-04*.26X= 5X=v :;< *0,0AE59158<26:>O =OdH=3V5K5K H=VJ="9"(">">J=Z!0L /5*L33l#$L/|A*? |A~DA0K DAr%   