


'''simple docstring'''






import inspect
import unittest

from transformers import ViTConfig
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 ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
		from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import ViTImageProcessor




class 						a__							:





							'''simple docstring'''






							def __init__(						self   ,       lowerCamelCase_   ,       lowerCamelCase_=13   ,       lowerCamelCase_=30   ,       lowerCamelCase_=2   ,       lowerCamelCase_=3   ,       lowerCamelCase_=True   ,       lowerCamelCase_=True   ,       lowerCamelCase_=32   ,       lowerCamelCase_=5   ,       lowerCamelCase_=4   ,       lowerCamelCase_=37   ,       lowerCamelCase_="gelu"   ,       lowerCamelCase_=0.1   ,       lowerCamelCase_=0.1   ,       lowerCamelCase_=10   ,       lowerCamelCase_=0.02   ,       lowerCamelCase_=None   ,       lowerCamelCase_=2   ,       )			->				Any:
														lowerCAmelCase__			  =      parent
														lowerCAmelCase__			  =      batch_size
														lowerCAmelCase__			  =      image_size
														lowerCAmelCase__			  =      patch_size
														lowerCAmelCase__			  =      num_channels
														lowerCAmelCase__			  =      is_training
														lowerCAmelCase__			  =      use_labels
														lowerCAmelCase__			  =      hidden_size
														lowerCAmelCase__			  =      num_hidden_layers
														lowerCAmelCase__			  =      num_attention_heads
														lowerCAmelCase__			  =      intermediate_size
														lowerCAmelCase__			  =      hidden_act
														lowerCAmelCase__			  =      hidden_dropout_prob
														lowerCAmelCase__			  =      attention_probs_dropout_prob
														lowerCAmelCase__			  =      type_sequence_label_size
														lowerCAmelCase__			  =      initializer_range
														lowerCAmelCase__			  =      scope
														lowerCAmelCase__			  =      encoder_stride

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







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[Any]:
														lowerCAmelCase__			  =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

														lowerCAmelCase__			  =      None
														if self.use_labels:
																					lowerCAmelCase__			  =      ids_tensor([self.batch_size]   ,       self.type_sequence_label_size  )

														lowerCAmelCase__			  =      self.get_config()

														return config, pixel_values, labels







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														return ViTConfig(
														    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=lowerCamelCase_   ,       initializer_range=self.initializer_range   ,       encoder_stride=self.encoder_stride   ,       )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				List[str]:
														lowerCAmelCase__			  =      ViTModel(config=lowerCamelCase_  )
														model.to(lowerCamelCase_  )
														model.eval()
														lowerCAmelCase__			  =      model(lowerCamelCase_  )
														self.parent.assertEqual(result.last_hidden_state.shape   ,       (self.batch_size, self.seq_length, self.hidden_size)  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				str:
														lowerCAmelCase__			  =      ViTForMaskedImageModeling(config=lowerCamelCase_  )
														model.to(lowerCamelCase_  )
														model.eval()
														lowerCAmelCase__			  =      model(lowerCamelCase_  )
														self.parent.assertEqual(
														    result.reconstruction.shape   ,       (self.batch_size, self.num_channels, self.image_size, self.image_size)  )

														# test greyscale images
														lowerCAmelCase__			  =      1
														lowerCAmelCase__			  =      ViTForMaskedImageModeling(lowerCamelCase_  )
														model.to(lowerCamelCase_  )
														model.eval()

														lowerCAmelCase__			  =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
														lowerCAmelCase__			  =      model(lowerCamelCase_  )
														self.parent.assertEqual(result.reconstruction.shape   ,       (self.batch_size, 1, self.image_size, self.image_size)  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				Tuple:
														lowerCAmelCase__			  =      self.type_sequence_label_size
														lowerCAmelCase__			  =      ViTForImageClassification(lowerCamelCase_  )
														model.to(lowerCamelCase_  )
														model.eval()
														lowerCAmelCase__			  =      model(lowerCamelCase_   ,       labels=lowerCamelCase_  )
														self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.type_sequence_label_size)  )

														# test greyscale images
														lowerCAmelCase__			  =      1
														lowerCAmelCase__			  =      ViTForImageClassification(lowerCamelCase_  )
														model.to(lowerCamelCase_  )
														model.eval()

														lowerCAmelCase__			  =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
														lowerCAmelCase__			  =      model(lowerCamelCase_  )
														self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.type_sequence_label_size)  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      self.prepare_config_and_inputs()
														(
														    (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   
														)			  =      config_and_inputs
														lowerCAmelCase__			  =      {'''pixel_values''': pixel_values}
														return config, inputs_dict




@require_torch
class 						a__							(				a__							,							a__							,							unittest.TestCase    ):





							'''simple docstring'''






							lowercase__     :  Any      		= (
							    (
							        ViTModel,
							        ViTForImageClassification,
							        ViTForMaskedImageModeling,
							    )
							    if is_torch_available()
							    else ()
							)
							lowercase__     :  Tuple      		= (
							    {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification}
							    if is_torch_available()
							    else {}
							)
							lowercase__     :  Any      		= True

							lowercase__     :  Union[str, Any]      		= False
							lowercase__     :  Any      		= False
							lowercase__     :  Tuple      		= False







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				int:
														lowerCAmelCase__			  =      ViTModelTester(self  )
														lowerCAmelCase__			  =      ConfigTester(self   ,       config_class=lowerCamelCase_   ,       has_text_modality=lowerCamelCase_   ,       hidden_size=37  )







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







							@unittest.skip(reason='''ViT does not use inputs_embeds'''  )
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[Any]:
														pass







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__  ,   lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs_for_common()

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







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[str]:
														lowerCAmelCase__  ,   lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs_for_common()

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

																					lowerCAmelCase__			  =      ['''pixel_values''']
																					self.assertListEqual(arg_names[:1]   ,       lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				str:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[Any]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_  )







							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCAmelCase__			  =      ViTModel.from_pretrained(lowerCamelCase_  )
																					self.assertIsNotNone(lowerCamelCase_  )


def    _snake_case  (	)		->       List[Any]:
							lowerCAmelCase__			  =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
							return image






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





							'''simple docstring'''






							@cached_property
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[str]:
														return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''  ) if is_vision_available() else None







							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Tuple:
														lowerCAmelCase__			  =      ViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''  ).to(lowerCamelCase_  )

														lowerCAmelCase__			  =      self.default_image_processor
														lowerCAmelCase__			  =      prepare_img()
														lowerCAmelCase__			  =      image_processor(images=lowerCamelCase_   ,       return_tensors='''pt'''  ).to(lowerCamelCase_  )

														# forward pass
														with torch.no_grad():
																					lowerCAmelCase__			  =      model(**lowerCamelCase_  )

														# verify the logits
														lowerCAmelCase__			  =      torch.Size((1, 10_00)  )
														self.assertEqual(outputs.logits.shape   ,       lowerCamelCase_  )

														lowerCAmelCase__			  =      torch.tensor([-0.2_744, 0.8_215, -0.0_836]  ).to(lowerCamelCase_  )

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







							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				str:
														# ViT models have an `interpolate_pos_encoding` argument in their forward method,
														# allowing to interpolate the pre-trained position embeddings in order to use
														# the model on higher resolutions. The DINO model by Facebook AI leverages this
														# to visualize self-attention on higher resolution images.
														lowerCAmelCase__			  =      ViTModel.from_pretrained('''facebook/dino-vits8'''  ).to(lowerCamelCase_  )

														lowerCAmelCase__			  =      ViTImageProcessor.from_pretrained('''facebook/dino-vits8'''   ,       size=4_80  )
														lowerCAmelCase__			  =      prepare_img()
														lowerCAmelCase__			  =      image_processor(images=lowerCamelCase_   ,       return_tensors='''pt'''  )
														lowerCAmelCase__			  =      inputs.pixel_values.to(lowerCamelCase_  )

														# forward pass
														with torch.no_grad():
																					lowerCAmelCase__			  =      model(lowerCamelCase_   ,       interpolate_pos_encoding=lowerCamelCase_  )

														# verify the logits
														lowerCAmelCase__			  =      torch.Size((1, 36_01, 3_84)  )
														self.assertEqual(outputs.last_hidden_state.shape   ,       lowerCamelCase_  )

														lowerCAmelCase__			  =      torch.tensor(
														    [[4.2_340, 4.3_906, -6.6_692], [4.5_463, 1.8_928, -6.7_257], [4.4_429, 0.8_496, -5.8_585]]  ).to(lowerCamelCase_  )

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







							@slow
							@require_accelerate
							@require_torch_gpu
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				str:
														lowerCAmelCase__			  =      ViTModel.from_pretrained('''facebook/dino-vits8'''   ,       torch_dtype=torch.floataa   ,       device_map='''auto'''  )
														lowerCAmelCase__			  =      self.default_image_processor

														lowerCAmelCase__			  =      prepare_img()
														lowerCAmelCase__			  =      image_processor(images=lowerCamelCase_   ,       return_tensors='''pt'''  )
														lowerCAmelCase__			  =      inputs.pixel_values.to(lowerCamelCase_  )

														# forward pass to make sure inference works in fp16
														with torch.no_grad():
																					lowerCAmelCase__			  =      model(lowerCamelCase_  )






'''simple docstring'''






import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase									=   logging.get_logger(__name__)

lowerCAmelCase									=   {
    """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""",
    # See all SEW-D models at https://huggingface.co/models?filter=sew-d
}
class 		lowerCamelCase	(   _A							):
	snake_case_   = "sew-d"



	def __init__( self		,   a_=32		,   a_=768		,   a_=12		,   a_=12		,   a_=3_072		,   a_=2		,   a_=512		,   a_=256		,   a_=True		,   a_=True		,   a_=("p2c", "c2p")		,   a_="layer_norm"		,   a_="gelu_python"		,   a_=0.1		,   a_=0.1		,   a_=0.1		,   a_=0.0		,   a_=0.1		,   a_=0.02		,   a_=1e-7		,   a_=1e-5		,   a_="group"		,   a_="gelu"		,   a_=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)		,   a_=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)		,   a_=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)		,   a_=False		,   a_=128		,   a_=16		,   a_=True		,   a_=0.05		,   a_=10		,   a_=2		,   a_=0.0		,   a_=10		,   a_=0		,   a_="mean"		,   a_=False		,   a_=False		,   a_=256		,   a_=0		,   a_=1		,   a_=2		,   **a_		,   ):
			super().__init__(**a_		,   pad_token_id=a_		,   bos_token_id=a_		,   eos_token_id=a_						)
			lowerCAmelCase  :     Optional[int]       						= hidden_size
			lowerCAmelCase  :     Optional[int]       						= feat_extract_norm
			lowerCAmelCase  :     str       						= feat_extract_activation
			lowerCAmelCase  :     Dict       						= list(a_						)
			lowerCAmelCase  :     int       						= list(a_						)
			lowerCAmelCase  :     Any       						= list(a_						)
			lowerCAmelCase  :     Optional[Any]       						= conv_bias
			lowerCAmelCase  :     Tuple       						= num_conv_pos_embeddings
			lowerCAmelCase  :     int       						= num_conv_pos_embedding_groups
			lowerCAmelCase  :     Optional[int]       						= len(self.conv_dim						)
			lowerCAmelCase  :     Dict       						= num_hidden_layers
			lowerCAmelCase  :     str       						= intermediate_size
			lowerCAmelCase  :     Any       						= squeeze_factor
			lowerCAmelCase  :     Optional[int]       						= max_position_embeddings
			lowerCAmelCase  :     Tuple       						= position_buckets
			lowerCAmelCase  :     Union[str, Any]       						= share_att_key
			lowerCAmelCase  :     str       						= relative_attention
			lowerCAmelCase  :     List[str]       						= norm_rel_ebd
			lowerCAmelCase  :     int       						= list(a_						)
			lowerCAmelCase  :     str       						= hidden_act
			lowerCAmelCase  :     Dict       						= num_attention_heads
			lowerCAmelCase  :     Optional[Any]       						= hidden_dropout
			lowerCAmelCase  :     int       						= attention_dropout
			lowerCAmelCase  :     Optional[int]       						= activation_dropout
			lowerCAmelCase  :     Any       						= feat_proj_dropout
			lowerCAmelCase  :     Optional[int]       						= final_dropout
			lowerCAmelCase  :     Union[str, Any]       						= layer_norm_eps
			lowerCAmelCase  :     str       						= feature_layer_norm_eps
			lowerCAmelCase  :     Tuple       						= initializer_range
			lowerCAmelCase  :     int       						= vocab_size

			if (
			    (len(self.conv_stride						) != self.num_feat_extract_layers)
			    or (len(self.conv_kernel						) != self.num_feat_extract_layers)
			    or (len(self.conv_dim						) != self.num_feat_extract_layers)
			):
					raise ValueError(
					    "Configuration for convolutional layers is incorrect."
					    "It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,"
					    F'''but is `len(config.conv_dim) = {len(self.conv_dim						)}`, `len(config.conv_stride)'''
					    F'''= {len(self.conv_stride						)}`, `len(config.conv_kernel) = {len(self.conv_kernel						)}`.'''						)

			# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
			lowerCAmelCase  :     List[str]       						= apply_spec_augment
			lowerCAmelCase  :     List[Any]       						= mask_time_prob
			lowerCAmelCase  :     Tuple       						= mask_time_length
			lowerCAmelCase  :     List[Any]       						= mask_time_min_masks
			lowerCAmelCase  :     List[str]       						= mask_feature_prob
			lowerCAmelCase  :     Union[str, Any]       						= mask_feature_length
			lowerCAmelCase  :     List[Any]       						= mask_feature_min_masks

			# ctc loss
			lowerCAmelCase  :     Any       						= ctc_loss_reduction
			lowerCAmelCase  :     str       						= ctc_zero_infinity

			# sequence classification
			lowerCAmelCase  :     Dict       						= use_weighted_layer_sum
			lowerCAmelCase  :     Tuple       						= classifier_proj_size





	@property
	def 	_lowerCamelCase     ( self						):
			return functools.reduce(operator.mul		,   self.conv_stride		,   1						)



