Repository navigation
Orders and Costs
An einsum of several tensors is computed one pair of tensors at a time, and the order of the pairs decides the cost,
often by orders of magnitude. Summa chooses the order from the dimensions alone; EinsumPath shows it, with its cost.
Every block on this page runs after
Needs["Summa`"]Three matrices
chain = "ij,jk,kl->il"; dims = {{1000, 10}, {10, 1000}, {1000, 10}};
path = EinsumPath[chain, dims];
path["Contractions"] // Columnjk,kl->jl
ij,jl->il
Its cost in flops, the cost of the naive sum (one loop over all the indices at once) and the speed-up:
Lookup[path, {"Flops", "NaiveFlops", "Speedup"}]A step costs the product of the dimensions of its indices, one multiplication and addition for each set of their
values:
EinsumPath[spec, {dims1, dims2, ...}] needs only the dimensions; EinsumPath[spec, t1, t2, ...] reads them from the
tensors.
| Key | |
|---|---|
"Spec" |
the einsum, with the implicit result written out |
"Path" |
the order, as pairs of positions (below) |
"Method" |
how the order was found: "Optimal", "Greedy", "Sequential", "User", or "Trivial" for one or two tensors |
"Contractions" |
each step, as an einsum of two tensors |
"StepFlops" |
the cost of each step |
"Flops" |
the total cost, with the diagonals and sums taken within one tensor |
"NaiveFlops" |
the cost of the naive sum |
"Speedup" |
"NaiveFlops" divided by "Flops"
|
"LargestIntermediate" |
the number of entries of the largest intermediate tensor, a measure of the memory needed |
Lookup[path, {"StepFlops", "LargestIntermediate"}]{{100000., 100000.}, 10000.}
The option "Path", of Einsum and of EinsumPath, EinsumFunction, EinsumDiagram and EinsumTree, takes
-
Automatic(the default):"Optimal"up to eight tensors,"Greedy"beyond; -
"Optimal": an exact search, the order of least flops; up to fourteen tensors (beyond, it givesEinsum::optbigand uses"Greedy"); -
"Greedy": at each step, among the pairs that share an index, the one whose result is smallest compared to the two tensors (as in opt_einsum); -
"Sequential": from left to right; - a list of pairs (below).
Lookup[EinsumPath[chain, dims, "Path" -> #], "Flops"] & /@ {"Optimal", "Greedy", "Sequential"}The greedy order is often optimal, not always. In this network of seven tensors it costs four times as much:
net = "gi,ld,kji,hd,fba,kh,jef->";
netDims = {{3, 5}, {5, 8}, {20, 5, 5}, {8, 8}, {5, 3, 5}, {20, 8}, {5, 5, 5}};
Lookup[EinsumPath[net, netDims, "Path" -> #], "Flops"] & /@ {"Optimal", "Greedy", "Sequential"}A path is a list of pairs, in the format of numpy's einsum_path but counted from 1: each pair names two tensors by
their positions in the current list; both are removed, and their result is appended at the end. For the chain,
{{2, 3}, {1, 2}} contracts {{1, 2}, {1, 2}} is
EinsumPath[chain, dims, "Path" -> {{1, 2}, {1, 2}}]["Flops"]The order changes the cost, not the result:
SeedRandom[1]; {ta, tb, tc} = RandomReal[1, #] & /@ dims;
Einsum[chain, ta, tb, tc, "Path" -> {{1, 2}, {1, 2}}] == Einsum[chain, ta, tb, tc]True
A path from numpy, [(1, 2), (0, 1)], is {{2, 3}, {1, 2}} here. A path must contract everything, n - 1 pairs for
n tensors (Einsum::path otherwise).
EinsumTree draws the order: each box is an intermediate tensor, with its indices, what the step does (∑ the indices
it sums, ∘ a batch product, ⊗ an outer product) and its flops; the title gives the totals. Four matrices:
EinsumTree["ij,jk,kl,lm->im", {{1000, 10}, {10, 1000}, {1000, 10}, {10, 1000}}]
Without dimensions, EinsumTree[spec] gives every index the dimension 8 (the option "DefaultDimension") and shows no
flops. EinsumFunction[...]["Tree"] is the tree of a compiled einsum.
The exact search grows as Automatic turns to the greedy search
beyond eight. "Optimal" pays when the contraction takes much longer than the search, or is done many times
(EinsumFunction plans once: Speed). The norm of a matrix-product state of six sites,
twelve tensors, with labels made by code:
chainOf[l_, bond_] := Table[Join[If[i > 1, {bond[i - 1]}, {}], {phys[i]}, If[i < l, {bond[i]}, {}]], {i, l}];
norm6 = Join[chainOf[6, ket], chainOf[6, bra]] -> {};
EinsumDiagram[norm6]
With bonds of dimension 32, the greedy order costs
dims6 = Map[If[Head[#] === phys, 2, 32] &, First[norm6], {2}];
Lookup[EinsumPath[norm6, dims6], {"Method", "Flops", "Speedup"}]and the exact search, over all the subsets of the twelve tensors, finds no better order:
EinsumPath[norm6, dims6, "Path" -> "Optimal"]["Flops"]Start
Recipes
Reference
Documents