arXiv:2610.28958cs.LGOct 2026

Manuscript Offprint

arXiv:2610.28958

Transformers as Generic Deep Neural Networks: Exact Compilation, the Cost of Removing Activation-Activation Products, and Multi-Agent Attention Clustering

Sergey Salishev, Zeev Volkovich, Oleg Granichin

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original. This text is shared under the same licence.

Abstract

Transformers are deployed as a special model class. Neural processing units (NPU) add dedicated operators for attention and normalization, and quantization relies on Transformer-specific algorithms. We show the computation does not force this. Every Transformer block, with attention, LayerNorm and a GELU MLP, compiles exactly into a generic deep neural network built from fixed affine maps, routing and the square, sigmoid and inverse square root, plus GELU or its sigmoid approximation. Without activation–activation products, every transform-wise compilation adds at least N2N^2 scalar nonlinear evaluations per head for dense scores. Keeping that one operation type removes this cost; its elementwise form, present on every surveyed NPU, suffices with the sigmoid and inverse square root. For top-k attention with exchangeable tail scores, we prove that exact or conditional-mean tail mass gives an unbiased, minimum-mean-square-error estimate of the attention distribution, while tail drop is conditionally biased. When queries or agents cluster, centroids choose the keys first, reducing the number of computed scores from N2N^2 to NkNk per head. On DeiT-Tiny, MambaIRv2-Light and Pythia-70M the compiled models match their references with all keys, standard post-training quantization applies, and the exact tail-mass estimate beats tail dropping with half as many keys on DeiT-Tiny and, for k≤8k \leq 8, on Pythia-70M.

Introduction

Transformers [1] are treated as a model class apart from the deep neural networks (DNNs) that accelerators and tools were first built for. This view has two practical consequences.

The first is hardware. Accelerators built for convolutional networks run a fixed operator set and hand everything else to the host. The Edge TPU compiler cuts the model at the first unsupported operation and runs the rest on the CPU [2]. NVIDIA’s Jetson DLA has no matrix-multiply layer [3]. On phones, llm.npu runs the linear layers on the NPU and keeps LayerNorm and attention on the CPU or GPU [4]. Vendors answer with Transformer-specific operators and units for attention, softmax and normalization [5–7].

The second is quantization. Post-training quantization (PTQ) of Transformers relies on dedicated algorithms. Liu et al. search quantization scales with a ranking-aware objective [8], and PTQ4ViT adds twin-uniform quantizers for the post-softmax and post-GELU activations [9]. FQ-ViT adds power-of-two factors for LayerNorm and a log-integer softmax [10]. RepQ-ViT reparameterizes quantization scales [11], IGQ-ViT groups channels per instance [12], I-BERT replaces the nonlinearities by integer polynomials [13], and SmoothQuant moves activation outliers into the weights of language models [14].

We show that the computation does not force this split. The argument has four steps.

Exact compilation. Every Transformer block, with LayerNorm, scaled dot-product attention, softmax and a GELU MLP, compiles exactly into a generic DNN of fixed affine maps, scalar nonlinearities and routing, with no product of two activations (Theorem 1). We call it the strict graph. Squares replace products, through ab=12[(a+b)2−a2−b2]ab = \frac{1}{2}[(a + b)^2 - a^2 - b^2] where the squares of the factors are shared and through polarization otherwise, and no exponential or logarithm is needed: squares, the sigmoid and the inverse square root suffice, with GELU or its approximation xσ(1.702x)x \sigma(1.702x) as the MLP activation.

The cost of strictness. Since the strict graph has no dedicated layers, standard DNN tools apply to it. But removing products of two activations has a price. For free arguments, any compilation without them adds at least N2N^2 scalar nonlinear evaluations per head at QK⊤QK^\top and NdNd at PV, whatever the scalar functions, depth or width (Theorem 2).

One more operation type. Adding activation–activation products removes the price. Their elementwise form, the Hadamard product, is on every NPU we survey, and it suffices: with it, a block needs only the sigmoid and the inverse square root besides its MLP activation, and only softmax adds work, 2N22N^{2} evaluations per head (Proposition 1). With the exponential and the reciprocal it adds no pointwise work (Proposition 2), and with matrix multiplication of two activations the extended compilation adds no work of either kind (Theorem 3). On a GPU, for k≤64k \leq 64, the Hadamard and strict graphs run at 1.0 to 1.3 and 1.2 to 1.5 times the latency of this extended graph. Theorems 2 and 3 make this operation type necessary and sufficient for compilation without added pointwise work. Practice already uses this operation set. ONNX defines LayerNormalization and Softmax as functions over it [15, 16], current NPUs expose it [6, 17, 18], and convolutional networks with squeeze-and-excitation gating already use the elementwise product [19, 20].

Clustered attention. The attention weights of a query cluster on a few keys [21–23] when its scores have a gap (Lemma 1). Queries cluster too [24, 25], and queries close to one point share their top keys (Lemma 2). Top-k attention keeps the k largest scores of each query [21, 22], the topological rule of bird flocks [26]. We add an estimate of the omitted probability mass, spread uniformly over the dropped keys. For exchangeable tail scores, exact or conditional-mean tail mass gives the unbiased, minimum-mean-square-error attention-distribution estimate; tail drop is conditionally biased (Theorem 4). Centroids of the query clusters [25] choose the keys before the scores, so the strict graph forms only Nk scores per head (Corollary 2, Proposition 5). One step of the Motsch–Tadmor flocking model is one attention head (Proposition 6).

We test the compilation on three model families: DeiT-Tiny [27] for image classification on Oxford-IIIT Pet [28] and ImageNet-1k [29], MambaIRv2-Light [30] for super-resolution, and Pythia-70M [31] for language modeling.

Related work. Universal approximation gives some network within each error on a compact set [32, 33]. Forming products from squares is classical: tables of quarter-squares multiply by polarization [34], Winograd's inner product forms the self-terms of each row once and halves the products of a matrix multiplication [35], and expressivity proofs for ReLU networks use ab=12[(a+b)2−a2−b2]ab = \frac{1}{2}[(a + b)^2 - a^2 - b^2] [36]. Compilers such as Glow map a model onto the operators a device provides [37]. Privacy-preserving inference decomposes attention, softmax and LayerNorm into primitive operations for cryptographic protocols, usually with polynomial approximations [38–40]. Multiplication-free training replaces every product by an approximate piecewise-affine operation and trains on that arithmetic [41]. Training-time alternatives simplify LayerNorm [42] or remove it [43, 44]. Top-k attention saves memory on long contexts [21–23], usually by renormalizing the kept scores. Scatterbrain adds a low-rank estimate of the non-selected attention to a sparse one [45], Combiner attends to the rest of the sequence through region summaries [46], and MagicPIG replaces top-k selection by sampling to avoid its bias [47]. Clustered attention chooses the kept keys by query centroids and gives the other keys the weights of the centroid [25]; we use its key selection with calibrated centroids. Lookup tables, piecewise-linear units and integer approximations implement scalar nonlinearities in hardware [7, 48, 49].

What is new. Unlike universal approximation, the compiled graph is exact on all inputs, reuses the trained weights and has a known size. Unlike compilers and privacy-preserving decompositions, we ask which small operator set expresses a whole block exactly. New are the lower bound that prices product-free compilation and the extended construction that removes the price. Unlike multiplication-free training, we compile a trained model, keep LayerNorm, and remove only products of two activations. Our tail-mass estimate is simpler than those of Scatterbrain, Combiner and clustered attention. It is a fixed affine map for a constant mass. For clustered queries, new are the cost bounds for the selected scores and the flocking step as one attention head.

References (42)