Program¶
Top-level probabilistic program definitions and execution.
program
¶
Program: compile a morphism expression into a trainable nn.Module.
Program
¶
Program(morphism: Morphism | ContinuousMorphism | Module | None = None)
Bases: Module
Wraps a morphism expression as a trainable nn.Module.
Traverses the morphism DAG, collects all learnable parameters and observed buffers, and provides a forward() that materializes the composed tensor.
Supports both discrete Morphism instances (which produce a
membership tensor via forward()) and ContinuousMorphism
instances (which expose rsample / log_prob).
| PARAMETER | DESCRIPTION |
|---|---|
morphism
|
The root morphism expression (possibly a composition tree).
TYPE:
|
Examples:
>>> from quivers import FinSet, morphism, Program
>>> X = FinSet(name="X", cardinality=3)
>>> Y = FinSet(name="Y", cardinality=4)
>>> f = morphism(X, Y)
>>> prog = Program(f)
>>> out = prog() # shape (3, 4)
Source code in src/quivers/program.py
morphism
property
¶
morphism: Morphism | ContinuousMorphism | Module | None
The underlying morphism expression, or None for a
morphism-less module (one declaring only signatures /
encoders / decoders / losses, or exporting a parametric
program template that has not been instantiated).
domain
property
¶
Domain of the underlying morphism.
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the Program wraps no morphism (an uninstantiated parametric template or a structural-artifact container). |
codomain
property
¶
Codomain of the underlying morphism.
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the Program wraps no morphism (an uninstantiated parametric template or a structural-artifact container). |
entry_points
¶
entry_points() -> tuple[EntryPoint, ...]
The executable entry points of the compiled module.
| RETURNS | DESCRIPTION |
|---|---|
tuple[EntryPoint, ...]
|
Every |
| RAISES | DESCRIPTION |
|---|---|
ExecutionFailure
|
With code |
Source code in src/quivers/program.py
entry
¶
entry(name: str) -> EntryPoint
One entry point of the compiled module, by name.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The entry's source name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
EntryPoint
|
The entry. |
| RAISES | DESCRIPTION |
|---|---|
ExecutionFailure
|
With code |
Source code in src/quivers/program.py
run
¶
run(name: str, *arguments: HostValue, data: Mapping[str, HostValue] | None = None, sites: Mapping[str, HostValue] | None = None, static_arguments: Sequence[str] = (), fuel: int | None = None, seed: int | None = None) -> EntryRun
Invoke an entry point on the reference machine.
The invocation is the one qvr run and the REPL's :run
make, through invoke_entry,
with the same validation, providers, trace, and error codes.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The entry's source name.
TYPE:
|
*arguments
|
Host values for its value parameters, in order.
TYPE:
|
data
|
A program's parameters by name.
TYPE:
|
sites
|
The sites a program run conditions on; every other site is drawn.
TYPE:
|
static_arguments
|
TYPE:
|
fuel
|
A step budget for the run.
TYPE:
|
seed
|
A seed for the reference generator the run draws with.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
EntryRun
|
The value, a program's log joint, and the execution result. |
| RAISES | DESCRIPTION |
|---|---|
ExecutionFailure
|
With code |
Source code in src/quivers/program.py
rsample
¶
Reparameterized sample (continuous programs only).
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Input tensor.
TYPE:
|
sample_shape
|
Extra leading sample dimensions. |
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Sampled output. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the underlying morphism is not continuous. |
Source code in src/quivers/program.py
log_prob
¶
Log-probability (continuous programs only).
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Input tensor.
TYPE:
|
y
|
Output tensor.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Log-probability of y given x. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the underlying morphism is not continuous. |
Source code in src/quivers/program.py
forward
¶
forward(n_steps: int | None = None) -> Tensor
Materialize the composed tensor (discrete programs only).
| PARAMETER | DESCRIPTION |
|---|---|
n_steps
|
If provided, sets the step count on all
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Tensor of shape |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the underlying morphism is continuous. |
Source code in src/quivers/program.py
log_membership
¶
log_membership() -> Tensor
Log of the membership tensor.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Log-membership values. Entries near 0 map to large negative values. |
Source code in src/quivers/program.py
nll_loss
¶
Negative log-likelihood loss for observed (domain, codomain) pairs.
For each pair (x, y), computes -log(tensor[x, y]). Suitable when the morphism represents fuzzy membership and observed pairs should have high membership.
| PARAMETER | DESCRIPTION |
|---|---|
domain_indices
|
Integer tensor of shape (batch,) or (batch, n_domain_dims) indexing into the domain.
TYPE:
|
codomain_indices
|
Integer tensor of shape (batch,) or (batch, n_codomain_dims) indexing into the codomain.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar mean negative log-likelihood. |
Source code in src/quivers/program.py
bce_loss
¶
Binary cross-entropy between the morphism tensor and a target.
| PARAMETER | DESCRIPTION |
|---|---|
target
|
Target tensor of the same shape as the morphism tensor, with values in [0, 1].
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar BCE loss. |