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

Reduce memory footprint #218

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9d6a9fd
Rename inverse_hessian.jl to lbfgs.jl
sethaxen Oct 24, 2024
0301ec1
Rename Hinit and make in-place
sethaxen Oct 24, 2024
a006c35
Build inverse Hessian in-place
sethaxen Oct 24, 2024
2d280f3
Refactor to modify cache structs in-place
sethaxen Oct 25, 2024
306d80a
Bump required Julia version
sethaxen Oct 25, 2024
5630cb0
Replace Unpack with Julia's destructuring syntax
sethaxen Oct 25, 2024
0059640
Remove outdated version checks
sethaxen Oct 25, 2024
4811186
Increment patch number
sethaxen Oct 25, 2024
225c8c9
Merge branch 'main' into reduce_storage
sethaxen Oct 25, 2024
8116d87
Bump downgrade CI Julia lower bound
sethaxen Oct 25, 2024
086b377
Merge branch 'main' into reduce_storage
sethaxen Oct 25, 2024
80864bd
Merge branch 'main' into reduce_storage
sethaxen Oct 25, 2024
bd9fba4
Increment patch version number
sethaxen Oct 25, 2024
0ef937a
Make rand_and_logpdf mutating
sethaxen Oct 31, 2024
7d99101
Make saving of ELBO draws optional
sethaxen Oct 31, 2024
6a968fb
Avoid overwriting draws if saving samples
sethaxen Nov 22, 2024
9baf53c
Rewrite lbfgs_inverse_hessians to update an LBFGSState
sethaxen Nov 25, 2024
a83d165
Keep L-BFGS defined as minimization problem
sethaxen Nov 25, 2024
8dd5b9f
Merge branch 'main' into reduce_storage
sethaxen Dec 10, 2024
9c9fe71
Merge branch 'main' into reduce_storage
sethaxen Jan 3, 2025
e02ad8b
Compute distribution and ELBO in callback
sethaxen Jan 3, 2025
8bb11ed
Check correct sign of logp(x)
sethaxen Jan 13, 2025
5eea214
Simplify construction of callback
sethaxen Jan 13, 2025
b38f781
Forward ndraws_elbo keyword
sethaxen Jan 13, 2025
bdbad37
Infer concrete container types
sethaxen Jan 13, 2025
f520bde
Correctly specify history length
sethaxen Jan 13, 2025
62370c8
Bump Optimization lower bound
sethaxen Jan 15, 2025
21da4db
Make cache construction work for Optimization <v4
sethaxen Jan 15, 2025
2ee6ce6
Improve inference of logp return type
sethaxen Jan 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ LogDensityProblems = "2.1.0"
LogDensityProblemsAD = "1.7.0"
MCMCChains = "6.0.2"
Optim = "1.7.2"
Optimization = "3.21, 4"
Optimization = "3.24, 4"
OptimizationNLopt = "0.2, 0.3"
OptimizationOptimJL = "0.2.1, 0.3, 0.4"
PDMats = "0.11.26"
Expand Down
2 changes: 1 addition & 1 deletion src/Pathfinder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ default_ad() = ADTypes.AutoForwardDiff()

include("transducers.jl")
include("woodbury.jl")
include("lbfgs.jl")
include("optimize.jl")
include("inverse_hessian.jl")
include("mvnormal.jl")
include("elbo.jl")
include("resample.jl")
Expand Down
20 changes: 13 additions & 7 deletions src/elbo.jl
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
function maximize_elbo(rng, logp, dists, ndraws, executor)
function maximize_elbo(rng, logp, dists, ndraws, executor; save_samples::Bool=true)
dim = isempty(dists) ? 0 : length(first(dists))
Tdraws = Matrix{eltype(eltype(dists))}
draws = save_samples ? nothing : Tdraws(undef, (dim, ndraws))
EE = Core.Compiler.return_type(
elbo_and_samples, Tuple{typeof(rng),typeof(logp),eltype(dists),Int}
elbo_and_samples!, Tuple{Tdraws,typeof(rng),typeof(logp),eltype(dists)}
)
estimates = similar(dists, EE)
isempty(estimates) && return 0, estimates

Folds.map!(estimates, dists, executor) do dist
return elbo_and_samples(rng, logp, dist, ndraws)
_draws = draws === nothing ? Tdraws(undef, (dim, ndraws)) : draws
return elbo_and_samples!(_draws, rng, logp, dist; save_samples)
end
_, iteration_opt = _findmax(estimates |> Transducers.Map(est -> est.value))
return iteration_opt, estimates
end

function elbo_and_samples(rng, logp, dist, ndraws)
ϕ, logqϕ = rand_and_logpdf(rng, dist, ndraws)
function elbo_and_samples!(ϕ, rng, logp, dist; save_samples::Bool=true)
ϕ, logqϕ = rand_and_logpdf!(rng, dist, ϕ)
logpϕ = similar(logqϕ)
logpϕ .= logp.(eachcol(ϕ))
logr = logpϕ - logqϕ
elbo = Statistics.mean(logr)
elbo_se = sqrt(Statistics.var(logr) / length(logr))
return ELBOEstimate(elbo, elbo_se, ϕ, logpϕ, logqϕ, logr)
ϕ_save = save_samples ? copyto!(similar(ϕ), ϕ) : similar(ϕ, map(zero, size(ϕ)))
return ELBOEstimate(elbo, elbo_se, ϕ_save, logpϕ, logqϕ, logr)
end

struct ELBOEstimate{T,P,L<:AbstractVector{T}}
struct ELBOEstimate{T,P<:AbstractMatrix{T},L<:Vector{T}}
value::T
std_err::T
draws::P
Expand Down
133 changes: 0 additions & 133 deletions src/inverse_hessian.jl

This file was deleted.

Loading
Loading