Skip to content

Commit a38c709

Browse files
torfjeldeyebai
andauthored
Insane memory-usage when using Emcee with large number of iterations (#1976)
* use `reduce` instead of splatting to avoid stack overflow * added test for mem usage of Emcee * version bump * for emcee, walkers are now indexed along the chain index rather than iteration index * reduced number of iterations for emcee to see if that fixes tests * attempt at reducing the memory usage of Emcee tests * attempt at further reducing the memory footprint of the test * fixed size issues * Update emcee.jl --------- Co-authored-by: Hong Ge <[email protected]>
1 parent f8450d6 commit a38c709

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/inference/emcee.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,13 @@ function AbstractMCMC.bundle_samples(
123123

124124
# Extract names & construct param array.
125125
nms = [nms; extra_params]
126-
parray = map(x -> hcat(x[1], x[2]), zip(vals_vec, extra_values_vec))
127-
parray = cat(parray..., dims=3)
126+
# `hcat` first to ensure we get the right `eltype`.
127+
x = hcat(first(vals_vec), first(extra_values_vec))
128+
# Pre-allocate to minimize memory usage.
129+
parray = Array{eltype(x),3}(undef, length(vals_vec), size(x, 2), size(x, 1))
130+
for (i, (vals, extras)) in enumerate(zip(vals_vec, extra_values_vec))
131+
parray[i, :, :] = transpose(hcat(vals, extras))
132+
end
128133

129134
# Get the average or final log evidence, if it exists.
130135
le = getlogevidence(samples, state, spl)

test/inference/emcee.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
@testset "emcee.jl" begin
42
@testset "gdemo" begin
53
Random.seed!(9876)
@@ -14,6 +12,15 @@
1412
check_gdemo(chain)
1513
end
1614

15+
@testset "memory usage with large number of iterations" begin
16+
# https://github.com/TuringLang/Turing.jl/pull/1976
17+
@info "Testing emcee with large number of iterations"
18+
spl = Emcee(10, 2.0)
19+
n_samples = 10_000
20+
chain = sample(gdemo_default, spl, n_samples)
21+
check_gdemo(chain)
22+
end
23+
1724
@testset "initial parameters" begin
1825
nwalkers = 250
1926
spl = Emcee(nwalkers, 2.0)
@@ -30,7 +37,7 @@
3037

3138
# Initial parameters
3239
chain = sample(gdemo_default, spl, 1; init_params=fill([2.0, 1.0], nwalkers))
33-
@test chain[:s] == fill(2.0, nwalkers, 1)
34-
@test chain[:m] == fill(1.0, nwalkers, 1)
40+
@test chain[:s] == fill(2.0, 1, nwalkers)
41+
@test chain[:m] == fill(1.0, 1, nwalkers)
3542
end
3643
end

0 commit comments

Comments
 (0)