

import inspect
import unittest
import warnings

from transformers import DeiTConfig
from transformers.models.auto import get_values
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch
						from torch import nn

						from transformers import (
						    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
						    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
						    MODEL_MAPPING,
						    DeiTForImageClassification,
						    DeiTForImageClassificationWithTeacher,
						    DeiTForMaskedImageModeling,
						    DeiTModel,
						)
						from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import DeiTImageProcessor



class 					lowercase						:


							def __init__(			self    ,			_snake_case    ,			_snake_case=13    ,			_snake_case=30    ,			_snake_case=2    ,			_snake_case=3    ,			_snake_case=True    ,			_snake_case=True    ,			_snake_case=32    ,			_snake_case=5    ,			_snake_case=4    ,			_snake_case=37    ,			_snake_case="gelu"    ,			_snake_case=0.1    ,			_snake_case=0.1    ,			_snake_case=10    ,			_snake_case=0.02    ,			_snake_case=3    ,			_snake_case=None    ,			_snake_case=2    ,			)							->				Tuple:
								UpperCAmelCase_		:      Union[str, Any]       	=					parent
								UpperCAmelCase_		:      Any       	=					batch_size
								UpperCAmelCase_		:      int       	=					image_size
								UpperCAmelCase_		:      Any       	=					patch_size
								UpperCAmelCase_		:      Optional[int]       	=					num_channels
								UpperCAmelCase_		:      str       	=					is_training
								UpperCAmelCase_		:      Optional[Any]       	=					use_labels
								UpperCAmelCase_		:      Optional[Any]       	=					hidden_size
								UpperCAmelCase_		:      Optional[Any]       	=					num_hidden_layers
								UpperCAmelCase_		:      Dict       	=					num_attention_heads
								UpperCAmelCase_		:      Any       	=					intermediate_size
								UpperCAmelCase_		:      List[str]       	=					hidden_act
								UpperCAmelCase_		:      Dict       	=					hidden_dropout_prob
								UpperCAmelCase_		:      str       	=					attention_probs_dropout_prob
								UpperCAmelCase_		:      Optional[Any]       	=					type_sequence_label_size
								UpperCAmelCase_		:      Dict       	=					initializer_range
								UpperCAmelCase_		:      Tuple       	=					scope
								UpperCAmelCase_		:      List[str]       	=					encoder_stride

								# in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
								UpperCAmelCase_		:      Tuple       	=					(image_size // patch_size) ** 2
								UpperCAmelCase_		:      str       	=					num_patches + 2


							def 						_snake_case							(			self)							->				int:
								UpperCAmelCase_		:      Tuple       	=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

								UpperCAmelCase_		:      Optional[Any]       	=					None
								if self.use_labels:
									UpperCAmelCase_		:      int       	=					ids_tensor([self.batch_size]    ,			self.type_sequence_label_size)

								UpperCAmelCase_		:      Union[str, Any]       	=					self.get_config()

								return config, pixel_values, labels


							def 						_snake_case							(			self)							->				str:
								return DeiTConfig(
								    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=_A    ,			initializer_range=self.initializer_range    ,			encoder_stride=self.encoder_stride    ,			)


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Optional[Any]:
								UpperCAmelCase_		:      int       	=					DeiTModel(config=_A)
								model.to(_A)
								model.eval()
								UpperCAmelCase_		:      Optional[int]       	=					model(_A)
								self.parent.assertEqual(result.last_hidden_state.shape    ,			(self.batch_size, self.seq_length, self.hidden_size))


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Optional[int]:
								UpperCAmelCase_		:      Optional[int]       	=					DeiTForMaskedImageModeling(config=_A)
								model.to(_A)
								model.eval()
								UpperCAmelCase_		:      List[str]       	=					model(_A)
								self.parent.assertEqual(
								    result.reconstruction.shape    ,			(self.batch_size, self.num_channels, self.image_size, self.image_size))

								# test greyscale images
								UpperCAmelCase_		:      List[str]       	=					1
								UpperCAmelCase_		:      Dict       	=					DeiTForMaskedImageModeling(_A)
								model.to(_A)
								model.eval()

								UpperCAmelCase_		:      Tuple       	=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
								UpperCAmelCase_		:      Any       	=					model(_A)
								self.parent.assertEqual(result.reconstruction.shape    ,			(self.batch_size, 1, self.image_size, self.image_size))


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case)							->				Optional[Any]:
								UpperCAmelCase_		:      Union[str, Any]       	=					self.type_sequence_label_size
								UpperCAmelCase_		:      List[Any]       	=					DeiTForImageClassification(_A)
								model.to(_A)
								model.eval()
								UpperCAmelCase_		:      str       	=					model(_A    ,			labels=_A)
								self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.type_sequence_label_size))

								# test greyscale images
								UpperCAmelCase_		:      Any       	=					1
								UpperCAmelCase_		:      Optional[Any]       	=					DeiTForImageClassification(_A)
								model.to(_A)
								model.eval()

								UpperCAmelCase_		:      Dict       	=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
								UpperCAmelCase_		:      int       	=					model(_A    ,			labels=_A)
								self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.type_sequence_label_size))





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



@require_torch
class 					lowercase						(  __lowercase,					__lowercase,					unittest.TestCase ):
							_lowerCamelCase		:      Optional[Any]=   (
							    (
							        DeiTModel,
							        DeiTForImageClassification,
							        DeiTForImageClassificationWithTeacher,
							        DeiTForMaskedImageModeling,
							    )
							    if is_torch_available()
							    else ()
							)
							_lowerCamelCase		:      Optional[int]=   (
							    {
							        "feature-extraction": DeiTModel,
							        "image-classification": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
							    }
							    if is_torch_available()
							    else {}
							)

							_lowerCamelCase		:      Any=   False
							_lowerCamelCase		:      Optional[int]=   False
							_lowerCamelCase		:      Optional[int]=   False


							def 						_snake_case							(			self)							->				Tuple:
								UpperCAmelCase_		:      Union[str, Any]       	=					DeiTModelTester(self)
								UpperCAmelCase_		:      List[Any]       	=					ConfigTester(self    ,			config_class=_A    ,			has_text_modality=_A    ,			hidden_size=37)


							def 						_snake_case							(			self)							->				Optional[int]:
								self.config_tester.run_common_tests()


							@unittest.skip(reason='DeiT does not use inputs_embeds')
							def 						_snake_case							(			self)							->				Union[str, Any]:
								pass


							def 						_snake_case							(			self)							->				List[str]:
								UpperCAmelCase_	,    UpperCAmelCase_		:      Any       	=					self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
									UpperCAmelCase_		:      Tuple       	=					model_class(_A)
									self.assertIsInstance(model.get_input_embeddings()    ,			(nn.Module))
									UpperCAmelCase_		:      Union[str, Any]       	=					model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(_A    ,			nn.Linear))


							def 						_snake_case							(			self)							->				Tuple:
								UpperCAmelCase_	,    UpperCAmelCase_		:      int       	=					self.model_tester.prepare_config_and_inputs_for_common()

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

									UpperCAmelCase_		:      Tuple       	=					['pixel_values']
									self.assertListEqual(arg_names[:1]    ,			_A)


							def 						_snake_case							(			self)							->				Union[str, Any]:
								UpperCAmelCase_		:      Optional[Any]       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_A)


							def 						_snake_case							(			self)							->				Any:
								UpperCAmelCase_		:      Tuple       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*_A)


							def 						_snake_case							(			self)							->				Tuple:
								UpperCAmelCase_		:      List[Any]       	=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_A)


							def 						_snake_case							(			self    ,			_snake_case    ,			_snake_case    ,			_snake_case=False)							->				Tuple:
								UpperCAmelCase_		:      Optional[int]       	=					super()._prepare_for_class(_A    ,			_A    ,			return_labels=_A)

								if return_labels:
									if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
										del inputs_dict["labels"]

								return inputs_dict


							def 						_snake_case							(			self)							->				List[Any]:
								if not self.model_tester.is_training:
									return

								UpperCAmelCase_	,    UpperCAmelCase_		:      Tuple       	=					self.model_tester.prepare_config_and_inputs_for_common()
								UpperCAmelCase_		:      List[str]       	=					True

								for model_class in self.all_model_classes:
									# DeiTForImageClassificationWithTeacher supports inference-only
									if (
									    model_class in get_values(_A)
									    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
									):
										continue
									UpperCAmelCase_		:      Union[str, Any]       	=					model_class(_A)
									model.to(_A)
									model.train()
									UpperCAmelCase_		:      Any       	=					self._prepare_for_class(_A    ,			_A    ,			return_labels=_A)
									UpperCAmelCase_		:      List[Any]       	=					model(**_A).loss
									loss.backward()


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

								UpperCAmelCase_		:      List[str]       	=					False
								UpperCAmelCase_		:      Tuple       	=					True

								for model_class in self.all_model_classes:
									if model_class in get_values(_A) or not model_class.supports_gradient_checkpointing:
										continue
									# DeiTForImageClassificationWithTeacher supports inference-only
									if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
										continue
									UpperCAmelCase_		:      Union[str, Any]       	=					model_class(_A)
									model.gradient_checkpointing_enable()
									model.to(_A)
									model.train()
									UpperCAmelCase_		:      Optional[int]       	=					self._prepare_for_class(_A    ,			_A    ,			return_labels=_A)
									UpperCAmelCase_		:      Dict       	=					model(**_A).loss
									loss.backward()


							def 						_snake_case							(			self)							->				List[Any]:
								UpperCAmelCase_	,    UpperCAmelCase_		:      Any       	=					self.model_tester.prepare_config_and_inputs_for_common()

								UpperCAmelCase_		:      str       	=					[
								    {'title': 'multi_label_classification', 'num_labels': 2, 'dtype': torch.float},
								    {'title': 'single_label_classification', 'num_labels': 1, 'dtype': torch.long},
								    {'title': 'regression', 'num_labels': 1, 'dtype': torch.float},
								]

								for model_class in self.all_model_classes:
									if (
									    model_class
									    not in [
									        *get_values(_A),
									        *get_values(_A),
									    ]
									    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
									):
										continue

									for problem_type in problem_types:
										with self.subTest(msg=F"""Testing {model_class} with {problem_type["title"]}"""):
											UpperCAmelCase_		:      Any       	=					problem_type['title']
											UpperCAmelCase_		:      List[Any]       	=					problem_type['num_labels']

											UpperCAmelCase_		:      List[Any]       	=					model_class(_A)
											model.to(_A)
											model.train()

											UpperCAmelCase_		:      Any       	=					self._prepare_for_class(_A    ,			_A    ,			return_labels=_A)

											if problem_type["num_labels"] > 1:
												UpperCAmelCase_		:      Tuple       	=					inputs['labels'].unsqueeze(1).repeat(1    ,			problem_type['num_labels'])

											UpperCAmelCase_		:      Any       	=					inputs['labels'].to(problem_type['dtype'])

											# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
											# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
											# they have the same size." which is a symptom something in wrong for the regression problem.
											# See https://github.com/huggingface/transformers/issues/11780
											with warnings.catch_warnings(record=_A) as warning_list:
												UpperCAmelCase_		:      List[str]       	=					model(**_A).loss
											for w in warning_list:
												if "Using a target size that is different to the input size" in str(w.message):
													raise ValueError(
													    F"""Something is going wrong in the regression problem: intercepted {w.message}""")

											loss.backward()





							@slow
							def 						_snake_case							(			self)							->				List[Any]:
								for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									UpperCAmelCase_		:      Any       	=					DeiTModel.from_pretrained(_A)
									self.assertIsNotNone(_A)







def 						SCREAMING_SNAKE_CASE(  )    ->    str:
	UpperCAmelCase_		:      Optional[int]       	=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
	return image







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


							@cached_property
							def 						_snake_case							(			self)							->				Optional[Any]:
								return (
								    DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224')
								    if is_vision_available()
								    else None
								)


							@slow
							def 						_snake_case							(			self)							->				List[Any]:
								UpperCAmelCase_		:      str       	=					DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224').to(
								    _A)

								UpperCAmelCase_		:      Any       	=					self.default_image_processor
								UpperCAmelCase_		:      Optional[int]       	=					prepare_img()
								UpperCAmelCase_		:      str       	=					image_processor(images=_A    ,			return_tensors='pt').to(_A)

								# forward pass
								with torch.no_grad():
									UpperCAmelCase_		:      Union[str, Any]       	=					model(**_A)

								# verify the logits
								UpperCAmelCase_		:      Union[str, Any]       	=					torch.Size((1, 1000))
								self.assertEqual(outputs.logits.shape    ,			_A)

								UpperCAmelCase_		:      List[Any]       	=					torch.tensor([-1.0_266, 0.1_912, -1.2_861]).to(_A)

								self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,			_A    ,			atol=1e-4))





							@slow
							@require_accelerate
							@require_torch_gpu
							def 						_snake_case							(			self)							->				Optional[Any]:
								UpperCAmelCase_		:      Dict       	=					DeiTModel.from_pretrained(
								    'facebook/deit-base-distilled-patch16-224'    ,			torch_dtype=torch.floataa    ,			device_map='auto')
								UpperCAmelCase_		:      Dict       	=					self.default_image_processor

								UpperCAmelCase_		:      int       	=					prepare_img()
								UpperCAmelCase_		:      int       	=					image_processor(images=_A    ,			return_tensors='pt')
								UpperCAmelCase_		:      List[str]       	=					inputs.pixel_values.to(_A)

								# forward pass to make sure inference works in fp16
								with torch.no_grad():
									UpperCAmelCase_		:      Dict       	=					model(_A)









'''simple docstring'''


import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
from .feature_extraction_wavaveca import WavaVecaFeatureExtractor
from .tokenization_wavaveca import WavaVecaCTCTokenizer



class 					lowercase						(  a_ ):
							_lowerCamelCase		:      List[str]=   "Wav2Vec2FeatureExtractor"
							_lowerCamelCase		:      Any=   "AutoTokenizer"


							def __init__(			self    ,			_snake_case    ,			_snake_case)							->				List[str]:
								super().__init__(_snake_case    ,			_snake_case)
								UpperCAmelCase_		:      int       	=					self.feature_extractor
								UpperCAmelCase_		:      str       	=					False


							@classmethod
							def 						_snake_case							(			cls    ,			_snake_case    ,			**_snake_case)							->				List[Any]:
								try:
									return super().from_pretrained(_snake_case    ,			**_snake_case)
								except OSError:
									warnings.warn(
									    F"""Loading a tokenizer inside {cls.__name__} from a config that does not"""
									    ' include a `tokenizer_class` attribute is deprecated and will be '
									    'removed in v5. Please add `\'tokenizer_class\': \'Wav2Vec2CTCTokenizer\'`'
									    ' attribute to either your `config.json` or `tokenizer_config.json` '
									    'file to suppress this warning: '    ,			_snake_case    ,			)

									UpperCAmelCase_		:      Any       	=					WavaVecaFeatureExtractor.from_pretrained(_snake_case    ,			**_snake_case)
									UpperCAmelCase_		:      List[str]       	=					WavaVecaCTCTokenizer.from_pretrained(_snake_case    ,			**_snake_case)

									return cls(feature_extractor=_snake_case    ,			tokenizer=_snake_case)


							def __call__(			self    ,			*_snake_case    ,			**_snake_case)							->				Optional[Any]:
								# For backward compatibility
								if self._in_target_context_manager:
									return self.current_processor(*_snake_case    ,			**_snake_case)

								if "raw_speech" in kwargs:
									warnings.warn('Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.')
									UpperCAmelCase_		:      List[str]       	=					kwargs.pop('raw_speech')
								else:
									UpperCAmelCase_		:      Dict       	=					kwargs.pop('audio'    ,			_snake_case)
								UpperCAmelCase_		:      str       	=					kwargs.pop('sampling_rate'    ,			_snake_case)
								UpperCAmelCase_		:      List[str]       	=					kwargs.pop('text'    ,			_snake_case)
								if len(_snake_case) > 0:
									UpperCAmelCase_		:      List[str]       	=					args[0]
									UpperCAmelCase_		:      Tuple       	=					args[1:]

								if audio is None and text is None:
									raise ValueError('You need to specify either an `audio` or `text` input to process.')

								if audio is not None:
									UpperCAmelCase_		:      Union[str, Any]       	=					self.feature_extractor(_snake_case    ,			*_snake_case    ,			sampling_rate=_snake_case    ,			**_snake_case)
								if text is not None:
									UpperCAmelCase_		:      Optional[int]       	=					self.tokenizer(_snake_case    ,			**_snake_case)

								if text is None:
									return inputs
								elif audio is None:
									return encodings
								else:
									UpperCAmelCase_		:      List[str]       	=					encodings['input_ids']
									return inputs


							def 						_snake_case							(			self    ,			*_snake_case    ,			**_snake_case)							->				str:
								# For backward compatibility
								if self._in_target_context_manager:
									return self.current_processor.pad(*_snake_case    ,			**_snake_case)

								UpperCAmelCase_		:      Union[str, Any]       	=					kwargs.pop('input_features'    ,			_snake_case)
								UpperCAmelCase_		:      Dict       	=					kwargs.pop('labels'    ,			_snake_case)
								if len(_snake_case) > 0:
									UpperCAmelCase_		:      Optional[Any]       	=					args[0]
									UpperCAmelCase_		:      int       	=					args[1:]

								if input_features is not None:
									UpperCAmelCase_		:      Dict       	=					self.feature_extractor.pad(_snake_case    ,			*_snake_case    ,			**_snake_case)
								if labels is not None:
									UpperCAmelCase_		:      Union[str, Any]       	=					self.tokenizer.pad(_snake_case    ,			**_snake_case)

								if labels is None:
									return input_features
								elif input_features is None:
									return labels
								else:
									UpperCAmelCase_		:      Union[str, Any]       	=					labels['input_ids']
									return input_features


							def 						_snake_case							(			self    ,			*_snake_case    ,			**_snake_case)							->				Dict:
								return self.tokenizer.batch_decode(*_snake_case    ,			**_snake_case)


							def 						_snake_case							(			self    ,			*_snake_case    ,			**_snake_case)							->				Tuple:
								return self.tokenizer.decode(*_snake_case    ,			**_snake_case)





							@contextmanager
							def 						_snake_case							(			self)							->				Tuple:
								warnings.warn(
								    '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '
								    'labels by using the argument `text` of the regular `__call__` method (either in the same call as '
								    'your audio inputs, or in a separate call.')
								UpperCAmelCase_		:      Optional[Any]       	=					True
								UpperCAmelCase_		:      Tuple       	=					self.tokenizer
								yield
								UpperCAmelCase_		:      Tuple       	=					self.feature_extractor
								UpperCAmelCase_		:      Tuple       	=					False






