Checked programs¶
A CheckedProgram is a program morphism that runs its checked Quivers Indexed Effect Core (QIEC) computation on the
reference machine, the library's interpreter for QIEC
(running and scoring). The
compiler produces one for a program whose calls it cannot turn into compiled steps. It runs
under the effect handler stack with tensor values. Inference methods such as MCMC and SVI
read the latents from a trace and use the same log joint and its gradients
(runtime providers).
The evaluator shares a pass across runs when the control flow and handlers permit it. Its work report counts the passes, and a sampling run that reaches a different set of sites from its trace raises an error.
checked_program
¶
A program that runs its checked computation on the reference machine.
A program that calls a named computation the compiler cannot reduce
to compiled runtime steps (a recursive call, a branch whose arms draw
different sites, a handler that is not discharged, a case) compiles to
a CheckedProgram. It is
a MonadicProgram with no host steps,
whose body is the program's computation in its checked module, run on the
reference machine with every number a tensor.
A run installs the handlers on the effect handler stack exactly as a run
of a compiled program does: each handler's installations go on the
module's canonical random, score and Param instances, around
the computation, under a drawing handler that draws and scores every site
nobody else answers and a replay that conditions every site given a value.
Draws and densities go through the tensor backend, so the log joint, a
reparameterized draw and the value keep the gradient of every tensor the
run was given, through calls, recursion and handler clauses. A trace,
log_joint, rsample, and every inference consumer built on them read
the program as they read a compiled one.
A site reached more than once in a run, inside a recursion or a repeated
call, is one site per occurrence; a trace names its occurrences
"<label>@<n>" in the order the run reaches them, as a replay takes
them.
RUN
module-attribute
¶
The name of the computation a run wraps the program in.
OBSERVED_APART
module-attribute
¶
OBSERVED_APART = "a handler on the stack is installed in each run and may watch its sites as the run reaches them, and in a pass over every run at once a site's value is every run's"
Why the forward runs of a call under handlers are made one by one.
CheckedProgram
¶
CheckedProgram(domain: AnySpace, codomain: AnySpace, module: QiecModule, name: str, refusal: Exception | None = None, *, prior: bool = False)
Bases: MonadicProgram
A program run as its checked computation on the reference machine.
| PARAMETER | DESCRIPTION |
|---|---|
domain
|
The program's declared domain.
TYPE:
|
codomain
|
The program's declared codomain.
TYPE:
|
module
|
The checked module holding the program's entry point.
TYPE:
|
name
|
The program's name.
TYPE:
|
refusal
|
Why the compiled runtime does not run the program: the located
diagnostic of the call it could not lower to steps;
TYPE:
|
prior
|
Whether the program drops the scores of its observations, which leaves its prior; otherwise each observation with a law is a site, conditioned when it is given and drawn when it is not.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
weights |
The learned weights the program's
TYPE:
|
work |
The work of the last call: its
TYPE:
|
vectorization_obstacle |
Why the last call made one pass per run, or
TYPE:
|
Source code in src/quivers/effects/checked_program.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
refusal
property
¶
Why the compiled runtime does not run the program.
| RETURNS | DESCRIPTION |
|---|---|
Exception
|
The located diagnostic the compiler recorded for the call it could not lower, or one naming the program. |
steps_list_sites
property
¶
No: the sites are those a run of the checked computation reaches.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
module
property
¶
module: QiecModule
The checked module the program runs in.
| RETURNS | DESCRIPTION |
|---|---|
QiecModule
|
The module. |
entry
property
¶
entry: ProgramEntry
The program's elaborated entry point.
| RETURNS | DESCRIPTION |
|---|---|
ProgramEntry
|
The entry. |
kept_data
property
¶
The observations that stay data the program must be given.
| RETURNS | DESCRIPTION |
|---|---|
Mapping[str, str]
|
Each observation the program scores but cannot draw, with why. |
potentials
property
¶
The scores of the program that no law of a site accounts for.
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, ...]
|
The structural path, dotted, of every |
potential_sites
property
¶
The trace names of the program's own scores, every one a potential.
| RETURNS | DESCRIPTION |
|---|---|
frozenset[str]
|
|
observed_names
property
¶
The names the program observes.
| RETURNS | DESCRIPTION |
|---|---|
set[str]
|
The labels of its |
prior_program
¶
prior_program() -> CheckedProgram
The program without the scores of its observations.
| RETURNS | DESCRIPTION |
|---|---|
CheckedProgram
|
The program over the same module and learned weights, every observation's density dropped and every draw kept. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If an observation's density is added where no bind of the program's body can drop it: inside a computation it calls, or under a branch. |
Source code in src/quivers/effects/checked_program.py
rsample
¶
rsample(x: Tensor, sample_shape: Size = Size(), observations: Mapping[str, object] | None = None) -> Tensor
Run the program forward under the handler stack.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input. Shape
TYPE:
|
sample_shape
|
Additional leading sample dimensions; each element is one independent call. |
observations
|
The program's data by parameter name, and the values of the
sites the runs are given, by label or
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The returned values, shape |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the program returns a value that is not a tensor. |
Source code in src/quivers/effects/checked_program.py
has_conditional_density
¶
No: a program's density at its output marginalizes its draws.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Always |
Source code in src/quivers/effects/checked_program.py
log_prob
¶
Log-probability is not defined for a program's output alone.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input.
TYPE:
|
y
|
Program output.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
Always: the density at the output marginalizes every draw. |
Source code in src/quivers/effects/checked_program.py
log_joint
¶
The joint log density at given values for every site.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Program input. Shape
TYPE:
|
intermediates
|
The program's data by parameter name and a value for every
site the runs reach, by label, or by
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The log joint of each run, shape |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If a site a run reaches is given no value. |
Source code in src/quivers/effects/checked_program.py
__repr__
¶
The program's name and spaces.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The representation. |
machine_value_admitted
¶
Whether a handler's answer is a value the machine holds.
Every number the machine holds is a tensor, so an answer that is a Python number would enter the computation without the run's dtype and without a gradient; it is refused where it enters.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The answer.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|