Carriers¶
Each of these carriers computes a statistic of all derivations in the same sum and
product traversal that computes their weight. For fixed carrier width, the extra
work per operation is bounded in the cost of the component arithmetic. The proofs
below concern the algebraic operations, and the law grid tests their implementations
at the stated numerical precision.
Each is an OrderPreservingSemiring of the one
algebra hierarchy whose product commutes, with its elements packed on one trailing axis, so it
composes, sums, and reduces over empty axes like any other member, and its law claims carry
evidence: the laws about the carrier by the arguments below, the laws about the implementation by
the law grid.
Expectation¶
An element of ExpectationSemiring is \(\langle p, r
\rangle\) with \(p\) in a commutative semiring \(K\) and \(r\) in a \(K\)-semimodule \(M\)
(Semimodule), with
zero \(\langle 0, 0 \rangle\) and unit \(\langle 1, 0 \rangle\) (Eisner 2002; Li and Eisner 2009, section 3).
Theorem. If \(K\) is a commutative semiring and \(M\) a semimodule over it, these operations form a commutative semiring.
Proof. The sum is componentwise, so it is associative and commutative with unit \(\langle 0, 0 \rangle\). The product is commutative because \(p_1 p_2 = p_2 p_1\) and \(\oplus\) commutes. It is associative: both bracketings of three factors give \(\langle p_1 p_2 p_3,\ p_1 p_2 r_3 \oplus p_1 p_3 r_2 \oplus p_2 p_3 r_1 \rangle\) by the action's associativity and distributivity. \(\langle 1, 0 \rangle\) is a unit since \(1 \cdot r = r\) and \(p \cdot 0 = 0\). Distributivity: \(\langle p, r \rangle (\langle a, b \rangle \oplus \langle c, d \rangle) = \langle p(a \oplus c),\ p(b \oplus d) \oplus (a \oplus c) r \rangle\), which equals the sum of the two products by distributivity of \(K\) and of the action over both sums. The zero annihilates since \(0 \cdot r = 0\) and \(p \cdot 0 = 0\). QED
Over derivations \(d\) whose factors \(e\) carry \(\langle p_e, r_e \rangle\), the sum of products is \(\langle Z,\ \bigoplus_d \bigoplus_{e \in d} p(d \setminus e)\, r_e \rangle\), where \(p(d \setminus e)\) multiplies the weights of the other factors of \(d\). With \(p_e = \exp(\theta \cdot f_e)\) and \(r_e = p_e f_e\) the statistic is \(\sum_d p(d) \sum_{e \in d} f_e = \nabla_\theta Z\), the gradient that reverse mode computes through the sum. Both are checked against enumeration of every path and against autograd.
Star. Where \(p^* = 1 \oplus p p^*\) exists, \(\langle p, r \rangle^* = \langle p^*,\ p^* (p^* r) \rangle\): the statistic \(q\) of a solution of \(X = 1 \oplus \langle p, r \rangle X\) must satisfy \(q = p q \oplus p^* r\), and \(p (p^* p^* r) \oplus p^* r = p^* (p p^* \oplus 1) r = p^* p^* r\).
Second-order expectation¶
An element of SecondOrderExpectationSemiring
is \(\langle p, r, s, t \rangle\) with \(r \in K^m\), \(s \in K^k\) and \(t \in K^{m \times k}\):
(Li and Eisner 2009, section 4). With each factor \(e\) carrying \(\langle p_e, p_e r_e, p_e s_e, p_e r_e s_e^{\mathsf T} \rangle\), the sum over derivations has last part \(\bigoplus_d p(d)\, r(d)\, s(d)^{\mathsf T}\), where \(r(d)\) and \(s(d)\) sum the statistics of the factors of \(d\); this is checked against enumeration of every path.
Theorem. Over a commutative semiring \(K\) these operations form a commutative semiring.
Proof. Write an element as \(p + \sum_i r_i \varepsilon_i + \sum_j s_j \eta_j + \sum_{ij} t_{ij} \varepsilon_i \eta_j\), a polynomial over \(K\) in commuting indeterminates whose monomials are among \(1\), \(\varepsilon_i\), \(\eta_j\) and \(\varepsilon_i \eta_j\). The product above is the polynomial product followed by dropping every other monomial: the \(\varepsilon_i \eta_j\) coefficient of the product collects \(p_1 t_2\), \(p_2 t_1\), \(r_{1i} s_{2j}\) and \(r_{2i} s_{1j}\). The dropped monomials are closed under multiplication by any monomial, so dropping them from a product gives the same result whether or not they were dropped from the factors first. Dropping is therefore a homomorphism of sums and products from the commutative polynomial semiring onto these elements, and the elements inherit associativity, commutativity, distributivity and the units. QED
Star. Where \(p^*\) exists, \(\langle p, r, s, t \rangle^* = \langle p^*, p^{*2} r, p^{*2} s, p^{*2} t \oplus 2 p^{*3} r s^{\mathsf T} \rangle\), with \(2 = 1 \oplus 1\): substituting into \(X = 1 \oplus x X\) and using only \(p^* = 1 \oplus p p^*\) and commutativity gives each part.
Products¶
ProductSemiring runs several commutative semirings side by
side, operation by operation. A direct product of commutative semirings is a commutative semiring,
since every law holds in each factor. The star of a product is the product of the stars, where each
exists.
Signed logarithmic weight derivatives¶
SignedLogDualSemiring represents a real coefficient as a sign and the
natural logarithm of its magnitude. The zero representation is
\((0,-\infty)\). The primal and tangent have independent signs and magnitudes.
A zero primal thus need not imply a zero tangent.
Carrier laws. Decode the two parts as \(p\) and \(d\) and interpret the pair as \(p+d\varepsilon\) in \(\mathbb{R}[\varepsilon]/(\varepsilon^2)\). Componentwise addition and the product \((p,d)(q,e)=(pq,pe+qd)\) are exactly the quotient's operations. Thus they inherit the commutative semiring laws in exact arithmetic. Interpreting one input as \((p,p')\) and applying these operations gives one directional derivative of each finite sum of products by the ordinary chain rule. A product may be pruned as algebraic zero only when both parts are zero.
For opposing magnitudes with logs \(h>l\), subtraction computes
\(h+\log(-\operatorname{expm1}(l-h))\). A reduction first cancels equal
represented magnitudes, so the three terms \(e^{1000}\), \(1\), and \(-e^{1000}\)
can retain their unit remainder. The numerical implementation uses float64.
A nonzero summand that rounds away raises FloatingPointError, as does a
nonzero coefficient that cannot be decoded into linear float64. These
checks do not certify a general error bound for floating-point reductions.
Pass the primal and tangent explicitly, then use an acyclic indexed or
elimination plan's run method. The derivative is the tangent of the
computed element. For instance, at \(x=0\) the factors \(x\), \(e^{1000}\),
and \(e^{-1000}\) give primal zero and tangent one in the direction
\(dx=1\). The individual large and small factors can stay in the log
representation throughout that contraction. Direct autograd of the packed
signs and log magnitudes is refused.
References¶
- Eisner, J. (2002). Parameter estimation for probabilistic finite-state transducers. ACL. doi:10.3115/1073083.1073085
- Li, Z., and Eisner, J. (2009). First- and second-order expectation semirings with applications to minimum-risk training on translation forests. EMNLP. doi:10.3115/1699510.1699517
carriers
¶
Carriers of statistics: expectation, second-order expectation, products.
An expectation carrier pairs a weight p with a statistic r acted
on by weights [Eisner (2002); Li and Eisner (2009)]:
<p1, r1> ⊗ <p2, r2> = <p1 p2, p1 r2 ⊕ p2 r1>,
<p1, r1> ⊕ <p2, r2> = <p1 ⊕ p2, r1 ⊕ r2>.
Summing products over derivations gives <Z, ⊕_d p(d) r(d)> where
r(d) sums the statistics of a derivation's factors; with factor
weights exp(θ · f) and statistics p f the second component is the
gradient of Z in θ. The second-order carrier adds a second
statistic and their products; the product carrier runs several
semirings side by side. Each is an OrderPreservingSemiring of the one
hierarchy whose product commutes, with packed elements on one trailing
axis, and each gives its star from the star of its weights where that
exists.
References
Eisner, J. (2002). Parameter estimation for probabilistic finite-state transducers. ACL, 1-8. doi:10.3115/1073083.1073085
Li, Z., and Eisner, J. (2009). First- and second-order expectation semirings with applications to minimum-risk training on translation forests. EMNLP, 40-51. doi:10.3115/1699510.1699517
ElementStar
¶
The star p* = 1 ⊕ p ⊗ p* of the weights, elementwise.
ExpectationSemiring
¶
ExpectationSemiring(module: Semimodule)
Bases: _Packed
Weights paired with statistics in a semimodule (Li and Eisner 2009).
An element is <p, r>, packed as [p, r_1, ..., r_d], with p
in the weights' semiring and r in a semimodule over it of vectors
of length d. The product is <p1 p2, p1 · r2 ⊕ p2 · r1>, the
sum is componentwise, the zero is <0, 0> and the unit <1, 0>.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The statistics, vectors over a commutative semiring with scalar elements.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the module's scalars are not a commutative |
Source code in src/quivers/core/carriers.py
base
property
¶
base: Semiring
The semiring of the weights.
| RETURNS | DESCRIPTION |
|---|---|
Semiring
|
The weights. |
module
property
¶
module: Semimodule
The semimodule of the statistics.
| RETURNS | DESCRIPTION |
|---|---|
Semimodule
|
The statistics. |
evidence
¶
evidence(law: Law) -> LawEvidence
The evidence for a law; laws about the carrier hold by a theorem.
| PARAMETER | DESCRIPTION |
|---|---|
law
|
A law the structure claims.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
LawEvidence
|
The argument when the laws of the weights and the statistics are established. |
Source code in src/quivers/core/carriers.py
parts
¶
The weight and the statistic of each element.
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Packed elements.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of torch.Tensor and torch.Tensor
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/core/carriers.py
pack
¶
Elements from weights and statistics.
| PARAMETER | DESCRIPTION |
|---|---|
p
|
Weights.
TYPE:
|
r
|
Statistics, with a trailing axis of
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
|
Source code in src/quivers/core/carriers.py
tensor_op
¶
<p1 p2, p1 · r2 ⊕ p2 · r1>.
| PARAMETER | DESCRIPTION |
|---|---|
a
|
Packed elements.
TYPE:
|
b
|
Packed elements, broadcastable with
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The product. |
Source code in src/quivers/core/carriers.py
zero_like
¶
<0, 0> at every index.
| PARAMETER | DESCRIPTION |
|---|---|
shape
|
The index shape.
TYPE:
|
like
|
The tensor whose dtype and device are used.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Shape |
Source code in src/quivers/core/carriers.py
unit_like
¶
<1, 0> at every index.
| PARAMETER | DESCRIPTION |
|---|---|
shape
|
The index shape.
TYPE:
|
like
|
The tensor whose dtype and device are used.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Shape |
Source code in src/quivers/core/carriers.py
contains
¶
Whether the weight and the statistic are in their carriers.
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Packed elements.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
One truth value per element. |
Source code in src/quivers/core/carriers.py
star
¶
star(t: Tensor, weights_star: ElementStar) -> Tensor
<p, r>* = <p*, p* · (p* · r)>, where the star of p exists.
It solves X = 1 ⊕ <p, r> ⊗ X: the weight is p* and the
statistic q must satisfy q = p · q ⊕ p* · r, which
p* · (p* · r) does since p* = 1 ⊕ p p*.
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Packed elements.
TYPE:
|
weights_star
|
The star of the weights, elementwise.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The packed star. |
Source code in src/quivers/core/carriers.py
SecondOrderExpectationSemiring
¶
SecondOrderExpectationSemiring(base: Semiring, first: int, second: int)
Bases: _Packed
Weights with two statistics and their products (Li and Eisner 2009).
An element is <p, r, s, t> with r of length m, s of
length k and t an m x k matrix, packed in that order with
t row by row. The product is
<p1 p2, p1 r2 ⊕ p2 r1, p1 s2 ⊕ p2 s1,
p1 t2 ⊕ p2 t1 ⊕ r1 s2ᵀ ⊕ r2 s1ᵀ>,
so summing over derivations gives ⊕_d p(d) r(d) s(d)ᵀ in the last
part, the expectation of the product of two additive statistics.
| PARAMETER | DESCRIPTION |
|---|---|
base
|
A commutative semiring with scalar elements.
TYPE:
|
first
|
The length
TYPE:
|
second
|
The length
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
ValueError
|
If a length is negative. |
Source code in src/quivers/core/carriers.py
evidence
¶
evidence(law: Law) -> LawEvidence
The evidence for a law; laws about the carrier hold by a theorem.
| PARAMETER | DESCRIPTION |
|---|---|
law
|
A law the structure claims.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
LawEvidence
|
The argument when the laws of the base are established. |
Source code in src/quivers/core/carriers.py
parts
¶
The four parts of each element.
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Packed elements.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of torch.Tensor
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/core/carriers.py
pack
¶
Elements from their four parts.
| PARAMETER | DESCRIPTION |
|---|---|
p
|
Weights.
TYPE:
|
r
|
First statistics, trailing
TYPE:
|
s
|
Second statistics, trailing
TYPE:
|
t
|
Products, trailing
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The packed elements. |
Source code in src/quivers/core/carriers.py
tensor_op
¶
The product of two packed elements.
| PARAMETER | DESCRIPTION |
|---|---|
a
|
Packed elements.
TYPE:
|
b
|
Packed elements, broadcastable with
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
|
Source code in src/quivers/core/carriers.py
zero_like
¶
<0, 0, 0, 0> at every index.
| PARAMETER | DESCRIPTION |
|---|---|
shape
|
The index shape.
TYPE:
|
like
|
The tensor whose dtype and device are used.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The zero. |
Source code in src/quivers/core/carriers.py
unit_like
¶
<1, 0, 0, 0> at every index.
| PARAMETER | DESCRIPTION |
|---|---|
shape
|
The index shape.
TYPE:
|
like
|
The tensor whose dtype and device are used.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The unit. |
Source code in src/quivers/core/carriers.py
contains
¶
star
¶
star(t: Tensor, weights_star: ElementStar) -> Tensor
<p*, p*² r, p*² s, p*² t ⊕ 2 p*³ r sᵀ>, where p* exists.
It solves X = 1 ⊕ <p, r, s, t> ⊗ X using only
p* = 1 ⊕ p p* and commutativity; the factor 2 is 1 ⊕ 1.
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Packed elements.
TYPE:
|
weights_star
|
The star of the weights, elementwise.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The packed star. |
Source code in src/quivers/core/carriers.py
ProductSemiring
¶
ProductSemiring(factors: Sequence[OrderPreservingSemiring])
Bases: _Packed
Several commutative semirings side by side.
An element is a tuple of elements, one per factor, packed by concatenation; every operation acts factor by factor. Factors have elements of rank at most one and share a tensor dtype.
| PARAMETER | DESCRIPTION |
|---|---|
factors
|
The factors; each must claim a commutative product.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If there are fewer than two factors or one does not commute. |
ValueError
|
If a factor's elements have rank above one. |
Source code in src/quivers/core/carriers.py
factors
property
¶
factors: tuple[OrderPreservingSemiring, ...]
The factors.
| RETURNS | DESCRIPTION |
|---|---|
tuple of OrderPreservingSemiring
|
The factors, in order. |
evidence
¶
evidence(law: Law) -> LawEvidence
The evidence for a law; laws about the carrier hold by a theorem.
| PARAMETER | DESCRIPTION |
|---|---|
law
|
A law the structure claims.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
LawEvidence
|
The argument when the laws of every factor are established. |
Source code in src/quivers/core/carriers.py
parts
¶
The element of each factor.
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Packed elements.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of torch.Tensor
|
One tensor per factor, with its element axes. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/core/carriers.py
pack
¶
Elements from one element per factor.
| PARAMETER | DESCRIPTION |
|---|---|
parts
|
One tensor per factor, with its element axes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The packed elements. |
Source code in src/quivers/core/carriers.py
tensor_op
¶
The product, factor by factor.
| PARAMETER | DESCRIPTION |
|---|---|
a
|
Packed elements.
TYPE:
|
b
|
Packed elements, broadcastable with
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The packed product. |
Source code in src/quivers/core/carriers.py
zero_like
¶
The zero of every factor.
| PARAMETER | DESCRIPTION |
|---|---|
shape
|
The index shape.
TYPE:
|
like
|
The tensor whose dtype and device are used.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The packed zero. |
Source code in src/quivers/core/carriers.py
unit_like
¶
The unit of every factor.
| PARAMETER | DESCRIPTION |
|---|---|
shape
|
The index shape.
TYPE:
|
like
|
The tensor whose dtype and device are used.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The packed unit. |
Source code in src/quivers/core/carriers.py
contains
¶
Whether every factor's element is in its carrier.
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Packed elements.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
One truth value per element. |
Source code in src/quivers/core/carriers.py
star
¶
star(t: Tensor, stars: Sequence[ElementStar]) -> Tensor
The star of every factor.
| PARAMETER | DESCRIPTION |
|---|---|
t
|
Packed elements.
TYPE:
|
stars
|
The star of each factor.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The packed star, which exists where every factor's does. |
Source code in src/quivers/core/carriers.py
signed_log
¶
A packed signed logarithmic carrier for first-order weight derivatives.
SignedLogDualSemiring
¶
Bases: _Packed
A real dual algebra with independently signed log primal and tangent.
Each element stores (sign, log_abs, tangent_sign, tangent_log_abs).
Zero in either component is (0, -inf). A zero primal can have a
nonzero tangent. The operations carry one supplied direction without
differentiating the sign or logarithm of the primal representation.
The carrier uses float64 arithmetic. Equal represented magnitudes cancel in a multiway sum before other additions. Detectable disappearance of a nonzero summand raises a numerical error. Algebraic laws concern the decoded ideal dual algebra.
evidence
¶
evidence(law: Law) -> LawEvidence
Give the ideal carrier evidence for the requested law.
Source code in src/quivers/core/signed_log.py
pack
¶
Combine two broadcastable canonical signed log pairs.
Source code in src/quivers/core/signed_log.py
parts
¶
Return the primal and tangent pairs without changing their encoding.
Source code in src/quivers/core/signed_log.py
from_linear
¶
Embed two finite float64 components as a dual number.
to_linear
¶
Decode both components, refusing an unrepresentable nonzero value.
contains
¶
Return a Boolean tensor for the canonical primal and tangent pairs.
tensor_op
¶
Multiply the primal coefficients and apply the product rule.
Source code in src/quivers/core/signed_log.py
positive_quotient
¶
Divide dual numbers by a dual with positive primal coefficient.
| PARAMETER | DESCRIPTION |
|---|---|
numerator
|
Two canonical encoded dual numbers, with broadcastable shapes.
TYPE:
|
denominator
|
Two canonical encoded dual numbers, with broadcastable shapes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The quotient and its directional coefficient in signed log. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a denominator primal is not positive. |
FloatingPointError
|
If the encoded arithmetic exceeds float64 precision. |
Source code in src/quivers/core/signed_log.py
zero_like
¶
Broadcast the canonical primal and tangent zero to the index shape.
Source code in src/quivers/core/signed_log.py
unit_like
¶
Broadcast the canonical primal one and tangent zero.