Rate this Page

torch.cuda.cudart#

torch.cuda.cudart()[source]#

Retrieves the CUDA runtime API module.

This function initializes the CUDA runtime environment if it is not alreadyinitialized and returns the CUDA runtime API module (_cudart). The CUDAruntime API module provides access to various CUDA runtime functions.

Parameters

None

Returns

The CUDA runtime API module (_cudart).

Return type

module

Raises
  • RuntimeError – If CUDA cannot be re-initialized in a forked subprocess.

  • AssertionError – If PyTorch is not compiled with CUDA support or if libcudart functions are unavailable.

Example of CUDA operations with profiling:
>>>importtorch>>>fromtorch.cudaimportcudart,check_error>>>importos>>>>>>os.environ["CUDA_PROFILE"]="1">>>>>>defperform_cuda_operations_with_streams():>>>stream=torch.cuda.Stream()>>>withtorch.cuda.stream(stream):>>>x=torch.randn(100,100,device='cuda')>>>y=torch.randn(100,100,device='cuda')>>>z=torch.mul(x,y)>>>returnz>>>>>>torch.cuda.synchronize()>>>print("====== Start nsys profiling ======")>>>check_error(cudart().cudaProfilerStart())>>>withtorch.autograd.profiler.emit_nvtx():>>>result=perform_cuda_operations_with_streams()>>>print("CUDA operations completed.")>>>check_error(torch.cuda.cudart().cudaProfilerStop())>>>print("====== End nsys profiling ======")
To run this example and save the profiling information, execute:
>>> $ nvprof --profile-from-start off --csv --print-summary -o trace_name.prof -f -- python cudart_test.py

This command profiles the CUDA operations in the provided script and savesthe profiling information to a file namedtrace_name.prof.The–profile-from-start off option ensures that profiling starts onlyafter thecudaProfilerStart call in the script.The–csv and–print-summary options format the profiling output as aCSV file and print a summary, respectively.The-o option specifies the output file name, and the-f option forces theoverwrite of the output file if it already exists.