Tensor Distribution Backend¶
TensorBackend samples and scores the
runtime distributions of the
reference machine by torch. It builds each family's
distribution as the compiled runtime builds the value of a Distribution term, so the two score a
family by the same code, and every density keeps its gradient.
tensor_backend
¶
The reference machine's distributions, sampled and scored by torch.
A quivers.qiec.distributions.RuntimeDistribution names a family, its
arguments as tensors, and the plate it is drawn over. TensorBackend
builds the family's torch distribution exactly as the compiled runtime
builds the value of a Distribution term, with the plate's axes as the
distribution's leading batch axes, so the machine and the compiled runtime
score a family by the same code. A density keeps the gradient of every
argument and of the point, and a draw of a reparameterizable family keeps
the gradient of the arguments; a draw of any other family is a sample
whose law depends on the arguments, which no derivative of the draw
itself accounts for, and has none.
TorchArgument
¶
TorchArgument = Tensor | Distribution | tuple[TorchArgument, ...]
An argument of a torch family: a tensor, a distribution, or a tuple.
TensorBackend
¶
Sampling and scoring by torch, with gradients.
Every density is the family's torch log_prob at once over the
plate, with the plate's event axes summed; draws use torch's global
generator.
differentiable
property
¶
Yes: densities and reparameterized draws keep their gradients.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
sample
¶
sample(distribution: RuntimeDistribution, rng: Random) -> object
Draw at every position of the plate.
In a pass over many runs at once (quivers._positions.map_positions)
every run draws its own value: the draw is made beneath the mapping
by quivers._positions.draw, over a distribution whose arguments
lead with the run axis.
| PARAMETER | DESCRIPTION |
|---|---|
distribution
|
The distribution.
TYPE:
|
rng
|
Unused: torch draws from its own generator.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
object
|
The draw, reparameterized when the family allows it. |
| RAISES | DESCRIPTION |
|---|---|
DistributionError
|
If the family has no torch distribution for its arguments. |
Source code in src/quivers/continuous/tensor_backend.py
log_prob
¶
log_prob(distribution: RuntimeDistribution, value: object, keep_batch: bool) -> Tensor
Evaluate the log density at a point.
| PARAMETER | DESCRIPTION |
|---|---|
distribution
|
The distribution.
TYPE:
|
value
|
The point.
TYPE:
|
keep_batch
|
Whether to keep one density per batch position.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The density in the run's real dtype, its plate event axes summed, its batch axes summed unless kept. |
| RAISES | DESCRIPTION |
|---|---|
DistributionError
|
If the family has no torch distribution for its arguments, or the point is not shaped like the plate. |
Source code in src/quivers/continuous/tensor_backend.py
log_mass
¶
log_mass(distribution: RuntimeDistribution, batch_axes: int) -> Tensor
The log total mass of a construction.
| PARAMETER | DESCRIPTION |
|---|---|
distribution
|
The distribution.
TYPE:
|
batch_axes
|
The leading batch axes kept.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The log mass in the run's real dtype. |
| RAISES | DESCRIPTION |
|---|---|
DistributionError
|
If the family has no torch distribution for its arguments. |
Source code in src/quivers/continuous/tensor_backend.py
DrawnLaw
¶
DrawnLaw(distribution: RuntimeDistribution, native: Distribution)
Bases: ContinuousMorphism
The law a site of a checked program was drawn from, as a morphism.
A site of a program that runs on the reference machine draws from a
RuntimeDistribution its run constructs. A trace records the site's
law as the morphism its run drew from, so a latent registry reads its
support and event shape, and a guide its bijector, as for a site of a
compiled program. The morphism has no input: its law is the one the
run constructed, and sampling and scoring it repeat that law.
| PARAMETER | DESCRIPTION |
|---|---|
distribution
|
The distribution the site was drawn from, at one position of its plate.
TYPE:
|
native
|
Its torch distribution.
TYPE:
|
Source code in src/quivers/continuous/tensor_backend.py
support
property
¶
support: Constraint
The support of the law.
| RETURNS | DESCRIPTION |
|---|---|
Constraint
|
The torch distribution's support. |
rsample
¶
Draw from the law.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Unused: the law has no input.
TYPE:
|
sample_shape
|
Leading sample dimensions. |
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reparameterized draws when the family allows them. |
Source code in src/quivers/continuous/tensor_backend.py
log_prob
¶
The log density of a value.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Unused: the law has no input.
TYPE:
|
y
|
The value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The density at each position of the law's batch. |
Source code in src/quivers/continuous/tensor_backend.py
torch_law
¶
torch_law(distribution: RuntimeDistribution) -> Distribution
The torch distribution of a runtime distribution.
| PARAMETER | DESCRIPTION |
|---|---|
distribution
|
The distribution.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Distribution
|
The family's distribution, its batch axes the plate's batch and event axes, its event axes the family's own. |
| RAISES | DESCRIPTION |
|---|---|
DistributionError
|
If the family has no torch constructor for its arguments. |
Source code in src/quivers/continuous/tensor_backend.py
site_morphism
¶
site_morphism(distribution: RuntimeDistribution) -> ContinuousMorphism
The morphism a trace records for a site drawn from a distribution.
| PARAMETER | DESCRIPTION |
|---|---|
distribution
|
The distribution the site was drawn from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ContinuousMorphism
|
A |
| RAISES | DESCRIPTION |
|---|---|
DistributionError
|
If the family has no torch distribution for its arguments. |