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

Move Libtask to an extension #75

Merged
merged 23 commits into from
Oct 12, 2023
Merged

Move Libtask to an extension #75

merged 23 commits into from
Oct 12, 2023

Conversation

FredericWantiez
Copy link
Member

Still a draft but we can track the progress on moving Libtask to its own extension

@FredericWantiez FredericWantiez marked this pull request as draft March 30, 2023 21:36
@FredericWantiez
Copy link
Member Author

We probably need to think about the handling of the rng in the non-libtask case

src/AdvancedPS.jl Outdated Show resolved Hide resolved
ext/AdvancedPSLibtaskExt.jl Outdated Show resolved Hide resolved
ext/LibtaskExt.jl Outdated Show resolved Hide resolved
ext/LibtaskExt.jl Outdated Show resolved Hide resolved
ext/LibtaskExt.jl Outdated Show resolved Hide resolved
src/AdvancedPS.jl Outdated Show resolved Hide resolved
ext/LibtaskExt.jl Outdated Show resolved Hide resolved
ext/LibtaskExt.jl Outdated Show resolved Hide resolved
ext/LibtaskExt.jl Outdated Show resolved Hide resolved
src/AdvancedPS.jl Outdated Show resolved Hide resolved
@github-actions
Copy link
Contributor

github-actions bot commented Jul 20, 2023

Pull Request Test Coverage Report for Build 6448601669

  • 85 of 93 (91.4%) changed or added relevant lines in 5 files are covered.
  • 2 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.7%) to 95.61%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/AdvancedPS.jl 0 2 0.0%
ext/AdvancedPSLibtaskExt.jl 77 83 92.77%
Files with Coverage Reduction New Missed Lines %
src/container.jl 1 99.21%
src/model.jl 1 87.5%
Totals Coverage Status
Change from base Build 6423814092: -0.7%
Covered Lines: 392
Relevant Lines: 410

💛 - Coveralls

@codecov
Copy link

codecov bot commented Jul 20, 2023

Codecov Report

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

Comparison is base (46ed087) 96.32% compared to head (5626791) 95.60%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #75      +/-   ##
==========================================
- Coverage   96.32%   95.60%   -0.72%     
==========================================
  Files           7        8       +1     
  Lines         381      410      +29     
==========================================
+ Hits          367      392      +25     
- Misses         14       18       +4     
Files Coverage Δ
src/container.jl 99.21% <ø> (+2.26%) ⬆️
src/model.jl 87.50% <100.00%> (-8.16%) ⬇️
src/pgas.jl 96.29% <ø> (ø)
src/resampling.jl 97.01% <ø> (ø)
src/rng.jl 90.00% <100.00%> (ø)
src/smc.jl 97.36% <100.00%> (-0.71%) ⬇️
src/AdvancedPS.jl 33.33% <0.00%> (-66.67%) ⬇️
ext/AdvancedPSLibtaskExt.jl 92.77% <92.77%> (ø)

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


Base.copy(model::LibtaskModel) = LibtaskModel(model.f, copy(model.ctask))

const LibtaskTrace{R} = AdvancedPS.Trace{<:LibtaskModel,R}
Copy link
Member

@yebai yebai Aug 4, 2023

Choose a reason for hiding this comment

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

I don't think we can access this LibtaskModel externally. Hence, users can not use this libtask extension even when Libtask is loaded. To fix this, we can consider overloading a function defined in AdvancedPS.

Suggested change
const LibtaskTrace{R} = AdvancedPS.Trace{<:LibtaskModel,R}
const LibtaskTrace{R} = AdvancedPS.Trace{<:LibtaskModel,R}
AdvancedPS.Trace(Libtask, f, args; __rng__ = Random.default_rng()) = AdvancedPS.Trace(LibtaskModel(f, args...), rng=__rng__)

Copy link
Member

Choose a reason for hiding this comment

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

I think it's not possible (for general Julia versions - maybe at all?) to dispatch on modules. One will be able to use them e.g. as type parameters only in 1.10: JuliaLang/julia#47749

Copy link
Member

Choose a reason for hiding this comment

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

It's possible to access types by retrieving the extension module with get_extension. But that does not lead to a good API IMO and also adding accessible types is not the intended use case of extensions.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, @devmotion -- that's very helpful to know. However, in this case, we can dispatch any type. For example:

Suggested change
const LibtaskTrace{R} = AdvancedPS.Trace{<:LibtaskModel,R}
const LibtaskTrace{R} = AdvancedPS.Trace{<:LibtaskModel,R}
AdvancedPS.Trace(Val{:libtask}, f, args; __rng__ = Random.default_rng()) = AdvancedPS.Trace(LibtaskModel(f, args...), rng=__rng__)

Copy link
Member Author

@FredericWantiez FredericWantiez Aug 5, 2023

Choose a reason for hiding this comment

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

I wanted to hide all the internals away from the user, not sure they should know about what Trace is. The main package implements sample(::AbstractStateSpace) and the extension overloads it with sample(::AbstractModel).

This works only if you load Libtask (and enable the extension):

struct Model <: AbstractMCMC.AbstractModel end

function (model::Model)(rng::AbstractRNG)
    x = rand(...)
end 

sample(rng, Model(), PG(100), 1_000)

# # Test task copy version of trace
# trng = AdvancedPS.TracedRNG()
# tmodel = AdvancedPS.GenericModel(f2, trng)
# tr = AdvancedPS.Trace(tmodel, trng)
Copy link
Member

Choose a reason for hiding this comment

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

With the additional constructor proposed in https://github.com/TuringLang/AdvancedPS.jl/pull/75/files#r1284737656, we should be able to uncomment these tests.

@yebai yebai marked this pull request as ready for review August 4, 2023 18:46
@yebai
Copy link
Member

yebai commented Aug 4, 2023

Excellent work -- I left some suggestions above.

@FredericWantiez
Copy link
Member Author

FredericWantiez commented Oct 1, 2023

Just realized while fixing some of tests that the Sampler API is a bit broken i.e there is no way to sample a StateSpaceModel with PG if one wanted to. The issues is here:

update_ref!(ref, pc)

update_ref! should probably dispatch on the type of the sampler somehow.

@FredericWantiez
Copy link
Member Author

Something like this would actually would use resample the ancestor particle for both

chains_pg = sample(model, pg, Nₛ);
chains_pgas = sample(model, pgas, Nₛ);

@yebai
Copy link
Member

yebai commented Oct 5, 2023

Something like this would actually would use resample the ancestor particle for both

Not sure I follow here, can you clarity?

examples/particle-gibbs/script.jl Outdated Show resolved Hide resolved
examples/particle-gibbs/script.jl Outdated Show resolved Hide resolved
examples/particle-gibbs/script.jl Outdated Show resolved Hide resolved
examples/particle-gibbs/script.jl Outdated Show resolved Hide resolved
@yebai yebai merged commit a5216ea into master Oct 12, 2023
13 of 20 checks passed
@yebai yebai deleted the fred/extension branch October 12, 2023 13:32
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