如何在 Pytorch 中使用 CUDA 流?

gas*_*oon 5 python pytorch

我想在 Pytorch 中使用 CUDA 流来并行一些计算,但我不知道该怎么做。例如,如果有 2 个任务 A 和 B 需要并行化,我想做以下事情:

stream0 = torch.get_stream()
stream1 = torch.get_stream()
with torch.now_stream(stream0):
    // task A
with torch.now_stream(stream1):
    // task B
torch.synchronize()
// get A and B's answer
Run Code Online (Sandbox Code Playgroud)

如何在真正的 python 代码中实现目标?

小智 13

s1 = torch.cuda.Stream()
s2 = torch.cuda.Stream()
# Initialise cuda tensors here. E.g.:
A = torch.rand(1000, 1000, device = ‘cuda’)
B = torch.rand(1000, 1000, device = ‘cuda’)
# Wait for the above tensors to initialise.
torch.cuda.synchronize()
with torch.cuda.stream(s1):
    C = torch.mm(A, A)
with torch.cuda.stream(s2):
    D = torch.mm(B, B)
# Wait for C and D to be computed.
torch.cuda.synchronize()
# Do stuff with C and D.
Run Code Online (Sandbox Code Playgroud)

  • “torch.cuda.synchronize()”等待 C 和 D 只部分正确。它等待设备中提交任何流的所有工作,包括“C”和“D”。您可以检查 torch.cuda.syncronize( )导致 https://github.com/pytorch/pytorch/blob/master/c10/cuda/CUDAFunctions.cpp 内的 cudaDeviceSyncronize() 调用,这是 ast 例程的描述:https://docs.nvidia.com /cuda/cuda-runtime-api/group__CUDART__DEVICE.html#group__CUDART__DEVICE_1g10e20b05a95f638a4071a655503df25d (2认同)