It’s mentioned in the paper that: By splitting a prompt of length L into N chunks, each of size K, where N = L/K, the overall memory complexity of prefilling is reduced from O(L²) to O(LK).
Chunking and Segmenting the Input: When dealing with very long contexts (thousands of tokens), instead of processing the entire input in one pass, the context is divided into smaller segments (chunks). For each chunk, the model computes the key-value (KV) pairs, caches them, and moves on to the next chunk. This process is called chunked prefill.
Quadratic Memory Requirement (in attention mechanism)
For each token in the sequence:
With chunking: By splitting the context into chunks of size K, the memory complexity is reduced to O(LK) (linear with respect to the input length and chunk size), meaning that only the most recent chunk requires full memory resources during computation.
$\text{Memory Complexity} = \frac{L}{K} \times O(K^2) = O(LK)$
where, number of chunks = $\frac{L}{K}$, chunk size (in tokens) = $K$
Processing each chunk should still involve a quadratic complexity for that chunk. If we look at each chunk individually:
However, the reduction in overall complexity comes from the fact that chunking allows us to handle the context in a piecewise manner, reducing the peak memory usage at any one time.
For $N$ chunks, each of size $K$, the memory complexity for that chunk is $O(K^2)$.
When processing sequentially, the complexity per chunk is $O(K^2)$, but crucially, the total complexity is governed by the cumulative operations across all chunks:
$\text{Total Memory Complexity} = N \times O(K^2)= \frac{L}{K} \times O(K^2) = O(LK)$
Thus, while each chunk has a quadratic complexity within itself, the total memory usage across the entire sequence is reduced due to the smaller chunk sizes, which is where the reduction to O(LK) comes from.