Repository navigation
Closed
Labels
TF 2.0Issues relating to TensorFlow 2.0comp:kerasKeras related issuescomp:opsOPs related issuesstaleThis label marks the issue/pr stale - to be closed automatically if no activitystat:awaiting responseStatus - Awaiting response from authortype:performancePerformance Issue
Description
System information
- Have I written custom code (as opposed to using a stock
example script provided in TensorFlow): Yes - OS Platform and Distribution (e.g.,
Linux Ubuntu 16.04): Standard colab environment - Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if
the issue happens on mobile device: N/A - TensorFlow installed from (source or
binary): - TensorFlow version (use command below): tf-nightly pip - Python version: - Bazel
version (if compiling from source): N/A - GCC/Compiler version (if compiling from
source): N/A - CUDA/cuDNN version: - GPU model and memory: N/A
This is a follow-up to #29075 (comment), to capture a separate issue identified by @szc11121.
Describe the current behavior
import time
import tensorflow as tf
tf.__version__
class Toymodel(tf.keras.Model):
def __init__(self, use_embedding):
super(Toymodel, self).__init__()
if use_embedding:
self.emb = tf.keras.layers.Embedding(100000, 512)
self.use_embedding = use_embedding
self.fc = tf.keras.layers.Dense(1)
def call(self, constant_input):
if self.use_embedding:
constant_input_emb = self.emb(constant_input)
else:
constant_input_emb = tf.expand_dims(constant_input, -1)
logit = self.fc(constant_input_emb)
output = tf.keras.activations.sigmoid(logit)
return logit, output
def run_step(model, optimizer, constant_input, y):
with tf.GradientTape() as tape:
logit, output = model(constant_input)
loss = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(logits=logit,
labels=tf.cast(y, dtype=tf.float32)))
gradient = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradient, model.trainable_weights))
return loss, output
def run_loop(in_graph, use_embedding):
model = Toymodel(use_embedding)
opt = tf.keras.optimizers.SGD(learning_rate=0.1, momentum=0.0)
if in_graph:
f = tf.function(run_step)
else:
f = run_step
for _ in range(20):
start = time.time()
loss, output = f(model, opt, tf.random.uniform([100], minval=0, maxval=100000), tf.random.uniform([100, 1]))
end = time.time()
print(end - start)
print('Eager, no embedding')
run_loop(False, False)
print('Graph, no embedding')
run_loop(True, False)
print('Eager, embedding')
run_loop(False, True)
print('Graph, embedding')
run_loop(True, True)
Describe the expected behavior
The graph versions sound be in faster in both cases (ignoring initial calls). However, the case which includes an embedding layer shows a 10x performance drop compared to the same model running eagerly:
Eager, no embedding
0.010893821716308594
0.0032660961151123047
...
Graph, no embedding
0.34873294830322266
0.0013270378112792969
...
Eager, embedding
0.4676947593688965
0.006254434585571289
...
Graph, embedding
0.5206460952758789
0.07123064994812012
...
Metadata
Metadata
Assignees
Labels
TF 2.0Issues relating to TensorFlow 2.0comp:kerasKeras related issuescomp:opsOPs related issuesstaleThis label marks the issue/pr stale - to be closed automatically if no activitystat:awaiting responseStatus - Awaiting response from authortype:performancePerformance Issue
Activity
amahendrakar commentedon Apr 7, 2020
Was able to reproduce the issue with TF v2.2.0rc2 and TF-nightly. Please find the attached gist. Thanks!
szc11121 commentedon Apr 20, 2020
any update?
mdanatg commentedon Apr 20, 2020
@omalleyt12 @fchollet
dathudeptrai commentedon Aug 19, 2020
hi, please update and try to fix this issue. There is some people report this bug recently. See (#42475). This bug cause slow training on language, signal model, please fix that. @mdanatg @omalleyt12 @fchollet
[-]Embedding layer ~10x slower in graph mode[/-][+]Embedding layer ~10x slower in tf.function[/+]⚔ Update new Embedding.
jvishnuvardhan commentedon Apr 2, 2021
Looks like this was resolved. Please check the performance of the four cases below. Please check the gist here. Thanks!
Performance of graph mode is better than eager model as shown above. Thanks!
google-ml-butler commentedon Apr 9, 2021
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you.
szc11121 commentedon Apr 9, 2021
when will this fix release? since its tf2.4.1 now and tf2.6 seems still has a long way to go
jvishnuvardhan commentedon Apr 9, 2021
@szc11121 The following are the results with
TF2.5rc0. May be there is a2.5rc1and2.5rc2then stableTF2.5(near future). If you want to use stable version, then in near futureTF2.5will be released. Please check the gist here. Thanks!google-ml-butler commentedon Apr 16, 2021
Closing as stale. Please reopen if you'd like to work on this further.