diff --git a/src/transformers/models/paligemma/processing_paligemma.py b/src/transformers/models/paligemma/processing_paligemma.py index 5934fdc4cc95..ea848fa1cbcc 100644 --- a/src/transformers/models/paligemma/processing_paligemma.py +++ b/src/transformers/models/paligemma/processing_paligemma.py @@ -127,9 +127,10 @@ def __call__( # NOTE: ideally users should build 'labels' with data collator, since we return token_type_ids. # Kept only for BC! if "token_type_ids" in result: - labels = np.array(result["input_ids"]) - labels[np.array(result["token_type_ids"]) == 0] = -100 - result["labels"] = labels + result["labels"] = [ + np.where(np.asarray(token_type_ids) == 0, -100, input_ids) + for input_ids, token_type_ids in zip(result["input_ids"], result["token_type_ids"]) + ] return BatchFeature(data=result, tensor_type=return_tensors) diff --git a/tests/models/paligemma/test_processing_paligemma.py b/tests/models/paligemma/test_processing_paligemma.py index be0084920a0d..dfa96e34262a 100644 --- a/tests/models/paligemma/test_processing_paligemma.py +++ b/tests/models/paligemma/test_processing_paligemma.py @@ -83,6 +83,22 @@ def test_call_with_suffix(self): self.assertTrue("labels" in inputs) self.assertEqual(len(inputs["labels"][0]), len(inputs["input_ids"][0])) + def test_unpadded_batch_with_suffix(self): + processor = self.get_processor() + images = self.prepare_images_inputs(batch_size=2) + text = ["Describe the image.", "Describe the image in more detail."] + suffix = ["A cat.", "A cat sitting on a wooden bench."] + + inputs = processor(images=images, text=text, suffix=suffix) + + self.assertNotEqual(len(inputs["input_ids"][0]), len(inputs["input_ids"][1])) + for index in range(2): + single = processor(images=images[index], text=text[index], suffix=suffix[index]) + self.assertEqual(inputs["input_ids"][index], single["input_ids"][0]) + self.assertEqual(list(inputs["labels"][index]), list(single["labels"][0])) + self.assertIn(-100, inputs["labels"][index]) + self.assertTrue(any(label != -100 for label in inputs["labels"][index])) + def test_text_with_image_tokens(self): image_processor = self.get_component("image_processor") tokenizer = self.get_component("tokenizer")