我可以使用TensorFlow测量单个操作的执行时间吗?

use*_*888 68 tensorflow

我知道我可以测量一次调用的执行时间sess.run(),但是有可能获得更精细的粒度并测量单个操作的执行时间吗?

Oli*_*rot 100

我已经使用该Timeline对象来获取图中每个节点的执行时间:

  • 你使用经典sess.run()但也指定可选参数optionsrun_metadata
  • 然后Timeline使用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文件.你应该看到类似的东西:

时间线

  • 使用TensorFlow 0.12.0-rc0,我发现我需要确保libcupti.so/libcupti.dylib在库路径中才能使其工作.对于我(在Mac上),我将`/ usr/local/cuda/extras/CUPTI/lib`添加到`DYLD_LIBRARY_PATH`. (5认同)

Ian*_*low 22

在公开发布中还没有办法做到这一点.我们意识到这是一个重要的功能,我们正在努力.

  • 这个答案可能有更新吗?因为 https://github.com/tensorflow/tensorflow/issues/899 似乎可以计算单个操作的 FLOP,这可以深入了解执行时间。 (12认同)

Urs*_*Urs 21

由于在搜索"Tensorflow Profiling"时这是高位,请注意当前(2017年末,TensorFlow 1.4)获取时间轴的方式是使用ProfilerHook.这适用于tf.Estimator中的MonitoredSessions,其中tf.RunOptions不可用.

estimator = tf.estimator.Estimator(model_fn=...)
hook = tf.train.ProfilerHook(save_steps=10, output_dir='.')
estimator.train(input_fn=..., steps=..., hooks=[hook])
Run Code Online (Sandbox Code Playgroud)


Sal*_*ali 14

您可以使用运行时统计信息提取此信息.您将需要执行以下操作(请查看上述链接中的完整示例):

run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
sess.run(<values_you_want_to_execute>, options=run_options, run_metadata=run_metadata)
your_writer.add_run_metadata(run_metadata, 'step%d' % i)
Run Code Online (Sandbox Code Playgroud)

不仅仅是打印它你可以在tensorboard中看到它:

此外,单击节点将显示确切的总内存,计算时间和张量输出大小.

[链接示例


Pet*_*den 10

为了更新这个答案,我们确实有一些CPU分析功能,专注于推理.如果您查看https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/benchmark,您将看到一个程序,您可以在模型上运行以获得每个操作时间.