LaTex2Web logo

Documents Live, a web authoring and publishing system

If you see this, something is wrong

Table of contents

First published on Friday, Nov 13, 2026 and last modified on Sunday, Nov 15, 2026 by François Chaplais.

Like what you see? Register! It's free and requires no credit card. Or Book a demo!
Attention at the Theoretical Minimum: A Mathematics of Arrays Framework for Memory-Optimal Transformer Kernels

Lenore Mullin University at Albany, SUNY, Albany, NY, USA Email

Gaetan Hains LACL, Université Paris-Est Créteil, Créteil, France Email

Keywords: Mathematics of Arrays, transformer attention, memory optimization, Denotational Normal Form, Operational Normal Form, exascale computing, formal verification, psi-reduction calculus, FlashAttention, high-performance computing

Abstract

1 Introduction

2 Related Work

3 Mathematics of Arrays Framework

Definition 1. Arrays and Shape

Definition 2. MoA Primitives

Definition 3. Omega Operator

Definition 4. Denotational Normal Form

Definition 5. Operational Normal Form

Theorem 1. Storage Theorem

4 MoA DNF Derivation of Attention

Proposition 1. Memory Minimality

\[ O\left(nd_k + nd_v\right) ~\text{versus}~ O\left(n^2 + nd_k + nd_v\right) \]

5 Numerical Verification

\[ \begin{align*} \mathbf{Q} &= \begin{pmatrix} -0.1984 & 0.2698 & 0.3414 & -0.0372 \\ 0.2547 & -1.0674 & 0.3460 & -2.5242 \\ 0.6822 & -0.6265 & 0.0252 & 0.3978 \end{pmatrix}, \\ \mathbf{K} &= \begin{pmatrix} -1.1567 & 0.6885 & -0.1884 & 0.4743 \\ 0.2246 & 1.7564 & 0.5235 & -2.3014 \\ -1.5899 & 0.3730 & -0.8257 & -1.2069 \end{pmatrix}, \\ \mathbf{V} &= \begin{pmatrix} 1.0739 & 0.4006 & -0.9671 & 0.4870 \\ 0.5589 & -0.7209 & -0.7650 & 0.2689 \\ 0.8237 & 0.3763 & 0.8320 & 0.0014 \end{pmatrix}. \end{align*} \]
import torch

def stable_softmax(x, dim=-1):
    # Subtract row maximum for numerical stability
    z = x - torch.max(x, dim=dim, keepdim=True)[0]
    num = torch.exp(z)
    den = torch.sum(num, dim=dim, keepdim=True)
    return num / den

# Dimensions: batch=1, seq_len=3, embed_dim=4
batch_size, seq_len, embed_dim = 1, 3, 4
d_k = embed_dim

# Random Q, K, V matrices (shape: [1, 3, 4])
q = torch.randn(batch_size, seq_len, embed_dim)
k = torch.randn(batch_size, seq_len, embed_dim)
v = torch.randn(batch_size, seq_len, embed_dim)

# Step 1-2: Scaled similarity scores
scores = torch.matmul(q, k.transpose(-2, -1)) / (d_k ** 0.5)

# Step 3: Numerically stable softmax
attn_weights = stable_softmax(scores, dim=-1)

# Step 4: Weighted sum of values
output = torch.matmul(attn_weights, v)

6 Predictive Performance and Energy Model

Theorem 2. Lower Bound on Attention Data Movement

7 Implementation Roadmap


Algorithm 1 generic C implementation from MoA DNF
1.Require: \( Q[n][d_k]\) , \( K[n][d_k]\) , \( V[n][d_v]\) , \( \sqrt{d_k}\)
2.Ensure: \( \textit{Output}[n][d_v]\)
3.for \( i'' \leftarrow 0\) to \( n-1\) do
4. Compute row \( i''\) of attention weights
5.for \( k \leftarrow 0\) to \( n-1\) do
6.\( s[k] \leftarrow \sum_{j=0}^{d_k-1} Q[i''][j] \cdot K[k][j] \;/\; \sqrt{d_k}\)
7.end for
8.\( m \leftarrow \max_{k} s[k]\)
9.for \( k \leftarrow 0\) to \( n-1\) do
10.\( e[k] \leftarrow \exp(s[k] - m)\)
11.end for
12.\( Z \leftarrow \sum_{k=0}^{n-1} e[k]\)
13.for \( k \leftarrow 0\) to \( n-1\) do
14.\( w[k] \leftarrow e[k] / Z\)
15.end for
16. Compute row \( i''\) of output
17.for \( p_v \leftarrow 0\) to \( d_v-1\) do
18.\( \textit{Output}[i''][p_v] \leftarrow \sum_{p=0}^{n-1} w[p] \cdot V[p][p_v]\)
19.end for
20.end for

\[ N = \langle n \rangle \;\to\; \langle n/132, 132 \rangle \]
\[ \frac{\partial \mathcal{L}}{\partial \mathbf{Q}} \;=\; \frac{\partial \mathcal{L}}{\partial \textit{Output}} \cdot \mathbf{V}^\top \cdot \frac{\partial \mathrm{softmax}}{\partial (\mathbf{Q}\mathbf{K}^\top/\sqrt{d_k})} \cdot \frac{\mathbf{K}}{\sqrt{d_k}}, \]

8 Mechanization: From Python to MoA to FPGA via C

9 Exascale and National Priority Relevance

10 Conclusion

Appendix

A Complete Element-Level Derivation Trace

\[ \begin{align*} s_{0,0} &= (-0.1984)(-1.1567) + (0.2698)(0.6885) + (0.3414)(-0.1884) + (-0.0372)(0.4743) \\ &= 0.22960 + 0.18577 - 0.06432 - 0.01764 = 0.33341. \end{align*} \]
\[ \begin{align*} s_{0,1} &= (-0.1984)(0.2246) + (0.2698)(1.7564) + (0.3414)(0.5235) + (-0.0372)(-2.3014) \\ &= -0.04456 + 0.47387 + 0.17872 + 0.08561 = 0.69364. \end{align*} \]
\[ \begin{align*} s_{0,2} &= (-0.1984)(-1.5899) + (0.2698)(0.3730) + (0.3414)(-0.8257) + (-0.0372)(-1.2069) \\ &= 0.31544 + 0.10063 - 0.28185 + 0.04490 = 0.17912. \end{align*} \]
\[ x_{\text{row 0}} = \langle 0.16670,\; 0.34682,\; 0.08956 \rangle. \]
\[ z_{\text{row 0}} = \langle -0.18012,\; 0.0,\; -0.25726 \rangle. \]
\[ w_{\text{row 0}} = \langle 0.3202,\; 0.3834,\; 0.2964 \rangle, \]
\[ \begin{align*} \textit{Output}[0][0] &= 0.3202 \cdot 1.0739 + 0.3834 \cdot 0.5589 + 0.2964 \cdot 0.8237 = 0.3439 + 0.2143 + 0.2441 = 0.8023, \\ \textit{Output}[0][1] &= 0.3202 \cdot 0.4006 + 0.3834 \cdot (-0.7209) + 0.2964 \cdot 0.3763 = 0.1283 - 0.2764 + 0.1115 = -0.0366, \\ \textit{Output}[0][2] &= 0.3202 \cdot (-0.9671) + 0.3834 \cdot (-0.7650) + 0.2964 \cdot 0.8320 = -0.3096 - 0.2933 + 0.2466 = -0.3563, \\ \textit{Output}[0][3] &= 0.3202 \cdot 0.4870 + 0.3834 \cdot 0.2689 + 0.2964 \cdot 0.0014 = 0.1560 + 0.1031 + 0.0004 = 0.2595, \end{align*} \]

Acknowledgments

References

[1] Mullin, L., Hains, G., 2025. Towards automatic, predictable and high-performance parallel code generation. In: Proceedings of CSCE/PDPTA2024, Springer Nature Switzerland, pp. 109–119.

[2] Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A.N., Kaiser, Ł., Polosukhin, I., 2017. Attention is all you need. In: Advances in Neural Information Processing Systems (NeurIPS).

[3] Dao, T., Fu, D.Y., Ermon, S., Rudra, A., Ré, C., 2022. FlashAttention: Fast and memory-efficient exact attention with IO-awareness. arXiv:2205.14135.

[4] Qin, Y., Wang, Y., Deng, D., Zhao, Z., Yang, X., Liu, L., Wei, S., Hu, Y., Yin, S., 2023. FACT: FFN-attention co-optimized transformer architecture with eager correlation prediction. In: Proceedings of ISCA, pp. 1–14.

[5] Park, J., Yoon, H., Ahn, D., Choi, J., Kim, J.-J., 2020. OPTIMUS: Optimized matrix multiplication structure for transformer neural network accelerator. In: Proceedings of MLSys, vol. 2, pp. 363–378.

[6] Sharma, A., Krishna, L.H., Srinivasu, B., 2025. High-performance Gemmini-based matrix multiplication accelerator for deep learning workloads. IEEE Transactions on VLSI Systems 33 (12), 3276–3289.

[7] León-Vega, L.G., Chacón-Rodríguez, A., Salazar-Villalobos, E., Castro-Godínez, J., 2023. Acceleration of fully connected layers on FPGA using the Strassen matrix multiplication. In: Proceedings of IEEE BIP, pp. 1–6.

[8] Li, R., Chen, S., 2025. Design and implementation of an FPGA-based hardware accelerator for transformer. arXiv:2503.16731.

[9] Tan, X., Teo, T.H., 2025. Design and implementation of a BRAM-banked double-buffered matrix multiplication accelerator for transformer models on edge FPGAs. In: Proceedings of IEEE MCSoC, pp. 582–585.

[10] Chang, Y.-R., Chen, H.-F., Shih, H.-Y., 2025. MOSA: Matrix optimized self-attention hardware accelerator for mobile device. In: Proceedings of ICEIC, pp. 1–4.

[11] Dao, T., 2023. FlashAttention-2: Faster attention with better parallelism and work partitioning. arXiv:2307.08691.

[12] Ham, T.J., Jung, S.J., Kim, S., Oh, Y.H., Park, Y., Song, Y., Park, J.-H., Lee, S., Park, K., Lee, J.W., Jeong, D.-K., 2020. A\( ^3\) : Accelerating attention mechanisms in neural networks with approximation. In: Proceedings of IEEE HPCA, pp. 328–341.

[13] Yang, Q., Wang, X., Zhou, Y., Li, Q., Qiao, S., 2025. Hardware friendly transformer optimization with dynamic attention matrix fusion. In: Proceedings of IEEE ISCAS, pp. 1–5.

[14] Gao, Y., Song, Z., Wang, W., Yin, J., 2023. A fast optimization view: Reformulating single layer attention in LLM based on tensor and SVM trick. arXiv:2309.07418.

[15] Binder, E., Sudarsanam, A., Sunkavalli, R., Low, T.M., 2025. FATHOM: Fast attention through optimizing memory. In: Proceedings of IEEE IPDPS, pp. 1166–1178.

[16] Liu, J., Wu, C., Li, J., Chen, X., Wang, S., 2024. Research on matrix multiplication optimization and deployment method for heterogeneous platforms. In: Proceedings of ICCBD+AI, pp. 266–270.

[17] Mullin, L.M., 1988. A Mathematics of Arrays. Ph.D. dissertation. Syracuse University, Syracuse, NY.

[18] Thomas, S., Mullin, L., Swirydowicz, K., 2021a. Improving the performance of DGEMM with MoA and cache-blocking. In: Proceedings of ACM ARRAY'21.

[19] Thomas, S., Mullin, L., Swirydowicz, K., Khan, R., 2021b. Threaded multicore GEMM with MoA and cache-blocking. In: Proceedings of CSC'21, CSCE'21.

[20] Mullin, L.M.R., 2023. From array algebra to energy efficiency on GPUs. arXiv:2306.11148.

[21] Markus, A., Mullin, L., 2026. Array access and performance regarding numerical algorithms. Transactions on Engineering on Computing Sciences 14 (2), 1–14.

[22] Mullin, L., Hains, G., 2026. New mathematics for computer performance: array algebra and cost functions. Technical Report. https://hal.science/hal-05442652 .

[23] Hains, G., 2026. From algorithm to code to hardware to execution: Why isn't computing performance an experimental science? In: Proceedings of PDP 2026, Euromicro Conference on Parallel, Distributed, and Network-Based Processing. Cluj-Napoca, Romania.

[24] Grout, I.A., Mullin, L., 2018. Hardware considerations for tensor implementation and analysis using the field programmable gate array. Electronics 7 (11), 320. 10.3390/electronics7110320.

[25] Grout, I.A., Mullin, L., 2022a. Realizing mathematics of arrays operations as custom architecture hardware-software co-design solutions. Information 13 (11), 528. 10.3390/info13110528.

[26] Grout, I.A., Mullin, L., 2022b. Processing in memory for mathematics of arrays operations. In: Proceedings of SAI Computing Conference, 2022.

Discussion: login to participate.