



"""simple docstring"""

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__	:


	'''simple docstring'''







	def __init__(			self							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					Any=13    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=32    ,					_SCREAMING_SNAKE_CASE							:					List[str]=2    ,					_SCREAMING_SNAKE_CASE							:					Tuple=3    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=16    ,					_SCREAMING_SNAKE_CASE							:					str=[1, 2, 1]    ,					_SCREAMING_SNAKE_CASE							:					Dict=[2, 2, 4]    ,					_SCREAMING_SNAKE_CASE							:					Dict=2    ,					_SCREAMING_SNAKE_CASE							:					str=2.0    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]=True    ,					_SCREAMING_SNAKE_CASE							:					str=0.0    ,					_SCREAMING_SNAKE_CASE							:					str=0.0    ,					_SCREAMING_SNAKE_CASE							:					int=0.1    ,					_SCREAMING_SNAKE_CASE							:					Tuple="gelu"    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]=False    ,					_SCREAMING_SNAKE_CASE							:					Tuple=True    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]=0.0_2    ,					_SCREAMING_SNAKE_CASE							:					List[Any]=1E-5    ,					_SCREAMING_SNAKE_CASE							:					Tuple=True    ,					_SCREAMING_SNAKE_CASE							:					str=None    ,					_SCREAMING_SNAKE_CASE							:					Any=True    ,					_SCREAMING_SNAKE_CASE							:					int=10    ,					_SCREAMING_SNAKE_CASE							:					Any=8    ,					_SCREAMING_SNAKE_CASE							:					List[str]=["stage1", "stage2"]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]=[1, 2]    ,					)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Tuple 							=    parent
					SCREAMING_SNAKE_CASE	:       Tuple 							=    batch_size
					SCREAMING_SNAKE_CASE	:       Dict 							=    image_size
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    patch_size
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    num_channels
					SCREAMING_SNAKE_CASE	:       int 							=    embed_dim
					SCREAMING_SNAKE_CASE	:       int 							=    depths
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    num_heads
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    window_size
					SCREAMING_SNAKE_CASE	:       List[str] 							=    mlp_ratio
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    qkv_bias
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    hidden_dropout_prob
					SCREAMING_SNAKE_CASE	:       Tuple 							=    attention_probs_dropout_prob
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    drop_path_rate
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    hidden_act
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    use_absolute_embeddings
					SCREAMING_SNAKE_CASE	:       List[str] 							=    patch_norm
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    layer_norm_eps
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    initializer_range
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    is_training
					SCREAMING_SNAKE_CASE	:       Dict 							=    scope
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    use_labels
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    type_sequence_label_size
					SCREAMING_SNAKE_CASE	:       int 							=    encoder_stride
					SCREAMING_SNAKE_CASE	:       Tuple 							=    out_features
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    out_indices





	def  _lowerCAmelCase    (			self							:					Any						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Dict 							=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

					SCREAMING_SNAKE_CASE	:       List[str] 							=    None
					if self.use_labels:
									SCREAMING_SNAKE_CASE	:       int 							=    ids_tensor([self.batch_size]    ,					self.type_sequence_label_size						)

					SCREAMING_SNAKE_CASE	:       List[str] 							=    self.get_config()

					return config, pixel_values, labels





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





					"""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  _lowerCAmelCase    (			self							:					List[Any]    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]    ,					_SCREAMING_SNAKE_CASE							:					Tuple						)	->  Union[str, Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       str 							=    SwinModel(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Tuple 							=    model(_SCREAMING_SNAKE_CASE						)

					SCREAMING_SNAKE_CASE	:       List[Any] 							=    ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    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  _lowerCAmelCase    (			self							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					str    ,					_SCREAMING_SNAKE_CASE							:					int    ,					_SCREAMING_SNAKE_CASE							:					Dict						)	->  Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    SwinBackbone(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       int 							=    model(_SCREAMING_SNAKE_CASE						)

					# 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], 16, 16]						)

					# verify channels
					self.parent.assertEqual(len(model.channels						)    ,					len(config.out_features						)						)

					# verify backbone works with out_features=None
					SCREAMING_SNAKE_CASE	:       str 							=    None
					SCREAMING_SNAKE_CASE	:       str 							=    SwinBackbone(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       int 							=    model(_SCREAMING_SNAKE_CASE						)

					# 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  _lowerCAmelCase    (			self							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					Dict						)	->  Optional[int]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    SwinForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    model(_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(
					    result.logits.shape    ,					(self.batch_size, self.num_channels, self.image_size, self.image_size)						)

					# test greyscale images
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    1
					SCREAMING_SNAKE_CASE	:       int 							=    SwinForMaskedImageModeling(_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()

					SCREAMING_SNAKE_CASE	:       List[Any] 							=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    model(_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, 1, self.image_size, self.image_size)						)





	def  _lowerCAmelCase    (			self							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[int]    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					List[Any]						)	->  List[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    self.type_sequence_label_size
					SCREAMING_SNAKE_CASE	:       Any 							=    SwinForImageClassification(_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    model(_SCREAMING_SNAKE_CASE    ,					labels=_SCREAMING_SNAKE_CASE						)
					self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.type_sequence_label_size)						)

					# test greyscale images
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    1
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    SwinForImageClassification(_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()

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







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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[Any] 							=    self.prepare_config_and_inputs()
					(
					    (
					    SCREAMING_SNAKE_CASE
					)							,	(
					    SCREAMING_SNAKE_CASE
					)							,	(
					    SCREAMING_SNAKE_CASE
					)							,	
					)	:       str 							=    config_and_inputs
					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    {'pixel_values': pixel_values}
					return config, inputs_dict




@require_torch
class 						lowerCAmelCase__	(  _lowerCamelCase       ,       _lowerCamelCase       ,       unittest.TestCase   ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE  :		Any     	=   (
	    (
	        SwinModel,
	        SwinBackbone,
	        SwinForImageClassification,
	        SwinForMaskedImageModeling,
	    )
	    if is_torch_available()
	    else ()
	)
	_SCREAMING_SNAKE_CASE  :		str     	=   (
	    {'''feature-extraction''': SwinModel, '''image-classification''': SwinForImageClassification}
	    if is_torch_available()
	    else {}
	)
	_SCREAMING_SNAKE_CASE  :		int     	=   True

	_SCREAMING_SNAKE_CASE  :		Optional[int]     	=   False
	_SCREAMING_SNAKE_CASE  :		List[str]     	=   False
	_SCREAMING_SNAKE_CASE  :		str     	=   False





	def  _lowerCAmelCase    (			self							:					Dict						)	->  Any:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[Any] 							=    SwinModelTester(self						)
					SCREAMING_SNAKE_CASE	:       List[str] 							=    ConfigTester(self    ,					config_class=_SCREAMING_SNAKE_CASE    ,					embed_dim=37						)





	def  _lowerCAmelCase    (			self							:					Dict						)	->  Any:





					"""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  _lowerCAmelCase    (			self							:					str						)	->  int:





					"""simple docstring"""


					return





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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE						)





	@unittest.skip(reason='Got `CUDA error: misaligned address` with PyTorch 2.0.0.'						)
	def  _lowerCAmelCase    (			self							:					int						)	->  Any:





					"""simple docstring"""


					pass





	def  _lowerCAmelCase    (			self							:					List[str]						)	->  Tuple:





					"""simple docstring"""


					super().test_training_gradient_checkpointing()





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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_backbone(*_SCREAMING_SNAKE_CASE						)





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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[Any] 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_image_modeling(*_SCREAMING_SNAKE_CASE						)





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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Any 							=    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE						)





	@unittest.skip(reason='Swin does not use inputs_embeds'						)
	def  _lowerCAmelCase    (			self							:					str						)	->  int:





					"""simple docstring"""


					pass





	@unittest.skip(reason='Swin Transformer does not use feedforward chunking'						)
	def  _lowerCAmelCase    (			self							:					str						)	->  List[str]:





					"""simple docstring"""


					pass





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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE	:       Dict 							=    self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE	:       Dict 							=    model_class(_SCREAMING_SNAKE_CASE						)
									self.assertIsInstance(model.get_input_embeddings()    ,					(nn.Module)						)
									SCREAMING_SNAKE_CASE	:       List[str] 							=    model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(_SCREAMING_SNAKE_CASE    ,					nn.Linear						)						)





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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE	:       Tuple 							=    self.model_tester.prepare_config_and_inputs_for_common()

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

									SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    ['pixel_values']
									self.assertListEqual(arg_names[:1]    ,					_SCREAMING_SNAKE_CASE						)





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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE	:       int 							=    self.model_tester.prepare_config_and_inputs_for_common()
					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    True

					for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE	:       Dict 							=    True
									SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    False
									SCREAMING_SNAKE_CASE	:       List[str] 							=    True
									SCREAMING_SNAKE_CASE	:       Optional[int] 							=    model_class(_SCREAMING_SNAKE_CASE						)
									model.to(_SCREAMING_SNAKE_CASE						)
									model.eval()
									with torch.no_grad():
													SCREAMING_SNAKE_CASE	:       List[Any] 							=    model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)						)
									SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    outputs.attentions
									SCREAMING_SNAKE_CASE	:       List[str] 							=    len(self.model_tester.depths						)
									self.assertEqual(len(_SCREAMING_SNAKE_CASE						)    ,					_SCREAMING_SNAKE_CASE						)

									# check that output_attentions also work using config
									del inputs_dict["output_attentions"]
									SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    True
									SCREAMING_SNAKE_CASE	:       Optional[int] 							=    config.window_size**2
									SCREAMING_SNAKE_CASE	:       Any 							=    model_class(_SCREAMING_SNAKE_CASE						)
									model.to(_SCREAMING_SNAKE_CASE						)
									model.eval()
									with torch.no_grad():
													SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)						)
									SCREAMING_SNAKE_CASE	:       int 							=    outputs.attentions
									self.assertEqual(len(_SCREAMING_SNAKE_CASE						)    ,					_SCREAMING_SNAKE_CASE						)

									self.assertListEqual(
									    list(attentions[0].shape[-3:]						)    ,					[self.model_tester.num_heads[0], window_size_squared, window_size_squared]    ,					)
									SCREAMING_SNAKE_CASE	:       Dict 							=    len(_SCREAMING_SNAKE_CASE						)

									# Check attention is always last and order is fine
									SCREAMING_SNAKE_CASE	:       List[Any] 							=    True
									SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    True
									SCREAMING_SNAKE_CASE	:       Any 							=    model_class(_SCREAMING_SNAKE_CASE						)
									model.to(_SCREAMING_SNAKE_CASE						)
									model.eval()
									with torch.no_grad():
													SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)						)

									# also another +1 for reshaped_hidden_states
									SCREAMING_SNAKE_CASE	:       str 							=    1 if model_class.__name__ == 'SwinBackbone' else 2
									self.assertEqual(out_len + added_hidden_states    ,					len(_SCREAMING_SNAKE_CASE						)						)

									SCREAMING_SNAKE_CASE	:       str 							=    outputs.attentions

									self.assertEqual(len(_SCREAMING_SNAKE_CASE						)    ,					_SCREAMING_SNAKE_CASE						)

									self.assertListEqual(
									    list(self_attentions[0].shape[-3:]						)    ,					[self.model_tester.num_heads[0], window_size_squared, window_size_squared]    ,					)





	def  _lowerCAmelCase    (			self							:					str    ,					_SCREAMING_SNAKE_CASE							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Union[str, Any]						)	->  str:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    model_class(_SCREAMING_SNAKE_CASE						)
					model.to(_SCREAMING_SNAKE_CASE						)
					model.eval()

					with torch.no_grad():
									SCREAMING_SNAKE_CASE	:       int 							=    model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)						)

					SCREAMING_SNAKE_CASE	:       int 							=    outputs.hidden_states

					SCREAMING_SNAKE_CASE	:       Dict 							=    getattr(
					    self.model_tester    ,					'expected_num_hidden_layers'    ,					len(self.model_tester.depths						) + 1						)
					self.assertEqual(len(_SCREAMING_SNAKE_CASE						)    ,					_SCREAMING_SNAKE_CASE						)

					# Swin has a different seq_length
					SCREAMING_SNAKE_CASE	:       int 							=    (
					    config.patch_size
					    if isinstance(config.patch_size    ,					collections.abc.Iterable						)
					    else (config.patch_size, config.patch_size)
					)

					SCREAMING_SNAKE_CASE	:       List[Any] 							=    (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	:       Optional[int] 							=    outputs.reshaped_hidden_states
									self.assertEqual(len(_SCREAMING_SNAKE_CASE						)    ,					_SCREAMING_SNAKE_CASE						)

									SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE	:       Tuple 							=    reshaped_hidden_states[0].shape
									SCREAMING_SNAKE_CASE	:       Any 							=    (
									    reshaped_hidden_states[0].view(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					height * width						).permute(0    ,					2    ,					1						)
									)
									self.assertListEqual(
									    list(reshaped_hidden_states.shape[-2:]						)    ,					[num_patches, self.model_tester.embed_dim]    ,					)





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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE	:       Dict 							=    self.model_tester.prepare_config_and_inputs_for_common()

					SCREAMING_SNAKE_CASE	:       List[str] 							=    (
					    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	:       Optional[int] 							=    True
									self.check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)

									# check that output_hidden_states also work using config
									del inputs_dict["output_hidden_states"]
									SCREAMING_SNAKE_CASE	:       Optional[int] 							=    True

									self.check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE						)





	def  _lowerCAmelCase    (			self							:					str						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    self.model_tester.prepare_config_and_inputs_for_common()
					SCREAMING_SNAKE_CASE	:       Any 							=    3

					SCREAMING_SNAKE_CASE	:       List[str] 							=    (
					    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	:       Union[str, Any] 							=    (
					    config.patch_size
					    if isinstance(config.patch_size    ,					collections.abc.Iterable						)
					    else (config.patch_size, config.patch_size)
					)

					SCREAMING_SNAKE_CASE	:       int 							=    image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
					SCREAMING_SNAKE_CASE	:       List[str] 							=    image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

					for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE	:       Optional[int] 							=    True
									self.check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					(padded_height, padded_width)						)

									# check that output_hidden_states also work using config
									del inputs_dict["output_hidden_states"]
									SCREAMING_SNAKE_CASE	:       List[Any] 							=    True
									self.check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					(padded_height, padded_width)						)





	@slow
	def  _lowerCAmelCase    (			self							:					List[str]						)	->  Any:





					"""simple docstring"""


					for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE	:       int 							=    SwinModel.from_pretrained(_SCREAMING_SNAKE_CASE						)
									self.assertIsNotNone(_SCREAMING_SNAKE_CASE						)







	def  _lowerCAmelCase    (			self							:					Dict						)	->  Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE	:       List[Any] 							=    self.model_tester.prepare_config_and_inputs_for_common()

					SCREAMING_SNAKE_CASE	:       int 							=    _config_zero_init(_SCREAMING_SNAKE_CASE						)
					for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE	:       Any 							=    model_class(config=_SCREAMING_SNAKE_CASE						)
									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   ):


	'''simple docstring'''







	@cached_property
	def  _lowerCAmelCase    (			self							:					Tuple						)	->  Any:





					"""simple docstring"""


					return (
					    AutoImageProcessor.from_pretrained('microsoft/swin-tiny-patch4-window7-224'						)
					    if is_vision_available()
					    else None
					)







	@slow
	def  _lowerCAmelCase    (			self							:					str						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    SwinForImageClassification.from_pretrained('microsoft/swin-tiny-patch4-window7-224'						).to(_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       int 							=    self.default_image_processor

					SCREAMING_SNAKE_CASE	:       Union[str, Any] 							=    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
					SCREAMING_SNAKE_CASE	:       Optional[int] 							=    image_processor(images=_SCREAMING_SNAKE_CASE    ,					return_tensors='pt'						).to(_SCREAMING_SNAKE_CASE						)

					# forward pass
					with torch.no_grad():
									SCREAMING_SNAKE_CASE	:       List[str] 							=    model(**_SCREAMING_SNAKE_CASE						)

					# verify the logits
					SCREAMING_SNAKE_CASE	:       Dict 							=    torch.Size((1, 1_000)						)
					self.assertEqual(outputs.logits.shape    ,					_SCREAMING_SNAKE_CASE						)
					SCREAMING_SNAKE_CASE	:       str 							=    torch.tensor([-0.0_9_4_8, -0.6_4_5_4, -0.0_9_2_1]						).to(_SCREAMING_SNAKE_CASE						)
					self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,					_SCREAMING_SNAKE_CASE    ,					atol=1E-4						)						)




@require_torch
class 						lowerCAmelCase__	(  unittest.TestCase       ,       _lowerCamelCase   ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE  :		Dict     	=   (SwinBackbone,) if is_torch_available() else ()
	_SCREAMING_SNAKE_CASE  :		Optional[int]     	=   SwinConfig





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





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Optional[Any] 							=    SwinModelTester(self						)










"""simple docstring"""

import torch
import torch.nn.functional as F

from diffusers import VQDiffusionScheduler

from .test_schedulers import SchedulerCommonTest
class 						lowerCAmelCase__	(  _lowerCamelCase   ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE  :		Dict     	=   (VQDiffusionScheduler,)





	def  _lowerCAmelCase    (			self							:					Tuple    ,					**_SCREAMING_SNAKE_CASE							:					Optional[int]						)	->  int:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       List[str] 							=    {
					    'num_vec_classes': 4_097,
					    'num_train_timesteps': 100,
					}

					config.update(**_SCREAMING_SNAKE_CASE						)
					return config





	def  _lowerCAmelCase    (			self							:					Dict    ,					_SCREAMING_SNAKE_CASE							:					Tuple						)	->  List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE	:       Tuple 							=    4
					SCREAMING_SNAKE_CASE	:       List[str] 							=    8
					SCREAMING_SNAKE_CASE	:       List[Any] 							=    8

					SCREAMING_SNAKE_CASE	:       Tuple 							=    torch.randint(0    ,					_SCREAMING_SNAKE_CASE    ,					(batch_size, height * width)						)

					return sample





	@property
	def  _lowerCAmelCase    (			self							:					str						)	->  int:





					"""simple docstring"""


					assert False





	def  _lowerCAmelCase    (			self							:					List[str]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]						)	->  Optional[int]:





					"""simple docstring"""


					def model(_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					_SCREAMING_SNAKE_CASE							:					Optional[Any]    ,					*_SCREAMING_SNAKE_CASE							:					str						):
									SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE	:       Any 							=    sample.shape
									SCREAMING_SNAKE_CASE	:       Optional[int] 							=    torch.rand((batch_size, num_vec_classes - 1, num_latent_pixels)						)
									SCREAMING_SNAKE_CASE	:       List[str] 							=    F.log_softmax(logits.double()    ,					dim=1						).float()
									return return_value

					return model





	def  _lowerCAmelCase    (			self							:					List[str]						)	->  Dict:





					"""simple docstring"""


					for timesteps in [2, 5, 100, 1_000]:
									self.check_over_configs(num_train_timesteps=_SCREAMING_SNAKE_CASE						)





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





					"""simple docstring"""


					for num_vec_classes in [5, 100, 1_000, 4_000]:
									self.check_over_configs(num_vec_classes=_SCREAMING_SNAKE_CASE						)





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





					"""simple docstring"""


					for t in [0, 50, 99]:
									self.check_over_forward(time_step=_SCREAMING_SNAKE_CASE						)







	def  _lowerCAmelCase    (			self							:					Any						)	->  Dict:





					"""simple docstring"""


					pass





