【问题标题】:RuntimeError when using Theano shared variable in a Celery celery worker在 Celery celery worker 中使用 Theano 共享变量时出现 RuntimeError
【发布时间】:2016-01-26 01:19:55
【问题描述】:

我有一个名为simple_theano_tasksCelery task

@app.task(bind=True, queue='test')
def simple_theano_tasks(self):
  import theano, numpy as np
  my_array = np.zeros((0,), dtype=theano.config.floatX)
  shared = theano.shared(my_array, name='my_variable', borrow=True)
  print 'Done. Shared value is {}'.format(shared.get_value())

当 THEANO 被配置以使用 CPU 时,一切都按预期工作(没有错误):

$ THEANO_FLAGS=device=cpu celery -A my_project worker -c1 -l info -Q test

[INFO/MainProcess] 收到任务:my_project.tasks.simple_theano_tasks[xxxx]

[警告/Worker-1] 完成。共享值为 []

[INFO/MainProcess] 任务 my_project.tasks.simple_theano_tasks[xxxx] 0.00407959899985s 成功


现在,当我在启用 GPU 的情况下执行完全相同的操作时,Theano(或 CUDA)会引发错误:

$ THEANO_FLAGS=device=gpu celery -A my_project worker -c1 -l info -Q test

...

使用 gpu 设备 0:GeForce GTX 670M(启用 CNMeM)

...

[INFO/MainProcess] 收到任务:my_project.tasks.simple_theano_tasks[xxx]

[ERROR/MainProcess] 任务 my_project.tasks.simple_theano_tasks[xxx] 引发意外:RuntimeError("Cuda error 'initialization error' while copying %lli data element to device memory",)

Traceback(最近一次调用最后一次):

文件“/.../local/lib/python2.7/site-packages/celery/app/trace.py”,第 240 行,在 trace_task R = retval = fun(*args, **kwargs)

protected_call 中的文件“/.../local/lib/python2.7/site-packages/celery/app/trace.py”,第 438 行 return self.run(*args, **kwargs)

文件“/.../my_project/tasks.py”,第 362 行,在 simple_theano_tasks shared = theano.shared(my_array, name='my_variable', borrow=True)

文件“/.../local/lib/python2.7/site-packages/theano/compile/sharedvalue.py”,第 247 行,共享 allow_downcast=allow_downcast, **kwargs)

文件“/.../local/lib/python2.7/site-packages/theano/sandbox/cuda/var.py”,第 229 行,在 float32_shared_constructor deviceval = type_support_filter(value, type.broadcastable, False, None) RuntimeError:将 %lli 数据元素复制到设备内存时出现 Cuda 错误“初始化错误”


最后,当我在 Python shell 中运行完全相同的代码时,我没有错误:

$ THEANO_FLAGS=device=gpu python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import theano, numpy as np
Using gpu device 0: GeForce GTX 670M (CNMeM is enabled)
>>> my_array = np.zeros((0,), dtype=theano.config.floatX)
>>> shared = theano.shared(my_array, name='my_variable', borrow=True)
>>> print 'Done. Shared value is {}'.format(shared.get_value())
Done. Shared value is []

有没有人知道:

  • 为什么 theano 在 Celery worker 中的行为会有所不同?
  • 如何解决这个问题?

一些额外的上下文

  • 我正在使用theano@0.7.0 和Celery@3.1.18

  • “~/.theanorc”文件

[全局]

floatX=float32

设备=gpu

[模式]=FAST_RUN

[nvcc]

fastmath=真

[库]

cnmem=0.1

[cuda]

root=/usr/local/cuda

【问题讨论】:

  • @asksol 你有什么想法吗? BR

标签: python cuda celery theano


【解决方案1】:

解决方法是:

  1. 将 CPU 指定为目标设备(在“.theanorc”中或使用“THEANO_FLAGS=device=cpu”)
  2. 稍后,将分配的设备覆盖到指定的 GPU

Celery 任务现在是:

@app.task(bind=True, queue='test')
def simple_theano_tasks(self):
  # At this point, no theano import statements have been processed, and so the device is unbound
  import theano, numpy as np
  import theano.sandbox.cuda
  theano.sandbox.cuda.use('gpu') # enable gpu
  my_array = np.zeros((0,), dtype=theano.config.floatX)
  shared = theano.shared(my_array, name='my_variable', borrow=True)
  print 'Done. Shared value is {}'.format(shared.get_value())

注意I found the solution reading this article about using multiple GPU

【讨论】:

    猜你喜欢
    • 2019-06-25
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2023-04-01
    • 2021-01-24
    • 2021-12-22
    • 2016-09-20
    相关资源
    最近更新 更多