Wiring¶
A wiring is an
einsum string that says which
tensor axes denote the same index and which remain in the result. In
ij,jk->ik, j is summed out and
i and k survive. The wiring fixes the shape, and the composition rule fixes whether summing out
means a sum, a minimum, or another reduction. Einsum wiring thus extends binary composition to
contractions of any number of tensors. These contractions form an algebra of wiring diagrams only
when that algebra's equations hold (Spivak, 2013; Vagner et al., 2015).
wiring
¶
N-ary tensor contractions specified by shared axis names.
A CompositionRule defines binary composition:
(f >> g)[i, k] = ⋁_j f[i, j] ⊗ g[j, k]. A wiring rule can instead
combine several tensors in one contraction, for instance:
out[s, c] = ⋁_{p, q} (
arg1[s, p] ⊗ arg2[s, q] ⊗ kernel[p, q, c]
)
Here s remains shared by both arguments, while p and q are
eliminated. Expressing this through binary composition would also require
operations that arrange, copy, or pair the interfaces; plain chaining alone
does not describe that wiring.
WiringRule pairs a CompositionRule, which supplies tensor_op and
join, with an einsum-style specification of the axes to retain or reduce.
EinsumWiring evaluates it by the plans of quivers.core.elimination:
variable elimination when the rule establishes the laws of a commutative
semiring, the written order otherwise. CONTRACTION_WORK and
WRITTEN_ORDER_WORK are the cost claims of the two, and
EinsumWiring.apply_with_work measures the counters they bound.
Wiring diagrams and operads provide a mathematical account of how compatible
interfaces connect Vagner et al. (2015). Interpreting these contractions as
operations of an operad requires the relevant composition laws, including
the compatibility of tensor products and reductions. A runtime wiring
specification alone does not establish those laws.
CONTRACTION_MEASURES
module-attribute
¶
CONTRACTION_MEASURES: tuple[WorkField, ...] = measuring({WorkField.SCALAR_SUMS, WorkField.SCALAR_PRODUCTS, WorkField.SCALAR_DIVISIONS, WorkField.STAR_PIVOTS, WorkField.EXACT_OPERATIONS, WorkField.KEY_COMPARISONS, WorkField.HASH_OPERATIONS, WorkField.ELEMENTS_ALLOCATED, WorkField.PEAK_LIVE_ELEMENTS})
The counters a contraction measures.
CONTRACTION_WORK
module-attribute
¶
CONTRACTION_WORK = ComplexityClaim(query='contraction of a tensor network over a commutative semiring', parameters=(ComplexityParameter(name='k', meaning='inputs'), ComplexityParameter(name='s', meaning='letters summed out'), ComplexityParameter(name='W', meaning='elements of the largest bag of the chosen order, or of the output when that is larger')), model=ALGEBRA_OPERATIONS, bounds=(ComplexityBound(phase=Phase.EXECUTION, resource='time', expression='(k + s) W', status=Conditional(hypothesis='the exponential time hypothesis: for classes of bounded arity no algorithm beats treewidth by more than a logarithmic factor in the exponent', sources=(_FAQ, _DECHTER, _MARX))), ComplexityBound(phase=Phase.EXECUTION, resource='space', expression='(s + 1) W', status=Conditional(hypothesis='as for time', sources=(_FAQ, _DECHTER))), ComplexityBound(phase=Phase.DIFFERENTIATION, resource='time', expression='(k + s) W', status=Conditional(hypothesis='as for execution; a gradient costs a constant times it', sources=(_FAQ, _BAUR_STRASSEN))), ComplexityBound(phase=Phase.DIFFERENTIATION, resource='space', expression='(k + s) W', status=Conditional(hypothesis='the autograd tape holds the operands of every product', sources=(_FAQ,)))), inapplicable=(PhaseInapplicable(phase=Phase.CONSTRUCTION, reason='the order is chosen from the letters and their extents, in time polynomial in the letters and independent of the values'), PhaseInapplicable(phase=Phase.REFIT, reason='a refit is one more contraction; the plan of the extents is kept')), conditions=("every law of ELIMINATION_LAWS established by the algebra's own claims", 'the order is greedy by the elements of each bag and is recorded with its widths; an order of least width is NP-hard to find and is not claimed', 'at a tie of a selective sum the derivative goes to the first best index in the order of elimination'))
The cost of a contraction by variable elimination.
WRITTEN_ORDER_WORK
module-attribute
¶
WRITTEN_ORDER_WORK = ComplexityClaim(query='contraction of a tensor network over an algebra without those laws', parameters=(ComplexityParameter(name='k', meaning='inputs'), ComplexityParameter(name='U', meaning='the product of the extents of every letter')), model=ALGEBRA_OPERATIONS, bounds=tuple(ComplexityBound(phase=phase, resource=resource, expression=expression, status=Unresolved(obligation='whether fewer operations suffice: the algebra establishes no law that moves a sum inside a product, so the written expression is evaluated as it is defined')) for phase, resource, expression in _WRITTEN_BOUNDS), inapplicable=(PhaseInapplicable(phase=Phase.CONSTRUCTION, reason='the written order is read off the specification'), PhaseInapplicable(phase=Phase.REFIT, reason='a refit is one more contraction')), conditions=('the inputs are multiplied left to right in written order, then every contracted letter is summed at once, as the contraction is defined',))
The cost of a contraction in the written order.
WiringRule
¶
Bases: ABC
An n-ary morphism contraction under a fixed
CompositionRule.
Generalizes binary composition >> to arbitrary-arity
tensor networks. A WiringRule carries:
- a
composition_rulewhosetensor_opandjoindrive the contraction; - a wiring specification that names which axes of which inputs participate in which contractions, and which axes survive to the output.
Subclasses implement apply to perform the contraction
given a list of input tensors.
composition_rule
abstractmethod
property
¶
composition_rule: CompositionRule
The underlying binary composition rule of this contraction.
| RETURNS | DESCRIPTION |
|---|---|
CompositionRule
|
The enriching algebra. |
input_arity
abstractmethod
property
¶
Number of input tensors this wiring expects.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The number of inputs. |
apply
abstractmethod
¶
__repr__
¶
The class name and arity.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The representation. |
EinsumWiring
¶
EinsumWiring(composition_rule: CompositionRule, spec: str)
Bases: WiringRule
A multi-input contraction specified by an einsum-style string.
Spec syntax::
"input_1, input_2, ..., input_n -> output"
where each input_i and output is a sequence of axis
letters identifying which dimensions of that tensor align.
Axes that appear in some input but not in the output are
contracted (reduced via the composition rule's join);
axes that appear in the output are preserved (broadcast as
needed).
Examples::
EinsumWiring(product_fuzzy, "ij, jk -> ik")
# binary composition: contract over j, preserve (i, k)
EinsumWiring(product_fuzzy, "sp, sq, pqo -> so")
# ternary contraction: two argument tensors over
# (s, *) and a kernel contracted over (p, q),
# preserving (s, o)
How the contraction is evaluated is fixed by
quivers.core.elimination.plan_contraction from the rule's law
claims and the extents of the inputs, and recorded as a
quivers.core.elimination.ContractionPlan. A rule that establishes
every law of quivers.core.elimination.ELIMINATION_LAWS is
contracted by variable elimination, with the cost of
CONTRACTION_WORK; any other rule is evaluated in the written order,
with the cost of WRITTEN_ORDER_WORK: every input is aligned to all
letters, the inputs are folded left to right with tensor_op, and
the contracted letters are reduced at once with join. A rule
whose elements have axes of their own, such as a
quivers.core.algebras.MatrixSemiring, keeps them last in every
tensor.
| PARAMETER | DESCRIPTION |
|---|---|
composition_rule
|
The rule supplying
TYPE:
|
spec
|
The einsum specification.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
ValueError
|
If the specification is malformed. |
Parse and check a specification.
| PARAMETER | DESCRIPTION |
|---|---|
composition_rule
|
The rule supplying
TYPE:
|
spec
|
The einsum specification.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
ValueError
|
If the specification has no arrow, an empty input, a letter repeated within an input or the output, or an output letter in no input. |
Source code in src/quivers/core/wiring.py
composition_rule
property
¶
composition_rule: CompositionRule
The rule of the contraction.
| RETURNS | DESCRIPTION |
|---|---|
CompositionRule
|
The rule. |
input_arity
property
¶
The number of inputs.
| RETURNS | DESCRIPTION |
|---|---|
int
|
One per input specification. |
input_specs
property
¶
The letters of each input.
| RETURNS | DESCRIPTION |
|---|---|
tuple of str
|
One string per input. |
output_spec
property
¶
The letters of the output.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The output letters. |
complexity
property
¶
complexity: ComplexityClaim
The cost claim of the contraction under its rule.
| RETURNS | DESCRIPTION |
|---|---|
ComplexityClaim
|
|
plan
¶
plan(extents: Mapping[str, int]) -> ContractionPlan
The plan for given extents, made once per extents.
| PARAMETER | DESCRIPTION |
|---|---|
extents
|
The extent of every letter.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ContractionPlan
|
The plan. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If a letter has no extent of at least zero. |
Source code in src/quivers/core/wiring.py
apply_with_work
¶
apply_with_work(*tensors: Tensor) -> tuple[Tensor, WorkReport]
Run the contraction and count its work.
| PARAMETER | DESCRIPTION |
|---|---|
*tensors
|
The inputs, in the order of the specification.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of torch.Tensor and WorkReport
|
The output, its axes in the order of the output letters, and the work of execution in elements of the algebra. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If an input is not a tensor. |
ValueError
|
If the inputs do not fit the specification. |
Source code in src/quivers/core/wiring.py
apply
¶
Run the contraction.
| PARAMETER | DESCRIPTION |
|---|---|
*tensors
|
The inputs, in the order of the specification.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The output, its axes in the order of the output letters. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If an input is not a tensor. |
ValueError
|
If the inputs do not fit the specification. |
Source code in src/quivers/core/wiring.py
contraction_report
¶
contraction_report(counts: ContractionCounts) -> WorkReport
The work of a contraction as a work report of its execution.
| PARAMETER | DESCRIPTION |
|---|---|
counts
|
The counts.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
WorkReport
|
Sums, products, elements formed and the largest number held, each measured; a contraction divides, pivots, compares and hashes nothing, which is measured as zero. |
Source code in src/quivers/core/wiring.py
einsum_wiring
¶
einsum_wiring(composition_rule: CompositionRule, spec: str) -> EinsumWiring
Convenience constructor for EinsumWiring.
| PARAMETER | DESCRIPTION |
|---|---|
composition_rule
|
The rule supplying
TYPE:
|
spec
|
An einsum specification such as
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
EinsumWiring
|
The wiring rule for |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/quivers/core/wiring.py
contract
¶
contract(rule: WiringRule, *tensors: Tensor) -> Tensor
Apply a WiringRule to a sequence of input tensors.
Thin wrapper around WiringRule.apply for call-site
readability::
result = contract(my_rule, arg1, arg2, kernel)
is an alternative to the method-call form.
| PARAMETER | DESCRIPTION |
|---|---|
rule
|
The wiring rule to apply.
TYPE:
|
*tensors
|
The input tensors, in the order the rule expects.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The contracted tensor. |
Source code in src/quivers/core/wiring.py
Cited sources¶
David I. Spivak. The Operad of Wiring Diagrams: Formalizing a Graphical Language for Databases, Recursion, and Plug-and-Play Circuits. 2013. arXiv:1305.0297, doi:10.48550/arXiv.1305.0297.
Dmitry Vagner, David I. Spivak, and Eugene Lerman. Algebras of Open Dynamical Systems on the Operad of Wiring Diagrams. Theory and Applications of Categories, 30(51):1793–1822, 2015. URL: https://www.tac.mta.ca/tac/volumes/30/51/30-51abs.html.