我使用 chainer 框架来训练我的 CNN。为了加快速度,我使用了 cupy 和 multiprocess 包。但是,即使我添加了 multiprocessing.set_start_method('spawn') 语句,我仍然会遇到这样的错误:
CUDARuntime 错误。在里面
TypeError:需要一个整数
我的环境是:
win10
py3.7
cuda9.2
在我问这个问题之前,我已经参考了这个链接:Cupy get error in multithread.pool if GPU already used。总程序代码太长,所以我显示我的程序的一部分(引发错误的函数)如下:
class CNNEvaluation(object):
def __init__(self, gpu_num, epoch_num=50, batchsize=256,dataset='cifar10', valid_data_ratio=0.1, verbose=True):
self.gpu_num = gpu_num
self.epoch_num = epoch_num
self.batchsize = batchsize
self.dataset = dataset
self.valid_data_ratio = valid_data_ratio
self.verbose = verbose
def __call__(self, net_lists):
ctx = mp.get_context('spawn')
evaluations = np.zeros(len(net_lists))
for i in np.arange(0, len(net_lists), self.gpu_num):
process_num = np.min((i + self.gpu_num, len(net_lists))) - i
pool = ctx.Pool(process_num)
arg_data = [(cnn_eval, net_lists[i+j], j, self.epoch_num, self.batchsize, self.dataset,
self.valid_data_ratio, self.verbose) for j in range(process_num)]
evaluations[i:i+process_num] = pool.map(arg_wrapper_mp, arg_data)
pool.terminate()
return evaluations
输出如下:
Exception in thread Thread-6:
Traceback (most recent call last):
File "D:\AppInstall\Anaconda3\envs\py3_7\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "D:\AppInstall\Anaconda3\envs\py3_7\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "D:\AppInstall\Anaconda3\envs\py3_7\lib\multiprocessing\pool.py", line 470, in _handle_results
task = get()
File "D:\AppInstall\Anaconda3\envs\py3_7\lib\multiprocessing\connection.py", line 251, in recv
return _ForkingPickler.loads(buf.getbuffer())
File "cupy\cuda\runtime.pyx", line 134, in
cupy.cuda.runtime.CUDARuntimeError.__init__
TypeError: an integer is required
然后代码冻结并且不退出。我不明白为什么我添加了 multiprocessing.set_start_method('spawn') 语句,我仍然有错误消息。是因为我在 windows envs 而不是 linux 中运行程序吗?