-
Notifications
You must be signed in to change notification settings - Fork 37
[Merged by Bors] - Introduce traits for contexts #286
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f9e753a
initial work
torfjelde f090ff5
added some missing implementations
torfjelde 4e36c55
formatting
torfjelde df5478f
Merge branch 'master' into tor/context-traits
torfjelde ee035cb
added some more functionality for context traits
torfjelde b7998bc
fixed PointwiseLikelihood
torfjelde 4e3e08f
fixed a doctest
torfjelde 9e23d4d
formatting
torfjelde 51209fd
Merge branch 'master' into tor/context-traits
yebai 7c9dc5e
make NodeTrait an abstract type
torfjelde e67089f
make matchingvalue work nicely with contexts
torfjelde 9479ce4
added a bunch of tests for the new trait system for contexts
torfjelde 1315d88
formatting
torfjelde 59b39f1
bump major version for Turing
torfjelde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,12 +35,27 @@ function tilde_assume(context::SamplingContext, right, vn, inds, vi) | |
| end | ||
|
|
||
| # Leaf contexts | ||
| tilde_assume(::DefaultContext, right, vn, inds, vi) = assume(right, vn, vi) | ||
| function tilde_assume(context::AbstractContext, args...) | ||
| return tilde_assume(NodeTrait(tilde_assume, context), context, args...) | ||
| end | ||
| function tilde_assume(::IsLeaf, context::AbstractContext, right, vn, vinds, vi) | ||
| return assume(right, vn, vi) | ||
| end | ||
| function tilde_assume(::IsParent, context::AbstractContext, args...) | ||
| return tilde_assume(childcontext(context), args...) | ||
| end | ||
|
|
||
| function tilde_assume(rng, context::AbstractContext, args...) | ||
| return tilde_assume(NodeTrait(tilde_assume, context), rng, context, args...) | ||
| end | ||
| function tilde_assume( | ||
| rng::Random.AbstractRNG, ::DefaultContext, sampler, right, vn, inds, vi | ||
| ::IsLeaf, rng, context::AbstractContext, sampler, right, vn, vinds, vi | ||
| ) | ||
| return assume(rng, sampler, right, vn, vi) | ||
| end | ||
| function tilde_assume(::IsParent, rng, context::AbstractContext, args...) | ||
| return tilde_assume(rng, childcontext(context), args...) | ||
| end | ||
|
|
||
| function tilde_assume(context::PriorContext{<:NamedTuple}, right, vn, inds, vi) | ||
| if haskey(context.vars, getsym(vn)) | ||
|
|
@@ -64,12 +79,6 @@ function tilde_assume( | |
| end | ||
| return tilde_assume(rng, PriorContext(), sampler, right, vn, inds, vi) | ||
| end | ||
| function tilde_assume(::PriorContext, right, vn, inds, vi) | ||
| return assume(right, vn, vi) | ||
| end | ||
| function tilde_assume(rng::Random.AbstractRNG, ::PriorContext, sampler, right, vn, inds, vi) | ||
| return assume(rng, sampler, right, vn, vi) | ||
| end | ||
|
|
||
| function tilde_assume(context::LikelihoodContext{<:NamedTuple}, right, vn, inds, vi) | ||
| if haskey(context.vars, getsym(vn)) | ||
|
|
@@ -102,18 +111,9 @@ function tilde_assume( | |
| return assume(rng, sampler, NoDist(right), vn, vi) | ||
| end | ||
|
|
||
| function tilde_assume(context::MiniBatchContext, right, vn, inds, vi) | ||
| return tilde_assume(context.context, right, vn, inds, vi) | ||
| end | ||
|
|
||
| function tilde_assume(rng, context::MiniBatchContext, sampler, right, vn, inds, vi) | ||
| return tilde_assume(rng, context.context, sampler, right, vn, inds, vi) | ||
| end | ||
|
|
||
| function tilde_assume(context::PrefixContext, right, vn, inds, vi) | ||
| return tilde_assume(context.context, right, prefix(context, vn), inds, vi) | ||
| end | ||
|
|
||
| function tilde_assume(rng, context::PrefixContext, sampler, right, vn, inds, vi) | ||
| return tilde_assume(rng, context.context, sampler, right, prefix(context, vn), inds, vi) | ||
| end | ||
|
|
@@ -162,16 +162,16 @@ function tilde_observe(context::SamplingContext, right, left, vi) | |
| end | ||
|
|
||
| # Leaf contexts | ||
| tilde_observe(::DefaultContext, right, left, vi) = observe(right, left, vi) | ||
| function tilde_observe(::DefaultContext, sampler, right, left, vi) | ||
| return observe(sampler, right, left, vi) | ||
| function tilde_observe(context::AbstractContext, args...) | ||
| return tilde_observe(NodeTrait(tilde_observe, context), context, args...) | ||
| end | ||
| tilde_observe(::IsLeaf, context::AbstractContext, args...) = observe(args...) | ||
| function tilde_observe(::IsParent, context::AbstractContext, args...) | ||
| return tilde_observe(childcontext(context), args...) | ||
| end | ||
|
|
||
| tilde_observe(::PriorContext, right, left, vi) = 0 | ||
| tilde_observe(::PriorContext, sampler, right, left, vi) = 0 | ||
| tilde_observe(::LikelihoodContext, right, left, vi) = observe(right, left, vi) | ||
| function tilde_observe(::LikelihoodContext, sampler, right, left, vi) | ||
| return observe(sampler, right, left, vi) | ||
| end | ||
|
|
||
| # `MiniBatchContext` | ||
| function tilde_observe(context::MiniBatchContext, right, left, vi) | ||
|
|
@@ -185,9 +185,6 @@ end | |
| function tilde_observe(context::PrefixContext, right, left, vname, vi) | ||
| return tilde_observe(context.context, right, left, prefix(context, vname), vi) | ||
| end | ||
| function tilde_observe(context::PrefixContext, right, left, vi) | ||
| return tilde_observe(context.context, right, left, vi) | ||
| end | ||
|
|
||
| """ | ||
| tilde_observe!(context, right, left, vname, vinds, vi) | ||
|
|
@@ -288,9 +285,28 @@ function dot_tilde_assume(context::SamplingContext, right, left, vn, inds, vi) | |
| end | ||
|
|
||
| # `DefaultContext` | ||
| function dot_tilde_assume(::DefaultContext, right, left, vns, inds, vi) | ||
| function dot_tilde_assume(context::AbstractContext, args...) | ||
| return dot_tilde_assume(NodeTrait(dot_tilde_assume, context), context, args...) | ||
| end | ||
| function dot_tilde_assume(rng, context::AbstractContext, args...) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO we should make We only did it this way to attempt to be non-breaking (it wasn't 😅 ), so might was well make the change |
||
| return dot_tilde_assume(rng, NodeTrait(dot_tilde_assume, context), context, args...) | ||
| end | ||
|
|
||
| function dot_tilde_assume(::IsLeaf, ::AbstractContext, right, left, vns, inds, vi) | ||
| return dot_assume(right, left, vns, vi) | ||
| end | ||
| function dot_tilde_assume( | ||
| ::IsLeaf, rng, ::AbstractContext, sampler, right, left, vns, inds, vi | ||
| ) | ||
| return dot_assume(rng, sampler, right, vns, left, vi) | ||
| end | ||
|
|
||
| function dot_tilde_assume(::IsParent, context::AbstractContext, args...) | ||
| return dot_tilde_assume(childcontext(context), args...) | ||
| end | ||
| function dot_tilde_assume(rng, ::IsParent, context::AbstractContext, args...) | ||
| return dot_tilde_assume(rng, childcontext(context), args...) | ||
| end | ||
|
|
||
| function dot_tilde_assume(rng, ::DefaultContext, sampler, right, left, vns, inds, vi) | ||
| return dot_assume(rng, sampler, right, vns, left, vi) | ||
|
|
@@ -371,25 +387,6 @@ function dot_tilde_assume( | |
| dot_tilde_assume(rng, PriorContext(), sampler, right, left, vn, inds, vi) | ||
| end | ||
| end | ||
| function dot_tilde_assume(context::PriorContext, right, left, vn, inds, vi) | ||
| return dot_assume(right, left, vn, vi) | ||
| end | ||
| function dot_tilde_assume( | ||
| rng::Random.AbstractRNG, context::PriorContext, sampler, right, left, vn, inds, vi | ||
| ) | ||
| return dot_assume(rng, sampler, right, vn, left, vi) | ||
| end | ||
|
|
||
| # `MiniBatchContext` | ||
| function dot_tilde_assume(context::MiniBatchContext, right, left, vn, inds, vi) | ||
| return dot_tilde_assume(context.context, right, left, vn, inds, vi) | ||
| end | ||
|
|
||
| function dot_tilde_assume( | ||
| rng, context::MiniBatchContext, sampler, right, left, vn, inds, vi | ||
| ) | ||
| return dot_tilde_assume(rng, context.context, sampler, right, left, vn, inds, vi) | ||
| end | ||
|
|
||
| # `PrefixContext` | ||
| function dot_tilde_assume(context::PrefixContext, right, left, vn, inds, vi) | ||
|
|
@@ -586,18 +583,16 @@ function dot_tilde_observe(context::SamplingContext, right, left, vi) | |
| end | ||
|
|
||
| # Leaf contexts | ||
| dot_tilde_observe(::DefaultContext, right, left, vi) = dot_observe(right, left, vi) | ||
| function dot_tilde_observe(::DefaultContext, sampler, right, left, vi) | ||
| return dot_observe(sampler, right, left, vi) | ||
| function dot_tilde_observe(context::AbstractContext, args...) | ||
| return dot_tilde_observe(NodeTrait(tilde_observe, context), context, args...) | ||
| end | ||
| dot_tilde_observe(::IsLeaf, ::AbstractContext, args...) = dot_observe(args...) | ||
| function dot_tilde_observe(::IsParent, context::AbstractContext, args...) | ||
| return dot_tilde_observe(childcontext(context), args...) | ||
| end | ||
|
|
||
| dot_tilde_observe(::PriorContext, right, left, vi) = 0 | ||
| dot_tilde_observe(::PriorContext, sampler, right, left, vi) = 0 | ||
| function dot_tilde_observe(context::LikelihoodContext, right, left, vi) | ||
| return dot_observe(right, left, vi) | ||
| end | ||
| function dot_tilde_observe(context::LikelihoodContext, sampler, right, left, vi) | ||
| return dot_observe(sampler, right, left, vi) | ||
| end | ||
|
|
||
| # `MiniBatchContext` | ||
| function dot_tilde_observe(context::MiniBatchContext, right, left, vi) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also very nice as it allows us to define something like a
CUDAContextin the future which will ensure that all the arguments are moved to the GPU before execution.