Search code examples
pythonmultiprocessingtheano

How to parallel a Theano function using multiprocessing?


I have a function returned by theano.function(), and I want to use it within multiprocessing for speedup. The following is a simplified demo script to show where I run into problem:

import numpy as np
from multiprocessing import Pool
from functools import partial
import theano
from theano import tensor

def get_theano_func():
    x = tensor.dscalar()
    y = x + 0.1
    f = theano.function([x], [y])
    return f

def func1(func, x):
    return func(x)

def MPjob(xlist):
    f = get_theano_func()
    fp = partial(func1, func=f)
    pool = Pool(processes=5)
    Results = pool.imap(fp, xlist)
    Y = []
    for y in Results:
        Y.append(y[0])
    pool.close()
    return Y

if __name__ == '__main__':
    xlist = np.arange(0, 5, 1)
    Y = MPjob(xlist)
    print(Y)

In the above codes, the theano function 'f' is fed to 'func1()' as input argument. If MPjob() runs correctly, it should return [0.1, 1.1, 2.1, 3.1, 4.1]. However, an exception "TypeError: func1() got multiple values for argument 'func'" raised.

The full trackback log is as follows:

multiprocessing.pool.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "C:\Python35\lib\multiprocessing\pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
TypeError: func1() got multiple values for argument 'func'
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "F:/DaweiLeng/Code/workspace/Python/General/theano_multiprocess_debug.py", line 36, in <module>
    Y = MPjob(xlist)
  File "F:/DaweiLeng/Code/workspace/Python/General/theano_multiprocess_debug.py", line 29, in MPjob
    for y in Results:
  File "C:\Python35\lib\multiprocessing\pool.py", line 695, in next
    raise value
TypeError: func1() got multiple values for argument 'func'

Anyone got a hint?


Solution

  • Turns out it's related with the partial() function. The full explanation is here https://github.com/Theano/Theano/issues/4720#issuecomment-232029702