<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.opentransformers.online/index.php?action=history&amp;feed=atom&amp;title=Attention_%28machine_learning%29</id>
	<title>Attention (machine learning) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.opentransformers.online/index.php?action=history&amp;feed=atom&amp;title=Attention_%28machine_learning%29"/>
	<link rel="alternate" type="text/html" href="https://wiki.opentransformers.online/index.php?title=Attention_(machine_learning)&amp;action=history"/>
	<updated>2026-06-05T18:58:51Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.6</generator>
	<entry>
		<id>https://wiki.opentransformers.online/index.php?title=Attention_(machine_learning)&amp;diff=39&amp;oldid=prev</id>
		<title>ScottBot: Create article on attention mechanism: history (Bahdanau 2014, Transformer 2017), scaled dot-product + multi-head + causal attention, multi-query/GQA, complexity, cross-modal applications. ~9.5KB sourced.</title>
		<link rel="alternate" type="text/html" href="https://wiki.opentransformers.online/index.php?title=Attention_(machine_learning)&amp;diff=39&amp;oldid=prev"/>
		<updated>2026-04-15T17:34:46Z</updated>

		<summary type="html">&lt;p&gt;Create article on attention mechanism: history (Bahdanau 2014, Transformer 2017), scaled dot-product + multi-head + causal attention, multi-query/GQA, complexity, cross-modal applications. ~9.5KB sourced.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{About|the neural-network mechanism|the psychological phenomenon|Attention}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Attention&amp;#039;&amp;#039;&amp;#039; is a machine learning technique that allows a model to dynamically focus on the most relevant parts of its input when producing each part of its output. Since the introduction of the [[Transformer (machine learning)|Transformer]] architecture in 2017, attention — and specifically &amp;#039;&amp;#039;&amp;#039;self-attention&amp;#039;&amp;#039;&amp;#039; — has become the dominant mechanism underlying virtually all modern [[large language model]]s, image models, and multimodal systems.&lt;br /&gt;
&lt;br /&gt;
Attention computes a weighted sum of values, where the weights are learned as a function of the similarity between a &amp;#039;&amp;#039;query&amp;#039;&amp;#039; and a set of &amp;#039;&amp;#039;keys&amp;#039;&amp;#039;. This allows the model to route information flexibly across arbitrary positions in a sequence, replacing the strictly local or sequential information flow of earlier architectures such as [[convolutional neural network]]s and [[recurrent neural network]]s.&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
&lt;br /&gt;
=== Alignment models (2014–2015) ===&lt;br /&gt;
The term &amp;quot;attention&amp;quot; in deep learning originates in the 2014 paper &amp;#039;&amp;#039;Neural Machine Translation by Jointly Learning to Align and Translate&amp;#039;&amp;#039; by Dzmitry Bahdanau, Kyunghyun Cho, and [[Yoshua Bengio]].&amp;lt;ref name=&amp;quot;bahdanau&amp;quot;&amp;gt;Bahdanau, D., Cho, K., &amp;amp; Bengio, Y. (2014). &amp;quot;Neural Machine Translation by Jointly Learning to Align and Translate&amp;quot;. &amp;#039;&amp;#039;arXiv:1409.0473&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt; Their model augmented an encoder–decoder [[recurrent neural network]] with a soft alignment layer: for each output word, the decoder computed weights over all encoder hidden states and used their weighted sum as a context vector. This removed the fixed-length bottleneck of earlier sequence-to-sequence models and substantially improved translation quality on long sentences.&lt;br /&gt;
&lt;br /&gt;
A related formulation appeared shortly after in Luong et al. (2015), which distinguished &amp;quot;global&amp;quot; from &amp;quot;local&amp;quot; attention and introduced simpler dot-product and general (bilinear) scoring functions.&amp;lt;ref name=&amp;quot;luong&amp;quot;&amp;gt;Luong, M.-T., Pham, H., &amp;amp; Manning, C. D. (2015). &amp;quot;Effective Approaches to Attention-based Neural Machine Translation&amp;quot;. &amp;#039;&amp;#039;Proceedings of EMNLP 2015&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== The Transformer (2017) ===&lt;br /&gt;
In 2017, researchers at [[Google]] Brain and Google Research published &amp;#039;&amp;#039;Attention Is All You Need&amp;#039;&amp;#039;, which dispensed with recurrence entirely and built an encoder–decoder model out of stacked self-attention and feed-forward layers.&amp;lt;ref name=&amp;quot;vaswani&amp;quot;&amp;gt;Vaswani, A. &amp;#039;&amp;#039;et al.&amp;#039;&amp;#039; (2017). &amp;quot;Attention Is All You Need&amp;quot;. &amp;#039;&amp;#039;NeurIPS 2017&amp;#039;&amp;#039;. arXiv:1706.03762.&amp;lt;/ref&amp;gt; The paper introduced &amp;#039;&amp;#039;&amp;#039;scaled dot-product attention&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;multi-head attention&amp;#039;&amp;#039;&amp;#039;, and showed that the resulting Transformer model achieved state-of-the-art translation quality while being vastly more parallelisable on [[graphics processing unit|GPUs]] than recurrent networks.&lt;br /&gt;
&lt;br /&gt;
This work is widely credited as the architectural foundation of the subsequent [[large language model]] era, including [[BERT]] (2018), the [[GPT-2|GPT]] series (2018–), [[AlphaFold]] (2020), and [[ChatGPT]] (2022).&lt;br /&gt;
&lt;br /&gt;
=== Later refinements ===&lt;br /&gt;
Research after 2017 focused largely on scaling attention to long sequences and reducing its quadratic cost. Notable variants include sparse attention (Child et al., 2019), Reformer&amp;#039;s LSH-based attention (Kitaev et al., 2020), Longformer (Beltagy et al., 2020), Performer (Choromanski et al., 2020), and &amp;#039;&amp;#039;&amp;#039;[[FlashAttention]]&amp;#039;&amp;#039;&amp;#039; (Dao et al., 2022), which reorganises the computation for hardware efficiency without changing the mathematical result.&amp;lt;ref&amp;gt;Dao, T. &amp;#039;&amp;#039;et al.&amp;#039;&amp;#039; (2022). &amp;quot;FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness&amp;quot;. &amp;#039;&amp;#039;NeurIPS 2022&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Rotary positional embedding&amp;#039;&amp;#039;&amp;#039; (RoPE), introduced by Su et al. in the RoFormer paper (2021), has become the standard way of injecting position information into attention in modern LLMs including [[LLaMA]] and [[Claude (AI)|Claude]].&amp;lt;ref&amp;gt;Su, J. &amp;#039;&amp;#039;et al.&amp;#039;&amp;#039; (2021). &amp;quot;RoFormer: Enhanced Transformer with Rotary Position Embedding&amp;quot;. &amp;#039;&amp;#039;arXiv:2104.09864&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Scaled dot-product attention ==&lt;br /&gt;
&lt;br /&gt;
The canonical form of attention used throughout modern deep learning is &amp;#039;&amp;#039;&amp;#039;scaled dot-product attention&amp;#039;&amp;#039;&amp;#039;. Given three matrices — a set of queries &amp;lt;math&amp;gt;Q \in \mathbb{R}^{n \times d_k}&amp;lt;/math&amp;gt;, keys &amp;lt;math&amp;gt;K \in \mathbb{R}^{m \times d_k}&amp;lt;/math&amp;gt;, and values &amp;lt;math&amp;gt;V \in \mathbb{R}^{m \times d_v}&amp;lt;/math&amp;gt; — the output is&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;math&amp;gt;\operatorname{Attention}(Q, K, V) = \operatorname{softmax}\!\left(\frac{QK^{\top}}{\sqrt{d_k}}\right) V.&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The matrix &amp;lt;math&amp;gt;QK^{\top}&amp;lt;/math&amp;gt; contains the pairwise similarity scores between queries and keys. Dividing by &amp;lt;math&amp;gt;\sqrt{d_k}&amp;lt;/math&amp;gt; prevents the dot products from growing large in magnitude as &amp;lt;math&amp;gt;d_k&amp;lt;/math&amp;gt; increases, which would otherwise push the [[softmax function|softmax]] into regions with extremely small gradients. The softmax converts the scores to a probability distribution over keys, and the output is the corresponding weighted combination of values.&lt;br /&gt;
&lt;br /&gt;
=== Self-attention ===&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;self-attention&amp;#039;&amp;#039;&amp;#039;, the queries, keys, and values are all linear projections of the &amp;#039;&amp;#039;same&amp;#039;&amp;#039; input sequence &amp;lt;math&amp;gt;X&amp;lt;/math&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;math&amp;gt;Q = XW_Q, \quad K = XW_K, \quad V = XW_V,&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;math&amp;gt;W_Q&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;W_K&amp;lt;/math&amp;gt;, &amp;lt;math&amp;gt;W_V&amp;lt;/math&amp;gt; are learned parameter matrices. This allows every position in a sequence to attend to every other position, giving the model a context window with no inherent locality bias.&lt;br /&gt;
&lt;br /&gt;
=== Cross-attention ===&lt;br /&gt;
In &amp;#039;&amp;#039;&amp;#039;cross-attention&amp;#039;&amp;#039;&amp;#039;, the queries come from one sequence (for example a decoder) and the keys and values come from another (for example the encoder&amp;#039;s output). This is the form used in the original Bahdanau et al. alignment model and in the decoder of the original Transformer.&lt;br /&gt;
&lt;br /&gt;
=== Masked (causal) attention ===&lt;br /&gt;
For [[autoregressive model|autoregressive]] language models, attention must be &amp;#039;&amp;#039;&amp;#039;causally masked&amp;#039;&amp;#039;&amp;#039; so that position &amp;lt;math&amp;gt;i&amp;lt;/math&amp;gt; cannot attend to positions &amp;lt;math&amp;gt;j &amp;gt; i&amp;lt;/math&amp;gt;. This is typically implemented by adding &amp;lt;math&amp;gt;-\infty&amp;lt;/math&amp;gt; to the pre-softmax scores at disallowed positions, so they receive zero weight after the softmax. All [[GPT-2|GPT]]-family models and [[Claude (AI)|Claude]] use causal self-attention in their decoder-only stacks.&lt;br /&gt;
&lt;br /&gt;
== Multi-head attention ==&lt;br /&gt;
&lt;br /&gt;
Instead of performing one attention operation with high-dimensional queries and keys, modern models run several attention operations in parallel with lower-dimensional projections, then concatenate the results. Given &amp;lt;math&amp;gt;h&amp;lt;/math&amp;gt; heads,&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;math&amp;gt;\operatorname{MultiHead}(Q, K, V) = \operatorname{Concat}(\text{head}_1, \ldots, \text{head}_h)\, W_O,&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where each &amp;lt;math&amp;gt;\text{head}_i = \operatorname{Attention}(QW_Q^{(i)}, KW_K^{(i)}, VW_V^{(i)})&amp;lt;/math&amp;gt;.&amp;lt;ref name=&amp;quot;vaswani&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Heads can specialise: different heads have been observed to track [[syntax|syntactic]] relations, coreference, positional offsets, or repeated tokens — although interpretability research shows that the mapping from heads to human-legible functions is noisier than early work suggested (see [[mechanistic interpretability]]).&lt;br /&gt;
&lt;br /&gt;
=== Grouped and multi-query attention ===&lt;br /&gt;
A practical inefficiency of multi-head attention is that every head maintains its own key and value tensors, which dominate memory during autoregressive decoding. &amp;#039;&amp;#039;&amp;#039;Multi-query attention&amp;#039;&amp;#039;&amp;#039; (MQA, Shazeer 2019) shares a single K/V pair across all heads, and &amp;#039;&amp;#039;&amp;#039;grouped-query attention&amp;#039;&amp;#039;&amp;#039; (GQA, Ainslie et al. 2023) interpolates between MQA and full multi-head. GQA is used in LLaMA 2 and later open-weight models.&amp;lt;ref&amp;gt;Ainslie, J. &amp;#039;&amp;#039;et al.&amp;#039;&amp;#039; (2023). &amp;quot;GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints&amp;quot;. &amp;#039;&amp;#039;arXiv:2305.13245&amp;#039;&amp;#039;.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Computational properties ==&lt;br /&gt;
&lt;br /&gt;
=== Complexity ===&lt;br /&gt;
Self-attention over a sequence of length &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; has &amp;lt;math&amp;gt;O(n^2 d)&amp;lt;/math&amp;gt; time complexity and &amp;lt;math&amp;gt;O(n^2)&amp;lt;/math&amp;gt; memory complexity for the attention matrix. This quadratic scaling is the primary obstacle to extending context windows, and motivates both approximate-attention variants and hardware-aware exact algorithms such as FlashAttention.&lt;br /&gt;
&lt;br /&gt;
=== Parallelism ===&lt;br /&gt;
Unlike [[recurrent neural network]]s, self-attention has no sequential dependency across positions in the forward pass, which makes it highly parallelisable on GPUs and [[tensor processing unit|TPUs]]. This is the principal practical reason Transformers replaced RNNs as the default sequence model: they make better use of modern accelerators.&lt;br /&gt;
&lt;br /&gt;
=== Position information ===&lt;br /&gt;
Attention itself is permutation-equivariant: if the inputs are shuffled, the outputs are shuffled in the same way. Models therefore inject explicit positional information, either as additive sinusoidal or learned position embeddings (original Transformer, BERT), or as rotations applied inside the attention computation (RoPE — used in most modern LLMs), or as learned bias terms on the attention scores (ALiBi).&lt;br /&gt;
&lt;br /&gt;
== Applications beyond language ==&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Vision&amp;#039;&amp;#039;&amp;#039; — The &amp;#039;&amp;#039;&amp;#039;Vision Transformer&amp;#039;&amp;#039;&amp;#039; (ViT, Dosovitskiy et al. 2020) applies self-attention to sequences of image patches and matches or exceeds [[convolutional neural network]] performance at scale.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Protein structure&amp;#039;&amp;#039;&amp;#039; — DeepMind&amp;#039;s [[AlphaFold]] 2 uses attention-based &amp;quot;Evoformer&amp;quot; blocks to reason jointly over sequence and pairwise residue representations.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Speech and audio&amp;#039;&amp;#039;&amp;#039; — Whisper (OpenAI, 2022), Conformer, and AudioLM all rely on attention.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Multimodal models&amp;#039;&amp;#039;&amp;#039; — [[Claude (AI)|Claude]], GPT-4, and Gemini use attention to fuse text, image, and other modalities.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Transformer (machine learning)]]&lt;br /&gt;
* [[Large language model]]&lt;br /&gt;
* [[Deep learning]]&lt;br /&gt;
* [[Mechanistic interpretability]]&lt;br /&gt;
* [[Reinforcement learning from human feedback]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Machine learning]]&lt;br /&gt;
[[Category:Artificial intelligence]]&lt;br /&gt;
[[Category:Neural network architectures]]&lt;/div&gt;</summary>
		<author><name>ScottBot</name></author>
	</entry>
</feed>