Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7cddac7
Fast Log Density Function
penelopeysm Nov 5, 2025
5ed4295
Make it work with AD
penelopeysm Nov 6, 2025
e199520
Optimise performance for identity VarNames
penelopeysm Nov 6, 2025
4cefaca
Mark `get_range_and_linked` as having zero derivative
penelopeysm Nov 6, 2025
6dfd106
Update comment
penelopeysm Nov 6, 2025
41ee7f3
make AD testing / benchmarking use FastLDF
penelopeysm Nov 6, 2025
22e32a6
Fix tests
penelopeysm Nov 6, 2025
79cc128
Optimise away `make_evaluate_args_and_kwargs`
penelopeysm Nov 6, 2025
f7c6a78
const func annotation
penelopeysm Nov 6, 2025
b1a7650
Disable benchmarks on non-typed-Metadata-VarInfo
penelopeysm Nov 6, 2025
e60873a
Fix `_evaluate!!` correctly to handle submodels
penelopeysm Nov 6, 2025
fa0664e
Actually fix submodel evaluate
penelopeysm Nov 6, 2025
09a1fbb
Document thoroughly and organise code
penelopeysm Nov 6, 2025
7306ba4
Support more VarInfos, make it thread-safe (?)
penelopeysm Nov 6, 2025
53bccc1
fix bug in parsing ranges from metadata/VNV
penelopeysm Nov 6, 2025
30b9247
Fix get_param_eltype for TSVI
penelopeysm Nov 6, 2025
316937a
Disable Enzyme benchmark
penelopeysm Nov 6, 2025
075cee8
Don't override _evaluate!!, that breaks ForwardDiff (sometimes)
penelopeysm Nov 6, 2025
5f5a92c
Move FastLDF to experimental for now
penelopeysm Nov 6, 2025
0716de5
Fix imports, add tests, etc
penelopeysm Nov 6, 2025
cd2461e
More test fixes
penelopeysm Nov 6, 2025
1b8b873
Fix imports / tests
penelopeysm Nov 6, 2025
ff5680d
Remove AbstractFastEvalContext
penelopeysm Nov 6, 2025
500d5ac
Changelog and patch bump
penelopeysm Nov 6, 2025
e560c30
Add correctness tests, fix imports
penelopeysm Nov 6, 2025
22e9dbe
Merge branch 'main' into py/fastldf
penelopeysm Nov 6, 2025
c3bdcd0
Merge branch 'main' into py/fastldf
penelopeysm Nov 6, 2025
86d8a73
Concretise parameter vector in tests
penelopeysm Nov 8, 2025
4ec0c72
Merge branch 'main' into py/fastldf
penelopeysm Nov 8, 2025
4b324b0
Add zero-allocation tests
penelopeysm Nov 9, 2025
c55171b
Add Chairmarks as test dep
penelopeysm Nov 9, 2025
77deae9
Disable allocations tests on multi-threaded
penelopeysm Nov 9, 2025
8715446
Fast InitContext (#1125)
penelopeysm Nov 10, 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
10 changes: 10 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# DynamicPPL Changelog

## 0.38.10

Added `DynamicPPL.Experimental.FastLDF`, a version of `LogDensityFunction` that provides performance improvements on the order of 2–10× for both model evaluation as well as automatic differentiation.
Exact speedups depend on the model size: larger models have less significant speedups because the bulk of the work is done in calls to `logpdf`.

Please note that `FastLDF` is currently considered internal and its API may change without warning.
We intend to replace `LogDensityFunction` with `FastLDF` in a release in the near future, but until then we recommend not using it.

For more information about `FastLDF`, please see https://github.com/TuringLang/DynamicPPL.jl/pull/1113 as well as the `src/fasteval.jl` file, which contains extensive comments.

## 0.38.9

Remove warning when using Enzyme as the AD backend.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
version = "0.38.9"
version = "0.38.10"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
13 changes: 6 additions & 7 deletions ext/DynamicPPLEnzymeCoreExt.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
module DynamicPPLEnzymeCoreExt

if isdefined(Base, :get_extension)
using DynamicPPL: DynamicPPL
using EnzymeCore
else
using ..DynamicPPL: DynamicPPL
using ..EnzymeCore
end
using DynamicPPL: DynamicPPL
using EnzymeCore

# Mark is_transformed as having 0 derivative. The `nothing` return value is not significant, Enzyme
# only checks whether such a method exists, and never runs it.
@inline EnzymeCore.EnzymeRules.inactive(::typeof(DynamicPPL.is_transformed), args...) =
nothing
# Likewise for get_range_and_linked.
@inline EnzymeCore.EnzymeRules.inactive(
::typeof(DynamicPPL.Experimental.get_range_and_linked), args...
) = nothing

end
3 changes: 3 additions & 0 deletions ext/DynamicPPLMooncakeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ using Mooncake: Mooncake

# This is purely an optimisation.
Mooncake.@zero_derivative Mooncake.DefaultCtx Tuple{typeof(is_transformed),Vararg}
Mooncake.@zero_derivative Mooncake.DefaultCtx Tuple{
typeof(DynamicPPL.Experimental.get_range_and_linked),Vararg
}

end # module
38 changes: 20 additions & 18 deletions src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -718,14 +718,15 @@ end
# TODO(mhauru) matchingvalue has methods that can accept both types and values. Why?
# TODO(mhauru) This function needs a more comprehensive docstring.
"""
matchingvalue(vi, value)
matchingvalue(param_eltype, value)
Convert the `value` to the correct type for the `vi` object.
Convert the `value` to the correct type, given the element type of the parameters
being used to evaluate the model.
"""
function matchingvalue(vi, value)
function matchingvalue(param_eltype, value)
T = typeof(value)
if hasmissing(T)
_value = convert(get_matching_type(vi, T), value)
_value = convert(get_matching_type(param_eltype, T), value)
# TODO(mhauru) Why do we make a deepcopy, even though in the !hasmissing branch we
# are happy to return `value` as-is?
if _value === value
Expand All @@ -738,29 +739,30 @@ function matchingvalue(vi, value)
end
end

function matchingvalue(vi, value::FloatOrArrayType)
return get_matching_type(vi, value)
function matchingvalue(param_eltype, value::FloatOrArrayType)
return get_matching_type(param_eltype, value)
end
function matchingvalue(vi, ::TypeWrap{T}) where {T}
return TypeWrap{get_matching_type(vi, T)}()
function matchingvalue(param_eltype, ::TypeWrap{T}) where {T}
return TypeWrap{get_matching_type(param_eltype, T)}()
end

# TODO(mhauru) This function needs a more comprehensive docstring. What is it for?
"""
get_matching_type(vi, ::TypeWrap{T}) where {T}
get_matching_type(param_eltype, ::TypeWrap{T}) where {T}
Get the specialized version of type `T` for `vi`.
Get the specialized version of type `T`, given an element type of the parameters
being used to evaluate the model.
"""
get_matching_type(_, ::Type{T}) where {T} = T
function get_matching_type(vi, ::Type{<:Union{Missing,AbstractFloat}})
return Union{Missing,float_type_with_fallback(eltype(vi))}
function get_matching_type(param_eltype, ::Type{<:Union{Missing,AbstractFloat}})
return Union{Missing,float_type_with_fallback(param_eltype)}
end
function get_matching_type(vi, ::Type{<:AbstractFloat})
return float_type_with_fallback(eltype(vi))
function get_matching_type(param_eltype, ::Type{<:AbstractFloat})
return float_type_with_fallback(param_eltype)
end
function get_matching_type(vi, ::Type{<:Array{T,N}}) where {T,N}
return Array{get_matching_type(vi, T),N}
function get_matching_type(param_eltype, ::Type{<:Array{T,N}}) where {T,N}
return Array{get_matching_type(param_eltype, T),N}
end
function get_matching_type(vi, ::Type{<:Array{T}}) where {T}
return Array{get_matching_type(vi, T)}
function get_matching_type(param_eltype, ::Type{<:Array{T}}) where {T}
return Array{get_matching_type(param_eltype, T)}
end
2 changes: 1 addition & 1 deletion src/contexts/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ struct InitFromParams{P,S<:Union{AbstractInitStrategy,Nothing}} <: AbstractInitS
function InitFromParams(
params::NamedTuple, fallback::Union{AbstractInitStrategy,Nothing}=InitFromPrior()
)
return InitFromParams(to_varname_dict(params), fallback)
return new{typeof(params),typeof(fallback)}(params, fallback)
end
end
function init(rng::Random.AbstractRNG, vn::VarName, dist::Distribution, p::InitFromParams)
Expand Down
2 changes: 2 additions & 0 deletions src/experimental.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Experimental

using DynamicPPL: DynamicPPL

include("fasteval.jl")

# This file only defines the names of the functions, and their docstrings. The actual implementations are in `ext/DynamicPPLJETExt.jl`, since we don't want to depend on JET.jl other than as a weak dependency.
"""
is_suitable_varinfo(model::Model, varinfo::AbstractVarInfo; kwargs...)
Expand Down
Loading