o
    i e                     @   s~   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
 d
ejZdS )z-Base Class for TPU Embeddings Mid level APIs.    N)AnyDictIterableOptionalUnionText)dtypes)	variables)tpu_embedding_v2_utils)autotrackable)nestc                   @   s   e Zd ZdZ	ddeejef deej	 fddZ
edd Zd	ejd
edeeejf fddZdd Zdd Zddedee defddZ	ddedee defddZdS )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                 C   s   || _ g | _t|D ]	}| j|j qg | _t|D ]}|j| jvr,| j|j qg }t| jD ]A\}}|j	du r@||_	|j	durVt
|j	tjsVtdt|j	|jdu rad||_|j|v rotd|j d||j q4d| _dS )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    r#   W/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/tpu/tpu_embedding_base.py__init__"   s:   





zTPUEmbeddingBase.__init__c                 C      t )z;Returns a dict of embedding tables, keyed by `TableConfig`.NotImplementedErrorr   r#   r#   r$   embedding_tablesL   s   z!TPUEmbeddingBase.embedding_tablesr   	trainablereturnc                    st   j jffdd d fdd	jj|d}fdd}jd	ur2j||}ni }||d
< |S )zBCreate all variables including table variables and slot variables.c                    s&   ~t j| |d}tj| | ||dS )N)dtype)r   initial_valueshaper-   r+   )	functoolspartialtf_variablesVariable)r   r/   r-   initializerr+   r.   )variable_shaper#   r$   getterV   s   z2TPUEmbeddingBase._create_variables.<locals>.getterTc                    s   j | |tj |dS )N)r   r4   r/   r-   r6   r+   ) _add_variable_with_custom_getterr   float32)r   r4   r+   )r6   r   r5   r#   r$   variable_creatorc   s   z<TPUEmbeddingBase._create_variables.<locals>.variable_creator)r+   c                    s    j d |  |dS )N/F)r   )r   r4   )r   r9   r#   r$   slot_creatorr   s   z8TPUEmbeddingBase._create_variables.<locals>.slot_creatorN
parameters)T)vocabulary_sizedimr   r4   r   _create_slots)r   r   r+   r<   r;   	slot_varsr#   )r6   r   r   r9   r5   r$   _create_variablesQ   s   

z"TPUEmbeddingBase._create_variablesc                 C   r&   )8Create variables and slots variables for TPU embeddings.r'   r)   r#   r#   r$   _create_variables_and_slots|   s   z,TPUEmbeddingBase._create_variables_and_slotsc                 C   s   | j rdS |  | _d| _ dS )rB   NT)r   rC   
_variablesr)   r#   r#   r$   build   s   

zTPUEmbeddingBase.buildfeaturesweightsc                 C   s   | j s|   | ||S )z.Call the mid level api to do embedding lookup.)r   rE   embedding_lookupr   rF   rG   r#   r#   r$   __call__   s   zTPUEmbeddingBase.__call__c                 C   r&   )z4Lookup the embedding table using the input features.r'   rI   r#   r#   r$   rH      s   z!TPUEmbeddingBase.embedding_lookup)N)__name__
__module____qualname____doc__r   r
   FeatureConfigr   r   r   r%   propertyr*   TableConfigboolr   r   r2   r3   rA   rC   rE   r   rJ   rH   r#   r#   r#   r$   r      s4    	
*

+r   )rN   r0   typingr   r   r   r   r   r   tensorflow.python.frameworkr   tensorflow.python.opsr	   r2   tensorflow.python.tpur
   tensorflow.python.trackabler   tensorflow.python.utilr   AutoTrackabler   r#   r#   r#   r$   <module>   s    