

'''simple docstring'''

import inspect
import unittest

from transformers import DataaVecVisionConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch
						from torch import nn

						from transformers import (
						    MODEL_MAPPING,
						    DataaVecVisionForImageClassification,
						    DataaVecVisionForSemanticSegmentation,
						    DataaVecVisionModel,
						)
						from transformers.models.dataavec.modeling_dataavec_vision import DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import BeitImageProcessor







class lowerCamelCase   :

				def __init__(       self,						lowercase_,						lowercase_=100,						lowercase_=13,						lowercase_=30,						lowercase_=2,						lowercase_=3,						lowercase_=True,						lowercase_=True,						lowercase_=32,						lowercase_=4,						lowercase_=4,						lowercase_=37,						lowercase_="gelu",						lowercase_=0.1,						lowercase_=0.1,						lowercase_=10,						lowercase_=0.02,						lowercase_=3,						lowercase_=None,						lowercase_=[0, 1, 2, 3],						)    ->							Dict:
					snake_case							=					parent
					snake_case							=					100
					snake_case							=					batch_size
					snake_case							=					image_size
					snake_case							=					patch_size
					snake_case							=					num_channels
					snake_case							=					is_training
					snake_case							=					use_labels
					snake_case							=					hidden_size
					snake_case							=					num_hidden_layers
					snake_case							=					num_attention_heads
					snake_case							=					intermediate_size
					snake_case							=					hidden_act
					snake_case							=					hidden_dropout_prob
					snake_case							=					attention_probs_dropout_prob
					snake_case							=					type_sequence_label_size
					snake_case							=					initializer_range
					snake_case							=					scope
					snake_case							=					out_indices
					snake_case							=					num_labels

					# in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
					snake_case							=					(image_size // patch_size) ** 2
					snake_case							=					num_patches + 1

				def 					_lowerCamelCase    (       self )    ->							Union[str, Any]:
					snake_case							=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

					snake_case							=					None
					snake_case							=					None
					if self.use_labels:
						snake_case							=					ids_tensor([self.batch_size],						self.type_sequence_label_size )
						snake_case							=					ids_tensor([self.batch_size, self.image_size, self.image_size],						self.num_labels )

					snake_case							=					self.get_config()

					return config, pixel_values, labels, pixel_labels

				def 					_lowerCamelCase    (       self )    ->							Tuple:
					return DataaVecVisionConfig(
					    vocab_size=self.vocab_size,						image_size=self.image_size,						patch_size=self.patch_size,						num_channels=self.num_channels,						hidden_size=self.hidden_size,						num_hidden_layers=self.num_hidden_layers,						num_attention_heads=self.num_attention_heads,						intermediate_size=self.intermediate_size,						hidden_act=self.hidden_act,						hidden_dropout_prob=self.hidden_dropout_prob,						attention_probs_dropout_prob=self.attention_probs_dropout_prob,						is_decoder=lowercase_,						initializer_range=self.initializer_range,						out_indices=self.out_indices,						)

				def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_,						lowercase_,						lowercase_ )    ->							Tuple:
					snake_case							=					DataaVecVisionModel(config=lowercase_ )
					model.to(lowercase_ )
					model.eval()
					snake_case							=					model(lowercase_ )
					# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
					snake_case							=					(self.image_size // self.patch_size) ** 2
					self.parent.assertEqual(result.last_hidden_state.shape,						(self.batch_size, num_patches + 1, self.hidden_size) )

				def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_,						lowercase_,						lowercase_ )    ->							Any:
					snake_case							=					self.type_sequence_label_size
					snake_case							=					DataaVecVisionForImageClassification(lowercase_ )
					model.to(lowercase_ )
					model.eval()
					snake_case							=					model(lowercase_,						labels=lowercase_ )
					self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.type_sequence_label_size) )

				def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_,						lowercase_,						lowercase_ )    ->							str:
					snake_case							=					self.num_labels
					snake_case							=					DataaVecVisionForSemanticSegmentation(lowercase_ )
					model.to(lowercase_ )
					model.eval()
					snake_case							=					model(lowercase_ )
					self.parent.assertEqual(
					    result.logits.shape,						(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) )
					snake_case							=					model(lowercase_,						labels=lowercase_ )
					self.parent.assertEqual(
					    result.logits.shape,						(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2) )






				def 					_lowerCamelCase    (       self )    ->							Union[str, Any]:
					snake_case							=					self.prepare_config_and_inputs()
					snake_case			,			snake_case			,			snake_case			,			snake_case							=					config_and_inputs
					snake_case							=					{'pixel_values': pixel_values}
					return config, inputs_dict







@require_torch
class lowerCamelCase   (    __lowerCAmelCase				,				__lowerCAmelCase				,				unittest.TestCase				):
				snake_case_							     = (
				    (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
				    if is_torch_available()
				    else ()
				)
				snake_case_							     = (
				    {
				        '''feature-extraction''': DataaVecVisionModel,
				        '''image-classification''': DataaVecVisionForImageClassification,
				        '''image-segmentation''': DataaVecVisionForSemanticSegmentation,
				    }
				    if is_torch_available()
				    else {}
				)

				snake_case_							     = False
				snake_case_							     = False
				snake_case_							     = False

				def 					_lowerCamelCase    (       self )    ->							Tuple:
					snake_case							=					DataaVecVisionModelTester(self )
					snake_case							=					ConfigTester(
					    self,						config_class=lowercase_,						has_text_modality=lowercase_,						hidden_size=37 )

				def 					_lowerCamelCase    (       self )    ->							Any:
					self.config_tester.run_common_tests()

				def 					_lowerCamelCase    (       self )    ->							List[Any]:
					# Data2VecVision does not use inputs_embeds
					pass

				@require_torch_multi_gpu
				@unittest.skip(
				    reason='Data2VecVision has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`' )
				def 					_lowerCamelCase    (       self )    ->							Tuple:
					pass

				def 					_lowerCamelCase    (       self )    ->							Tuple:
					snake_case			,			snake_case							=					self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						snake_case							=					model_class(lowercase_ )
						self.assertIsInstance(model.get_input_embeddings(),						(nn.Module) )
						snake_case							=					model.get_output_embeddings()
						self.assertTrue(x is None or isinstance(lowercase_,						nn.Linear ) )

				def 					_lowerCamelCase    (       self )    ->							Dict:
					snake_case			,			snake_case							=					self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						snake_case							=					model_class(lowercase_ )
						snake_case							=					inspect.signature(model.forward )
						# signature.parameters is an OrderedDict => so arg_names order is deterministic
						snake_case							=					[*signature.parameters.keys()]

						snake_case							=					['pixel_values']
						self.assertListEqual(arg_names[:1],						lowercase_ )

				def 					_lowerCamelCase    (       self )    ->							Optional[Any]:
					snake_case							=					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*lowercase_ )

				def 					_lowerCamelCase    (       self )    ->							Optional[Any]:
					snake_case							=					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_segmentation(*lowercase_ )

				def 					_lowerCamelCase    (       self )    ->							Tuple:
					if not self.model_tester.is_training:
						return

					snake_case			,			snake_case							=					self.model_tester.prepare_config_and_inputs_for_common()
					snake_case							=					True

					for model_class in self.all_model_classes:
						if model_class in [*get_values(lowercase_ )]:
							continue

						snake_case							=					model_class(lowercase_ )
						model.to(lowercase_ )
						model.train()
						snake_case							=					self._prepare_for_class(lowercase_,						lowercase_,						return_labels=lowercase_ )
						snake_case							=					model(**lowercase_ ).loss
						loss.backward()

				def 					_lowerCamelCase    (       self )    ->							List[Any]:
					snake_case			,			snake_case							=					self.model_tester.prepare_config_and_inputs_for_common()
					if not self.model_tester.is_training:
						return

					snake_case							=					False
					snake_case							=					True

					for model_class in self.all_model_classes:
						if model_class in [*get_values(lowercase_ )] or not model_class.supports_gradient_checkpointing:
							continue
						# TODO: remove the following 3 lines once we have a MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING
						# this can then be incorporated into _prepare_for_class in test_modeling_common.py
						elif model_class.__name__ == "Data2VecVisionForSemanticSegmentation":
							snake_case			,			snake_case			,			snake_case			,			snake_case							=					inputs_dict['pixel_values'].shape
							snake_case							=					torch.zeros(
							    [self.model_tester.batch_size, height, width],						device=lowercase_ ).long()
						snake_case							=					model_class(lowercase_ )
						model.gradient_checkpointing_enable()
						model.to(lowercase_ )
						model.train()
						snake_case							=					self._prepare_for_class(lowercase_,						lowercase_,						return_labels=lowercase_ )
						snake_case							=					model(**lowercase_ ).loss
						loss.backward()

				def 					_lowerCamelCase    (       self )    ->							int:
					snake_case			,			snake_case							=					self.model_tester.prepare_config_and_inputs_for_common()

					snake_case							=					_config_zero_init(lowercase_ )
					for model_class in self.all_model_classes:
						snake_case							=					model_class(config=lowercase_ )
						for name, param in model.named_parameters():
							# we skip lambda parameters as these require special initial values
							# determined by config.layer_scale_init_value
							if "lambda" in name:
								continue
							if param.requires_grad:
								self.assertIn(
								    ((param.data.mean() * 1E9).round() / 1E9).item(),						[0.0, 1.0],						msg=F'''Parameter {name} of model {model_class} seems not properly initialized''',						)

				def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_,						lowercase_,						lowercase_=2E-4,						lowercase_="outputs",						lowercase_=None )    ->							Tuple:
					# We override with a slightly higher tol value, as semseg models tend to diverge a bit more
					super().check_pt_tf_outputs(lowercase_,						lowercase_,						lowercase_,						lowercase_,						lowercase_,						lowercase_ )

				def 					_lowerCamelCase    (       self )    ->							Dict:
					snake_case							=					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*lowercase_ )






				@slow
				def 					_lowerCamelCase    (       self )    ->							Dict:
					for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						snake_case							=					DataaVecVisionModel.from_pretrained(lowercase_ )
						self.assertIsNotNone(lowercase_ )

def      __magic_name__							(			)      ->		Tuple:
	snake_case							=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
	return image







@require_torch
@require_vision
class lowerCamelCase   (    unittest.TestCase				):

				@cached_property
				def 					_lowerCamelCase    (       self )    ->							Union[str, Any]:
					return (
					    BeitImageProcessor.from_pretrained('facebook/data2vec-vision-base-ft1k' ) if is_vision_available() else None
					)






				@slow
				def 					_lowerCamelCase    (       self )    ->							Dict:
					snake_case							=					DataaVecVisionForImageClassification.from_pretrained('facebook/data2vec-vision-base-ft1k' ).to(
					    lowercase_ )

					snake_case							=					self.default_image_processor
					snake_case							=					prepare_img()
					snake_case							=					image_processor(images=lowercase_,						return_tensors='pt' ).to(lowercase_ )

					# forward pass
					with torch.no_grad():
						snake_case							=					model(**lowercase_ )
					snake_case							=					outputs.logits

					# verify the logits
					snake_case							=					torch.Size((1, 1000) )
					self.assertEqual(logits.shape,						lowercase_ )

					snake_case							=					torch.tensor([0.3_277, -0.1_395, 0.0_911] ).to(lowercase_ )

					self.assertTrue(torch.allclose(logits[0, :3],						lowercase_,						atol=1E-4 ) )

					snake_case							=					[model.config.labelaid[i] for i in ['remote control, remote', 'tabby, tabby cat']]
					self.assertEqual(logits[0].topk(2 ).indices.cpu().tolist(),						lowercase_ )








'''simple docstring'''

import unittest

import requests

from transformers import MODEL_FOR_VISION_2_SEQ_MAPPING, TF_MODEL_FOR_VISION_2_SEQ_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_torch_available():
						from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
						lowerCAmelCase_     =      False


if is_vision_available():
						from PIL import Image
else:







						class lowerCamelCase   :

										@staticmethod
										def 					_lowerCamelCase    (       *lowercase_,						**lowercase_ )    ->							List[Any]:
											pass







@is_pipeline_test
@require_vision
class lowerCamelCase   (    unittest.TestCase				):
				snake_case_							     = MODEL_FOR_VISION_2_SEQ_MAPPING
				snake_case_							     = TF_MODEL_FOR_VISION_2_SEQ_MAPPING

				def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_,						lowercase_ )    ->							Optional[Any]:
					snake_case							=					pipeline('image-to-text',						model=lowercase_,						tokenizer=lowercase_,						image_processor=lowercase_ )
					snake_case							=					[
					    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' ),
					    './tests/fixtures/tests_samples/COCO/000000039769.png',
					]
					return pipe, examples

				def 					_lowerCamelCase    (       self,						lowercase_,						lowercase_ )    ->							Tuple:
					snake_case							=					pipe(lowercase_ )
					self.assertEqual(
					    lowercase_,						[
					        [{'generated_text': ANY(lowercase_ )}],
					        [{'generated_text': ANY(lowercase_ )}],
					    ],						)

				@require_tf
				def 					_lowerCamelCase    (       self )    ->							int:
					snake_case							=					pipeline('image-to-text',						model='hf-internal-testing/tiny-random-vit-gpt2',						framework='tf' )
					snake_case							=					'./tests/fixtures/tests_samples/COCO/000000039769.png'

					snake_case							=					pipe(lowercase_ )
					self.assertEqual(
					    lowercase_,						[
					        {
					            'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
					        },
					    ],						)

					snake_case							=					pipe([image, image] )
					self.assertEqual(
					    lowercase_,						[
					        [
					            {
					                'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
					            }
					        ],
					        [
					            {
					                'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
					            }
					        ],
					    ],						)

					snake_case							=					pipe(lowercase_,						max_new_tokens=1 )
					self.assertEqual(
					    lowercase_,						[{'generated_text': 'growth'}],						)

				@require_torch
				def 					_lowerCamelCase    (       self )    ->							Tuple:
					snake_case							=					pipeline('image-to-text',						model='hf-internal-testing/tiny-random-vit-gpt2' )
					snake_case							=					'./tests/fixtures/tests_samples/COCO/000000039769.png'

					snake_case							=					pipe(lowercase_ )
					self.assertEqual(
					    lowercase_,						[
					        {
					            'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
					        },
					    ],						)

					snake_case							=					pipe([image, image] )
					self.assertEqual(
					    lowercase_,						[
					        [
					            {
					                'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
					            }
					        ],
					        [
					            {
					                'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
					            }
					        ],
					    ],						)

				@require_torch
				def 					_lowerCamelCase    (       self )    ->							Union[str, Any]:
					snake_case							=					pipeline('image-to-text',						model='hf-internal-testing/tiny-random-BlipForConditionalGeneration' )
					snake_case							=					'./tests/fixtures/tests_samples/COCO/000000039769.png'
					snake_case							=					'a photo of'

					snake_case							=					pipe(lowercase_,						prompt=lowercase_ )
					self.assertTrue(outputs[0]['generated_text'].startswith(lowercase_ ) )

				@slow
				@require_torch
				def 					_lowerCamelCase    (       self )    ->							Optional[Any]:
					snake_case							=					pipeline('image-to-text',						model='ydshieh/vit-gpt2-coco-en' )
					snake_case							=					'./tests/fixtures/tests_samples/COCO/000000039769.png'

					snake_case							=					pipe(lowercase_ )
					self.assertEqual(lowercase_,						[{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}] )

					snake_case							=					pipe([image, image] )
					self.assertEqual(
					    lowercase_,						[
					        [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
					        [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
					    ],						)

				@slow
				@require_torch
				def 					_lowerCamelCase    (       self )    ->							List[Any]:
					snake_case							=					pipeline('image-to-text',						model='Salesforce/blip-image-captioning-base' )
					snake_case							=					'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
					snake_case							=					Image.open(requests.get(lowercase_,						stream=lowercase_ ).raw )

					snake_case							=					pipe(lowercase_ )
					self.assertEqual(lowercase_,						[{'generated_text': 'a pink pokemon pokemon with a blue shirt and a blue shirt'}] )

				@slow
				@require_torch
				def 					_lowerCamelCase    (       self )    ->							Dict:
					snake_case							=					pipeline('image-to-text',						model='microsoft/git-base-coco' )
					snake_case							=					'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
					snake_case							=					Image.open(requests.get(lowercase_,						stream=lowercase_ ).raw )

					snake_case							=					pipe(lowercase_ )
					self.assertEqual(lowercase_,						[{'generated_text': 'a cartoon of a purple character.'}] )

				@slow
				@require_torch
				def 					_lowerCamelCase    (       self )    ->							Optional[Any]:
					snake_case							=					pipeline('image-to-text',						model='Salesforce/blip-image-captioning-base' )
					snake_case							=					'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
					snake_case							=					Image.open(requests.get(lowercase_,						stream=lowercase_ ).raw )

					snake_case							=					'a photography of'

					snake_case							=					pipe(lowercase_,						prompt=lowercase_ )
					self.assertEqual(lowercase_,						[{'generated_text': 'a photography of a volcano'}] )

					with self.assertRaises(lowercase_ ):
						snake_case							=					pipe([image, image],						prompt=[prompt, prompt] )

				@slow
				@require_torch
				def 					_lowerCamelCase    (       self )    ->							Dict:
					snake_case							=					pipeline('image-to-text',						model='microsoft/git-base-coco' )
					snake_case							=					'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
					snake_case							=					Image.open(requests.get(lowercase_,						stream=lowercase_ ).raw )

					snake_case							=					'a photo of a'

					snake_case							=					pipe(lowercase_,						prompt=lowercase_ )
					self.assertEqual(lowercase_,						[{'generated_text': 'a photo of a tent with a tent and a tent in the background.'}] )

					with self.assertRaises(lowercase_ ):
						snake_case							=					pipe([image, image],						prompt=[prompt, prompt] )

				@unittest.skipIf(
				    not is_torch_greater_or_equal_than_1_11,						reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.' )
				@slow
				@require_torch
				def 					_lowerCamelCase    (       self )    ->							Any:
					snake_case							=					pipeline('image-to-text',						model='google/pix2struct-ai2d-base' )
					snake_case							=					'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
					snake_case							=					Image.open(requests.get(lowercase_,						stream=lowercase_ ).raw )

					snake_case							=					'What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'

					snake_case							=					pipe(lowercase_,						prompt=lowercase_ )
					self.assertEqual(lowercase_,						[{'generated_text': 'ash cloud'}] )

					with self.assertRaises(lowercase_ ):
						snake_case							=					pipe([image, image],						prompt=[prompt, prompt] )






				@slow
				@require_tf
				def 					_lowerCamelCase    (       self )    ->							Optional[int]:
					snake_case							=					pipeline('image-to-text',						model='ydshieh/vit-gpt2-coco-en',						framework='tf' )
					snake_case							=					'./tests/fixtures/tests_samples/COCO/000000039769.png'

					snake_case							=					pipe(lowercase_ )
					self.assertEqual(lowercase_,						[{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}] )

					snake_case							=					pipe([image, image] )
					self.assertEqual(
					    lowercase_,						[
					        [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
					        [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
					    ],						)





