Skip to content

Fix zero-type of logjac for ReshapeTransform #851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 19, 2025
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
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.35.3"
version = "0.35.4"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
32 changes: 20 additions & 12 deletions src/distribution_wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,38 @@
) where {N}
return Distributions.rand!(rng, d.dist, x)
end
Distributions.logpdf(d::NoDist{<:Univariate}, ::Real) = 0
Distributions.logpdf(d::NoDist{<:Multivariate}, ::AbstractVector{<:Real}) = 0
function Distributions.logpdf(d::NoDist{<:Multivariate}, x::AbstractMatrix{<:Real})
return zeros(Int, size(x, 2))
function Distributions.logpdf(::NoDist{<:Univariate}, x::Real)
return zero(LogProbType)
end
function Distributions.logpdf(::NoDist{<:Multivariate}, x::AbstractVector{<:Real})
return zero(LogProbType)
end
function Distributions.logpdf(::NoDist{<:Multivariate}, x::AbstractMatrix{<:Real})
return zeros(LogProbType, size(x, 2))

Check warning on line 64 in src/distribution_wrappers.jl

View check run for this annotation

Codecov / codecov/patch

src/distribution_wrappers.jl#L63-L64

Added lines #L63 - L64 were not covered by tests
end
function Distributions.logpdf(::NoDist{<:Matrixvariate}, x::AbstractMatrix{<:Real})
return zero(LogProbType)
end
Distributions.logpdf(d::NoDist{<:Matrixvariate}, ::AbstractMatrix{<:Real}) = 0
Distributions.minimum(d::NoDist) = minimum(d.dist)
Distributions.maximum(d::NoDist) = maximum(d.dist)

Bijectors.logpdf_with_trans(d::NoDist{<:Univariate}, ::Real, ::Bool) = 0
function Bijectors.logpdf_with_trans(::NoDist{<:Univariate}, x::Real, ::Bool)
return zero(LogProbType)
end
function Bijectors.logpdf_with_trans(
d::NoDist{<:Multivariate}, ::AbstractVector{<:Real}, ::Bool
::NoDist{<:Multivariate}, x::AbstractVector{<:Real}, ::Bool
)
return 0
return zero(LogProbType)

Check warning on line 78 in src/distribution_wrappers.jl

View check run for this annotation

Codecov / codecov/patch

src/distribution_wrappers.jl#L78

Added line #L78 was not covered by tests
end
function Bijectors.logpdf_with_trans(
d::NoDist{<:Multivariate}, x::AbstractMatrix{<:Real}, ::Bool
::NoDist{<:Multivariate}, x::AbstractMatrix{<:Real}, ::Bool
)
return zeros(Int, size(x, 2))
return zeros(LogProbType, size(x, 2))

Check warning on line 83 in src/distribution_wrappers.jl

View check run for this annotation

Codecov / codecov/patch

src/distribution_wrappers.jl#L83

Added line #L83 was not covered by tests
end
function Bijectors.logpdf_with_trans(
d::NoDist{<:Matrixvariate}, ::AbstractMatrix{<:Real}, ::Bool
::NoDist{<:Matrixvariate}, x::AbstractMatrix{<:Real}, ::Bool
)
return 0
return zero(LogProbType)

Check warning on line 88 in src/distribution_wrappers.jl

View check run for this annotation

Codecov / codecov/patch

src/distribution_wrappers.jl#L88

Added line #L88 was not covered by tests
end

Bijectors.bijector(d::NoDist) = Bijectors.bijector(d.dist)
34 changes: 28 additions & 6 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ const NO_DEFAULT = NoDefault()
# A short-hand for a type commonly used in type signatures for VarInfo methods.
VarNameTuple = NTuple{N,VarName} where {N}

# TODO(mhauru) This is currently used in the transformation functions of NoDist,
# ReshapeTransform, and UnwrapSingletonTransform, and in VarInfo. We should also use it in
# SimpleVarInfo and maybe other places.
"""
The type for all log probability variables.

This is Float64 on 64-bit systems and Float32 on 32-bit systems.
"""
const LogProbType = float(Real)

"""
@addlogprob!(ex)

Expand Down Expand Up @@ -252,12 +262,16 @@ function (f::UnwrapSingletonTransform)(x)
return only(x)
end

Bijectors.with_logabsdet_jacobian(f::UnwrapSingletonTransform, x) = (f(x), 0)
function Bijectors.with_logabsdet_jacobian(f::UnwrapSingletonTransform, x)
return f(x), zero(LogProbType)
end

function Bijectors.with_logabsdet_jacobian(
inv_f::Bijectors.Inverse{<:UnwrapSingletonTransform}, x
)
f = inv_f.orig
return (reshape([x], f.input_size), 0)
result = reshape([x], f.input_size)
return result, zero(LogProbType)
end

"""
Expand Down Expand Up @@ -306,18 +320,26 @@ function (inv_f::Bijectors.Inverse{<:ReshapeTransform})(x)
return inverse(x)
end

Bijectors.with_logabsdet_jacobian(f::ReshapeTransform, x) = (f(x), 0)
function Bijectors.with_logabsdet_jacobian(f::ReshapeTransform, x)
return f(x), zero(LogProbType)
end

function Bijectors.with_logabsdet_jacobian(inv_f::Bijectors.Inverse{<:ReshapeTransform}, x)
return (inv_f(x), 0)
return inv_f(x), zero(LogProbType)
end

struct ToChol <: Bijectors.Bijector
uplo::Char
end

Bijectors.with_logabsdet_jacobian(f::ToChol, x) = (Cholesky(Matrix(x), f.uplo, 0), 0)
Bijectors.with_logabsdet_jacobian(::Bijectors.Inverse{<:ToChol}, y::Cholesky) = (y.UL, 0)
function Bijectors.with_logabsdet_jacobian(f::ToChol, x)
return Cholesky(Matrix(x), f.uplo, 0), zero(LogProbType)
end

function Bijectors.with_logabsdet_jacobian(::Bijectors.Inverse{<:ToChol}, y::Cholesky)
return y.UL, zero(LogProbType)
end

function Bijectors.with_logabsdet_jacobian(::Bijectors.Inverse{<:ToChol}, y)
return error(
"Inverse{ToChol} is only defined for Cholesky factorizations. " *
Expand Down
2 changes: 1 addition & 1 deletion src/varinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ end

# VarInfo

VarInfo(meta=Metadata()) = VarInfo(meta, Ref{Float64}(0.0), Ref(0))
VarInfo(meta=Metadata()) = VarInfo(meta, Ref{LogProbType}(0.0), Ref(0))

function TypedVarInfo(vi::VectorVarInfo)
new_metas = group_by_symbol(vi.metadata)
Expand Down