Abstract
Transformers have revolutionized various domains, yet their application to scientific computing has been hindered by the inefficiency of existing "loop-transformer" architectures. This paper introduces NLAFormer, a streamlined transformer designed to execute fundamental numerical linear algebra (NLA) operations with high efficiency. By leveraging the inherent mathematical structure of transformers rather than emulating CPU-like control flows, NLAFormer achieves significant reductions in computational complexity and layer count. We prove that NLAFormer can express core NLA operations with minimal layers and demonstrate that it can learn to outperform classical iterative solvers, such as the Conjugate Gradient method, through data-driven optimization.
Introduction
The utility of transformers in scientific computing—specifically numerical linear algebra—has been limited by the "heavy" emulation of computer control flows in previous "loop-transformer" designs. For example, transposing an $n \times n$ matrix using such models requires an input size of $O(n^3)$ and four layers. This work proposes NLAFormer, which treats the transformer as a collection of native linear operators. The goal is to create a model that is both mathematically expressive and computationally lean, capable of performing NLA operations natively.
The NLAFormer Architecture
NLAFormer distills the standard transformer architecture into components specifically useful for NLA, focusing on the interplay between Multi-Head Attention and Feed-Forward Networks.
To solve complex problems like linear systems ($Ax = b$), the model employs a three-stage pipeline: a pre-processing block ($TF_{pre}$) for initialization, an iterative loop block ($TF_{loop}$) that mimics algorithmic steps, and a post-processing block ($TF_{post}$) for final output formatting. Training is conducted using either Result Supervision or Joint Supervision (incorporating intermediate variables like residuals).
Theoretical Expressiveness and Efficiency
A core contribution of this research is the theoretical foundation proving that a 1- or 2-layer transformer can express ten fundamental NLA operations. This represents a significant leap in efficiency over prior architectures.
As detailed in the table above, NLAFormer slashes complexity across the board. For matrix transposition, it reduces the required input size from $O(n^3)$ to $O(n^2)$ and the layer count from 4 to 1. Similar improvements are seen in matrix-matrix multiplication and pointwise arithmetic, where NLAFormer requires only 1-2 layers compared to the $O(n)$ layers often needed by loop-transformers.
Experimental Results
Evaluated on synthetic datasets of linear systems ($n=20$) with varying condition numbers, NLAFormer demonstrated remarkable capabilities:
- Spontaneous Algorithm Discovery: Under "Result Supervision," the model's internal variables spontaneously aligned with the intermediate variables of the classical Conjugate Gradient (CG) algorithm, effectively "discovering" the recursive logic of CG on its own.
- Performance Gains: NLAFormer identified update strategies that allowed it to converge to solutions up to 50% faster than standard CG and Jacobi-preconditioned CG (PCG) methods.
- Robustness: The model showed stable performance across varying embedding dimensions ($d=128$ to $512$) and condition numbers, indicating strong generalization within its problem domain.
Conclusion
NLAFormer demonstrates that transformers can be highly efficient engines for numerical linear algebra when their architecture is aligned with the underlying mathematics. While current experiments focus on small-scale linear systems, the potential for scaling to larger matrices and non-linear problems is significant. Future work will explore NLAFormer as a "smart initializer" to provide high-quality starting points for traditional solvers, bridging the gap between deep learning and classical scientific computing.
Summary updated