AI and Deep Learning

How to handle KV cache fragmentation when serving long-context models on vLLM?

SC Asked by Scott Ward · 22-04-2025
0 upvotes 17,356 views 0 comments
The question

I am working with the 128k context version of Llama 3, and I’m seeing huge memory waste. Does the PagedAttention in vLLM completely solve the fragmentation issue, or do I still need to manually tune the block size to accommodate these extremely long sequences?

3 answers

0
ME
Answered on 28-04-2025

The core innovation of vLLM is that it treats GPU memory like virtual memory in an OS. This means it allocates "pages" of KV cache only when needed. For 128k context, you don't necessarily need to change the block size (default is 16), but you do need to be careful with your --gpu-memory-utilization setting. If you set it too high, you won't leave enough room for the dynamic allocation of new pages as the conversation grows. I recommend starting at 0.85 and using FP8 quantization for the KV cache itself, which was a feature introduced in the mid-2024 releases to save 50% of cache memory.

0
KE
Answered on 30-04-2025

Are you seeing "Out of Memory" errors during the prefill stage or during the actual token generation phase?

BR 02-05-2025

It usually happens during generation, Kevin. It seems like vLLM handles the initial prompt fine, but as the response gets longer, the system runs out of physical blocks to assign to the active sequences, causing a crash or a slow-down.

0
BR
Answered on 05-05-2025

Try reducing the --max-model-len to only what you actually need. Just because the model supports 128k doesn't mean your app should allow it.

SC 07-05-2025

Smart advice, Brandon. Limiting the context window at the vLLM server level is the most effective way to prevent runaway memory usage while you are still tuning your production hardware.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session