𝗞𝗩 𝗖𝗮𝗰𝗵𝗲 𝗮𝗻𝗱 𝗣𝗮𝗴𝗲𝗱𝗔𝘁𝘁𝗲𝗻𝘁𝗶𝗼𝗻: 𝗪𝗵𝘆 𝗬𝗼𝘂𝗿 𝗟𝗟𝗠 𝗦𝗲𝗿𝘃𝗲𝗿 𝗦𝗹𝗼𝘄𝘀 𝗗𝗼𝘄𝗻

Your LLM server is running slow.

You deployed a 70B model on four A100 GPUs. Everything looks fine at 8 AM. By lunch, latency doubles. You check your memory. Most of it is taken up by "tensor buffers." These are actually cached states from old conversations.

This is the KV cache problem. It is the biggest bottleneck in production LLM serving.

What is the KV cache?

Every transformer model generates tokens one by one. To create a new token, the model needs the Key and Value tensors from all previous tokens. Recomputing these every time is too slow. Instead, the engine stores them. This storage is the KV cache.

The memory problem:

For a Llama 3.1 70B model, a single 4096-token sequence needs about 1.3 GB of memory.

If you have 256 users at once, you need 336 GB of memory. This is more than four A100 GPUs can hold. The KV cache grows so fast that it often uses more memory than the model weights themselves.

Traditional memory management fails because:

  • Internal fragmentation: You allocate space for 4096 tokens but only use 300. You waste 93% of that space.
  • No sharing: Two users with the same system prompt each store their own copy of that prompt.
  • All-or-nothing eviction: When memory runs out, you must move the entire sequence to the CPU. This stalls the GPU.

How PagedAttention fixes this:

PagedAttention works like an operating system. It divides the KV cache into small, fixed-size blocks called pages.

This solves three main issues:

  • On-demand allocation: A sequence only takes up pages as it grows. You do not waste memory on unused capacity.
  • Shared prefix support: Multiple users can share the same physical pages for a common system prompt. This uses "copy-on-write" logic to save massive amounts of memory.
  • Fine-grained eviction: When memory is full, the system moves small pages to the CPU instead of huge sequences.

The Result:

Using PagedAttention (the tech inside vLLM) can increase throughput by 2x to 4x compared to traditional methods.

When to use it:

  • High concurrency.
  • Sequences of different lengths.
  • Prompts that share the same start.

When to skip it:

  • Single-user local inference.
  • Very small models.
  • Tasks where every sequence is exactly the same length.

KV Cache และ PagedAttention: สิ่งที่พวกมันทำและทำไมถึงสำคัญ

ในการใช้งาน Large Language Models (LLMs) อย่าง GPT-4 หรือ Llama 3 สิ่งหนึ่งที่คุณจะสังเกตเห็นคือความเร็วในการสร้างข้อความ (generation speed) มักจะขึ้นอยู่กับความยาวของบริบท (context length) ยิ่งข้อความยาวขึ้น การประมวลผลก็ยิ่งช้าลงและใช้ทรัพยากรมากขึ้น

แต่ทำไมถึงเป็นเช่นนั้น? คำตอบอยู่ที่กลไกที่เรียกว่า Attention และวิธีที่เราจัดการกับข้อมูลที่เกิดขึ้นระหว่างการประมวลผล นั่นคือ KV Cache และ PagedAttention

KV Cache คืออะไร?

เพื่อให้เข้าใจ KV Cache เราต้องเข้าใจก่อนว่า LLMs ทำงานอย่างไร ในขั้นตอนการสร้างข้อความ (inference) โมเดลจะทำนายโทเคน (token) ทีละตัว โดยอาศัยข้อมูลจากโทเคนก่อนหน้าทั้งหมด

ในกลไก Attention, แต่ละโทเคนจะถูกแปลงเป็นเวกเตอร์สามชุด: Query (Q), Key (K), และ Value (V)

เมื่อโมเดลกำลังสร้างโทเคนถัดไป มันต้องคำนวณความสัมพันธ์ระหว่าง Query ของโทเคนปัจจุบันกับ Key และ Value ของโทเคนทั้งหมดที่ผ่านมา หากเราต้องคำนวณ K และ V ใหม่ทุกครั้งสำหรับทุกโทเคนในทุกขั้นตอนการสร้าง จะเป็นการสิ้นเปลืองพลังงานประมวลผล (computation) อย่างมหาศาล

KV Cache จึงเข้ามาแก้ปัญหานี้ โดยการเก็บ (cache) เวกเตอร์ Key และ Value ของโทเคนเดิมที่เคยคำนวณไปแล้วไว้ในหน่วยความจำ (VRAM) เพื่อที่ในการสร้างโทเคนถัดไป โมเดลจะสามารถดึงข้อมูล K และ V มาใช้ได้ทันทีโดยไม่ต้องคำนวณใหม่

ปัญหาของ KV Cache แบบดั้งเดิม

แม้ว่า KV Cache จะช่วยลดภาระการคำนวณ แต่ก็สร้างปัญหาใหม่ในด้านการจัดการหน่วยความจำ:

  1. การจองหน่วยความจำล่วงหน้า (Static Allocation): เพื่อความรวดเร็ว ระบบมักจะจองพื้นที่หน่วยความจำล่วงหน้าตามความยาวสูงสุดของบริบท (maximum context length) ที่โมเดลรองรับ เช่น หากโมเดลรองรับ 4,096 โทเคน ระบบจะจองพื้นที่สำหรับ 4,096 โทเคนทันที แม้ว่าคำถามของผู้ใช้จะมีเพียง 10 โทเคนก็ตาม
  2. การแตกตัวของหน่วยความจำ (Memory Fragmentation):
    • Internal Fragmentation: พื้นที่ที่จองไว้แต่ไม่ได้ใช้งาน (เช่น จองไว้ 4,096 แต่ใช้จริงแค่ 100) ทำให้เสียพื้นที่ไปโดยเปล่าประโยชน์
    • External Fragmentation: เมื่อมีการสร้างคำขอ (requests) หลายรายการพร้อมกัน พื้นที่ว่างในหน่วยความจำอาจถูกแบ่งเป็นส่วนเล็กๆ ที่ไม่ต่อเนื่องกัน ทำให้ไม่สามารถจัดสรรพื้นที่ขนาดใหญ่สำหรับคำขอใหม่ได้

ผลลัพธ์คือ เราไม่สามารถรองรับจำนวนผู้ใช้งาน (batch size) ได้มากเท่าที่ควร เพราะหน่วยความจำเต็มไปด้วย "พื้นที่ว่างที่ใช้งานไม่ได้"

PagedAttention: ทางออกที่ชาญฉลาด

PagedAttention เป็นเทคนิคที่นำเสนอโดยทีมผู้พัฒนา vLLM ซึ่งได้รับแรงบันดาลใจจากแนวคิด Virtual Memory และ Paging ในระบบปฏิบัติการ (Operating Systems)

แทนที่จะจองพื้นที่หน่วยความจำแบบก้อนเดียวขนาดใหญ่และต่อเนื่องกัน PagedAttention จะแบ่ง KV Cache ออกเป็นบล็อก (blocks) ขนาดเล็กๆ ที่มีขนาดคงที่

วิธีการทำงานของ PagedAttention:

  1. การแบ่งบล็อก (Block Partitioning): KV Cache จะถูกแบ่งเป็นบล็อกๆ (เช่น บล็อกละ 16 โทเคน)
  2. การจัดสรรแบบไม่ต่อเนื่อง (Non-contiguous Allocation): บล็อกเหล่านี้ไม่จำเป็นต้องวางอยู่ติดกันในหน่วยความจำ (VRAM) เราสามารถเก็บบล็อกที่ 1 ไว้ที่ตำแหน่งหนึ่ง และบล็อกที่ 2 ไว้ที่อีกตำแหน่งหนึ่งได้
  3. ตารางหน้า (Page Table): ระบบจะใช้ตารางเพื่อเก็บข้อมูลว่าบล็อกของแต่ละคำขอ (request) อยู่ที่ตำแหน่งใดในหน่วยความจำ ทำให้โมเดลสามารถเข้าถึงข้อมูลได้อย่างต่อเนื่องเสมือนว่ามันวางอยู่ติดกัน

ทำไม PagedAttention ถึงสำคัญ?

การนำ PagedAttention มาใช้ส่งผลกระทบอย่างมหาศาลต่อประสิทธิภาพของ LLM Inference:

  • ลดการสูญเสียหน่วยความจำ (Near-zero Waste): เนื่องจากเราสามารถจัดสรรบล็อกได้ตามความต้องการจริง ทำให้ปัญหา Internal Fragmentation แทบจะหมดไป
  • เพิ่ม Throughput อย่างมหาศาล: เมื่อเราใช้หน่วยความจำได้อย่างมีประสิทธิภาพมากขึ้น เราจึงสามารถใส่คำขอ (requests) จำนวนมากขึ้นลงใน GPU ตัวเดียวกันได้ในเวลาเดียวกัน (เพิ่ม Batch Size)
  • รองรับบริบทที่ยาวขึ้น: การจัดการหน่วยความจำที่มีประสิทธิภาพช่วยให้เราสามารถประมวลผลลำดับข้อความที่ยาวขึ้นได้โดยไม่ติดปัญหาหน่วยความจำเต็มเร็วเกินไป

สรุป

หาก KV Cache คือการจดบันทึกข้อมูลเพื่อไม่ต้องคำนวณใหม่ PagedAttention ก็คือระบบการจัดเก็บสมุดบันทึกเหล่านั้นอย่างเป็นระเบียบและใช้พื้นที่อย่างคุ้มค่าที่สุด

การทำความเข้าใจทั้งสองสิ่งนี้เป็นสิ่งสำคัญสำหรับวิศวกร AI ที่ต้องการปรับแต่ง (optimize) ระบบ Inference ให้ทำงานได้รวดเร็วและรองรับผู้ใช้งานจำนวนมากในระดับโปรดักชัน


Optional learning community: https://t.me/GyaanSetuAi