Investigation · matrix-3way-multiplication

Is A·B·C cheaper as one three-way product?

Computing A·B·C normally means two matrix multiplications, (A·B)·C. With Strassen, that costs 7 + 7 = 14 scalar multiplications for 2×2 matrices. Can an algorithm that sees all three matrices at once do better? The answer depends on what you count as the cost.

Count scalar multiplications
No: 14 stays

A fused 3-way decomposition needs N⁴ products. Two-level schemes need 2·R(N). A search for 13 found only approximate (border) schemes.

Count EML tree size
Yes: 25 vs 33

x·y·z is much shorter as one exp−ln tree, but that tree contains no multiplications at all. It is the log-table trick.

Writes expensive, reads free
Yes: ½ writes

Fusing the chain writes only the N² outputs instead of 2N², with no extra arithmetic if one column of B·C fits in registers.

Repeated values, cache products
Yes: ≤ k²+k³

With k distinct values, a fused triple table makes the multiplication count independent of N, at the price of N⁴ additions.

Activation after the product
Yes: −48%

Up to 48% saved: settle saturated outputs from a guaranteed low-precision estimate, but only on the last product. The whole-chain cascade costs more than exact.

The common thread: every gain comes from deciding what to do with the intermediate product A·B or B·C. See Takeaways. Follow-up: what if matrices were triangles, pentagons or hexagons?

1 · Fused 3-way products: rank exactly N⁴

Pair the output with a fourth matrix D. Then A·B·C is the 4-linear form TN(A,B,C,D) = trace(ABCD) = Σ AijBjkCklDli. A "3-way algorithm" writes TN as a sum of R terms, each a product of three linear forms (one each in A, B, C) scattered into the outputs. The (A,C | B,D) flattening sends every index tuple (i,j,k,l) to its own column, so it is a permutation-like 0/1 matrix of rank N⁴. Tensor rank (and border rank) is at least any flattening rank, and the naive algorithm uses N⁴ terms. So R(TN) = N⁴: 16 for 2×2, worse than the Strassen chain's 14.

(A,B | C,D) flattening: only depends on the products AB and CD.
(A,C | B,D) flattening: one 1 per row and per column, so full rank.

Two-level schemes can only tie the chain

Suppose level 1 computes s products ℓ(A)·m(B), and level 2 multiplies t combinations of those by forms c(C). The span of the level-1 products must contain the four forms (AB)ij, so s ≥ R(⟨N,N,N⟩). By symmetry t ≥ R(⟨N,N,N⟩). The chain attains both bounds. At N = 3, beating 46 here would mean 3×3 matrix multiplication in fewer than 23 products, a famous open problem.

Two chained Strassen steps compute A·B·C with exactly 14 multiplications. This is also proved in Lean (lean/Chain.lean, core Lean 4, grind).

2 · The search for 13 multiplications

The next model is broader: any circuit whose gates have degree ≤ 3, where linear forms may mix entries of A, B and C, which allows Winograd-style commutative tricks. Constants and linear terms provably don't help, so the question reduces to (ABC)i = Σr γir vr Σk αrk ℓk mk with cost s + t. Levenberg–Marquardt fitting started from 60 random points per split:

split (s quadratic, t cubic)runsbest residualreading
7 + 7 (control)32~1e-3 → 0finds 14-schemes from about 1 start in 10
5 + 86036no
6 + 7463.2e-3border scheme (see chart)
7 + 66028no
8 + 56041no
9 + 46057no

The 6 + 7 near-hits are not algorithms. Under ridge continuation (minimise fit² + μ‖p‖² while μ → 0) the error falls only as the coefficients diverge, with residual roughly ∝ ‖p‖⁻³. That is the signature of an order-1 border decomposition: each product carries three ε−1/3 factors, the 1/ε terms cancel, and an O(ε) error remains. The same thing happens for A·B alone. 6 commutative products approximate 2×2 A·B arbitrarily well, but an exact algorithm needs 7 (Winograd 1971). So the "13" is just a border-6 A·B followed by the ordinary 7 for ·C, which is not a 3-way effect.

Ridge continuation, log–log axes. Both curves fall roughly like the dashed slope −3 line (local slopes −2.3 to −3.1), so the residual only reaches 0 if the coefficients go to infinity.

Exact check over GF(2)

The same model is encoded as SAT (CaDiCaL) and matched coefficient by coefficient as formal polynomials over GF(2). Finite fields have no border phenomena, so each answer is exact for GF(2), but says nothing directly about ℝ.

splitresulttime
7 + 7SAT26 min (rediscovered the A·(BC) chain)
8 + 5UNSAT48 min
6 + 7, 7 + 6, 5 + 8unresolvedno answer; stopped after 11 hours

3 · EML trees: x·y·z costs 25, not 33

Odrzywołek showed that eml(a,b) = exp(a) − ln(b) together with the constant 1 generates every elementary function. His direct search found x·y at RPN length 17. An exhaustive search written for this page reproduces his values (x−y 11, −x 15, 1/x 15, x² 17, x·y 17). The x·y tree is a log-domain accumulator: s = e − ln x, then s ← eml(ln s, y) = s − ln y, then exp(e − s). Each additional factor costs 8 tokens, giving 8n + 1 instead of 16(n−1) + 1 for nested trees.

Evaluated live in complex arithmetic (principal branch, ln 0 = −∞). Negative inputs work because the branch jumps cancel inside exp.

Why this doesn't help the matrix question. The EML tree has no multiplications in it: it is exp(ln x + ln y + ln z), and the saving is just dropping the exp→ln round trip between the two factors. In EML, products are cheap and addition is expensive (x+y needs 19 tokens). Each entry of A·B·C is a sum of four triple products, so the EML cost is dominated by additions.

4 · When writes are expensive and reads are free

This is the setting of non-volatile and asymmetric memory. Reads cost nothing, every word written to memory costs one, and a small scratch area of S registers is not counted as memory. The N² outputs must be written, so W ≥ N². Here three-way fusion does pay off: the intermediate A·B never has to be stored. Every schedule below runs in your browser, instrumented and checked against a direct product.

Total = ω·writes + operations, where operations are multiplications, or exps in the e/ln domain. The cheapest row is highlighted. Strassen rows use temporaries written to memory, the setting of Carson, Demmel et al., who proved Strassen-like algorithms cannot be write-avoiding.

Which memory words get written (N = 4)

5 · Repeated values: caching products

If the matrices contain only k distinct values, the same scalar products recur and can be cached. Quantised neural-network weights (4-bit means k = 16) and ternary weights are examples. A cache keyed by value pair is the same thing as grouping by the distributive law: Dil = Σa a · (Σj : Aij=a (BC)jl) needs one multiplication per distinct value in the row. Four schedules, each instrumented and checked against the exact product:

Multiplications against N (log–log) at the chosen k, from runs on random matrices. The fused triple table flattens out once all k³ triples have appeared. With all-distinct values, caching gains nothing and the triple table is the worst option.

Results at N = 32

computing…
Multiplications for one random instance per k, computed live by the same code as the table above. The cheapest schedule in each row is highlighted.

What this shows

This idea is well established for two-matrix products: the Four Russians method for Boolean matrices, the Mailman algorithm for small alphabets, and lookup-table kernels for quantised LLMs (LUT-GEMM, T-MAC). The triple-table trade-off for three factors was not found written up anywhere.

6 · An activation after the product: skip the saturated outputs

Neural networks rarely use A·B·C bare; they apply an activation: Y = f(A·B·C + β). Where f is flat, the exact value doesn't matter. For a sign only the side of 0 counts. A ReLU gives 0 for every negative input. A hardtanh or sigmoid is saturated far from 0. So the plan is to compute everything cheaply at b bits, attach a strict error bound that turns each output into an interval, and settle every output where f is flat across its interval. Only the undecided outputs get a deeper, full-precision multiplication. Cost here counts bit operations: a p-bit × q-bit multiply costs p·q, and full precision is 16 bits.

"bound" scales the strict error bound. At ×1 every decision is guaranteed correct. Smaller factors are a SeerNet-style prediction, with wrong decisions counted against the exact result.

What this shows

7 · Picking terms at random

Instead of lowering precision, estimate each output from a random sample of its terms and decide where it falls relative to a threshold (a sign or ReLU after the product). For three factors there's an appealing twist. Each output is a double sum, Dil = ΣjΣk AijBjkCkl, so you can sample (j, k) pairs directly at 2 multiplications each, without ever building a row or column of the intermediate. Cost here is multiplications; the exact chain is 2N³.

N = 32. Multiplications relative to the exact chain. Undecided outputs are recomputed exactly through the minimum row/column cover. *The guaranteed low-precision cascade of section 6 is shown for orientation; its percentage uses section 6's bit-operation cost (entries rescaled into [−1, 1)), not multiplications.

What this shows

Takeaways

The common thread is the intermediate product. Every gain came from treating A·B (or B·C) deliberately rather than as a black box:

Surprises

  1. Applying the shortcut to the whole chain cost more than doing nothing (107–181%). In a single product, fixing one uncertain output costs one dot product (N work). In a chain it costs a whole row of A·B or column of B·C (N² work), so the smallest unit of correction is a row or column, not an entry.
  2. The guaranteed bound was about 30× too cautious. With the bound shrunk to ≈ 0.03×, the whole-chain cascade reaches 50% with no wrong decisions on this data, beating the guaranteed hybrid (52%). Which strategy wins depends on whether you need a guarantee.
  3. More precision levels made it worse. A 4-bit first pass settled almost nothing and only added cost; one well-chosen level was best.
  4. ReLU, the most common activation, benefits least, because only its negative outputs can be skipped.
  5. A classic graph result turned up naturally: choosing the fewest rows and columns to recompute is a minimum vertex cover in a bipartite graph, solved exactly by König's theorem.
  6. Caching helps the plain chain more than expected. Each entry of A·B meets only k values of C, so a cache brings even the textbook chain to about k² + N²k multiplications.
  7. Random sampling mostly loses to low precision, and its empirical bounds are dangerously overconfident on sparse data. To decide a threshold, seeing every term roughly beats seeing some terms exactly.

For practice

Possibly new, and what to do next

Most results here rediscover known theory or practice (see the table below). Four may be new:

Next steps:

Prior work, and how these results compare

Three-way matrix products have been studied from several directions. Most of what this page finds either restates known theory for the chain tensor or rediscovers established practice. The table puts each result next to the closest published one.

topicpublishedthis pageverdict
Three-way forms in general Ordinary multiplication is the trilinear form trace(A·B·C). Rank 7 for 2×2: Strassen 1969, optimal over every field (Winograd 1971; Hopcroft–Kerr over GF(2)). Border rank is also 7 (Landsberg 2006; border support rank is seven). The four-way form trace(A·B·C·D) has rank and border rank exactly N⁴, via one permutation-like flattening. consistent Routine extension; not found stated for this tensor.
Products of many matrices The iterated matrix multiplication polynomial IMM is complete for algebraic branching programs. Research targets asymptotic formula and ABP lower bounds (small-depth multilinear formula bounds, set-multilinear formula bounds, sums of ABPs). Superpolynomial formula lower bounds remain open, an analogue of NL vs NC¹. Small-size exact question: 2×2, 13 vs 14. Two-level schemes need 2·R(⟨N,N,N⟩); degree-3 circuits show only border 13s. complementary Different regime; the literature doesn't answer 13 vs 14.
Approximate (border) algorithms Border multiplicative complexity is at least half the border rank (Landsberg survey), so commutative 2×2 algorithms need at least 4 approximately and 7 exactly. Bini-style approximate algorithms are classical. Numerical evidence that 6 commutative products approximate 2×2 A·B arbitrarily well (residual ∝ ‖p‖≈−3). open Fits inside the known 4…7 window. No explicit construction yet, and not found in a quick search.
Exact search over GF(2) SAT and flip-graph searches are standard tools for finding schemes (Heule–Kauers–Seidl, SAT 2019; Kauers–Moosbauer flip graphs). ⟨2,2,2⟩ has rank 7 over GF(2) (Hopcroft–Kerr 1971). Homogeneous degree-3 circuits for A·B·C: 7+7 SAT (26 min), 8+5 UNSAT (48 min), other splits unresolved. partial New but incomplete.
Writes expensive, reads cheap Write-avoiding algorithms: classical matmul can write only the output, but Strassen-like algorithms cannot avoid writes (Carson, Demmel et al. 2015; asymmetric-memory matmul). In practice: back-to-back GEMM fusion (Bolt, CUTLASS) and FlashAttention, which fuses softmax(QKᵀ)·V without writing the intermediate. Fused A·B·C writes N² words (the optimum) at 2N³ multiplications with N registers, or N³ + N⁴/S with S registers. Strassen with spilled temporaries writes about 25× more. rediscovery Same conclusion as the theory and the GPU practice.
Repeated values Products over small alphabets are cached or grouped: Four Russians (Boolean), Mailman (O(n²/log n) matrix–vector), LUT-GEMM and T-MAC for quantised weights. For A·B·C: caching the chain already gives ≈ k² + N²k multiplications. A fused triple table gives ≤ k² + k³, independent of N, but needs N⁴ additions. extension Known technique; the triple-table trade-off for three factors was not found stated.
Activation-aware skipping For a single product: SnaPEA (early exit for ReLU), SeerNet (low-bit prediction of ReLU zeros), Precision Gating (low precision first, high precision where needed), ComPreEND. Mostly predictive. Three factors with a strict bound: the whole-chain cascade costs more than exact; exact B·C plus a cascade on A·(BC) saves up to 48%, guaranteed. extension Known idea; the three-factor finding that the cascade belongs on the last product was not found stated.
Random sampling Sampling-based approximate products: Cohen–Lewis 1999 (random walks; concentrates for nonnegative matrices), Drineas–Kannan–Mahoney 2006 (norm-based error bounds), diamond sampling (Ballard–Kolda–Pinar–Seshadhri 2015, largest entries via cycles in a tripartite graph). Per-term sampling loses to low precision for threshold decisions. Walks through A→B→C weighted by the rest of the chain give guaranteed top-1% decisions at ≈ 25% of the multiplications on sparse nonnegative data. extension Cohen–Lewis applied to a chain of three, with guaranteed decisions.
EML trees Odrzywołek 2026: exp−ln with constant 1 generates all elementary functions; direct search gives x·y in 17 tokens. A ternary operator is named only as future work. Reproduced the paper's sizes. x·y·z takes 25 tokens directly vs 33 composed (8n+1 vs 16(n−1)+1), but with zero multiplications. new, minor Not found elsewhere; it is the log-table trick in EML form.
Genuinely ternary products The Bhattacharya–Mesner product multiplies three 3-D hypermatrices at once over a shared index; it has no associativity law (AMS Notices, hypermatrix algebra). Not studied here: A·B·C of ordinary matrices is associative and splits into two binary products. different object

What is proven, what is numerical

Rank of trace(ABCD) is exactly N⁴ (and so is its border rank)proof
Two-level nested schemes need ≥ 2·R(⟨N,N,N⟩) multiplicationsproof
Strassen∘Strassen = A·B·C with 14 multiplications; flattening injectivity for all NLean
No exact 13-multiplication degree-3 circuit over ℝ (only border schemes found)numerical
2×2 A·B has border quadratic complexity ≤ 6numerical
No 13-multiplication scheme over GF(2) with split 8+5SAT
Splits 6+7, 7+6, 5+8 over GF(2); circuits whose intermediates exceed degree 3open
x·y·z in 25 EML tokens (8n+1 for n factors)verified
No x·y·z EML tree shorter than 25open
Fused A·B·C attains the write lower bound N² at 2N³ multiplications with N registersconstruction
With k distinct values, a fused triple table needs ≤ k² + k³ multiplications for any N (N⁴ additions)construction
Activation cascade decisions are always correct at bound ×1 (strict error bound)proof + tests
Savings of 19–48% for f(A·(BC)) at N = 16–64 on random matricesmeasured
Random walks i→j→k→l land on output l with probability exactly Dil/ΣlDil (nonnegative data); decisions hold with probability ≥ 1 − δproof + tests
Walks need ≈ 16–30% of the multiplications for top-1% decisions on sparse heavy-tailed data (but many random draws)measured