Next-Latent Prediction: Autoregression in Latent Space

Predict the next continuous latent instead of the next token: build autoregression in latent space from scratch and see why the head makes or breaks it.

Cover Image for Next-Latent Prediction: Autoregression in Latent Space

A language model generates text one token at a time, even when several tokens belong to a single word or idea. You might instead want it to work with a compact representation of a chunk, then decode that representation into text. Next-latent prediction explores this possibility by making the next step a continuous vector. It connects to JEPA, which predicts representations, while keeping the step-by-step generation of an autoregressive model.

Predicting a vector still has to preserve the possible next words. An MSE head collapses all probability onto one successor. The codebook head matches the true three-way branching at 1.09 nats; diffusion recovers branching with added noise. We build and compare the heads on a small artificial language.

Let's Build It

Click the badge to open the full, runnable notebook in Google Colab:

Open In Colab

To study the idea without a billion-parameter model, we need a corpus with two properties: it should have real structure worth compressing into latents, and its continuations should be multimodal so the choice of head matters. A small dictionary of "words" over a Markov chain gives us both. Each word is a fixed template of K = 4 tokens over a 16-symbol vocabulary, and sentences are sequences of words drawn from an order-1 chain whose transitions are deliberately spread across a few near-equiprobable successors.

def make_words(M, K, V, seed=0):           # M distinct K-token "words"
    rng = np.random.default_rng(seed)
    words, seen = [], set()
    while len(words) < M:
        w = tuple(rng.integers(0, V, K).tolist())
        if w not in seen:
            seen.add(w); words.append(w)
    return np.array(words)

def make_transition(M, fanout=3, seed=1):  # each word -> ~3 near-equiprobable successors
    rng = np.random.default_rng(seed)
    T = np.zeros((M, M))
    for i in range(M):
        succ = rng.choice(M, size=fanout, replace=False)
        p = rng.uniform(0.8, 1.2, fanout)
        T[i, succ] = p / p.sum()
    return T

With M = 8 words of K = 4 tokens each, a 16-word sentence is 64 tokens long. The whole point of working in latent space is to autoregress over something shorter, so a small autoencoder learns to squeeze each 4-token word into one latent vector and to reconstruct it.

class WordAutoencoder(nn.Module):
    def encode(self, tok):     # (B, K) tokens -> (B, d) latent
        return self.enc(self.embed(tok).flatten(1))
    def decode(self, z):       # (B, d) latent -> (B, K, V) token logits
        return self.dec(z).view(-1, self.K, self.V)

Once trained, the autoencoder reconstructs every word with 100% token accuracy, so the latents are lossless: nothing about the compression itself limits what follows. Encoding turns a 64-token stream into a 16-latent stream. That factor of 4 is the compression the method is named for, and it is where the autoregression will happen.

Encoding each four-token word into one latent turns a 64-token stream into a 16-latent stream, a four-fold shorter sequence to autoregress over.

Now the model itself: a small causal Transformer over the latent stream, predicting the next latent from all the ones before it. The architecture is unremarkable. The head is where the design decision lives, and we build three.

if model.head == "vq":        # classify the next word over the frozen codebook
    loss = F.cross_entropy(model.out(h).reshape(-1, M), next_word_id.reshape(-1))
elif model.head == "mse":     # regress the next latent vector directly
    loss = F.mse_loss(model.out(h), next_latent)
else:                         # diffusion: denoise the next latent, conditioned on context
    loss = F.mse_loss(model.denoiser(noised_next, context, t), noise)

The MSE head is the obvious first thing to try: the target is a vector, so regress it. The VQ head treats the eight per-word latents as a codebook and classifies which one comes next, which quietly brings a softmax and a cross-entropy back. The diffusion head models the conditional distribution of the next latent by learning to denoise it, conditioned on the Transformer's context vector. Training all three on the same latent stream takes a couple of minutes on a laptop CPU.

What Just Happened?

The corpus has a fixed amount of irreducible uncertainty. Given the current word, the next one is drawn from about three successors, which works out to a conditional entropy of H = 1.09 nats, a perplexity of roughly 3 out of the 8 words. A model that has learned the chain should reproduce that spread. A model that collapses will not.

The head decides everything

Plotting the predicted next latents against the eight word codes tells the whole story at a glance. The VQ head's predictions land exactly on the codes, because it can only ever pick one. The MSE head's predictions land in the empty space between the codes.

In latent space, VQ predictions land on the real word codes while MSE predictions fall into the void between them, because MSE regresses the mean of the plausible successors.

This is not a bug in the training. It is the correct answer to the wrong question. When a word has three plausible successors sitting at three different points in latent space, the single vector that minimises mean-squared error to all of them is their average, and that average is a point that corresponds to no word at all. Decode it and you get garbage. The regression head is doing exactly what it was asked to do, and what it was asked to do was incoherent for a multimodal target.

The effect on the actual next-word distribution is stark. After a given word, the true chain spreads its mass over three successors. The VQ head matches that spread almost exactly. The diffusion head roughly matches it with some noise. The MSE head puts all of its mass on a single word.

After one source word the true chain has three successors; VQ reproduces the spread, diffusion approximates it, and MSE collapses onto a single mode.

Two robust numbers summarise the three heads. The first is how far each predicted latent lands from the nearest real word code, measured as a fraction of the typical spacing between codes: 0 means the prediction sits exactly on a word. The second is the conditional entropy of what the model generates, which should match the true 1.09 nats.

Only the VQ head lands on real word codes; only VQ and diffusion reproduce the true branching entropy, while MSE collapses to zero.

The VQ head lands exactly on codes (distance 0.00) and reproduces the branching entropy to two decimal places (1.09 nats, with a KL of 0.005 to the true transition). The MSE head is the mirror image: its conditional entropy is 0.00, because given a context it is deterministic and cannot represent branching at all, and the single point it predicts sits 0.40 of the way to a neighbouring code, out in the void. The diffusion head recovers the branching (entropy above the true value, 1.44, since it over-disperses), but its samples land off the codes too (0.49), a little noisier than the codebook. The pattern is clean: only the codebook head is both on-word and correctly branching.

Watching the collapse

Rolling the two heads out autoregressively makes the difference physical. Starting from the same word, the VQ head hops from code to code, staying on the manifold of real words and visiting different ones as it samples. The MSE head takes one step, lands in the void between codes, and stays there.

An autoregressive rollout: the VQ head hops between valid word codes while the MSE head drifts into the empty region between them and stops moving.

For reference, a plain next-token Transformer trained on the same corpus reaches 84% next-token accuracy and a perplexity of 1.32, working over the full 64-token sequence rather than the 16-latent one. The point of the comparison is not that latent prediction wins on quality here; at this toy scale it does not need to. The point is that the latent model does the same job over a sequence a quarter as long, and that getting there at all depends entirely on not using the naive head.

Going Deeper

The reason the MSE head fails is worth stating precisely, because it is the same reason that shows up at scale. Regression with a squared-error loss learns the conditional mean E[z_next | context]. For a unimodal target that mean is a fine prediction. For a multimodal target the mean sits between the modes, in a region the data never visits. This is the identical failure that makes an image model trained with pixel MSE produce blurry averages, and it is why the literature on continuous autoregression reaches for distributional heads: a codebook turns the problem back into classification, a diffusion or flow head models the density directly, and a mixture head predicts several modes at once.

The codebook route is the one that works cleanest here, and it is worth noticing what it costs. By quantising the latent back down to one of eight discrete codes, the VQ head reintroduces the very softmax-over-a-vocabulary that motivated leaving token space in the first place. That is the honest tension in this whole idea. A purely continuous next-latent model is elegant but hard to train, and the fixes tend to smuggle some discreteness back in. Diffusion heads avoid the codebook but pay in sampling steps and in the kind of off-manifold noise visible in the scorecard.

What the toy leaves out

This corpus is chosen to make the mechanism visible, not to be realistic, and its conveniences are exactly the hard parts of the real problem. The words form a tiny closed dictionary, so a codebook of eight entries is enough and the autoencoder is lossless. On natural data there is no clean dictionary, the encoder loses information, and the number of "codes" needed explodes. The chain is order-1, so context is trivial; real latent prediction has to carry long-range dependencies through the latent stream. And because we teacher-force with the true latents during training, the rollout never has to recover from its own mistakes, which sidesteps the exposure-bias problem that makes long continuous rollouts drift. The toy proves that latent-space autoregression is coherent and that the head is the crux. It does not prove that it scales, which is exactly the open question the research is chasing.

Where This Comes From

Next-latent prediction is best read as one point on a line that runs from discrete tokens toward reasoning in a continuous space.

Next-latent prediction sits between next-token generation and JEPA on a line from discrete tokens toward reasoning in latent space.

Next-token prediction with a softmax is the workhorse every LLM is built on. JEPA (Assran and colleagues, 2023), following LeCun's 2022 "path towards autonomous machine intelligence", predicts a latent representation rather than raw input, but not autoregressively. Coconut (Hao and colleagues, 2024) trains a model to reason in a continuous latent space by feeding its last hidden state back as the next input embedding. The codebook head borrows directly from VQ-VAE (van den Oord and colleagues, 2017), and the diffusion head from the same denoising-diffusion machinery behind diffusion-based decoding. The common thread is a move away from the discrete token as the fixed atom of computation, and the common obstacle is the one this post makes concrete: continuous targets need a head that can represent a distribution.

Interactive Tools

  • Markov Chain Simulator: The corpus here is a Markov chain over words; watch how transition probabilities shape the sequences a model has to learn to reproduce.
  • Regression Playground: Fit squared-error regression and see it average conflicting targets, the exact mechanism that collapses the MSE head on multimodal data.

Stay in the loop

Get notified when new tutorials and deep-dives are published. No spam, unsubscribe anytime.

Related Posts

Frequently Asked Questions