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

Nested PrefixContext behaviour is not fully defined #786

Open
penelopeysm opened this issue Jan 25, 2025 · 0 comments
Open

Nested PrefixContext behaviour is not fully defined #786

penelopeysm opened this issue Jan 25, 2025 · 0 comments

Comments

@penelopeysm
Copy link
Member

penelopeysm commented Jan 25, 2025

# Setup
using DynamicPPL
p1 = PrefixContext{:a}(DefaultContext())
p2 = PrefixContext{:a}(PrefixContext{:b}(DefaultContext()))
p3 = PrefixContext{:a}(SamplingContext(PrefixContext{:b}(DefaultContext())))
p4 = SamplingContext(PrefixContext{:a}(DefaultContext()))

julia> prefix(p1, @varname(x))
a.x

julia> prefix(p2, @varname(x))
b.a.x

julia> prefix(p3, @varname(x)) 
a.x

julia> prefix(p4, @varname(x))
ERROR: MethodError: no method matching prefix(::SamplingContext{SampleFromPrior, PrefixContext{…}, Random.TaskLocalRNG}, ::VarName{:x, typeof(identity)})
The function `prefix` exists, but no method is defined for this combination of argument types.

Several points:

  1. prefix(p1, @varname(x)) is all good.

  2. It seems to me that it would be more natural for prefix(p2, @varname(x)) to return a.b.x as right now this means that nested submodels have the prefixing occurring in the less intuitive way:

using DynamicPPL, Distributions

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

@model function submodel()
  x ~ to_submodel(prefix(subsubmodel(), :c), false)
  return x
end

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

keys(VarInfo(parentmodel()))
# 2-element Vector{VarName{sym, typeof(identity)} where sym}:
#  c.a.x
#  c.b.x
  1. The intuitive behaviour for prefix(p3, @varname(x)) would be to prefix with both a and b

  2. prefix(p4, @varname(x)) should traverse the context tree accordingly. This is related to to_submodel does not correctly handle manually specified prefixes when sampling #785, where the underlying problem is that the model is being evaluated with a DebugContext(SamplingContext(PrefixContext...))) but the varnames aren't prefixed.

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

No branches or pull requests

1 participant