Pytorch精準(zhǔn)記錄函數(shù)運(yùn)行時(shí)間的方法
0. 引言
參考Pytorch官方文檔對(duì)CUDA的描述,GPU的運(yùn)算是異步執(zhí)行的。一般來(lái)說(shuō),異步計(jì)算的效果對(duì)于調(diào)用者來(lái)說(shuō)是不可見的,因?yàn)?/p>
- 每個(gè)設(shè)備按照排隊(duì)的順序執(zhí)行操作
- Pytorch對(duì)于CPU和GPU的同步,GPU間的同步是自動(dòng)執(zhí)行的,不需要顯示寫在代碼中
異步計(jì)算的后果是,沒(méi)有同步的時(shí)間測(cè)量是不準(zhǔn)確的。
1. 解決方案
參考引言中提到的幫助文檔,Pytorch官方給出的解決方案是使用torch.cuda.Event記錄時(shí)間,具體代碼如下:
# import torch start_event = torch.cuda.Event(enable_timing=True) end_event = torch.cuda.Event(enable_timing=True) start_event.record() # Run your code snippet here end_event.record() torch.cuda.synchronize() # Wait for the events to be recorded! elapsed_time_ms = start_event.elapsed_time(end_event) # elapsed time (ms)
將你的代碼插入start_event.record()和end_event.record()中間以測(cè)量時(shí)間(單位毫秒)。
有能力的讀者也可以包裝為裝飾器或者with語(yǔ)句使用:
先書寫一個(gè)自定義with類(ContextManager)
class CudaTimer:
def __init__(self):
self.start_event = torch.cuda.Event(enable_timing=True)
self.end_event = torch.cuda.Event(enable_timing=True)
def __enter__(self):
self.start_event.record()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.end_event.record()
torch.cuda.synchronize()
self.elapsed_time = self.start_event.elapsed_time(self.end_event) / 1000 # ms -> s
再安裝如下with語(yǔ)句返回:
with CudaTimer() as timer: # run your code here dt = timer.elapsed_time # s
這樣保證了多個(gè)文件調(diào)用時(shí)語(yǔ)句的簡(jiǎn)單性。特別提醒:獲取timer.elapsed_time操作不要寫在with語(yǔ)句內(nèi)部。在with語(yǔ)句未結(jié)束時(shí),是無(wú)法獲取timer的成員變量的。
2. 補(bǔ)充
對(duì)于CPU和GPU混合操作的函數(shù),使用torch.cuda.event可能會(huì)使統(tǒng)計(jì)時(shí)間比實(shí)際時(shí)間短,此時(shí)可以使用time.time()代替,標(biāo)準(zhǔn)的with對(duì)象書寫如下:
# import time
class Timer:
def __enter__(self):
self.start_time = time.time()
return self
def __exit__(self, exc_type, exc_value, traceback):
torch.cuda.synchronize()
self.elapsed_time = time.time() - self.start_time
然后只需要將上文的with CudaTimer() as timer替換為with Timer() as timer即可。
到此這篇關(guān)于Pytorch精準(zhǔn)記錄函數(shù)運(yùn)行時(shí)間的方法的文章就介紹到這了,更多相關(guān)Pytorch記錄函數(shù)運(yùn)行時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 工具 字符串轉(zhuǎn)numpy浮點(diǎn)數(shù)組的實(shí)現(xiàn)
這篇文章主要介紹了python 工具 字符串轉(zhuǎn)numpy浮點(diǎn)數(shù)組的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
python csv實(shí)時(shí)一條一條插入且表頭不重復(fù)問(wèn)題
這篇文章主要介紹了python csv實(shí)時(shí)一條一條插入且表頭不重復(fù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
python-pymysql如何實(shí)現(xiàn)更新mysql表中任意字段數(shù)據(jù)
這篇文章主要介紹了python-pymysql如何實(shí)現(xiàn)更新mysql表中任意字段數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
python調(diào)用DLL與EXE文件截屏對(duì)比分析
這篇文章主要為大家介紹了python調(diào)用DLL與EXE文件截屏對(duì)比分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-10-10
Python 函數(shù)繪圖及函數(shù)圖像微分與積分
今天小編就為大家分享一篇Python 函數(shù)繪圖及函數(shù)圖像微分與積分,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
解決jupyter notebook圖片顯示模糊和保存清晰圖片的操作
這篇文章主要介紹了解決jupyter notebook圖片顯示模糊和保存清晰圖片的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
解決pycharm啟動(dòng)后總是不停的updating indices...indexing的問(wèn)題
今天小編就為大家分享一篇解決pycharm啟動(dòng)后總是不停的updating indices...indexing的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11

