Transformers as Generic Deep Neural Networks: Exact Compilation, the Cost of Removing Activation-Activation Products, and Multi-Agent Attention Clustering
Organizations: St. Petersburg State University, Saint Petersburg 199034, Russia · Department of Software Engineering, Braude College of Engineering, Karmiel 2161002, Israel
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 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- 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 to 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 , on Pythia-70M.
Figures & tables
| Transform | Source | Bound, dense / routed | Strict | Strict, routed | Hadamard | Extended |
|---|---|---|---|---|---|---|
| Symbolic | ||||||
| / | ||||||
| / | ||||||
| Extended | Hadamard | Strict | Strict, polarization | |||||||||
| Acc. | Latency | Memory | Acc. | Latency | Memory | Acc. | Latency | Memory | Acc. | Latency | Memory | |
| Top- support | ||||||||||||
| 16 | 86.24 | 83.51 | 84.08 | 83.95 | ||||||||
| 32 | 89.23 | 87.46 | 87.98 | 87.71 | ||||||||
| 64 | 90.35 | 89.59 | 89.78 | 89.70 | ||||||||
| 197 | 91.36 | 90.35 | 90.73 | 90.57 | ||||||||
| Per-channel | Per-tensor | |||||||
|---|---|---|---|---|---|---|---|---|
| PV | Graph | Nodes | bf16 | PTQ | PTQ | |||
| 16 | 0.08 | strict | 380 | 84.08 | 82.61 | 83.02 | ||
| 32 | 0.16 | strict | 380 | 87.98 | 87.30 | 87.27 | ||
| 64 | 0.32 | strict | 380 | 89.78 | 89.72 | 89.64 | ||
| 197 | 1.00 | strict | 368 | 90.73 | 90.27 | 89.75 | ||
| 16 | 0.08 | extended | 247 | 88.58 | 86.13 | 84.90 | ||
| Oxford-IIIT Pet | ImageNet-1k | ||||||
|---|---|---|---|---|---|---|---|
| PV | Compiled | Distilled | Latency | Compiled | |||
| 1 | 0.01 | 15.73 | 6.26 | 10.84 | |||
| 2 | 0.01 | 42.95 | 4.07 | 27.79 | |||
| 4 | 0.02 | 67.10 | 2.20 | 45.03 | |||
| 8 | 0.04 | 79.50 | 0.97 | 57.20 | |||
| 16 | 0.08 | 86.24 | 0.38 | 64.66 | |||
| Tail drop | Calibrated | Exact | Calibrated | |||||
| pre | post | pre | post | pre | post | pre | post | |
| 0 | – | – | 30.22 | 31.10 | 30.22 | 31.10 | 35.98 | 37.24 |
| 1 | 22.11 | 30.64 | 30.45 | 31.32 | 30.95 | 31.73 | 36.17 | 37.36 |
| 2 | 26.05 | 31.63 | 30.62 | 31.55 | 31.36 | 31.97 | 36.35 | 37.52 |
| 4 | 29.66 | 32.07 | 30.85 | 31.81 | 31.71 | 32.11 | 36.50 | 37.62 |
| Exact tail mass | Tail drop | ||||||
|---|---|---|---|---|---|---|---|
| NLL | NLL | PPL | NLL | NLL | PPL | ||
| 128 | 4.5496 | 0.0000 | 94.59 | 4.5496 | 0.0000 | 94.59 | 0.000 |
| 64 | 4.5497 | 0.0002 | 94.61 | 4.5497 | 0.0002 | 94.61 | 0.004 |
| 32 | 4.5513 | 0.0017 | 94.75 | 4.5567 | 0.0072 | 95.27 | 0.023 |
| 16 | 4.5584 | 0.0088 | 95.43 | 4.5885 | 0.0390 | 98.35 | 0.062 |
| 8 | 4.5822 | 0.0326 | 97.73 | 4.6811 | 0.1316 | 107.89 | 0.123 |
| Accelerator | Unary maps on the NPU | Strict graph | Hadamard graph | Extended graph |
| Arm Ethos-U55/U65 [ 68 ] | Exp, Log, Rsqrt, GELU, Sigmoid, Tanh | Whole | Whole | Whole |
| Arm Ethos-U85 [ 68 ] | Exp, Log, Rsqrt, GELU, Sigmoid, Tanh | Whole | Whole | Whole |
| VeriSilicon VIP [ 69 ] | Exp, Log, Rsqrt, GELU, Sigmoid, Tanh | Whole | Whole | Whole |
| TI TDA4VM, C7x with MMA [ 70 ] | Exp, Log, Sqrt, GELU, Sigmoid, Tanh; power by a constant | Whole | Whole | Whole |
| Rockchip RK3588 [ 71 ] | GELU, Sigmoid, Tanh; power with exponent , 2 or 3 | Whole | Whole | Whole |
| Renesas R-Car V4H/V4M, CNN-IP and DSP [ 72 ] | CNN-IP: ReLU, Sigmoid, Tanh | DSP: Rsqrt | DSP: Rsqrt | DSP: MatMul, softmax, LayerNorm, GELU |
| Time (ms/batch) | Working memory (MiB) | |||||||||||
| Strict | Hadamard | Strict | Hadamard | |||||||||
| Module | top- | routed | top- | routed | Ext. | Ref. | top- | routed | top- | routed | Ext. | Ref. |
| ( ) | 49.7 | 10.7 | 31.6 | 9.2 | 4.0 | 4.1 | 469 | 77 | 471 | 78 | 14 | 14 |
| ( ) | 7.4 | 11.8 | 4.9 | 7.0 | 5.6 | 2.8 | 81 | 81 | 78 | 78 | 39 | 12 |
| LayerNorm | 23.0 | 22.2 | 17.9 | 17.9 | 17.9 | 5.5 | 16 | 17 | 19 | 19 | 19 | 9 |
| Softmax | 24.9 | 24.0 | 14.0 | 12.7 | 15.1 | 2.1 | 15 | 18 | 9 | 11 | 8 | 28 |
| Norm expansion | |||||
|---|---|---|---|---|---|
| Cancellation | Polarization, per term | partial sums | per term | per term, scaled | |
| 16 | 65 | 4.3% | 9.1% | 5.0% | 1.0% |
| 197 | 543 | 14.1% | 57.4% | 14.4% | 2.7% |
| tail drop | fixed | calibrated | exact | calibrated, distilled | |
|---|---|---|---|---|---|
| 1 | 2.53 | 2.78 | 15.73 | 8.69 | 41.89 |
| 2 | 3.16 | 2.86 | 42.95 | 21.31 | 64.57 |
| 4 | 2.83 | 3.11 | 67.10 | 58.60 | 78.09 |
| 8 | 5.70 | 5.51 | 79.50 | 81.77 | 84.71 |
| 16 | 39.44 | 39.33 | 86.24 | 88.80 | 88.58 |
| 32 | 81.71 | 82.07 | 89.23 | 90.84 | 90.19 |
| calibrated mean , training | exact , test | |
|---|---|---|
| 1 | 0.7825 | (0.0007, 0.9925) |
| 2 | 0.7139 | (0.0004, 0.9852) |
| 4 | 0.6294 | (0.0004, 0.9710) |
| 8 | 0.5278 | (0.0001, 0.9438) |
| 16 | 0.4116 | (0.0000, 0.8915) |
| 32 | 0.2826 | (0, 0.7920) |
| compiled | CE saved | CE mean | Jeffreys saved | Jeffreys mean | |
|---|---|---|---|---|---|
| 1 | 15.73 | 41.24 | 41.89 | ||
| 2 | 42.95 | 64.49 | 64.57 | ||
| 4 | 67.10 | 77.02 | 78.09 | ||
| 8 | 79.50 | 84.33 | 84.71 | ||
| 16 | 86.24 | 88.17 | 88.58 | ||
| 32 | 89.23 | 89.92 | 90.19 |
| Network | Model | bf16 | fp32 | bf16 fp32 |
| DeiT-Tiny, Pet | reference | 91.36 | 91.33 | |
| extended | 91.36 | 91.33 | ||
| strict, GELU | 91.06 | 91.33 | ||
| Hadamard, GELU | 91.28 | 91.33 | ||
| strict, sigmoid GELU | 90.73 | 90.27 | ||
| Hadamard, sigmoid GELU | 90.35 | 90.27 |
| GELU | ||||||
|---|---|---|---|---|---|---|
| bf16 | , per-channel | , per-tensor | bf16 | , per-channel | , per-tensor | |
| 16 | 86.37 | 84.08 | ||||
| 32 | 89.07 | 87.98 | ||||
| 64 | 90.35 | 89.78 | ||||
| 197 | 91.06 | 90.73 | ||||
Efficient Transformer Inference
Efficient Transformer Inference studies methods and systems that reduce the latency, memory, and energy needed to run Transformer models.
See all 121 papers