低GPU使用率和Tensorflow + RNN的性能

时间:2017-02-18 19:35:12

标签: tensorflow gpu lstm

我已经实现了一个试图从句子中预测单词的网络。网络实际上非常复杂,但这是一个简单的版本:

  1. 获取句子中的单词索引并转换为嵌入
  2. 通过LSTM运行每个句子
  3. 使用LSTM输出的线性乘法给句子中的每个单词一个分数
  4. 这是代码:

    # 40 samples with random size up to 500, vocabulary size is 10000 with 50 dimensions
    def inference(inputs):
       inputs = tf.constant(inputs)
       word_embeddings = tf.nn.embedding_lookup(embeddings, inputs)
    
       lengths = sequence_length(inputs)
    
       cell = BasicLSTMCell(cell_size)
       outputs, output_states = tf.nn.bidirectional_dynamic_rnn(cell, cell,     word_embeddings, sequence_length=lengths, dtype=tf.float32)
       lstm_out = tf.concat(outputs, 2)
    
       words_representations = tf.reshape(lstm_out, [-1, cell_size * 2])
       W = tf.Variable(tf.random_uniform([cell_size * 2, 1], -1, 1), name='W',     dtype=tf.float32)
       words_scores = tf.reshape(tf.matmul(words_representations, W), [-1,     max_length])
    
       return words_scores
    

    Reproducible code on gist

    使用Titan X,我的问题是GPU利用率低,约为40%。

    一些注意事项:

    • 我使用40的批量大小。虽然我可以把它做得更大,比如1000,并且每个样品的速度都非常好(每批几乎保持相同的时间!~0.7s vs~0.8s) - 我想要我的由于种种原因,批量大小保持在这个大小,其中包括与我正在尝试实施的文章类似。
    • 在此示例中,给定批次也是整个数据,因此不存在批处理,排队等问题,这可能使您更容易理解问题。

    我怀疑是问题,并且正如围绕论坛的类似问题的答案所指出的那样,重复CPU到GPU的内存传输。这是跟踪结果的样子:

    enter image description here

    绿条(pid 9和11)都是MEMCPYHtoD或MEMCPYDtoH。我该如何避免它们?

    如果不增加批量,可以做些什么来提高速度性能呢?

    可以找到样本可重现的代码here。使用Tensorflow 1.0,cudnn 5.1,cuda 8

    感谢。

0 个答案:

没有答案