Traces¶
The trace handler and the trace records.
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
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 log_prob into the joint density.
The accumulator uses the narrowest right-aligned shape shared by the nonzero site contributions. Wider axes are summed before addition so broadcasting cannot duplicate scalar terms. Zero contributions do not determine the target shape but are still added.
| PARAMETER | DESCRIPTION |
|---|---|
batch_size
|
Leading extent of the program input. Not consulted: the joint's shape follows from the sites themselves, and a program whose sites reduce their own plate axes has a joint narrower than its input.
TYPE:
|
device
|
Device the accumulator seeds on.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The joint log-density. |