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

On the sub model syntax #270

Closed
yebai opened this issue Jun 30, 2021 · 2 comments
Closed

On the sub model syntax #270

yebai opened this issue Jun 30, 2021 · 2 comments

Comments

@yebai
Copy link
Member

yebai commented Jun 30, 2021

We recently added support for submodels (#233). There are follow-up discussions in #267 (comment) and slack on the proposed submodel syntax. I thought it may be helpful to open an issue for some discussions.

Current syntax:

    @submodel x = model(args...)
    @submodel prefix x = model(args...)
Treats `model` as a distribution, where `x` is the return-value of `model`.

I think we can support both tilde and assignment, but with different semantics, i.e.

  1. @submodel x = model(args...) will assign returned value of model to variable x.
  2. @submodel x ~ model(args...) will assign a named tuple to variable x. This named tuple is a "SimpleVarInfo" style object, e.g. it contains all variables in the sub-model.

Here option 2 is slightly more general since a user can manipulate the returned named tuple x to for specific purposes. It is also more consistent with the Turing modelling syntax.

cc @devmotion @phipsgabler @torfjelde @mohamed82008

EDIT: we can also remove the @submodel annotation eventually, and use a unified notation ~ for both Distributions and sub-models.

@torfjelde
Copy link
Member

Current syntax:

    @submodel x = model(args...)
    @submodel prefix x = model(args...)
Treats `model` as a distribution, where `x` is the return-value of `model`.

I think we can support both tilde and assignment, but with different semantics, i.e.

  1. @submodel x = model(args...) will assign returned value of model to variable x.
  2. @submodel x ~ model(args...) will assign a named tuple to variable x. This named tuple is a "SimpleVarInfo" style object, e.g. it contains all variables in the sub-model.

Here option 2 is slightly more general since a user can manipulate the returned named tuple x to for specific purposes. It is also more consistent with the Turing modelling syntax.

After #269 (2) seems not as useful maybe, since __varinfo__ will essentially be this? If the point is that we only want the information that was added from within the submodel, the best approach would probably be to diff the varinfo before running the submodel with the resulting varinfo and then "un-prefix" the variables. But I'm not so certain people would ever use this when they can access the return-values anyways? 😕

EDIT: we can also remove the @submodel annotation eventually, and use a unified notation ~ for both Distributions and sub-models.

We could actually do this for (1) quite easily if we decided to use ~ for it (but again, this has issues):

function tilde_assume!!(context, right::Model, vn, inds, vi)
    retval, new_vi = _evaluate(right, vi, PreficContext(vn, context))
    return retval, new_vi
end

PrefixContext(vn::VarName, context) = PrefixContext{Symbol(vn)}(context)
# Just to make it inferrable at compile-time when it's possible.
function PrefixContext(::VarName{sym, Tuple{}}, context) where {sym}
    return PrefixContext{sym}(context)
end

But this of course suffers the same issues as (2) since it requires using the ~ notation + it's not so easy to override the prefix being used, which can be annoying.

@mhauru
Copy link
Member

mhauru commented Dec 2, 2024

This conversation has been overtaken by the conversation in #696

@mhauru mhauru closed this as completed Dec 2, 2024
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

3 participants