Transpilation architecture¶
The package quivers.transpile translates a QVR module \(M\) into source text for another probabilistic
programming language \(\mathsf T\): Stan, NumPyro, Pyro, PyMC, Edward2, Turing.jl, Gen.jl, Church,
WebPPL, BUGS, or JAGS. A translation must keep the model's factors, shapes, and supports while the
notation changes. A Normal with scale=2 may become a constructor that takes a standard deviation of
\(2\) or one that takes a precision of \(1/2^2 = 0.25\), and copying the number without converting it
would change the model. The pipeline below keeps meaning and spelling apart: an intermediate
representation (IR) records what each statement means, and a renderer for each target decides how to
write it. What the evidence for a correct translation is, and how it is checked, is on
Transpilation correctness.
1. The pipeline¶
Checkelaborates the module to the Quivers Indexed Effect Core (QIEC): it compiles the indexed family declarations, elaborates every program that is an entry point to a named computation, and checks the whole module (Typing and checking). It does not depend on the target.Planreads each program's computation and recognizes the statements a probabilistic programming language has: anIRSamplefor eachRandom.samplerequest, anIRObservefor each density scored at an observation, anIRDeterministicfor each pure binding, anIRScorefor each other weight, and anIRMarginalizefor each latent variable summed out. Each node carries the support and plate shape read from the module's types, fromFAMILY_META, and fromarg_constraintsof the PyTorch distribution class. The inputs of the plan are the parameters of the entry point. The result is oneIRProgram, which also holds the whole checked module as a typed tree,IRQiecModule. A module with no program lowers to its module and an empty plan. Planning also checks target-specific representability, such as whether a target accepts per-row vector parameters; these checks can refuse a plan before rendering.Render[T]is one class for each target (StanRenderer,NumPyroRenderer,PyMCRenderer, and so on). It turns the plan into a panproto schema of the target's syntax tree, using the support predicates of §2.3 and the entries ofFAMILY_META, and then renders the module's other computations. Before it places a computation,AnalyzeQIEC[T]lists the QIEC features the computation uses (open or effectful rows, static polymorphism, indexed cases, transport, attachments, resumption grades) and compares them with the features the target supports. A missing feature stops the rendering with a diagnosticqiec:capability:<feature>:<computation>that keeps the computation's source location.Pretty[T]ispanproto.AstParserRegistry.emit_prettyfor the target's tree-sitter grammar. It prints the schema as source text.
The compiler of the runtime checks the same module and attaches its QiecModule to the compiled
Program, but transpilation does not use that object. transpile() classifies the source, and
Lower.forward() elaborates and checks the syntax tree itself, so a direct call of Lower is checked
the same way. Source typing is independent of the target. Planning checks the
representability of structural statements and inputs; rendering checks the needed
QIEC computations and the declaration forms available in the target.
2. The IR¶
The IR is defined in quivers.transpile.ir. Every entry is a didactic
Model or TaggedUnion, and the IR holds structure only: no strings of a target language, no schema
vertices, and no panproto types.
2.1 The typed QIEC module¶
IRProgram.module is an IRQiecModule, a structural copy of the checked module without loss: every
declaration, stable identifier, source location, row and lacks constraint, value, equality witness,
transport, computation node, handler clause, and resumption grade has its own typed representation, so
a renderer never parses a serialized string.
Pyro, NumPyro, PyMC, Edward2, Turing, Gen, WebPPL, and Church run this tree through an implementation of the QIEC runtime written for each target: free computations, handlers bound to instances, stable addresses of requests, constructors, case analysis on indexed families, attachments, and checks of resumption grades. Tests parse the source emitted for every target and, where the target's runtime is installed, compare its requests and results with the reference machine, the library's interpreter for QIEC.
Stan, BUGS, and JAGS render no handlers, so
project_for_target
gives them the module with its authored handlers discharged, and a
computation that keeps a handler carries the obstacle that names it. They accept a static subset of
that module, which the analysis checks: each computation has a closed,
empty row and no static parameters, returns a scalar, and uses the scalar
computation forms that the target's capability analysis accepts. Stan admits
value parameters of types Bool, Int, and Real, including checked scalar
conditionals. BUGS and JAGS require computations without
parameters, since their models have no callable functions with parameters. Every other QIEC feature gets
a capability diagnostic, and no renderer treats an executable term as metadata it may ignore.
2.2 Plate: event and batch axes¶
Ten draws from a three-dimensional multivariate Normal have batch shape \((10)\) and event shape \((3)\) (event and batch axes) (Pyro Developers, n.d.-e). A target may write them as one vectorized draw or as ten iterations of a loop, but it must keep the three coordinates of each draw joint: with a covariance that is not diagonal, the density has terms that couple coordinates, which three scalar densities would lose. A draw's plate records the two kinds of axes:
class Plate(dx.Model):
event_dims: tuple[Dim, ...]
batch_dims: tuple[Dim, ...]
class Dim(dx.TaggedUnion, discriminator="kind"):
name: str
class DimStatic(Dim):
size: int
kind: Literal["static"] = "static"
class DimDynamic(Dim):
size_name: str
kind: Literal["dynamic"] = "dynamic"
event_dims comes from the draw's over axes and batch_dims from its iid_over axes, in source
order. A renderer writes the batch axes in the target's own form: nested for loops in Stan, nested
plate contexts in NumPyro and Pyro, dims=(...) in PyMC, and filldist or arraydist in Turing and
Gen.
2.3 Classifying supports¶
quivers.transpile.ir defines predicates on the support constraints of PyTorch
(torch.distributions.constraints.Constraint):
def is_real_scalar(c: Constraint) -> bool: ...
def is_real_positive(c: Constraint) -> bool: ...
def is_real_unit_interval(c: Constraint) -> bool: ...
def is_real_vector(c: Constraint) -> bool: ...
def is_real_simplex(c: Constraint) -> bool: ...
def is_real_cov_matrix(c: Constraint) -> bool: ...
def is_real_corr_chol(c: Constraint) -> bool: ...
def is_real_matrix(c: Constraint) -> bool: ...
def is_real_one_hot(c: Constraint) -> bool: ...
def is_int_bit(c: Constraint) -> bool: ...
def is_int_category(c: Constraint) -> bool: ...
def is_int_count(c: Constraint) -> bool: ...
A renderer derives declarations from the structural support, its predicates,
and its bounds. Family-specific spelling remains in the family registry.
A PyTorch constraint does not survive didactic's encoding and decoding, so the
IR stores a structural copy, ConstraintSpec. from_constraint builds this
copy for distribution supports, and .to_constraint() reconstructs the PyTorch
constraint when a renderer needs it.
Pure bindings and retained computation calls instead obtain their scalar
carrier directly from the checked binder type: Bool becomes CSBoolean,
Int becomes CSInteger, and Real becomes CSReal. Tensor axes remain in
Plate; inspecting the expression's syntax is unnecessary. A runtime Nat
input retains its nonnegative boundary restriction as CSNonnegativeInteger,
while integer arithmetic on that input has the unrestricted integer carrier.
These records survive serialization and target planning.
2.4 IRArg: arguments as a typed tree¶
The parser leaves indexed arguments as strings such as "phi[z]". Lower parses them into a tree, so
that no renderer parses strings:
class IRArg(dx.TaggedUnion, discriminator="kind"):
...
class IRArgNumber(IRArg):
value: int | float
kind: Literal["number"] = "number"
class IRArgRef(IRArg):
name: str
indices: tuple[IRArg, ...] = ()
kind: Literal["ref"] = "ref"
class IRArgBroadcast(IRArg):
"""A scalar broadcast to satisfy an arg's expected constraint."""
value: IRArg
target_shape: tuple[int, ...]
kind: Literal["broadcast"] = "broadcast"
class IRArgList(IRArg):
elements: tuple[IRArg, ...]
kind: Literal["list"] = "list"
class IRArgMatrix(IRArg):
rows: tuple[IRArgList, ...]
kind: Literal["matrix"] = "matrix"
class IRArgFamilyRef(IRArg):
"""A reference to a morphism whose ``~ Family(...)`` init clause
names the wrapped distribution (used by Truncated, Mixture,
Independent, Transformed, LKJCorrelationFactor)."""
name: str
kind: Literal["family_ref"] = "family_ref"
The IR also has IRArgKernel for a kernel matrix given as an argument. Lower wraps a scalar argument in
IRArgBroadcast when the argument's constraint is IndependentConstraint(base, n) with \(n \ge 1\), that is,
when the family expects a vector there. Each renderer writes the broadcast in its target:
rep_vector(x, K) in Stan, jnp.full((K,), x) in NumPyro, torch.full((K,), x) in Pyro,
np.full((K,), x) in PyMC, fill(x, K) in Turing and Gen, repeat(K, function() { return x; }) in
WebPPL, and (make-list K x) in Church.
2.5 IRNode: the statements of a program¶
class IRNode(dx.TaggedUnion, discriminator="kind"):
...
class IRDataInput(IRNode):
name: str
constraint: ConstraintSpec
plate: Plate
kind: Literal["data_input"] = "data_input"
class IRSample(IRNode):
name: str
family: str
args: tuple[IRArg, ...]
arg_names: tuple[str, ...]
constraint: ConstraintSpec
plate: Plate
kind: Literal["sample"] = "sample"
class IRObserve(IRNode):
name: str
family: str
args: tuple[IRArg, ...]
arg_names: tuple[str, ...]
constraint: ConstraintSpec
plate: Plate
via: str | None
kind: Literal["observe"] = "observe"
class IRDeterministic(IRNode):
name: str
expr: IRExpr
constraint: ConstraintSpec
plate: Plate
kind: Literal["deterministic"] = "deterministic"
class IRScore(IRNode):
name: str
expr: IRExpr
kind: Literal["score"] = "score"
class IRMarginalize(IRNode):
latent: str
family: str
args: tuple[IRArg, ...]
arg_names: tuple[str, ...]
constraint: ConstraintSpec
plate: Plate
reduction: Literal["logsumexp"]
scope: tuple[IRNode, ...]
kind: Literal["marginalize"] = "marginalize"
class IRCall(IRNode):
name: str
callee: str
static_arguments: tuple[IRQiecStatic, ...]
arguments: tuple[IRExpr, ...]
random_instance: str
score_instance: str
plate: Plate
constraint: ConstraintSpec
kind: Literal["call"] = "call"
class IRReturn(IRNode):
names: tuple[str, ...]
kind: Literal["return"] = "return"
class IRProgram(dx.Model):
name: str
inputs: tuple[IRDataInput, ...]
body: tuple[IRNode, ...]
module: IRQiecModule
cards: dict[str, int]
arg_names gives, for each entry of args, its keyword name in PyTorch's arg_constraints ("loc",
"scale", "concentration", ...). NumPyro, Pyro, PyMC, and Edward2 call families by keyword and read
it, and Stan, BUGS, and JAGS call positionally and ignore it. An IRCall is a call of a computation that
the plan did not inline: the dynamic targets call the callee's generated entry point with the program's
Random and Score instances bound to the target's own draw and factor primitives, Stan calls it as a
user-defined function when it can define one, and BUGS and JAGS refuse it.
3. FamilyMeta: facts for translation¶
A family name such as Normal identifies a family. A target may call its location loc or mu, and its
spread a scale or a precision, and the registry records these translations. The shapes and supports of
arguments belong to the typed distribution. The renderer needs both to write the intended density.
The registry is FAMILY_META in quivers.transpile.family_meta:
class FamilyMeta(dx.Model):
qvr_name: str
distribution_class: type[Distribution]
target_names: dict[str, str]
arg_aliases: dict[str, dict[str, str]]
quivers_class: type[ContinuousMorphism] | None
structured_lowering: StructuredSampleLowering | None
event_rank: int
qvr_nameis the family's name in QVR ("Normal","Dirichlet").distribution_classis the PyTorchDistributionsubclass, or a small class that gives the rightarg_constraintsandsupportfor a family PyTorch lacks, such asOrderedLogisticandHalfStudentT. It is the source of the family's argument constraints and support.target_namesmaps each target to the family's name there. Renderers readFAMILY_META[family].target_names[target]and keep no table of their own.arg_aliasesrenames arguments for a target. Renderers that convert a parameterization, such as the mean and precision of a BUGS or JAGS Normal, or PyMC'safor the Dirichlet concentration, key the arithmetic on the alias.quivers_classis theContinuousMorphismsubclass the runtime builds, such asConditionalNormal, and is empty for a family that wraps another morphism.event_rankis the number of event dimensions.
finite_enumerable_at_call_site(family_meta, args) answers whether a marginalize's index object is the
family's support, so that summing over it removes the axis. It is true for Bernoulli, Categorical,
OrderedLogistic, and OrderedProbit, and for Binomial when its total_count is a literal number.
The registry does not hold the shapes and constraints of arguments, which Lower reads from
distribution_class.arg_constraints, or the support, which comes from distribution_class.support,
evaluated on fixed parameters for a support that depends on them, as for Uniform(low, high). A new
family needs one ContinuousMorphism subclass and one FamilyMeta entry, and no renderer changes.
4. Lower: from a module to the IR¶
Program elaboration gives the well-typed computation that lowering reads,
and the transpilation pipeline the comparisons that test this
stage. Lower, in quivers.transpile.plan, implements
dx.Mapping[Module, IRProgram]. Its forward:
- elaborates and checks the module, so that no direct call bypasses checking, refuses a program that has
no computation under the diagnostic kind the elaboration reports, and lowers a module without a program
to its
IRQiecModulewith an empty plan; - expands composite
letbindings, so that each step of the plan corresponds to one step of the expanded source, whose morphism fixes the values a categorical draw ranges over and the form of a structured family's arguments; - walks the computation: a
Random.samplebecomes anIRSamplenamed by its site label, aScore.addof a density at an observation becomes anIRObservewhoseviais the index map (a function from observation rows to groups) by which aSegmentSumregroups the density, a pure binding becomes anIRDeterministic, or is absorbed into anIRScorewhen the next request scores its value, the call of a helper for summing out a latent variable, whose result is scored, becomes anIRMarginalizewhose scope is the helper's body, and the finalReturnbecomes anIRReturn; - reads each family, plate, and argument from its
DistributionValue: a literal, a binding, an indexed binding, a list, a matrix, a broadcast scalar asIRArgBroadcast, a kernel matrix asIRArgKernel, and the output of a parameter map as a reference to theIRDeterministicbindings that compute it, in the order PyTorch's constructor takes them, with the support resolved throughFAMILY_METAand narrowed by the declared bounds of the site's axes; - reads pure values back as
letexpressions: primitives as their operators and builtins, gathers as subscripts, comprehensions as factor expressions, and reductions along the last axis as builtin calls. A sharedLetValuebecomes a deterministic statement of its own, under a fresh name, before the statement that reads it, and a binding inside a comprehension is moved out as a comprehension of its value over the same axis, read at the index, so each shared value is computed once; - derives the inputs from the parameters of the entry point, typed by the module and named by the axes the elaboration recorded, grouped as the targets declare them: the domain, scalars, index maps, observations, parameter maps, and data;
- spells every name for the targets. A draw from a program runs the program in place under names of the
caller: a local
zof a program drawn asthetais the sitetheta$zon the reference machine and the compiled runtime, and since no target admits$in a name, the plan spells ittheta__z.
Lower imports no renderer. Its source elaboration and checking use the same
QIEC rules for every target; its planning phase receives the target to report
unsupported structural representations before source emission.
5. Renderer[T]: from the IR to a schema¶
Each target implements the Renderer protocol, with one public method and four points where targets
differ:
class Renderer(Protocol):
@abstractmethod
def render(self, ir: IRProgram) -> panproto.Schema: ...
@abstractmethod
def declare(self, name, constraint, plate, *, block) -> SchemaFragment: ...
@abstractmethod
def sample(self, name, family, args, arg_names, constraint,
plate, observed) -> SchemaFragment: ...
@abstractmethod
def marginalize(self, node: IRMarginalize) -> SchemaFragment: ...
@abstractmethod
def broadcast(self, value, target_shape) -> SchemaFragment: ...
BlockKind says where a declaration goes ("data", "parameters", "transformed_parameters",
"generated_quantities", "function_body"), and each target reads it in its own structure: Stan has these
blocks, NumPyro's block is the body of the model function, PyMC's is the with pymc.Model() as model:
scope, and BUGS and JAGS have one model { ... }.
RendererBase walks the IR: an IRDataInput is declared, an IRSample that is not observed is declared and
sampled, an IRObserve is declared and sampled with observed=True, an IRDeterministic is declared and
assigned, an IRScore adds to the log density, an IRMarginalize goes to marginalize, and an IRReturn
becomes the target's return. It also provides the substitution of indices that sample and marginalize
share, and marginal_atoms, the values of the latent at which every target's marginalize scores one copy
of the scope.
declare decides by the predicates of §2.3. For Stan:
| Predicate | Event | Batch | Declaration |
|---|---|---|---|
is_real_scalar(c) |
() | () | real <name>; |
is_real_scalar(c) |
() | (B,) | vector[B] <name>; |
is_real_positive(c) |
() | () | real<lower=0> <name>; |
is_real_positive(c) |
() | (B,) | vector<lower=0>[B] <name>; |
is_real_unit_interval(c) |
() | () | real<lower=0, upper=1> <name>; |
is_real_vector(c) |
(E,) | () | vector[E] <name>; |
is_real_vector(c) |
(E,) | (B,) | array[B] vector[E] <name>; |
is_real_simplex(c) |
(E,) | () | simplex[E] <name>; |
is_real_simplex(c) |
(E,) | (B,) | array[B] simplex[E] <name>; |
is_real_cov_matrix(c) |
(E,) | () | cov_matrix[E] <name>; |
is_real_corr_chol(c) |
(E,) | () | cholesky_factor_corr[E] <name>; |
is_real_matrix(c) |
(R,C) | () | matrix[R, C] <name>; |
is_int_bit(c) |
() | () | int<lower=0, upper=1> <name>; |
is_int_bit(c) |
() | (B,) | array[B] int<lower=0, upper=1> <name>; |
is_int_category(c) |
() | () | int<lower=0, upper=K-1> <name>; |
is_int_count(c) |
() | () | int<lower=0> <name>; |
An unrestricted integer carrier, CSInteger, declares an int without a
nonnegative bound. Deterministic integer and Boolean bindings require a separate
placement decision: Stan forbids integer transformed parameters, so these
bindings and their transitive deterministic dependents become model-local
variables. Unrelated continuous bindings remain transformed parameters. Returning
a model-local value replays its needed pure bindings in generated quantities,
without adding a draw or density factor. Top-level data, parameter, and output
sizes must remain data-only; a model-local size dependency receives an explicit
declaration diagnostic. Local array sizes can use earlier model-local values.
The other targets likewise combine support, event axes, and batch axes when choosing declarations. Distribution naming and reparameterization remain separate from this carrier and placement decision.
5.1 Kinds of target¶
Van de Meent and coauthors distinguish first-order languages, whose programs have a fixed finite graph of random variables, from higher-order languages, which evaluate arbitrary programs and record the random choices of each run (van de Meent et al., 2018). The eleven targets fall into three groups by how a model is written:
- Blocks with static types (Stan (Carpenter et al., 2017)):
data { ... } parameters { ... } model { ... } generated quantities { ... }, with declarations placed in the schema vertices of each block. - Model functions that record a trace (NumPyro (Phan et al., 2019), Pyro (Bingham et al., 2019), Turing.jl
(Ge et al., 2018), Gen.jl (Cusumano-Towner et al., 2019), Church (Goodman et al., 2008), WebPPL
(Goodman and Stuhlmüller, 2014)): the renderer writes a model function (
def model(...),@model function,(define (model ...))) and expresses batch axes with the target's own form (numpyro.plate,pyro.plate,filldist,@trace,mapoveriota,repeat). - Declared graphs of random variables (PyMC (Salvatier et al., 2016), Edward2 (Tran et al., 2018), BUGS
(Lunn et al., 2009), JAGS (Plummer, 2003)): PyMC and Edward2 call named distribution constructors with the
batch shape in
dims=(...)orsample_shape=[...], and BUGS and JAGS loop over rows,for (m in 1:N) { name[m] ~ d<family>(args) }. The conversion of a Normal's scale to the precision BUGS takes,tau = 1 / (scale * scale), is the alias inFAMILY_META.arg_aliases["bugs"]with a table of arithmetic in the renderer.
A score step, and the scored weight of a called helper, is a term of the log joint density on every
target: Stan's target +=, NumPyro's and Pyro's factor, PyMC's Potential, Turing's @addlogprob!,
and WebPPL's and Church's factor. Edward2 and Gen have no factor primitive, and on them the weight is a
traced choice of a distribution with one point whose log density is the weight. Gen traces such choices
under the address :qvr_factor, and its marginalize reuses the Turing renderer's, since both write Julia
with Distributions.jl. BUGS and JAGS add a weight with the zeros trick, observing a zero from a Poisson
distribution whose rate carries the weight (Lunn et al., 2012). No other renderer imports another.
6. An example from source to Stan and NumPyro¶
The gallery source docs/examples/source/lda.qvr has documents, topics, and a vocabulary. theta gives
each document its topic weights, phi gives each topic its word weights, z is a topic that is summed out, and
word_idx is the index map from tokens to documents:
composition log_prob [level=algebra]
object Doc : FinSet 20
object Topic : FinSet 3
object Vocab : FinSet 50
object Token : FinSet 200
object Mix : Real 3
program lda(alpha : Real, beta : Real) : Token -> Mix
let theta : Doc <- sample Dirichlet(concentration=alpha) [over=Topic, iid_over=Doc]
let phi : Topic <- sample Dirichlet(concentration=beta) [over=Vocab, iid_over=Topic]
marginalize z : Topic <- Categorical(probs=theta) [over=Doc, reduction=logsumexp]
observe w : Token <- Categorical(probs=phi[z]) [via=word_idx]
return theta
export lda
The grouping over=Doc states one topic per document, shared by its tokens, a mixture of unigrams
(Nigam et al., 2000). Since the prior theta is a plate over Doc, the evaluators instead give each token its
own topic drawn from its document's weights (Programs as kernels §2.7),
which is the topic for each token in latent Dirichlet allocation (LDA) (Blei et al., 2003). The output below shows it.
After Lower, the IR has:
- inputs
alphaandbeta, real scalars, andword_idxandw, integer arrays overToken; IRSample(theta)with a simplex support and the platePlate(event_dims=(Topic,), batch_dims=(Doc,)), whose one argument isIRArgBroadcast(value=IRArgRef("alpha"), target_shape=(3,)), andIRSample(phi)likewise overVocabandTopic;IRMarginalize(z)with argumentIRArgRef("theta")and a scope with oneIRObserve(w), whose argument isIRArgRef("phi", indices=(IRArgRef("z"),))and whoseviais"word_idx";IRReturn(names=("theta",)).
StanRenderer writes:
data {
real alpha; real beta; array [200] int <lower = 0 , upper = 199> word_idx; array [200] int <lower = 0 , upper = 49> w;
} parameters {
array [20] simplex [3] theta; array [3] simplex [50] phi;
} model {
for (m_Doc in 1 : 20) {
target +=dirichlet_lpdf(theta[m_Doc] | rep_vector(alpha,3));
} for (m_Topic in 1 : 3) {
target +=dirichlet_lpdf(phi[m_Topic] | rep_vector(beta,50));
} {
array [200] vector [3] lps_z = rep_array(rep_vector(0,3),200); for (n_Token in 1 : 200) {
for (k in 1 : 3) {
lps_z[n_Token,k] = categorical_lpmf(k | theta[word_idx[n_Token]+1]);
}
} for (n_Token in 1 : 200) {
for (k in 1 : 3) {
lps_z[n_Token,k] += categorical_lpmf(w[n_Token]+1 | phi[k]);
}
} for (n_Token in 1 : 200) {
target +=log_sum_exp(lps_z[n_Token]);
}
}
}generated quantities{
array [20] simplex [3] theta_value = theta;
}
and NumPyroRenderer writes:
import jax.numpy as jnp
import numpyro
import numpyro.distributions
import jax.scipy.special as jsp
def model(alpha,beta,word_idx,w=None) :
with numpyro.plate("Doc" ,20) :
theta = numpyro.sample("theta" ,numpyro.distributions.Dirichlet(concentration=jnp.full((3,),alpha)))
with numpyro.plate("Topic" ,3) :
phi = numpyro.sample("phi" ,numpyro.distributions.Dirichlet(concentration=jnp.full((50,),beta)))
__marg_z_0 = numpyro.distributions.Categorical(probs=phi[0]).log_prob(w)
__marg_z_1 = numpyro.distributions.Categorical(probs=phi[1]).log_prob(w)
__marg_z_2 = numpyro.distributions.Categorical(probs=phi[2]).log_prob(w)
__marg_z_w = jnp.log(theta[word_idx])
__marg_z = jnp.stack([__marg_z_0,__marg_z_1,__marg_z_2],axis=- 1)
numpyro.factor("z" ,jnp.sum(jsp.logsumexp(__marg_z_w+__marg_z,axis=- 1)))
return theta
Both sum the topic out for each token: Stan by a table lps_z and log_sum_exp, NumPyro by the stacked log
densities at each topic, logsumexp, and numpyro.factor, so no sample site remains for z. Both keep the
zero-based codes of the data: Stan declares w with lower = 0 and adds one where it indexes. Neither
renderer names Dirichlet or Categorical in its code: each declares through is_real_simplex and
is_int_category. Stan bounds word_idx by Token (upper = 199), not by Doc (upper = 19).
7. Adding a family¶
- Write a
ContinuousMorphismsubclass inquivers.continuous.families, or in a new module ofquivers.continuouswhen the family has a structure of its own, as the ordered families with cutpoints do. - Add an entry to
FAMILY_METAwithqvr_name,distribution_class(the PyTorch class, or a small class with the rightarg_constraintsandsupport),quivers_class,target_names, andarg_aliases.
Every renderer then handles the family through the predicates on its support. A target needs its own change only when the family needs a conversion of its parameters or a wrapper there.
8. Adding a target¶
- Choose the target's tree-sitter grammar (
stan,python,julia,scheme,javascript,bugs,jags). - Write a
RendererBasesubclass inquivers.transpile.renderers.<target>that overridesdeclare,sample,marginalize, andbroadcast. - Add
target_names[<target>]to eachFamilyMetaentry whose family the target supports. A family without one is refused at its use withUnsupportedConstructand a precise kind. - Register the renderer, with its grammar, in the table of renderers of
quivers.transpile.
The walk of the IR, the use of FAMILY_META, and the dispatch on predicates are inherited from
RendererBase, and no renderer imports another, except Gen's use of Turing's marginalize.
9. Rules the architecture keeps¶
- One place for each fact. The facts about a family (number of event dimensions, support, argument constraints, name and argument names for each target) are stored once, and renderers read them there.
- No test of a family's name in a renderer. A renderer decides by the predicates of §2.3 and by
FAMILY_META.target_names. - No field of the syntax tree is dropped. Each draw's
overandiid_over, each observation'svia, and eachmarginalize's reduction and scope is used byLoweror refused by a renderer with a preciseUnsupportedConstructkind. - The same interface for every target. Every renderer reads the same
FAMILY_META, the same output ofLower, and the same helpers ofRendererBase. - A refusal, not a guess. A renderer that cannot write a construct raises
UnsupportedConstructwith a precise kind and does not emit placeholder or approximate code.
A renderer that breaks one of these either produces a schema that the structural tests reject, fails the
Renderer protocol, or raises UnsupportedConstruct.
Cited sources¶
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).
Jan-Willem van de Meent, Brooks Paige, Hongseok Yang, and Frank Wood. An Introduction to Probabilistic Programming. 2018. arXiv:1809.10756, doi:10.48550/arXiv.1809.10756.
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.
Du Phan, Neeraj Pradhan, and Martin Jankowiak. Composable Effects for Flexible and Accelerated Probabilistic Programming in NumPyro. arXiv preprint arXiv:1912.11554, 2019. Presented at the NeurIPS 2019 workshop on Program Transformations for Machine Learning. URL: https://arxiv.org/abs/1912.11554, doi:10.48550/arXiv.1912.11554.
Eli Bingham, Jonathan P. Chen, Martin Jankowiak, Fritz Obermeyer, Neeraj Pradhan, Theofanis Karaletsos, Rohit Singh, Paul Szerlip, Paul Horsfall, and Noah D. Goodman. Pyro: Deep Universal Probabilistic Programming. Journal of Machine Learning Research, 20(28):1–6, 2019. URL: https://www.jmlr.org/papers/v20/18-403.html.
Hong Ge, Kai Xu, and Zoubin Ghahramani. Turing: A Language for Flexible Probabilistic Inference. In Proceedings of the Twenty-First International Conference on Artificial Intelligence and Statistics, volume 84 of Proceedings of Machine Learning Research, 1682–1690. PMLR, 2018. URL: https://proceedings.mlr.press/v84/ge18b.html.
Marco F. Cusumano-Towner, Feras A. Saad, Alexander K. Lew, and Vikash K. Mansinghka. Gen: A General-Purpose Probabilistic Programming System with Programmable Inference. In Proceedings of the 40th ACM SIGPLAN Conference on Programming Language Design and Implementation, 221–236. ACM, 2019. URL: https://pldi19.sigplan.org/details/pldi-2019-papers/45/Gen-A-General-Purpose-Probabilistic-Programming-System-with-Programmable-Inference, doi:10.1145/3314221.3314642.
Noah D. Goodman, Vikash K. Mansinghka, Daniel Roy, Keith Bonawitz, and Joshua B. Tenenbaum. Church: A Language for Generative Models. In Proceedings of the 24th Conference on Uncertainty in Artificial Intelligence, volume R6 of Proceedings of Machine Learning Research, 220–229. PMLR, 2008. Reissued by PMLR on 9 October 2024. URL: https://proceedings.mlr.press/r6/goodman08a.html.
Noah D. Goodman and Andreas Stuhlmüller. The Design and Implementation of Probabilistic Programming Languages. Author-published electronic textbook, 2014. URL: https://dippl.org.
John Salvatier, Thomas V. Wiecki, and Christopher Fonnesbeck. Probabilistic Programming in Python Using PyMC3. PeerJ Computer Science, 2:e55, 2016. URL: https://peerj.com/articles/cs-55/, doi:10.7717/peerj-cs.55.
Dustin Tran, Matthew D. Hoffman, Dave Moore, Christopher Suter, Srinivas Vasudevan, Alexey Radul, Matthew Johnson, and Rif A. Saurous. Simple, Distributed, and Accelerated Probabilistic Programming. In Advances in Neural Information Processing Systems, volume 31. 2018. URL: https://proceedings.neurips.cc/paper/2018/file/201e5bacd665709851b77148e225b332-Paper.pdf.
David Lunn, David Spiegelhalter, Andrew Thomas, and Nicky Best. The BUGS Project: Evolution, Critique and Future Directions. Statistics in Medicine, 28(25):3049–3067, 2009. URL: https://onlinelibrary.wiley.com/doi/abs/10.1002/sim.3680, doi:10.1002/sim.3680.
Martyn Plummer. JAGS: A Program for Analysis of Bayesian Graphical Models Using Gibbs Sampling. In Kurt Hornik, Friedrich Leisch, and Achim Zeileis, editors, Proceedings of the 3rd International Workshop on Distributed Statistical Computing. 2003. URL: https://www.r-project.org/conferences/DSC-2003/Proceedings/Plummer.pdf.
David Lunn, Chris Jackson, Nicky Best, Andrew Thomas, and David Spiegelhalter. The BUGS Book: A Practical Introduction to Bayesian Analysis. Chapman and Hall/CRC, 2012. doi:10.1201/b13613.
Kamal Nigam, Andrew Kachites McCallum, Sebastian Thrun, and Tom Mitchell. Text Classification from Labeled and Unlabeled Documents using EM. Machine Learning, 39(2–3):103–134, 2000. doi:10.1023/A:1007692713085.
David M. Blei, Andrew Y. Ng, and Michael I. Jordan. Latent Dirichlet Allocation. Journal of Machine Learning Research, 3:993–1022, 2003. URL: https://www.jmlr.org/papers/v3/blei03a.html.