快捷键

torch.cuda.cudart

torch.cuda.cudart()[source][source]

获取 CUDA 运行时 API 模块。

此函数初始化 CUDA 运行时环境(如果尚未初始化)并返回 CUDA 运行时 API 模块(_cudart)。CUDA 运行时 API 模块提供了访问各种 CUDA 运行时函数的接口。

参数:

无 -

返回值:

CUDA 运行时 API 模块(_cudart)。

返回类型:

模块

引发:
  • 运行时错误 - 如果在分叉的子进程中无法重新初始化 CUDA。

  • 断言错误 - 如果 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 贡献者。

使用 Sphinx 构建,并使用 Read the Docs 提供的主题。

文档

PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源并获得您的疑问解答

查看资源