Online DPO: Idefics2 image batches fail in vision tensor duplication and scoring preparation
Reproduction
Following the separate Idefics2 limitation identified in the review of #6968, I reproduced two blockers on main 0b4b33d7faa2ad88b92db2bfac3880ece6de8a66. This uses two image-bearing rows, not a mixed image/text batch, and leaves #6968's scope unchanged. This was found during contribution follow-up, not a production workload.
import copy
import tempfile
from unittest.mock import patch
import torch
from datasets import Dataset
from PIL import Image
from transformers import AutoModelForImageTextToText, AutoProcessor
from trl.experimental.online_dpo import OnlineDPOConfig, OnlineDPOTrainer
torch.set_num_threads(2)
model_id = "trl-internal-testing/tiny-Idefics2ForConditionalGeneration"
model = AutoModelForImageTextToText.from_pretrained(model_id, dtype=torch.float32)
processor = AutoProcessor.from_pretrained(model_id)
inputs = {
"prompt": [[{"role": "user", "content": "Describe the image."}]] * 2,
"image": [Image.new("RGB", (64, 64)), Image.new("RGB", (64, 64))],
}
def reward(prompts, **kwargs):
return [0.0] * len(prompts)
with tempfile.TemporaryDirectory() as output:
trainer = OnlineDPOTrainer(
model=model, processing_class=processor, reward_funcs=reward,
train_dataset=Dataset.from_dict(inputs),
args=OnlineDPOConfig(
output_dir=output, use_cpu=True, bf16=False, use_vllm=False,
max_steps=1, max_new_tokens=1, per_device_train_batch_size=2,
report_to="none",
),
)
trainer.training_step(trainer.model, copy.deepcopy(inputs))
The first failure, before generation runs:
Traceback (most recent call last):
File "repro.py", line 33, in <module>
trainer.training_step(trainer.model, copy.deepcopy(inputs))
File "trl/experimental/online_dpo/online_dpo_trainer.py", line 1156, in training_step
prompt_ids, prompt_mask, completion_ids, completion_mask = self._generate(model, prompts, images)
File "trl/experimental/online_dpo/online_dpo_trainer.py", line 1006, in _generate
vision_generation_kwargs["pixel_values"] = prompt_inputs["pixel_values"].repeat(2, 1, 1, 1)
RuntimeError: Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor
Idefics2 returns pixel_values shaped (2, 5, 3, 378, 378) and pixel_attention_mask shaped (2, 5, 378, 378). Both have more dimensions than the hard-coded repeat tuples. The scoring preparation contains the same assumptions.
To isolate the second blocker, replace the last line above with this diagnostic stub (not an end-to-end generation test):
ids = torch.ones((4, 1), dtype=torch.long)
with patch.object(trainer, "_generate", return_value=(ids, ids, ids, ids)):
trainer.training_step(trainer.model, copy.deepcopy(inputs))
Traceback (most recent call last):
File "repro.py", line 35, in <module>
trainer.training_step(trainer.model, copy.deepcopy(inputs))
File "trl/experimental/online_dpo/online_dpo_trainer.py", line 1167, in training_step
processed = self.processing_class(text=[""] * len(images), return_tensors="pt", **kwargs)
File "transformers/models/idefics2/processing_idefics2.py", line 180, in __call__
raise ValueError(...)
ValueError: The number of images in the text [0, 0] and images [1, 1] should be the same.
The real processor accepts the same images with matching image placeholders. That control does not establish a successful training run; GPU/vLLM execution was not tested.
Proposed scope and ownership
I would like to implement the focused fix and regression coverage myself: preserve the processor's tensor ranks when duplicating vision inputs, and avoid the empty-text reprocessing when preparing scoring inputs. The intended regression would exercise a real image-bearing training_step, with an existing VLM case guarding compatibility. I would not hard-code an Idefics2 token into the trainer.
Given the experimental-support discussion in #7182, would maintaining this path be welcome? I will wait for direction before opening the follow-up PR. This is separate from #6968, not a reason to expand or block that PR.
System Info
- TRL main
0b4b33d7faa2ad88b92db2bfac3880ece6de8a66, version1.13.0.dev0 - Windows 11, Python 3.13.13
- PyTorch 2.13.0+cpu; Transformers 5.16.1
- Real tiny Idefics2 model and processor, CPU only
I came across this while going through open VLM issues (not from a production run) and put together a fix with AI assistance, in case it's useful to @DaoyuanLi2816, who offered to take this. It makes the vision-tensor duplication rank-agnostic and builds the scoring inputs with processing_class.image_processor instead of calling the processor with empty text. With that, the reproduction above passes for tiny Idefics2, and the existing Online DPO test suite still passes. Branch: https://github.com/eliem08/trl/tree/fix/online-dpo-vlm-tensor-ranks
Two related things I saw on transformers 5.19 that also happen without this change:
- Qwen2-VL fails in Online DPO because
mm_token_type_idsisn't passed to generate/forward. - With tiny Llava, scoring fails with "Image features and image tokens do not match": left-truncation to
max_lengthdrops image tokens.
Also, test_online_dpo_vlm_trainer uses an images column, but training_step only looks for image, so that test doesn't exercise the vision path.
Both blockers were fixed in #7704 (tracked separately as #7702). Vision tensors are now duplicated with torch.cat whatever their rank, and the forward pass reuses the generation-time vision inputs instead of re-running the processor with empty text:
https://github.com/huggingface/trl/blob/375a6434bec36d3bc49bb8a4cf0b10f29f073324/trl/experimental/online_dpo/online_dpo_trainer.py#L984-L996
Your reproduction passes on current main. @eliem08, the side issues you spotted are covered too: mm_token_type_ids in #7859, and the Llava truncation plus the images test column in #7703.