
    AVhm             
          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 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*dg       e(dd      d_d              Z+ e*d d!g      d_d"       Z,d# Z- G d$ d%ej\                        Z/ G d& d'e/      Z0 G d( d)e0      Z1 ejd                  d* +       e*d,g        G d- d.e0                    Z3 e*d,g       G d/ d0e3             Z4 G d1 d2e4      Z5 G d3 d4ejl                        Z7 e*d5       G d6 d7e7             Z8 e*d8       G d9 d:             Z9 e*d;       G d< d=e7             Z: G d> d?e:      Z; G d@ dAe:      Z< G dB dC ejz                  dCdDdEg            Z> e>dFd      Z? G dG dHe>      Z@dI ZA G dJ dKe/      ZB e*dLg        G dM dNe/             ZC e*dLg       G dO dPeC             ZDddddQe?e
j                  de9j                  e9j                  dRf
dSZHddQe?e
j                  dfdTZIddUde9j                  e9j                  dRfdVZJ	 	 d`dWZK e*dX       ej                  dY       G dZ d[e/                    ZM e*d\       ej                  dY       G d] d^e/                    ZNy)azLookup operations.    N)saveable_compat)context)constant_op)dtypes)ops)sparse_tensortensor)tensor_shape)tensor_util)	array_ops)control_flow_ops)gen_lookup_ops)lookup_grad)math_ops)
string_ops)*)registration)asset)base)resource)BaseSaverBuilder)internal)compat)
deprecated)	tf_exportinitialize_all_tables)v1z$Use `tf.tables_initializer` instead.c                     t        |       S )zReturns an Op that initializes all tables of the default graph.

  Args:
    name: Optional name for the initialization op.

  Returns:
    An Op that initializes all tables.  Note that if there are
    not tables the returned Op is a NoOp.
  )tables_initializernames    P/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/lookup_ops.pyr   r   4   s     
D	!!    zinitializers.tables_initializerr    c                     t        j                  t         j                  j                        }|rt	        j
                  |d| iS t	        j                  |       S )a~  Returns an Op that initializes all tables of the default graph.

  Args:
    name: Optional name for the initialization op.

  Returns:
    An Op that initializes all tables.  Note that if there are
    not tables the returned Op is a NoOp.

  @compatibility(TF2)
  `tf.compat.v1.tables_initializer` is no longer needed with eager execution and
  `tf.function`. In TF2, when creating an initializable table like a
  `tf.lookup.StaticHashTable`, the table will automatically be initialized on
  creation.

  #### Before & After Usage Example

  Before:

  >>> with tf.compat.v1.Session():
  ...   init = tf.compat.v1.lookup.KeyValueTensorInitializer(['a', 'b'], [1, 2])
  ...   table = tf.compat.v1.lookup.StaticHashTable(init, default_value=-1)
  ...   tf.compat.v1.tables_initializer().run()
  ...   result = table.lookup(tf.constant(['a', 'c'])).eval()
  >>> result
  array([ 1, -1], dtype=int32)

  After:

  >>> init = tf.lookup.KeyValueTensorInitializer(['a', 'b'], [1, 2])
  >>> table = tf.lookup.StaticHashTable(init, default_value=-1)
  >>> table.lookup(tf.constant(['a', 'c'])).numpy()
  array([ 1, -1], dtype=int32)

  @end_compatibility
  r"   r!   )r   get_collection	GraphKeysTABLE_INITIALIZERSr   groupno_op)r"   initializerss     r#   r    r    C   sI    L ##CMM$D$DE,!!<;d;;			T	**r$   c                     |j                   | j                  k7  rt        d| j                   d| d      |j                   | j                  k7  rt        d| j                   d| d      y)aD  Check that the given key_dtype and value_dtype matches the table dtypes.

  Args:
    table: The table to check types against to.
    key_dtype: The key data type to check.
    value_dtype: The value data type to check.

  Raises:
    TypeError: when 'key_dtype' or 'value_dtype' doesn't match the table data
      types.
  z&Invalid key dtype for table, expected 	 but got .z(Invalid value dtype for table, expected N)
base_dtype	key_dtype	TypeErrorvalue_dtype)tabler0   r2   s      r#   check_table_dtypesr4   o   s     U__,
<U__<M N(k, - -u000
>(();-qB C C 1r$   c                   n     e Zd ZdZ fdZd Zed        Zed        Zed        Z	d
dZ
d
dZd	 Z xZS )LookupInterfacez>Represent a lookup table that persists across different steps.c                     t        j                  |      | _        t        j                  |      | _        t        t
        |           y)zConstruct a lookup table interface.

    Args:
      key_dtype: The table key type.
      value_dtype: The table value type.
    N)r   as_dtype
_key_dtype_value_dtypesuperr6   __init__)selfr0   r2   	__class__s      r#   r<   zLookupInterface.__init__   s3     ooi0DO4D	/4)+r$   c                     t         NNotImplementedErrorr=   s    r#   _create_resourcez LookupInterface._create_resource   s    
r$   c                     | j                   S )zThe table key dtype.r9   rC   s    r#   r0   zLookupInterface.key_dtype        ??r$   c                     | j                   S )zThe table value dtype.r:   rC   s    r#   r2   zLookupInterface.value_dtype        r$   c                     t         S )zThe name of the table.rA   rC   s    r#   r"   zLookupInterface.name   s
     r$   c                     t         )-Compute the number of elements in this table.rA   r=   r"   s     r#   sizezLookupInterface.size       
r$   c                     t         z=Looks up `keys` in a table, outputs the corresponding values.rA   r=   keysr"   s      r#   lookupzLookupInterface.lookup   rP   r$   c                 $    | j                  |      S rR   )rU   )r=   rT   s     r#   __getitem__zLookupInterface.__getitem__   s    ;;tr$   r@   )__name__
__module____qualname____doc__r<   rD   propertyr0   r2   r"   rO   rU   rW   __classcell__r>   s   @r#   r6   r6      s[    F	,      r$   r6   c                   H     e Zd ZdZ fdZd Zed        ZddZddZ	 xZ
S )InitializableLookupTableBasezjInitializable lookup table interface.

  An initializable lookup tables persist across different steps.
  c                    t         t        |   |j                  |j                         t        j                  || j                        | _        | j                  j                         j                  t        j                  g              t        |t        j                        r| j!                  |d      | _        t        j$                         5  | j'                         | _        ddd       t+        j,                         sUt        j.                         j1                         3t        j$                         5  | j3                         | _        ddd       y| j3                         | _        y# 1 sw Y   xY w# 1 sw Y   yxY w)a  Construct a table object from a table reference.

    If requires a table initializer object (subclass of `TableInitializerBase`).
    It provides the table key and value types, as well as the op to initialize
    the table. The caller is responsible to execute the initialization op.

    Args:
      default_value: The value to use if a key is missing in the table.
      initializer: The table initializer to use.
    dtype_initializerN)r;   r`   r<   r0   r2   r   convert_to_tensorr:   _default_value	get_shape
merge_withr   TensorShape
isinstancetrackable_base	Trackable_track_trackablerd   
init_scoperD   _resource_handler   executing_eagerlyget_default_graph_get_control_flow_context_initialize_init_op)r=   default_valueinitializerr>   s      r#   r<   z%InitializableLookupTableBase.__init__   s,    

&6{7L7L7B7N7NP//T..0D!!#..|/G/G/KL+~778//^Ld		 6"335d6%%'99;G>> +((*+ + &&(dm6 6+ +s   E.9E:.E7:Fc                 8    | j                   j                  |       S r@   )rd   
initializerC   s    r#   rs   z(InitializableLookupTableBase._initialize   s    ''--r$   c                     | j                   S )zThe default value of the table.)rf   rC   s    r#   ru   z*InitializableLookupTableBase.default_value   s     r$   c                     t        j                  |d| j                  z  | j                  g      5  t	        j
                  | j                        cddd       S # 1 sw Y   yxY wzCompute the number of elements in this table.

    Args:
      name: A name for the operation (optional).

    Returns:
      A scalar tensor containing the number of elements in this table.
    %s_SizeN)r   
name_scoper"   resource_handler   lookup_table_size_v2rN   s     r#   rO   z!InitializableLookupTableBase.size   sS     
i$))3d6J6J5K	L G001E1EFG G Gs   AA"c                    |}t        |t        j                  t        j                  f      r|j
                  }|j                  j                  | j                  k7  r%t        d| j                   d|j                         t        j                  |d| j                  z  | j                  || j                  f      5  t        j                   | j                  || j                        }ddd       j#                  |j%                                t        |t        j                        r+t        j                  |j&                  ||j(                        S t        |t        j                        r|j+                  |      S |S # 1 sw Y   xY w)a  Looks up `keys` in a table, outputs the corresponding values.

    The `default_value` is used for keys not present in the table.

    Args:
      keys: Keys to look up. May be either a `SparseTensor` or dense `Tensor`.
      name: A name for the operation (optional).

    Returns:
      A `SparseTensor` if keys are sparse, a `RaggedTensor` if keys are ragged,
      otherwise a dense `Tensor`.

    Raises:
      TypeError: when `keys` or `default_value` doesn't match the table data
        types.
    !Dtype of argument `keys` must be , received: 	%s_LookupN)rj   r   SparseTensorr   RaggedTensorvaluesrc   r/   r9   r1   r   r}   r"   r~   rf   r   lookup_table_find_v2	set_shaperg   indicesdense_shapewith_values)r=   rT   r"   
key_tensorr   s        r#   rU   z#InitializableLookupTableBase.lookup   sU   " J $33X5J5JKL;;jzz/9$//9J K##'::,0 1 1 
kDII%			z4+>+>?
A H 2243G3G3=373F3FHfH Z))+,$223''fd>N>NOO 
D(//	0f%%mH Hs   <,FF
r@   )rX   rY   rZ   r[   r<   rs   r\   ru   rO   rU   r]   r^   s   @r#   r`   r`      s1    
)6.  
G*r$   r`   c                       e Zd Zed        Zy)InitializableLookupTableBaseV1c                     | j                   S r@   rt   rC   s    r#   rv   z*InitializableLookupTableBaseV1.initializer      ==r$   NrX   rY   rZ   r\   rv    r$   r#   r   r     s     r$   r   c                 "    t        | t              S r@   )rj   StaticHashTable)objs    r#   <lambda>r     s    *S/: r$   )	predicatezlookup.StaticHashTablec                   b     e Zd ZdZ	 	 d	 fd	Zd Zed        Zd
dZd Z	d Z
ed        Z xZS )r   a  A generic hash table that is immutable once initialized.

  Example usage:

  >>> keys_tensor = tf.constant(['a', 'b', 'c'])
  >>> vals_tensor = tf.constant([7, 8, 9])
  >>> input_tensor = tf.constant(['a', 'f'])
  >>> table = tf.lookup.StaticHashTable(
  ...     tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor),
  ...     default_value=-1)
  >>> table.lookup(input_tensor).numpy()
  array([ 7, -1], dtype=int32)

  Or for more pythonic code:

  >>> table[input_tensor].numpy()
  array([ 7, -1], dtype=int32)

  The result of a lookup operation has the same shape as the argument:

  >>> input_tensor = tf.constant([['a', 'b'], ['c', 'd']])
  >>> table[input_tensor].numpy()
  array([[ 7,  8],
         [ 9, -1]], dtype=int32)


  c                 j   || _         || _        || _        | j                  sL| j                   j                  | _        | j                  s%dt	        t        j                               | _        |xs d| _        d| _        t        t        | /  ||       | j                  j                         | _        y)a  Creates a non-initialized `HashTable` object.

    Creates a table, the type of its keys and values are specified by the
    initializer.
    Before using the table you will have to initialize it. After initialization
    the table will be immutable.

    Args:
      initializer: The table initializer to use. See `HashTable` kernel for
        supported key and value types.
      default_value: The value to use if a key is missing in the table.
      name: A name for the operation (optional).
      experimental_is_anonymous: Whether to use anonymous mode for the
        table (default is False). In anonymous mode, the table
        resource can only be accessed via a resource handle. It can't
        be looked up by a name. When all resource handles pointing to
        that resource are gone, the resource will be deleted
        automatically.

    Returns:
      A `HashTable` object.
    hash_table_
hash_tableN)rd   rf   _is_anonymous_shared_namestruuiduuid4_name_table_namer;   r   r<   rg   _value_shape)r=   rv   ru   r"   experimental_is_anonymousr>   s        r#   r<   zStaticHashTable.__init__9  s    6 $D'D2D++88d
 034::</@B%DJD	/4)-E++557Dr$   c                    | j                   rKt        j                  | j                  j                  | j                  j
                  | j                        }nUt        j                  | j                  | j                  j                  | j                  j
                  | j                        }t        j                         r	d | _        |S |j                  j                  j                  d      d   | _        |S )Nr0   r2   r"   )shared_namer0   r2   r"   /)r   r   anonymous_hash_tablerd   r0   r2   r   hash_table_v2r   r   rp   r   opr"   split)r=   	table_refs     r#   rD   z StaticHashTable._create_resourced  s     55%%//''33zzi
 !..''%%//''33zz	i
   "d  #**005b9dr$   c                     | j                   S r@   r   rC   s    r#   r"   zStaticHashTable.namev      r$   c                 r   t        j                  |d| j                  z  | j                  g      5  t	        j
                  | j                  | j                  | j                        \  }}ddd       j                  j                         j                  | j                               ||fS # 1 sw Y   ExY w)  Returns tensors of all keys and values in the table.

    Args:
      name: A name for the operation (optional).

    Returns:
      A pair of tensors with the first tensor containing all keys and the
        second tensors containing all values in the table.
    z	%s_ExportN)r   r}   r"   r~   r   lookup_table_export_v2r9   r:   r   rg   concatenater   r=   r"   exported_keysexported_valuess       r#   exportzStaticHashTable.exportz  s     
kDII58L8L7M	N D'5'L'L


1B1B(D$m_D m557CC /))D Ds   9B--B6c                      y r@   r   )r=   unused_kwargss     r#   _serialize_to_protoz#StaticHashTable._serialize_to_proto  s    r$   c                 x    t        | ||       t        |t        j                        r| j	                  ||       y y r@   )setattrrj   rk   rl   rm   )r=   r"   values      r#   _add_trackable_childz$StaticHashTable._add_trackable_child  s3    D$%112
E4( 3r$   c                 X     G d dt         j                        } |j                  di |S )Nc                       e Zd Zed        Zy)IStaticHashTable._deserialize_from_proto.<locals>._RestoredStaticHashTablec                      y)NRestoredStaticHashTabler   )clss    r#   _resource_typezXStaticHashTable._deserialize_from_proto.<locals>._RestoredStaticHashTable._resource_type  s    (r$   N)rX   rY   rZ   classmethodr   r   r$   r#   _RestoredStaticHashTabler     s    ) )r$   r   r   )r   RestoredResource_deserialize_from_proto)r   kwargsr   s      r#   r   z'StaticHashTable._deserialize_from_proto  s-    )8#<#< ) <#;;EfEEr$   )NFr@   )rX   rY   rZ   r[   r<   rD   r\   r"   r   r   r   r   r   r]   r^   s   @r#   r   r     sT    > ).	)8V$  *$)
 F Fr$   r   c                        e Zd ZdZed        Zy)StaticHashTableV1a  A generic hash table that is immutable once initialized.

  When running in graph mode, you must evaluate the tensor returned by
  `tf.tables_initializer()` before evaluating the tensor returned by
  this class's `lookup()` method. Example usage in graph mode:

  ```python
  keys_tensor = tf.constant([1, 2])
  vals_tensor = tf.constant([3, 4])
  input_tensor = tf.constant([1, 5])
  table = tf.lookup.StaticHashTable(
      tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor), -1)
  out = table.lookup(input_tensor)
  with tf.Session() as sess:
      sess.run(tf.tables_initializer())
      print(sess.run(out))
  ```

  Note that in graph mode if you set `experimental_is_anonymous` to
  `True`, you should only call `Session.run` once, otherwise each
  `Session.run` will create (and destroy) a new table unrelated to
  each other, leading to errors such as "Table not initialized".
  You can do so like this:

  ```python
  keys_tensor = tf.constant([1, 2])
  vals_tensor = tf.constant([3, 4])
  input_tensor = tf.constant([1, 5])
  table = tf.lookup.StaticHashTable(
      tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor), -1,
      experimental_is_anonymous=True)
  with tf.control_dependencies([tf.tables_initializer()]):
    out = table.lookup(input_tensor)
  with tf.Session() as sess:
    print(sess.run(out))
  ```

  In eager mode, no special code is needed to initialize the table.
  Example usage in eager mode:

  ```python
  tf.enable_eager_execution()
  keys_tensor = tf.constant([1, 2])
  vals_tensor = tf.constant([3, 4])
  input_tensor = tf.constant([1, 5])
  table = tf.lookup.StaticHashTable(
      tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor), -1)
  print(table.lookup(input_tensor))
  ```
  c                     | j                   S r@   r   rC   s    r#   rv   zStaticHashTableV1.initializer  r   r$   N)rX   rY   rZ   r[   r\   rv   r   r$   r#   r   r     s    1f  r$   r   c                       e Zd Zed        Zy)	HashTablec                     | j                   S r@   rv   rC   s    r#   initzHashTable.init  r   r$   N)rX   rY   rZ   r\   r   r   r$   r#   r   r     s     r$   r   c                   L    e Zd ZdZd Zed        Zed        Zd Zed        Z	y)TableInitializerBasez)Base class for lookup table initializers.c                 l    t        j                  |      | _        t        j                  |      | _        y)zConstruct a table initializer object.

    Args:
      key_dtype: Type of the table keys.
      value_dtype: Type of the table values.
    N)r   r8   r9   r:   )r=   r0   r2   s      r#   r<   zTableInitializerBase.__init__  s%     ooi0DO4Dr$   c                     | j                   S )zThe expected table key dtype.rF   rC   s    r#   r0   zTableInitializerBase.key_dtype  rG   r$   c                     | j                   S )zThe expected table value dtype.rI   rC   s    r#   r2   z TableInitializerBase.value_dtype  rJ   r$   c                     t         )z$Returns the table initialization op.rA   )r=   r3   s     r#   rx   zTableInitializerBase.initialize  rP   r$   c                 r    d}t        j                         r |t        t        j                               z  }|S )z.Returns a shared name to be used by the table. )r   rp   r   r   uidr=   r   s     r#   r   z!TableInitializerBase._shared_name  s1     K  " S^#kr$   N)
rX   rY   rZ   r[   r<   r\   r0   r2   rx   r   r   r$   r#   r   r     sK    15      r$   r   z lookup.KeyValueTensorInitializerc                   *     e Zd ZdZd fd	Zd Z xZS )KeyValueTensorInitializera  Table initializers given `keys` and `values` tensors.

  >>> keys_tensor = tf.constant(['a', 'b', 'c'])
  >>> vals_tensor = tf.constant([7, 8, 9])
  >>> input_tensor = tf.constant(['a', 'f'])
  >>> init = tf.lookup.KeyValueTensorInitializer(keys_tensor, vals_tensor)
  >>> table = tf.lookup.StaticHashTable(
  ...     init,
  ...     default_value=-1)
  >>> table.lookup(input_tensor).numpy()
  array([ 7, -1], dtype=int32)

  c                    t        j                         szt        j                         j	                         Xt        j
                         5  t        j                  ||d      | _        t        j                  ||d      | _        ddd       n:t        j                  ||d      | _        t        j                  ||d      | _        ||nd| _	        t        j                         r0| xj                  t        t        j                               z  c_	        t        t        | ;  | j                  j                  | j                  j                         y# 1 sw Y   xY w)ar  Constructs a table initializer object based on keys and values tensors.

    Args:
      keys: The tensor for the keys.
      values: The tensor for the values.
      key_dtype: The `keys` data type. Used when `keys` is a python array.
      value_dtype: The `values` data type. Used when `values` is a python array.
      name: A name for the operation (optional).
    NrT   rc   r"   r   key_value_init)r   rp   r   rq   rr   rn   re   _keys_valuesr   r   r   r;   r   r<   rc   )r=   rT   r   r0   r2   r"   r>   s         r#   r<   z"KeyValueTensorInitializer.__init__  s    %%'99;G>> 6**4yvN
,,+H66 6
 ((YVLdj**
(4dl)/?DJ  " jjC	N"j	
#T3DJJ4D4D48LL4F4FH6 6s   ;EEc                    t        || j                  j                  | j                  j                         t	        j
                  | j                  |j                  | j                  | j                  f      5  t        j                  |j                  | j                  | j                        }ddd       t	        j                  t        j                  j                         |S # 1 sw Y   9xY w)a,  Initializes the given `table` with `keys` and `values` tensors.

    Args:
      table: The table to initialize.

    Returns:
      The operation that initializes the table.

    Raises:
      TypeError: when the keys and values data types do not match the table
      key and value data types.
    )r   N)r4   r   rc   r   r   r}   r   r~   r   lookup_table_import_v2add_to_collectionr'   r(   )r=   r3   init_ops      r#   rx   z$KeyValueTensorInitializer.initialize7  s     udjj..0B0BC	

E114::t||L
N P55e6K6K6:jj$,,PgP #--::GDNP Ps   86C&&C/)NNN)rX   rY   rZ   r[   r<   rx   r]   r^   s   @r#   r   r   	  s    H<r$   r   zlookup.TextFileIndexc                       e Zd ZdZdZdZy)TextFileIndexa"  The key and value content to get from each line.

  This class defines the key and value used for `tf.lookup.TextFileInitializer`.

  The key and value content to get from each line is specified either
  by the following, or a value `>=0`.
  * `TextFileIndex.LINE_NUMBER` means use the line number starting from zero,
    expects data type int64.
  * `TextFileIndex.WHOLE_LINE` means use the whole line content, expects data
    type string.

  A value `>=0` means use the index (starting at zero) of the split line based
      on `delimiter`.
  r   N)rX   rY   rZ   r[   
WHOLE_LINELINE_NUMBERr   r$   r#   r   r   M  s     *+r$   r   zlookup.TextFileInitializerc                   B     e Zd ZdZ	 	 	 	 d fd	Zd Zed        Z xZS )TextFileInitializera;  Table initializers from a text file.

  This initializer assigns one entry in the table for each line in the file.

  The key and value type of the table to initialize is given by `key_dtype` and
  `value_dtype`.

  The key and value content to get from each line is specified by
  the `key_index` and `value_index`.

  * `TextFileIndex.LINE_NUMBER` means use the line number starting from zero,
    expects data type int64.
  * `TextFileIndex.WHOLE_LINE` means use the whole line content, expects data
    type string.
  * A value `>=0` means use the index (starting at zero) of the split line based
      on `delimiter`.

  For example if we have a file with the following content:

  >>> import tempfile
  >>> f = tempfile.NamedTemporaryFile(delete=False)
  >>> content='\n'.join(["emerson 10", "lake 20", "palmer 30",])
  >>> f.file.write(content.encode('utf-8'))
  >>> f.file.close()

  The following snippet initializes a table with the first column as keys and
  second column as values:

  * `emerson -> 10`
  * `lake -> 20`
  * `palmer -> 30`

  >>> init= tf.lookup.TextFileInitializer(
  ...    filename=f.name,
  ...    key_dtype=tf.string, key_index=0,
  ...    value_dtype=tf.int64, value_index=1,
  ...    delimiter=" ")
  >>> table = tf.lookup.StaticHashTable(init, default_value=-1)
  >>> table.lookup(tf.constant(['palmer','lake','tarkus'])).numpy()

  Similarly to initialize the whole line as keys and the line number as values.

  * `emerson 10 -> 0`
  * `lake 20 -> 1`
  * `palmer 30 -> 2`

  >>> init = tf.lookup.TextFileInitializer(
  ...   filename=f.name,
  ...   key_dtype=tf.string, key_index=tf.lookup.TextFileIndex.WHOLE_LINE,
  ...   value_dtype=tf.int64, value_index=tf.lookup.TextFileIndex.LINE_NUMBER)
  >>> table = tf.lookup.StaticHashTable(init, -1)
  >>> print(table.lookup(tf.constant('palmer 30')).numpy())
  2
  c
                 V   t        |t        j                        s|st        d      || _        t        j                  |      }t        j                  |      }|dk  rt        d| d      |t        j                  k(  r2|t
        j                  k7  rt        dt        j                   d|       |t        j                  k(  r>|j                  s2|t
        j                  k7  rt        dt        j                   d|       |dk  rt        d|       |t        j                  k(  r2|t
        j                  k7  rt        d	t        j                   d|       |t        j                  k(  r>|j                  s2|t
        j                  k7  rt        d
t        j                   d|       ||dk  rt        d|       || _        || _        || _        || _        || _        | j%                  t'        j(                  |      d      | _        |	| _        t.        t0        | g  ||       y)a  Constructs a table initializer object to populate from a text file.

    It generates one key-value pair per line. The type of table key and
    value are specified by `key_dtype` and `value_dtype`, respectively.
    Similarly the content of the key and value are specified by the key_index
    and value_index.

    - TextFileIndex.LINE_NUMBER means use the line number starting from zero,
      expects data type int64.
    - TextFileIndex.WHOLE_LINE means use the whole line content, expects data
      type string or int64.
    - A value >=0 means use the index (starting at zero) of the split line based
      on `delimiter`.

    Args:
      filename: The filename of the text file to be used for initialization. The
        path must be accessible from wherever the graph is initialized (eg.
        trainer or eval workers). The filename may be a scalar `Tensor`.
      key_dtype: The `key` data type.
      key_index: the index that represents information of a line to get the
        table 'key' values from.
      value_dtype: The `value` data type.
      value_index: the index that represents information of a line to get the
        table 'value' values from.'
      vocab_size: The number of elements in the file, if known.
      delimiter: The delimiter to separate fields in a line.
      name: A name for the operation (optional).
      value_index_offset: A number to add to all indices extracted from the file
        This is useful for cases where a user would like to reserve one or more
        low index values for control characters. For instance, if you would
        like to ensure that no vocabulary item is mapped to index 0 (so you can
        reserve 0 for a masking value), you can set value_index_offset to 1;
        this will mean that the first vocabulary element is mapped to 1
        instead of 0.

    Raises:
      ValueError: when the filename is empty, or when the table key and value
      data types do not match the expected data types.
    z>`filename` argument required for tf.lookup.TextFileInitializerr   z'`key_index` should be >= -2, received: r.   z,`key_dtype` must be int64 if `key_index` is r   z?`key_dtype` should be either integer or string for `key_index` z)`value_index` should be >= -2, received: z.`value_dtype` must be int64 for `value_index` zC`value_dtype` should be either integer or string for `value_index` Nr   z&`vocab_size` should be > 0, received: 	_filename)rj   
tensor_libTensor
ValueError_filename_argr   r8   r   r   int64r   
is_integerstring
_key_index_value_index_vocab_size
_delimiterr   rm   r   Assetr   _offsetr;   r   r<   )r=   filenamer0   	key_indexr2   value_index
vocab_size	delimiterr"   value_index_offsetr>   s             r#   r<   zTextFileInitializer.__init__  s&   b h
 1 128WXX!D	*I//+.K2~@1MNNM---)v||2KE'334LM N N	m..	.!!	V]](B
K%%&l9+?@ @ RB%( ) ) m///K6<<4OG'334LO P P	00	0##+*F
O%%&l;-AB B 	Z1_?
|LMMDO#D!DDODJ**H{,DN%DL	
t-iEr$   c           
         t        || j                  | j                         t        j                  | j
                  d|j                  f      5  t        j                  | j                  t        j                  d      }t        j                  |j                  || j                  | j                  | j                  dn| j                  | j                   | j"                        }ddd       t        j$                  t        j&                  j(                         t+        j,                         sCt/        j0                        r.t        j$                  t        j&                  j2                  |       |S # 1 sw Y   xY w)a  Initializes the table from a text file.

    Args:
      table: The table to be initialized.

    Returns:
      The operation that initializes the table.

    Raises:
      TypeError: when the keys and values data types do not match the table
      key and value data types.
    text_file_initasset_filepathr!   Nr   )r4   r0   r2   r   r}   r   r~   re   r   r   r   r   "initialize_table_from_text_file_v2r   r   r   r   r   r   r'   r(   r   rp   r   is_constantASSET_FILEPATHS)r=   r3   r  r   s       r#   rx   zTextFileInitializer.initialize  s    udnnd.>.>?	

$4u7L7L6N	O &&
..&--.>@hAA


4??D<M<M  ("d.>.>
,,g #--::GD $$&;+B+B8+L	CMM998DN s   BE33E<c                    | j                   r}| j                  r>d| j                  | j                   | j                  | j                  | j                  fz  }|S d| j                  | j                   | j                  | j                  fz  }|S | j                  r8d| j                  d| j                  d| j                  d| j                  }|S d| j                  d| j                  d| j                  }|S )Nzhash_table_%s_%d_%s_%s_%szhash_table_%s_%d_%s_%sr   _)r   r   r   r   r   r   s     r#   r   z TextFileInitializer._shared_name  s     
1 0 0$//t||5- -$  / 0 0$//2   
1B1BLL  
 1B1BD r$   )N	Nr   )	rX   rY   rZ   r[   r<   rx   r\   r   r]   r^   s   @r#   r   r   a  s8    5z "#\F|:  r$   r   c                   V     e Zd ZdZej
                  ej                  dddf fd	Z xZS )TextFileStringTableInitializerzDTable initializer for `int64` IDs to string tables from a text file.Nr  text_file_string_table_initc           
      t    t         t        |   |t        j                  |t        j
                  ||||       y)a  Constructs an initializer for an id-to-string table from a text file.

    It populates a table that its key and value types are int64 and string,
    respectively. It generates one key-value pair per line.
    The content of the key and value are specified by `key_column_index`
    and `value_column_index`.

    - TextFileIndex.LINE_NUMBER means use the line number starting from zero,
      expects data type int64.
    - TextFileIndex.WHOLE_LINE means use the whole line content, expects data
      type string or int64.
    - A value >=0 means use the index (starting at zero) of the split line based
      on `delimiter`.

    Args:
      filename: The filename of the text file to be used for initialization. The
        path must be accessible from wherever the graph is initialized (eg.
        trainer or eval workers). The filename may be a scalar `Tensor`.
      key_column_index: The column index from the text file to get the keys
        from. The default is to use the line number, starting from zero.
      value_column_index: The column index from the text file to get the values
        from. The default is to use the whole line content.
      vocab_size: The number of elements in the file, if known.
      delimiter: The delimiter to separate fields in a line.
      name: Optional name for the op.

    Raises:
      TypeError: when the filename is empty, or when the table key and value
      data types do not match the expected data types.
    r  r  r"   N)r;   r  r<   r   r   r   )r=   r  key_column_indexvalue_column_indexr  r  r"   r>   s          r#   r<   z'TextFileStringTableInitializer.__init__3  s;    J 

($8 9 r$   )	rX   rY   rZ   r[   r   r   r   r<   r]   r^   s   @r#   r  r  0  s-    L !. 9 9"/":":1- -r$   r  c                   l     e Zd ZdZej
                  ej                  dddej                  f fd	Z	 xZ
S )TextFileIdTableInitializerzDTable initializer for string to `int64` IDs tables from a text file.Nr  text_file_id_table_initc           
      X    t         t        |   |||t        j                  ||||       y)a  Constructs an initializer for an string-to-id table from a text file.

    It populates a table that its key and value types are string and int64,
    respectively. It generates one key-value pair per line.
    The content of the key and value are specified by the key_index
    and value_index.

    - TextFileIndex.LINE_NUMBER means use the line number starting from zero,
      expects data type int64.
    - TextFileIndex.WHOLE_LINE means use the whole line content, expects data
      type string.
    - A value >=0 means use the index (starting at zero) of the split line based
      on `delimiter`.

    Args:
      filename: The filename of the text file to be used for initialization. The
        path must be accessible from wherever the graph is initialized (eg.
        trainer or eval workers). The filename may be a scalar `Tensor`.
      key_column_index: The column index from the text file to get the `key`
        values from. The default is to use the whole line content.
      value_column_index: The column index from the text file to get the `value`
        values from. The default is to use the line number, starting from zero.
      vocab_size: The number of elements in the file, if known.
      delimiter: The delimiter to separate fields in a line.
      name: Optional name for the op.
      key_dtype: The `key` data type.

    Raises:
      TypeError: when the filename is empty, or when the table key and value
      data types do not match the expected data types.
    r  N)r;   r  r<   r   r   )	r=   r  r  r  r  r  r"   r0   r>   s	           r#   r<   z#TextFileIdTableInitializer.__init__f  s7    N 

$d4 5 r$   )rX   rY   rZ   r[   r   r   r   r   r   r<   r]   r^   s   @r#   r  r  c  s4    L !. 8 8"/";";-/ /r$   r  c                       e Zd ZdZdZy)
HasherSpeca  A structure for the spec of the hashing function to use for hash buckets.

  `hasher` is the name of the hashing function to use (eg. "fasthash",
  "stronghash").
  `key` is optional and specify the key to use for the hash function if
  supported, currently only used by a strong hash.

  Fields:
    hasher: The hasher name to use.
    key: The key to be used by the hashing function, if required.
  r   N)rX   rY   rZ   r[   	__slots__r   r$   r#   r  r    s    
 )r$   r  hasherkeyfasthashc                   &     e Zd ZdZdZ fdZ xZS )StrongHashSpeca7  A structure to specify a key of the strong keyed hash spec.

  The strong hash requires a `key`, which is a list of 2 unsigned integer
  numbers. These should be non-zero; random numbers generated from random.org
  would be a fine choice.

  Fields:
    key: The key to be used by the keyed hashing function.
  r   c                    t        |      dk7  rt        dt        |             t        |d   t        j                        rt        |d   t        j                        st        d|z        t        | t        #  | d|      S )N   z!`key` must have size 2, received r      z0Invalid key %s. Must be unsigned integer values.
stronghash)	lenr   rj   compat_utilintegral_typesr1   r;   r"  __new__)r   r  r>   s     r#   r*  zStrongHashSpec.__new__  sw    
3x1}:3s8*EFFc!fk889A**B,H3NOOn-c<EEr$   )rX   rY   rZ   r[   r  r*  r]   r^   s   @r#   r"  r"    s     )F Fr$   r"  c                 ~    t         j                  | j                  j                  k(  r| S t	        j
                  |       S r@   )r   r   rc   r/   r   	as_stringr	   s    r#   
_as_stringr-    s.    ]]fll---M			f	%%r$   c                        e Zd ZdZeddf fd	Zd Zd Zed        Z	e e
dd      d	               Zed
        Zed        ZddZd ZddZ xZS )IdTableWithHashBucketsa  String to Id table wrapper that assigns out-of-vocabulary keys to buckets.

  For example, if an instance of `IdTableWithHashBuckets` is initialized with a
  string-to-id table that maps:

  * `emerson -> 0`
  * `lake -> 1`
  * `palmer -> 2`

  The `IdTableWithHashBuckets` object will performs the following mapping:

  * `emerson -> 0`
  * `lake -> 1`
  * `palmer -> 2`
  * `<other term> -> bucket_id`, where bucket_id will be between `3` and
  `3 + num_oov_buckets - 1`, calculated by:
  `hash(<term>) % num_oov_buckets + vocab_size`

  If input_tensor is `["emerson", "lake", "palmer", "king", "crimson"]`,
  the lookup result is `[0, 1, 2, 4, 7]`.

  If `table` is None, only out-of-vocabulary buckets are used.

  Example usage:

  ```python
  num_oov_buckets = 3
  input_tensor = tf.constant(["emerson", "lake", "palmer", "king", "crimnson"])
  table = tf.IdTableWithHashBuckets(
      tf.StaticHashTable(
          tf.lookup.TextFileInitializer(
              filename,
              key_dtype=tf.string,
              key_index=tf.lookup.TextFileIndex.WHOLE_LINE,
              value_dtype=tf.int64,
              value_index=tf.lookup.TextFileIndex.LINE_NUMBER,
              delimiter="\t"),
          default_value),
      num_oov_buckets)
  out = table.lookup(input_tensor).
  table.init.run()
  print(out.eval())
  ```

  The hash function used for generating out-of-vocabulary buckets ID is handled
  by `hasher_spec`.
  Nc                    |r|j                  d      }|r||j                  }t        j                  t        j                  f}|j                  |vrt        d| d| d      |j                  j                  |j                  k7  r*t        d|j                  rdndd	|j                  d      |j                  t        j                  k7  rt        d
|j                  z        || _        |xs | j                  j                  }n1|dk  rt        d      |t        j                  n|}d| _        |xs d}|j                  s"t        j                  |k7  rt        d| d      || _        t        |t              st        dt        |       d      || _        |r|j!                  d      d   | _        nd| _        t$        t&        | S  |t        j                         y)a  Construct a `IdTableWithHashBuckets` object.

    Args:
      table: Table that maps `tf.string` or `tf.int64` keys to `tf.int64` ids.
      num_oov_buckets: Number of buckets to use for out-of-vocabulary keys.
      hasher_spec: A `HasherSpec` to specify the hash function to use for
        assignation of out-of-vocabulary buckets  (optional).
      name: A name for the operation (optional).
      key_dtype: Data type of keys passed to `lookup`. Defaults to
        `table.key_dtype` if `table` is specified, otherwise `tf.string`. Must
        be string or integer, and must be castable to `table.key_dtype`.

    Raises:
      ValueError: when `table` in None and `num_oov_buckets` is not positive.
      TypeError: when `hasher_spec` is invalid.
    r   N%Invalid `key_dtype`, expected one of z, received r.   zInvalid `key dtype`, expected integernon-integerr-   z1Invalid `value_dtype`: expected int64 but got %s.r   z4`oov_buckets` must be > 0 if no `table` is supplied.hash_bucket5Invalid `key_dtype`, expected integer or string, got .`hasher_spec` must be of type HasherSpec, got r   )rstripr0   r   r   r   r1   r   r2   _tabler"   r   _num_oov_bucketsrj   r  type_hasher_specr   r   r;   r/  r<   )r=   r3   num_oov_bucketshasher_specr"   r0   supported_table_key_dtypesr>   s          r#   r<   zIdTableWithHashBuckets.__init__  s   0 [[d		OO	$*LL&--#@ 	 :	:?56k)AO P 	P		#	#y';';	;&/&:&:M* + 	+ 
		fll	*K**, - 	-dk%T[[%%d	A	OPP#,#4&--)idk"]d  v}}	'AM"1& ' '+Dk:.Fk*+1. / /#DC,dd	
 $0FLLIr$   c                 P    | j                   | j                   j                         S y r@   r8  rD   rC   s    r#   rD   z'IdTableWithHashBuckets._create_resource7  "    {{[[))++r$   c                     | j                   | j                   j                         S t        j                  d d      5  t	        j
                         cd d d        S # 1 sw Y   y xY wNr   r8  rs   r   r}   r   r*   rC   s    r#   rs   z"IdTableWithHashBuckets._initialize<  N    {{[[$$&&	f	% &##%& & &   AA$c                     | j                   | j                   j                  S t        j                  d d      5  t	        j
                         cd d d        S # 1 sw Y   y xY wrC  r8  rt   r   r}   r   r*   rC   s    r#   rv   z"IdTableWithHashBuckets.initializerB  K    {{[[!!!	f	% &##%& & &   AA z
2018-12-15zUse `initializer` instead.c                     | j                   S r@   r   rC   s    r#   r   zIdTableWithHashBuckets.initI  s     r$   c                 H    | j                   | j                   j                  S y r@   r8  r~   rC   s    r#   r~   z&IdTableWithHashBuckets.resource_handleN      {{[[(((r$   c                     | j                   S r@   r   rC   s    r#   r"   zIdTableWithHashBuckets.nameT  r   r$   c                 *   t        j                  |d| j                  z        5  | j                  r| j                  j	                         }n%t        j
                  dt        j                        }|| j                  z   cddd       S # 1 sw Y   yxY wrM   r|   r   rb   N	r   r}   r"   r8  rO   re   r   r   r9  r=   r"   tsizes      r#   rO   zIdTableWithHashBuckets.sizeX  k    	i$))3	4 +	  "%%av||<T***+ + +   AB		Bc                 |   t        |t              st        dt        |       d      |j                  dk(  rt
        j                  S |j                  dk(  rt
        j                  S |j                  dk(  r/t        j                  t
        j                  |j                        S t        d|j                   d      )	zCReturns the string_to_hash_bucket op to use based on `hasher_spec`.r6  r.   r   legacyr&  )r  zFound unknown hasher z in `hasher_spec`)rj   r  r1   r:  r  r   string_to_hash_bucket_faststring_to_hash_bucket	functoolspartialstring_to_hash_bucket_strongr  r   )r=   r=  s     r#   _get_string_to_hash_bucket_fnz4IdTableWithHashBuckets._get_string_to_hash_bucket_fna  s    k:.Fk*+1. / /Z'222X%---\)

1
1{H H

 2 233DEG Gr$   c                    |j                   j                  | j                  k7  r%t        d| j                   d|j                          |}t	        |t
        j                  t        j                  f      r|j                  }| j                  rU| j                  j                  j                  t        j                  k(  r$t        j                  |t        j                        }| j                   dk(  r| j                  j#                  ||      }nt%        j&                  |d| j(                  z        5  | j+                  | j,                        } |t/        |      | j                   d      }| j                  r| j                  j#                  |      }t        j0                  || j                  j3                               }t        j4                  || j                  j6                        }t9        j:                  |||      }n|}ddd       t	        |t
        j                        r+t        j                  |j<                  |j>                        S t	        |t        j                        r|jA                        S S # 1 sw Y   {xY w)	  Looks up `keys` in the table, outputs the corresponding values.

    It assigns out-of-vocabulary keys to buckets based in their hashes.

    Args:
      keys: Keys to look up. May be either a `SparseTensor` or dense `Tensor`.
      name: Optional name for the op.

    Returns:
      A `SparseTensor` if keys are sparse, a `RaggedTensor` if keys are ragged,
      otherwise a dense `Tensor`.

    Raises:
      TypeError: when `keys` doesn't match the table key data type.
    r   r   r   r!   r   r4  num_bucketsr"   N)!rc   r/   r9   r1   rj   r   r   r   r   r   r8  r0   r   r   r   castr9  rU   r   r}   r"   r^  r;  r-  addrO   	not_equalru   r   where_v2r   r   r   )r=   rT   r"   r   idsstr_to_hash_bucketbucketsis_id_non_defaults           r#   rU   zIdTableWithHashBuckets.lookupp  s     zz/9$//9J K##'::,0 1 1F $33X5J5JKL{{f{{--88FLLH}}VV\\2f!KKvD1c >>$dii 78 !??$v--  ;;""6*#LL$++*:*:*<='&00dkk6O6OP
""#4c7C## $223''c4;K;KLL 
D(//	0c""J) s   2CI??Jr@   )rX   rY   rZ   r[   FastHashSpecr<   rD   rs   r\   rv   r   r   r~   r"   rO   r^  rU   r]   r^   s   @r#   r/  r/    s    .f (=J~
& & & l89 :   
  +G3r$   r/  zlookup.StaticVocabularyTablec                   f     e Zd ZdZ	 	 	 d	 fd	Zd Zd Zed        Zed        Z	d
dZ
d
dZ xZS )StaticVocabularyTablea?	  String to Id table that assigns out-of-vocabulary keys to hash buckets.

  For example, if an instance of `StaticVocabularyTable` is initialized with a
  string-to-id initializer that maps:

  >>> init = tf.lookup.KeyValueTensorInitializer(
  ...     keys=tf.constant(['emerson', 'lake', 'palmer']),
  ...     values=tf.constant([0, 1, 2], dtype=tf.int64))
  >>> table = tf.lookup.StaticVocabularyTable(
  ...    init,
  ...    num_oov_buckets=5)

  The `Vocabulary` object will performs the following mapping:

  * `emerson -> 0`
  * `lake -> 1`
  * `palmer -> 2`
  * `<other term> -> bucket_id`, where `bucket_id` will be between `3` and
  `3 + num_oov_buckets - 1 = 7`, calculated by:
  `hash(<term>) % num_oov_buckets + vocab_size`

  If input_tensor is:

  >>> input_tensor = tf.constant(["emerson", "lake", "palmer",
  ...                             "king", "crimson"])
  >>> table[input_tensor].numpy()
  array([0, 1, 2, 6, 7])

  If `initializer` is None, only out-of-vocabulary buckets are used.

  Example usage:

  >>> num_oov_buckets = 3
  >>> vocab = ["emerson", "lake", "palmer", "crimnson"]
  >>> import tempfile
  >>> f = tempfile.NamedTemporaryFile(delete=False)
  >>> f.write('\n'.join(vocab).encode('utf-8'))
  >>> f.close()

  >>> init = tf.lookup.TextFileInitializer(
  ...     f.name,
  ...     key_dtype=tf.string, key_index=tf.lookup.TextFileIndex.WHOLE_LINE,
  ...     value_dtype=tf.int64, value_index=tf.lookup.TextFileIndex.LINE_NUMBER)
  >>> table = tf.lookup.StaticVocabularyTable(init, num_oov_buckets)
  >>> table.lookup(tf.constant(["palmer", "crimnson" , "king",
  ...                           "tarkus", "black", "moon"])).numpy()
  array([2, 3, 5, 6, 6, 4])

  The hash function used for generating out-of-vocabulary buckets ID is
  Fingerprint64.

  Note that the out-of-vocabulary bucket IDs always range from the table `size`
  up to `size + num_oov_buckets - 1` regardless of the table values, which could
  cause unexpected collisions:

  >>> init = tf.lookup.KeyValueTensorInitializer(
  ...     keys=tf.constant(["emerson", "lake", "palmer"]),
  ...     values=tf.constant([1, 2, 3], dtype=tf.int64))
  >>> table = tf.lookup.StaticVocabularyTable(
  ...     init,
  ...     num_oov_buckets=1)
  >>> input_tensor = tf.constant(["emerson", "lake", "palmer", "king"])
  >>> table[input_tensor].numpy()
  array([1, 2, 3, 3])
  c                    |dk  rt        d      |r|j                  d      }|rK||j                  }t        j                  t        j
                  f}|j                  |vrt        d|d|j                  d      |j                  j                  |j                  k7  r*t        d|j                  rd	nd
d|j                  d      |j                  t        j                  k7  r*t        dt        j                  d|j                  d      t        |t        j                        r| j                  |d      | _        t        |d|      | _        |xs | j                  j                   }nt        j
                  }d| _        |xs d}|j                  s!t        j
                  |k7  rt        d|       || _        d| _        ||j'                  d      d   | _        t(        t*        | [  |t        j                         y)a  Construct a `StaticVocabularyTable` object.

    Args:
      initializer: A `TableInitializerBase` object that contains the data used
        to initialize the table. If None, then we only use out-of-vocab buckets.
      num_oov_buckets: Number of buckets to use for out-of-vocabulary keys. Must
        be greater than zero. If out-of-vocab buckets are not required, use
        `StaticHashTable` instead.
      lookup_key_dtype: Data type of keys passed to `lookup`. Defaults to
        `initializer.key_dtype` if `initializer` is specified, otherwise
        `tf.string`. Must be string or integer, and must be castable to
        `initializer.key_dtype`.
      name: A name for the operation (optional).
      experimental_is_anonymous: Whether to use anonymous mode for the
        table (default is False). In anonymous mode, the table
        resource can only be accessed via a resource handle. It can't
        be looked up by a name. When all resource handles pointing to
        that resource are gone, the resource will be deleted
        automatically.

    Raises:
      ValueError: when `num_oov_buckets` is not positive.
      TypeError: when lookup_key_dtype or initializer.key_dtype are not
        integer or string. Also when initializer.value_dtype != int64.
    r   z3`num_oov_buckets` must be > 0; use StaticHashTable.r   Nr1  z
, but got r.   zInvalid `key_dtype`, expected r2  r3  r-   z Invalid `value_dtype`, expected rd   r   )ru   r   r4  r5  )r   r7  r0   r   r   r   r1   r   r2   rj   rk   rl   rm   rd   r   r8  r"   r9  r   r   r;   rm  r<   )r=   rv   r<  lookup_key_dtyper"   r   r>  r>   s          r#   r<   zStaticVocabularyTable.__init__  s   > !LMM [[d		!&00$*LL&--#@ 			&@	@3[5J5JL M 	M				)	)-=-H-H	H*55Y=H""$% 	% 
	 	 FLL	0{'>'>@ A 	A	K!9!9	: 11+~N
$=?dk %T[[%%ddk"]d''fmm.>/?M)*, - -+DDC,d	
/0@&,,Or$   c                 P    | j                   | j                   j                         S y r@   r@  rC   s    r#   rD   z&StaticVocabularyTable._create_resource4  rA  r$   c                     | j                   | j                   j                         S t        j                  d d      5  t	        j
                         cd d d        S # 1 sw Y   y xY wrC  rD  rC   s    r#   rs   z!StaticVocabularyTable._initialize9  rE  rF  c                 H    | j                   | j                   j                  S y r@   rM  rC   s    r#   r~   z%StaticVocabularyTable.resource_handle?  rN  r$   c                     | j                   S r@   r   rC   s    r#   r"   zStaticVocabularyTable.nameE  r   r$   c                 *   t        j                  |d| j                  z        5  | j                  r| j                  j	                         }n%t        j
                  dt        j                        }|| j                  z   cddd       S # 1 sw Y   yxY wrQ  rR  rS  s      r#   rO   zStaticVocabularyTable.sizeI  rU  rV  c                    |j                   j                  | j                  k7  r%t        d| j                   d|j                          |}t	        |t
        j                  t        j                  f      r|j                  }| j                  rU| j                  j                  j                  t        j                  k(  r$t        j                  |t        j                        }t!        j"                  |d| j$                  z        5  t'        j(                  t+        |      | j,                  d      }| j                  r| j                  j/                  |      }t        j0                  || j                  j3                               }t        j4                  || j                  j6                        }t9        j:                  |||      }n|}ddd       t	        |t
        j                        r+t        j                  |j<                  |j>                        S t	        |t        j                        r|jA                        S S # 1 sw Y   {xY w)r`  r   r   r   r4  ra  N)!rc   r/   r9   r1   rj   r   r   r   r   r   r8  r0   r   r   r   rc  r   r}   r"   r   rY  r-  r9  rU   rd  rO   re  ru   r   rf  r   r   r   )r=   rT   r"   r   ri  rg  rj  s          r#   rU   zStaticVocabularyTable.lookupR  s     zz/9$//9J K##'::,0 1 1F $33X5J5JKL{{f{{--88FLLH}}VV\\2f 
kDII5	6 55
V
++g 
kk  (,,w(8(8(:;$..sDKK4M4MN  !2CA $223''c4;K;KLL 
D(//	0c""J% s   CII)NNFr@   )rX   rY   rZ   r[   r<   rD   rs   r\   r~   r"   rO   rU   r]   r^   s   @r#   rm  rm    sY    @J !%).HPT
&  
  +.r$   rm  c                       e Zd Zed        Zy)StaticVocabularyTableV1c                     | j                   | j                   j                  S t        j                  d d      5  t	        j
                         cd d d        S # 1 sw Y   y xY wrC  rH  rC   s    r#   rv   z#StaticVocabularyTableV1.initializer  rI  rJ  Nr   r   r$   r#   rw  rw    s     & &r$   rw  r   r  c
                    | t        | t              r| st        d      |dk  rt        d|z        |J|dk  rE| }
t        | t        j                        rt        j                  |       xs d}
t        d||
fz        |j                  s(t        j                  |j                  k7  rt        d      t        j                  |d	      5  d}t        j                  dd
      5  t        | ||j                  rt        j                  n|d|||	      }t!        ||      }ddd       |rt#        ||||      }|cddd       S # 1 sw Y   %xY w# 1 sw Y   yxY w)a
  Returns a lookup table that converts a string tensor into int64 IDs.

  This operation constructs a lookup table to convert tensor of strings into
  int64 IDs. The mapping can be initialized from a vocabulary file specified in
  `vocabulary_file`, where the whole line is the key and the zero-based line
  number is the ID.

  Any lookup of an out-of-vocabulary token will return a bucket ID based on its
  hash if `num_oov_buckets` is greater than zero. Otherwise it is assigned the
  `default_value`.
  The bucket ID range is
  `[vocabulary size, vocabulary size + num_oov_buckets - 1]`.

  The underlying table must be initialized by calling
  `session.run(tf.compat.v1.tables_initializer())` or
  `session.run(table.init())` once.

  To specify multi-column vocabulary files, use key_column_index and
  value_column_index and delimiter.

  - TextFileIndex.LINE_NUMBER means use the line number starting from zero,
    expects data type int64.
  - TextFileIndex.WHOLE_LINE means use the whole line content, expects data
    type string.
  - A value >=0 means use the index (starting at zero) of the split line based
    on `delimiter`.

  Sample Usages:

  If we have a vocabulary file "test.txt" with the following content:

  ```
  emerson
  lake
  palmer
  ```

  ```python
  features = tf.constant(["emerson", "lake", "and", "palmer"])
  table = tf.lookup.index_table_from_file(
      vocabulary_file="test.txt", num_oov_buckets=1)
  ids = table.lookup(features)
  ...
  tf.compat.v1.tables_initializer().run()

  ids.eval()  ==> [0, 1, 3, 2]  # where 3 is the out-of-vocabulary bucket
  ```

  Args:
    vocabulary_file: The vocabulary filename, may be a constant scalar `Tensor`.
    num_oov_buckets: The number of out-of-vocabulary buckets.
    vocab_size: Number of the elements in the vocabulary, if known.
    default_value: The value to use for out-of-vocabulary feature values.
      Defaults to -1.
    hasher_spec: A `HasherSpec` to specify the hash function to use for
      assignation of out-of-vocabulary buckets.
    key_dtype: The `key` data type.
    name: A name for this op (optional).
    key_column_index: The column index from the text file to get the `key`
      values from. The default is to use the whole line content.
    value_column_index: The column index from the text file to get the `value`
      values from. The default is to use the line number, starting from zero.
    delimiter: The delimiter to separate fields in a line.

  Returns:
    The lookup table to map a `key_dtype` `Tensor` to index `int64` `Tensor`.

  Raises:
    ValueError: If `vocabulary_file` is not set.
    ValueError: If `num_oov_buckets` is negative or `vocab_size` is not greater
      than zero.
  N:`vocabulary_file` must be specified and must not be empty.r   z8num_oov_buckets must be greater or equal than 0, got %d.r%  ?zD`vocab_size` must be greater than 0, got %d for vocabulary_file: %s.z4Dtype for `keys` should be either integer or string.string_to_indexr   
table_init)r  r0   r"   r  r  r  r<  r=  r0   )rj   r   r   r   r   r   constant_valuer   r   r   r/   r1   r   r}   r  r   r   r/  )vocabulary_filer<  r  ru   r=  r0   r"   r  r  r  vocab_file_valuer3   r   s                r#   index_table_from_filer    sx   d OS!A%4
DF Fq
B	  
Q&/:#4#45$33ODK
 ,/9;K.LM N N


V]]i6J6J%J
J
KK
~~d-. E	l	+ 
5'
$-$8$8FLLi+/d  m4e
5 $
)!	e ) 
5 
5 s$   	E";EEE
	EEc           	      P   | t        d      |dk  rt        d|z        |j                  s(t        j                  |j                  k7  rt        d      t        j                  |d      5  t        j                  |       }|j                  j                  |j                  k7  r*t        d|j                  rdnd	d
|j                  d      |j                  s5|j                  j                  |k7  rt        d|d
|j                  d      t        j                  |      }t        j                  t        j                  |      t        j                        }t        j                  dd      5  |j                  j                  r$t        j                  |t        j                        n|}	t!        |	||	j                  j                  t        j                  d      }
t#        |
|      }ddd       |rt%        |||      }cddd       S # 1 sw Y   %xY w# 1 sw Y   yxY w)a  Returns a lookup table that converts a string tensor into int64 IDs.

  This operation constructs a lookup table to convert tensor of strings into
  int64 IDs. The mapping can be initialized from a string `vocabulary_list` 1-D
  tensor where each element is a key and corresponding index within the tensor
  is the value.

  Any lookup of an out-of-vocabulary token will return a bucket ID based on its
  hash if `num_oov_buckets` is greater than zero. Otherwise it is assigned the
  `default_value`. The bucket ID range is
  `[vocabulary list size, vocabulary list size + num_oov_buckets - 1]`.

  The underlying table must be initialized by calling
  `session.run(tf.compat.v1.tables_initializer())` or
  `session.run(table.init())` once.

  Elements in `vocabulary_list` cannot have duplicates, otherwise when executing
  the table initializer op, it will throw a `FailedPreconditionError`.

  Sample Usages:

  ```python
  vocabulary_list = tf.constant(["emerson", "lake", "palmer"])
  table = tf.lookup.index_table_from_tensor(
      vocabulary_list=vocabulary_list, num_oov_buckets=1, default_value=-1)
  features = tf.constant(["emerson", "lake", "and", "palmer"])
  ids = table.lookup(features)
  ...
  tf.compat.v1.tables_initializer().run()

  ids.eval()  ==> [0, 1, 4, 2]
  ```

  Args:
    vocabulary_list: A 1-D `Tensor` that specifies the mapping of keys to
      indices. The type of this object must be castable to `dtype`.
    num_oov_buckets: The number of out-of-vocabulary buckets.
    default_value: The value to use for out-of-vocabulary feature values.
      Defaults to -1.
    hasher_spec: A `HasherSpec` to specify the hash function to use for
      assignment of out-of-vocabulary buckets.
    dtype: The type of values passed to `lookup`. Only string and integers are
      supported.
    name: A name for this op (optional).

  Returns:
    The lookup table to map an input `Tensor` to index `int64` `Tensor`.

  Raises:
    ValueError: If `vocabulary_list` is invalid.
    ValueError: If `num_oov_buckets` is negative.
  Nz$`vocabulary_list` must be specified.r   z:`num_oov_buckets` must be greater or equal than 0, got %d.z)`dtype` must either be integer or string.r|  zInvalid `dtype`: Expected r2  r3  , got r.   r   r}  r!   r~  )r   r   r   r   r/   r1   r   r}   re   rc   r   rO   r   rc  ranger   r   r   r/  )vocabulary_listr<  ru   r=  rc   r"   rT   num_elementsr   
table_keysr   r3   s               r#   index_table_from_tensorr    s   t 
;
<<q
D	  

%2B2B!B
?
@@
~~d-.   1Dzz 0 00((9m;TZZIJ J 4::#8#8E#Atzz+ , ,>>$'L]]8>>,7FF	l	+ 	5!%!6!6 ==
<@ &





%
%
,,d  m4e	5 $
)!	e
 9 	5 	5 s&   +DH1A;H,HH	HH%UNKc           	          | t        | t              r| st        d      ||dk  rt        d| d      t        j                  |d      5  t        | |d|||      }t        ||      cddd       S # 1 sw Y   yxY w)	a  Returns a lookup table that maps a `Tensor` of indices into strings.

  This operation constructs a lookup table to map int64 indices into string
  values. The table is initialized from a vocabulary file specified in
  `vocabulary_file`, where the whole line is the value and the
  zero-based line number is the index.

  Any input which does not have a corresponding index in the vocabulary file
  (an out-of-vocabulary entry) is assigned the `default_value`

  The underlying table must be initialized by calling
  `session.run(tf.compat.v1.tables_initializer())` or
  `session.run(table.init())` once.

  To specify multi-column vocabulary files, use key_column_index and
  value_column_index and delimiter.

  - TextFileIndex.LINE_NUMBER means use the line number starting from zero,
    expects data type int64.
  - TextFileIndex.WHOLE_LINE means use the whole line content, expects data
    type string.
  - A value >=0 means use the index (starting at zero) of the split line based
    on `delimiter`.

  Sample Usages:

  If we have a vocabulary file "test.txt" with the following content:

  ```
  emerson
  lake
  palmer
  ```

  ```python
  indices = tf.constant([1, 5], tf.int64)
  table = tf.lookup.index_to_string_table_from_file(
      vocabulary_file="test.txt", default_value="UNKNOWN")
  values = table.lookup(indices)
  ...
  tf.compat.v1.tables_initializer().run()

  values.eval() ==> ["lake", "UNKNOWN"]
  ```

  Args:
    vocabulary_file: The vocabulary filename, may be a constant scalar `Tensor`.
    vocab_size: Number of the elements in the vocabulary, if known.
    default_value: The value to use for out-of-vocabulary indices.
    name: A name for this op (optional).
    key_column_index: The column index from the text file to get the `key`
      values from. The default is to use the line number, starting from zero.
    value_column_index: The column index from the text file to get the `value`
      values from. The default is to use the whole line content.
    delimiter: The delimiter to separate fields in a line.

  Returns:
    The lookup table to map a string values associated to a given index `int64`
    `Tensors`.

  Raises:
    ValueError: when `vocabulary_file` is empty.
    ValueError: when `vocab_size` is invalid.
  Nrz  r%  z)`vocab_size` must be greater than 0, got r.   index_to_stringr}  )r  r"   r  r  r  )rj   r   r   r   r}   r  r   )r  r  ru   r"   r  r  r  r   s           r#   index_to_string_table_from_filer  l  s    N OS!A%4
DF F 
Q
@AN
OO
~~d-. 
2))-D T=1
2 
2 
2s   A33A<c                    | t        d      t        j                  |d      5  t        j                  | t        j
                        } t        j                  |       }t        j                  t        j                  |      t        j                        }t        || t        j                  t        j
                  d      }t        ||      cddd       S # 1 sw Y   yxY w)a  Returns a lookup table that maps a `Tensor` of indices into strings.

  This operation constructs a lookup table to map int64 indices into string
  values. The mapping is initialized from a string `vocabulary_list` 1-D
  `Tensor` where each element is a value and the corresponding index within the
  tensor is the key.

  Any input which does not have a corresponding index in 'vocabulary_list'
  (an out-of-vocabulary entry) is assigned the `default_value`

  The underlying table must be initialized by calling
  `session.run(tf.compat.v1.tables_initializer())` or
  `session.run(table.init())` once.

  Elements in `vocabulary_list` cannot have duplicates, otherwise when executing
  the table initializer op, it will throw a `FailedPreconditionError`.

  Sample Usages:

  ```python
  vocabulary_list = tf.constant(["emerson", "lake", "palmer"])
  indices = tf.constant([1, 5], tf.int64)
  table = tf.lookup.index_to_string_table_from_tensor(
      vocabulary_list, default_value="UNKNOWN")
  values = table.lookup(indices)
  ...
  tf.compat.v1.tables_initializer().run()

  values.eval() ==> ["lake", "UNKNOWN"]
  ```

  Args:
    vocabulary_list: A 1-D string `Tensor` that specifies the strings to map
      from indices.
    default_value: The value to use for out-of-vocabulary indices.
    name: A name for this op (optional).

  Returns:
    The lookup table to map a string values associated to a given index `int64`
    `Tensors`.

  Raises:
    ValueError: when `vocabulary_list` is not set.
  Nz-`vocabulary_list` argument must be specified.r  r}  r!   )r   r   r}   re   r   r   r   rO   r   rc  r  r   r   r   )r  ru   r"   r  rT   r   s         r#   !index_to_string_table_from_tensorr    s    ` 
D
EE
~~d-. 2++OV]]KO>>/2L==5v||DD$ov||V]]OD T=12 2 2s   B(CCz$lookup.experimental.MutableHashTabler3   c                        e Zd ZdZ	 	 	 d fd	Zd Zed        ZddZddZ	ddZ
ddZdd	Zd
 Zd Zd Z G d dej"                        Z xZS )MutableHashTablea)  A generic mutable hash table implementation.

  Data can be inserted by calling the `insert` method and removed by calling the
  `remove` method. It does not support initialization via the init method.

  `MutableHashTable` requires additional memory during checkpointing and restore
  operations to create temporary key and value tensors.

  Example usage:

  >>> table = tf.lookup.experimental.MutableHashTable(key_dtype=tf.string,
  ...                                                 value_dtype=tf.int64,
  ...                                                 default_value=-1)
  >>> keys_tensor = tf.constant(['a', 'b', 'c'])
  >>> vals_tensor = tf.constant([7, 8, 9], dtype=tf.int64)
  >>> input_tensor = tf.constant(['a', 'f'])
  >>> table.insert(keys_tensor, vals_tensor)
  >>> table.lookup(input_tensor).numpy()
  array([ 7, -1])
  >>> table.remove(tf.constant(['c']))
  >>> table.lookup(keys_tensor).numpy()
  array([ 7, 8, -1])
  >>> sorted(table.export()[0].numpy())
  [b'a', b'b']
  >>> [a.item() for a in sorted(table.export()[1].numpy())]
  [7, 8]
  c                 T   t        j                  ||      | _        | j                  j                         | _        || _        || _        || _        || _        || _	        | j                  s8d| _
        t        j                         rdt        j                         fz  | _
        t        t        | C  ||       | j#                         | _        |rZt        j'                  | |      }t        j                         s/t        j(                  t         j*                  j,                  |       yyy)a<  Creates an empty `MutableHashTable` object.

    Creates a table, the type of its keys and values are specified by key_dtype
    and value_dtype, respectively.

    Args:
      key_dtype: the type of the key tensors.
      value_dtype: the type of the value tensors.
      default_value: The value to use if a key is missing in the table.
      name: A name for the operation (optional).
      checkpoint: if True, the contents of the table are saved to and restored
        from checkpoints. If `shared_name` is empty for a checkpointed table, it
        is shared using the table node name.
      experimental_is_anonymous: Whether to use anonymous mode for the
        table (default is False). In anonymous mode, the table
        resource can only be accessed via a resource handle. It can't
        be looked up by a name. When all resource handles pointing to
        that resource are gone, the resource will be deleted
        automatically.

    Returns:
      A `MutableHashTable` object.

    Raises:
      ValueError: If checkpoint is True and no name was specified.
    rb   Ntable_%d)r   re   rf   rg   r   _checkpointr9   r:   r   r   r   r   rp   r   r;   r  r<   rD   ro   	_Saveabler   r'   SAVEABLE_OBJECTS)	r=   r0   r2   ru   r"   
checkpointr   saveabler>   s	           r#   r<   zMutableHashTable.__init__%  s    B //[*D++557D!DDO#DDJ2Dd		"	"	$
 '#'')5	
D*9kB 113D!++D$7h&&(cmm<<hG ) r$   c                    | j                   r| j                  j                         j                  dk(  r8t	        j
                  | j                  | j                  | j                        }n1t	        j                  | j                  | j                  | j                  j                         | j                        }n| j                  xr | j                  d u }| j                  j                         j                  dk(  rCt	        j                  | j                  || j                  | j                  | j                        }n[t	        j                  | j                  || j                  | j                  | j                  j                         | j                        }t        j                         r	d | _        |S |j"                  j$                  j'                  d      d   | _        |S )Nr   r   )r0   r2   value_shaper"   )r   use_node_name_sharingr0   r2   r"   )r   r  r0   r2   r  r"   r   r   )r   rf   rg   ndimsr   anonymous_mutable_hash_tabler9   r:   r   'anonymous_mutable_hash_table_of_tensorsr  r   mutable_hash_table_v2 mutable_hash_table_of_tensors_v2r   rp   r   r   r"   r   )r=   r   r  s      r#   rD   z!MutableHashTable._create_resource]  s   				&	&	(	.	.!	3"??oo))	
 #JJoo))++557		 #..L43D3D3L				&	&	(	.	.!	3"88))"7oo))	 #CC))"7oo))++557	   "d  #**005b9dr$   c                     | j                   S r@   r   rC   s    r#   r"   zMutableHashTable.name  r   r$   c                 H   t        j                  |d| j                  z  | j                  g      5  t        j                  | j                        5  t        j                  | j                        cddd       cddd       S # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY wr{   r   r}   r"   r~   colocate_withr   r   rN   s     r#   rO   zMutableHashTable.size       
i$))3d6J6J5K	L IT112 I2243G3GHI II II I II I I#    BB/	BB	BB!c                 d   |j                   | j                  k7  r%t        d| j                   d|j                          t        j                  |d| j
                  z  | j                  || j                  f      5  t        j                  | j                  |      }ddd       |S # 1 sw Y   S xY w)  Removes `keys` and its associated values from the table.

    If a key is not present in the table, it is silently ignored.

    Args:
      keys: Keys to remove. Can be a tensor of any shape. Must match the table's
        key type.
      name: A name for the operation (optional).

    Returns:
      The created Operation.

    Raises:
      TypeError: when `keys` do not match the table data types.
    r   r   %s_lookup_table_removeN
rc   r9   r1   r   r}   r"   r~   rf   r   lookup_table_remove_v2r=   rT   r"   r   s       r#   removezMutableHashTable.remove  s      zzT__$9$//9J K##'::,0 1 1 
6B--tT5H5HI
K M001E1EtLbM I	M Is   :!B%%B/c                    t        j                  |d| j                  z  | j                  || j                  f      5  t        j
                  || j                  d      }t        j                  | j                        5  t        j                  | j                  |||n| j                        }ddd       ddd       S # 1 sw Y   xY w# 1 sw Y   S xY w)a  Looks up `keys` in a table, outputs the corresponding values.

    The `default_value` is used for keys not present in the table.

    Args:
      keys: Keys to look up. Can be a tensor of any shape. Must match the
        table's key_dtype.
      dynamic_default_values: The values to use if a key is missing in the
        table. If None (by default), the `table.default_value` will be used.
        Shape of `dynamic_default_values` must be same with
        `table.default_value` or the lookup result tensor.
        In the latter case, each key will have a different default value.

        For example:

          ```python
          keys = [0, 1, 3]
          dynamic_default_values = [[1, 3, 4], [2, 3, 9], [8, 3, 0]]

          # The key '0' will use [1, 3, 4] as default value.
          # The key '1' will use [2, 3, 9] as default value.
          # The key '3' will use [8, 3, 0] as default value.
          ```

      name: A name for the operation (optional).

    Returns:
      A tensor containing the values in the same shape as `keys` using the
        table's value type.

    Raises:
      TypeError: when `keys` do not match the table data types.
    %s_lookup_table_findrT   r   N)
r   r}   r"   r~   rf   re   r9   r  r   r   )r=   rT   dynamic_default_valuesr"   r   s        r#   rU   zMutableHashTable.lookup  s    D 
4tyy@--tT5H5HI
K L""4tVLdT112 L44  $%1 )?7;7J7JLLL M	L LL Ms$   AC>0C .C C		CCc                    t        j                  |d| j                  z  | j                  ||g      5  t        j                  || j
                  d      }t        j                  || j                  d      }t        j                  | j                        5  t        j                  | j                  ||      }ddd       ddd       S # 1 sw Y   xY w# 1 sw Y   S xY w)  Associates `keys` with `values`.

    Args:
      keys: Keys to insert. Can be a tensor of any shape. Must match the table's
        key type.
      values: Values to be associated with keys. Must be a tensor of the same
        shape as `keys` and match the table's value type.
      name: A name for the operation (optional).

    Returns:
      The created Operation.

    Raises:
      TypeError: when `keys` or `values` doesn't match the table data
        types.
    %s_lookup_table_insertrT   r!   r   N
r   r}   r"   r~   re   r9   r:   r  r   lookup_table_insert_v2r=   rT   r   r"   r   s        r#   insertzMutableHashTable.insert  s    " 
6B--tV<
> ;""4vFd$$VT->->XNfT112 ;2243G3G39;;	; I	; ;	; Is$   A$C"C
8C
C	CC c                 p   t        j                  |d| j                  z  | j                  g      5  t        j                  | j                        5  t        j                  | j                  | j                  | j                        \  }}ddd       ddd       fS # 1 sw Y   xY w# 1 sw Y   fS xY wr   z%s_lookup_table_export_valuesN	r   r}   r"   r~   r  r   r   r9   r:   r   s       r#   r   zMutableHashTable.export  s     
=		I--.
0 FT112 F)7)N)N  $//43D3D*F&FF
 /))F FF
 /))#    B)9B	B)B&	"B))B5c                 8    | j                         }|d   |d   dS )3Implements checkpointing protocols for `Trackable`.r   r%  -keys-valuesr   r=   tensorss     r#   _serialize_to_tensorsz&MutableHashTable._serialize_to_tensors       kkmGQZGAJ77r$   c                 >   t        j                  d| j                  z        5  t        j                  | j                        5  t        j                  | j                  |d   |d         cddd       cddd       S # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)r  %s_table_restorer  r  Nr   r}   r   r  r~   r   r   r=   restored_tensorss     r#   _restore_from_tensorsz&MutableHashTable._restore_from_tensors      	*TZZ7	8 )T112 )44  W%Y')) )) )) ) )) ) )#    B'A=*	B=B	BBc                     | |vrOt        | j                  | j                  | j                  | j                  | j
                  | j                        || <   | j                         }||    j                  |       yr  N)	r  r9   r:   rf   r   r  r   r  r  r=   
object_map
serializeds      r#   _copy_trackable_to_cpuz'MutableHashTable._copy_trackable_to_cpu  sn    :)
//






**





j ++-Jt**:6r$   c                   *     e Zd ZdZd fd	Zd Z xZS )MutableHashTable._Saveable1SaveableObject implementation for DenseHashTable.c                     |j                         }t        j                  |d   d|dz         t        j                  |d   d|dz         g}|xs || _        t        t
        j                  |   |||       y Nr   r   r  r%  r  )r   r   SaveSpec
table_namer;   r  r  r<   r=   r3   r"   r  r  specsr>   s         r#   r<   z#MutableHashTable._Saveable.__init__*  sr    g

#
#GAJD7N
C

#
#GAJD94D
Ee #*ddo&&6ueTJr$   c                 h   ~t        j                  d| j                  z        5  t        j                  | j                  j
                        5  t        j                  | j                  j
                  |d   |d         cd d d        cd d d        S # 1 sw Y   nxY w	 d d d        y # 1 sw Y   y xY wNr  r   r%  r   r}   r  r  r   r~   r   r   r=   r  restored_shapess      r#   restorez"MutableHashTable._Saveable.restore4      
>>,t>? Ltww667 	L66tww7N7N7G7J7G7JL	L 	LL L	L 	L 	LL L L#   *B(1B?	B(B	B((B1r@   rX   rY   rZ   r[   r<   r  r]   r^   s   @r#   r  r  '  s    ;KLr$   r  )r  TFr@   )NN)rX   rY   rZ   r[   r<   rD   r\   r"   rO   r  rU   r  r   r  r  r  r   SaveableObjectr  r]   r^   s   @r#   r  r    sz    @ ').6Hp&P  I4)V6*"8
)7$L"11 L Lr$   r  z"lookup.experimental.DenseHashTablec                        e Zd ZdZ	 	 	 	 d fd	Zd Zed        ZddZddZ	ddZ
ddZdd	Zdd
ZddZd Zd Zd Z G d dej&                        Z xZS )DenseHashTablea  A mutable hash table with faster lookups and higher memory usage.

  Data can be inserted by calling the `insert` method and removed by calling the
  `remove` method. It does not support initialization via the init method.

  Compared to `MutableHashTable`, `DenseHashTable` offers generally faster
  `insert`, `remove` and `lookup` operations, in exchange for a higher overall
  memory footprint.

  It uses "open addressing" with quadratic reprobing to resolve collisions. This
  requires specifying two keys in the key space, `empty_key` and `deleted_key`,
  that can never inserted into the table.

  Unlike `MutableHashTable`, `DenseHashTable` does not require additional memory
  for temporary tensors created during checkpointing and restore operations.

  Example usage:

  >>> table = tf.lookup.experimental.DenseHashTable(
  ...     key_dtype=tf.string,
  ...     value_dtype=tf.int64,
  ...     default_value=-1,
  ...     empty_key='',
  ...     deleted_key='$')
  >>> keys = tf.constant(['a', 'b', 'c'])
  >>> values = tf.constant([0, 1, 2], dtype=tf.int64)
  >>> table.insert(keys, values)
  >>> table.remove(tf.constant(['c']))
  >>> table.lookup(tf.constant(['a', 'b', 'c','d'])).numpy()
  array([ 0,  1, -1, -1])
  c
                    t        j                  ||d      | _        || _        || _        || _        | j                  j                         | _        || _        || _	        || _
        || _        |	| _        | j                  s8d| _        t        j                         rdt        j                          fz  | _        t"        t$        | O  ||       | j)                         | _        |rZt$        j-                  | |      }
t        j                         s/t        j.                  t         j0                  j2                  |
       yyy)a  Creates an empty `DenseHashTable` object.

    Creates a table, the type of its keys and values are specified by key_dtype
    and value_dtype, respectively.

    Args:
      key_dtype: the type of the key tensors.
      value_dtype: the type of the value tensors.
      default_value: The value to use if a key is missing in the table.
      empty_key: the key to use to represent empty buckets internally. Must not
        be used in insert, remove or lookup operations.
      deleted_key: the key to use to represent deleted buckets internally. Must
        not be used in insert, remove or lookup operations and be different from
        the empty_key.
      initial_num_buckets: the initial number of buckets (optional,
        default to 2^17=131072). Note that the default value is
        relatively large (~1MB), so if you are going to create many
        tables (likely the case when `experimental_is_anonymous` is
        `True`), you should set `initial_num_buckets` to a smaller
        value to reduce memory usage.
      name: A name for the operation (optional).
      checkpoint: if True, the contents of the table are saved to and restored
        from checkpoints. If `shared_name` is empty for a checkpointed table, it
        is shared using the table node name.
      experimental_is_anonymous: Whether to use anonymous mode for the
        table (default is False). In anonymous mode, the table
        resource can only be accessed via a resource handle. It can't
        be looked up by a name. When all resource handles pointing to
        that resource are gone, the resource will be deleted
        automatically.

    Returns:
      A `DenseHashTable` object.

    Raises:
      ValueError: If checkpoint is True and no name was specified.
    ru   r   Nr  )r   re   rf   r9   r:   _initial_num_bucketsrg   r   r  r   
_empty_key_deleted_keyr   r   r   rp   r   r;   r  r<   rD   ro   r  r   r'   r  )r=   r0   r2   ru   	empty_keydeleted_keyinitial_num_bucketsr"   r  r   r  r>   s              r#   r<   zDenseHashTable.__init__c  s   ^ //[@DDO#D !4D++557D!DDJDO#D2Dd		"	"	$
 '#'')5	.$(K@ 113D))$5h&&(cmm<<hG ) r$   c           
         t        j                  | j                  | j                  d      }t        j                  | j                  | j                  d      }| j
                  rDt        j                  ||| j                  | j                  | j                  | j                        }nk| j                  xr | j                  d u }t        j                  ||| j                  || j                  | j                  | j                  | j                        }t        j                          r	d | _        |S |j$                  j&                  j)                  d      d   | _        |S )Nr  r   r  )r  r  r2   r  r  r"   )r  r  r   r  r2   r  r  r"   r   r   )r   re   r  r9   r  r   r   "anonymous_mutable_dense_hash_tabler:   r   r  r   r  r   mutable_dense_hash_table_v2r   rp   r   r   r"   r   )r=   r  r  r   r  s        r#   rD   zDenseHashTable._create_resource  s/   %%t[BI''}FK CC!''''"77zzi #..L43D3D3L <<!'' 5''''"77zzi   "d  #**005b9dr$   c                     | j                   S r@   r   rC   s    r#   r"   zDenseHashTable.name  r   r$   c                 H   t        j                  |d| j                  z  | j                  g      5  t        j                  | j                        5  t        j                  | j                        cddd       cddd       S # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY wr{   r  rN   s     r#   rO   zDenseHashTable.size  r  r  c                    t        j                  |d| j                  z  | j                  |g      5  t        j                  || j
                  d      }t        j                  | j                        5  t        j                  | j                  || j                        }ddd       ddd       S # 1 sw Y   xY w# 1 sw Y   S xY w)a  Looks up `keys` in a table, outputs the corresponding values.

    The `default_value` is used for keys not present in the table.

    Args:
      keys: Keys to look up. Can be a tensor of any shape. Must match the
        table's key_dtype.
      name: A name for the operation (optional).

    Returns:
      A tensor containing the values in the same shape as `keys` using the
        table's value type.

    Raises:
      TypeError: when `keys` do not match the table data types.
    r  rT   r   N)
r   r}   r"   r~   re   r9   r  r   r   rf   )r=   rT   r"   r   s       r#   rU   zDenseHashTable.lookup  s    " 
4tyy@--t4
6 J""4tVLdT112 J44T5I5I4595H5HJJJ M	J JJ Ms$   AB=3,B1B=1B:	6B==Cc                    t        j                  |d| j                  z  | j                  ||g      5  t        j                  || j
                  d      }t        j                  || j                  d      }t        j                  | j                        5  t        j                  | j                  ||      }ddd       cddd       S # 1 sw Y   xY w# 1 sw Y   yxY w)r  r  rT   r   r   Nr  r  s        r#   insert_or_assignzDenseHashTable.insert_or_assign  s    " 
6B--tV<
> ""4tVLd$$
)):fT112 ;2243G3G39;;  
; ; s$   A$C"C8	CC	CC c                 (    | j                  |||      S )r  )r  )r=   rT   r   r"   s       r#   r  zDenseHashTable.insert	  s    "   vt44r$   c                 f   |j                   | j                  k7  r&t        d| j                  d|j                   d      t        j                  |d| j
                  z  | j                  || j                  f      5  t        j                  | j                  |      }ddd       |S # 1 sw Y   S xY w)r  z'Signature mismatch. Keys must be dtype r  r.   r  Nr  r  s       r#   erasezDenseHashTable.erase(	  s      zzT__$

4 5 5 
6B--tT5H5HI
K M 001E1EtLbM
 IM
 Is   ;!B&&B0c                 &    | j                  ||      S )r  )r  rS   s      r#   r  zDenseHashTable.removeC	  s      ::dD!!r$   c                 p   t        j                  |d| j                  z  | j                  g      5  t        j                  | j                        5  t        j                  | j                  | j                  | j                        \  }}ddd       ddd       fS # 1 sw Y   xY w# 1 sw Y   fS xY wr  r  r   s       r#   r   zDenseHashTable.exportU	  s     
=		I--.
0 FT112 F)7)N)N  $//43D3D*F&FF /))	F FF /))r  c                 8    | j                         }|d   |d   dS )2Implements checkpointing interface in `Trackable`.r   r%  r  r  r  s     r#   r  z$DenseHashTable._serialize_to_tensorsg	  r  r$   c                 >   t        j                  d| j                  z        5  t        j                  | j                        5  t        j                  | j                  |d   |d         cddd       cddd       S # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)r  r  r  r  Nr  r  s     r#   r  z$DenseHashTable._restore_from_tensorsl	  r  r  c                 4   | |vrpt        | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  	      || <   | j                         }||    j                  |       yr  )r  r9   r:   rf   r  r  r  r   r  r   r  r  r  s      r#   r  z%DenseHashTable._copy_trackable_to_cpuu	  s    :'
//






//




#
#
**






j ++-Jt**:6r$   c                   *     e Zd ZdZd fd	Zd Z xZS )DenseHashTable._Saveabler  c                     |j                         }t        j                  |d   d|dz         t        j                  |d   d|dz         g}|xs || _        t        t
        j                  |   |||       y r  )r   r   r  r  r;   r  r  r<   r  s         r#   r<   z!DenseHashTable._Saveable.__init__	  sq    g

#
#GAJD7N
C

#
#GAJD94D
Ee #*ddoN$$d4UE4Hr$   c                 h   ~t        j                  d| j                  z        5  t        j                  | j                  j
                        5  t        j                  | j                  j
                  |d   |d         cd d d        cd d d        S # 1 sw Y   nxY w	 d d d        y # 1 sw Y   y xY wr  r  r  s      r#   r  z DenseHashTable._Saveable.restore	  r  r  r@   r  r^   s   @r#   r  r  	  s    ;ILr$   r  )NMutableDenseHashTableTFr@   )rX   rY   rZ   r[   r<   rD   r\   r"   rO   rU   r  r  r  r  r   r  r  r  r   r  r  r]   r^   s   @r#   r  r  >  s    P $(+).IHVB  I465&6"$*$8
)7*L"11 L Lr$   r  )init_all_tables)r  N)Or[   collectionsr[  r   tensorflow.python.checkpointr   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   r   r
   r   r   r   tensorflow.python.opsr   r   r   r   r   r   $tensorflow.python.ops.gen_lookup_opstensorflow.python.saved_modelr   tensorflow.python.trackabler   r   rk   r    tensorflow.python.training.saverr   tensorflow.python.typesr   tensorflow.python.utilr   r(  "tensorflow.python.util.deprecationr    tensorflow.python.util.tf_exportr   r   r    r4   TrackableResourcer6   r`   r   register_tf_serializabler   r   r   rl   r   r   r   r   r  r  
namedtupler  rk  r"  r-  r/  rm  rw  r   r   r   r  r  r  r  legacy_saveable_namer  r  r   r$   r#   <module>r     s       8 + 3 . + 5 < 4 3 + 2 0 - * , 3 6 - > 0 = , 8 9 6 &'(D89
" : )
" 02FGH(+ I(+VC(*h00 *Z_? _D%A  '&&:<
#+AF2 AF ,<AFH '()6 6 *6t! $>33 $N -.@ 4 @ /@F !"  #& '(K. K )K\0%8 0f2!4 2j'''x6GH  *d+FZ F.&\_ \~ )b1YO Y 2Yx -./&3 & 0& +/*+%)(*&2$*MM#+8+C+C-:-F-F$(wv -.*,(4"(--!%aJ 0427)-5B5N5N7D7O7O.2Y2z 5:+/;2| 12%%%g.sL sL / 3sLl	 /0%%%g.^L_ ^L / 1^Lr$   