Repository: tensorflow
Issue: Textfile initializer sharing bug
Labels: stat:awaiting tensorflower,type:bug,comp:ops,TF 2.15

### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

Yes

### Source

binary

### TensorFlow version

2.15

### Custom code

Yes

### OS platform and distribution

_No response_

### Mobile device

_No response_

### Python version

_No response_

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

The logic here for TextFileInitializer [sharing](https://github.com/tensorflow/tensorflow/blob/059b23d0dfd34e0d6cdf1f4a65dbc8ed1dfdf54a/tensorflow/python/ops/lookup_ops.py#L790) does not consider key/value dtype. If two text file initializers are made for same vocab file but with different dtypes (tf.int64/tf.string), then sharing will cause one of them to fail and crash.

The fix should be small change to add dtypes to shared_name.

### Standalone code to reproduce the issue

```shell
import os
import tempfile

import tensorflow as tf
from tensorflow.lookup import TextFileIndex

with tempfile.TemporaryDirectory("w") as tmp_dir:
    vocab_file = os.path.join(tmp_dir, "vocab.txt")
    with open(vocab_file, "w") as f:
        f.write("\n".join(["1", "2", "3"]))

    initializer1 = tf.lookup.TextFileInitializer(
        vocab_file, tf.string, TextFileIndex.WHOLE_LINE, tf.int64, TextFileIndex.LINE_NUMBER
    )
    table1 = tf.lookup.StaticHashTable(initializer1, -1)

    initializer2 = tf.lookup.TextFileInitializer(
        vocab_file, tf.int64, TextFileIndex.WHOLE_LINE, tf.int64, TextFileIndex.LINE_NUMBER
    )
    table2 = tf.lookup.StaticHashTable(initializer2, -1)

    _ = table1.lookup(tf.constant(["1", "2", "3"], dtype=tf.string))
    _ = table2.lookup(tf.constant([1, 2, 3], dtype=tf.int64))
```


### Relevant log output

```shell
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-12-13 13:50:41.113925: W tensorflow/core/framework/op_kernel.cc:1745] OP_REQUIRES failed at lookup_table_op.h:94 : INVALID_ARGUMENT: Conflicting key/value dtypes int64->int64 with string-int64 for table hash_table_/var/folders/3g/0v44vhyd05q6n_77rl_jnh6r0000gn/T/tmpiimi4gc8w/vocab.txt_-2_-1
Traceback (most recent call last):
  File "/Users/pa-loaner/.pyenv/versions/3.9.16/lib/python3.9/pdb.py", line 1726, in main
    pdb._runscript(mainpyfile)
  File "/Users/pa-loaner/.pyenv/versions/3.9.16/lib/python3.9/pdb.py", line 1586, in _runscript
    self.run(statement)
  File "/Users/pa-loaner/.pyenv/versions/3.9.16/lib/python3.9/bdb.py", line 580, in run
    exec(cmd, globals, locals)
  File "<string>", line 1, in <module>
  File "/Users/pa-loaner/Snapchat/Dev/training-platform/scratch/text_file_bug.py", line 1, in <module>
    import os
  File "/Users/pa-loaner/Snapchat/Dev/.venvs/bento/lib/python3.9/site-packages/tensorflow/python/training/tracking/resource.py", line 104, in __call__
    return previous_getter(*args, **kwargs)
  File "/Users/pa-loaner/Snapchat/Dev/.venvs/bento/lib/python3.9/site-packages/tensorflow/python/training/tracking/resource.py", line 99, in <lambda>
    previous_getter = lambda *a, **kw: default_resource_creator(None, *a, **kw)
  File "/Users/pa-loaner/Snapchat/Dev/.venvs/bento/lib/python3.9/site-packages/tensorflow/python/training/tracking/resource.py", line 96, in default_resource_creator
    obj.__init__(*a, **kw)
  File "/Users/pa-loaner/Snapchat/Dev/.venvs/bento/lib/python3.9/site-packages/tensorflow/python/ops/lookup_ops.py", line 347, in __init__
    super(StaticHashTable, self).__init__(default_value, initializer)
  File "/Users/pa-loaner/Snapchat/Dev/.venvs/bento/lib/python3.9/site-packages/tensorflow/python/ops/lookup_ops.py", line 198, in __init__
    self._resource_handle = self._create_resource()
  File "/Users/pa-loaner/Snapchat/Dev/.venvs/bento/lib/python3.9/site-packages/tensorflow/python/ops/lookup_ops.py", line 360, in _create_resource
    table_ref = gen_lookup_ops.hash_table_v2(
  File "/Users/pa-loaner/Snapchat/Dev/.venvs/bento/lib/python3.9/site-packages/tensorflow/python/ops/gen_lookup_ops.py", line 466, in hash_table_v2
    _ops.raise_from_not_ok_status(e, name)
  File "/Users/pa-loaner/Snapchat/Dev/.venvs/bento/lib/python3.9/site-packages/tensorflow/python/framework/ops.py", line 7164, in raise_from_not_ok_status
    raise core._status_to_exception(e) from None  # pylint: disable=protected-access
tensorflow.python.framework.errors_impl.InvalidArgumentError: Conflicting key/value dtypes int64->int64 with string-int64 for table hash_table_/var/folders/3g/0v44vhyd05q6n_77rl_jnh6r0000gn/T/tmpiimi4gc8w/vocab.txt_-2_-1 [Op:HashTableV2] name: hash_table
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /Users/pa-loaner/Snapchat/Dev/.venvs/bento/lib/python3.9/site-packages/tensorflow/python/framework/ops.py(7164)raise_from_not_ok_status()
-> raise core._status_to_exception(e) from None  # pylint: disable=protected-access
```