Skip to content
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

Fix type inference of eltype(vi, spl) (Turing#2151) #568

Merged
merged 7 commits into from
Jan 24, 2024

Conversation

devmotion
Copy link
Member

@devmotion devmotion commented Jan 6, 2024

Fixes the error in TuringLang/Turing.jl#2151 on Julia 1.10 for me:

using Turing, ReverseDiff, LinearAlgebra

# 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_iter = 1500
model = state_space(y, length(t))
chns = sample(model, NUTS(n_iter * 2, 0.8; adtype = AutoReverseDiff(true)), n_iter);

cc @sjwild

@@ -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))
Copy link
Member Author

Choose a reason for hiding this comment

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

This fixes the ReverseDiff issue but probably reduces performance in cases where vcat with multiple arguments is implemented in an optimized way.

@yebai yebai requested a review from torfjelde January 11, 2024 13:31
@torfjelde
Copy link
Member

This looks good to me, but there's something weird happening with the tests; looking into this.

@devmotion Could you bump patch version?

Copy link
Contributor

Pull Request Test Coverage Report for Build 7583034714

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.5%) to 79.326%

Totals Coverage Status
Change from base Build 7506113239: -0.5%
Covered Lines: 2329
Relevant Lines: 2936

💛 - Coveralls

Copy link

codecov bot commented Jan 19, 2024

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (c6e3b6b) 79.64% compared to head (4ff8324) 84.28%.
Report is 1 commits behind head on master.

Files Patch % Lines
src/abstract_varinfo.jl 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #568      +/-   ##
==========================================
+ Coverage   79.64%   84.28%   +4.63%     
==========================================
  Files          26       26              
  Lines        2938     3175     +237     
==========================================
+ Hits         2340     2676     +336     
+ Misses        598      499      -99     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@coveralls
Copy link

coveralls commented Jan 19, 2024

Pull Request Test Coverage Report for Build 7628589803

  • 4 of 5 (80.0%) changed or added relevant lines in 2 files are covered.
  • 25 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.02%) to 84.635%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/abstract_varinfo.jl 3 4 75.0%
Files with Coverage Reduction New Missed Lines %
src/model.jl 1 87.88%
src/simple_varinfo.jl 5 79.64%
src/varinfo.jl 6 85.6%
src/threadsafe.jl 13 44.23%
Totals Coverage Status
Change from base Build 7615074409: 0.02%
Covered Lines: 2677
Relevant Lines: 3163

💛 - Coveralls

@devmotion
Copy link
Member Author

I think TuringLang/Turing.jl/pull/2156 will fix the test issues.

test/turing/varinfo.jl Outdated Show resolved Hide resolved
test/turing/varinfo.jl Outdated Show resolved Hide resolved
test/turing/varinfo.jl Outdated Show resolved Hide resolved
devmotion and others added 2 commits January 23, 2024 16:52
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@devmotion
Copy link
Member Author

@torfjelde The PR is ready for another round of review 🙂

Copy link
Member

@torfjelde torfjelde left a comment

Choose a reason for hiding this comment

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

LGTM:)

@devmotion devmotion enabled auto-merge January 24, 2024 00:15
@devmotion devmotion added this pull request to the merge queue Jan 24, 2024
Merged via the queue into master with commit c1d3636 Jan 24, 2024
14 checks passed
@devmotion devmotion deleted the dw/eltype_union branch January 24, 2024 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants