我正在尝试更改一些 PyTorch 代码,以便它可以在 CPU 上运行。
该模型经过训练,torch.nn.DataParallel()因此当我加载预训练模型并尝试使用它时,我必须使用nn.DataParallel()我目前正在做的事情,如下所示:
device = torch.device("cuda:0")
net = nn.DataParallel(net, device_ids=[0])
net.load_state_dict(torch.load(PATH))
net.to(device)
Run Code Online (Sandbox Code Playgroud)
然而,当我将我的手电筒设备切换到 CPU 后,如下所示:
device = torch.device('cpu')
net = nn.DataParallel(net, device_ids=[0])
net.load_state_dict(torch.load(PATH))
net.to(device)
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
File "C:\My\Program\win-py362-venv\lib\site-packages\torch\nn\parallel\data_parallel.py", line 156, in forward
"them on device: {}".format(self.src_device_obj, t.device))
RuntimeError: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cpu
Run Code Online (Sandbox Code Playgroud)
我假设它仍在寻找 CUDA,因为这就是device_ids设置的内容,但有没有办法让它使用 CPU?PyTorch 存储库中的这篇文章让我认为我可以,但它没有解释如何做到。
如果没有,是否有其他方法可以在您的 CPU 上使用通过 DataParallel 训练的模型?
我用来rowwise在每一行上执行一个函数。这需要很长时间。为了加快速度,有没有办法使用并行处理,以便多个核心同时处理不同的行?
例如,我将 PRISM 天气数据 ( https://prism.oregonstate.edu/ ) 聚合到州一级,同时按人口进行加权。这是基于https://www.patrickbaylis.com/blog/2021-08-15-pop-weighted-weather/。
请注意,下面的代码需要下载每日天气数据以及具有非常小的地理区域人口估计值的 shapefile。
library(prism)
library(tidyverse)
library(sf)
library(exactextractr)
library(tigris)
library(terra)
library(raster)
library(ggthemes)
################################################################################
#get daily PRISM data
prism_set_dl_dir("/prism/daily/")
get_prism_dailys(type = "tmean", minDate = "2012-01-01", maxDate = "2021-07-31", keepZip=FALSE)
Get states shape file and limit to lower 48
states = tigris::states(cb = TRUE, resolution = "20m") %>%
filter(!NAME %in% c("Alaska", "Hawaii", "Puerto Rico"))
setwd("/prism/daily")
################################################################################
#get list of files in the directory, and extract date
##see if it is stable (TRUE) …Run Code Online (Sandbox Code Playgroud) 我有一个包含内部循环的外部 foreach/dopar 并行循环。内部循环的每个实例都应该处理同一组随机数。其余部分,即外部主体的其余部分和并行实例应照常工作,即具有独立的随机数。
我可以在非并行实现中实现这一点,方法是在内循环开始之前保存 RNG 的状态,并在执行内循环的每个实例之后恢复该状态。请参见以下示例:
library(doSNOW)
seed = 4711
cl = makeCluster(2)
registerDoSNOW(cl)
clusterSetupRNGstream (cl, seed=rep(seed,6))
erg = foreach(irun = 1:3,.combine = rbind) %dopar% {
#do some random stuff in outer loop
smp = runif(1)
# save current state of RNG
s = .Random.seed
# inner loop, does some more random stuff
idx = numeric(5)
for(ii in seq.int(5)) {
idx[ii] = sample.int(10, 1)
# reset RNG for next loop iteration
set.seed(s)
}
c(smp,idx)
}
> print(erg)
[,1] [,2] [,3] …Run Code Online (Sandbox Code Playgroud) I\xe2\x80\x99m 在高效并行化方面遇到一些麻烦np.concatenate。这是一个最小的工作示例。(我知道在这里我可以分别对a和b求和,但我专注于并行连接操作,因为这是我在项目中需要做的事情。然后我将对连接数组进行进一步的操作,例如排序。)
无论我在多少个核心上运行此程序,它似乎总是花费相同的时间(约 10 秒)。如果说有什么不同的话,那就是核心数越多,速度就越慢。我尝试在装饰器中使用 cc 来释放 GIL nogil=True,但没有成功。请注意,即使没有加速,所有核心在计算过程中显然都在使用。
有谁能够帮助我?
\n非常感谢
\nfrom numba import prange, njit\nimport numpy as np\n\n\n@njit()\ndef cc():\n\n r = np.random.rand(20)\n a = r[r < 0.5]\n b = r[r > 0.7]\n\n c = np.concatenate((a, b))\n\n return np.sum(c)\n\n\n@njit(parallel=True)\ndef cc_wrap():\n n = 10 ** 7\n result = np.empty(n)\n for i in prange(n):\n result[i] = cc()\n\n return result\n\ncc_wrap()\nRun Code Online (Sandbox Code Playgroud)\n 根据主题中的错误,修复方法是什么?
环境:
使用joblib并行处理时出现错误:
result_chunks = joblib.Parallel(n_jobs=njobs)(joblib.delayed(f_chunk)(i) for i in n_chunks)
Run Code Online (Sandbox Code Playgroud) 让我们想象一些抽象代码
private void Main()
{
var workTask1 = DoWork1();
var workTask2 = DoWork2();
var workTask3 = DoWork3();
await Task.WhenAll(workTask1, workTask2, workTask3);
AnalyzeWork(workTask1.Result, workTask2.Result, workTask3.Result);
}
private async Task<object> DoWork1()
{
var someOperationTask1 = someOperation1();
var someOperationTask2 = someOperation2();
await Task.WhenAll(someOperationTask1, someOperationTask2);
return new object
{
SomeOperationResult1 = someOperationTask1.Result,
SomeOperationResult2 = someOperationTask2.Result,
};
}
private async Task<object> DoWork2()
{
var someOperationTask3 = someOperation3();
var someOperationTask4 = someOperation4();
await Task.WhenAll(someOperationTask3, someOperationTask4);
return new object
{
SomeOperationResult3 = someOperationTask3.Result,
SomeOperationResult4 = someOperationTask4.Result,
}; …Run Code Online (Sandbox Code Playgroud) The task is like How to set bits of a bit vector efficiently in parallel?, but for CUDA.
Consider a bit vector of N bits in it (N is large, e.g. 4G) and an array of M numbers (M is also large, e.g. 1G), each in range 0..N-1 indicating which bit of the vector must be set to 1. The bit vector is just an array of integers, specifically uint32_t.
I've tried a naive implementation with …
我有一个在我的机器上编写的本地模块,我正在使用julia 1.7,当我想使用这个模块时,我会写这样的内容:
@everywhere include("Foo.jl")
using .Foo
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
UndefVarError: Foo not defined
Stacktrace:
[1] top-level scope
@ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/Distributed/src/macros.jl:200
[2] eval
@ ./boot.jl:373 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
Run Code Online (Sandbox Code Playgroud)
现在我不明白为什么它没有定义,即使它是在本地计算机的同一目录中编写的。
cppreference 对于std::exclusive_scan是这样说的:
d_first - 目标范围的开始;可能等于第一个
std::exclusive_scan所以在“就地”模式下使用覆盖存储应该没有问题。但是,对于 GCC 12.2.0 附带的 libstdc++ 实现,它无法与使用执行策略的重载一起使用,即使它是std::execution::seq. 考虑这个例子:
#include <algorithm>
#include <numeric>
#include <execution>
#include <vector>
#include <cassert>
int main()
{
const int size = 10;
std::vector<int> vec(size);
// without execution policy
std::fill(vec.begin(), vec.end(), 1);
std::exclusive_scan(vec.begin(), vec.end(), vec.begin(), 0);
assert(vec[0] == 0); // the first element should be 0
assert(vec[size-1] == size-1); // the last element should be the sum
// sequential execution policy
std::fill(vec.begin(), vec.end(), 1);
std::exclusive_scan(std::execution::seq, vec.begin(), vec.end(), vec.begin(), …Run Code Online (Sandbox Code Playgroud) 我正在使用 Simon Marlow 的书学习 Haskell 中的并行编程。在关于并行数独求解器的章节中,我决定使用回溯算法编写自己的求解器。问题是,当我尝试在 6 个核心之间分配 6 个案例时,几乎没有性能增益。当我尝试使用更多情况进行示例时,我获得了更显着的性能提升,但距离理论上的最大值(应在 5 到 6 之间)仍然很远。我知道某些情况可能运行得慢得多,但 threadscope 图显示没有理由这么少获得。有人可以解释一下我做错了什么吗?也许 ST 线程有一些我不理解的地方?
这是代码:
数独.hs
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
module Sudoku (getSudokus, solve) where
import Data.Vector(Vector, (!), generate, thaw, freeze)
import Data.List ( nub )
import qualified Data.Vector.Mutable as MV
import Text.Trifecta
import Control.Monad ( replicateM, when )
import Control.Applicative ((<|>))
import Control.Monad.ST
import Control.DeepSeq (NFData)
import GHC.Generics (Generic)
data Cell = Given Int
| Filled Int
| Empty
deriving (Generic, NFData)
newtype Sudoku …Run Code Online (Sandbox Code Playgroud)