-
Notifications
You must be signed in to change notification settings - Fork 36
Accumulator miscellanea: Subset, merge, acclogp, and LogProbAccumulator #999
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
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a29b953
logjac accumulator
penelopeysm d6c9cfa
Fix tests
penelopeysm e671a56
Fix a whole bunch of stuff
penelopeysm 5a4b01b
Fix final tests
penelopeysm 974c282
Fix docs
penelopeysm 60b6863
Fix docs/doctests
penelopeysm 53a2f61
Fix maths in LogJacobianAccumulator docstring
penelopeysm a47641c
Twiddle with a comment
penelopeysm 10de51f
Add changelog
penelopeysm e9bf50b
Simplify accs with LogProbAccumulator
mhauru f983da5
Replace + with accumulate for LogProbAccs
mhauru bc51d62
Introduce merge and subset for accs
mhauru 2dc61a8
Improve acc tests
mhauru e852b5e
Fix docstring typo.
mhauru e8c5a70
Merge remote-tracking branch 'origin/breaking' into mhauru/logprobacc
mhauru 1aac709
Fix merge
mhauru 85515a9
Merge branch 'breaking' into mhauru/logprobacc
penelopeysm f0519df
Merge branch 'breaking' into mhauru/logprobacc
penelopeysm 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
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 |
---|---|---|
|
@@ -30,6 +30,13 @@ To be able to work with multi-threading, it should also implement: | |
- `split(acc::T)` | ||
- `combine(acc::T, acc2::T)` | ||
|
||
If two accumulators of the same type should be merged in some non-trivial way, other than | ||
always keeping the second one over the first, `merge(acc1::T, acc2::T)` should be defined. | ||
|
||
If limiting the accumulator to a subset of `VarName`s is a meaningful operation and should | ||
do something other than copy the original accumulator, then | ||
`subset(acc::T, vns::AbstractVector{<:VarnName})` should be defined.` | ||
|
||
See the documentation for each of these functions for more details. | ||
""" | ||
abstract type AbstractAccumulator end | ||
|
@@ -113,6 +120,24 @@ used by various AD backends, should implement a method for this function. | |
""" | ||
convert_eltype(::Type, acc::AbstractAccumulator) = acc | ||
|
||
""" | ||
subset(acc::AbstractAccumulator, vns::AbstractVector{<:VarName}) | ||
|
||
Return a new accumulator that only contains the information for the `VarName`s in `vns`. | ||
|
||
By default returns a copy of `acc`. Subtypes should override this behaviour as needed. | ||
""" | ||
subset(acc::AbstractAccumulator, ::AbstractVector{<:VarName}) = copy(acc) | ||
|
||
""" | ||
merge(acc1::AbstractAccumulator, acc2::AbstractAccumulator) | ||
|
||
Merge two accumulators of the same type. Returns a new accumulator of the same type. | ||
|
||
By default returns a copy of `acc2`. Subtypes should override this behaviour as needed. | ||
""" | ||
Base.merge(acc1::AbstractAccumulator, acc2::AbstractAccumulator) = copy(acc2) | ||
|
||
""" | ||
AccumulatorTuple{N,T<:NamedTuple} | ||
|
||
|
@@ -158,6 +183,50 @@ function Base.convert(::Type{AccumulatorTuple{N,T}}, accs::AccumulatorTuple{N}) | |
return AccumulatorTuple(convert(T, accs.nt)) | ||
end | ||
|
||
""" | ||
subset(at::AccumulatorTuple, vns::AbstractVector{<:VarName}) | ||
|
||
Replace each accumulator `acc` in `at` with `subset(acc, vns)`. | ||
""" | ||
function subset(at::AccumulatorTuple, vns::AbstractVector{<:VarName}) | ||
return AccumulatorTuple(map(Base.Fix2(subset, vns), at.nt)) | ||
end | ||
|
||
""" | ||
_joint_keys(nt1::NamedTuple, nt2::NamedTuple) | ||
|
||
A helper function that returns three tuples of keys given two `NamedTuple`s: | ||
The keys only in `nt1`, only in `nt2`, and in both, and in that order. | ||
|
||
Implemented as a generated function to enable constant propagation of the result in `merge`. | ||
""" | ||
@generated function _joint_keys( | ||
nt1::NamedTuple{names1}, nt2::NamedTuple{names2} | ||
) where {names1,names2} | ||
only_in_nt1 = tuple(setdiff(names1, names2)...) | ||
only_in_nt2 = tuple(setdiff(names2, names1)...) | ||
in_both = tuple(intersect(names1, names2)...) | ||
return :($only_in_nt1, $only_in_nt2, $in_both) | ||
end | ||
|
||
""" | ||
merge(at1::AccumulatorTuple, at2::AccumulatorTuple) | ||
|
||
Merge two `AccumulatorTuple`s. | ||
|
||
For any `accumulator_name` that exists in both `at1` and `at2`, we call `merge` on the two | ||
accumulators themselves. Other accumulators are copied. | ||
""" | ||
function Base.merge(at1::AccumulatorTuple, at2::AccumulatorTuple) | ||
keys_in_at1, keys_in_at2, keys_in_both = _joint_keys(at1.nt, at2.nt) | ||
accs_in_at1 = (getfield(at1.nt, key) for key in keys_in_at1) | ||
accs_in_at2 = (getfield(at2.nt, key) for key in keys_in_at2) | ||
accs_in_both = ( | ||
merge(getfield(at1.nt, key), getfield(at2.nt, key)) for key in keys_in_both | ||
) | ||
return AccumulatorTuple(accs_in_at1..., accs_in_both..., accs_in_at2...) | ||
end | ||
Comment on lines
+203
to
+228
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. I checked, and this implementation causes only one allocation of 32 bits, due to a new AccumulatorTuple being created. |
||
|
||
""" | ||
setacc!!(at::AccumulatorTuple, acc::AbstractAccumulator) | ||
|
||
|
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
What's the difference between
combine
andmerge
?I guess this follows on from our conversation on Slack. but I wonder if there a way to restrict the call on subset / merge to only the accumulators we care about? basically is it possible for us to circumvent the need to call
subset
on the entire AccumulatorTuple and thus avoid including slightly weird implementations ofsubset
on the logp accumulators? Mainly inspired by how you avoided implementing these for e.g. PLDAcc.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.
combine
andmerge
probably do the same thing for most accumulators, but I'm not sure they have to for all accumulators.combine
has the restriction thatcombine(acc, split(acc)) == acc
, whereasmerge
could do anything that is desirable on a call tomerge
.I don't know how to specify a subset of accumulators to call merge/subset on in a way that is any simpler than this implementation. Even before this PR, we used to call
copy
on accs in merge and subset. Now that has just been shifted to be a method ofAbstractAccumulator
that is the fallback for what to do when a subtype doesn't specify anything else.