I am using a library to generate a rather expensive function which is returned as a RuntimeGeneratedFunction that I am also able to convert into an Expr. I would like to avoid having to re-generate this function and was hoping that there was some way to save this function to a file that I can then load and call at a later time? There’s probably a way to generate this function at precompile, but then the precompile time will be large. Any ideas or other approaches would be great!
I tried just saving the function to a file with JLD2, but when you load the file the resulting object is not callable.
If the generation of the Expr is the expensive part, does it help to just save that Expr to file (using JLD2.jl)? Then in your code, the @generated would first just look for the jld2-file and load the expr from there to return or else generate it anew and save it for later.
Yeah I realized the Expr can be saved to JLD2. I’m not so familiar with how @generated is supposed to work. Are you suggesting something like:
@generated function(path)
return load(path, "expr")
end
With this approach would the only thing happening at pre-compile time be the loading of the expression from the file? Do you see issues if I pre-compute the files and ship them in a package? I see some other threads with world age issues but I do not completely understand that.
Could do I think as well. Not sure the pros/cons of each
function (path)
return @RuntimeGeneratedFunction((load(path, "expr"))
end
This is close to what I am suggesting but does not work in this form. You need to write it like this:
@generated function foo(arguments, the, function, takes)
path = "path/to/savefile.jld2"
isfile(path) && return JLD2.load(path)["expr"] # load return a Dict so need to use the key we used for saving
# file not present, throw error or compute expr and save
end
From your snippet above it looks like there are many of these functions. Is this ghe case? I have some ideas to make life easier, then to copy/paste the code above a couple of times
Yes there are multiple functions that I would like to save to a file. I was planning on saving them to the same JLD2 file if that is possible.
I am using FastDifferentiation.jl so its similar to symbolics, but not quite. I did not see this page of the Symbolics.jl docs until you mentioned this, maybe I’ll try and add something similar to FastDifferntiation.jl. It looks like it just converts the a Symbol to a string and then writes that to a file? Now that I see that it seems obvious and also kind of avoids having to used @generated I believe since I can just add the julia file to the package.
essentially yes I think.
if your expression is something that can be eval’d it should in principle work to just save this to a file and include the entire file next time. Note that if your expression is big enough this might still have some compilation overhead. (probably this can also be removed with precompilation)
It might be that my idea wont work though - I havent really needed to do this before
The compilation overhead will be much smaller than actually generating the derivatives will be is the hope. I’ll figure out what works for saving/loading and mark something here as a solution.