0

Is there a way to serialize or save a function that has been binarized using the ufuncify tool from SymPy's autowrap module?

There is a solution here using dill: How to serialize sympy lambdified function?, but this only works for lambdified functions.

Here's a minimal working example to illustrate:

>>> import pickle
>>> import dill
>>> import numpy
>>> from sympy import *
>>> from sympy.utilities.autowrap import ufuncify

>>> x, y, z = symbols('x y z')
>>> fx = ufuncify([x], sin(x))

>>> dill.settings['recurse'] = True
>>> dill.detect.errors(fx)
pickle.PicklingError("Can't pickle <ufunc 'wrapper_module_0'>: it's not found as __main__.wrapper_module_0")

Motivation: I have a few 50-million character-long SymPy expressions that I'd like to speed up; ufuncify works wonderfully (three-orders-of-magnitude improvement over lambidfy alone), but it takes 3 hr to ufuncify each expression. I would like to be able to take advantage of the ufuncify-ied expressions from time to time (without waiting a day to re-process them). Update: to illustrate, computer going to sleep killed my python kernel, and now I will need to wait ~10 hr to recover the binarized functions

Ken
  • 114
  • 1
  • 4

1 Answers1

0

Got it. This is actually simpler than using dill/pickle, as the autowrap module produces and compiles C code (by default) that can be imported as a C-extension module [1] [2].

One way to save the generated code is to simply specify the temp directory for autowrap[3], e.g.:

Python environment 1:

import numpy
from sympy import *
from sympy.utilities.autowrap import ufuncify

x, y, z = symbols('x y z')
fx = ufuncify([x], sin(x), tempdir='tmp')

Python environment 2 (same root directory):

import sys
sys.path.append('tmp')
import wrapper_module_0

wrapper_module_0.wrapped_8859643136(1)

There's probably a better approach (e.g., is there an easy way to name the module and its embedded function?), but this will work for now.

[1] http://www.scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html

[2] https://docs.python.org/2/extending/extending.html

[3] http://docs.sympy.org/latest/modules/utilities/autowrap.html

Ken
  • 114
  • 1
  • 4