float16 is much slower than float32 in tensorflow when do embedding_lookup_sparse

Viewed 1037

When run the following code on Nvidia GTX1080ti, if FLOAT_DTYPE=tf.float16, the run time is 0.61s, if FLOAT_DTYPE = tf.float32, the time is 0.26s. The timeline show that scatter_sub take up a large part of the time. Why float16 is so much slower than float32? Tensorflow version is 1.8.

import time
import tensorflow as tf

FLOAT_DTYPE = tf.float16

var = tf.get_variable("embedding", initializer = tf.ones(shape = [1000000, 5], dtype = FLOAT_DTYPE), dtype = FLOAT_DTYPE)
embeddings = tf.nn.embedding_lookup(var, [i for i in range(100000)])

w = tf.get_variable("w", shape = [5, 1], dtype = FLOAT_DTYPE)
z = tf.matmul(embeddings, w)

opt = tf.train.AdamOptimizer(1e-3, epsilon = 1e-4)

g = tf.gradients(z, xs = [var])
train = opt.apply_gradients([(g[0], var)])

with tf.Session() as sess:
    tf.global_variables_initializer().run()

    start = time.time()
    for _ in range(10):
        sess.run(train)
    elapsed = time.time() - start
    print 'Time: %f' % elapsed
0 Answers
Related