Search

Search papers and lessons

HiPPO Matrix

Level 4 · 12 min read

The HiPPO matrix is a fixed square matrix chosen so that a linear state keeps a compressed record of the whole input history, which lets state space models remember long sequences.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Discrete math
  • Architectures
  • Abstract algebra
  • ReadReady to readNot yet

TL;DR

The HiPPO matrix is a fixed square table of numbers that you plug into a state space model as its state matrix. Its entries are chosen so that the state, a list of numbers that changes over time, keeps a compressed summary of the whole input history, not just the last few inputs. Plain state space models forget long ago inputs very fast; this matrix is the main fix that made them remember long sequences.

Builds on: State Space Model, Orthogonal Projection, Legendre Polynomials · Leads to: S4

Where it's used

Initialising the state matrix of S4. The S4 model (the S4 paper) is a deep sequence model whose core layer is a state space model. Instead of starting its state matrix at random, S4 starts it at the HiPPO matrix. The S4 paper runs an ablation on CIFAR-10 that compares initialisations, and reports that the HiPPO initialisation gives validation accuracies more than 15 percent higher than a random one once the matrix is trained.

The earlier LSSL result. Before S4, the LSSL model used the same idea. The S4 paper reports that changing a random state matrix into the HiPPO matrix raised the LSSL's accuracy on sequential MNIST, where an image is read one pixel at a time, from 60% to 98%. That single change is the clearest early sign that the matrix does more than a random one.

Long sequences in general. Any task where the answer depends on something seen thousands of steps earlier, such as audio, long documents read one token at a time, or sensor logs, needs a model whose state holds old information. The HiPPO matrix is the piece that gives a linear state that property. Some S4 models use a state size of N = 256, so the matrix in those cases is 256 by 256.

How it works

Take a tiny instance first: a state with two numbers, and a 2 by 2 HiPPO matrix. Switch the input off so you can watch the state move by itself.

  1. Set up. The state is x = (x0, x1). The matrix A is 2 by 2, so the state follows a linear rule, x' = A x, which is the part of a state space model without the input term (see State Space Model).

  2. Fill the matrix. The diagonal entries are the first thing to set. Entry A00 = -1 and entry A11 = -2, because the diagonal rule is minus (index + 1). The entry below the diagonal, A10, is -√(3 · 1) = -√3, because the rule for a below-diagonal entry is minus the square root of (2n+1)(2k+1) with n = 1 and k = 0. The entry above the diagonal, A01, is 0. Check: (2·1+1)(2·0+1) = 3 · 1 = 3, and the square root of 3 is about 1.73.

  3. Write the matrix down. A = [[-1, 0], [-√3, -2]]. Every entry above the diagonal is zero, so the matrix is lower triangular: the first row of A involves only x0, while the second row involves both x0 and x1.

  4. Apply it to a state. Let x = (1, 1). Then A x = (-1 · 1 + 0 · 1, -√3 · 1 + (-2) · 1) = (-1, -2 - √3). Numerically, that is about (-1, -3.73). These are the rates of change of x0 and x1 at this moment. Check: the first rate depends only on x0, so it is -1 times x0 = -1. The second rate is -√3 times x0 plus -2 times x1.

  5. Read what the matrix does. Coordinate 0 changes only because of itself, so it does not hear from coordinate 1. Coordinate 1 changes because of both. Information flows one way, from the lower index to the higher one. This is the picture below.

flowchart LR
  x0["x0: changes by itself at rate -1"] -->|"feeds via -√3"| x1["x1: changes at rate -2 and from x0"]
  x1 -.->|"no feedback: A01 = 0"| x0
  1. Check the stability. A lower triangular matrix has its eigenvalues on the diagonal (see Eigenvalues and Eigenvectors), so the eigenvalues here are -1 and -2. Both are negative, so with the input off, the state shrinks toward zero rather than blowing up. This is what keeps the state bounded while it accumulates history.

  2. Why this is a memory. HiPPO's theory says that when the matrix is paired with a matching input vector B, the state at time t stores the coefficients of a best-fit polynomial approximation of the input history. The polynomials are the Legendre polynomials (see Legendre Polynomials), and the fit is a projection in the sense of Orthogonal Projection. The toy example above does not show that property; it only shows the mechanics of the matrix.

The math

A HiPPO matrix of size N by N has entries (indices run from 0 to N-1)

Ank=−{(2n+1)(2k+1)if n>kn+1if n=k0if n<kA_{nk} = -\begin{cases} \sqrt{(2n+1)(2k+1)} & \text{if } n \gt k \\ n+1 & \text{if } n = k \\ 0 & \text{if } n \lt k \end{cases}

Here n is the row index and k is the column index. The minus sign in front makes every nonzero entry negative.

Step 1 in symbols: the diagonal

For n = k the entry is -(n+1). So A00 = -1, A11 = -2, A22 = -3, and so on. The diagonal entries are the negatives of the counting numbers 1, 2, 3, ....

Step 2 in symbols: below the diagonal

For n greater than k, the entry is -√((2n+1)(2k+1)). The product (2n+1)(2k+1) is the product of two odd numbers, so it is always odd. For N = 3:

  • A10 = -√(3 · 1) = -√3 ≈ -1.732
  • A20 = -√(5 · 1) = -√5 ≈ -2.236
  • A21 = -√(5 · 3) = -√15 ≈ -3.873

Step 3 in symbols: above the diagonal

For n less than k the entry is 0. For N = 3, the entries A01, A02 and A12 are all zero. So for N = 3 the matrix is

A=(−100−3−20−5−15−3)A = \begin{pmatrix} -1 & 0 & 0 \\ -\sqrt{3} & -2 & 0 \\ -\sqrt{5} & -\sqrt{15} & -3 \end{pmatrix}

Step 4 in symbols: applying it to a state

Let x be the state vector with entries x_0, ..., x_{N-1}. The n-th entry of A x is a sum over the columns k that are at or before n:

(Ax)n=∑k=0nAnk xk(\mathbf{A}\mathbf{x})_n = \sum_{k=0}^{n} A_{nk}\, x_k

Because the entries after the diagonal are zero, the sum stops at k = n. This is the formal version of "coordinate n hears from coordinates up to n."

Step 5 in symbols: the eigenvalues

A lower triangular matrix has its eigenvalues equal to its diagonal entries, so the eigenvalues of this A are -1, -2, ..., -N. All are negative and distinct, which is why the homogeneous motion decays.

Worked examples

Example
Example 1: the smallest case, N = 1
  1. With N = 1 there is one entry, A00. The rule for diagonal entries gives A00 = -(0+1) = -1.
  2. So the state rule is x' = -x. The state is a single number that shrinks at a rate equal to its own size.
  3. Start from x(0) = 2. The solution of x' = -x is x(t) = 2 e^(-t), using the exponential function.
  4. At t = 1, x(1) = 2 · e^(-1) ≈ 2 · 0.368 = 0.736.
  5. Interpretation: with no input, the one-number state decays by a factor e^(-1) ≈ 0.368 every unit of time. A state that decays like this forgets its past fast, which is the problem HiPPO is designed to fix in larger sizes.
Example
Example 2: the standard case, N = 2
  1. Use the matrix from the walkthrough: A = [[-1, 0], [-√3, -2]].
  2. Take the state x = (1, 1).
  3. Row 0 of A times x: -1 · 1 + 0 · 1 = -1.
  4. Row 1 of A times x: -√3 · 1 + (-2) · 1 = -2 - √3 ≈ -3.732.
  5. So A x = (-1, -3.732).
  6. Interpretation: the rates of change are both negative, so both coordinates are shrinking right now. The second coordinate shrinks faster because it is pushed by the first one as well as by itself.
  7. Check with x = (0, 1): row 0 gives 0, row 1 gives -2. Coordinate 0 has rate 0, so it does not feel coordinate 1 at all.
Example
Example 3: the realistic case, N = 256
  1. Some S4 models use state size N = 256, so the matrix has 256 rows and 256 columns.
  2. The entry in the bottom-left corner is row n = 255, column k = 0. Its value is -√((2·255+1)(2·0+1)) = -√(511 · 1) = -√511 ≈ -22.6.
  3. The entry in the bottom-right corner, row 255 and column 255, is the diagonal entry -(255+1) = -256.
  4. The top-left entry is A00 = -1, and the top-right entry, row 0 and column 255, is 0.
  5. Count the nonzero entries. The entries on or below the diagonal are the nonzero ones. There are 256 · 257 / 2 = 32,896 of them, out of 256 · 256 = 65,536 in total, so about half the matrix is nonzero.
  6. Interpretation: the matrix is far from symmetric, and its diagonal alone spans a wide range, from -1 to -256. Entries that large and that unequal are one reason the S4 paper treats the matrix's structure with care.
  7. Try it: for N = 3, what is the entry A20? Answer: it is the row 2, column 0 entry, so it equals -√((2·2+1)(2·0+1)) = -√5 ≈ -2.236.

Common mistakes and tradeoffs

  • It is tempting to think the matrix is symmetric, like many matrices you have seen. In fact the HiPPO matrix is not symmetric, because its entries below the diagonal are nonzero and its entries above the diagonal are zero. The S4 paper notes that it is not a normal matrix. This matters: a non-normal matrix can behave differently from the symmetric matrices that most textbooks start with.
  • It is tempting to think the matrix is learned or random. In fact it is written down by a formula that comes from the Legendre polynomials. Nothing is trained. In S4 the matrix is used as a starting point, and the other parameters are then learned.
  • It is tempting to think a bigger N always helps. In fact a larger N means a larger state and more computation, so the choice of N is a tradeoff between memory capacity and cost. Some S4 models use N = 256.
  • When to use it. Use the HiPPO matrix when the model needs a long memory of its input and the input is a sequence of values sampled over time. Start the state matrix from it rather than from random numbers.
  • When not to use it. It is not needed for short inputs where a few recent steps are enough, and it is not a substitute for a model that can attend to any earlier position directly.
  • Alternatives. Random initialisation is simpler but loses the long memory. Other structured matrices, from later S4 variants and from follow-up work, build on the same idea with different polynomial bases.

History

  • Before 2019. Recurrent networks were known to struggle with long-range dependencies. The S4 paper notes that a plain state space model with a random state matrix performs very poorly in practice, and that the problem is tied to gradients that scale badly with sequence length.
  • 2019. Legendre Memory Units (Voelker, Kajić and Eliasmith, NeurIPS 2019) used Legendre polynomials to build a recurrent memory. The S4 paper cites this work among related approaches to long-range memory.
  • 2020. Gu, Dao, Ermon, Rudra and Ré presented HiPPO at NeurIPS 2020. The paper derived a family of matrices, including the one above, from the idea of projecting the input history onto a polynomial basis, and showed that the state can be updated over time.
  • 2021. The LSSL used the HiPPO matrix and improved sequential MNIST from 60% to 98%, as reported in the S4 paper. S4 then used the HiPPO matrix as its initialisation and made the model fast enough to train on long sequences.
  • Why it caught on. The matrix is a single fixed formula that works without tuning, and it gave state space models a clear reason to be used for long sequences.

Recap

  • A state space model keeps a state that changes by a linear rule, and the state matrix decides what the state remembers.
  • A random state matrix forgets old inputs quickly.
  • The HiPPO matrix has a fixed formula: negative diagonal entries from 1 to N, negative entries below the diagonal from the square roots of products of odd numbers, and zeros above.
  • Its eigenvalues are the negatives of 1 to N, so the state shrinks while it accumulates a compressed history.
  • The HiPPO theory says that this state stores the coefficients of a Legendre polynomial fit of the input history.
  • Read next: S4, which uses this matrix as its starting point, and Diagonal Matrix for the diagonalised form that makes S4 fast.

Check yourself

QuestionRecall: what is the entry of the HiPPO matrix at row n = 3, column k = 3?Show answer

It is on the diagonal, so it equals -(n+1) = -(3+1) = -4.

QuestionCompute: for N = 2, what is the rate of change of the state x = (2, 0) under x' = A x with A = [[-1, 0], [-√3, -2]]?Show answer

Row 0: -1 · 2 + 0 · 0 = -2. Row 1: -√3 · 2 + (-2) · 0 = -2√3 ≈ -3.464. So the rates are (-2, -3.464). The second coordinate changes even though it starts at zero, because it is fed by the first coordinate through the below-diagonal entry -√3.

QuestionWhy: the HiPPO matrix is lower triangular. What does that say about which coordinates can influence which?Show answer

In row n, only the columns k at or before n are nonzero, so coordinate n can be changed by coordinates 0 through n. Coordinate n can be changed by coordinates 0 through n, so higher-index coordinates can be influenced by lower-index ones, but not the other way round. Information in the state flows from the earlier coordinates to the later ones, so coordinate 0 is never changed by coordinate 1.

QuestionApplied: a model with state size N = 4 must be initialised. Would you start its state matrix at random, or from the HiPPO matrix, if the task is classifying audio clips of 10 seconds each?Show answer

Start from the HiPPO matrix. A 10 second clip has many samples, and the class may depend on structure that is spread across the whole clip. A random state matrix forgets early inputs fast, so the state at the end would mostly reflect the last few samples. The HiPPO matrix is built so that the state keeps a compressed summary of the whole history, which is what the classifier needs.

Sources