Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/logdensityfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,13 @@ end
@inline function LogDensityProblems.logdensity(
ldf::LogDensityFunction, params::AbstractVector{<:Real}
)
if length(params) != ldf._dim
throw(
ArgumentError(
"The length of the input vector is $(length(params)), but the LogDensityFunction expects a vector of length $(ldf._dim) based on the ranges that were extracted when the LogDensityFunction was constructed.",
),
)
end
return logdensity_internal(
params,
ldf.model,
Expand Down
12 changes: 12 additions & 0 deletions test/logdensityfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ end
@test LogDensityProblems.dimension(ldf) == 4
end

@testset "logdensity checks the input length" begin
@model function m3()
x ~ Normal()
y ~ Normal()
return nothing
end
ldf = DynamicPPL.LogDensityFunction(m3())
@test LogDensityProblems.logdensity(ldf, zeros(2)) isa Real
@test_throws ArgumentError LogDensityProblems.logdensity(ldf, zeros(1))
@test_throws ArgumentError LogDensityProblems.logdensity(ldf, zeros(3))
end

@testset "capabilities" begin
@model f() = x ~ Normal()
model = f()
Expand Down
Loading