hub icon indicating copy to clipboard operation
hub copied to clipboard

xling embedding extremely slow when not using feed dict / tf.Variable

Open r-wheeler opened this issue 7 years ago • 5 comments

Importing the module

import tensorflow as tf 
import tensorflow_hub as hub 

xling_8_embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder-xling-many/1")

%time xling_8_embed(tf.placeholder(dtype=tf.string, shape=[None]))

CPU times: user 9.7 s, sys: 321 ms, total: 10 s Wall time: 10 s <tf.Tensor 'module_1_apply_default_3/Encoder/l2_normalize:0' shape=(?, 512) dtype=float32>

%time xling_8_embed(tf.constant(["dog", "Puppies are nice.", "I enjoy taking long walks along the beach with my dog."]))

CPU times: user 5min 26s, sys: 3.17 s, total: 5min 29s Wall time: 5min 30s <tf.Tensor 'module_1_apply_default_4/Encoder/l2_normalize:0' shape=(3, 512) dtype=float32>

In a real world case, using this embedding in an estimator precludes using feed_dict, when used in a custom model_fn like:

def model_fn(features, labels):
    embeddings = xling_8_embed(features['text'])
...

r-wheeler avatar Jun 09 '19 15:06 r-wheeler

@r-wheeler , There is growth in the graph for each time you apply the "Module.call" and seeing slowness due to it. The correct pattern with TF Graphs/Sessions is to create the nodes in the graph that describe the computation that you want to execute and then execute it multiple times with session.run(), with the right feed_dict arguments.

rmothukuru avatar Jun 12 '19 09:06 rmothukuru

@rmothukuru would you happen to have an example of "the correct pattern"? This is pretty confusing

tgsmith61591 avatar Jun 12 '19 14:06 tgsmith61591

@rmothukuru. Thanks for the reply!

In both of these examples I am only calling xling_8_embed 1 time, the only difference is call being abled to atf.Variable or a tf.Tensor

r-wheeler avatar Jun 12 '19 14:06 r-wheeler

Hi r-wheeler you are right there is something wrong with the fact that feeding a constant takes minutes to create the graph and feeding a placeholder just takes a few seconds.

For the time being you can work around with tf.placeholder_with_default, to somehow trigger the code paths that are efficient. E.g. something like:

 inputs = tf.constant(["dog", "Puppies are nice.", "I enjoy taking long walks along the beach with my dog."])
 inputs = tf.placeholder_with_default(inputs, [None])
 xling_8_embed(inputs)

The root cause of the bad code paths is not clear yet.

PS: for the record, a copy-paste example ready to repro in colab.

andresusanopinto avatar Jun 14 '19 08:06 andresusanopinto

Closed due to lack of activity. Please reopen if issue persists.

j-grimstad avatar Jul 24 '20 11:07 j-grimstad