-
Notifications
You must be signed in to change notification settings - Fork 36
Closed
Description
In models with a small vector parameter with a fixed length, using a StaticArray
for the vector parameter can lead to a significant speedup. The following is a proof of concept:
using Turing, StaticArrays, Setfield, MacroTools, LinearAlgebra
macro immutable(expr)
esc(MacroTools.postwalk(expr) do x
if @capture(x, L_[i_] = R_)
:($L = Setfield.@set $L[$i] = $R)
else
x
end
end)
end
d = 10
alg = HMC(0.1, 1)
alg = IS()
# x isa `Vector` of `SVector`s and `y` is a `Vector`
@model demo1(X, y, ::Type{T} = Float64) where {T} = begin
a = similar(zero(eltype(X)), T)
for i in 1:length(a)
@immutable(a[i] ~ Normal())
end
sigma ~ Beta()
for i in 1:length(y)
y[i] ~ Normal(dot(a, X[i]), sigma)
end
end
X1 = [@SVector rand(d) for i in 1:1000]
y1 = rand(1000)
model1 = demo1(X1, y1)
sample(model1, alg, 50000);
# X isa `Matrix` and `y` is a `Vector`
@model demo2(X, y, ::Type{T} = Float64) where {T} = begin
a = similar(X, T, size(X, 2))
for i in 1:length(a)
a[i] ~ Normal()
end
means = X * a
sigma ~ Beta()
for i in 1:length(y)
y[i] ~ Normal(means[i], sigma)
end
end
X2 = rand(1000, d)
y2 = rand(1000)
model2 = demo2(X2, y2)
sample(model2, alg, 50000);
I noticed a speedup of 20-50% from using a static vector depending on d
and the algorithm used. Would be nice to formalize this some more and officially add it to DynamicPPL.
Metadata
Metadata
Assignees
Labels
No labels