Skip to content

Commit a3146eb

Browse files
committed
Add zero-allocation tests
1 parent 0161109 commit a3146eb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

test/fasteval.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module DynamicPPLFastLDFTests
22

33
using AbstractPPL: AbstractPPL
4+
using Chairmarks
45
using DynamicPPL
56
using Distributions
67
using DistributionsAD: filldist
@@ -76,6 +77,34 @@ end
7677
end
7778
end
7879

80+
@testset "FastLDF: performance" begin
81+
# Evaluating these three models should not lead to any allocations.
82+
@model function f()
83+
x ~ Normal()
84+
return 1.0 ~ Normal(x)
85+
end
86+
@model function submodel_inner()
87+
m ~ Normal(0, 1)
88+
s ~ Exponential()
89+
return (m=m, s=s)
90+
end
91+
# Note that for the allocation tests to work on this one, `inner` has
92+
# to be passed as an argument to `submodel_outer`, instead of just
93+
# being called inside the model function itself
94+
@model function submodel_outer(inner)
95+
params ~ to_submodel(inner)
96+
y ~ Normal(params.m, params.s)
97+
return 1.0 ~ Normal(y)
98+
end
99+
@testset for model in (f(), submodel_inner(), submodel_outer(submodel_inner()))
100+
vi = VarInfo(model)
101+
fldf = DynamicPPL.Experimental.FastLDF(model, DynamicPPL.getlogjoint_internal, vi)
102+
x = vi[:]
103+
bench = median(@be LogDensityProblems.logdensity(fldf, x))
104+
@test iszero(bench.allocs)
105+
end
106+
end
107+
79108
@testset "AD with FastLDF" begin
80109
# Used as the ground truth that others are compared against.
81110
ref_adtype = AutoForwardDiff()

0 commit comments

Comments
 (0)