Post training a VLM for reasoning with GRPO using TRL
Authored by: Sergio Paniego π¨ WARNING: This section is resource-intensive and requires substantial computational power. If youβre running this in Colab, it will utilize an A100 GPU. In this recipe, weβll demonstrate how to post-train a Vision Language Model (VLM) using GRPO for adding reasoning capabilities to a VLM using the Hugging Face ecosystem, specifically with the Transformer Reinforcement Learning library (trl). Weβll be fine-tuning Qwen2.5-VL-3B-Instruct using a subset of the lmms-lab/multimodal-open-r1-8k-verified dataset. This dataset includes images with problem descriptions along with their solution and thinking trace to reach that solution. Weβll leverage this data format, along with the GRPO reward functions, to teach the model how to reason to reach the solution.1. Install Dependencies
Letβs start by installing the essential libraries weβll need for fine-tuning. Weβll installtrl from source, as the VLM GRPO trainer hasnβt been included in an official release at the time of writing.
2. Load Dataset π
We leverage lmms-lab/multimodal-open-r1-8k-verified for this recipe. This dataset contains 8k multimodal RL training examples focused on math reasoning. This data was created using GPT4o and includesimage, problem, solution, original question and original answer for each sample. It was created in this project.
For our particular case where we want the model to learn to reason using images, we use image and problem as input and solution as output.
For this educational resource, weβll only use 5% of the dataset and divide it into train and test sets to make it faster to train. In a real training, weβd use the full dataset.
Weβll load the dataset and divide it.
problem and image columns, we also include a custom system prompt to tell the model how weβd like the generation.
The system prompt is extracted from DeepSeek R1. Refer to this previous recipe for more details.
We convert the dataset samples into conversation samples, including the system prompt and one image and problem description per sample, since this is how the GRPO trainer expects them.
We also set padding_side="left" to ensure that generated completions during training are concatenated directly after the prompt, which is essential for GRPO to correctly compare token-level probabilities between preferred and rejected responses.
3. Post-Training the VLM Using GRPO
The diagram below highlights the main differences between PPO (Proximal Policy Optimization) and GRPO (Group Relative Policy Optimization), specifically the removal of the value model in GRPO. For more detailed information on the key differences, you can refer to this further explanation. To implement the training pipeline, we leverage trl, Hugging Faceβs library for reinforcement learning, which provides a streamlined interface and built-in support for key training algorithms. In our case, we use theGRPOConfig and GRPOTrainer classes. A crucial step in this process is defining custom reward functions that guide the modelβs behavior and help it align with our specific objectives.
But first, letβs load the model. In this case, we use Qwen/Qwen2.5-VL-3B-Instruct, a powerful VLM developed by Qwen. For better results, it would be important to consider models with a larger number of parameters.
Others examples of VLM projects that include reasoning capabilities are:
3.1 Loading the Baseline Model
Letβs load the baseline model first. As previously introduced,Qwen/Qwen2.5-VL-3B-Instruct.
3.2 Configuring LoRA
Weβll leverage LoRA for training the model, so letβs configure it.3.3 Loading Reward Functions
For the reward component of the system, we can use either pretrained reward models or reward functions defined directly in code. For training, the DeepSeek-R1 authors used an accuracy-based reward model that evaluates whether the response is correct, alongside a format-based reward that ensures the model places its reasoning process between<think> </think> tags. You can find more details here. We can simply define and implement these reward functions as generic Python functions.
In this case, we will utilize the following reward functions, directly extracted from the Open R1 implementation:
- Format Enforcement: Ensures that the generation follows a specific format using
<think> </think> <answer> </answer>tags for reasoning.
- Solution Accuracy: Verifies whether the solution to the problem is correct, comparing it to the
solutioncolumn in the dataset.
3.4 Configuring GRPO Training Parameters
Next, letβs configure the training parameters for GRPO. We recommend experimenting with themax_completion_length, num_generations, and max_prompt_length parameters.
Itβd be interesting to play with the max_completion_length, num_generations, and max_prompt_length params in order to find the best training combination.
The parameter selection has been adjusted to fit within the hardware limitations of a Google Colab session. To observe the full potential of reward improvements, especially in the second objective function, and to further improve the modelβs reasoning capabilities in a real-world scenario, a more ambitious setup would be required. This would involve larger models, an increased number of generations, and a high-quality, diverse dataset.
3.5 Training the Model π
Now, letβs configure the trainer and start training the model! In this case, we pass the two reward functions we previously defined to the trainer, in addition with the model, trainings arguments and dataset. Below, youβll find a diagram of the training procedure weβll be reproducing, which is extracted from the Open-R1 project.[307/307 1:48:37, Epoch 1/1]
| Step | Training Loss |
|---|---|
| 10 | 0.000000 |
| 20 | 0.000000 |
| 30 | 0.040757 |
| 40 | 0.000000 |
| 50 | 0.067859 |
| 60 | 0.000000 |
| 70 | 0.000000 |
| 80 | 0.000000 |
| 90 | 0.000000 |
| 100 | 0.000000 |
| 110 | 0.000000 |
| 120 | 0.000000 |
| 130 | 0.000000 |
| 140 | 0.000000 |
| 150 | 0.000000 |
| 160 | 0.015370 |
| 170 | 0.000000 |
| 180 | 0.000000 |
| 190 | 0.000000 |
| 200 | 0.000000 |
| 210 | 0.000000 |
| 220 | 0.052498 |
| 230 | 0.000000 |
| 240 | 0.000000 |
| 250 | 0.000000 |
| 260 | 0.013925 |
| 270 | 0.000000 |
| 280 | 0.000000 |
| 290 | 0.013467 |
| 300 | 0.000000 |
4. Check the Model Performance
Now that weβve our model trained, we can check itβs performance to evaluate it qualitatively.We recommend restarting your session in order to free the resources used for training.
<think>reasoning</think><answer>solution</answer>. Letβs check the actual solution, to understand if the model is correct.
5. Continuing Your Learning Journey π§βπ
The learning journey does not stop here! If youβre eager on discovering more about GRPO, reasoning or VLMs, we can recommend some materials:Post training an LLM for reasoning with GRPO in TRLrecipe and the linked resources that it includes- GLM-4.1V-9B-Thinking paper
- TRL GRPO for VLMs example

