2015-12-15 258 views

回答

18

在公開發布中還沒有辦法做到這一點。我們知道這是一個重要的功能,我們正在努力。

+7

是否有可能對此答案有更新?因爲https://github.com/tensorflow/tensorflow/issues/899似乎可以計算單個操作的FLOP,這可以提供洞察執行時間的信息。 –

65

我已經使用了Timeline object得到執行的時間圖中的每個節點:

  • 您使用的是經典sess.run()也指定可選參數optionsrun_metadata
  • 然後創建一個Timelinerun_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) 

然後,您可以打開谷歌Chrome,前往chrome://tracing頁面並加載timeline.json文件。 你應該看到:

timeline

+1

嗨!我嘗試爲我的網絡培訓創建一個時間軸,但不幸的是,按照您展示的方式進行操作只會產生最後一次調用session.run的時間軸。有沒有辦法在所有會話中彙總時間表? –

+1

使用TensorFlow 0.12.0-rc0,我發現我需要確保libcupti.so/libcupti.dylib位於庫路徑中才能正常工作。對於我(在Mac上),我在'DYLD_LIBRARY_PATH'中添加了'/ usr/local/cuda/extras/CUPTI/lib'。 –

8

您可以提取使用runtime statistics此信息。你需要做這樣的事情(請在上述環節的完整的例子):

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) 

不僅僅是打印出來你可以更好的在tensorboard看到它:

另外,單擊一個節點將顯示確切的總內存,計算時間和張量輸出大小。

[1]: https://www.tensorflow.org/get_started/graph_viz#

+1

「運行時統計信息」鏈接錯誤;它只是指向顯示的圖像。 – SuperElectric

1

對於脂肪lobyte奧利維爾Moindrot的答覆意見,如果你想對所有會話收集的時間表,你可以改變‘open('timeline.json', 'w')’到‘open('timeline.json', 'a')’ 。

2

由於在搜索「Tensorflow性能分析」時這個數字很高,請注意當前(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]) 
0

最近通過烏伯SBNet定製運算庫(http://www.github.com/uber/sbnet)釋放具有cuda_op_timer的實現,其可以通過以下方式被使用:

with tf.control_dependencies([input1, input2]): 
    dt0 = sbnet_module.cuda_op_timer(timer_name="section_1", 
is_start=True) 
with tf.control_dependencies([dt0]): 
    input1 = tf.identity(input1) 
    input2 = tf.identity(input2) 

### portion of subgraph to time goes in here 

with tf.control_dependencies([result1, result2, dt0]): 
    cuda_time = sbnet_module.cuda_op_timer(timer_name="section_1", 
is_start=False) 
with tf.control_dependencies([cuda_time]): 
    result1 = tf.identity(result1) 
    result2 = tf.identity(result2) 

注意,子圖的任何部分可以是異步的你應該非常小心地指定定時器操作的所有輸入和輸出依賴關係。否則,計時器可能會按順序插入圖形中,您可能會得到錯誤的時間。我發現時間軸和time.time()時間分析Tensorflow圖的非常有限的實用程序。

話雖如此,我個人建議切換到PyTorch :)開發迭代速度更快,代碼運行速度更快,而且一切都不那麼痛苦。

從tf.Session中減去開銷(可能是巨大的)的另一個有點冒失和神祕的方法是將圖複製N次,並運行變量N,從而求解未知固定開銷的方程。即你會在N1 = 10和N2 = 20的時候測量session.run(),你知道你的時間是t,開銷是x。所以像

N1*x+t = t1 
N2*x+t = t2 

解決x和t。缺點是這可能需要大量的內存並且不一定準確:)還要確保你的輸入是完全不同的/隨機的/獨立的,否則TF會摺疊整個子圖並且不能運行N次......用Tensorflow獲得樂趣: )

相關問題