Skip to content
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

Share ForwardDiff tag between models? #36

Closed
KristofferC opened this issue Feb 27, 2023 · 2 comments
Closed

Share ForwardDiff tag between models? #36

KristofferC opened this issue Feb 27, 2023 · 2 comments

Comments

@KristofferC
Copy link
Contributor

As it is right now, each model gets it's own ForwardDiff tag:

struct MyTag end
.

This means that if you have two models, most methods must be compiled twice due to the type of Dual numbers changing due to the changing tag. I don't see a reason why the tag could not be defined inside ModelBaseEcon itself and then shared among the models. The reason for having the tag at all is to prevent "perturbation confusion" (JuliaDiff/ForwardDiff.jl#83) but that doesn't seem (at least to me) to apply here.

That would mean that all the precompile stuff at

function precompilefuncs(resid, RJ, ::Val{N}, tag) where {N}
ccall(:jl_generating_output, Cint, ()) == 1 || return nothing
# tag = MyTag # ForwardDiff.Tag{resid,Float64}
dual = ForwardDiff.Dual{tag,Float64,N}
duals = Array{dual,1}
cfg = ForwardDiff.GradientConfig{tag,Float64,N,duals}
mdr = DiffResults.MutableDiffResult{1,Float64,Tuple{Array{Float64,1}}}
precompile(resid, (Array{Float64,1},)) || error("precompile")
precompile(resid, (duals,)) || error("precompile")
precompile(RJ, (Array{Float64,1},)) || error("precompile")
for pred in Symbol[:isinf, :isnan, :isfinite, :iseven, :isodd, :isreal, :isinteger, :-, :+, :log, :exp]
pred (:iseven, :isodd) || precompile(getfield(Base, pred), (Float64,)) || error("precompile")
precompile(getfield(Base, pred), (dual,)) || error("precompile")
end
for pred in Symbol[:isequal, :isless, :<, :>, :(==), :(!=), :(<=), :(>=), :+, :-, :*, :/, :^]
precompile(getfield(Base, pred), (Float64, Float64)) || error("precompile")
precompile(getfield(Base, pred), (dual, Float64)) || error("precompile")
precompile(getfield(Base, pred), (Float64, dual)) || error("precompile")
precompile(getfield(Base, pred), (dual, dual)) || error("precompile")
end
precompile(ForwardDiff.extract_gradient!, (Type{tag}, mdr, dual)) || error("precompile")
precompile(ForwardDiff.vector_mode_gradient!, (mdr, typeof(resid), Array{Float64,1}, cfg)) || error("precompile")
# precompile(Tuple{typeof(ForwardDiff.extract_gradient!), Type{tag}, mdr, dual}) || error("precompile")
# precompile(Tuple{typeof(ForwardDiff.vector_mode_gradient!), mdr, resid, Array{Float64, 1}, cfg}) || error("precompile")
return nothing
end

could instead be compiled into ModelBaseEcon (for values of N from 1 to the max chunk size) instead of for each model.

@KristofferC
Copy link
Contributor Author

I made an attempt in #38

@bbejanov
Copy link
Member

bbejanov commented Apr 4, 2023

Thanks for the suggestion!
#38 has been merged, so this issue is now resolved.

@bbejanov bbejanov closed this as completed Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants