Repository navigation
How It Works
What happens in a call Einsum[spec, t1, t2, ...]. The first four stages make a plan, which depends only on the einsum,
the shapes of the tensors and the options, and is kept in a cache; the last one computes. The plan of an
EinsumFunction can be read, ef["Plan"]. Every block on this page runs after
Needs["Summa`"]The string is split at the arrow and at the commas: spaces are dropped, → is the arrow, ... and … are the
ellipsis; a list is read as it is. The labels become the integers 1, 2, ... in their order of first appearance, the
ellipsis becomes as many labels as each tensor needs (aligned from the right), and the implicit result is written out:
EinsumPath["ba,cb", {{2, 3}, {4, 2}}]["Spec"]ba,cb->ac
Every index must have one dimension in all the tensors that have it, or the dimension 1, which is broadcast. A wrong einsum stops here, with one message (FAQ).
Before any product, each tensor is reduced alone: its broadcast axes of size 1 are dropped; an index repeated in it
gives a diagonal, taken by one Transpose with repeated levels (Transpose[t, {1, 1, 2}] is Total. For "iijx,jk->k" the first
tensor becomes a vector in j (label 2) at once:
ef = EinsumFunction["iijx,jk->k", {{3, 3, 4, 5}, {4, 6}}];
First[ef["Plan"]["Pre"]]<|Squeeze -> None, Diag -> {1, 1, 2, 3}, Sum -> {3, 1}, Labels -> {2}|>
Transpose[t, {1, 1, 2, 3}] takes the diagonal in i, and Total sums the levels of x and of i.
The order is a sequence of pairwise contractions. When a set
with "Optimal") finds the least total cost by dynamic
programming over the subsets,
over the disjoint Automatic is the exact search up to eight tensors and the greedy one beyond; "Optimal" is accepted up to
fourteen.
Each step of the order becomes a call to one kernel. The indices of its two tensors are sorted: an index of one tensor only, not needed later, is summed first; an index of both tensors is a batch index if it is still needed, and summed otherwise; the others are free. Then
- if one tensor is a scalar: a multiplication (
Scale); - if nothing is summed and the indices of one tensor are among those of the other: a product element by element,
broadcast, with no transpose (
Hadamard,Prefix,Suffix); - if both tensors have the same indices, some summed and some kept: the product in the layout of one tensor and
Totalover the summed levels, used when the batched product would have to transpose (Reduce); - with no batch index: the product of two matrices, (free × summed) and (summed × free), by BLAS (
Dot); - with batch indices: a batch of matrix products,
MapThread[Dot, ...]or products of whole arrays andTotal, chosen by a cost model (Batch; the option"BatchMethod").
For the last two, the order of the two tensors, of the batch indices and of the summed indices (eight choices) is the
one that transposes the least data, with the transpose of the result in the last step; then each tensor needs at most
one Transpose and one ArrayReshape. The kernel of each step is in the plan:
kernels[spec_, dims_] := spec <> ": " <> StringRiffle[#["Type"] & /@ EinsumFunction[spec, dims]["Plan"]["Steps"], ", "];
Column[{kernels["ij,jk->ik", {{3, 4}, {4, 5}}], kernels["bij,bjk->bik", {{9, 4, 4}, {9, 4, 4}}],
kernels["ij,j->ij", {{3, 4}, {4}}], kernels["kh,kh->h", {{5, 4}, {5, 4}}], kernels["ai,bi,ci->abc", {{40, 8}, {40, 8}, {40, 8}}]}]ij,jk->ik: Dot
bij,bjk->bik: Batch
ij,j->ij: Suffix
kh,kh->h: Reduce
ai,bi,ci->abc: Batch, Dot
The tensors are packed when they can be, and preprocessed; the steps run in order, and each intermediate tensor is
dropped as soon as it has been used; a last Transpose, if needed, puts the result in the order asked. The plan is kept
under the einsum, the shapes and the options "Path" and "BatchMethod", so that the next call with the same shapes
only computes; the cache keeps up to 2000 plans, starts again when full, and EinsumClearCache[] empties it. An
EinsumFunction keeps its own plan.
The tests (wolfram -script Tests/RunTests.wls, 735 of them) compare Einsum with numpy.einsum on 213 contractions
computed in exact integer arithmetic, each with every order and every kernel, and with an explicit sum in machine
precision, real and complex; on 40 networks the exact search must cost as little as an exhaustive search over all
orders, and the greedy one no less.
Start
Recipes
Reference
Documents