Contractions¶
A contraction \(x_1, \dots, x_k \to y\) names each axis of each input by a letter. With \(\mathcal L\) the letters, \(Y\) the output letters and \(\mathcal L_c = \mathcal L \setminus Y\) the summed ones, its value is
plan_contraction fixes how it is evaluated from the
letters, their extents and the algebra's law claims, and records the result as a
ContractionPlan;
run_contraction evaluates a plan and counts its work.
EinsumWiring, binary composition
(CompositionRule.compose) and
day_convolution all run through them.
| algebra | algorithm | cost | status |
|---|---|---|---|
establishes every law of ELIMINATION_LAWS |
variable elimination | \(O((k + s)\,W)\) time, \(O((s + 1)\,W)\) space (CONTRACTION_WORK) |
conditional on the exponential time hypothesis |
| any other | the written order | \(O(k\,U)\) time, \(O(U)\) space (WRITTEN_ORDER_WORK) |
unresolved |
Here \(s = |\mathcal L_c|\), \(W\) is the number of elements of the largest bag of the chosen order (or of the output, when that is larger), and \(U\) is the product of every extent.
Elimination¶
For a summed letter \(v\) let \(F_v\) be the factors that contain it and \(B_v\) the letters of \(F_v\), its bag. One step replaces \(F_v\) by the single factor
summing at the same time every other summed letter whose factors all lie in \(F_v\).
Theorem. In a commutative semiring, a step leaves the value of the contraction unchanged.
Proof. Write the product of all factors as \(P \otimes Q\), with \(P\) the product of \(F_v\) and \(Q\) that
of the others. \(Q\) does not depend on \(v\), so by commutativity and associativity of \(\oplus\) the sum
over \(\mathcal L_c\) is a sum over \(\mathcal L_c \setminus \{v\}\) of a sum over \(v\), and by
distributivity \(\bigoplus_v P \otimes Q = (\bigoplus_v P) \otimes Q\). Reordering the product needs
\(\otimes\) commutative and associative. An index range of size zero makes a sum empty, which is the
zero, and the zero annihilates the product it enters; a product of no factors is the unit. These
are the laws of ELIMINATION_LAWS. QED.
Each law is read from the algebra's claims through Lawful.establishes, never from its name, so an
inline rule whose laws are only declared is evaluated in the written order. The order is chosen by
elimination_order, cheapest bag first by its
elements, ties in order of first mention. Finding an order of least width is NP-hard
(Arnborg, Corneil and Proskurowski 1987), so the plan records the
order, its bags and their widths and claims nothing about them. The same function orders the
variables of a rule body in eliminate, with the number of
variables of a bag as its cost.
A step multiplies \(|F_v| - 1\) times over \(B_v\) and sums \(|B_v|\) terms into \(|B_v| / n_v\) results. Every factor is joined once, and \(k + s\) factors exist at most, so the products are at most \((k + s)\,W\) and the sums at most \(s\,W\); the factors held between steps are at most one per step. This gives the bounds above (Dechter 1999; Abo Khamis, Ngo and Rudra 2016 for semirings). For a line of \(k\) matrices of order \(n\), \(W = n^3\), where the written order forms a tensor of \(n^{k+1}\) elements.
At a tie of a selective sum, each sum gives the whole derivative to its first best term, so the contraction gives it to the first best index in the order of elimination.
The written order¶
An algebra that does not establish the laws, such as
ProductFuzzyAlgebra, is evaluated as the formula is
written: every input is aligned to all letters, the inputs are multiplied left to right, and the
summed letters are summed at once. No law moves a sum inside a product, so whether fewer operations
suffice is open for such an algebra. A rule whose elements have axes of their own, such as a
MatrixSemiring, keeps them last and keeps the order of
its factors.
Work¶
EinsumWiring.apply_with_work returns a
WorkReport of the execution: products, binary sums (a sum of
\(m\) terms is \(m - 1\) of them), elements formed, and the largest number of formed elements held at
once, each in elements of the algebra; divisions, pivots, comparisons and hashing are measured as
zero. The evidence harness checks these counts against a dispatch counter and the claims on growing
families.
elimination
¶
Contractions of tensor networks by variable elimination.
A contraction x_1, ..., x_k -> y names each axis of each input by a
letter. Its value at an index of the output letters is the sum, over
every index of the letters that are not in the output, of the product of
the inputs' entries at that index. Evaluated as written, the product of
all inputs is formed over every letter before anything is summed, so the
largest tensor has the product of all extents as its size.
In a commutative semiring the sum distributes over the product, and both
may be regrouped and reordered. A summed letter can thus be removed by
multiplying only the factors that contain it and summing it away, which
replaces them by one factor over the other letters of their bag
(variable elimination; Dechter 1999, and InsideOut of Abo Khamis, Ngo and
Rudra 2016 for semirings). The work is bounded by the elements of the
largest bag of the chosen order instead of the product of all extents.
Finding an order of least width is NP-hard (Arnborg, Corneil and
Proskurowski 1987), so elimination_order chooses one greedily, and the
plan records the order and its widths without claiming them least.
The laws are read from the algebra's claims, never from its name: an
algebra that establishes every law of ELIMINATION_LAWS is eliminated,
and any other is evaluated in the written order, with its products taken
left to right in the order of the inputs.
References
Dechter, R. (1999). Bucket elimination: a unifying framework for reasoning. Artificial Intelligence 113(1-2), 41-85. doi:10.1016/S0004-3702(99)00059-4
Abo Khamis, M., Ngo, H. Q., and Rudra, A. (2016). FAQ: questions asked frequently. PODS, 13-28. doi:10.1145/2902251.2902280
Arnborg, S., Corneil, D. G., and Proskurowski, A. (1987). Complexity of finding embeddings in a k-tree. SIAM Journal on Algebraic and Discrete Methods 8(2), 277-284. doi:10.1137/0608024
ELIMINATION_LAWS
module-attribute
¶
ELIMINATION_LAWS: tuple[Law, ...] = (PLUS_ASSOCIATIVE, PLUS_COMMUTATIVE, PLUS_UNIT, TIMES_ASSOCIATIVE, TIMES_COMMUTATIVE, TIMES_UNIT, DISTRIBUTIVE, ZERO_ANNIHILATES, JOIN_ITERATED)
The laws elimination reorders by: a commutative semiring on one domain.
Sums are regrouped and reordered, products are regrouped and reordered, a sum is moved inside the product of the factors that do not contain its letter, an absent term is the zero and annihilates its product, a sum over several axes is taken one axis at a time, and a product of no factors is the unit.
ContractionAlgebra
¶
Bases: Protocol
The operations and law claims a contraction uses.
tensor_op
¶
join
¶
ContractionStep
didactic-model
¶
ContractionStep(*, letters: tuple[str, ...] = (), factors: tuple[int, ...] = (), bag: tuple[str, ...] = (), elements: int = 0)
Bases: Model
One step of an elimination: factors multiplied, letters summed out.
| PARAMETER | DESCRIPTION |
|---|---|
letters
|
The letters summed out at this step: the chosen one and every other summed letter whose factors are all joined here.
TYPE:
|
factors
|
The factors joined: inputs are numbered from zero in order, and
the result of step
TYPE:
|
bag
|
The letters of the joined factors, in order of first mention.
TYPE:
|
elements
|
Elements of the product over the bag.
TYPE:
|
ContractionPlan
didactic-model
¶
ContractionPlan(*, inputs: tuple[str, ...] = (), output: str = '', extents: dict[str, int] = {}, algorithm: Literal['variable elimination', 'written order'] = 'written order', order: tuple[str, ...] = (), steps: tuple[ContractionStep, ...] = (), width: int = 0, largest: int = 0)
Bases: Model
How a contraction is evaluated, fixed before any value is read.
| PARAMETER | DESCRIPTION |
|---|---|
inputs
|
The letters of each input.
TYPE:
|
output
|
The letters of the output.
TYPE:
|
extents
|
The extent of each letter, in order of first mention.
TYPE:
|
algorithm
|
TYPE:
|
order
|
The summed letters in the order
TYPE:
|
steps
|
The steps of an elimination; empty in the written order.
TYPE:
|
width
|
Letters of the largest bag less one: the induced width of the order. In the written order, every letter less one.
TYPE:
|
largest
|
Elements of the largest tensor the plan forms: the largest bag or the output, and in the written order the product of every extent.
TYPE:
|
summed
property
¶
The letters summed out, in order of first mention.
| RETURNS | DESCRIPTION |
|---|---|
tuple of str
|
Every letter not in the output. |
ContractionCounts
didactic-model
¶
Bases: Model
The work of one contraction, counted in elements of the algebra.
| PARAMETER | DESCRIPTION |
|---|---|
products
|
Binary products, one per element of each product formed.
TYPE:
|
sums
|
Binary sums: a sum of
TYPE:
|
allocated
|
Elements of the tensors formed.
TYPE:
|
peak
|
Elements of the tensors the plan formed that were held at once, besides the inputs.
TYPE:
|
elimination_order
¶
elimination_order(variables: Sequence[str], edges: Sequence[Collection[str]], kept: Collection[str], cost: Callable[[frozenset[str]], float] = len) -> tuple[str, ...]
A greedy order of the variables to sum out: cheapest bag first.
At each step the variable whose bag (the variables of the edges that
contain it, itself included) has the least cost is removed, ties in
the order of variables; its edges are merged into one without it.
Finding an order of least width is NP-hard (Arnborg, Corneil and
Proskurowski 1987), so this order is recorded, not claimed least.
| PARAMETER | DESCRIPTION |
|---|---|
variables
|
Every variable, in the order that breaks ties.
TYPE:
|
edges
|
The variables of each factor, and of any constraint that has to see its variables together.
TYPE:
|
kept
|
The variables that are not summed out.
TYPE:
|
cost
|
A deterministic ordered cost of the bag; its size by default.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of str
|
The variables not in |
Source code in src/quivers/core/elimination.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
plan_contraction
¶
plan_contraction(algebra: ContractionAlgebra, inputs: Sequence[str], output: str, extents: Mapping[str, int]) -> ContractionPlan
Choose how to evaluate a contraction, from its letters and extents.
| PARAMETER | DESCRIPTION |
|---|---|
algebra
|
The algebra, whose claims decide between elimination and the written order.
TYPE:
|
inputs
|
The letters of each input; no letter repeats within one.
TYPE:
|
output
|
The output letters, each in some input and none repeated.
TYPE:
|
extents
|
The extent of every letter.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ContractionPlan
|
The plan. Elimination orders its letters by the elements of their bags, ties in order of first mention. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If an input or the output is not a string of letters. |
ValueError
|
If there is no input, a letter repeats within an input or the output, an output letter is in no input, or a letter has no extent or a negative one. |
Source code in src/quivers/core/elimination.py
run_contraction
¶
run_contraction(plan: ContractionPlan, algebra: ContractionAlgebra, tensors: Sequence[Tensor], element_rank: int = 0) -> tuple[Tensor, ContractionCounts]
Evaluate a contraction by its plan.
In the written order every input is aligned to all letters, the inputs are multiplied left to right, and the summed letters are summed at once. In an elimination each step multiplies the factors it joins over its bag, in the order they were made, and sums its letters away; the factors left are multiplied over the output. At a tie of a selective sum the derivative goes to the first best term of each sum the plan forms, so to the first best index in the order of elimination.
| PARAMETER | DESCRIPTION |
|---|---|
plan
|
The plan.
TYPE:
|
algebra
|
The algebra the plan was made for.
TYPE:
|
tensors
|
The inputs, each with its index axes in the order of its letters
and then
TYPE:
|
element_rank
|
The number of trailing axes of one element of the algebra.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of torch.Tensor and ContractionCounts
|
The output, its index axes in the order of the output letters, and the work. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If an input is not a tensor. |
ValueError
|
If the inputs do not fit the plan. |
Source code in src/quivers/core/elimination.py
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 | |
fiber_join
¶
fiber_join(algebra: ContractionAlgebra, values: Tensor, fibers: Sequence[Sequence[int]], zero: Tensor, axis: int = -1) -> Tensor
The sum of a table over each fiber of a map from one of its axes.
This is the contraction of the table with the graph of a map: entry
p of the axis is summed into target q when p lies in
fiber q. Every fiber is padded with the zero to the length of the
largest, so one sum over a dense axis takes them all; the zero is the
unit of the sum, so padding changes no value. The work is the number
of targets times the largest fiber, never the table times the
targets.
| PARAMETER | DESCRIPTION |
|---|---|
algebra
|
The algebra.
TYPE:
|
values
|
The table.
TYPE:
|
fibers
|
For each target, the entries of the axis it sums; each entry in at most one fiber.
TYPE:
|
zero
|
The algebra's zero, of the table's shape with extent one on the axis.
TYPE:
|
axis
|
The axis mapped; the last by default.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The table with the axis replaced by one of |