-
-
Notifications
You must be signed in to change notification settings - Fork 572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problems pickling objects #699
Comments
Pickling geometry mesh and solution all work |
@tinosulzer it looks like the problem is definitely with autograd. It seems we use it everywhere though! The above comment references using sympy or casadi. I know a bit about sympy and you seem to know about casadi. What do you suggest we do? |
Worth checking both sympy and casadi both work with pickling first, but if either one does then I'm happy to drop autograd. It shouldn't be too bad, only appears when related to differentiating functions? Casadi would definitely work and probably be quicker, but if you're more comfortable implementing sympy and there won't be any performance loss then go for that |
Is your casadi branch ready? |
most of the casadi functionality is in master now, just a few small things to wrap up which is what I'm working on now in separate issues Edit: just checked and you can't pickle casadi either 😦: import pickle
import casadi
a = casadi.SX.sym("a")
with open("file","wb") as f:
pickle.dump(a, f) gives TypeError: can't pickle SwigPyObject objects |
So the only thing autograd is used for is to differentiate the interpolated functions is that right? |
No, it's used to differentiate generic user-specified functions such as the diffusivity or OCP, interpolated functions have their own inbuilt differentiation routines. But this is the only thing it's used for |
Could a solution be to create interpolated functions out of these user-specified ones? Or get the users to specify the derivative too? Sympy could possibly work. There's also a package called jax that might do it but it's linux only |
Not a fan of getting users to specify derivatives as that could easily get out of sync, creating interpolated functions could work but requires knowing ranges of values. We could just write our own rules for all the different functions though, maybe that's the easiest, I was planning on doing this anyway at some point |
Not sure what you mean by that |
At the moment we have |
Have implemented this but it made simplify much slower :( will investigate ... |
Sympy allows pickling of symbols but not the lambda functions
|
Hi @tinosulzer thanks for your work on this so far. I already made a branch for this which I think @rtimms made a change in. Shall we just delete my one? issue-699-enable-pickle |
If the changes don't clash (it doesn't look like they do, but I couldn't tell what @rtimms changed) then we can just merge the two branches |
i merged master into pybamm-pnm. was the new branch based off of that? |
The problem with setup becoming slower is not such a big deal if we move to using casadi as default (possible after #685 once interpolation works). The conversion to casadi is fast, only happens at the solver stage, and is only saved within the solver object, so we wouldn't necessarily have to pickle it. How comfortable are we generally with moving to using casadi's automatic differentiation as default, with ours only as backup? Pros: very fast, saves reinventing the wheel; cons: sunk cost, ours would probably get out of date |
I'm in favour of moving to casadi. It's far more sophisticated than anything we would want to implement ourselves. I think the time we spent time on our method isn't reason enough to stop us moving to something better. And for me at least, implementing stuff related to the jacobian helped me learn a lot about the codebase, debugging and python generally, so the time I invested personally isn't a sunk cost in my view. |
Did we not find out that casadi can't be pickled either. I think the solver would have to be pickled for multiprocessing. Is it worth me trying to get some basic multiprocessing set up, say for parametric study so we can test these options. In general I'm in favour of not re-inventing the wheel but I don't know much about casadi and how supported it will be, regularly it will be developed. Easy it is to install on various os. It would be good to have a little tutorial from @tinosulzer about it at our next dev meeting. Thanks for your hard work on it |
Definitely worth looking into multiprocessing :) it turns out you can pickle casadi once everything is set up, just not in the intermediate stages, but that is not a problem for us. https://web.casadi.org/blog/parfor/ |
Note that there are alternative to the standard library multiprocessing library that explicitly avoid standard pickle, such as this one: https://joblib.readthedocs.io/en/latest/parallel.html. This uses cloudpickle, which can pickle lambda functions. |
@martinjrobins thanks for the info. I will look into these alternatives as well but I read the link and there could be some significant slow downs potentially. |
Describe the bug
Some objects cannot be pickled due to the following error:
AttributeError: Can't pickle local object 'primitive..f_wrapped'
To Reproduce
Expected behaviour
Pickle saves the object
Additional context
This type of error can occur when classes are defined within other classes or functions i.e. not the global scope. There was one example of this in pybamm with the klu solver but I think the problem may also be with autograd as the f_wrapped function is from there
The text was updated successfully, but these errors were encountered: