






'''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  _SCREAMING_SNAKE_CASE				:


			'''simple docstring'''





			def __init__( self				:    List[Any]			,   lowercase				:    int			,   lowercase				:    Tuple=1_0_0			,   lowercase				:    str=1_3			,   lowercase				:    Union[str, Any]=3_0			,   lowercase				:    int=2			,   lowercase				:    int=3			,   lowercase				:    Optional[int]=True			,   lowercase				:    Optional[Any]=True			,   lowercase				:    List[str]=3_2			,   lowercase				:    Tuple=4			,   lowercase				:    Optional[Any]=4			,   lowercase				:    Optional[int]=3_7			,   lowercase				:    Dict="gelu"			,   lowercase				:    Any=0.1			,   lowercase				:    List[Any]=0.1			,   lowercase				:    int=1_0			,   lowercase				:    List[str]=0.0_2			,   lowercase				:    Tuple=3			,   lowercase				:    int=None			,   lowercase				:    List[Any]=[0, 1, 2, 3]			,   )				->     int:

									'''simple docstring'''







									UpperCamelCase__       =			parent
									UpperCamelCase__       =			1_0_0
									UpperCamelCase__       =			batch_size
									UpperCamelCase__       =			image_size
									UpperCamelCase__       =			patch_size
									UpperCamelCase__       =			num_channels
									UpperCamelCase__       =			is_training
									UpperCamelCase__       =			use_labels
									UpperCamelCase__       =			hidden_size
									UpperCamelCase__       =			num_hidden_layers
									UpperCamelCase__       =			num_attention_heads
									UpperCamelCase__       =			intermediate_size
									UpperCamelCase__       =			hidden_act
									UpperCamelCase__       =			hidden_dropout_prob
									UpperCamelCase__       =			attention_probs_dropout_prob
									UpperCamelCase__       =			type_sequence_label_size
									UpperCamelCase__       =			initializer_range
									UpperCamelCase__       =			scope
									UpperCamelCase__       =			out_indices
									UpperCamelCase__       =			num_labels

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






			def 					A   ( self				:    Any      )				->     Any:

									'''simple docstring'''







									UpperCamelCase__       =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

									UpperCamelCase__       =			self.get_config()

									return config, pixel_values, labels, pixel_labels






			def 					A   ( self				:    Dict      )				->     Optional[Any]:

									'''simple docstring'''







									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 					A   ( self				:    List[str]			,   lowercase				:    Optional[Any]			,   lowercase				:    List[str]			,   lowercase				:    Optional[Any]			,   lowercase				:    List[str]      )				->     Tuple:

									'''simple docstring'''







									UpperCamelCase__       =			DataaVecVisionModel(config=lowercase      )
									model.to(lowercase      )
									model.eval()
									UpperCamelCase__       =			model(lowercase      )
									# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
									UpperCamelCase__       =			(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 					A   ( self				:    Dict			,   lowercase				:    List[Any]			,   lowercase				:    Union[str, Any]			,   lowercase				:    int			,   lowercase				:    Optional[Any]      )				->     List[str]:

									'''simple docstring'''







									UpperCamelCase__       =			self.type_sequence_label_size
									UpperCamelCase__       =			DataaVecVisionForImageClassification(lowercase      )
									model.to(lowercase      )
									model.eval()
									UpperCamelCase__       =			model(lowercase			,   labels=lowercase      )
									self.parent.assertEqual(result.logits.shape			,   (self.batch_size, self.type_sequence_label_size)      )






			def 					A   ( self				:    Union[str, Any]			,   lowercase				:    str			,   lowercase				:    int			,   lowercase				:    Optional[int]			,   lowercase				:    str      )				->     Tuple:

									'''simple docstring'''







									UpperCamelCase__       =			self.num_labels
									UpperCamelCase__       =			DataaVecVisionForSemanticSegmentation(lowercase      )
									model.to(lowercase      )
									model.eval()
									UpperCamelCase__       =			model(lowercase      )
									self.parent.assertEqual(
									    result.logits.shape			,   (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)      )
									UpperCamelCase__       =			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 					A   ( self				:    str      )				->     Union[str, Any]:

									'''simple docstring'''







									UpperCamelCase__       =			self.prepare_config_and_inputs()
									UpperCamelCase__     , UpperCamelCase__     , UpperCamelCase__     , UpperCamelCase__       =			config_and_inputs
									UpperCamelCase__       =			{"""pixel_values""": pixel_values}
									return config, inputs_dict
@require_torch
class  _SCREAMING_SNAKE_CASE				(		SCREAMING_SNAKE_CASE							,SCREAMING_SNAKE_CASE							,unittest.TestCase						):


			'''simple docstring'''





			__a				:				str         =       (
			    (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
			    if is_torch_available()
			    else ()
			)
			__a				:				Union[str, Any]         =       (
			    {
			        "feature-extraction": DataaVecVisionModel,
			        "image-classification": DataaVecVisionForImageClassification,
			        "image-segmentation": DataaVecVisionForSemanticSegmentation,
			    }
			    if is_torch_available()
			    else {}
			)

			__a				:				Union[str, Any]         =       False
			__a				:				int         =       False
			__a				:				int         =       False






			def 					A   ( self				:    Optional[int]      )				->     Optional[int]:

									'''simple docstring'''







									UpperCamelCase__       =			DataaVecVisionModelTester(self      )
									UpperCamelCase__       =			ConfigTester(
									    self			,   config_class=lowercase			,   has_text_modality=lowercase			,   hidden_size=3_7      )






			def 					A   ( self				:    List[str]      )				->     List[Any]:

									'''simple docstring'''







									self.config_tester.run_common_tests()






			def 					A   ( self				:    Tuple      )				->     Optional[int]:

									'''simple docstring'''







									pass






			@require_torch_multi_gpu
			@unittest.skip(
			    reason="""Data2VecVision has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""      )
			def 					A   ( self				:    int      )				->     List[Any]:

									'''simple docstring'''







									pass






			def 					A   ( self				:    Tuple      )				->     Optional[int]:

									'''simple docstring'''







									UpperCamelCase__     , UpperCamelCase__       =			self.model_tester.prepare_config_and_inputs_for_common()

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






			def 					A   ( self				:    int      )				->     List[str]:

									'''simple docstring'''







									UpperCamelCase__     , UpperCamelCase__       =			self.model_tester.prepare_config_and_inputs_for_common()

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

															UpperCamelCase__       =			["""pixel_values"""]
															self.assertListEqual(arg_names[:1]			,   lowercase      )






			def 					A   ( self				:    str      )				->     Optional[int]:

									'''simple docstring'''







									UpperCamelCase__       =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowercase      )






			def 					A   ( self				:    List[str]      )				->     int:

									'''simple docstring'''







									UpperCamelCase__       =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_segmentation(*lowercase      )






			def 					A   ( self				:    str      )				->     Dict:

									'''simple docstring'''







									if not self.model_tester.is_training:
															return

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

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

															UpperCamelCase__       =			model_class(lowercase      )
															model.to(lowercase      )
															model.train()
															UpperCamelCase__       =			self._prepare_for_class(lowercase			,   lowercase			,   return_labels=lowercase      )
															UpperCamelCase__       =			model(**lowercase      ).loss
															loss.backward()






			def 					A   ( self				:    str      )				->     Optional[Any]:

									'''simple docstring'''







									UpperCamelCase__     , UpperCamelCase__       =			self.model_tester.prepare_config_and_inputs_for_common()
									if not self.model_tester.is_training:
															return

									UpperCamelCase__       =			False
									UpperCamelCase__       =			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":
																					UpperCamelCase__     , UpperCamelCase__     , UpperCamelCase__     , UpperCamelCase__       =			inputs_dict["""pixel_values"""].shape
																					UpperCamelCase__       =			torch.zeros(
																					    [self.model_tester.batch_size, height, width]			,   device=lowercase      ).long()
															UpperCamelCase__       =			model_class(lowercase      )
															model.gradient_checkpointing_enable()
															model.to(lowercase      )
															model.train()
															UpperCamelCase__       =			self._prepare_for_class(lowercase			,   lowercase			,   return_labels=lowercase      )
															UpperCamelCase__       =			model(**lowercase      ).loss
															loss.backward()






			def 					A   ( self				:    List[str]      )				->     List[str]:

									'''simple docstring'''







									UpperCamelCase__     , UpperCamelCase__       =			self.model_tester.prepare_config_and_inputs_for_common()

									UpperCamelCase__       =			_config_zero_init(lowercase      )
									for model_class in self.all_model_classes:
															UpperCamelCase__       =			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 					A   ( self				:    Any			,   lowercase				:    List[str]			,   lowercase				:    int			,   lowercase				:    List[Any]			,   lowercase				:    Dict=2e-4			,   lowercase				:    str="outputs"			,   lowercase				:    Tuple=None      )				->     List[str]:

									'''simple docstring'''







									super().check_pt_tf_outputs(lowercase			,   lowercase			,   lowercase			,   lowercase			,   lowercase			,   lowercase      )






			def 					A   ( self				:    Any      )				->     List[Any]:

									'''simple docstring'''







									UpperCamelCase__       =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*lowercase      )







			@slow
			def 					A   ( self				:    List[Any]      )				->     Union[str, Any]:

									'''simple docstring'''







									for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															UpperCamelCase__       =			DataaVecVisionModel.from_pretrained(lowercase      )
															self.assertIsNotNone(lowercase      )







def 						__magic_name__(			):
						'''simple docstring'''





						UpperCamelCase__       =			Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
						return image







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


			'''simple docstring'''





			@cached_property
			def 					A   ( self				:    Dict      )				->     Dict:

									'''simple docstring'''







									return (
									    BeitImageProcessor.from_pretrained("""facebook/data2vec-vision-base-ft1k"""      ) if is_vision_available() else None
									)







			@slow
			def 					A   ( self				:    Any      )				->     Dict:

									'''simple docstring'''







									UpperCamelCase__       =			DataaVecVisionForImageClassification.from_pretrained("""facebook/data2vec-vision-base-ft1k"""      ).to(
									    lowercase      )

									UpperCamelCase__       =			self.default_image_processor
									UpperCamelCase__       =			prepare_img()
									UpperCamelCase__       =			image_processor(images=lowercase			,   return_tensors="""pt"""      ).to(lowercase      )

									# forward pass
									with torch.no_grad():
															UpperCamelCase__       =			model(**lowercase      )
									UpperCamelCase__       =			outputs.logits

									# verify the logits
									UpperCamelCase__       =			torch.Size((1, 1_0_0_0)      )
									self.assertEqual(logits.shape			,   lowercase      )

									UpperCamelCase__       =			torch.tensor([0.3_2_7_7, -0.1_3_9_5, 0.0_9_1_1]      ).to(lowercase      )

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

									UpperCamelCase__       =			[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 json
import os
import shutil
import tempfile
import unittest

from transformers import is_speech_available, is_torch_available
from transformers.models.speechta import SpeechTaTokenizer
from transformers.testing_utils import get_tests_dir, require_torch
from transformers.utils import FEATURE_EXTRACTOR_NAME


if is_speech_available() and is_torch_available():
					from transformers import SpeechTaFeatureExtractor, SpeechTaProcessor

					from .test_feature_extraction_speechta import floats_list


lowerCamelCase_					:			List[str]     						=				get_tests_dir('''fixtures/test_sentencepiece_bpe_char.model''')
@require_torch
class  _SCREAMING_SNAKE_CASE				(		unittest.TestCase						):


			'''simple docstring'''





			def 					A   ( self				:    Union[str, Any]      )				->     Union[str, Any]:

									'''simple docstring'''







									UpperCamelCase__       =			tempfile.mkdtemp()

									UpperCamelCase__       =			SpeechTaTokenizer(lowercase      )
									tokenizer.save_pretrained(self.tmpdirname      )

									UpperCamelCase__       =			{
									    """feature_size""": 1,
									    """padding_value""": 0.0,
									    """sampling_rate""": 1_6_0_0_0,
									    """do_normalize""": False,
									    """num_mel_bins""": 8_0,
									    """hop_length""": 1_6,
									    """win_length""": 6_4,
									    """win_function""": """hann_window""",
									    """fmin""": 8_0,
									    """fmax""": 7_6_0_0,
									    """mel_floor""": 1e-10,
									    """reduction_factor""": 2,
									    """return_attention_mask""": True,
									}

									UpperCamelCase__       =			os.path.join(self.tmpdirname			,   lowercase      )
									with open(self.feature_extraction_file			,   """w"""			,   encoding="""utf-8"""      ) as fp:
															fp.write(json.dumps(lowercase      ) + """\n"""      )






			def 					A   ( self				:    Union[str, Any]			,   **lowercase				:    List[Any]      )				->     Any:

									'''simple docstring'''







									return SpeechTaTokenizer.from_pretrained(self.tmpdirname			,   **lowercase      )






			def 					A   ( self				:    str			,   **lowercase				:    Tuple      )				->     Optional[Any]:

									'''simple docstring'''







									return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname			,   **lowercase      )






			def 					A   ( self				:    Union[str, Any]      )				->     str:

									'''simple docstring'''







									shutil.rmtree(self.tmpdirname      )






			def 					A   ( self				:    List[Any]      )				->     Optional[int]:

									'''simple docstring'''







									UpperCamelCase__       =			self.get_tokenizer()
									UpperCamelCase__       =			self.get_feature_extractor()

									UpperCamelCase__       =			SpeechTaProcessor(tokenizer=lowercase			,   feature_extractor=lowercase      )

									processor.save_pretrained(self.tmpdirname      )
									UpperCamelCase__       =			SpeechTaProcessor.from_pretrained(self.tmpdirname      )

									self.assertEqual(processor.tokenizer.get_vocab()			,   tokenizer.get_vocab()      )
									self.assertIsInstance(processor.tokenizer			,   lowercase      )

									self.assertEqual(processor.feature_extractor.to_json_string()			,   feature_extractor.to_json_string()      )
									self.assertIsInstance(processor.feature_extractor			,   lowercase      )






			def 					A   ( self				:    Tuple      )				->     Union[str, Any]:

									'''simple docstring'''







									UpperCamelCase__       =			SpeechTaProcessor(tokenizer=self.get_tokenizer()			,   feature_extractor=self.get_feature_extractor()      )
									processor.save_pretrained(self.tmpdirname      )

									UpperCamelCase__       =			self.get_tokenizer(bos_token="""(BOS)"""			,   eos_token="""(EOS)"""      )
									UpperCamelCase__       =			self.get_feature_extractor(do_normalize=lowercase			,   padding_value=1.0      )

									UpperCamelCase__       =			SpeechTaProcessor.from_pretrained(
									    self.tmpdirname			,   bos_token="""(BOS)"""			,   eos_token="""(EOS)"""			,   do_normalize=lowercase			,   padding_value=1.0      )

									self.assertEqual(processor.tokenizer.get_vocab()			,   tokenizer_add_kwargs.get_vocab()      )
									self.assertIsInstance(processor.tokenizer			,   lowercase      )

									self.assertEqual(processor.feature_extractor.to_json_string()			,   feature_extractor_add_kwargs.to_json_string()      )
									self.assertIsInstance(processor.feature_extractor			,   lowercase      )






			def 					A   ( self				:    List[Any]      )				->     Dict:

									'''simple docstring'''







									UpperCamelCase__       =			self.get_feature_extractor()
									UpperCamelCase__       =			self.get_tokenizer()

									UpperCamelCase__       =			SpeechTaProcessor(tokenizer=lowercase			,   feature_extractor=lowercase      )

									UpperCamelCase__       =			floats_list((3, 1_0_0_0)      )

									UpperCamelCase__       =			feature_extractor(audio=lowercase			,   return_tensors="""np"""      )
									UpperCamelCase__       =			processor(audio=lowercase			,   return_tensors="""np"""      )

									for key in input_feat_extract.keys():
															self.assertAlmostEqual(input_feat_extract[key].sum()			,   input_processor[key].sum()			,   delta=1e-2      )






			def 					A   ( self				:    List[str]      )				->     Optional[Any]:

									'''simple docstring'''







									UpperCamelCase__       =			self.get_feature_extractor()
									UpperCamelCase__       =			self.get_tokenizer()

									UpperCamelCase__       =			SpeechTaProcessor(tokenizer=lowercase			,   feature_extractor=lowercase      )

									UpperCamelCase__       =			floats_list((3, 1_0_0_0)      )

									UpperCamelCase__       =			feature_extractor(audio_target=lowercase			,   return_tensors="""np"""      )
									UpperCamelCase__       =			processor(audio_target=lowercase			,   return_tensors="""np"""      )

									for key in input_feat_extract.keys():
															self.assertAlmostEqual(input_feat_extract[key].sum()			,   input_processor[key].sum()			,   delta=1e-2      )






			def 					A   ( self				:    Optional[Any]      )				->     Optional[Any]:

									'''simple docstring'''







									UpperCamelCase__       =			self.get_feature_extractor()
									UpperCamelCase__       =			self.get_tokenizer()

									UpperCamelCase__       =			SpeechTaProcessor(tokenizer=lowercase			,   feature_extractor=lowercase      )

									UpperCamelCase__       =			"""This is a test string"""

									UpperCamelCase__       =			processor(text=lowercase      )
									UpperCamelCase__       =			tokenizer(lowercase      )

									for key in encoded_tok.keys():
															self.assertListEqual(encoded_tok[key]			,   encoded_processor[key]      )






			def 					A   ( self				:    List[Any]      )				->     int:

									'''simple docstring'''







									UpperCamelCase__       =			self.get_feature_extractor()
									UpperCamelCase__       =			self.get_tokenizer()

									UpperCamelCase__       =			SpeechTaProcessor(tokenizer=lowercase			,   feature_extractor=lowercase      )

									UpperCamelCase__       =			"""This is a test string"""

									UpperCamelCase__       =			processor(text_target=lowercase      )
									UpperCamelCase__       =			tokenizer(lowercase      )

									for key in encoded_tok.keys():
															self.assertListEqual(encoded_tok[key]			,   encoded_processor[key]      )






			def 					A   ( self				:    Any      )				->     Tuple:

									'''simple docstring'''







									UpperCamelCase__       =			self.get_feature_extractor()
									UpperCamelCase__       =			self.get_tokenizer()

									UpperCamelCase__       =			SpeechTaProcessor(tokenizer=lowercase			,   feature_extractor=lowercase      )

									UpperCamelCase__       =			[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

									UpperCamelCase__       =			processor.batch_decode(lowercase      )
									UpperCamelCase__       =			tokenizer.batch_decode(lowercase      )

									self.assertListEqual(lowercase			,   lowercase      )







			def 					A   ( self				:    List[Any]      )				->     List[str]:

									'''simple docstring'''







									UpperCamelCase__       =			self.get_feature_extractor()
									UpperCamelCase__       =			self.get_tokenizer()

									UpperCamelCase__       =			SpeechTaProcessor(tokenizer=lowercase			,   feature_extractor=lowercase      )

									self.assertListEqual(
									    processor.model_input_names			,   feature_extractor.model_input_names			,   msg="""`processor` and `feature_extractor` model input names do not match"""			,   )
