Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/transformers/models/paligemma/processing_paligemma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions tests/models/paligemma/test_processing_paligemma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ["<image>Describe the image.", "<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")
Expand Down