Some checks failed
Self-hosted runner (nightly-past-ci-caller) / Get number (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.11 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.10 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.9 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.8 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.7 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.6 (push) Has been cancelled
Self-hosted runner (nightly-past-ci-caller) / TensorFlow 2.5 (push) Has been cancelled
Self-hosted runner (benchmark) / Benchmark (aws-g5-4xlarge-cache) (push) Has been cancelled
Build documentation / build (push) Has been cancelled
Build documentation / build_other_lang (push) Has been cancelled
CodeQL Security Analysis / CodeQL Analysis (push) Has been cancelled
New model PR merged notification / Notify new model (push) Has been cancelled
PR CI / pr-ci (push) Has been cancelled
Slow tests on important models (on Push - A10) / Get all modified files (push) Has been cancelled
Secret Leaks / trufflehog (push) Has been cancelled
Update Transformers metadata / build_and_package (push) Has been cancelled
Slow tests on important models (on Push - A10) / Model CI (push) Has been cancelled
Check Tiny Models / Check tiny models (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Model CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Pipeline CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Example CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / DeepSpeed CI (push) Has been cancelled
Self-hosted runner (Intel Gaudi3 scheduled CI caller) / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI - Flash Attn / Setup (push) Has been cancelled
Nvidia CI - Flash Attn / Model CI (push) Has been cancelled
Nvidia CI / Setup (push) Has been cancelled
Nvidia CI / Model CI (push) Has been cancelled
Nvidia CI / Torch pipeline CI (push) Has been cancelled
Nvidia CI / Example CI (push) Has been cancelled
Nvidia CI / Trainer/FSDP CI (push) Has been cancelled
Nvidia CI / DeepSpeed CI (push) Has been cancelled
Nvidia CI / Quantization CI (push) Has been cancelled
Nvidia CI / Kernels CI (push) Has been cancelled
Doctests / Setup (push) Has been cancelled
Doctests / Call doctest jobs (push) Has been cancelled
Doctests / Send results to webhook (push) Has been cancelled
Extras Smoke Test / Get supported Python versions (push) Has been cancelled
Extras Smoke Test / Test extras on Python ${{ matrix.python-version }} (push) Has been cancelled
Extras Smoke Test / Check Slack token availability (push) Has been cancelled
Extras Smoke Test / Notify failures to Slack (push) Has been cancelled
Self-hosted runner (AMD scheduled CI caller) / Trigger Scheduled AMD CI (push) Has been cancelled
Stale Bot / Close Stale Issues (push) Has been cancelled
121 lines
5.4 KiB
Python
121 lines
5.4 KiB
Python
# Copyright 2024 The HuggingFace Team. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import unittest
|
|
|
|
from transformers import PaliGemmaProcessor
|
|
from transformers.testing_utils import get_tests_dir, require_torch, require_vision
|
|
|
|
from ...test_processing_common import ProcessorTesterMixin
|
|
|
|
|
|
SAMPLE_VOCAB = get_tests_dir("fixtures/test_sentencepiece.model")
|
|
|
|
|
|
@require_vision
|
|
class PaliGemmaProcessorTest(ProcessorTesterMixin, unittest.TestCase):
|
|
processor_class = PaliGemmaProcessor
|
|
|
|
@classmethod
|
|
def _setup_image_processor(cls):
|
|
image_processor_class = cls._get_component_class_from_processor("image_processor")
|
|
image_processor = image_processor_class.from_pretrained("google/siglip-so400m-patch14-384")
|
|
image_processor.image_seq_length = 0
|
|
return image_processor
|
|
|
|
@classmethod
|
|
def _setup_tokenizer(cls):
|
|
tokenizer_class = cls._get_component_class_from_processor("tokenizer")
|
|
tokenizer = tokenizer_class.from_pretrained(SAMPLE_VOCAB, keep_accents=True)
|
|
tokenizer.add_special_tokens({"additional_special_tokens": ["<image>"]})
|
|
return tokenizer
|
|
|
|
@classmethod
|
|
def _setup_test_attributes(cls, processor):
|
|
cls.image_token = processor.image_token
|
|
|
|
def test_get_num_vision_tokens(self):
|
|
"Tests general functionality of the helper used internally in vLLM"
|
|
|
|
processor = self.get_processor()
|
|
|
|
output = processor._get_num_multimodal_tokens(image_sizes=[(100, 100), (300, 100), (500, 30)])
|
|
self.assertTrue("num_image_tokens" in output)
|
|
self.assertEqual(len(output["num_image_tokens"]), 3)
|
|
|
|
self.assertTrue("num_image_patches" in output)
|
|
self.assertEqual(len(output["num_image_patches"]), 3)
|
|
|
|
@require_torch
|
|
@require_vision
|
|
def test_image_seq_length(self):
|
|
input_str = "lower newer"
|
|
image_input = self.prepare_image_inputs()
|
|
image_processor = self.get_component("image_processor")
|
|
tokenizer = self.get_component("tokenizer", max_length=112, padding="max_length")
|
|
image_processor.image_seq_length = 14
|
|
processor = self.processor_class(tokenizer=tokenizer, image_processor=image_processor)
|
|
inputs = processor(
|
|
text=input_str, images=image_input, return_tensors="pt", max_length=112, padding="max_length"
|
|
)
|
|
self.assertEqual(len(inputs["input_ids"][0]), 112)
|
|
|
|
@require_torch
|
|
def test_call_with_suffix(self):
|
|
input_str = "lower newer"
|
|
suffix = "upper older longer string"
|
|
image_input = self.prepare_image_inputs()
|
|
processor = self.get_processor()
|
|
inputs = processor(text=input_str, images=image_input, suffix=suffix)
|
|
self.assertTrue("labels" in inputs)
|
|
self.assertEqual(len(inputs["labels"][0]), len(inputs["input_ids"][0]))
|
|
|
|
inputs = processor(text=input_str, images=image_input, suffix=suffix, return_tensors="pt")
|
|
self.assertTrue("labels" in inputs)
|
|
self.assertEqual(len(inputs["labels"][0]), len(inputs["input_ids"][0]))
|
|
|
|
def test_text_with_image_tokens(self):
|
|
image_processor = self.get_component("image_processor")
|
|
tokenizer = self.get_component("tokenizer")
|
|
|
|
processor = self.processor_class(tokenizer=tokenizer, image_processor=image_processor)
|
|
text_multi_images = "<image><image>Dummy text!"
|
|
text_single_image = "<image>Dummy text!"
|
|
text_no_image = "Dummy text!"
|
|
|
|
image = self.prepare_image_inputs()
|
|
|
|
out_noimage = processor(text=text_no_image, images=image, return_tensors="pt")
|
|
out_singlimage = processor(text=text_single_image, images=image, return_tensors="pt")
|
|
for k in out_noimage:
|
|
self.assertTrue(out_noimage[k].tolist() == out_singlimage[k].tolist())
|
|
|
|
out_multiimages = processor(text=text_multi_images, images=[image, image], return_tensors="pt")
|
|
out_noimage = processor(text=text_no_image, images=[[image, image]], return_tensors="pt")
|
|
|
|
# We can't be sure what is users intention, whether user want "one text + two images" or user forgot to add the second text
|
|
with self.assertRaises(ValueError):
|
|
out_noimage = processor(text=text_no_image, images=[image, image], return_tensors="pt")
|
|
|
|
for k in out_noimage:
|
|
self.assertTrue(out_noimage[k].tolist() == out_multiimages[k].tolist())
|
|
|
|
text_batched = ["Dummy text!", "Dummy text!"]
|
|
text_batched_with_image = ["<image>Dummy text!", "<image>Dummy text!"]
|
|
out_images = processor(text=text_batched_with_image, images=[image, image], return_tensors="pt")
|
|
out_noimage_nested = processor(text=text_batched, images=[[image], [image]], return_tensors="pt")
|
|
out_noimage = processor(text=text_batched, images=[image, image], return_tensors="pt")
|
|
for k in out_noimage:
|
|
self.assertTrue(out_noimage[k].tolist() == out_images[k].tolist() == out_noimage_nested[k].tolist())
|