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

Handle nested PrefixContext #787

Merged
merged 6 commits into from
Jan 27, 2025
Merged

Handle nested PrefixContext #787

merged 6 commits into from
Jan 27, 2025

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Jan 25, 2025

This PR introduces code to better handle PrefixContext nested within arbitrary chains of contexts.

PrefixContext > AbstractContext > PrefixContext

Correctly handles the case where there are multiple PrefixContexts in a chain that are not contiguous, i.e. point (3) in #786.

julia> using DynamicPPL

julia> p3 = PrefixContext{:a}(SamplingContext(PrefixContext{:b}(DefaultContext())))
PrefixContext{:a, SamplingContext{SampleFromPrior, PrefixContext{:b, DefaultContext}, Random.TaskLocalRNG}}(SamplingContext{SampleFromPrior, PrefixContext{:b, DefaultContext}, Random.TaskLocalRNG}(Random.TaskLocalRNG(), SampleFromPrior(), PrefixContext{:b, DefaultContext}(DefaultContext())))

julia> prefix(p3, @varname(x))
b.a.x  # previously this returned a.x

(Note, I still think this should return a.b.x not b.a.x (cf point (2) in #786) but I've refrained from changing this behaviour in this PR because it would be a breaking change.)

AbstractContext > PrefixContext

Correctly handles the case where PrefixContext is not the outermost context, i.e. point (4) in #786.

julia> p4 = SamplingContext(PrefixContext{:a}(DefaultContext()))
SamplingContext{SampleFromPrior, PrefixContext{:a, DefaultContext}, Random.TaskLocalRNG}(Random.TaskLocalRNG(), SampleFromPrior(), PrefixContext{:a, DefaultContext}(DefaultContext()))

julia> prefix(p4, @varname(x))
a.x  # previously this errored

DebugContext > PrefixContext

This also makes sure to prefix variables accordingly inside check_model_and_trace.

Fixes #785:

using DynamicPPL, Distributions

@model function submodel()
  x ~ Normal(0, 1)
  return x
end

@model function parentmodel()
  x1 ~ to_submodel(DynamicPPL.prefix(submodel(), :a), false)
  x2 ~ to_submodel(DynamicPPL.prefix(submodel(), :b), false)
  return x1 + x2
end

DynamicPPL.DebugUtils.check_model_and_trace(parentmodel(); error_on_failure=true)
# used to error, now passes successfully

ValuesAsInModelContext > PrefixContext

Finally, values_as_in_model correctly picks up prefixes now:

Fixes #788

using DynamicPPL, Distributions

@model function submodel()
  x ~ Normal(0, 1)
  return x
end

@model function parentmodel()
  x1 ~ to_submodel(prefix(submodel(), :a), false)
  x2 ~ to_submodel(prefix(submodel(), :b), false)
  return x1 + x2
end

m = parentmodel()
vi = VarInfo(m)
values_as_in_model(m, false, vi)
# OrderedDict{Any, Any} with 2 entries:
#   a.x => 0.640188
#   b.x => 0.477075
# (used to only have a single key `x`)

@penelopeysm penelopeysm marked this pull request as draft January 25, 2025 17:55
Copy link

codecov bot commented Jan 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.17%. Comparing base (727da63) to head (47048c2).
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #787      +/-   ##
==========================================
+ Coverage   86.16%   86.17%   +0.01%     
==========================================
  Files          36       36              
  Lines        4301     4305       +4     
==========================================
+ Hits         3706     3710       +4     
  Misses        595      595              

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

@coveralls
Copy link

coveralls commented Jan 25, 2025

Pull Request Test Coverage Report for Build 12970005900

Details

  • 6 of 16 (37.5%) changed or added relevant lines in 3 files are covered.
  • 4 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.01%) to 86.259%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/debug_utils.jl 0 10 0.0%
Files with Coverage Reduction New Missed Lines %
src/contexts.jl 1 31.77%
src/varinfo.jl 3 84.48%
Totals Coverage Status
Change from base Build 12908348905: 0.01%
Covered Lines: 3710
Relevant Lines: 4301

💛 - Coveralls

Comment on lines -276 to 282
function prefix(::PrefixContext{Prefix}, vn::VarName{Sym}) where {Prefix,Sym}
if @generated
return :(VarName{$(QuoteNode(Symbol(Prefix, PREFIX_SEPARATOR, Sym)))}(getoptic(vn)))
else
VarName{Symbol(Prefix, PREFIX_SEPARATOR, Sym)}(getoptic(vn))
end
# TODO(penelopeysm): Prefixing arguably occurs the wrong way round here
function prefix(ctx::PrefixContext{Prefix}, vn::VarName{Sym}) where {Prefix,Sym}
return prefix(
childcontext(ctx), VarName{Symbol(Prefix, PREFIX_SEPARATOR, Sym)}(getoptic(vn))
)
end
Copy link
Member Author

Choose a reason for hiding this comment

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

performance

using DynamicPPL, Distributions, Chairmarks

@model function submodel()
    x ~ Normal(0, 1)
end

@model function parentmodel()
    x1 ~ to_submodel(submodel(), true)
end

m = parentmodel()

@be VarInfo(m)
# Current master
Benchmark: 3827 samples with 2 evaluations
 min    10.667 μs (171 allocs: 8.609 KiB)
 median 11.042 μs (171 allocs: 8.609 KiB)
 mean   12.263 μs (171 allocs: 8.609 KiB, 0.03% gc time)
 max    4.467 ms (171 allocs: 8.609 KiB, 98.57% gc time)

# This PR
julia> @be VarInfo(m)
Benchmark: 3749 samples with 2 evaluations
 min    11.209 μs (171 allocs: 8.609 KiB)
 median 11.729 μs (171 allocs: 8.609 KiB)
 mean   12.650 μs (171 allocs: 8.609 KiB, 0.03% gc time)
 max    3.150 ms (171 allocs: 8.609 KiB, 98.04% gc time)

@penelopeysm penelopeysm marked this pull request as ready for review January 26, 2025 01:03
@penelopeysm penelopeysm requested a review from sunxd3 January 27, 2025 11:12
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.

Lovely; thanks @penelopeysm :)

@penelopeysm penelopeysm added this pull request to the merge queue Jan 27, 2025
@penelopeysm penelopeysm removed this pull request from the merge queue due to a manual request Jan 27, 2025
@penelopeysm
Copy link
Member Author

@sunxd3 Will wait for you if you'd like to review, feel free to pass if you don't have time / don't want to:)

@sunxd3
Copy link
Member

sunxd3 commented Jan 27, 2025

thanks @penelopeysm, I haven't followed closely with this part of the code, but it looks good to me

@penelopeysm penelopeysm added this pull request to the merge queue Jan 27, 2025
Merged via the queue into master with commit 29a6c7e Jan 27, 2025
20 checks passed
@penelopeysm penelopeysm deleted the py/prefix1 branch January 27, 2025 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants