You could have invented transformers from memory
Large language models today possess an extraordinary ability to recall obscure facts from various corners of the world. This was demonstrated when the author, short on time, asked the language model Astra to remind them of the rules of a board game. The model provided a detailed explanation, enabling the author to quickly recall and play the game.
This widespread access to knowledge often leads us to underestimate the remarkable capabilities of language models. However, understanding how these models manage to retain such vast amounts of information requires a closer look at the underlying mechanisms.
In this post, the author explores the concept of memory stores by examining linear maps, multi-layer perceptrons (MLPs), and self-attention mechanisms. The key factor that determines the effectiveness of a memory store is its ability to accurately recall the information it has stored. As the number of stored memories increases, they begin to interfere with each other, making recall more challenging.
The author introduces the interference-to-signal ratio as a metric that measures the extent to which other memories hinder the retrieval of a specific one.
By analyzing the number of memories a store can hold before interference outweighs the signal, the author defines capacity as the upper limit. The post progresses from a basic linear layer to an MLP and ultimately to self-attention mechanisms, each demonstrating improved capacity and recall capabilities. By employing 64-dimensional keys, the capacity expands from approximately 64 memories to around 2,700, and then surpasses one million.
To delve deeper into the subject, the author defines a memory store as a function, f, which holds M memories, each represented by a key-value pair. Within this framework, a memory consists of a key, k_i (a vector in a d_k-dimensional real space), utilized for lookup based on similarity, and a value, v_i (another vector in a d_v-dimensional real space).
To utilize the memory store, an input x is provided, and the store recalls an output, f(x). A high-quality memory store is capable of retrieving v_i when queried with k_i or a vector resembling k_i. This type of memory store is termed an associative memory.
The foundational memory store discussed in the post is linear. It achieves this by accumulating memories through outer products, with the rows of matrices K ∈ ℝ^(M×d_k) and V ∈ ℝ^(M×d_v) representing the keys k_i^T and the values v_i^T, respectively. To incorporate a new memory (k, v), the corresponding weights are updated by adding vk^T to the existing values.
In the context of a network with a scalar loss L, a linear layer y = Wx undergoes a gradient descent update, wherein the gradient with respect to W is represented as the product of the gradient of the loss (∇L(y)) and the input, x^T. This update mechanism can be viewed as performing attention over all training inputs, as demonstrated by Irie, Csordás, and Schmidhuber.
Recalling information from the linear memory store is straightforward: by multiplying the input by the matrix W, the recall process involves comparing the query with every stored key and returning the sum of the values, weighted by the similarity between the query and each key. To elaborate, if every key is normalized to have a length of 1, the recall yields the value of the target memory whose key matches the query, along with interference from the remaining memories whose keys are similar to the query.
In simpler terms, an orthonormal set of keys would result in perfect recall, as there would be no interference. However, since the d_k-dimensional real space can accommodate only d_k orthogonal directions, storing more memories than the key dimension (M > d_k) inevitably leads to interference. The capacity of the linear memory store is determined by this interference-to-signal ratio, which reaches its limit once the number of stored memories equals the key dimension.
Rewritten by urgent.news; may contain errors — check the original.
Source: alexwang.ai