快捷方式

torch.cuda.cudart

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

檢索 CUDA 執行時 API 模組。

此函式會初始化 CUDA 執行時環境(如果尚未初始化),並返回 CUDA 執行時 API 模組 (_cudart)。CUDA 執行時 API 模組提供對各種 CUDA 執行時函式的訪問。

引數

返回值

CUDA 執行時 API 模組 (_cudart)。

返回型別

module

引發
  • 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 全面的開發者文件

查閱文件

教程

獲取面向初學者和高階開發者的深度教程

查閱教程

資源

查詢開發資源並獲得問題解答

查閱資源