简体   繁体   English

Pytorch CPU和GPU并行运行

[英]Pytorch CPU and GPU run in parallel

Is there a way to do something with CPU (compute mean and variance of current mini-batch loss) while GPU is doing back-propagation? 在GPU进行反向传播时,是否可以对CPU进行某些处理(计算当前最小批量损失的均值和方差)?

Something like this: 像这样:

for input, label in dataloader:
  output = model(input)
  losses = some_loss_function(output, label) # size = (batch_size,)
  loss = losses.sum() / batch_size
  # =========== do on CPU ============
  mean = loss.item()
  var = losses.pow(2).sum().item() / batch_size - mean**2
  # ============ BP ================
  loss.backward()
  #gradient update

will the backward() on GPU wait for CPU computation to finish? 将在GPU上的向后()等待CPU计算完成吗? Is there a way to do backward() and CPU computation in parallel? 有没有一种方法可以并行进行Backward()和CPU计算?

It doesn't do both computations at the same time. 它不会同时执行两个计算。 There is no exposed mechanism to do it parallely. 没有公开的机制可以并行执行。 (there is an advanced mechanism using CUDA streams that allows to do this in pytorch, but it is too error-prone for most users) (存在使用CUDA流的高级机制,该机制允许在pytorch中执行此操作,但对于大多数用户而言,它太容易出错)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pytorch:计算 for 循环在 GPU 和 CPU 上的运行时间 - Pytorch: Calculating running time on GPU and CPU of a for loop 使用 Visual Studio 代码在 GPU 中运行 PyTorch - Run PyTorch in GPU with visual studio code 需要在基于 python pytorch 的代码中将 GPU 选项更改为 CPU - Need to change GPU option to CPU in a python pytorch based code PyTorch RuntimeError: 参数 #1 'self' 的张量在 CPU 上,但预计它们会在 GPU 上 - PyTorch RuntimeError: Tensor for argument #1 'self' is on CPU, but expected them to be on GPU gpu 未完全使用,model 在 CPU 中运行的时间与在 gpu 中的运行时间相同 - gpu not fully used, model takes same time to run in cpu as in gpu 与 PyTorch 并行运行多个模型 - Run multiple models of an ensemble in parallel with PyTorch 每次我在 pytorch 中使用 cuda() 从 CPU 到 GPU 删除变量时,大约需要 5 到 10 分钟 - Everytime I use cuda() to remove Variable from CPU to GPU in pytorch,it takes about 5 to 10 minitues 如何让我的 Spyder 代码在 GPU 而不是 Ubuntu 上的 cpu 上运行? - How to make my Spyder code run on GPU instead of cpu on Ubuntu? Pytorch: GPU Memory 泄漏 - Pytorch : GPU Memory Leak Pytorch model 在 CPU 和 GPU 上都用完了 memory - Pytorch model running out of memory on both CPU and GPU, can’t figure out what I’m doing wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM