Skip to content

Commit

Permalink
Fix type inference of eltype(vi, spl) (Turing#2151) (#568)
Browse files Browse the repository at this point in the history
* Fix type inference of `eltype(vi, spl)` (Turing#2151)

* Update Project.toml

* Add test

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Fix test

---------

Co-authored-by: Tor Erlend Fjelde <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 24, 2024
1 parent 39751b1 commit c1d3636
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
3 changes: 1 addition & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"

version = "0.24.4"
version = "0.24.5"

[deps]
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
Expand Down
9 changes: 8 additions & 1 deletion src/abstract_varinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,14 @@ Determine the default `eltype` of the values returned by `vi[spl]`.
This method is considered legacy, and is likely to be deprecated in the future.
"""
function Base.eltype(vi::AbstractVarInfo, spl::Union{AbstractSampler,SampleFromPrior})
return eltype(Core.Compiler.return_type(getindex, Tuple{typeof(vi),typeof(spl)}))
T = Base.promote_op(getindex, typeof(vi), typeof(spl))
if T === Union{}
# In this case `getindex(vi, spl)` errors
# Let us throw a more descriptive error message
# Ref https://github.com/TuringLang/Turing.jl/issues/2151
return eltype(vi[spl])
end
return eltype(T)
end

# TODO: Should relax constraints on `vns` to be `AbstractVector{<:Any}` and just try to convert
Expand Down
2 changes: 1 addition & 1 deletion src/varinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ function getindex(vi::TypedVarInfo, spl::Sampler)
# Gets the ranges as a NamedTuple
ranges = _getranges(vi, spl)
# Calling getfield(ranges, f) gives all the indices in `vals` of the `vn`s with symbol `f` sampled by `spl` in `vi`
return vcat(_getindex(vi.metadata, ranges)...)
return reduce(vcat, _getindex(vi.metadata, ranges))
end
# Recursively builds a tuple of the `vals` of all the symbols
@generated function _getindex(metadata, ranges::NamedTuple{names}) where {names}
Expand Down
2 changes: 2 additions & 0 deletions test/turing/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
DynamicPPL = "366bfd00-2699-11ea-058f-f148b4cae6d8"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"

[compat]
DynamicPPL = "0.24"
ReverseDiff = "1.15"
Turing = "0.30"
julia = "1.7"
1 change: 1 addition & 0 deletions test/turing/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DynamicPPL
using Turing
using LinearAlgebra
using ReverseDiff

using Random
using Test
Expand Down
31 changes: 31 additions & 0 deletions test/turing/varinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,35 @@
@test vi.metadata.w.gids[1] == Set([hmc.selector])
@test vi.metadata.u.gids[1] == Set([hmc.selector]) =#
end

@testset "Turing#2151: eltype(vi, spl)" begin
# build data
t = 1:0.05:8
σ = 0.3
y = @. rand(sin(t) + Normal(0, σ))

@model function state_space(y, TT, ::Type{T}=Float64) where {T}
# Priors
α ~ Normal(y[1], 0.001)
τ ~ Exponential(1)
η ~ filldist(Normal(0, 1), TT - 1)
σ ~ Exponential(1)

# create latent variable
x = Vector{T}(undef, TT)
x[1] = α
for t in 2:TT
x[t] = x[t - 1] + η[t - 1] * τ
end

# measurement model
y ~ MvNormal(x, σ^2 * I)

return x
end

n = 10
model = state_space(y, length(t))
@test size(sample(model, NUTS(; adtype=AutoReverseDiff(true)), n), 1) == n
end
end

2 comments on commit c1d3636

@torfjelde
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/99754

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.24.5 -m "<description of version>" c1d363640e9e9aaeee73fff402b29b8de35b5918
git push origin v0.24.5

Please sign in to comment.