What is hybrid attention?

Introduction

Project introduction

What is hybrid attention?

When a language model writes text, every attention layer keeps a key vector and a value vector for every token it has read, so it never has to recompute them. This store is the KV cache. It grows with every token. Llama 3.1 8B keeps 128 KB per token, which reaches 16 GB at its full context of 131,072 tokens, more than the model's own weights.

Hybrid models keep only a few full attention layers and make the rest cheap. Gemma 3 puts 5 sliding window layers, which only look at the last 1024 tokens, before each full layer. Qwen3-Next and Qwen3.5 put 3 Gated DeltaNet layers, a kind of linear attention that keeps one fixed-size memory instead of a cache, before each full layer.

What do the cheap layers give up?

Most of language is local, since the next word usually depends on the last few. A window layer or a linear attention layer handles that well. What they lose is exact recall of something from far back, such as a name or a number mentioned thousands of tokens ago. A window cannot see it, and a fixed-size memory has to squeeze every token into the same small matrix.

Research on this tradeoff, such as Zoology and Based from Arora and colleagues, found that a model's ability to recall facts from its context tracks how much memory it keeps. A few full attention layers bring exact recall back while the cheap layers do the rest. In this project you build each kind of layer, plug it into small trained models, and measure both sides, what they recall and how many bytes they keep.

Cache as the text grows

2,048 tokens read
FFFFFFFF
64.0 MB

all full: can recall far back

SSSSSSSS
32.0 MB

all window: loses facts from far back

LLLLLLLL
2.0 MB

all linear: loses facts from far back

SSSFSSSF
40.0 MB

hybrid: can recall far back

F full attention · S sliding window of 1024 · L linear attention · 8 layers, 8 key/value heads of 128, 16-bit

What you will build

01

Share and limit

Grouped-query attention and a sliding window mask, and the cache each one saves.

02

A fixed-size memory

Linear attention as a running state, and the gated delta rule that overwrites it.

03

Count the memory

Exact cache sizes for any mix of layers, from a tiny model to Qwen3.5.

Start with a sliding window

Build the mask that lets each token see only its most recent neighbors.

Continue to Problem 1