Skip to content

Trace types

SampleSite and Trace record one run of a program: each site's value and log density. Inference algorithms use these records to compare runs, compute objectives, or replay chosen sites.

A finite structured sample site sets is_structured and keeps its checked point as value. The same trace record carries its address, sampleable, and tensor log weight. The point keeps its type under conditioning and replay. A trace with such a site describes one independent run. Tensor sites retain their ordinary run-axis layout.

A structured latent has no unconstrained tensor coordinates. A continuous latent registry records a structured observation as observed and requires an unobserved structured site to be handled separately. Exact enumeration uses the runtime's existing finite Random handler.

trace_types

Data types for execution traces.

SampleSite and Trace live here in a leaf module so both the effect-handler interpreter and the thin quivers.inference.trace wrapper import them without introducing a cycle. The effects package produces traces; the inference package consumes them.

SiteAddress

SiteAddress = tuple[str, tuple[tuple[str, str | int], ...], tuple[int, ...]]

The kernel's dynamic address of one occurrence of a site.

SampleSite didactic-model

SampleSite(*, name: str, morphism: ContinuousMorphism | None = field(opaque=True, extras=OPAQUE_BY_IDENTITY), value: object = field(opaque=True, extras=OPAQUE_BY_IDENTITY), log_prob: Tensor = field(opaque=True, extras=OPAQUE_BY_IDENTITY), is_observed: bool = False, is_deterministic: bool = False, sampleable: TorchSampleable | FiniteSampleable | None = field(default=None, opaque=True, extras=OPAQUE_BY_VALUE), address: SiteAddress | None = field(default=None, opaque=True, extras=OPAQUE_BY_VALUE), metadata: dict[str, HeldValue] = field(default_factory=dict, opaque=True, extras=OPAQUE_BY_IDENTITY), units: tuple[SiteUnits, ...] = field(default=(), opaque=True, extras=OPAQUE_BY_VALUE))

Bases: ValueModel

Record of a single site in a program trace.

Values and morphisms retain their runtime identity. Field updates use with_ and preserve their autograd connections.

PARAMETER DESCRIPTION
name

Variable name bound at this site.

TYPE: str

morphism

The distribution morphism (None for let bindings and scores).

TYPE: ContinuousMorphism or None DEFAULT: field(opaque=True, extras=OPAQUE_BY_IDENTITY)

value

The sampled, observed, or computed tensor. A site drawn from a finite structured law keeps its one typed point as a value.

TYPE: object DEFAULT: field(opaque=True, extras=OPAQUE_BY_IDENTITY)

log_prob

The density the site contributed to the run's score, after every score transformer on the stack. Zero for let bindings and for intervened sites.

TYPE: Tensor DEFAULT: field(opaque=True, extras=OPAQUE_BY_IDENTITY)

is_observed

Whether this site was conditioned on an observed value.

TYPE: bool DEFAULT: False

is_deterministic

Whether this is a let binding, a score step, or an intervened site.

TYPE: bool DEFAULT: False

sampleable

The sampleable the site's request carried. A tensor law records its morphism and input. A structured law records its typed support. Let bindings and scores have no sampleable.

TYPE: TorchSampleable or FiniteSampleable or None DEFAULT: field(default=None, opaque=True, extras=OPAQUE_BY_VALUE)

address

The kernel's dynamic address of the site's request, which is one identity for the run's trace and for every handler that answered it.

TYPE: SiteAddress or None DEFAULT: field(default=None, opaque=True, extras=OPAQUE_BY_VALUE)

metadata

Annotations handlers attached to the site.

TYPE: dict[str, HeldValue] DEFAULT: field(default_factory=dict, opaque=True, extras=OPAQUE_BY_IDENTITY)

units

For a score step that declares a factorization, its units in each of the runs the trace holds; empty for any other site.

TYPE: tuple[SiteUnits, ...] DEFAULT: field(default=(), opaque=True, extras=OPAQUE_BY_VALUE)

is_structured property

is_structured: bool

Whether the site's value is a single point of a structured law.

RETURNS DESCRIPTION
bool

True when the sampleable is a finite typed law.

Trace didactic-model

Trace(*, sites: dict[str, SampleSite] = field(default_factory=dict, opaque=True, extras=OPAQUE_BY_IDENTITY), output: Tensor | dict[str, Tensor] | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY), log_joint: Tensor | None = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY))

Bases: ValueModel

Complete execution trace of a monadic program.

The sites mapping grows as the program executes. Finalizing a trace uses with_ to attach its output and joint density.

PARAMETER DESCRIPTION
sites

All sites keyed by variable name.

TYPE: dict[str, SampleSite] DEFAULT: field(default_factory=dict, opaque=True, extras=OPAQUE_BY_IDENTITY)

output

The program's return value.

TYPE: Tensor or dict[str, Tensor] DEFAULT: field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY)

log_joint

Sum of log-densities across all sites. Shape (batch,).

TYPE: Tensor DEFAULT: field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY)

stochastic_sites property

stochastic_sites: dict[str, SampleSite]

The sites that are not deterministic.

RETURNS DESCRIPTION
dict[str, SampleSite]

Every sampled or observed site.

latent_sites property

latent_sites: dict[str, SampleSite]

The sites that are neither observed nor deterministic.

RETURNS DESCRIPTION
dict[str, SampleSite]

Every sampled latent site.

observed_sites property

observed_sites: dict[str, SampleSite]

The observed sites.

RETURNS DESCRIPTION
dict[str, SampleSite]

Every site conditioned on an observation.