Skip to content

Embedding layer ~10x slower in tf.function #38287

Closed

Description

@mdanatg

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
...

Activity

  1. added
    comp:keras
    comp:ops
    type:performance
    on Apr 6, 2020
  2. added
    TF 2.0
    on Apr 7, 2020
  3. amahendrakar commented on Apr 7, 2020

    @amahendrakar
    Contributor

    Was able to reproduce the issue with TF v2.2.0rc2 and TF-nightly. Please find the attached gist. Thanks!

  4. szc11121 commented on Apr 20, 2020

    @szc11121

    any update?

  5. mdanatg commented on Apr 20, 2020

    @mdanatg
    Author
  6. added
    stat:awaiting tensorflower
    on Apr 20, 2020
  7. dathudeptrai commented on Aug 19, 2020

    @dathudeptrai

    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

  8. changed the title [-]Embedding layer ~10x slower in graph mode[/-] [+]Embedding layer ~10x slower in tf.function[/+] on Sep 22, 2020
  9. added a commit that references this issue on Oct 20, 2020
  10. jvishnuvardhan commented on Apr 2, 2021

    @jvishnuvardhan
    Contributor

    Looks like this was resolved. Please check the performance of the four cases below. Please check the gist here. Thanks!

    > Eager, no embedding
    > 0.13755321502685547
    > 0.005505561828613281
    > ------
    > 
    > Graph, no embedding
    > 0.30103421211242676
    > 0.0021209716796875
    > ------
    > Eager, embedding
    > 0.5504169464111328
    > 0.006941795349121094
    > ---
    > Graph, embedding
    > 0.5892281532287598
    > 0.002460002899169922
    > ---
    

    Performance of graph mode is better than eager model as shown above. Thanks!

  11. added
    stat:awaiting response
    and removed
    stat:awaiting tensorflower
    on Apr 2, 2021
  12. google-ml-butler commented on Apr 9, 2021

    @google-ml-butler

    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.

  13. added
    stale
    on Apr 9, 2021
  14. szc11121 commented on Apr 9, 2021

    @szc11121

    Looks like this was resolved. Please check the performance of the four cases below. Please check the gist here. Thanks!

    > Eager, no embedding
    > 0.13755321502685547
    > 0.005505561828613281
    > ------
    > 
    > Graph, no embedding
    > 0.30103421211242676
    > 0.0021209716796875
    > ------
    > Eager, embedding
    > 0.5504169464111328
    > 0.006941795349121094
    > ---
    > Graph, embedding
    > 0.5892281532287598
    > 0.002460002899169922
    > ---
    

    Performance of graph mode is better than eager model as shown above. Thanks!

    when will this fix release? since its tf2.4.1 now and tf2.6 seems still has a long way to go

  15. jvishnuvardhan commented on Apr 9, 2021

    @jvishnuvardhan
    Contributor

    @szc11121 The following are the results with TF2.5rc0. May be there is a 2.5rc1 and 2.5rc2 then stable TF2.5 (near future). If you want to use stable version, then in near future TF2.5 will be released. Please check the gist here. Thanks!

    > Eager, no embedding
    > 0.12328195571899414
    > 0.0040400028228759766
    > 0.0034770965576171875
    
    > Graph, no embedding
    > 0.283444881439209
    > 0.0018429756164550781
    > 0.0014486312866210938
    
    > Eager, embedding
    > 0.5126852989196777
    > 0.00421595573425293
    > 0.004243135452270508
    
    > Graph, embedding
    > 0.5634112358093262
    > 0.005776643753051758
    > 0.0012359619140625
    
  16. google-ml-butler commented on Apr 16, 2021

    @google-ml-butler

    Closing as stale. Please reopen if you'd like to work on this further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

TF 2.0comp:kerascomp:opsstalestat:awaiting responsetype:performance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions