SEMA: a Scalable and Efficient Mamba like Attention via Token Localization and Averaging
Abstract
Attention is the critical component of a transformer. Yet the quadratic computational complexity of vanilla full attention in the input size and the inability of its linear attention variant to focus have been challenges for computer vision tasks. We provide a mathematical definition of generalized attention and formulate both vanilla softmax attention and linear attention within the general framework. We prove that generalized attention disperses, that is, as the number of keys tends to infinity, the query assigns equal weights to all keys. Motivated by the dispersion property and recent development of Mamba form of attention, we design Scalable and Efficient Mamba like Attention (SEMA) which utilizes token localization to avoid dispersion and maintain focusing, complemented by theoretically consistent arithmetic averaging to capture global aspect of attention. We support our approach on Imagenet-1k where classification results show that SEMA is a scalable and effective alternative beyond linear attention, outperforming recent vision Mamba models on increasingly larger scales of images at similar model parameter sizes. Source code can be found at: https://github.com/nhatthanhtran/SEMA.
Lay Summary
If a person is asked to memorize a small picture of a dog, they may focus on a few interesting parts of the image. However, if they are asked to memorize a large mural with many points of interest, they must spend much more effort deciding what to focus on. As the number of interesting regions grows, attention also becomes spread more evenly across the image. In this work, we use mathematical tools to show that many modern AI systems behave similarly when processing large images. These systems spend a large amount of computation deciding which regions to focus on, but for sufficiently large images, they often end up distributing attention somewhat evenly across many regions anyway. Motivated by this observation, we design a model that removes much of this costly decision-making process, allowing it to process large images more efficiently. Our experiments show that this intuition leads to an effective and scalable design.