The file concerns long context inference

The file concerns long context inference
4 min read

A language model can accept a long context without processing it effectively or exploiting it reliably. This is the starting point of an article on the Machine Learning Mastery site dedicated to long context inference, that is, generating responses from very long texts. According to the author, two questions arise separately: can the execution engine process and retain the tokens within its memory and latency budget, and can the model find and use the relevant information among them? A larger context window alone does not answer either of these questions.

The first cost appears during the prefill phase, or prefill, during which the model reads the entire prompt. For a prompt of N tokens, the attention calculation covers a triangular part of an N × N matrix, so this work grows approximately with N². However, tiled computation kernels like FlashAttention avoid storing the full matrix. During generation, each new token queries the cache KV, the memory that retains the keys and values of tokens already processed, and this work grows roughly linearly with the sequence length.

The cost of a long context

The size of this cache can be estimated by the formula MKV = 2 × L × HKV × D × N × b, where L is the number of layers, HKV the number of key-value heads, D the dimension of the heads, N the number of tokens, and b the number of bytes per element. The factor two corresponds to keys and values. This is an estimate: it does not cover the temporary memory of attention kernels, and it assumes each layer retains every token. For a batch of requests, the author recommends calculating separately the retained length of each, then adding block rounding, allocator margin, weights, and temporary workspace.

Splitting prefill and managing the cache

Chunked prefill (chunked prefill) consists of not processing the entire prompt in a single iteration but dividing it into smaller token ranges while maintaining a single logical prefill and a growing cache. Each chunk relies on the cache produced by the previous ones. This technique changes the scheduling and the intermediate peak workload, not the model context. It allows interleaving a long prefill with generation tasks of other requests, which reduces queue blocking and can improve GPU utilization, at the cost of a higher number of scheduling steps and possibly a kernel launch overhead.

The article provides an example on a small GPT-2 model initialized without downloading weights: a 48-token prompt processed in one block then split into 12-token chunks yields, according to the test, the same result as full processing, with minor numerical differences. It specifies this is a check of the model’s computation and not a server scheduler.

On the vLLM engine side, the example explicitly activates chunked prefill with the Qwen/Qwen2.5-0.5B-Instruct model, a maximum length of max_model_len=8192, and a budget of max_num_batched_tokens=512. The latter parameter is a scheduling budget, not the model’s context length: a smaller budget leaves more room for generation requests, while a larger budget can increase prefill throughput but degrade token latency for other requests. According to the article, current vLLM v1 versions enable this option by default when the model and backend support it. Finally, it is the tokenizer, not the number of characters or words, that determines if the request fits within max_model_len. For long-term retention, the author reviews several methods: sliding window attention, mixed attention, “attention sink” streams (attention sink), cache eviction, quantization, and offloading to CPU memory. The attention sink refers to a behavior observed in many models that concentrate significant attention on the first tokens. Keeping them with a sliding window of recent tokens bounds memory, but the model can no longer retrieve the content of the evicted middle. These methods are not interchangeable, and offloading is only useful if transfer time meets the targeted latency.

Choosing the useful context

“The least costly token is often the one you don’t send,” the article summarizes. A long context is justified when the task requires broad access to the source and the model can exploit it. Otherwise, searching for relevant passages, summarizing, prompt compression, or hierarchical processing by sections can reduce prefill time and cache memory. Each choice carries risks: search may miss useful elements, summarization may discard details, and a full context may drown the answer among distractors.

The author also emphasizes that the position of information matters. A model with a nominally long window may perform worse when the relevant element is in the middle of the text. He therefore recommends evaluating the positions and types of distractors encountered in the real application.

S’abonner
Notification pour
guest

0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
0
Nous aimerions avoir votre avis, veuillez laisser un commentaire.x