# Copyright 2024 The HuggingFace Inc. 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. """Testing suite for the PyTorch Idefics2 model.""" import tempfile import unittest from io import BytesIO import pytest import requests from transformers import ( AutoProcessor, BitsAndBytesConfig, Idefics2Config, Idefics2ForConditionalGeneration, Idefics2Model, is_torch_available, is_vision_available, ) from transformers.testing_utils import ( Expectations, cleanup, require_bitsandbytes, require_flash_attn, require_torch, require_torch_accelerator, require_torch_multi_accelerator, slow, torch_device, ) from ...generation.test_utils import GenerationTesterMixin from ...model_tester_config import ModelTesterConfigMixin from ...test_configuration_common import ConfigTester from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor if is_torch_available(): import torch if is_vision_available(): from PIL import Image class Idefics2VisionText2TextModelTester(ModelTesterConfigMixin): defaults = {} small_config_overrides = { "is_training": True, "batch_size": 2, "num_images": 2, "num_channels": 3, "seq_length": 10, "vision_config": { "image_size": 12, "patch_size": 12, "num_channels": 3, "hidden_size": 32, "num_hidden_layers": 2, "num_attention_heads": 4, "intermediate_size": 32, "dropout": 0.1, "attention_dropout": 0.1, "initializer_range": 0.02, }, "perceiver_config": { "hidden_act": "silu", "resampler_n_latents": 2, "resampler_depth": 2, "resampler_n_heads": 2, "num_key_value_heads": 1, "resampler_head_dim": 16, "attention_dropout": 0.0, }, "text_config": { "vocab_size": 100, "hidden_size": 64, "intermediate_size": 56, "num_hidden_layers": 2, "num_attention_heads": 2, "num_key_value_heads": 2, "hidden_act": "silu", "max_position_embeddings": 256, "initializer_range": 0.02, "rms_norm_eps": 1e-6, "pad_token_id": 0, # None in the original configuration_mistral, we set it to the unk_token_id "bos_token_id": 1, "eos_token_id": 2, "image_token_id": 57, "tie_word_embeddings": False, "rope_theta": 10000.0, "sliding_window": 32, "attention_dropout": 0.0, }, "use_cache": False, "tie_word_embeddings": False, # should be smaller than vocab size - 16 to survive clamping "image_token_id": 57, } @classmethod def get_small_config_defaults(cls, **kwargs): values = super().get_small_config_defaults(**kwargs) values["pad_token_id"] = values["text_config"]["pad_token_id"] # Hack - add properties here so use common tests values["vocab_size"] = values["text_config"]["vocab_size"] values["num_hidden_layers"] = values["text_config"]["num_hidden_layers"] values["num_attention_heads"] = values["text_config"]["num_attention_heads"] values["hidden_size"] = values["text_config"]["hidden_size"] return values def __init__(self, parent, **kwargs): self.parent = parent for key, value in self.get_small_config_defaults(**kwargs).items(): setattr(self, key, value) def get_config(self): return Idefics2Config( use_cache=self.use_cache, image_token_id=self.image_token_id, tie_word_embeddings=self.tie_word_embeddings, vision_config=self.vision_config, perceiver_config=self.perceiver_config, text_config=self.text_config, vocab_size=self.vocab_size, ) def prepare_config_and_inputs(self): pixel_values = floats_tensor( [ self.batch_size, self.num_images, self.vision_config["num_channels"], self.vision_config["image_size"], self.vision_config["image_size"], ] ) config = self.get_config() return config, pixel_values def prepare_config_and_inputs_for_common(self): config_and_inputs = self.prepare_config_and_inputs() config, pixel_values = config_and_inputs input_ids = ids_tensor([self.batch_size, self.seq_length], config.text_config.vocab_size - 2) + 1 n_image_tokens_per_batch = self.num_images * self.perceiver_config["resampler_n_latents"] input_ids[input_ids == self.image_token_id] = self.pad_token_id input_ids[:, :n_image_tokens_per_batch] = self.image_token_id attention_mask = input_ids.ne(1).to(torch_device) inputs_dict = { "pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask, } return config, inputs_dict @require_torch class Idefics2ModelTest(ModelTesterMixin, unittest.TestCase): """ Model tester for `Idefics2`. """ model_tester_class = Idefics2VisionText2TextModelTester all_model_classes = (Idefics2Model,) if is_torch_available() else () # Idefics2 merges batch_size and num_frames in the first output dimension skip_test_image_features_output_shape = True test_resize_embeddings = True _is_composite = True def setUp(self): self.model_tester = Idefics2VisionText2TextModelTester(self) self.config_tester = ConfigTester( self, config_class=Idefics2Config, has_text_modality=False, common_properties=["image_token_id"] ) def test_config(self): self.config_tester.run_common_tests() @unittest.skip(reason="inputs_embeds cannot be passed in without input_ids") def test_inputs_embeds(): pass @unittest.skip(reason="inputs_embeds cannot be passed in without input_ids") def test_inputs_embeds_matches_input_ids(self): pass @unittest.skip(reason="Model does not support padding right") def test_flash_attn_2_generate_padding_right(self): pass @unittest.skip(reason="Model does not support padding right") def test_flash_attn_2_inference_padding_right(self): pass def test_sdpa_can_dispatch_composite_models(self): for model_class in self.all_model_classes: config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common() model = model_class(config) with tempfile.TemporaryDirectory() as tmpdirname: model.save_pretrained(tmpdirname) model_sdpa = model_class.from_pretrained(tmpdirname) model_sdpa = model_sdpa.eval().to(torch_device) self.assertTrue(model_sdpa.config._attn_implementation == "sdpa") self.assertTrue(model_sdpa.vision_model.config._attn_implementation == "sdpa") self.assertTrue(model_sdpa.connector.perceiver_resampler.config._attn_implementation == "sdpa") model_eager = model_class.from_pretrained(tmpdirname, attn_implementation="eager") model_eager = model_eager.eval().to(torch_device) self.assertTrue(model_eager.config._attn_implementation == "eager") self.assertTrue(model_eager.vision_model.config._attn_implementation == "eager") self.assertTrue(model_eager.connector.perceiver_resampler.config._attn_implementation == "eager") for name, submodule in model_eager.named_modules(): class_name = submodule.__class__.__name__ if "SdpaAttention" in class_name or "SdpaSelfAttention" in class_name: raise ValueError("The eager model should not have SDPA attention layers") @require_torch class Idefics2ForConditionalGenerationModelTest(GenerationTesterMixin, ModelTesterMixin, unittest.TestCase): """ Model tester for `Idefics2ForConditionalGeneration`. """ model_tester_class = Idefics2VisionText2TextModelTester all_model_classes = (Idefics2ForConditionalGeneration,) if is_torch_available() else () pipeline_model_mapping = {"image-text-to-text": Idefics2ForConditionalGeneration} if is_torch_available() else () skip_test_image_features_output_shape = ( True # Idefics2 merges batch_size and num_frames in the first output dimension ) test_resize_embeddings = True def setUp(self): self.model_tester = Idefics2VisionText2TextModelTester(self) self.config_tester = ConfigTester(self, config_class=Idefics2Config, has_text_modality=False) @unittest.skip(reason="inputs_embeds cannot be passed in without input_ids") def test_inputs_embeds(): pass @unittest.skip(reason="Model does not support padding right") def test_flash_attn_2_generate_padding_right(self): pass @unittest.skip(reason="Model does not support padding right") def test_flash_attn_2_inference_padding_right(self): pass @pytest.mark.generate @slow @unittest.skip( reason="Idefics2 doesn't support SDPA for all backbones, vision backbones has only eager/FA2 attention" ) def test_eager_matches_sdpa_generate(self): pass @require_torch class Idefics2ForConditionalGenerationIntegrationTest(unittest.TestCase): def setUp(self): self.processor = AutoProcessor.from_pretrained("HuggingFaceM4/idefics2-8b-base") self.image1 = Image.open( BytesIO( requests.get( "https://huggingface.co/datasets/hf-internal-testing/transformers-synthetic-assets/resolve/main/images/statue_of_liberty.jpg" ).content ) ) self.image2 = Image.open( BytesIO( requests.get( "https://huggingface.co/datasets/hf-internal-testing/transformers-synthetic-assets/resolve/main/images/skyline_chicago.jpg" ).content ) ) self.image3 = Image.open( BytesIO( requests.get( "https://huggingface.co/datasets/hf-internal-testing/transformers-synthetic-assets/resolve/main/images/dreamstime_golden_gate_flowers.jpg" ).content ) ) def tearDown(self): cleanup(torch_device, gc_collect=True) @slow @require_torch_multi_accelerator def test_integration_test(self): model = Idefics2ForConditionalGeneration.from_pretrained( "HuggingFaceM4/idefics2-8b-base", dtype=torch.bfloat16, device_map="auto", ) # Create inputs text = "In this image, we see" images = self.image1 inputs = self.processor(text=text, images=images, return_tensors="pt", padding=True) inputs.to(torch_device) generated_ids = model.generate(**inputs, max_new_tokens=10) generated_texts = self.processor.batch_decode(generated_ids, skip_special_tokens=True) # Batch affects generated text. Single batch output: ['In this image, we see the Statue of Liberty in the foreground and'] expected_generated_text = "In this image, we see the Statue of Liberty, which is a col" self.assertEqual(generated_texts[0], expected_generated_text) @slow @require_bitsandbytes def test_integration_test_4bit(self): # Let' s make sure we test the preprocessing to replace what is used model = Idefics2ForConditionalGeneration.from_pretrained( "HuggingFaceM4/idefics2-8b-base", quantization_config=BitsAndBytesConfig(load_in_4bit=True) ) # Create pixel inputs text = ["In this image, we see", "bla, bla "] images = [[self.image1], [self.image2, self.image3]] inputs = self.processor(text=text, images=images, padding=True, return_tensors="pt").to(torch_device) generated_ids = model.generate(**inputs, max_new_tokens=10) generated_texts = self.processor.batch_decode(generated_ids, skip_special_tokens=True) expected_generated_texts = Expectations( { (None, None): "In this image, we see the Statue of Liberty, which is a col", } ) EXPECTED_GENERATED_TEXT = expected_generated_texts.get_expectation() self.assertEqual(generated_texts[0], EXPECTED_GENERATED_TEXT) @slow @require_bitsandbytes def test_integration_test_4bit_batch2(self): # Let' s make sure we test the preprocessing to replace what is used model = Idefics2ForConditionalGeneration.from_pretrained( "HuggingFaceM4/idefics2-8b-base", quantization_config=BitsAndBytesConfig(load_in_4bit=True) ) from datasets import load_dataset dataset = load_dataset("nielsr/docvqa_1200_examples", split="test") text = [f"{dataset[40]['query']['en']}", f"{dataset[41]['query']['en']}"] images = [[dataset[40]["image"]], [dataset[41]["image"]]] inputs = self.processor(text=text, images=images, padding=True, return_tensors="pt").to(torch_device) generated_ids = model.generate(**inputs, max_new_tokens=64) batched_generated_texts = self.processor.batch_decode(generated_ids, skip_special_tokens=True) text = f"{dataset[40]['query']['en']}" images = dataset[40]["image"] inputs = self.processor(text=text, images=images, padding=True, return_tensors="pt").to(torch_device) generated_ids = model.generate(**inputs, max_new_tokens=64) generated_text_0 = self.processor.batch_decode(generated_ids, skip_special_tokens=True) text = f"{dataset[41]['query']['en']}" images = dataset[41]["image"] inputs = self.processor(text=text, images=images, padding=True, return_tensors="pt").to(torch_device) generated_ids = model.generate(**inputs, max_new_tokens=64) generated_text_1 = self.processor.batch_decode(generated_ids, skip_special_tokens=True) self.assertEqual(batched_generated_texts[0], generated_text_0[0]) self.assertEqual(batched_generated_texts[1], generated_text_1[0]) @pytest.mark.flash_attn_test @require_flash_attn @require_torch_accelerator @require_bitsandbytes def test_flash_attn_2_eager_equivalence(self): # Create inputs text = "In this image, we see" images = self.image1 inputs = self.processor(text=text, images=images, return_tensors="pt", padding=True) inputs.to(torch_device) # Eager model model_eager = Idefics2ForConditionalGeneration.from_pretrained( "HuggingFaceM4/idefics2-8b-base", attn_implementation="eager", quantization_config=BitsAndBytesConfig(load_in_4bit=True), ) generated_ids_eager = model_eager.generate(**inputs, max_new_tokens=10) generated_texts_eager = self.processor.batch_decode(generated_ids_eager, skip_special_tokens=True) del model_eager # Flash Attention 2 model model_flash_attention_2 = Idefics2ForConditionalGeneration.from_pretrained( "HuggingFaceM4/idefics2-8b-base", attn_implementation="flash_attention_2", quantization_config=BitsAndBytesConfig(load_in_4bit=True), ) generated_ids_flash_attention_2 = model_flash_attention_2.generate(**inputs, max_new_tokens=10) generated_texts_flash_attention_2 = self.processor.batch_decode( generated_ids_flash_attention_2, skip_special_tokens=True ) self.assertEqual(generated_texts_eager[0], generated_texts_flash_attention_2[0])