Skip to content

Pass stochastic-state schemas through the unified evaluation context #1480

Description

@yebai

Extend DynamicPPL#1469 with an automatically derived AbstractPPL of schema in the single Context. This builds on JuliaBUGS’s schema system while retaining ordinary model syntax, without the special argument proposed in #965.

Proposed API sketch:

@model function model(y)
    k ~ DiscreteUniform(1, 3)
    theta ~ product_distribution([Normal() for _ in 1:k])
    y ~ Normal(sum(theta), 1)
end

m = model(0.3)
s = (; k=2, theta=[0.2, -0.1])

context = Context(
    rng,
    InitFromParams(VarNamedTuple(s), nothing),
    UnlinkAll();
    schema=of(s),
)

retval, vi = evaluate!!(m, context, VarInfo(accumulators))

Here VarInfo is #1469’s output-only accumulator collection; accumulators selects density and active-layout outputs.

Density evaluation consumes supplied values without implicit latent sampling. Each tilde operation validates compatibility with the supplied schema and distribution. Preparation discovers an initial layout through execution; dimension-changing kernels construct compatible proposals. Schemas support preallocation and storage reuse, accompanied by active-site and transformation metadata.

Schema validation would be optional, with three supported workflows:

  • Declared: supply a schema explicitly using the existing @of syntax.
  • Discover and freeze: evaluate with NoSchema(), collect the realised schema, then pass it into subsequent evaluations for validation.
  • Discover per simulation: evaluate each simulation with NoSchema() and collect its realised schema; use that schema when scoring the resulting state.

NoSchema() imposes no additional schema constraints, preserving existing behaviour. Schema collection is an independent output operation, and existing initialisation, transformation, and sampler checks remain in force.

Generative evaluation must distinguish prescribed layouts from layouts being constructed. PG reference replay preserves retained active variables and native shapes, while fresh particles may generate different layouts. Conditioned values remain unchanged. Evaluation records realised layouts in output accumulators rather than mutating the input schema. Reuse requires explicit output-to-input conversion.

The Gibbs rewrite reconstructs active state after component updates. Returning validated metadata from accepted-state evaluations could avoid that reconstruction where an evaluation already occurs. Turing#2891 illustrates why container shapes alone are insufficient.

Generative execution also supports SMC, initialisation, and prior-proposal MH. Kernels remain responsible for proposal-density corrections, valid dimension-changing transitions, and linking corrections when targeting linked coordinates.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions