LLM Learning Roadmap
✨ Awesome-Anything
🖼️ Digital Image Processing
🍃 LLM Components
🌱 LLM Pre-training
☘️ LLM Post-Training
🍀 LLM Popular Models
🪴 LLM Applications
🌿 LLM Optimization
🌾 LLM Compression
🌵 LLM Hands-on Practice
🌴 LLM Must-read Papers
🌳 LLM Q&A
🐝 VLM Image Encoders
📝 MISC.
10.4 Left Padding or Right Padding?
Simply put, in decoder-only models like GPT, Gemma, etc., we use the right padding for training and the left padding for inference.
Left Padding for Inference
let’s start with the simple one. It’s easy to understand why we need left padding for inference. For example:
Let the input be like:
[”I love LLM”, “I love ML and AI”]For the input of
I love LLM, if using the right padding, it becomes I love LLM [pad] [pad]. In such a case, we are basically asking the model to predict the next token based on the [pad] tokens, which is not the correct way.If we use the left padding, then the input becomes
[pad] [pad] I love LLM; the next predicted token will be closely adjacent to the meaningful input tokens.Right Padding for Training
Let’s say we have two samples in a batch:
Input1:
”I love LLM.”; output1: “I love it too!”Input2:
“I love ML and AI.”; output2: “I love them too!”During training, they will be concatenated using the right padding as
You can see that, for the first sample, when the model predicts the output
I love it too!, it’s based on the previous tokens I love LLM. without [pad] in between. Also, we don’t calculate the loss for predicting the [pad] based on I love it too!, so the model won’t expect [pad] always after the end of a sentence.In such a case (right padding during training), we will use left padding during inference to make the prediction pattern consistent.
What if we use left padding for training as well?
In this case, we will get training samples like
Looks not bad, isn’t it? We still keep the sentence coherent without
[pad] in between. And this is more consistent with the inference.But here is the problem: we control that the loss is not calculated on tokens of
[pad], but we cannot control the loss is not calculated on the first meaningful token I based on the previous tokens [pad]. Such additional meaningless loss may interfere with the LLM training and convergence dynamics.What happens if use the right padding during inference?
- Example Code
When padding_side="left", with the input
“1, 2, 3”, we getWhen padding_side="right", with the same input
“1, 2, 3”, we getYou can observe there is an obvious non-consistent between the input and output. This aligns with our assumption that the right padding during inference hurts the model's performance and may lead to unexpected behavior.
Another example: MedGemma
In Google MedGemma’s official fine-tuning code, they explicitly indicate using right padding for training and left padding for inference
- Training Config
- Inference Config
Case Study: GRPO in VLM-R1
GRPO is introduced in 3.4.1 GRPO
When we say right padding for training and left padding for inference, we are talking about the SFT; when it comes to the RLHF, the situation is different. Let's take GRPO as an example using the code from VLM-R1.
Data Preparation for VLM Module
Code from here
Vision language model (VLM) is basically an LLM with an additional vision module to support vision (e.g., image) input. But you can observe here the padding side is set to
left instead of right during training.This is because, in the SFT phase, the LLM is trained with teacher-forcing, which means the model is trained in a parallel behavior by inputting all prompts + ground truth outputs into the model. Like the example we mentioned before:
Where
I love LLM is the prompt and I love it too! is the ground truth output. In this case, we surely use the right padding.But during GRPO training, let’s say we use the outcome reward model (ORM), the outputs are not forced with ground truth text. Instead, we use rule-based reward by extracting the answer from the model’s output and comparing it with the true label, or a reward model (a language model too), or LLM-as-a-judge, i.e., letting an additional LLM judge if the question is correctly answered. In this case, we need the complete output from the trained model, so we are basically doing inference to get it. This is why we prepare the VLM inputs using left padding, making it look like we are using left padding for training.
Reward Model Input Processing
Code from here
As you remember, the reward model is usually frozen during RL like PPO, GRPO, etc. This seems to suggest using left padding because it’s just pure inference.
In fact, the case for the reward model is different again. The reward model is trained to output just a single reward score (in ORM), or we use another LLM (LLM-as-a-judge) to give a reward using prompt engineering.
In the above code, it uses the reward model, which is usually trained with the right padding, i.e., with input
"I love LLM. I love it too! [pad] [pad]", the reward model outputs 0.9 to encourage the LLM output I love it too! based on the user’s input I love LLM.In such a case, the reward model is trained with the right padding, i.e. the padding is part of the inputs for calculating loss, so we should also use the right padding for inference.
But if it’s LLM-as-a-judge, the reward score is acquired by prompt engineering, so there is no usage of left padding or right padding.
Note that, in our example code of 8.4 OpenRLHF: Reward Model Training and Code Analysis, we do similar thing: truncate inputs that are too long and right-pad ones that are too short.
Prev
10.3 KL(P||Q) or KL(Q||P)?
Next
10.5 What is QK-Norm?
Loading...