Back to LLMs Mind Map
中文·English
LLMsID: padding-packing

Padding & Packing

填充与打包 Padding/Packing
🎯Core Definition
Padding pads sequences of unequal length in a batch to the same length for GPU matrix alignment — usually dynamically to the batch's longest sequence (or a multiple of 8/16); Packing concatenates multiple short sequences without separators into one long sequence, eliminating pad-token compute waste entirely, with attention/loss masks blocking cross-sample boundaries during training.
💡Use Cases
batching in training and inference — dynamic padding saves significant compute vs fixed max length; on datasets with skewed lengths (e.g., instruction data), pack samples up to LmaxL_{max}; also a prerequisite for efficient FlashAttention training.
Key Problems Solved
with fixed-length padding, pad tokens can account for 50%+ of compute when short texts dominate, and pads still produce meaningless attention; packing raises useful-token utilization to near 100%, but sample seams must be masked or the model learns to predict across boundaries — a wrong pattern.
🎯5 High-Frequency Exam Points
1
Dynamic vs fixed-length padding? Why pad to multiples of 8/16?
2
How does sequence packing remove pad waste? Masking sample seams?
3
Problems when pads enter attention? Role of the attention mask?
4
Loss computation under packing? Why a loss mask is needed?
5
How padding/packing relate to FlashAttention and PagedAttention?
Updated 2026-08-12
🎯
Test Your Knowledge: Practice Questions for "Padding & Packing"
Single choice pitfall questions with instant feedback and mistake tracking.
🚀 Start Card Practice
Previous CardText EmbeddingsNext CardDecoding Strategies

🔗 More LLMs Knowledge Cards

Agent & Tool CallingAlignment Tax & Preference DataScaled Dot-Product AttentionAttention Variants MHA/MQA/GQA