STT-tensorflow/tensorflow/lite/kernels/hashtable
Jaesung Chung 077fe29d9d Add pybind for hash table kernels
This change fixes #45195

PiperOrigin-RevId: 345549641
Change-Id: I7305d171061df50bfb0e27275f28b9647478dd05
2020-12-03 15:43:07 -08:00
..
BUILD Add pybind for hash table kernels 2020-12-03 15:43:07 -08:00
hashtable_find.cc [tflite]: Insert nullptr checks when obtaining tensors. 2020-09-18 13:53:51 -07:00
hashtable_import.cc [tflite]: Insert nullptr checks when obtaining tensors. 2020-09-18 13:53:51 -07:00
hashtable_ops_test.cc
hashtable_ops_wrapper.cc Add pybind for hash table kernels 2020-12-03 15:43:07 -08:00
hashtable_ops.cc
hashtable_ops.h
hashtable_size.cc [tflite]: Insert nullptr checks when obtaining tensors. 2020-09-18 13:53:51 -07:00
hashtable.cc [tflite]: Insert nullptr checks when obtaining tensors. 2020-09-18 13:53:51 -07:00
README.md Merge pull request #44276 from kiszk:spelling_tweaks_lite_doc 2020-10-27 04:44:26 -07:00

How to use TF Lookup ops in TFLite

The objective of this file is to provide examples to demonstrate how to use TF Lookup ops in TFLite.

Supported Tensorflow Lookup ops in TFLite

Here is the supported status of TensorFlow Lookup ops.

TF Python lookup ops Supported status
tf.lookup.StaticHashTable Supported only with tensor initializers.

Supported mapping type: string → int64, int64 → string

tf.lookup.Hashtable Supported only with tensor initializers.

Supported mapping type: string → int64, int64 → string

tf.lookup.index_to_string_table_from_tensor Supported.
tf.lookup.index_table_from_tensor Supported natively when num_oov_buckets=0 and dtype=dtypes.string.

For the oov concept, you will need a Flex delegate.

tf.lookup.StaticVocabularyTable Supported but you will need a Flex delegate.

Use tf.index_table_from_tensor or tf.index_to_string_table_from_tensor instead if possible if you dont want to use Flex delegate.

tf.lookup.experimental.DenseHashTable

tf.contrib.lookup.MutableHashTable

tf.contrib.lookup.MutableDenseHashTable

Not supported yet.
tf.lookup.IdTableWithHashBuckets Supported but you need a Flex delegate.

Python Sample code

Here, you can find the Python sample code:

  • Static hash table (string → int64)
int64_values = tf.constant([1, 2, 3], dtype=tf.int64)
string_values = tf.constant(['bar', 'foo', 'baz'], dtype=tf.string)

initializer = tf.lookup.KeyValueTensorInitializer(string_values, int64_values)
table = tf.lookup.StaticHashTable(initializer, 4)

with tf.control_dependencies([tf.initializers.tables_initializer()]):
  input_string_tensor = tf.compat.v1.placeholder(tf.string, shape=[1])
  out_int64_tensor = table.lookup(input_string_tensor)
  • Static hash table, initialized from a file (string → int64)
with open('/tmp/vocab.file', 'r') as f:
  words = f.read().splitlines()

string_values = tf.constant(words, dtype=tf.string)

initializer = tf.lookup.KeyValueTensorInitializer(string_values, int64_values)
table = tf.lookup.StaticHashTable(initializer, 4)

with tf.control_dependencies([tf.initializers.tables_initializer()]):
  input_string_tensor = tf.placeholder(tf.string, shape=[1])
  out_int64_tensor = table.lookup(input_string_tensor)
  • Index table (string → int64)
UNK_ID = -1
vocab = tf.constant(["emerson", "lake", "palmer"])
vocab_table = tf.lookup.index_table_from_tensor(vocab, default_value=UNK_ID)

input_tensor = tf.compat.v1.placeholder(tf.string, shape=[5])

with tf.control_dependencies([tf.initializers.tables_initializer()]):
  out_tensor = vocab_table.lookup(input_tensor)
  • Index table, initialized from a file (string → int64)
with open('/tmp/vocab.file', 'r') as f:
  words = f.read().splitlines()

UNK_ID = -1
vocab = tf.constant(words)
vocab_table = tf.lookup.index_table_from_tensor(vocab, default_value=UNK_ID)

input_tensor = tf.compat.v1.placeholder(tf.string, shape=[5])

with tf.control_dependencies([tf.initializers.tables_initializer()]):
  out_tensor = vocab_table.lookup(input_tensor)
  • Index to string table (int64 → string)
UNK_WORD = "unknown"
vocab = tf.constant(["emerson", "lake", "palmer"])
vocab_table = tf.lookup.index_to_string_table_from_tensor(vocab, default_value=UNK_WORD)

input_tensor = tf.compat.v1.placeholder(tf.int64, shape=[1])

with tf.control_dependencies([tf.initializers.tables_initializer()]):
  out_tensor = vocab_table.lookup(input_tensor)
  • Index to string table, initialized from a file (int64 → string)
with open('/tmp/vocab.file', 'r') as f:
  words = f.read().splitlines()

UNK_WORD = "unknown"
vocab = tf.constant(words)
vocab_table = tf.lookup.index_to_string_table_from_tensor(vocab, default_value=UNK_WORD)

input_tensor = tf.compat.v1.placeholder(tf.int64, shape=[1])

with tf.control_dependencies([tf.initializers.tables_initializer()]):
  out_tensor = vocab_table.lookup(input_tensor)

How to Include Hashtable ops in your TFLite.

Currently, hashtable ops are not included in the builtin op set. You need to add hashtable ops manually by including the following dependency:

"//tensorflow/lite/kernels/hashtable:hashtable_op_kernels"

And then, your op resolver should add them like the following statements:

  // Add hashtable op handlers.
  tflite::ops::custom::AddHashtableOps(&resolver);