tensorflow代码优化策略
tensorflow code optimization strategy
请原谅这个问题的广泛性。也许等我知道的多了也许我可以问得更具体些。
我有一段性能敏感的 tensorflow 代码。从对 gpu 编程知之甚少的人的角度来看,我想知道什么指南或策略可以 "good place to start" 优化我的代码。 (单 GPU)
也许甚至读出在每个 tensorflow op 上花费的时间会很好......
我有一个模糊的理解
- 某些操作在分配给 cpu 而不是 gpu 时执行得更快,但不清楚是哪个
- 有一个 google 软件叫 "EEG",我在
中读到过
有一天可能会开源的论文。
可能还有其他我不知道的共同因素在起作用。
关于如何使用 Timeline 对象获取图中每个节点的执行时间,我想给出更完整的答案:
- 您使用经典
sess.run()
但指定参数 options
和 run_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)
然后您可以打开 Google Chrome,转到页面 chrome://tracing
并加载 timeline.json
文件。
你应该是这样的:
请原谅这个问题的广泛性。也许等我知道的多了也许我可以问得更具体些。
我有一段性能敏感的 tensorflow 代码。从对 gpu 编程知之甚少的人的角度来看,我想知道什么指南或策略可以 "good place to start" 优化我的代码。 (单 GPU)
也许甚至读出在每个 tensorflow op 上花费的时间会很好......
我有一个模糊的理解
- 某些操作在分配给 cpu 而不是 gpu 时执行得更快,但不清楚是哪个
- 有一个 google 软件叫 "EEG",我在
中读到过 有一天可能会开源的论文。
可能还有其他我不知道的共同因素在起作用。
关于如何使用 Timeline 对象获取图中每个节点的执行时间,我想给出更完整的答案:
- 您使用经典
sess.run()
但指定参数options
和run_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)
然后您可以打开 Google Chrome,转到页面 chrome://tracing
并加载 timeline.json
文件。
你应该是这样的: