Simplify the context mechanism in DynamicPPL
メンテナーはふだん 2 日以内に返信
@mhauru がすでに取り組んでいます。
2025年3月5日 から。
評価
この issue はまだ評価されていません。
説明
What's still left?
[!NOTE]
Last updated: 29 April 2026
- https://github.com/TuringLang/DynamicPPL.jl/issues/1469
- https://github.com/TuringLang/DynamicPPL.jl/issues/1012
- https://github.com/TuringLang/DynamicPPL.jl/issues/1221
Previous work
Here is a list of our current contexts and what's the current plan for them.
✅ = Already merged into main/breaking (or no plans to change)
♻️ = We know what to do but it's not done yet
❌ = Not sure what to do
| Context | in v0.36 | in future | Details |
|---|---|---|---|
DefaultContext |
Leaf | ♻️ Leaf | #1469 |
InitContext |
Leaf | ✅ Leaf | #955, #967 |
DynamicPPL.DynamicTransformationContext |
Leaf | ✅ Leaf | Keep |
LikelihoodContext |
Leaf | ✅ Accumulator | #885 |
PriorContext |
Leaf | ✅ Accumulator | #885 |
SamplingContext |
Parent | ✅ Removed | In DynamicPPL, replaced with InitContext: #955, #967 . In Turing, replace with sampler-specific leaf contexts |
CondFixContext |
Parent | ♻️ Model field | #1012 |
PrefixContext |
Parent | ♻️ Model field | #1221 |
DynamicPPL.FixedContext |
Parent | ✅ Removed | #1238 Merged into ConditionOrFixContext |
DynamicPPL.PointwiseLogdensityContext |
Parent | ✅ Accumulator | #885 |
DynamicPPL.PriorExtractorContext |
Parent | ✅ Accumulator | #907 |
DynamicPPL.DebugUtils.DebugContext |
Parent | ✅ Accumulator | #976 |
DynamicPPL.ValuesAsInModelContext |
Parent | ✅ Accumulator | #908 |
DynamicPPL.TestUtils.TestLogModifyingChildContext |
Parent | ✅ Removed | #885 |
MiniBatchContext |
Parent | ✅ Removed | Removed in #885. Could be replaced with getlogdensity (#925) |
DynamicPPL.TestUtils.TestParentContext |
Parent | ❌ | ❗This is just a mock to stay for testing purposes |
Turing.Inference.GibbsContext |
Parent(*) | ❌ | Maybe merge with ConditionContext? Otherwise probably keep. |
Turing.Optimisation.OptimizationContext |
Parent | ✅ Removed | Replaced with accumulators https://github.com/TuringLang/Turing.jl/pull/2550 https://github.com/TuringLang/Turing.jl/pull/2630 |
(*) Can only wrap leaf contexts or PrefixContext
Very old discussion
Very old discussion (click to expand)
(Writeup by @penelopeysm, but the ideas here were jointly discussed by @willtebbutt, @mhauru, @sunxd3 and I)
[!NOTE]
This is mostly obsolete and included mainly for historical interest. See this comment and #744 for a general overview of what's currently happening.
Background
Models on their own specify a set of variables and their relationship. However, there are many different ways a model can be evaluated, each with different outcomes. For example:
- one might want to only accumulate logp of observations, thus giving the value of the (log) likelihood
- one might want to record extra information about the variables seen in the model, e.g. to check whether a variable is specified twice
- one might want to use existing values stored in a varinfo, or sample new values
- ...
However, at the core, we still want to have a single model evaluation function; we don't want to write separate functions for each of these options.
The way this is accomplished is through the use of evaluation contexts. Specifically, DynamicPPL.evaluate!! takes an extra context argument which can contain any sort of information it wants, and is passed through the tilde pipeline all the way down to assume and observe. Thus, contexts may implement their own assume(..., ::MyContext) methods to inject custom behaviour that occurs when a model is evaluated.
Each context is its own type (which subtypes AbstractContext), and contexts are modelled as a linked lis. There are three 'leaf contexts', DefaultContext, LikelihoodContext, and PriorContext. 'Parent contexts' typically contain some kind of information, plus a child context.
There is, apparently, something very complicated about this, and we'd like to simplify it.
Possible directions
(1) Rework data structure
One option is to use a new data structure for a series of contexts, as shown below
-
(a) Extract the modifiers into a separate part
Instead of having the linked list above, one option would be to have a base context plus an array of 'context modifiers':
-
(b) Flatten the modifiers into a single 'super-context' that can do everything
In both cases, this would mean that instead of overloading assume and observe for each type of context, we would only have one method (or maybe N methods, where N is the number of leaf contexts). Inside that method though, we would have to either iterate over the list of modifiers, or dynamically perform some action depending on whether the modifier field in the super-context is nothing.
There is an additional danger in (b) as well, in that it does not preserve information about the order in which the modifications should be applied. That is to say, it assumes that modifiers are commutative. I don't know of a particular case where this isn't true, but I can imagine that it might happen with e.g. some combination of PrefixContext + ConditionContext.
In my (Penny's) opinion, doing this is pretty much just shuffling the complexity around and won't really give us substantial code simplification. It would also invalidate the type-based dispatch system that we now have, so I also worry about whether it would cause performance regressions.
(2) Place more type bounds on the contexts
Anohter option is to retain the linked list structure, but to place more constraints on what types of contexts can appear in which situation. For example, there are a number of contexts that are used solely in the implementation of a single function: see e.g. ValuesAsInModelContext and DynamicPPL.values_as_in_model. These contexts have no business being at anywhere but the head of the linked list (i.e. the top of the context stack), and we could enforce this by having something like this:
abstract type CanBeChildContext <: AbstractContext
struct DefaultContext <: CanBeChildContext end
# Note that we don't subtype CanBeChildContext
struct ValuesAsInModelContext <: AbstractContext
values::T
childcontext::CanBeChildContext
end
This makes it illegal to, for example, construct certain combinations such as a ValuesAsInModelContext{ValuesAsInModelContext{DefaultContext}}}.
(Markus had ideas about extending this trait-like system even more, so e.g. specific traits would allow contexts to perform specific actions such as modifying VarInfo. My memory of this is a little bit hazy, so I will ask him to write this)
My (Penny's) opinion is basically that I like the general idea, but I'm not yet convinced this will help us very much. I think it's really difficult to group contexts according to 'shared behaviour' / 'these contexts have equal levels of permissions'. And although the 'can be child context' one might seem like an obvious low-hanging fruit, I don't think it makes for very much improvement in the code because ValuesAsInModelContext is already contained to a single source file, and thus very unlikely to 'leak' out of it and cause weird contexts like SamplingContext{ValuesAsInModelContext}.
The other concern with this is that they're basically 'mixin' traits, which work nicely in something like Python because you can inherit from as many classes as you like, but in Julia the fact that each type must have only one supertype leads to difficulties when you want to inherit multiple types of behaviour. For example:
# Can do Foo
abstract type CanFooContext <: AbstractContext end
# Can do Bar
abstract type CanBarContext <: AbstractContext end
# Can do Foo and Bar
# You can plug this anywhere you need a CanFooContext, but not a CanBarContext?
abstract type CanFooBarContext <: CanFooContext end
What now?
The thing about the whole context thing is that I don't entirely know how it can be simplified without re-introducing the complexity in some other format. The purpose of having different contexts is because it allows us to abstract away the notion of 'model evaluation' into the tilde pipeline with assume / observe, but if we want to support different evaluation modes, then the extra information needed for this has to be supplied in some format.
Personally, I did find the context mechanism very confusing at the start, but after having worked with it a bit I think it is actually quite natural. (And as a Haskeller: it has a direct resemblance to monad transformer stacks for handling effects.) And my personal intuition would be that the best thing to do is to write more extensive documentation on this. We have a nice base of the minituring page to work with: https://turinglang.org/docs/developers/compiler/minituring-contexts/ which explains the 'need' for contexts very well, and I think it would be great to (a) expand on this need, by showing the model evaluation function, and (b) explain how it's used inside DynamicPPL itself, and (c) demonstrate how to extend this system with a custom context.
However, I'm conscious that maybe there are other perspectives that I'm not seeing, so if someone else has ideas on how contexts can be simplified, please do post below!
Related issues
- 主要言語
- Julia
- スター
- 291
- フォーク
- 43
- 平均マージ
- 1日 23時間
- マージ済み PR(30日)
- 15
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
TuringLang/DynamicPPL.jl のほかの issue
-
難易度 4/5 3〜5日 初心者へのやさしさ 64/100
TuringLang/DynamicPPL.jl#1499 ·
メンテナーはふだん 2 日以内に返信
-
enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
TuringLang/DynamicPPL.jl#1480 ·
メンテナーはふだん 2 日以内に返信
-
Separate model-evaluation inputs from outputs再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンdata-structures enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 30/100
TuringLang/DynamicPPL.jl#1469 ·
メンテナーはふだん 2 日以内に返信
-
enhancement
難易度 3/5 1〜2日 初心者へのやさしさ 64/100
TuringLang/DynamicPPL.jl#1466 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
documentation
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
TuringLang/DynamicPPL.jl#1390 · コメント 2 件 ·
メンテナーはふだん 2 日以内に返信
TuringLang/DynamicPPL.jl の issue をすべて見る
似ている issue
-
Release CorleoneInverse as 0.1.1対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
SciML/Corleone.jl#200 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
JuliaAI/MLJTransforms.jl#120 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
exercism/julia#1205 · コメント 3 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
SciML/OrdinaryDiffEq.jl#5175 ·
メンテナーはふだん 1 日以内に返信