
    BVh                         d Z ddlZddlmZmZmZmZmZ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	 d
ej(                        Zy)z-Base Class for TPU Embeddings Mid level APIs.    N)AnyDictIterableOptionalUnionText)dtypes)	variables)tpu_embedding_v2_utils)autotrackable)nestc                       e Zd ZdZ	 ddeej                  ef   deej                     fdZ
ed        Zdej                  ded	eeej$                  f   fd
Zd Zd Zddedee   d	efdZ	 ddedee   d	efdZy)TPUEmbeddingBasezThe TPUEmbedding Base class.

  This class only contains the basic logic to check the feature config and table
  config for the tpu embedding mid level APIs.
  Nfeature_config	optimizerc                 P   || _         g | _        t        j                  |      D ]'  }| j                  j	                  |j
                         ) g | _        t        j                  |      D ]@  }|j                  | j                  vs| j                  j	                  |j                         B g }t        | j                        D ]  \  }}|j                  ||_	        |j                  Qt        |j                  t        j                        s-t        dj                  t        |j                                    |j                   dj                  |      |_        |j                   |v rt        d|j                    d      |j	                  |j                           d| _        y)z$Creates the TPUEmbeddingBase object.Nz{} is an unsupported optimizer class. Please pass an instance of one of the optimizer classes under tf.tpu.experimental.embedding.ztable_{}z:Tables must have a unique name. Multiple tables with name z found.F)_feature_config_output_shapesr   flattenappendoutput_shape_table_configtable	enumerater   
isinstancer   
_Optimizer
ValueErrorformattypename_built)selfr   r   featuretable_namesir   s          X/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/tpu/tpu_embedding_base.py__init__zTPUEmbeddingBase.__init__"   sy   
 *DD<</ 7
  !5!567 D<</ 1	d00	0!!'--01 Kd001 %5		 #
//
%*@*K*KL ::@&!%//2;45 	5 
	&&q)
	{	" 66;jj\J K 	K$%" DK    c                     t         )z;Returns a dict of embedding tables, keyed by `TableConfig`.NotImplementedErrorr"   s    r&   embedding_tablesz!TPUEmbeddingBase.embedding_tablesL   s
     r(   r   	trainablereturnc                     j                   j                  ffdd fd	 j                  j                  |      }fd}j                  j                  j                  ||      }ni }||d<   |S )zBCreate all variables including table variables and slot variables.c                 j    ~t        j                  ||      }t        j                  | |||      S )N)dtype)r    initial_valueshaper2   r.   )	functoolspartialtf_variablesVariable)r    r4   r2   initializerr.   r3   variable_shapes         r&   getterz2TPUEmbeddingBase._create_variables.<locals>.getterV   sA    
  ''
~U4m""% r(   c                 N    j                  | |t        j                  |      S )N)r    r9   r4   r2   r;   r.   ) _add_variable_with_custom_getterr	   float32)r    r9   r.   r;   r"   r:   s      r&   variable_creatorz<TPUEmbeddingBase._create_variables.<locals>.variable_creatorc   s3     22! 3  r(   )r.   c                 8     j                   dz   | z   |d      S )N/F)r    )r    r9   r   r?   s     r&   slot_creatorz8TPUEmbeddingBase._create_variables.<locals>.slot_creatorr   s     ejj3.5{EJJr(   
parameters)T)vocabulary_sizedimr    r9   r   _create_slots)	r"   r   r.   rC   rB   	slot_varsr;   r?   r:   s	   ``    @@@r&   _create_variablesz"TPUEmbeddingBase._create_variablesQ   s}     ++UYY7N
 "

E%%<JK "////
LIii(Ilr(   c                     t         )8Create variables and slots variables for TPU embeddings.r*   r,   s    r&   _create_variables_and_slotsz,TPUEmbeddingBase._create_variables_and_slots|   s    
r(   c                 V    | j                   ry| j                         | _        d| _         y)rJ   NT)r!   rK   
_variablesr,   s    r&   buildzTPUEmbeddingBase.build   s#    {{668DODKr(   featuresweightsc                 ^    | j                   s| j                          | j                  ||      S )z.Call the mid level api to do embedding lookup.)r!   rN   embedding_lookupr"   rO   rP   s      r&   __call__zTPUEmbeddingBase.__call__   s%    ;;
jjl  733r(   c                     t         )z4Lookup the embedding table using the input features.r*   rS   s      r&   rR   z!TPUEmbeddingBase.embedding_lookup   s
     r(   )N)__name__
__module____qualname____doc__r   r   FeatureConfigr   r   r   r'   propertyr-   TableConfigboolr   r   r7   r8   rH   rK   rN   r   rT   rR    r(   r&   r   r      s     @D(2@@(JK( 0;;<(T  )%;%G%G )#'),0|7L7L1L,M)V4s 4Xc] 4c 4 15!$ (9<r(   r   )rY   r5   typingr   r   r   r   r   r   tensorflow.python.frameworkr	   tensorflow.python.opsr
   r7   tensorflow.python.tpur   tensorflow.python.trackabler   tensorflow.python.utilr   AutoTrackabler   r^   r(   r&   <module>rf      s6    4  = = . ; 8 5 'v}22 vr(   