张量流代码优化策略

use*_*229 9 cpu performance gpu tensorflow

请原谅这个问题的广泛性.也许一旦我知道更多,也许我可以更具体地问.

我有性能敏感的tensorflow代码.从对gpu编程知之甚少的人的角度来看,我想知道哪些指南或策略是优化我的代码的"好开始".(单个gpu)

或许甚至可以读出每个张量流操作花了多长时间...

我有一个模糊的理解

  • 当分配给cpu而不是gpu时,某些操作会更快,但是不清楚哪个操作
  • 我在一篇
    论文中读到了一篇名为"EEG"的谷歌软件,可能有一天会被开源.

可能还有其他常见因素在起作用,我不知道..

Oli*_*rot 18

我想提供一个更完整的答案,关于如何使用Timeline对象来获取图中每个节点的执行时间:

  • 你使用经典sess.run()但指定参数optionsrun_metadata
  • 然后使用run_metadata.step_stats数据创建时间轴对象

这是示例代码:

import tensorflow as tf
from tensorflow.python.client import timeline

x = tf.random_normal([1000, 1000])
y = tf.random_normal([1000, 1000])
res = tf.matmul(x, y)

# Run the graph with full trace option
with tf.Session() as sess:
    run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
    run_metadata = tf.RunMetadata()
    sess.run(res, options=run_options, run_metadata=run_metadata)

    # Create the Timeline object, and write it to a json
    tl = timeline.Timeline(run_metadata.step_stats)
    ctf = tl.generate_chrome_trace_format()
    with open('timeline.json', 'w') as f:
        f.write(ctf)
Run Code Online (Sandbox Code Playgroud)

然后,您可以打开Goog​​le Chrome,转到该页面chrome://tracing并加载该timeline.json文件.你应该这样:

时间线