请原谅这个问题的广泛性。也许一旦我知道更多,也许我可以更具体地问。
我有性能敏感的tensorflow代码。从对gpu编程知之甚少的人的角度来看,我想知道哪些指南或策略是优化我的代码的“好开始”。 (单个gpu)
也许甚至读出每个张量流操作花了多长时间就好了......
我有一个模糊的理解
我可能还有其他常见的因素,我不知道......
答案 0 :(得分:18)
我想更全面地回答一下如何使用时间轴对象来获取图表中每个节点的执行时间:
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
文件。
你应该这样: