Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# DynamicPPL Changelog

## 0.39.1

`LogDensityFunction` now allows you to call `logdensity_and_gradient(ldf, x)` with `AbstractVector`s `x` that are not plain Vectors (they will be converted internally before calculating the gradient).

## 0.39.0

### Breaking changes
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.39.0"
version = "0.39.1"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
8 changes: 8 additions & 0 deletions src/logdensityfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ struct LogDensityFunction{
F<:Function,
N<:NamedTuple,
ADP<:Union{Nothing,DI.GradientPrep},
# type of the vector passed to logdensity functions
X<:AbstractVector,
}
model::M
adtype::AD
Expand Down Expand Up @@ -202,12 +204,17 @@ struct LogDensityFunction{
typeof(getlogdensity),
typeof(all_iden_ranges),
typeof(prep),
typeof(x),
}(
model, adtype, getlogdensity, all_iden_ranges, all_ranges, prep, dim
)
end
end

function _get_input_vector_type(::LogDensityFunction{T,M,A,G,I,P,X}) where {T,M,A,G,I,P,X}
return X
end

###################################
# LogDensityProblems.jl interface #
###################################
Expand Down Expand Up @@ -265,6 +272,7 @@ end
function LogDensityProblems.logdensity_and_gradient(
ldf::LogDensityFunction{Tlink}, params::AbstractVector{<:Real}
) where {Tlink}
params = convert(_get_input_vector_type(ldf), params)
return DI.value_and_gradient(
LogDensityAt{Tlink}(
ldf.model, ldf._getlogdensity, ldf._iden_varname_ranges, ldf._varname_ranges
Expand Down
19 changes: 19 additions & 0 deletions test/logdensityfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ end
end
end

@testset "logdensity_and_gradient with views" begin
# This test ensures that you can call `logdensity_and_gradient` with an array
# type that isn't the same as the one used in the gradient preparation.
@model function f()
x ~ Normal()
return y ~ Normal()
end
@testset "$adtype" for adtype in test_adtypes
x = randn(2)
ldf = LogDensityFunction(f(); adtype)
logp, grad = LogDensityProblems.logdensity_and_gradient(ldf, x)
logp_view, grad_view = LogDensityProblems.logdensity_and_gradient(
ldf, (@view x[:])
)
@test logp == logp_view
@test grad == grad_view
end
end

# Test that various different ways of specifying array types as arguments work with all
# ADTypes.
@testset "Array argument types" begin
Expand Down