Optimized Matrix Multiplication
A C++ performance benchmark comparing standard O(n³) matrix multiplication against Strassen-Winograd and DeepMind's AlphaTensor 4x5x5 sub-cubic decomposition.
Timeline
Nov 2025
Role
Solo — algorithm implementation and benchmarking
Status
CompletedTechnology Stack
Key Challenges
- Transcribing DeepMind AlphaTensor 76-term bilinear tensor decomposition with exact indexing
- Managing recursive block allocations without heap fragmentation
- Designing bit-for-bit equivalence testing for floating-point routines
Key Learnings
- Theoretical scalar multiplication reduction does not guarantee faster wall-clock execution due to memory overhead
- Flat buffer index arithmetic minimizes cache misses compared to nested pointer structures
- Custom verification harnesses are essential before profiling complex numerical algorithms
Overview
Textbook matrix multiplication requires O(n³) scalar multiplications. In 2022, DeepMind's AlphaTensor discovered a sub-cubic decomposition for multiplying a 4×5 matrix by a 5×5 matrix using 76 scalar multiplications instead of 80.
This project implements the AlphaTensor decomposition in modern C++, comparing its practical execution performance against Strassen-Winograd and a baseline triple-loop on modern CPU architectures.
Implemented Algorithms
All algorithms operate on contiguous, row-major float* buffers to eliminate pointer chasing and maximize memory locality:
| Routine | Algorithm | Multiplications per Block |
|---|---|---|
naive_multiplication | Standard 3-loop baseline | |
Winograd | Strassen-Winograd 2×2 block recursion | 7 per 2×2 block (vs 8 standard) |
deepmind | AlphaTensor 4×5×5 tensor decomposition | 76 per 4×5×5 block (vs 80 standard) |
Implementation Details
1. Strassen-Winograd Recursion
Computes 2×2 sub-matrix blocks using 7 intermediate products ( through ). Once sub-block dimensions drop below a threshold, execution switches to the baseline algorithm to avoid recursion overhead.
2. AlphaTensor Decomposition
The 4×5×5 decomposition partitions matrices into block grids, evaluating 76 bilinear combinations ( through ) before linearly reassembling the 20 output blocks.
3. Bit-Exact Verification Oracle
To catch any indexing or sign bugs, the harness benchmarks fast paths against naive multiplication using small integer inputs that fit exactly into floating-point mantissas, verifying complete bit-for-bit equality.
Benchmark Execution
# Compile with aggressive optimization
g++ -O3 deepmind_winograd_matrix_multiplication.cpp -o matrix_mult
./matrix_multKey Findings
- Arithmetic vs. Memory Trade-Off: Reducing scalar multiplications by 5% introduces additional matrix additions and temporary buffer allocations.
- Cache Hierarchy Impact: At small-to-medium matrix sizes, memory bandwidth and cache alignment influence total execution time more heavily than arithmetic operation counts.
Tech Stack Summary
- Language: C++17
- Compiler: GCC / Clang (
-O3) - Focus Areas: Tensor Decomposition, Cache Optimization, Numerical Correctness, Hardware Profiling
