快捷方式

torch.cuda.cudart

torch.cuda.cudart()[原始碼][原始碼]

檢索 CUDA 運行時 API 模組。

如果 CUDA 運行時環境尚未初始化,此函數會初始化它,並傳回 CUDA 運行時 API 模組 (_cudart)。 CUDA 運行時 API 模組提供對各種 CUDA 運行時函數的存取。

參數

傳回

CUDA 運行時 API 模組 (_cudart)。

傳回類型

模組

引發
  • RuntimeError – 如果 CUDA 無法在 fork 的子進程中重新初始化,則會引發此錯誤。

  • AssertionError – 如果 PyTorch 編譯時未啟用 CUDA 支援,或者無法使用 libcudart 函式,則會引發此錯誤。

CUDA 運算搭配效能分析的範例
>>> import torch
>>> from torch.cuda import cudart, check_error
>>> import os
>>>
>>> os.environ['CUDA_PROFILE'] = '1'
>>>
>>> def perform_cuda_operations_with_streams():
>>>     stream = torch.cuda.Stream()
>>>     with torch.cuda.stream(stream):
>>>         x = torch.randn(100, 100, device='cuda')
>>>         y = torch.randn(100, 100, device='cuda')
>>>         z = torch.mul(x, y)
>>>     return z
>>>
>>> torch.cuda.synchronize()
>>> print("====== Start nsys profiling ======")
>>> check_error(cudart().cudaProfilerStart())
>>> with torch.autograd.profiler.emit_nvtx():
>>>     result = perform_cuda_operations_with_streams()
>>>     print("CUDA operations completed.")
>>> check_error(torch.cuda.cudart().cudaProfilerStop())
>>> print("====== End nsys profiling ======")
若要執行此範例並儲存效能分析資訊,請執行
>>> $ nvprof --profile-from-start off --csv --print-summary -o trace_name.prof -f -- python cudart_test.py

此指令會分析所提供腳本中的 CUDA 運算,並將效能分析資訊儲存到名為 trace_name.prof 的檔案。–profile-from-start off 選項確保效能分析僅在腳本中的 cudaProfilerStart 呼叫之後才開始。–csv–print-summary 選項分別將效能分析輸出格式化為 CSV 檔案並列印摘要。-o 選項指定輸出檔案名稱,而 -f 選項強制覆寫已存在的輸出檔案。

文件

存取 PyTorch 的完整開發者文件

查看文件

教學

取得初學者和進階開發者的深入教學

查看教學

資源

尋找開發資源並取得問題的解答

查看資源