Traces¶
TraceHandler records each sample site
a run reaches, with its value and log density, for
inspection or later replay.
A trace describes one run, so a different input or handler stack can change its values.
trace_handler
¶
Trace handler: records every site the program visits.
TraceHandler installs an observing handler of the program's random
instance and of its let and score steps at its position on the stack, so
it records each site's value as the handlers outside it answered it and
sees no site a handler inside it hid. A site's density is read after the
run from the contributions that reached the run's accumulator under the
site's provenance, after every score transformer on the stack. The thin
quivers.inference.trace.trace wrapper stacks this handler on and returns
its accumulated Trace.
TraceHandler
¶
Bases: EffectHandler
Record every site visited during a program's execution.
Produces a Trace whose sites dict is keyed by variable name,
whose output is the program's return value, and whose
log_joint is the sum of every site's log_prob. A call on an
input of B rows makes B independent runs: the trace records
each run's sites and lays them out along a leading run axis, so a
site's log_prob holds one density per run, shape (B,), and
its value holds one value per run (a plated site's rows after
the run axis). A TraceHandler is single-use: run the program under
one instance, read trace, then discard. The handler takes no
configuration.
Source code in src/quivers/effects/trace_handler.py
install
¶
install(run: RunContext) -> tuple[Installation, ...]
Install observers of the sites and of the let and score steps.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run being prepared.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Installation, ...]
|
One forwarding observer of the |
Source code in src/quivers/effects/trace_handler.py
finish
¶
finish(run: RunContext, output: object) -> None
Build the trace from the recorded events and the run's contributions.
| PARAMETER | DESCRIPTION |
|---|---|
run
|
The run, with every contribution the accumulator received.
TYPE:
|
output
|
The program's output, which the trace does not read; the
wrapper that stacks this handler sets
TYPE:
|
Source code in src/quivers/effects/trace_handler.py
total_log_joint
¶
Sum every site's density into the joint, one per run.
| PARAMETER | DESCRIPTION |
|---|---|
device
|
Device the accumulator seeds on.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The joint log-density of each run, shape |