
    BVhq                     8   d Z ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddl	mZ ddl	mZ dd	lmZ dd
lmZ ddlmZ dZej$                  ej&                  fZej*                  fZej.                  ej0                  ej2                  fZej6                  ej8                  ej:                  ej<                  ej$                  fez   ZdZ 	 	 	 	 	 ddZ!	 	 	 	 	 	 ddZ" G d de#      Z$ G d de$ejJ                        Z& G d de$ejN                        Z(	 	 ddZ)d Z*d Z+y)zTPU Feature Column Library.    N)feature_column)feature_column_lib)ops)	array_ops)init_ops)variable_scope)tpu)tpu_function)tpu_replication_tpu_feature_column_scope_seq_length_c                   
 t        | t              rt        dt        |              t        | t              sOt        dj                  dj                  t        D cg c]  }|j                   c}      t        |                   ||dk  rt        dj                  |            /t              s$t        dj                  | j                              -t        j                  ddt        j                  |      z  	      | j                  |f

fd
}t!        | |||dddd|||      }	|	_        |	S c c}w )a
  TPU embedding_column for `tf.feature_column.embedding_column`.

  Note that the interface for TPU embedding_column is different from the non-TPU
  version. The following args available for the non-TPU version are NOT
  supported: ckpt_to_load_from, tensor_name_in_ckp, max_norm and trainable.

  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.
    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  _TPUEmbeddingColumn.

  Raises:
    ValueError: if `dimension` not > 0.
    ValueError: if `initializer` is specified but not callable.
    TypeError: if categorical_column is not a supported type.
  Acategorical_column for tpu  embedding_column was denylisted type zEcategorical_column for tpu  embedding_column must be type {}, got {}. or N   Invalid dimension {}.zGinitializer must be callable if specified. Embedding of column_name: {}        meanstddevc                 L    t        j                  | dd      } |d |      S )NTembedding_column_layer)embedding_shapeinitializerweight_collections	trainablename)scope)fc_EmbeddingColumnLayer)r   r   r   r   r   s      T/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/tpu/feature_column.py_creatorz"embedding_column.<locals>._creator   s2    55'-%' "$e44    T)categorical_column	dimensioncombinerlayer_creatorckpt_to_load_fromtensor_name_in_ckptmax_normr   max_sequence_lengthlearning_rate_fnuse_safe_embedding_lookup)
isinstance"_DENYLISTED_CATEGORICAL_COLUMNS_V2	TypeErrortype_SUPPORTED_CATEGORICAL_COLUMNSformatjoin__name__
ValueErrorcallabler   r   truncated_normal_initializermathsqrt_num_buckets_TPUEmbeddingColumn_tpu_initializer)r$   r%   r&   r   r+   r,   r-   ccr"   columnr   s      `      @r!   embedding_columnr@   4   sk   l "$FG
 ''+,>'?&@B C C 
&(F	G
	55;VFKK"@I
BKKI
 =#$6&' '
 Y]
,33I>
??(=
 44:F+00523 3 77TYYy113K '33Y>/5 +-' 9;&  (&	-UI
s   D=c                    | D ]  }t        |t              rt        dt        |             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                  |            |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#        | |      D ]-  \  }}t%        |||||dddd|||      }|j'                  |       / |S c c}	w )at  List of dense columns that convert from sparse, categorical input.

  Note that the interface for TPU embedding_column is different from the non-TPU
  version. The following args available for the non-TPU version are NOT
  supported: ckpt_to_load_from, tensor_name_in_ckp, max_norm and trainable.

  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.
    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  _TPUEmbeddingColumn.

  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.
  r   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   z+initializer must be callable if specified. r   r   c                     | j                   S Nr   )xs    r!   <lambda>z*shared_embedding_columns.<locals>.<lambda>   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rC   rD   ).0cs     r!   	<genexpr>z+shared_embedding_columns.<locals>.<genexpr>
  s     /O1/Os   _shared_embeddingT)r$   r%   r&   r    shared_embedding_collection_namer(   r)   r*   r   r+   r,   r-   )r.   r/   r0   r1   r2   r3   r4   r5   lenr6   r7   r   r8   r9   r:   sortedr;   zip_TPUSharedEmbeddingColumnappend)categorical_columnsr%   r&   r   rN   max_sequence_lengthsr,   r-   r$   r>   sorted_columnsnum_bucketsrK   tpu_columnsr+   r?   s                   r!   shared_embedding_columnsrY      sj   @ 0 )$&HI ?0124 5 5 (*HI??Evkk)GH22;;HJ%&@() )) 
3%8!99	#&9"::
 44:F12C8K4L5NO O
 Y]
,33I>
??(=
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$$(;;$+ 25/21 --&-)I /)";=F v" 
s Is   !H
c                   ^    e 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d Zd Zy)_TPUBaseEmbeddingColumnz$Base class for TPU Embedding Column.Nc                    || _         || _        || _        | j                         r*|dk  r%t	        dj                  ||j                              | j                         s+|dk7  r%t	        dj                  ||j                              y y )Nr   zsmax_sequence_length must be greater than 0 for sequence columns. Got max_sequence_length={} for sequence column {}.r   z@Non zero max_seq_length={} specified for non sequence column {}.)_tpu_categorical_column_max_sequence_length_learning_rate_fnis_sequence_columnr6   r3   r   )selfr$   r+   r,   s       r!   __init__z _TPUBaseEmbeddingColumn.__init__'  s     $6D  3D-D!&9A&= --3V4G4F4K4K.MN N ##%*=*B --3V4G4F4K4K.MN N +C%r#   c                     t        d      )zReturns the embedding combiner.not implementedNotImplementedErrorra   s    r!   get_combinerz$_TPUBaseEmbeddingColumn.get_combiner8      
/
00r#   c                     t        d      )zDReturns the embedding table size, tuple of vocab size and dimension.rd   re   rg   s    r!   get_embedding_table_sizez0_TPUBaseEmbeddingColumn.get_embedding_table_size<  ri   r#   c                     t        d      )z2Returns the feature key name in the features dict.not implre   rg   s    r!   get_feature_key_namez,_TPUBaseEmbeddingColumn.get_feature_key_name@      
j
))r#   c                     t        d      )z Return the key name for weights.rm   re   rg   s    r!   get_weight_key_namez+_TPUBaseEmbeddingColumn.get_weight_key_nameD  ro   r#   c                     t        d      )zReturns the embedding variable name.

    Feature key name and embedding variable name are usually one-to-one mapping.
    But for shared embedding columns, it is many-to-one mapping.
    rm   re   rg   s    r!   get_embedding_var_namez._TPUBaseEmbeddingColumn.get_embedding_var_nameH  s     j
))r#   c                     t        d      )zReturns the initializer.rm   re   rg   s    r!   get_initializerz'_TPUBaseEmbeddingColumn.get_initializerP  ro   r#   c                     t        d      )DCheck if the categorical column of the embedding column is weighted.rm   re   rg   s    r!   is_categorical_column_weightedz6_TPUBaseEmbeddingColumn.is_categorical_column_weightedT  ro   r#   c                 6    t        | j                  t              S rC   )r.   r]   _SUPPORTED_SEQUENCE_COLUMNSrg   s    r!   r`   z*_TPUBaseEmbeddingColumn.is_sequence_columnX  s    d224OPPr#   c                     | j                   S rC   )r^   rg   s    r!   get_max_sequence_lengthz/_TPUBaseEmbeddingColumn.get_max_sequence_length[  s    $$$r#   c                     | j                   S rC   )r_   rg   s    r!   get_learning_rate_fnz,_TPUBaseEmbeddingColumn.get_learning_rate_fn^  s    !!!r#   c                 4    t        | j                               S )z7Get the key for the associated sequence length feature.):get_sequence_length_feature_key_name_from_feature_key_namern   rg   s    r!   $get_sequence_length_feature_key_namez<_TPUBaseEmbeddingColumn.get_sequence_length_feature_key_namea  s    E!!#% %r#   )r   N)r5   
__module____qualname____doc__rb   rh   rk   rn   rq   rs   ru   rx   r`   r|   r~   r    r#   r!   r[   r[   $  sK    , $% $N"11*****Q%"%r#   r[   c                       e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 ddZd Zd Zd Zd Z	d	 Z
d
 Zd ZddZ	 ddZy)r<   zCore Embedding Column.Nc                 V    ~t         j                  j                  | |||||||||
      S )N)r&   r'   r(   r)   r*   r   r-   )r   _EmbeddingColumn__new__)clsr$   r%   r&   r'   r(   r)   r*   r   r+   r,   r-   bypass_scope_validations                r!   r   z_TPUEmbeddingColumn.__new__j  sC      	 &&#+/"; ' 
= 
=r#   c                 R    t         j                  | ||	|
       d | _        || _        y N)r+   r,   )r[   rb   _key_bypass_scope_validation)ra   r$   r%   r&   r'   r(   r)   r*   r   r+   r,   r-   r   s                r!   rb   z_TPUEmbeddingColumn.__init__  s6     $$/)	 % +
 DI %<D!r#   c                     | j                   S rC   r&   rg   s    r!   rh   z _TPUEmbeddingColumn.get_combiner      ==r#   c                 F    | j                   j                  | j                  fS zReturns num_ids and width.r$   r;   r%   rg   s    r!   rk   z,_TPUEmbeddingColumn.get_embedding_table_size      ##00$..AAr#   c                     | j                         r | j                  j                  j                  S | j                  j                  S zget_feature_key_name.rx   r$   r   rg   s    r!   rn   z(_TPUEmbeddingColumn.get_feature_key_name  8    **,$$77<<<""'''r#   c                 P    | j                         r| j                  j                  S yzget_weight_key_name.Nrx   r$   weight_feature_keyrg   s    r!   rq   z'_TPUEmbeddingColumn.get_weight_key_name  #    **,$$777r#   c                 .    | j                   j                  S zget_embedding_var_name.)r$   r   rg   s    r!   rs   z*_TPUEmbeddingColumn.get_embedding_var_name  s    ""'''r#   c                     | j                   S rC   )r=   rg   s    r!   ru   z#_TPUEmbeddingColumn.get_initializer  s       r#   c                 n    t        | j                  t        j                  t        j
                  f      ryyrw   TFr.   r$   r   _WeightedCategoricalColumnfc_libWeightedCategoricalColumnrg   s    r!   rx   z2_TPUEmbeddingColumn.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 rC   )r   r   _get_dense_tensorinputsra   r   r   s   r!   host_computationz?_TPUEmbeddingColumn._get_dense_tensor.<locals>.host_computation  s&    ""44&,i9 	9r#   embedding_weightsr   )r	   under_tpu_inference_contextr   outside_compilation_is_running_on_cpur   r   r   getrn   _record_variable_scope_and_namers   r   ra   r   r   r   r   tensors   ````  r!   r   z%_TPUEmbeddingColumn._get_dense_tensor  s    
&&(9 001ABB  22
*I7 7
 ZZ1134F $##% $ = =?
 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 rC   )r   r   _get_sequence_dense_tensorr   s   r!   r   zH_TPUEmbeddingColumn._get_sequence_dense_tensor.<locals>.host_computation  s&    ""==&,i9 	9r#   r   r   dense_tensorsequence_length)r	   r   r   r   r   r   r   r   r   rn   r   r   squeezer   rs   r   _SequenceDenseColumnTensorSequenceLengthPairra   r   r   r   r   r   tensor_lengthss   ````   r!   r   z._TPUEmbeddingColumn._get_sequence_dense_tensor  s    
&&(9 001ABB  ;;
*I7 7 ZZ1134FZZ I I KLN &&~r:N $##% $ = =?
 "";;^ < = =r#   )
r   NNNNTr   NTFNNr5   r   r   r   r   rb   rh   rk   rn   rq   rs   ru   rx   r   r   r   r#   r!   r<   r<   g  s    
   $"&"##(,&+=B !!%#'#$ $)-',<0B((!2 8<=r#   r<   c                       e Zd ZdZ	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 ddZd Zd Zd Zd Z	d	 Z
d
 Zd ZddZ	 ddZy)rR   zCore Shared Embedding Column.Nc                 V    t         j                  j                  | |||||||||	|      S )N)r&   r   rN   r(   r)   r*   r   r-   )r   _SharedEmbeddingColumnr   )r   r$   r%   r&   r   rN   r(   r)   r*   r   r+   r,   r-   s                r!   r   z!_TPUSharedEmbeddingColumn.__new__  sC     $$,,)I+/"; - = =r#   c                 D    t         j                  | ||
|       d | _        y r   )r[   rb   r   )ra   r$   r%   r&   r   rN   r(   r)   r*   r   r+   r,   r-   s                r!   rb   z"_TPUSharedEmbeddingColumn.__init__  s,     $$/)	 % +
 DIr#   c                     | j                   S rC   r   rg   s    r!   rh   z&_TPUSharedEmbeddingColumn.get_combiner-  r   r#   c                 F    | j                   j                  | j                  fS r   r   rg   s    r!   rk   z2_TPUSharedEmbeddingColumn.get_embedding_table_size0  r   r#   c                     | j                         r | j                  j                  j                  S | j                  j                  S r   r   rg   s    r!   rn   z._TPUSharedEmbeddingColumn.get_feature_key_name4  r   r#   c                 P    | j                         r| j                  j                  S yr   r   rg   s    r!   rq   z-_TPUSharedEmbeddingColumn.get_weight_key_name:  r   r#   c                     | j                   S r   )rN   rg   s    r!   rs   z0_TPUSharedEmbeddingColumn.get_embedding_var_name@  s    000r#   c                     | j                   S rC   )r   rg   s    r!   ru   z)_TPUSharedEmbeddingColumn.get_initializerD  s    r#   c                 n    t        | j                  t        j                  t        j
                  f      ryyr   r   rg   s    r!   rx   z8_TPUSharedEmbeddingColumn.is_categorical_column_weightedG  r   r#   c                 >    t        j                         r fd}t        j                  |      S t	               r"t
        j                  j                         S j                   j                               }t         j                         dd       |S )Nc                  H    t         j                  j                         S rC   )r   r   r   r   s   r!   r   zE_TPUSharedEmbeddingColumn._get_dense_tensor.<locals>.host_computationS  s&    ((::&,i9 	9r#   r   Tis_shared_embedding)r	   r   r   r   r   r   r   r   r   rn   r   rs   r   s   ````  r!   r   z+_TPUSharedEmbeddingColumn._get_dense_tensorQ  s    
&&(9 001ABB&&88
*I7 7
 ZZ1134F $##% " Mr#   c                     t        j                         r fd}t        j                  |      S t	               r"t
        j                  j                         S j                   j                               }j                   j                               }t         j                         dd       t
        j                  j                  ||      S )Nc                  H    t         j                  j                         S rC   )r   r   r   r   s   r!   r   zN_TPUSharedEmbeddingColumn._get_sequence_dense_tensor.<locals>.host_computationk  s&    ((CC&,i9 	9r#   r   Tr   r   )r	   r   r   r   r   r   r   r   r   rn   r   r   rs   r   r   r   s   ````   r!   r   z4_TPUSharedEmbeddingColumn._get_sequence_dense_tensorh  s    
&&(9 001ABB&&AA
*I7 7 ZZ1134FZZ I I KLN $##% "
 "";;^ < = =r#   )
r   NNNNNTr   NTr   r   r   r#   r!   rR   rR     s    %
 /3 $"&"##(,=: 04!%#'#$ $)-*B(10 8<=r#   rR   c                    t        j                         }|j                  t              }|s|j	                  i        |d   }t        j                         }|j                  }| |v r_||    d   |k7  r&|s$|s"t        dj                  | |||    d               ||    d   |k7  r"t        dj                  | |||    d               y||f|| <   y)z4Add embedding variable name and scope to collection.r   zTFor embedding var name {}, the variable scope name is different, got {}; expected {}r   zOFor embedding var name {}, the embedding name is different, got {}; expected {}N)
r   get_default_graphget_collection_ref_TPU_FC_TO_SCOPErS   r   get_variable_scoper   r6   r3   )	embedding_var_nameembedding_var_name_in_fcr   r   g
collectionvar_def_dictcaptured_scopecaptured_scope_names	            r!   r   r     s   
 
!##$45*	bA,!446.&++<''(+/BB(?  &'9':'34F'G'J!LM M
 &'*.FF  &'9'?'34F'G'J!LM M G )<(@(BL#$r#   c                  B    t        j                         j                  du S )z1Returns True if the current context is CPU model.N)r
   get_tpu_contextnumber_of_shardsr   r#   r!   r   r     s    		%	%	'	8	8D	@@r#   c                     | t         z   S )zGets the name of the sequence length feature from that of the base feature.

  Args:
    feature_name: The feature key of a sequence column.

  Returns:
    A string which is the feature key for the associated feature length column.
  ) _SEQUENCE_FEATURE_LENGTH_POSTFIX)feature_names    r!   r   r     s     
8	88r#   )r   Nr   NT)r   NNNNT)FF),r   r9    tensorflow.python.feature_columnr   r   r   r   tensorflow.python.frameworkr   tensorflow.python.opsr   r   r   tensorflow.python.tpur	   r
   r   r   _SequenceCategoricalColumnSequenceCategoricalColumnrz   CategoricalColumn!_SUPPORTED_CATEGORICAL_COLUMNS_V2HashedCategoricalColumnBucketizedColumnCrossedColumnr/   _IdentityCategoricalColumn _VocabularyFileCategoricalColumn _VocabularyListCategoricalColumnr   r2   r   r@   rY   objectr[   r   r<   r   rR   r   r   r   r   r#   r!   <module>r      sW   "  A I + + * 0 % . 1 / !<<%??A  &,%=%=$? !&,&D&D&,&=&=&,&:&:&< " #%"?"?"$"E"E"$"E"E"$"?"?"$"?"?	"#
 &G"G  $2  
 %!%)*&*/3gX '-)->B26.27;CL@%f @%FP=123F3F P=fE= 7 " 9 9E=T 9><ABDA
	9r#   