code: 
  import collections
  import inspect
  import unittest
  
  from transformers import SwinConfig
  from transformers.testing_utils import require_torch, require_vision, slow, torch_device
  from transformers.utils import cached_property, is_torch_available, is_vision_available
  
  from ...test_backbone_common import BackboneTesterMixin
  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 SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel
  							from transformers.models.swin.modeling_swin import SWIN_PRETRAINED_MODEL_ARCHIVE_LIST
  
  
  if is_vision_available():
  							from PIL import Image
  
  							from transformers import AutoImageProcessor
  
  
  
  
  
  class 						lowerCamelCase			:
  
  
  
  
  
  
  
  	def __init__(    self			:int      ,    lowercase			:Optional[int]      ,    lowercase			:int=1_3      ,    lowercase			:Union[str, Any]=3_2      ,    lowercase			:Optional[int]=2      ,    lowercase			:int=3      ,    lowercase			:Union[str, Any]=1_6      ,    lowercase			:Optional[int]=[1, 2, 1]      ,    lowercase			:Optional[int]=[2, 2, 4]      ,    lowercase			:str=2      ,    lowercase			:Tuple=2.0      ,    lowercase			:Optional[int]=True      ,    lowercase			:str=0.0      ,    lowercase			:Dict=0.0      ,    lowercase			:List[Any]=0.1      ,    lowercase			:Union[str, Any]="gelu"      ,    lowercase			:int=False      ,    lowercase			:Optional[int]=True      ,    lowercase			:List[str]=0.02      ,    lowercase			:int=1e-5      ,    lowercase			:List[str]=True      ,    lowercase			:Any=None      ,    lowercase			:Union[str, Any]=True      ,    lowercase			:Optional[Any]=1_0      ,    lowercase			:List[Any]=8      ,    lowercase			:Any=["stage1", "stage2"]      ,    lowercase			:List[Any]=[1, 2]      ,    )		->			List[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   parent
  				SCREAMING_SNAKE_CASE    	=   batch_size
  				SCREAMING_SNAKE_CASE    	=   image_size
  				SCREAMING_SNAKE_CASE    	=   patch_size
  				SCREAMING_SNAKE_CASE    	=   num_channels
  				SCREAMING_SNAKE_CASE    	=   embed_dim
  				SCREAMING_SNAKE_CASE    	=   depths
  				SCREAMING_SNAKE_CASE    	=   num_heads
  				SCREAMING_SNAKE_CASE    	=   window_size
  				SCREAMING_SNAKE_CASE    	=   mlp_ratio
  				SCREAMING_SNAKE_CASE    	=   qkv_bias
  				SCREAMING_SNAKE_CASE    	=   hidden_dropout_prob
  				SCREAMING_SNAKE_CASE    	=   attention_probs_dropout_prob
  				SCREAMING_SNAKE_CASE    	=   drop_path_rate
  				SCREAMING_SNAKE_CASE    	=   hidden_act
  				SCREAMING_SNAKE_CASE    	=   use_absolute_embeddings
  				SCREAMING_SNAKE_CASE    	=   patch_norm
  				SCREAMING_SNAKE_CASE    	=   layer_norm_eps
  				SCREAMING_SNAKE_CASE    	=   initializer_range
  				SCREAMING_SNAKE_CASE    	=   is_training
  				SCREAMING_SNAKE_CASE    	=   scope
  				SCREAMING_SNAKE_CASE    	=   use_labels
  				SCREAMING_SNAKE_CASE    	=   type_sequence_label_size
  				SCREAMING_SNAKE_CASE    	=   encoder_stride
  				SCREAMING_SNAKE_CASE    	=   out_features
  				SCREAMING_SNAKE_CASE    	=   out_indices
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:str )		->			Any:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )
  
  				SCREAMING_SNAKE_CASE    	=   None
  				if self.use_labels:
  							SCREAMING_SNAKE_CASE    	=   ids_tensor([self.batch_size]      ,    self.type_sequence_label_size )
  
  				SCREAMING_SNAKE_CASE    	=   self.get_config()
  
  				return config, pixel_values, labels
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Optional[int] )		->			Tuple:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				return SwinConfig(
  				    image_size=self.image_size      ,    patch_size=self.patch_size      ,    num_channels=self.num_channels      ,    embed_dim=self.embed_dim      ,    depths=self.depths      ,    num_heads=self.num_heads      ,    window_size=self.window_size      ,    mlp_ratio=self.mlp_ratio      ,    qkv_bias=self.qkv_bias      ,    hidden_dropout_prob=self.hidden_dropout_prob      ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,    drop_path_rate=self.drop_path_rate      ,    hidden_act=self.hidden_act      ,    use_absolute_embeddings=self.use_absolute_embeddings      ,    path_norm=self.patch_norm      ,    layer_norm_eps=self.layer_norm_eps      ,    initializer_range=self.initializer_range      ,    encoder_stride=self.encoder_stride      ,    out_features=self.out_features      ,    out_indices=self.out_indices      ,    )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Dict      ,    lowercase			:Optional[Any]      ,    lowercase			:int      ,    lowercase			:Any )		->			Union[str, Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   SwinModel(config=lowercase )
  				model.to(lowercase )
  				model.eval()
  				SCREAMING_SNAKE_CASE    	=   model(lowercase )
  
  				SCREAMING_SNAKE_CASE    	=   ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1))
  				SCREAMING_SNAKE_CASE    	=   int(config.embed_dim * 2 ** (len(config.depths ) - 1) )
  
  				self.parent.assertEqual(result.last_hidden_state.shape      ,    (self.batch_size, expected_seq_len, expected_dim) )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:List[str]      ,    lowercase			:Dict      ,    lowercase			:Optional[Any]      ,    lowercase			:Tuple )		->			str:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   SwinBackbone(config=lowercase )
  				model.to(lowercase )
  				model.eval()
  				SCREAMING_SNAKE_CASE    	=   model(lowercase )
  
  				# verify hidden states
  				self.parent.assertEqual(len(result.feature_maps )      ,    len(config.out_features ) )
  				self.parent.assertListEqual(list(result.feature_maps[0].shape )      ,    [self.batch_size, model.channels[0], 1_6, 1_6] )
  
  				# verify channels
  				self.parent.assertEqual(len(model.channels )      ,    len(config.out_features ) )
  
  				# verify backbone works with out_features=None
  				SCREAMING_SNAKE_CASE    	=   None
  				SCREAMING_SNAKE_CASE    	=   SwinBackbone(config=lowercase )
  				model.to(lowercase )
  				model.eval()
  				SCREAMING_SNAKE_CASE    	=   model(lowercase )
  
  				# verify feature maps
  				self.parent.assertEqual(len(result.feature_maps )      ,    1 )
  				self.parent.assertListEqual(list(result.feature_maps[0].shape )      ,    [self.batch_size, model.channels[-1], 4, 4] )
  
  				# verify channels
  				self.parent.assertEqual(len(model.channels )      ,    1 )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Tuple      ,    lowercase			:Optional[int]      ,    lowercase			:List[Any]      ,    lowercase			:Optional[Any] )		->			List[str]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   SwinForMaskedImageModeling(config=lowercase )
  				model.to(lowercase )
  				model.eval()
  				SCREAMING_SNAKE_CASE    	=   model(lowercase )
  				self.parent.assertEqual(
  				    result.logits.shape      ,    (self.batch_size, self.num_channels, self.image_size, self.image_size) )
  
  				# test greyscale images
  				SCREAMING_SNAKE_CASE    	=   1
  				SCREAMING_SNAKE_CASE    	=   SwinForMaskedImageModeling(lowercase )
  				model.to(lowercase )
  				model.eval()
  
  				SCREAMING_SNAKE_CASE    	=   floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
  				SCREAMING_SNAKE_CASE    	=   model(lowercase )
  				self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, 1, self.image_size, self.image_size) )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:List[str]      ,    lowercase			:List[Any]      ,    lowercase			:Tuple      ,    lowercase			:Optional[int] )		->			Optional[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.type_sequence_label_size
  				SCREAMING_SNAKE_CASE    	=   SwinForImageClassification(lowercase )
  				model.to(lowercase )
  				model.eval()
  				SCREAMING_SNAKE_CASE    	=   model(lowercase      ,    labels=lowercase )
  				self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.type_sequence_label_size) )
  
  				# test greyscale images
  				SCREAMING_SNAKE_CASE    	=   1
  				SCREAMING_SNAKE_CASE    	=   SwinForImageClassification(lowercase )
  				model.to(lowercase )
  				model.eval()
  
  				SCREAMING_SNAKE_CASE    	=   floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
  				SCREAMING_SNAKE_CASE    	=   model(lowercase )
  				self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.type_sequence_label_size) )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Union[str, Any] )		->			Any:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.prepare_config_and_inputs()
  				(
  				    (
  				    SCREAMING_SNAKE_CASE
  				)	,       (
  				    SCREAMING_SNAKE_CASE
  				)	,       (
  				    SCREAMING_SNAKE_CASE
  				)	,       
  				)    	=   config_and_inputs
  				SCREAMING_SNAKE_CASE    	=   {'''pixel_values''': pixel_values}
  				return config, inputs_dict
  
  
  
  
  
  @require_torch
  class 						lowerCamelCase			(					__lowerCamelCase   ,		__lowerCamelCase   ,		unittest.TestCase				):
  	UpperCamelCase_     :				str								=  (
  	    (
  	        SwinModel,
  	        SwinBackbone,
  	        SwinForImageClassification,
  	        SwinForMaskedImageModeling,
  	    )
  	    if is_torch_available()
  	    else ()
  	)
  	UpperCamelCase_     :				Optional[Any]								=  (
  	    {'feature-extraction': SwinModel, 'image-classification': SwinForImageClassification}
  	    if is_torch_available()
  	    else {}
  	)
  	UpperCamelCase_     :				List[Any]								=  True
  
  	UpperCamelCase_     :				Union[str, Any]								=  False
  	UpperCamelCase_     :				Optional[Any]								=  False
  	UpperCamelCase_     :				Any								=  False
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Tuple )		->			Any:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   SwinModelTester(self )
  				SCREAMING_SNAKE_CASE    	=   ConfigTester(self      ,    config_class=lowercase      ,    embed_dim=3_7 )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:str )		->			int:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				self.create_and_test_config_common_properties()
  				self.config_tester.create_and_test_config_to_json_string()
  				self.config_tester.create_and_test_config_to_json_file()
  				self.config_tester.create_and_test_config_from_and_save_pretrained()
  				self.config_tester.create_and_test_config_with_num_labels()
  				self.config_tester.check_config_can_be_init_without_params()
  				self.config_tester.check_config_arguments_init()
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Any )		->			List[str]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				return
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Tuple )		->			Any:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs()
  				self.model_tester.create_and_check_model(*lowercase )
  
  
  
  
  
  
  
  	@unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.''' )
  	def    snake_case__    (    self			:int )		->			str:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				pass
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Dict )		->			Dict:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().test_training_gradient_checkpointing()
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:List[str] )		->			Optional[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs()
  				self.model_tester.create_and_check_backbone(*lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:int )		->			Any:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs()
  				self.model_tester.create_and_check_for_masked_image_modeling(*lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Optional[int] )		->			Dict:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs()
  				self.model_tester.create_and_check_for_image_classification(*lowercase )
  
  
  
  
  
  
  
  	@unittest.skip(reason='''Swin does not use inputs_embeds''' )
  	def    snake_case__    (    self			:List[Any] )		->			Optional[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				pass
  
  
  
  
  
  
  
  	@unittest.skip(reason='''Swin Transformer does not use feedforward chunking''' )
  	def    snake_case__    (    self			:Any )		->			List[str]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				pass
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Dict )		->			Optional[int]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs_for_common()
  
  				for model_class in self.all_model_classes:
  							SCREAMING_SNAKE_CASE    	=   model_class(lowercase )
  							self.assertIsInstance(model.get_input_embeddings()      ,    (nn.Module) )
  							SCREAMING_SNAKE_CASE    	=   model.get_output_embeddings()
  							self.assertTrue(x is None or isinstance(lowercase      ,    nn.Linear ) )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:List[Any] )		->			Optional[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs_for_common()
  
  				for model_class in self.all_model_classes:
  							SCREAMING_SNAKE_CASE    	=   model_class(lowercase )
  							SCREAMING_SNAKE_CASE    	=   inspect.signature(model.forward )
  							# signature.parameters is an OrderedDict => so arg_names order is deterministic
  							SCREAMING_SNAKE_CASE    	=   [*signature.parameters.keys()]
  
  							SCREAMING_SNAKE_CASE    	=   ['''pixel_values''']
  							self.assertListEqual(arg_names[:1]      ,    lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Optional[Any] )		->			Union[str, Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs_for_common()
  				SCREAMING_SNAKE_CASE    	=   True
  
  				for model_class in self.all_model_classes:
  							SCREAMING_SNAKE_CASE    	=   True
  							SCREAMING_SNAKE_CASE    	=   False
  							SCREAMING_SNAKE_CASE    	=   True
  							SCREAMING_SNAKE_CASE    	=   model_class(lowercase )
  							model.to(lowercase )
  							model.eval()
  							with torch.no_grad():
  										SCREAMING_SNAKE_CASE    	=   model(**self._prepare_for_class(lowercase      ,    lowercase ) )
  							SCREAMING_SNAKE_CASE    	=   outputs.attentions
  							SCREAMING_SNAKE_CASE    	=   len(self.model_tester.depths )
  							self.assertEqual(len(lowercase )      ,    lowercase )
  
  							# check that output_attentions also work using config
  							del inputs_dict["output_attentions"]
  							SCREAMING_SNAKE_CASE    	=   True
  							SCREAMING_SNAKE_CASE    	=   config.window_size**2
  							SCREAMING_SNAKE_CASE    	=   model_class(lowercase )
  							model.to(lowercase )
  							model.eval()
  							with torch.no_grad():
  										SCREAMING_SNAKE_CASE    	=   model(**self._prepare_for_class(lowercase      ,    lowercase ) )
  							SCREAMING_SNAKE_CASE    	=   outputs.attentions
  							self.assertEqual(len(lowercase )      ,    lowercase )
  
  							self.assertListEqual(
  							    list(attentions[0].shape[-3:] )      ,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]      ,    )
  							SCREAMING_SNAKE_CASE    	=   len(lowercase )
  
  							# Check attention is always last and order is fine
  							SCREAMING_SNAKE_CASE    	=   True
  							SCREAMING_SNAKE_CASE    	=   True
  							SCREAMING_SNAKE_CASE    	=   model_class(lowercase )
  							model.to(lowercase )
  							model.eval()
  							with torch.no_grad():
  										SCREAMING_SNAKE_CASE    	=   model(**self._prepare_for_class(lowercase      ,    lowercase ) )
  
  							# also another +1 for reshaped_hidden_states
  							SCREAMING_SNAKE_CASE    	=   1 if model_class.__name__ == '''SwinBackbone''' else 2
  							self.assertEqual(out_len + added_hidden_states      ,    len(lowercase ) )
  
  							SCREAMING_SNAKE_CASE    	=   outputs.attentions
  
  							self.assertEqual(len(lowercase )      ,    lowercase )
  
  							self.assertListEqual(
  							    list(self_attentions[0].shape[-3:] )      ,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]      ,    )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:List[Any]      ,    lowercase			:Any      ,    lowercase			:Optional[Any]      ,    lowercase			:Optional[Any]      ,    lowercase			:Optional[int] )		->			Dict:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   model_class(lowercase )
  				model.to(lowercase )
  				model.eval()
  
  				with torch.no_grad():
  							SCREAMING_SNAKE_CASE    	=   model(**self._prepare_for_class(lowercase      ,    lowercase ) )
  
  				SCREAMING_SNAKE_CASE    	=   outputs.hidden_states
  
  				SCREAMING_SNAKE_CASE    	=   getattr(
  				    self.model_tester      ,    '''expected_num_hidden_layers'''      ,    len(self.model_tester.depths ) + 1 )
  				self.assertEqual(len(lowercase )      ,    lowercase )
  
  				# Swin has a different seq_length
  				SCREAMING_SNAKE_CASE    	=   (
  				    config.patch_size
  				    if isinstance(config.patch_size      ,    collections.abc.Iterable )
  				    else (config.patch_size, config.patch_size)
  				)
  
  				SCREAMING_SNAKE_CASE    	=   (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
  
  				self.assertListEqual(
  				    list(hidden_states[0].shape[-2:] )      ,    [num_patches, self.model_tester.embed_dim]      ,    )
  
  				if not model_class.__name__ == "SwinBackbone":
  							SCREAMING_SNAKE_CASE    	=   outputs.reshaped_hidden_states
  							self.assertEqual(len(lowercase )      ,    lowercase )
  
  							SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   reshaped_hidden_states[0].shape
  							SCREAMING_SNAKE_CASE    	=   (
  							    reshaped_hidden_states[0].view(lowercase      ,    lowercase      ,    height * width ).permute(0      ,    2      ,    1 )
  							)
  							self.assertListEqual(
  							    list(reshaped_hidden_states.shape[-2:] )      ,    [num_patches, self.model_tester.embed_dim]      ,    )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Optional[int] )		->			str:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs_for_common()
  
  				SCREAMING_SNAKE_CASE    	=   (
  				    self.model_tester.image_size
  				    if isinstance(self.model_tester.image_size      ,    collections.abc.Iterable )
  				    else (self.model_tester.image_size, self.model_tester.image_size)
  				)
  
  				for model_class in self.all_model_classes:
  							SCREAMING_SNAKE_CASE    	=   True
  							self.check_hidden_states_output(lowercase      ,    lowercase      ,    lowercase      ,    lowercase )
  
  							# check that output_hidden_states also work using config
  							del inputs_dict["output_hidden_states"]
  							SCREAMING_SNAKE_CASE    	=   True
  
  							self.check_hidden_states_output(lowercase      ,    lowercase      ,    lowercase      ,    lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Any )		->			Union[str, Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs_for_common()
  				SCREAMING_SNAKE_CASE    	=   3
  
  				SCREAMING_SNAKE_CASE    	=   (
  				    self.model_tester.image_size
  				    if isinstance(self.model_tester.image_size      ,    collections.abc.Iterable )
  				    else (self.model_tester.image_size, self.model_tester.image_size)
  				)
  				SCREAMING_SNAKE_CASE    	=   (
  				    config.patch_size
  				    if isinstance(config.patch_size      ,    collections.abc.Iterable )
  				    else (config.patch_size, config.patch_size)
  				)
  
  				SCREAMING_SNAKE_CASE    	=   image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
  				SCREAMING_SNAKE_CASE    	=   image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])
  
  				for model_class in self.all_model_classes:
  							SCREAMING_SNAKE_CASE    	=   True
  							self.check_hidden_states_output(lowercase      ,    lowercase      ,    lowercase      ,    (padded_height, padded_width) )
  
  							# check that output_hidden_states also work using config
  							del inputs_dict["output_hidden_states"]
  							SCREAMING_SNAKE_CASE    	=   True
  							self.check_hidden_states_output(lowercase      ,    lowercase      ,    lowercase      ,    (padded_height, padded_width) )
  
  
  
  
  
  
  
  	@slow
  	def    snake_case__    (    self			:Dict )		->			List[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
  							SCREAMING_SNAKE_CASE    	=   SwinModel.from_pretrained(lowercase )
  							self.assertIsNotNone(lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:str )		->			str:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   self.model_tester.prepare_config_and_inputs_for_common()
  
  				SCREAMING_SNAKE_CASE    	=   _config_zero_init(lowercase )
  				for model_class in self.all_model_classes:
  							SCREAMING_SNAKE_CASE    	=   model_class(config=lowercase )
  							for name, param in model.named_parameters():
  										if "embeddings" not in name and 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"""      ,    )
  
  
  
  
  
  @require_vision
  @require_torch
  class 						lowerCamelCase			(					unittest.TestCase				):
  
  
  
  
  
  
  
  	@cached_property
  	def    snake_case__    (    self			:Tuple )		->			str:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				return (
  				    AutoImageProcessor.from_pretrained('''microsoft/swin-tiny-patch4-window7-224''' )
  				    if is_vision_available()
  				    else None
  				)
  
  
  
  
  
  
  
  	@slow
  	def    snake_case__    (    self			:str )		->			List[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   SwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224''' ).to(lowercase )
  				SCREAMING_SNAKE_CASE    	=   self.default_image_processor
  
  				SCREAMING_SNAKE_CASE    	=   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
  				SCREAMING_SNAKE_CASE    	=   image_processor(images=lowercase      ,    return_tensors='''pt''' ).to(lowercase )
  
  				# forward pass
  				with torch.no_grad():
  							SCREAMING_SNAKE_CASE    	=   model(**lowercase )
  
  				# verify the logits
  				SCREAMING_SNAKE_CASE    	=   torch.Size((1, 1_0_0_0) )
  				self.assertEqual(outputs.logits.shape      ,    lowercase )
  				SCREAMING_SNAKE_CASE    	=   torch.tensor([-0.09_48, -0.64_54, -0.09_21] ).to(lowercase )
  				self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,    lowercase      ,    atol=1e-4 ) )
  
  
  
  
  
  @require_torch
  class 						lowerCamelCase			(					unittest.TestCase   ,		__lowerCamelCase				):
  	UpperCamelCase_     :				Dict								=  (SwinBackbone,) if is_torch_available() else ()
  	UpperCamelCase_     :				Tuple								=  SwinConfig
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:int )		->			Dict:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   SwinModelTester(self )
style_context: 
  import argparse
  import os
  from dataclasses import dataclass
  
  import torch
  import torchaudio
  from tqdm import tqdm
  
  from transformers import AutoConfig, AutoModelForCTC, AutoProcessor
  
  
  
  
  
  class 						lowerCamelCase			:
  
  
  
  
  
  
  
  	def __init__(    self			:Optional[int]      ,    lowercase			:Optional[Any]      ,    lowercase			:Optional[int]      ,    lowercase			:int )		->			Optional[int]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   cuda
  				SCREAMING_SNAKE_CASE    	=   AutoConfig.from_pretrained(lowercase )
  				SCREAMING_SNAKE_CASE    	=   AutoModelForCTC.from_pretrained(lowercase )
  				self.model.eval()
  				if self.cuda:
  							self.model.to(device='''cuda''' )
  				SCREAMING_SNAKE_CASE    	=   AutoProcessor.from_pretrained(lowercase )
  				SCREAMING_SNAKE_CASE    	=   torchaudio.transforms.Resample(lowercase      ,    1_6_0_0_0 )
  				SCREAMING_SNAKE_CASE    	=   0
  				SCREAMING_SNAKE_CASE    	=   list(self.processor.tokenizer.get_vocab().keys() )
  				for i in range(len(lowercase ) ):
  							if vocab[i] == "[PAD]" or vocab[i] == "<pad>":
  										SCREAMING_SNAKE_CASE    	=   i
  				print('''Blank Token id [PAD]/<pad>'''      ,    lowercase )
  				SCREAMING_SNAKE_CASE    	=   blank_id
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Any      ,    lowercase			:int )		->			Optional[int]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   torchaudio.load(lowercase )
  				SCREAMING_SNAKE_CASE    	=   self.resampler(lowercase ).squeeze().numpy()
  				return speech
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Union[str, Any]      ,    lowercase			:Optional[Any] )		->			int:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.blank_id
  				SCREAMING_SNAKE_CASE    	=   '''|'''.join(item['''sent'''].split(''' ''' ) )
  				if not os.path.isfile(item['''wav_path'''] ):
  							print(item['''wav_path''']      ,    '''not found in wavs directory''' )
  
  				SCREAMING_SNAKE_CASE    	=   self.speech_file_to_array_fn(item['''wav_path'''] )
  				SCREAMING_SNAKE_CASE    	=   self.processor(lowercase      ,    sampling_rate=1_6_0_0_0      ,    return_tensors='''pt'''      ,    padding=lowercase )
  				if self.cuda:
  							SCREAMING_SNAKE_CASE    	=   inputs.to(device='''cuda''' )
  
  				with torch.no_grad():
  							SCREAMING_SNAKE_CASE    	=   self.model(inputs.input_values ).logits
  
  				# get the emission probability at frame level
  				SCREAMING_SNAKE_CASE    	=   torch.log_softmax(lowercase      ,    dim=-1 )
  				SCREAMING_SNAKE_CASE    	=   emissions[0].cpu().detach()
  
  				# get labels from vocab
  				SCREAMING_SNAKE_CASE    	=   ([''''''] + list(self.processor.tokenizer.get_vocab().keys() ))[
  				    :-1
  				]  # logits don't align with the tokenizer's vocab
  
  				SCREAMING_SNAKE_CASE    	=   {c: i for i, c in enumerate(lowercase )}
  				SCREAMING_SNAKE_CASE    	=   []
  				for c in transcript:
  							if c in dictionary:
  										tokens.append(dictionary[c] )
  
  				def get_trellis(lowercase			:List[Any]      ,    lowercase			:Any      ,    lowercase			:int=0 ):
  							SCREAMING_SNAKE_CASE    	=   emission.size(0 )
  							SCREAMING_SNAKE_CASE    	=   len(lowercase )
  
  							# Trellis has extra diemsions for both time axis and tokens.
  							# The extra dim for tokens represents <SoS> (start-of-sentence)
  							# The extra dim for time axis is for simplification of the code.
  							SCREAMING_SNAKE_CASE    	=   torch.full((num_frames + 1, num_tokens + 1)      ,    -float('''inf''' ) )
  							SCREAMING_SNAKE_CASE    	=   0
  							for t in range(lowercase ):
  										SCREAMING_SNAKE_CASE    	=   torch.maximum(
  										    # Score for staying at the same token
  										    trellis[t, 1:] + emission[t, blank_id]      ,    trellis[t, :-1] + emission[t, tokens]      ,    )
  							return trellis
  
  				SCREAMING_SNAKE_CASE    	=   get_trellis(lowercase      ,    lowercase      ,    lowercase )
  
  
  
  
  
  				@dataclass
  				class 						lowerCamelCase			:
  					UpperCamelCase_     :				int
  					UpperCamelCase_     :				int
  					UpperCamelCase_     :				float
  
  				def backtrack(lowercase			:Optional[int]      ,    lowercase			:List[str]      ,    lowercase			:Union[str, Any]      ,    lowercase			:Optional[Any]=0 ):
  							SCREAMING_SNAKE_CASE    	=   trellis.size(1 ) - 1
  							SCREAMING_SNAKE_CASE    	=   torch.argmax(trellis[:, j] ).item()
  
  							SCREAMING_SNAKE_CASE    	=   []
  							for t in range(lowercase      ,    0      ,    -1 ):
  										# 1. Figure out if the current position was stay or change
  										# Note (again):
  										# `emission[J-1]` is the emission at time frame `J` of trellis dimension.
  										# Score for token staying the same from time frame J-1 to T.
  										SCREAMING_SNAKE_CASE    	=   trellis[t - 1, j] + emission[t - 1, blank_id]
  										# Score for token changing from C-1 at T-1 to J at T.
  										SCREAMING_SNAKE_CASE    	=   trellis[t - 1, j - 1] + emission[t - 1, tokens[j - 1]]
  
  										# 2. Store the path with frame-wise probability.
  										SCREAMING_SNAKE_CASE    	=   emission[t - 1, tokens[j - 1] if changed > stayed else 0].exp().item()
  										# Return token index and time index in non-trellis coordinate.
  										path.append(Point(j - 1      ,    t - 1      ,    lowercase ) )
  
  										# 3. Update the token
  										if changed > stayed:
  													j -= 1
  													if j == 0:
  																break
  							else:
  										raise ValueError('''Failed to align''' )
  							return path[::-1]
  
  				SCREAMING_SNAKE_CASE    	=   backtrack(lowercase      ,    lowercase      ,    lowercase      ,    lowercase )
  
  
  
  
  
  				@dataclass
  				class 						lowerCamelCase			:
  					UpperCamelCase_     :				str
  					UpperCamelCase_     :				int
  					UpperCamelCase_     :				int
  					UpperCamelCase_     :				float
  
  
  
  
  
  
  
  					def __repr__(    self			:Optional[Any] )		->			str:
  
  
  
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								return f"""{self.label}\t{self.score:4.2f}\t{self.start*2_0:5d}\t{self.end*2_0:5d}"""
  
  
  
  
  
  
  
  					@property
  					def    snake_case__    (    self			:List[str] )		->			Any:
  
  
  
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								return self.end - self.start
  
  				def merge_repeats(lowercase			:List[str] ):
  							SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   0, 0
  							SCREAMING_SNAKE_CASE    	=   []
  							while ia < len(lowercase ):
  										while ia < len(lowercase ) and path[ia].token_index == path[ia].token_index:
  													ia += 1
  										SCREAMING_SNAKE_CASE    	=   sum(path[k].score for k in range(lowercase      ,    lowercase ) ) / (ia - ia)
  										segments.append(
  										    Segment(
  										        transcript[path[ia].token_index]      ,    path[ia].time_index      ,    path[ia - 1].time_index + 1      ,    lowercase      ,    ) )
  										SCREAMING_SNAKE_CASE    	=   ia
  							return segments
  
  				SCREAMING_SNAKE_CASE    	=   merge_repeats(lowercase )
  				with open(item['''out_path''']      ,    '''w''' ) as out_align:
  							for seg in segments:
  										out_align.write(str(lowercase ) + '''\n''' )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:List[Any]      ,    lowercase			:Dict      ,    lowercase			:Tuple      ,    lowercase			:str )		->			Dict:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				if not os.path.exists(lowercase ):
  							os.makedirs(lowercase )
  
  				# load text file
  				SCREAMING_SNAKE_CASE    	=   open(lowercase      ,    encoding='''utf8''' ).readlines()
  
  				SCREAMING_SNAKE_CASE    	=   []
  				for line in lines:
  							if len(line.strip().split('''\t''' ) ) != 2:
  										print('''Script must be in format: 00001  this is my sentence''' )
  										exit()
  
  							SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   line.strip().split('''\t''' )
  							SCREAMING_SNAKE_CASE    	=   os.path.join(lowercase      ,    wav_name + '''.wav''' )
  							SCREAMING_SNAKE_CASE    	=   os.path.join(lowercase      ,    wav_name + '''.txt''' )
  
  							items.append({'''sent''': sentence, '''wav_path''': wav_path, '''out_path''': out_path} )
  				print('''Number of samples found in script file'''      ,    len(lowercase ) )
  
  				for item in tqdm(lowercase ):
  							self.align_single_sample(lowercase )
  
  
  
  def       a			(   )    ->Optional[int]:
  
  
  
  
  			'''simple docstring'''
  
  
  			SCREAMING_SNAKE_CASE    	=   argparse.ArgumentParser()
  
  			parser.add_argument(
  			    '''--model_name'''		,	type=a		,	default='''arijitx/wav2vec2-xls-r-300m-bengali'''		,	help='''wav2vec model name'''  )
  			parser.add_argument('''--wav_dir'''		,	type=a		,	default='''./wavs'''		,	help='''directory containing wavs'''  )
  			parser.add_argument('''--text_file'''		,	type=a		,	default='''script.txt'''		,	help='''file containing text'''  )
  			parser.add_argument('''--input_wavs_sr'''		,	type=a		,	default=1_6000		,	help='''sampling rate of input audios'''  )
  			parser.add_argument(
  			    '''--output_dir'''		,	type=a		,	default='''./out_alignment'''		,	help='''output directory containing the alignment files'''  )
  			parser.add_argument('''--cuda'''		,	action='''store_true'''  )
  
  			SCREAMING_SNAKE_CASE    	=   parser.parse_args()
  
  			SCREAMING_SNAKE_CASE    	=   WavaVecaAligner(args.model_name		,	args.input_wavs_sr		,	args.cuda  )
  			aligner.align_data(args.wav_dir		,	args.text_file		,	args.output_dir  )
  
  
  if __name__ == "__main__":
  							main()