-
Notifications
You must be signed in to change notification settings - Fork 1
Description
The package currently only supports storing the generated models as .jl files, which can then be included and precompiled in a package for estimation/etc. This is necessary for large models where model generation can be slow. For smaller models, there is no reason that the functions couldn't be done interactively - although Julia's "world age" compilation issues make it more difficult that it might first appear.
The downside of the current approach are that: (1) modules and julia files are difficult to work with interactively/overwrite/etc.; and (2) the current hack of loading everything in the module and then passing a module is easy on the frontend but is abusing the module system. The most egregious issue there is use of the module wrapper class and https://github.com/HighDimensionalEconLab/DifferentiableStateSpaceModels.jl/blob/v0.4.19/src/types.jl#L1-L13
We needed to add that hack because between julia versions the Module type no longer supported deepcopy, which broke multithreaded sampling in Turing because our perturbation model couldn't be copied.
The solution to all of these problems is to (1) move from passing a module to the PerturbationModel constuctor to passing in functions, and then support runtime generated functions for that construction - as opposed to only loading from a module.
To sketch out the steps:
- Remove the
modtype in https://github.com/HighDimensionalEconLab/DifferentiableStateSpaceModels.jl/blob/v0.4.19/src/types.jl#L16-L31 and add in a huge number of generic type parameters for all of the underlying functions in the first and second order models. e.g.H!, H_yp!, H_x_p!, Ψ_yp!etc. and all of the other things that are currently access from the.modtype. For example,η, Q, u_symbolsetc.- To do a check to make sure you didn't miss anything, search in the sourcecode for
.mod.and all of those should be in the problem type.
- To do a check to make sure you didn't miss anything, search in the sourcecode for
- Change all cases of
.mod.m.in the sourcecode and just change to.or something along those lines.- For example, in
https://github.com/HighDimensionalEconLab/DifferentiableStateSpaceModels.jl/blob/v0.4.19/src/generate_perturbation.jl#L33it should change fromorder_vector_by_symbols(merge(p_d, p_f), m.mod.m.p_symbols)toorder_vector_by_symbols(merge(p_d, p_f), m.p_symbols) - Or https://github.com/HighDimensionalEconLab/DifferentiableStateSpaceModels.jl/blob/v0.4.19/src/generate_perturbation.jl#L85 changes from
m.mod.m.ȳ!(c.y, p)tom.ȳ!(c.y, p)
- For example, in
- Change the constructor in https://github.com/HighDimensionalEconLab/DifferentiableStateSpaceModels.jl/blob/v0.4.19/src/types.jl#L34-L45 so it has a module specialization. i.e.
function PerturbationModel(mod::Module)and then drop themodparameter and add all of those functions directly from the module itself.- e.g. if you had only added the
H!as the last parameter then you could do something like
- e.g. if you had only added the
function PerturbationModel(mod::Module)
return PerturbationModel{mod.max_order,mod.has_Ω,typeof(mod.η),typeof(mod.Q), typeof(mod.H!)}(
mod.max_order,
mod.n_y,
mod.n_x,
mod.n_p,
mod.n_ϵ,
mod.n_z,
mod.has_Ω,
mod.η,
mod.Q,
mod.H!)
end- But you will need to do that for all of the functions.
- Change the
showin https://github.com/HighDimensionalEconLab/DifferentiableStateSpaceModels.jl/blob/v0.4.19/src/types.jl#L48-L51 to use the internal values and avoid the.mod.m. - Delete the wrapper code https://github.com/HighDimensionalEconLab/DifferentiableStateSpaceModels.jl/blob/v0.4.19/src/types.jl#L1-L13 and do a final search in the code for
.mod.to make sure you didn't miss anything. - After you get that working, you will need to run the unit tests and benchmarks to make sure there weren't any hidden errors/etc.
Once that is done, we can add support for runtime functions in the next issue.