課桌公司網(wǎng)站建設百度seo搜索引擎優(yōu)化
CUD Stream
- https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#c-language-extensions
中指出在kenel的調(diào)用函數(shù)中最后一個可選參數(shù)表示該核函數(shù)處在哪個流之中。
- 參數(shù)Dg用于定義整個grid的維度和尺寸,即一個grid有多少個block。為dim3類型。Dim3 Dg(Dg.x, Dg.y, 1)表示grid中每行有Dg.x個block,每列有Dg.y個block,第三維恒為1(目前一個核函數(shù)只有一個grid)。整個grid中共有Dg.x*Dg.y個block,其中Dg.x和Dg.y最大值為65535。
- 參數(shù)Db用于定義一個block的維度和尺寸,即一個block有多少個thread。為dim3類型。Dim3 Db(Db.x, Db.y, Db.z)表示整個block中每行有Db.x個thread,每列有Db.y個thread,高度為Db.z。Db.x和Db.y最大值為512,Db.z最大值為62。 一個block中共有Db.x*Db.y*Db.z個thread。計算能力為1.0,1.1的硬件該乘積的最大值為768,計算能力為1.2,1.3的硬件支持的最大值為1024。
- Ns 的類型為 size_t,用于設置每個block除了靜態(tài)分配的shared Memory以外,最多能動態(tài)分配的shared memory大小,單位為byte。不需要動態(tài)分配時該值為0或省略不寫。如[__shared__](https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#shared)中所述,此動態(tài)分配的內(nèi)存由聲明為外部數(shù)組的任何變量使用;
- 參數(shù)S是一個cudaStream_t類型的可選參數(shù),初始值為零,表示該核函數(shù)處在哪個流之中。
-
CUDA編程中,默認使用默認流非并行執(zhí)行kernel,每個kernel由許多thread并行的執(zhí)行在GPU上。Stream的概念是相對Grid level來說的,使得kernel在一個device上同時執(zhí)行。
-
官方提供的用例
// https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#streams
cudaStream_t stream[2];
for (int i = 0; i < 2; ++i)cudaStreamCreate(&stream[i]);
float* hostPtr;
cudaMallocHost(&hostPtr, 2 * size);
// 以下代碼示例將其中每個流定義為從主機到設備的一個內(nèi)存副本、一個內(nèi)核啟動和一個從設備到主機的內(nèi)存副本的序列:
for (int i = 0; i < 2; ++i) {cudaMemcpyAsync(inputDevPtr + i * size, hostPtr + i * size,size, cudaMemcpyHostToDevice, stream[i]);MyKernel <<<100, 512, 0, stream[i]>>>(outputDevPtr + i * size, inputDevPtr + i * size, size);cudaMemcpyAsync(hostPtr + i * size, outputDevPtr + i * size,size, cudaMemcpyDeviceToHost, stream[i]);
}
// 通過調(diào)用 釋放流
for (int i = 0; i < 2; ++i)cudaStreamDestroy(stream[i]);
PyTorch Stream
-
在PyTorch中,默認情況下,GPU上的操作是在默認流(default stream)中執(zhí)行的。默認流是一個序列化的流,其中的操作按照它們出現(xiàn)的順序逐個執(zhí)行。這意味著在沒有顯式指定其他流的情況下,所有的操作都會在默認流中執(zhí)行。
-
然而,PyTorch還提供了功能可以將操作提交到其他流中執(zhí)行,以充分利用GPU的并行性。這對于并行處理多個任務或同時執(zhí)行多個獨立操作非常有用。
-
您可以使用
torch.cuda.Stream()
來創(chuàng)建其他流,并使用torch.cuda.current_stream()
來獲取當前流。然后,您可以將操作提交到指定的流中執(zhí)行,例如:
import torchdevice = torch.device('cuda')# 創(chuàng)建一個默認流
default_stream = torch.cuda.current_stream()# 創(chuàng)建一個自定義流
custom_stream = torch.cuda.Stream()# 在默認流中執(zhí)行操作
with torch.cuda.stream(default_stream):# 執(zhí)行操作...# 在自定義流中執(zhí)行操作
with torch.cuda.stream(custom_stream):# 執(zhí)行操作...
例子
import torch
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.
print(C)
print(D)
// https://stackoverflow.com/questions/70128833/why-and-when-to-use-torch-cuda-stream
這樣可以利用多個流來并行執(zhí)行計算,并在計算和數(shù)據(jù)傳輸之間實現(xiàn)重疊。這對于提高GPU利用率和加速訓練或推理過程非常有幫助。
錯誤示例
- 沒有使用 synchronize() 或者 wait_stream()進行同步,可能導致再未完成歸一化前執(zhí)行求和
// https://pytorch.org/docs/stable/notes/cuda.html
cuda = torch.device('cuda')
s = torch.cuda.Stream() # Create a new stream.
A = torch.empty((100, 100), device=cuda).normal_(0.0, 1.0)
with torch.cuda.stream(s):# sum() may start execution before normal_() finishes!B = torch.sum(A)
CG
-
https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#streams
-
https://pytorch.org/docs/stable/notes/cuda.html#multistream-capture
-
https://pytorch.org/cppdocs/notes/tensor_cuda_stream.html
-
https://pypi.org/project/pytorch-stream/
-
CUDA 的 Stream and Event https://zhuanlan.zhihu.com/p/369367933
-
GITHUBGIST Gist就是小型代碼片段的分享https://www.cnblogs.com/leader755/p/14284716.html
-
[JIT] 在 TorchScript 中支持 CUDA 流 https://github.com/pytorch/pytorch/issues/41355
-
https://pytorch.org/docs/stable/notes/cuda.html#cuda-semantics
-
https://github.com/pytorch/pytorch/issues/41355
多設備
// https://pytorch.org/docs/stable/notes/cuda.html#cuda-semantics
cuda = torch.device('cuda') # Default CUDA device
cuda0 = torch.device('cuda:0')
cuda2 = torch.device('cuda:2') # GPU 2 (these are 0-indexed)x = torch.tensor([1., 2.], device=cuda0)
# x.device is device(type='cuda', index=0)
y = torch.tensor([1., 2.]).cuda()
# y.device is device(type='cuda', index=0)with torch.cuda.device(1):# allocates a tensor on GPU 1a = torch.tensor([1., 2.], device=cuda)# transfers a tensor from CPU to GPU 1b = torch.tensor([1., 2.]).cuda()# a.device and b.device are device(type='cuda', index=1)# You can also use ``Tensor.to`` to transfer a tensor:b2 = torch.tensor([1., 2.]).to(device=cuda)# b.device and b2.device are device(type='cuda', index=1)c = a + b# c.device is device(type='cuda', index=1)z = x + y# z.device is device(type='cuda', index=0)# even within a context, you can specify the device# (or give a GPU index to the .cuda call)d = torch.randn(2, device=cuda2)e = torch.randn(2).to(cuda2)f = torch.randn(2).cuda(cuda2)# d.device, e.device, and f.device are all device(type='cuda', index=2)