DSL Programs and Let-Expressions¶
A program block is a probabilistic program. Its steps draw values, compute from
them, score observed data, and sum out discrete latent variables, and its last step
returns a value. DSL Declarations covers the objects,
morphisms, and kernels that programs use, and the Overview
summarizes the grammar.
Program declarations¶
A program reads as a sequence of steps. sample draws a random value, let computes
from values already bound, observe scores a measured value under a distribution, and
return gives the caller the result. marginalize sums a finite latent variable out
of the likelihood instead of drawing it. The steps define the model, and a separate
inference procedure (Inference Foundations) estimates its
posterior.
object X : FinSet 1
object Y : Real 1
object Z : Real 1
morphism f : Y -> Y ~ Normal
morphism g : Y * Z -> Y ~ Normal
program my_prog : X -> Y
let mu <- sample LogitNormal(mu=0.0, sigma=1.0)
let x <- sample Normal(loc=mu, scale=1.0)
return x
program with_params(a, b) : Y * Z -> Y
let w = a
let x <- sample f(w)
let y <- sample g(x, b)
return y
An observe step may score a value from an earlier let. For instance,
let centered = raw - 2.0 followed by
observe centered : Row <- Normal(loc=mean, scale=1.0) treats raw as
the external measurement and computes the scored response inside the
program. The computed response adds no latent coordinate. The
observation semantics specify the
score and its reference measure.
A program without observe or score steps defines a Markov kernel from its input
to its output, the composite of its draws in the Kleisli category
of the Giry monad. observe and score multiply each execution by a nonnegative
weight, so a program with them defines an unnormalized measure on its executions,
which inference normalizes into the posterior. Semantics
states this precisely.
The compiled program is a
MonadicProgram
(Probabilistic Programs describes rsample, log_joint, and the
observations dict).
Effect signatures¶
A program may declare the effects its body uses with effects=[...], a subset of
Sample, Score, Marginal, and Pure. The compiler checks that the body uses no
other effect, so effects=[Pure] rejects any sample, observe, score, or
marginalize step.
object X : FinSet 1
object Y : Real 1
program prior : X -> Y [effects=[Sample]]
let mu <- sample Normal(loc=0.0, scale=1.0)
return mu
program deterministic : Y -> Y [effects=[Pure]]
let doubled = 2.0 * x
return doubled
Compositional Effects covers effects in general.
QIEC typed computations¶
Every program elaborates to a computation of the Quivers Indexed Effect Core (QIEC),
the typed core language of QVR (language reference), and a
typed define writes such a computation directly. Its header gives the result type and
the effect row, the effect
instances it may use, and perform requests an operation of one instance:
effect Tap
ping : Int -> Int
instance tap : Tap
define echo(value : Int) : Int !{tap} =
let answer <- perform tap.ping(value)
return answer
A computation body is built from return, let ... <- perform ..., a call
let x <- f[statics](args) of a named computation, handle ... with ... in,
with instance x : E in (which creates an instance scoped to its body), and case
on an indexed value with an explicit motive (the result type as a function of the
indices) (McBride, 2002). A row such as !{tap} lists instance names, and an
open row !{tap | rho lacks tap} also admits any other instances rho stands for,
which must not include tap (Leijen, 2014). The checker verifies both the tail and
the lacks constraint.
Each call is checked against the callee's signature at the call's static arguments.
The checker handles the module's call graph one strongly connected component at a
time, so mutually recursive computations need no forward declarations, and a recursive
computation must reach a case branch or an if arm that does not call again. A
scoped instance may not remain in its body's row. A program draws from another program
with let x <- program sub(args) (or let (a, b) <- program sub for a pair), which runs sub's
steps in place under names of its own (sub's local z becomes x$z), and calls a
computation with let x <- f(args). Computations
and probabilistic programs give the full
syntax, and Transpiler support lists which targets accept
which computations.
Fitting programs that call computations¶
A program fitted with trace, stochastic variational inference (SVI), or Markov chain
Monte Carlo (MCMC) runs as a fixed sequence of compiled steps, so the compiler turns
each call of a computation into steps of the program where the callee's body allows
it. The module below uses each such form: leaky branches between two pure values,
shifted calls another pure computation, jitter draws a site of its own, and
traverse runs jitter once per group.
object Group : FinSet 4
instance random : Random
define leaky(x : Real) : Real !{} =
if x > 0.0 then
return x
else
return 0.1 * x
define shifted(x : Real) : Real !{} =
let rectified <- leaky(x)
return rectified + 1.0
define jitter(x : Real) : Real !{random} =
let z <- perform random.sample[Real](site("z"), Normal(loc=x, scale=0.5))
return z
program grouped : Group -> Group
let a <- sample Normal(loc=0.0, scale=1.0)
let center <- shifted(a)
let base = factor k : Group in center
let effects <- traverse(base, t -> jitter(t))
observe y : Group <- Normal(loc=effects, scale=1.0)
return y
export grouped
shifted(a) becomes let steps, with leaky's conditional read as
select(a > 0.0, a, 0.1 * a). The traverse becomes one draw of the site
effects$z over Group, jitter's site z repeated over the group and named under
the binder effects, as a draw from a program names the
callee's sites. The compiled program is a
MonadicProgram,
and its log joint density and gradients equal those the reference machine (the
library's interpreter for QIEC) computes:
import torch
from quivers.dsl import load
from quivers.inference import MCMC, NUTSKernel
program = load("grouped.qvr").morphism
result = MCMC(NUTSKernel(), num_warmup=300, num_samples=500, seed=0).run(
program, torch.zeros(1, 1), {"y": torch.tensor([2.0, 3.0, 2.5, 3.5])}
)
result.samples["effects$z"].shape # (chains, draws, 4)
A callee that installs authored handlers compiles too when its handlers can be
discharged, each request
replaced by its clause's answer, and a callee that returns a product is read by
component, as p[0] and p[1]. The compiled runtime refuses define-computation
calls that recurse, draw or score inside a conditional, install handlers it cannot
discharge, use case on a family value, or require static arguments. Such calls
still run on the reference machine (Program.run),
and every fitting entry point raises a
CompileError at the call
saying why and what to write instead
(which calls are fit).
Program calls with static arguments use the specialization described under
parametric programs.
Draws¶
A distribution draw uses let x <- sample F(...), with the bound name giving the site its label.
The draw binds x to a value from the checked distribution. The same binding form
also instantiates a template with program or applies a static recipe with apply.
An addressed call such as let x <- f(args) uses the callee’s checked signature and
retains its call frame.
Indexed draws (plates)¶
A plate repeats a draw over an index
set (Koller and Friedman, 2009). let v : A <- sample F(args) draws one value of F for
each element of A, independently given the arguments, so the joint distribution of
v is the product of the distributions of its elements. If Subject has four elements,
let by_subject : Subject <- sample Normal(loc=0.0, scale=sigma) draws four subject
effects. They are independent given the shared scale sigma and become dependent once
uncertainty about sigma is taken into account: one unusually large effect raises the
inferred scale and so widens the plausible range of the others. To make draws
dependent, pass a shared random parameter or choose a family whose event spans the
axes.
object Item : FinSet 1000
object Subject : FinSet 4
object Out : Real 1
program p : Out -> Out
let sigma <- sample HalfNormal(scale=1.0)
let duration_incr : Item <- sample HalfNormal(scale=1.0)
let by_subject : Subject <- sample Normal(loc=0.0, scale=sigma)
return sigma
Indexed observe¶
observe r : N <- F(args) scores observed values r, one per element of N. It
multiplies the execution's weight by \(\prod_{n \in N} p_F(r_n; \theta_n)\), the product
of the family's densities at the observed values, so the log weight grows by the sum
of their log densities. A density can exceed one: a uniform density on an interval of
width 0.1 is 10, and each observation adds log(10). The observed values are passed
at run time in the observations dict.
object RespCloze : FinSet 10
object Out : Real 1
program p : Out -> Out
let intercept_cloze <- sample Beta(concentration1=1.0, concentration0=1.0)
observe cloze_resp : RespCloze <- Bernoulli(probs=intercept_cloze)
return intercept_cloze
Family arguments may index plate variables, as theta[c] below.
Scoped marginalize¶
marginalize c : K <- F(args) followed by an indented block sums the finite latent c
out of the weight of the steps in the block: for each value of c in K it computes
the block's likelihood, weights it by F's probability of that value, and adds the
results, in log space by log-sum-exp
(Pyro Developers, n.d.-b; Stan Development Team, n.d.-b). The annotation K names the latent's values.
Without an over option there is one latent for the whole block, shared by every
observation in it:
object Row : FinSet 6
object Component : FinSet 2
program mixture1 : Row -> Row
let probs <- sample Dirichlet(concentration=2.0) [over=Component]
let mean : Component <- sample Normal(loc=0.0, scale=3.0)
marginalize z : Component <- Categorical(probs=probs)
observe y : Row <- Normal(loc=mean[z], scale=1.0)
return mean
export mixture1
This adds \(\log \sum_k \pi_k \prod_{n} \mathcal N(y_n; \mu_k, 1)\) to the log joint
density, which is not a sum of one term per row, so the rows have a joint score and no
scores of their own. One latent per row, or per group of rows, is the grouped form,
[over=G] on the block with [via=idx] on each observation, described under
grouped marginalization.
Indexed gather in let¶
arr[idx] in a let reads the entries of an array at the positions an integer array
gives. With by_verb = [0.2, -0.1, 0.5] and verb_of_item = [2, 0, 2],
by_verb[verb_of_item] is [0.5, 0.2, 0.5]: items 0 and 2 share the realized effect of
verb 2. Drawing an effect per item instead would be a different model, and the index
array is what keeps the sharing. Formally, for values \(v : A \to B\) of a plate over \(A\)
and an index map
\(\iota : N \to A\), the gather is the composite
\(v \circ \iota : N \to B\).
object Verb : FinSet 3
object Item : FinSet 5
object Out : Real 1
program p : Out -> Out
let sigma <- sample HalfNormal(scale=1.0)
let by_verb : Verb <- sample Normal(loc=0.0, scale=sigma)
let intercept_for_item = by_verb[verb_of_item]
observe y : Item <- Normal(loc=intercept_for_item, scale=1.0)
return sigma
Here verb_of_item is a data input,
an index array given at run time.
A subscript with several indices reads them together. With index vectors row_of and
col_of over the same rows, table[row_of, col_of] is one entry per row,
table[row_of[n], col_of[n]] at row n, as NumPy's advanced indexing and a loop over
rows in Stan read it. The result has the index vectors' shape followed by the axes of
table the subscript leaves. An Int index, a literal or a factor binder, stands for
the same value at every row, so table[row_of, 1] reads the second column at each row.
Index vectors of two different shapes in one subscript are refused, and a single index
vector, table[row_of], reads whole rows. A matrix indexed by (row, column) pairs, as
in matrix factorization or an item-response table, is thus one let:
object User : FinSet 4
object Item : FinSet 5
object Rating : FinSet 12
object Out : Real 1
program p : Out -> Out
let user : User <- sample Normal(loc=0.0, scale=1.0)
let item : Item <- sample Normal(loc=0.0, scale=1.0)
let affinity = factor u : User, i : Item in user[u] * item[i]
let mu = affinity[user_of, item_of]
observe rating : Rating <- Normal(loc=mu, scale=1.0)
return mu
Parametric programs¶
A program's static telescope in brackets uses the QIEC type, effect, and index binders, extended with object and morphism kinds. Its runtime telescope in parentheses receives values at the specialized types. The relevant forms are:
| Kind | Stands for |
|---|---|
[G : FinSet], [X : Space], [X : Object] |
a static object, such as a plate's index set |
[k : Mor[A, B]] |
a static morphism A -> B, passed by name |
[n : Nat] |
a static extent |
[A : Type], [E : Effect] |
a static QIEC type or effect interface; executor support is checked separately |
(scale : Real), (count : Nat) |
a runtime scalar, such as a hyperparameter |
(x : Real[Fin G]) |
a runtime tensor over the specialized object G |
The compiler does not build a MonadicProgram for a template by itself. It inlines the
template at each call, let v <- program template[static_args](value_args), substituting the
static arguments and binding runtime values, then renaming the template's internal names with the prefix
v$ and its returned value to v, as α-renaming in the
lambda calculus does. Each call thus
adds its own latent variables to the caller's joint, and two calls never share one by
accident.
A typed runtime parameter does not itself make a program a static template.
It may declare a covariate, response, or scalar input, and its uses determine its
role. This is the same two-stage interface used by a define computation; the
parameter reference explains
the corresponding conventions for effects, handlers, and indexed families.
The substitution reaches every position a name can occupy in a let or score
expression and in the arguments of a computation call: the array and the indices of
arr[idx], call arguments, method receivers, list and tuple items, lambda and factor
bodies, and an object parameter used as a factor binder's index set. It avoids
capture: a lambda parameter or factor binder shadows a parameter or local with the
same name and is renamed if a substituted name would otherwise be captured.
object Subject : FinSet 4
object Out : Real 1
# One HalfNormal scale and a Normal(0, sigma) plate over the grouping
# object G, with the half-normal scale as a parameter.
program random_intercepts[G : FinSet](prior_scale : Real) : G -> Real 1
let sigma <- sample HalfNormal(scale=prior_scale)
let v : G <- sample Normal(loc=0.0, scale=sigma)
return v
program uses : Out -> Out
let a <- program random_intercepts[Subject](1.0)
return a
Hierarchical Programs works through crossed random intercepts and grouped marginalization.
Derived quantities of the posterior¶
A program can compute a derived quantity from the latent variables of another program,
like Stan's generated quantities (Carpenter et al., 2017). posterior_of= in its option block names
the model whose latents it reads, and its parameters name those latents. Its body must
be pure (effects=[Pure]). Given data, its output is distributed as the pushforward of
the model's posterior along it: draw the latents from the posterior given the data, then
apply the program.
object Item : FinSet 2
object Out : Real 1
program scored : Item -> Out
let logit <- sample Normal(loc=0.0, scale=1.0)
return logit
program success_prob(logit : Real) : Item -> Out [effects=[Pure], posterior_of=scored]
let prob = sigmoid(logit)
return prob
The parameter logit names the model's latent and gives its type, and at run time each
posterior draw of the model's trace
gives its value. An unannotated parameter inherits the type of the model's latent
variable, including its vector dimensions.
Event and batch axes: over and iid_over¶
A draw's axes are of two kinds (Pyro Developers, n.d.-e). Event axes belong to one outcome
of the family, and batch axes index independent outcomes. A probability vector over
three topics is one Dirichlet outcome, so Topic is an event axis, and ten documents
each with such a vector make Doc a batch axis: ten vectors, each summing to one,
independent given their parameters. over=Topic names the event axis, and the plate
annotation : Doc or iid_over=Doc names the batch axis. A family's number of event
dimensions (Continuous Distribution Families)
fixes how many axes one outcome has. It is 0 for scalar families such as Normal, Beta,
and Gamma, 1 for vector families such as
MultivariateNormal,
Dirichlet,
GP (ConditionalGaussianProcess),
and Horseshoe,
and 2 for matrix families such as
MatrixNormal,
Wishart,
and InverseWishart.
The clause goes in the option block of every form that carries a family. On a
morphism it precedes the ~ initializer, and on a sample, observe, or
marginalize step it follows the family:
morphism NAME : A -> B [..., over=<axes>, iid_over=<axes>] ~ Family(args)
let NAME : Plate <- sample Family(args) [over=<axes>, iid_over=<axes>]
over=<axes> names the event axes, one name bare (over=A) and several in a list
(over=[A, B]). The order of the axes follows the family's event axes, so for
MatrixNormal the first is the row axis and the second the column axis. On a
morphism the number of axes must equal the family's number of event dimensions.
On a sample or observe step, over may list more axes than that. The leading
extra axes then index independent outcomes, and the trailing ones are the event axes:
let w : A <- sample Normal(loc=0.0, scale=1.0) [over=[B, C]] draws an (A, B, C) array of
independent normals, and let p : A <- sample Dirichlet(concentration=1.0) [over=[B, K]]
draws one K-simplex point per element of A and B, an (A, B, K) array. The site
holds every listed axis in the order written, after the plate, so a body reads it as
w[a, b, c], and the extra axes are batch axes as if listed in iid_over. A matrix
family takes no such extra axes.
iid_over=<axes> names the batch axes and is optional: every axis not in over is a
batch axis anyway, with independent draws along it. On a sample step, the plate
annotation : A beside an over clause is the batch plate, so
let rows : Doc <- sample Dirichlet(concentration=1.0) [over=Topic] draws one point of
the Topic simplex per document. Without over, the annotation on a vector family
names the family's own axis when nothing else fixes it
(let pi : K <- sample Dirichlet(concentration=1.0) draws one K-simplex point) and the
batch plate when the arguments fix the event
(let pc : Item <- sample Dirichlet(concentration=[1.0, 2.0, 3.0]) draws one point of the
three-simplex per item).
A vector family's parameter is named like every other parameter:
Dirichlet(concentration=[1.0, 2.0, 3.0]) gives an explicit vector, and
Dirichlet(concentration=1.0) a symmetric concentration at the dimension the step's
annotation, its over axis, or the program's codomain fixes. A draw that fixes no
simplex dimension is refused at compile time.
Axis names. Names resolve against the named factors of the surrounding morphism's
domain and codomain, or against the type annotation of a step. dom and cod stand
for a side that is a single object, and the factors of a product must be named.
Joint versus independent axes. The clause keeps apart a joint distribution on a product of axes, with possible correlation across them, and independent draws along an axis. It also keeps apart a multivariate normal over all \(|A| \cdot |B|\) entries with a dense covariance and a matrix normal, whose covariance is the Kronecker product \(V \otimes U\) of a column and a row covariance. The compiler never substitutes a family of a different number of event dimensions, and renaming a morphism's type makes stale axis names fail at type checking.
Errors. Two diagnostics come from this clause:
- "family 'F' has event_rank r, but
overlists n axis name(s)": on amorphism,overlists a number of axes different from the family's number of event dimensions, as happens when a flatMultivariateNormal(one event dimension) is replaced by aMatrixNormal(two). - "unknown axis name(s) ['A'] in
over": a name inoveris not a factor of the domain or codomain. Rename it, or usedomorcodfor a side that is one object.
object Dk : FinSet 5
object N : FinSet 7
object D : FinSet 3
object K : FinSet 2
object Out : Real 1
# A matrix normal prior on a morphism's 3 x 2 tensor
morphism W : D -> K [role=parameter, over=[dom, cod]] ~ MatrixNormal(loc=0.0, row_covariance=1.0, col_covariance=1.0)
program p : Out -> Out
# A five-dimensional multivariate normal, then one per observation row
let mu <- sample MultivariateNormal(loc=m0, scale_tril=L) [over=Dk]
observe y : N <- MultivariateNormal(loc=mu, scale_tril=L) [over=Dk]
return mu
Here m0 and L are data inputs.
Let expressions (arithmetic, primitives, and collections)¶
In a program block or a define computation, let binds the value of an
expression: arithmetic with the usual precedence, unary negation, the primitives
below, and finite collection operations. Each primitive is a deterministic function, a
Dirac kernel in the program's
semantics.
object Row : FinSet 3
object Out : Real 1
program p : Out -> Out
let mu <- sample Normal(loc=0.0, scale=1.0)
let sigma <- sample HalfNormal(scale=1.0)
let z_raw <- sample Normal(loc=0.0, scale=1.0)
let lapse <- sample Beta(concentration1=1.0, concentration0=9.0)
# arithmetic: +, -, *, /
let eta = mu + sigma * z_raw
let p_raw = sigmoid(eta)
let adjusted = (1.0 - lapse) * p_raw + 0.5 * lapse
let negated = -eta
# primitives
let positive = softplus(eta)
let magnitude = abs(p_raw - 0.5)
observe y : Row <- Bernoulli(probs=adjusted)
return mu
Checked primitive reference¶
The primitives below are those of the checked core, and the compiler resolves each
operator and call by the types of its arguments. A reduction collapses the last axis of
its argument, and a rowwise operation acts along that axis and keeps the shape. For a
vector over a plate, as in sum(theta) over theta : School, the last axis is the only
one and the result is the plate's total. For a plate of event vectors, as in
sum(z_row * w_row) over a Resp-plated value with an [over=LatentDim] event axis,
the result keeps the plate and holds one inner product per row. For a table a factor
builds over Row and Unit, the result holds one number per Row. normalize divides
each row by its sum. The compiled runtime, the
reference machine (the library's interpreter for QIEC), and
every transpile target read the primitives in
this way, and a contraction declaration reduces over a named
axis.
Conversions and weights: real, int, weight, weight_value,
observation_weight, scale_weight.
observation_weight(w) checks a finite nonnegative Real and returns it.
scale_weight(w, ell) takes a Real and a LogWeight, returning zero when
w is zero and their product otherwise. These are checked QIEC primitives;
their validation and zero-weight rule are part of their meaning. For a Real
log density, write
weight_value(scale_weight(observation_weight(w), weight(ell))).
The observation option
uses the same operations before reducing rows or groups.
Arithmetic and comparisons: +, -, *, /, %, ==, !=, <, <=, >,
>=, &&, ||, and the call forms pow, abs, min, and max. The
min(a, b) and max(a, b) with two arguments are pointwise, unlike the reductions of one argument below: two
tensors compare entry by entry, a number broadcasts against a tensor, and two Int
operands give an Int, so ys[max(t - 1, 0)] clamps an index expression into range.
Conditional values and Booleans as numbers: select(c, a, b) takes a where the
Bool condition c holds and b elsewhere, entry by entry. A number among the three
broadcasts against the tensors, and the tensors share one shape, so
select(v > 0.0, v, 0.1 * v) is a leaky rectifier of a vector. Both branches have one
type, Real or Int (write 0.0, not 0, beside a real branch). real(c) and
int(c) read a Bool as one where it holds and zero elsewhere, so a mask needs no
integer clamp:
object Pos : FinSet 4
object Out : Real 1
program p : Out -> Out
let margin <- sample Normal(loc=0.0, scale=1.0)
let causal = factor i : Pos, j : Pos in real(j <= i)
let hinge = select(margin < 1.0, 1.0 - margin, 0.0)
return hinge
select evaluates both branches and then keeps one entry of either, as torch.where
does, so it does not guard a computation. A branch that is NaN or infinite where it
is not taken is still computed, and its gradient there is zero times an infinite
derivative, which is NaN. Where the untaken branch is finite its gradient is zero.
When the condition is what keeps a branch in range, write the branch so that it is
finite everywhere, log(max(v, 1e-12)) rather than log(v).
Pointwise functions: exp, expm1, log, log1p, log2, log10, sqrt,
rsqrt, square, abs, sign, reciprocal, sin, cos, tan, asin, acos,
atan, sinh, cosh, asinh, acosh, atanh, floor, ceil, round, trunc,
erf, erfc, erfinv, lgamma, digamma.
Activations: relu, relu6, elu, selu, gelu, silu, mish, softplus,
logsigmoid, softsign, sigmoid, and tanh.
round rounds a tie to even. A transpile target computes each pointwise primitive with
the same meaning, through its own function where it has one and otherwise through a
definition in terms of others (gelu(x) as 0.5 * x * (1 + erf(x / sqrt(2)))), and a
primitive that a target cannot express is refused by name at transpilation
(Transpiler support).
Reductions over the last axis: sum, mean, min, max, prod, logsumexp.
Operations along the last axis: softmax, log_softmax, cumsum, sort,
normalize.
Joining and cutting the last axis: concat(a, b, ...) joins two or more tensors of
one element type along their last axis. Their leading axes must agree and their last
extents add, so concat(h, c) of two Real 4 vectors has 8 entries and concat of two
(2, 3) tables is (2, 6). slice(v, start, length) keeps the length entries of
the last axis from start, counted from zero. Both bounds are nonnegative integer
literals, since the checked type states the slice's extent, and a position computed at
run time is read with a gather, v[i]. A number joins as the vector with one entry [x]. A
slice past the end of the axis, a zero length, a number where a tensor belongs, and
tensors of different element types or leading axes are refused. This is how a scan
carries a state of several parts as one vector, such as the hidden and cell states of an
LSTM, here updated from its forget, input, candidate, and output gates f, i, g, and o:
define lstm_state(state : Real[Fin 8], f : Real[Fin 4], i : Real[Fin 4], g : Real[Fin 4], o : Real[Fin 4]) : Real[Fin 8] !{} =
let h_prev = slice(state, 0, 4)
let c_prev = slice(state, 4, 4)
let c_new = f * c_prev + i * g
let h_new = o * tanh(c_new)
let state_new = concat(h_new, c_new)
return state_new
The compiled runtime computes these with torch.cat and a slice of the last axis.
Within one run of the compiled runtime, the batch axis of the input is never among a
let's axes, so concat and slice never read it.
Products and normalizations: matmul(a, b) sums the last axis of a against the
first axis of b, which is a vector or a matrix with that axis's extent. A matrix times
a vector is a vector, a vector times a matrix is a vector, a vector times a vector is a
number, and an a of higher rank is multiplied row by row, so matmul(X, W) with
X : (T, D) and W : (D, H) is (T, H). The compiler checks that the two extents
agree. cholesky(a) returns the lower-triangular
Cholesky factor \(L\), with a positive
diagonal and \(L L^\top = a\), of each matrix in the last two axes of a real tensor whose last
two extents agree. Only the lower triangle of \(a\) is read. It costs O(n^3) for an
\(n\)-by-\(n\) matrix and is differentiable, so matmul(cholesky(K), z) with standard-Normal
z draws from \(\mathcal N(0, K)\) in non-centered form. A matrix that is not numerically
positive definite has no factor, and its entries are not numbers, so a sampler rejects that
proposal. Transpile targets refuse cholesky. layer_norm(v) and rms_norm(v) normalize each row along the last axis and keep
the shape: layer_norm(v) is
\((v - \bar v) / \sqrt{\overline{(v - \bar v)^2} + \epsilon}\), with the biased variance as
torch.nn.functional.layer_norm computes it (Ba et al., 2016), and rms_norm(v) is
\(v / \sqrt{\overline{v^2} + \epsilon}\), where a bar is the mean over the last axis. An
optional second argument, a positive real literal, sets \(\epsilon\), which is 0.00001
for layer_norm and 0.000001 for rms_norm when omitted. Neither has a learned gain
or bias, which are multiplied and added in ordinary let steps:
define normed_step(W_in : Real[Fin 4, Fin 3], W_rec : Real[Fin 4, Fin 4], b : Real[Fin 4], x_t : Real[Fin 3], h_prev : Real[Fin 4], gain : Real[Fin 4], shift : Real[Fin 4]) : Real[Fin 4] !{} =
let h = tanh(matmul(W_in, x_t) + matmul(W_rec, h_prev) + b)
let normed = gain * layer_norm(h) + shift
return normed
Finite collections: map, fold, length, and logsumexp_over are pure.
traverse calls a named computation once per value and appears on the right of <-.
The collection reference gives their
typing and shape rules.
The compiled runtime also accepts operations that the checked core lacks:
alpha_dropout, amax, amin, argmax, argmin, celu, clamp, cummax,
cummin, cumprod, dropout, flip, glu, hardshrink, hardsigmoid,
hardswish, hardtanh, leaky_relu, neg, norm, prelu, rrelu, softmin,
softshrink, std, swish, tanhshrink, threshold, and var. A program that uses
one runs in PyTorch, but qvr check reports it, the reference machine cannot run it,
and no transpile target accepts it. qvr check --target TARGET lists what a target
refuses. Calls resolve to these primitives before the module's declarations, and the
compiler checks the number of arguments of a call to a declared callable.
Factor expressions: assembling indexed tensors¶
A factor expression is an array comprehension: it evaluates its body at every tuple of
indices from finite sets and collects the results into a tensor
(Vasilache et al., 2018). Indexing reads one entry of a table, and factor builds the
whole table. For two verbs and three classes, the body runs at the six
(verb, class) pairs and gives a table with those two leading axes.
Uniform form¶
factor v_1 : I_1, v_2 : I_2, ..., v_n : I_n in <body> is the tensor of shape
(|I_1|, ..., |I_n|, *body_shape) whose entry at (i_1, ..., i_n) is <body>
evaluated with \(v_k = i_k\). The binders are integers visible only inside the body.
object Verb : FinSet 40
object Class : FinSet 4
object Out : Real 1
program p : Out -> Out
let coef : Verb <- sample Normal(loc=0.0, scale=1.0) [over=Class]
let weight <- sample Normal(loc=0.0, scale=1.0) [over=Class]
# One score per verb and class: shape (40, 4)
let cell = factor v : Verb, cls : Class in coef[v, cls] * weight[cls]
return weight
Pattern-match form (single-axis)¶
When each entry of a factor is a different expression, the case form lists
them side by side:
object Class : FinSet 4
object Out : Real 1
program p : Out -> Out
let prob_dur <- sample Beta(concentration1=1.0, concentration0=1.0)
let prob_telic_nodur <- sample Beta(concentration1=1.0, concentration0=1.0)
let prob_telic_dur <- sample Beta(concentration1=1.0, concentration0=1.0)
let class_probs = factor cls : Class in {
0 -> (1.0 - prob_dur) * (1.0 - prob_telic_nodur),
1 -> (1.0 - prob_dur) * prob_telic_nodur,
2 -> prob_dur * (1.0 - prob_telic_dur),
3 -> prob_dur * prob_telic_dur,
}
return prob_dur
The labels must cover 0, ..., |Index| - 1 exactly, and the compiler rejects gaps,
duplicates, and labels out of range. Here each entry of a probability vector over four
classes is a different product of scalar latent variables.
Inline distributions¶
sample and observe steps write their family inline, with literal and bound
arguments in any mix:
object Out : Real 1
program p : Out -> Out
# literal arguments
let x <- sample Normal(loc=0.0, scale=1.0)
let prob <- sample Beta(concentration1=2.0, concentration0=5.0)
# bound arguments
let sigma <- sample HalfNormal(scale=1.0)
let y <- sample Normal(loc=x, scale=sigma)
let b <- sample Bernoulli(probs=prob)
# a mix, and a family with differently named parameters
let z <- sample Normal(loc=-1.5, scale=0.3)
let r <- sample TruncatedNormal(mu=x, sigma=sigma, low=0.0, high=1.0)
return y
Some of the registered families (Continuous Distribution Families lists them all):
| Family | Parameters | Codomain |
|---|---|---|
Normal |
loc, scale |
Real N |
LogitNormal |
mu, sigma |
Real N {low=0, high=1} |
Uniform |
low, high |
Real N {low=..., high=...} |
Bernoulli |
probs or logits |
FinSet 2 |
Beta |
concentration1, concentration0 |
Real N {low=0, high=1} |
Exponential |
rate |
Real N {low=0} |
HalfCauchy |
scale |
Real N {low=0} |
HalfNormal |
scale |
Real N {low=0} |
LogNormal |
loc, scale |
Real N {low=0} |
Gamma |
concentration, rate |
Real N {low=0} |
Dirichlet |
concentration |
Simplex K |
TruncatedNormal |
mu, sigma, low, high |
Real N {low=..., high=...} |
MultivariateNormal |
loc, scale_tril |
Real N (one event dimension) |
MatrixNormal |
loc, row_covariance, col_covariance |
an R x C matrix (two event dimensions) |
OrderedProbit, OrderedLogistic |
eta (predictor for OrderedLogistic), cutpoints |
ordered categories |
LKJCholesky |
concentration |
CholeskyFactor K |
Wishart, InverseWishart |
df, scale_tril |
Covariance K |
Horseshoe |
scale |
Real N |
LogitNormal and TruncatedNormal name their location and scale mu and sigma,
where other families use loc and scale. When every argument is a literal the family
is fixed when the program is built, and otherwise it is built from the current values
of the bound arguments at each run.
Data vectors and matrices as family parameters¶
A name that nothing in the program binds is a
data input,
given when the program runs. When a data input fills a vector or matrix parameter of a
draw without batch axes, it is one tensor over the parameter's own axes, and the family
ties those axes to its outcome's: a MultivariateNormal's loc is a vector over the
event axis and its scale_tril a square matrix over it, and a Wishart's scale_tril
is a matrix over the outcome's two axes. The draw sizes each tied axis from what it
already knows: the event axes an over clause names, or another argument of known size,
such as a sampled mean. An axis that nothing in the draw sizes has one extent, shared by
every input tied to it and fixed by a later use, such as the over event axis of a
draw that reads the value, or the Real N codomain of a program that returns it.
object Dk : FinSet 3
object Row : FinSet 4
object D : Real 3
program shrinkage : Row -> D
# m0 is a 3-vector and L a 3 x 3 factor: the plated observe's
# event axis Dk fixes the extent the two data share.
let mu <- sample MultivariateNormal(loc=m0, scale_tril=L)
observe y : Row <- MultivariateNormal(loc=mu, scale_tril=L) [over=Dk]
return mu
export shrinkage
The program then runs with the data at those sizes, on the compiled runtime and on the reference machine:
import torch
from quivers.dsl import loads
program = loads(source)
data = {"m0": torch.zeros(3), "L": torch.eye(3), "y": torch.randn(4, 3)}
mu = torch.zeros(3)
compiled = program.morphism.log_joint(torch.zeros(1, 1), {**data, "mu": mu})
checked = program.run(
"shrinkage",
data={name: value.tolist() for name, value in data.items()},
sites={"mu": mu.tolist()},
).log_joint
A data input of another size is refused on both paths, with a message naming the input
and the size the program reads it at. One input read at two sizes, or a tied argument
whose size differs from the event axis it spans, is refused at the draw. A data input
filling a vector parameter of a draw whose plate names event axes is instead one value
that the plate broadcasts, as in Dirichlet(concentration=alpha) [over=K]. A matrix
parameter (a Cholesky factor, a covariance, or a matrix normal's mean) is never a
broadcast scalar and stays a matrix. The compiled runtime sizes every parameter when
it builds the program, so it refuses a multivariate draw whose event size nothing in the
program fixes, and its message lists the ways to fix it.
Ordinal, inverse-Wishart, and matrix-normal draws¶
An ordinal response under a probit link (McCullagh, 1980), a covariance drawn from an inverse Wishart, and a matrix drawn from a matrix normal are written inline like their logistic and Wishart counterparts:
object Row : FinSet 6
object One : FinSet 1
object Dim : FinSet 2
object Rows : FinSet 2
object Cols : FinSet 3
program ordinal : One -> One
let eta <- sample Normal(loc=0.0, scale=1.0)
observe y : Row <- OrderedProbit(eta=eta, cutpoints=[-1.0, 0.0, 1.5])
return eta
program covariance : One -> One
let sigma : Dim <- sample InverseWishart(df=5.0, scale_tril=[[1.0, 0.0], [0.3, 0.8]])
observe x : Row <- MultivariateNormal(loc=[0.0, 0.0], covariance_matrix=sigma) [over=Dim]
return sigma
program regression : One -> One
let B <- sample MatrixNormal(loc=B0, row_covariance=U, col_covariance=V) [over=[Rows, Cols]]
return B
export ordinal
export covariance
export regression
OrderedProbit gives category \(k\) probability \(\Phi(c_k - \eta) - \Phi(c_{k-1} - \eta)\),
with \(\Phi\) the standard normal distribution function and \(c\) the cutpoints, computed
in log space so a category far from the predictor keeps its probability.
InverseWishart(df, scale_tril=L) is the distribution of the inverse of a
Wishart(df, scale_tril=L) draw, which is the
inverse Wishart
\(\mathcal W^{-1}(\mathrm{df}, \Psi)\) with \(\Psi = (L L^\top)^{-1}\).
MatrixNormal(loc, U, V) draws an \(R \times C\) matrix whose row-major entries have
covariance \(U \otimes V\), the
matrix normal with row
covariance \(U\) and column covariance \(V\). An inverse-Wishart covariance is a
positive-definite latent site, which MCMC and SVI move through the unconstrained
coordinates of its Cholesky factor.
A Cholesky factor built in the program¶
The usual hierarchical covariance prior draws an LKJ correlation factor (Lewandowski et al., 2009), scales its rows by standard deviations, and passes the product to a multivariate normal as its Cholesky factor:
object Dk : FinSet 3
object Row : FinSet 4
object One : FinSet 1
program correlated : One -> One
let L : Dk <- sample LKJCholesky(concentration=2.0)
let sigma : Dk <- sample HalfNormal(scale=1.0)
let mu : Dk <- sample Normal(loc=0.0, scale=1.0)
let Ls = factor i : Dk, j : Dk in sigma[i] * L[i, j]
observe y : Row <- MultivariateNormal(loc=mu, scale_tril=Ls) [over=Dk]
return mu
export correlated
Both the compiled runtime and the reference machine score it. Which transpile targets accept a Cholesky factor known only at run time is listed in Transpiler support.
Alternative parameterizations¶
Several families accept more than one complete set of parameters, and the keywords a
draw writes select one. A draw that names two, such as Bernoulli(probs=p, logits=l),
is refused.
| Family | Keyword sets |
|---|---|
MultivariateNormal |
(loc, scale_tril), (loc, covariance_matrix), (loc, precision_matrix) |
Wishart |
(df, scale_tril), (df, covariance_matrix), (df, precision_matrix) |
Bernoulli, ContinuousBernoulli, Geometric, Categorical |
(probs), (logits) |
Binomial, NegativeBinomial |
(total_count, probs), (total_count, logits) |
object Row : FinSet 6
object One : FinSet 1
object Dim : FinSet 2
program logistic : One -> One
let eta <- sample Normal(loc=0.0, scale=2.0)
# The log-odds go straight to the family: no sigmoid in a let.
observe y : Row <- Bernoulli(logits=eta)
return eta
program gaussian_precision : One -> One
let v <- sample MultivariateNormal(loc=[0.0, 0.0], precision_matrix=[[2.0, -0.5], [-0.5, 1.0]]) [over=Dim]
return v
export logistic
export gaussian_precision
The compiled runtime and the reference machine score each parameterization as
torch.distributions does with the same keywords. Logits are scored in log space, with
\(\log p = -\mathrm{softplus}(-l)\) and \(\log(1 - p) = -\mathrm{softplus}(l)\), so an
observation stays finite where the probability itself rounds to zero or one, which in
single precision happens near \(|l| = 30\). A ContinuousBernoulli given by its logits
is scored with its normalizer written in the logit, \(l / \tanh(l / 2)\), for the same
reason. Transpiler support lists which targets accept which
parameterization.
A mixture weight that is the logistic function of a linear predictor is best given on
the same scale. At a marginalize head, a Bernoulli given by its logits weighs the
values 0 and 1 by \(\log \sigma(-l)\) and \(\log \sigma(l)\), computed in log space, where
a probability written sigmoid(eta) (or 1.0 - sigmoid(eta)) would be rounded in
single precision first and lose its digits once \(|\eta|\) passes about 10. A
zero-inflated Poisson regression (Lambert, 1992) thus gives its gate the linear
predictor itself:
object Row : FinSet 4
object One : FinSet 1
program zip : Row -> One
let a <- sample Normal(loc=0.0, scale=5.0)
let b <- sample Normal(loc=0.0, scale=5.0)
# The gate's log-odds: no sigmoid, and no `1.0 - ...` complement.
let eta = a + b * x
marginalize z : Row <- Bernoulli(logits=eta)
let gated_rate = real(z) * 3.0
observe y : Row <- Poisson(rate=gated_rate)
return a
export zip
With the covariate x a data input over Row, the log-odds vary by row, each row has
its own gate, and the block adds
\(\sum_n \log\bigl[(1 - \sigma(\eta_n))\, \mathbb 1[y_n = 0] + \sigma(\eta_n)\, \mathrm{Poisson}(y_n; 3)\bigr]\)
to the log joint density. With a single scalar log-odds instead, the block has one gate
shared by all rows. A gate uses Bernoulli: ContinuousBernoulli and
RelaxedBernoulli have densities on \([0, 1]\) rather than two atoms, so a sum over the
values 0 and 1 is not their marginal.
A conditional distribution whose parameters a network learns from the input is a
morphism f : A -> B ~ Family declaration instead.
Examples¶
Simple discrete model¶
object X : FinSet 3
object Y : FinSet 4
morphism f : X -> Y [role=parameter]
morphism g : Y -> Y [role=parameter]
define fg = f >> g
export fg
Continuous conditional model¶
object Cond : FinSet 2
object Latent : Real 3
object Obs : Real 5
morphism prior : Cond -> Latent ~ Normal
morphism likelihood : Latent -> Obs ~ Normal
define predictive = prior >> likelihood
export predictive
Probabilistic program with observations¶
object Data : FinSet 1
object Y : Real 1
program regression : Data -> Y
let theta <- sample LogitNormal(mu=0.0, sigma=1.0)
let y <- sample Normal(loc=theta, scale=0.5)
observe y_obs <- Normal(loc=y, scale=0.1)
return y
export regression
Init recipe end-to-end¶
object In : FinSet 8
object Out : FinSet 4
# Default: weights initialized from a small-scale randn.
morphism W_default : In -> Out [role=parameter, scale=0.1]
# init=auto: the initialization recipe of the module's Algebra,
# applied after the sigmoid under product_fuzzy (the default).
morphism W_auto : In -> Out [role=parameter, init=auto]
export W_auto
The Examples Gallery has complete models, and
Semantics gives what .qvr programs denote.
Cited sources¶
Conor McBride. Elimination with a Motive. In Types for Proofs and Programs (TYPES 2000), Lecture Notes in Computer Science, 197–216. Springer, 2002. doi:10.1007/3-540-45842-5_13.
Daan Leijen. Koka: Programming with Row Polymorphic Effect Types. In Proceedings of the 5th Workshop on Mathematically Structured Functional Programming (MSFP), volume 153 of Electronic Proceedings in Theoretical Computer Science, 100–126. 2014. doi:10.4204/EPTCS.153.8.
Daphne Koller and Nir Friedman. Probabilistic Graphical Models: Principles and Techniques. MIT Press, 2009. ISBN 9780262013192. URL: https://mitpress.mit.edu/9780262013192/probabilistic-graphical-models/.
Pyro Developers. Inference with discrete latent variables. Pyro Tutorials, version 1.9.1, n.d.-b. URL: https://pyro.ai/examples/enumeration.html (visited on 2026-11-06).
Stan Development Team. Finite mixtures. Stan User's Guide, version 2.40, n.d.-b. URL: https://mc-stan.org/docs/stan-users-guide/finite-mixtures.html (visited on 2026-11-06).
Bob Carpenter, Andrew Gelman, Matthew D. Hoffman, Daniel Lee, Ben Goodrich, Michael Betancourt, Marcus Brubaker, Jiqiang Guo, Peter Li, and Allen Riddell. Stan: A Probabilistic Programming Language. Journal of Statistical Software, 2017. URL: https://www.jstatsoft.org/article/view/v076i01, doi:10.18637/jss.v076.i01.
Pyro Developers. Tensor shapes in Pyro. Pyro Tutorials, version 1.9.1, n.d.-e. URL: https://pyro.ai/examples/tensor_shapes.html (visited on 2026-11-06).
Jimmy Lei Ba, Jamie Ryan Kiros, and Geoffrey E. Hinton. Layer Normalization. arXiv preprint arXiv:1607.06450, 2016. URL: https://doi.org/10.48550/arXiv.1607.06450, doi:10.48550/arXiv.1607.06450.
Nicolas Vasilache, Oleksandr Zinenko, Theodoros Theodoridis, Priya Goyal, Zachary DeVito, William S. Moses, Sven Verdoolaege, Andrew Adams, and Albert Cohen. Tensor Comprehensions: Framework-Agnostic High-Performance Machine Learning Abstractions. 2018. arXiv:1802.04730, doi:10.48550/arXiv.1802.04730.
Peter McCullagh. Regression Models for Ordinal Data. Journal of the Royal Statistical Society: Series B (Methodological), 42(2):109–127, 1980. URL: https://academic.oup.com/jrsssb/article/42/2/109/7027621, doi:10.1111/j.2517-6161.1980.tb01109.x.
Daniel Lewandowski, Dorota Kurowicka, and Harry Joe. Generating random correlation matrices based on vines and extended onion method. Journal of Multivariate Analysis, 100(9):1989–2001, 2009. URL: https://www.sciencedirect.com/science/article/pii/S0047259X09000876, doi:10.1016/j.jmva.2009.04.008.
Diane Lambert. Zero-Inflated Poisson Regression, With an Application to Defects in Manufacturing. Technometrics, 34(1):1–14, 1992. URL: https://www.tandfonline.com/doi/abs/10.1080/00401706.1992.10485228, doi:10.1080/00401706.1992.10485228.