Primitives¶
The pure primitives are the
core's basic operations on values, held in a closed registry of names, types, and implementations.
A new operation needs a signature the checker accepts
and an implementation on each runtime that runs it.
PrimitiveSignature.total additionally certifies that every well-typed argument
satisfies the operation's domain. This permits eager selection between proved
total branches; a partial operation in either branch requires lazy evaluation.
Checked probability operations¶
Four scalar Real -> Real operations carry the probability capability.
log_probability(p) accepts finite 0 <= p <= 1, giving -inf at
zero. log1mexp(x) accepts x <= 0, including -inf, and gives
-inf at zero. log_ndtr(x) gives a stable Normal log CDF and accepts
both infinities. positive_parameter(x) is the identity on finite
strictly positive reals. All reject Booleans and NaNs; the other endpoint
restrictions are checked at execution. Finite derivatives at singular
logarithmic endpoints are not promised.
Two checked observation operations retain their input roles.
observation_status : Int -> Int accepts the codes -1, 0, 1, and 2.
interval_upper : (Real, Real) -> Real returns the second endpoint when it is
finite and strictly greater than the first, which may be infinite. Both reject
Boolean carriers. Lifted endpoints broadcast to their common array shape; a
scalar upper endpoint thus contributes once per queried row, with its gradient
summed over those rows.
The native and reference implementations preserve these domains. Dynamic external runtimes provide checked helpers; ordinary Stan calls use checked functions, while BUGS/JAGS refuse them. Retained canonical computations require a target with the probability capability. A partial operation in an inactive conditional branch must stay inactive; unsupported symbolic control flow raises an explicit error. Registration does not establish that every target can differentiate or fit such a retained computation.
primitives
¶
The closed registry of pure primitives QIEC values may apply.
A primitive is nominal: it has a stable identity derived from its name, a
fixed monomorphic signature, and a capability tag a backend either
supports or refuses. Overloading is a surface concern: a source + on
two integers lowers to add_int and on two reals to add_real, so
the kernel never has to choose an implementation by inspecting a runtime
value.
The implementations here are what the reference machine runs. They take
the run's NumberDomain and their arguments as the machine holds them,
numbers as tensors, and keep every gradient: a primitive applied to a
tensor of positions is applied entrywise by torch's own broadcasting. A
partial primitive refuses an argument outside its domain rather than
answering NaN: where its result is NaN and no argument it reads is (the
logarithm of a negative number, say), and where it divides by zero. The
probability primitives the compiled runtime shares are exported by name.
The host scalar helpers below them compute the same functions on Python
numbers for the reference distribution backend.
STRING_HASH_OFFSET
module-attribute
¶
The FNV-1a offset basis of string_hash.
PRIMITIVES
module-attribute
¶
PRIMITIVES: Mapping[str, PrimitiveSignature] = MappingProxyType({signature.name: signature for signature, _ in _ENTRIES})
Every primitive by name.
The mapping is closed: a name absent here is not a primitive, and no backend may resolve one dynamically.
IMPLEMENTATIONS
module-attribute
¶
IMPLEMENTATIONS: Mapping[str, Implementation] = MappingProxyType({signature.name: implementation for signature, implementation in _ENTRIES})
The reference machine's tensor implementation of every primitive, by name.
PrimitiveCapability
¶
PrimitiveCapability = Literal['arithmetic', 'comparison', 'strict-scalar-comparison', 'strict-scalar-division', 'boolean', 'string', 'conversion', 'math', 'special', 'activation', 'weight', 'probability', 'rule-log-weight', 'bounded-index']
The feature a target must support to render a primitive.
Implementation
¶
The tensor implementation of one primitive.
It takes the run's NumberDomain and the primitive's arguments, numbers as
tensors and strings as strings, and returns the result in the same form.
PrimitiveSignature
didactic-model
¶
PrimitiveSignature(*, name: str, parameters: tuple[TypeExpr, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), result: TypeExpr = field(opaque=True, extras=OPAQUE_BY_VALUE), capability: PrimitiveCapability, total: bool = False)
Bases: QiecModel
One primitive's name, types, and the capability a backend needs for it.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The stable nominal name, such as
TYPE:
|
parameters
|
The argument types, in order.
TYPE:
|
result
|
The result type.
TYPE:
|
capability
|
The feature a target must support to render the primitive.
TYPE:
|
total
|
Whether all well-typed arguments satisfy its domain. This proof permits eager branch selection; unproved primitives default to false.
TYPE:
|
id
property
¶
id: PrimitiveId
The primitive's stable identity.
| RETURNS | DESCRIPTION |
|---|---|
PrimitiveId
|
An identity derived from the |
string_hash_value
¶
The 32-bit FNV-1a hash of a string's UTF-8 bytes, as a nonnegative int.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The string.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The hash with its top bit cleared, so it lies in |
Source code in src/quivers/qiec/primitives.py
observation_weight
¶
Validate finite, nonnegative observation weights.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
A floating tensor of coefficients multiplying log densities.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The supplied value, unchanged and retaining its gradient. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a weight is nonfinite or negative, or the tensor is not real. |
Source code in src/quivers/qiec/primitives.py
observation_status
¶
Validate recorded-outcome event codes.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Integer codes from minus one through two.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The codes, unchanged. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the tensor is not of integers or a code is outside the set. |
Source code in src/quivers/qiec/primitives.py
interval_upper
¶
Validate an active interval's finite, strictly larger upper endpoint.
| PARAMETER | DESCRIPTION |
|---|---|
lower
|
Real endpoints; the lower endpoint may be infinite.
TYPE:
|
upper
|
Real endpoints; the lower endpoint may be infinite.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The upper endpoint broadcast to both shapes, with its gradient. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If an endpoint is not real, the lower is NaN, or the upper is not finite or does not exceed the lower. |
Source code in src/quivers/qiec/primitives.py
positive_parameter
¶
Validate a positive real parameter without changing it.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Natural-scale parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The value, unchanged and retaining its gradient. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a value is not real, not finite, zero, or negative. |
Source code in src/quivers/qiec/primitives.py
log_probability
¶
Log probabilities, negative infinity at zero.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Finite real probabilities in the closed unit interval.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Their logarithms. The zero endpoint is masked before the logarithm, so its inactive derivative does not leak. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a value is not real, not finite, or outside [0, 1]. |
Source code in src/quivers/qiec/primitives.py
log1mexp
¶
log(1 - exp(value)) without cancellation.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Nonpositive real values, including negative infinity.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Negative infinity at zero and zero at negative infinity; the two branches are evaluated at protected arguments only. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a value is not real, NaN, or positive. |
Source code in src/quivers/qiec/primitives.py
log_ndtr
¶
The standard normal log distribution function.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
Real values, including either infinity.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Negative infinity at negative infinity, zero at infinity, with the infinite endpoints masked before the function is applied. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a value is not real or is NaN. |
Source code in src/quivers/qiec/primitives.py
scale_weight
¶
Multiply log weights, a zero coefficient absorbing any density.
| PARAMETER | DESCRIPTION |
|---|---|
coefficient
|
The coefficient, a scalar or one per density position.
TYPE:
|
density
|
The log density at each position.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The product, with zero-coefficient positions masked before the multiplication, so an infinite density there gives zero and no NaN gradient. |
Source code in src/quivers/qiec/primitives.py
primitive
¶
primitive(name: str) -> PrimitiveSignature
Look up a primitive by name.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The nominal name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PrimitiveSignature
|
The primitive's signature. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If no primitive has the name. Callers turn this into a source-located diagnostic; the registry itself has no location to blame. |