import tempfile
import unittest

import numpy as np

import transformers
from transformers import GPTaConfig, GPTaTokenizer, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
		import jax
		import jax.numpy as jnp

		from transformers.modeling_flax_pytorch_utils import (
		    convert_pytorch_state_dict_to_flax,
		    load_flax_weights_in_pytorch_model,
		)
		from transformers.models.gpta.modeling_flax_gpta import FlaxGPTaLMHeadModel, FlaxGPTaModel

if is_torch_available():
		import torch





class      __lowercase  :




				'''simple docstring'''


				def __init__(						self  :						str	,				_a  :						int	,				_a  :						Any=14	,				_a  :						List[str]=7	,				_a  :						Union[str, Any]=True	,				_a  :						str=True	,				_a  :						List[Any]=False	,				_a  :						List[str]=True	,				_a  :						List[Any]=99	,				_a  :						Optional[Any]=32	,				_a  :						str=5	,				_a  :						Dict=4	,				_a  :						str=37	,				_a  :						int="gelu"	,				_a  :						str=0.1	,				_a  :						Dict=0.1	,				_a  :						List[Any]=512	,				_a  :						Tuple=0.02	,				):
								UpperCamelCase__       						=							parent
								UpperCamelCase__       						=							batch_size
								UpperCamelCase__       						=							seq_length
								UpperCamelCase__       						=							is_training
								UpperCamelCase__       						=							use_input_mask
								UpperCamelCase__       						=							use_token_type_ids
								UpperCamelCase__       						=							use_labels
								UpperCamelCase__       						=							vocab_size
								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__       						=							max_position_embeddings
								UpperCamelCase__       						=							initializer_range
								UpperCamelCase__       						=							None
								UpperCamelCase__       						=							vocab_size - 1
								UpperCamelCase__       						=							vocab_size - 1
								UpperCamelCase__       						=							vocab_size - 1





				def 			A_				(						self  :						Union[str, Any]							):
								UpperCamelCase__       						=							ids_tensor([self.batch_size, self.seq_length]	,				self.vocab_size							)

								UpperCamelCase__       						=							None
								if self.use_input_mask:
												UpperCamelCase__       						=							random_attention_mask([self.batch_size, self.seq_length]							)

								UpperCamelCase__       						=							GPTaConfig(
								    vocab_size=self.vocab_size	,				n_embd=self.hidden_size	,				n_layer=self.num_hidden_layers	,				n_head=self.num_attention_heads	,				n_positions=self.max_position_embeddings	,				use_cache=lowerCamelCase__	,				bos_token_id=self.bos_token_id	,				eos_token_id=self.eos_token_id	,				pad_token_id=self.pad_token_id	,				)

								return (config, input_ids, input_mask)





				def 			A_				(						self  :						Union[str, Any]							):
								UpperCamelCase__       						=							self.prepare_config_and_inputs()
								UpperCamelCase__						,      UpperCamelCase__						,      UpperCamelCase__       						=							config_and_inputs
								UpperCamelCase__       						=							{'''input_ids''': input_ids, '''attention_mask''': attention_mask}
								return config, inputs_dict





				def 			A_				(						self  :						Any							):
								UpperCamelCase__						,      UpperCamelCase__						,      UpperCamelCase__       						=							self.prepare_config_and_inputs()

								UpperCamelCase__       						=							floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
								UpperCamelCase__       						=							ids_tensor([self.batch_size, self.seq_length]	,				vocab_size=2							)

								return (
								    config,
								    input_ids,
								    attention_mask,
								    encoder_hidden_states,
								    encoder_attention_mask,
								)





				def 			A_				(						self  :						Optional[Any]	,				_a  :						List[Any]	,				_a  :						Tuple	,				_a  :						Dict	,				_a  :						str							):
								UpperCamelCase__       						=							20
								UpperCamelCase__       						=							model_class_name(lowerCamelCase__							)

								UpperCamelCase__       						=							model.init_cache(input_ids.shape[0]	,				lowerCamelCase__							)
								UpperCamelCase__       						=							jnp.ones((input_ids.shape[0], max_decoder_length)	,				dtype='''i4'''							)

								UpperCamelCase__       						=							jnp.broadcast_to(
								    jnp.arange(input_ids.shape[-1] - 1							)[None, :]	,				(input_ids.shape[0], input_ids.shape[-1] - 1)							)
								UpperCamelCase__       						=							model(
								    input_ids[:, :-1]	,				attention_mask=lowerCamelCase__	,				past_key_values=lowerCamelCase__	,				position_ids=lowerCamelCase__	,				)

								UpperCamelCase__       						=							jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]	,				dtype='''i4'''							)
								UpperCamelCase__       						=							model(
								    input_ids[:, -1:]	,				attention_mask=lowerCamelCase__	,				past_key_values=outputs_cache.past_key_values	,				position_ids=lowerCamelCase__	,				)

								UpperCamelCase__       						=							model(lowerCamelCase__							)

								UpperCamelCase__       						=							np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])							)							)
								self.parent.assertTrue(diff < 1E-3	,				msg=F"""Max diff is {diff}"""							)





				def 			A_				(						self  :						Optional[int]	,				_a  :						Union[str, Any]	,				_a  :						str	,				_a  :						List[Any]	,				_a  :						List[str]							):
								UpperCamelCase__       						=							20
								UpperCamelCase__       						=							model_class_name(lowerCamelCase__							)

								UpperCamelCase__       						=							jnp.concatenate(
								    [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1])							)]	,				axis=-1	,				)

								UpperCamelCase__       						=							model.init_cache(input_ids.shape[0]	,				lowerCamelCase__							)
								UpperCamelCase__       						=							jnp.broadcast_to(
								    jnp.arange(input_ids.shape[-1] - 1							)[None, :]	,				(input_ids.shape[0], input_ids.shape[-1] - 1)							)

								UpperCamelCase__       						=							model(
								    input_ids[:, :-1]	,				attention_mask=lowerCamelCase__	,				past_key_values=lowerCamelCase__	,				position_ids=lowerCamelCase__	,				)
								UpperCamelCase__       						=							jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]	,				dtype='''i4'''							)
								UpperCamelCase__       						=							model(
								    input_ids[:, -1:]	,				past_key_values=outputs_cache.past_key_values	,				attention_mask=lowerCamelCase__	,				position_ids=lowerCamelCase__	,				)

								UpperCamelCase__       						=							model(lowerCamelCase__	,				attention_mask=lowerCamelCase__							)

								UpperCamelCase__       						=							np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])							)							)
								self.parent.assertTrue(diff < 1E-3	,				msg=F"""Max diff is {diff}"""							)






@require_flax
class      __lowercase  (       __A,  __A,  unittest.TestCase    ):




				'''simple docstring'''


				_A	:						Any							=       (FlaxGPTaModel, FlaxGPTaLMHeadModel) if is_flax_available() else ()
				_A	:						List[Any]							=       (FlaxGPTaLMHeadModel,) if is_flax_available() else ()





				def 			A_				(						self  :						Tuple							):
								UpperCamelCase__       						=							FlaxGPTaModelTester(self							)





				def 			A_				(						self  :						Union[str, Any]							):



								for model_class_name in self.all_model_classes:
												UpperCamelCase__						,      UpperCamelCase__						,      UpperCamelCase__       						=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.check_use_cache_forward(lowerCamelCase__	,				lowerCamelCase__	,				lowerCamelCase__	,				lowerCamelCase__							)





				def 			A_				(						self  :						List[Any]							):



								for model_class_name in self.all_model_classes:
												UpperCamelCase__						,      UpperCamelCase__						,      UpperCamelCase__       						=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.check_use_cache_forward_with_attn_mask(
												    lowerCamelCase__	,				lowerCamelCase__	,				lowerCamelCase__	,				lowerCamelCase__							)





				@slow
				def 			A_				(						self  :						List[str]							):
								UpperCamelCase__       						=							GPTaTokenizer.from_pretrained('''gpt2'''	,				pad_token='''</s>'''	,				padding_side='''left'''							)
								UpperCamelCase__       						=							tokenizer(['''Hello this is a long string''', '''Hey''']	,				return_tensors='''np'''	,				padding=lowerCamelCase__	,				truncation=lowerCamelCase__							)

								UpperCamelCase__       						=							FlaxGPTaLMHeadModel.from_pretrained('''gpt2'''							)
								UpperCamelCase__       						=							False
								UpperCamelCase__       						=							model.config.eos_token_id

								UpperCamelCase__       						=							jax.jit(model.generate							)

								UpperCamelCase__       						=							jit_generate(inputs['''input_ids''']	,				attention_mask=inputs['''attention_mask''']							).sequences

								UpperCamelCase__       						=							tokenizer.batch_decode(lowerCamelCase__	,				skip_special_tokens=lowerCamelCase__							)

								UpperCamelCase__       						=							[
								    '''Hello this is a long string of words. I\'m going to start with the first one.\n''',
								    '''Hey, I\'m not sure if I\'m going to be able to do''',
								]

								self.assertListEqual(lowerCamelCase__	,				lowerCamelCase__							)





				@is_pt_flax_cross_test
				def 			A_				(						self  :						List[Any]							):
								UpperCamelCase__						,      UpperCamelCase__       						=							self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
												with self.subTest(model_class.__name__							):
																# prepare inputs
																UpperCamelCase__       						=							self._prepare_for_class(lowerCamelCase__	,				lowerCamelCase__							)
																UpperCamelCase__       						=							{k: torch.tensor(v.tolist()							) for k, v in prepared_inputs_dict.items()}

																# load corresponding PyTorch class
																UpperCamelCase__       						=							model_class.__name__[4:]  # Skip the "Flax" at the beginning
																UpperCamelCase__       						=							getattr(lowerCamelCase__	,				lowerCamelCase__							)

																UpperCamelCase__						,      UpperCamelCase__       						=							pt_inputs['''input_ids'''].shape
																UpperCamelCase__       						=							np.random.randint(0	,				seq_length - 1	,				size=(batch_size,)							)
																for batch_idx, start_index in enumerate(lowerCamelCase__							):
																				UpperCamelCase__       						=							0
																				UpperCamelCase__       						=							1
																				UpperCamelCase__       						=							0
																				UpperCamelCase__       						=							1
																UpperCamelCase__       						=							pt_model_class(lowerCamelCase__							).eval()
																UpperCamelCase__       						=							model_class(lowerCamelCase__	,				dtype=jnp.floataa							)

																UpperCamelCase__       						=							convert_pytorch_state_dict_to_flax(pt_model.state_dict()	,				lowerCamelCase__							)
																UpperCamelCase__       						=							fx_state

																with torch.no_grad():
																				UpperCamelCase__       						=							pt_model(**lowerCamelCase__							).to_tuple()

																UpperCamelCase__       						=							fx_model(**lowerCamelCase__							).to_tuple()
																self.assertEqual(len(lowerCamelCase__							)	,				len(lowerCamelCase__							)	,				'''Output lengths differ between Flax and PyTorch'''							)
																for fx_output, pt_output in zip(lowerCamelCase__	,				lowerCamelCase__							):
																				self.assert_almost_equals(fx_output[:, -1]	,				pt_output[:, -1].numpy()	,				4E-2							)

																with tempfile.TemporaryDirectory() as tmpdirname:
																				pt_model.save_pretrained(lowerCamelCase__							)
																				UpperCamelCase__       						=							model_class.from_pretrained(lowerCamelCase__	,				from_pt=lowerCamelCase__							)

																UpperCamelCase__       						=							fx_model_loaded(**lowerCamelCase__							).to_tuple()
																self.assertEqual(
																    len(lowerCamelCase__							)	,				len(lowerCamelCase__							)	,				'''Output lengths differ between Flax and PyTorch'''							)
																for fx_output_loaded, pt_output in zip(lowerCamelCase__	,				lowerCamelCase__							):
																				self.assert_almost_equals(fx_output_loaded[:, -1]	,				pt_output[:, -1].numpy()	,				4E-2							)





				@is_pt_flax_cross_test
				def 			A_				(						self  :						Dict							):
								UpperCamelCase__						,      UpperCamelCase__       						=							self.model_tester.prepare_config_and_inputs_for_common()
								for model_class in self.all_model_classes:
												with self.subTest(model_class.__name__							):
																# prepare inputs
																UpperCamelCase__       						=							self._prepare_for_class(lowerCamelCase__	,				lowerCamelCase__							)
																UpperCamelCase__       						=							{k: torch.tensor(v.tolist()							) for k, v in prepared_inputs_dict.items()}

																# load corresponding PyTorch class
																UpperCamelCase__       						=							model_class.__name__[4:]  # Skip the "Flax" at the beginning
																UpperCamelCase__       						=							getattr(lowerCamelCase__	,				lowerCamelCase__							)

																UpperCamelCase__       						=							pt_model_class(lowerCamelCase__							).eval()
																UpperCamelCase__       						=							model_class(lowerCamelCase__	,				dtype=jnp.floataa							)

																UpperCamelCase__       						=							load_flax_weights_in_pytorch_model(lowerCamelCase__	,				fx_model.params							)
																UpperCamelCase__						,      UpperCamelCase__       						=							pt_inputs['''input_ids'''].shape
																UpperCamelCase__       						=							np.random.randint(0	,				seq_length - 1	,				size=(batch_size,)							)
																for batch_idx, start_index in enumerate(lowerCamelCase__							):
																				UpperCamelCase__       						=							0
																				UpperCamelCase__       						=							1
																				UpperCamelCase__       						=							0
																				UpperCamelCase__       						=							1

																# make sure weights are tied in PyTorch
																pt_model.tie_weights()

																with torch.no_grad():
																				UpperCamelCase__       						=							pt_model(**lowerCamelCase__							).to_tuple()

																UpperCamelCase__       						=							fx_model(**lowerCamelCase__							).to_tuple()
																self.assertEqual(len(lowerCamelCase__							)	,				len(lowerCamelCase__							)	,				'''Output lengths differ between Flax and PyTorch'''							)
																for fx_output, pt_output in zip(lowerCamelCase__	,				lowerCamelCase__							):
																				self.assert_almost_equals(fx_output[:, -1]	,				pt_output[:, -1].numpy()	,				4E-2							)

																with tempfile.TemporaryDirectory() as tmpdirname:
																				fx_model.save_pretrained(lowerCamelCase__							)
																				UpperCamelCase__       						=							pt_model_class.from_pretrained(lowerCamelCase__	,				from_flax=lowerCamelCase__							)

																with torch.no_grad():
																				UpperCamelCase__       						=							pt_model_loaded(**lowerCamelCase__							).to_tuple()

																self.assertEqual(
																    len(lowerCamelCase__							)	,				len(lowerCamelCase__							)	,				'''Output lengths differ between Flax and PyTorch'''							)
																for fx_output, pt_output in zip(lowerCamelCase__	,				lowerCamelCase__							):
																				self.assert_almost_equals(fx_output[:, -1]	,				pt_output[:, -1].numpy()	,				4E-2							)





				@slow
				def 			A_				(						self  :						List[Any]							):



								for model_class_name in self.all_model_classes:
												UpperCamelCase__       						=							model_class_name.from_pretrained('''gpt2'''	,				from_pt=lowerCamelCase__							)
												UpperCamelCase__       						=							model(np.ones((1, 1)							)							)
												self.assertIsNotNone(lowerCamelCase__							)









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

       def __init__(    self					,		lowerCamelCase__					,		lowerCamelCase__=13					,		lowerCamelCase__=32					,		lowerCamelCase__=2					,		lowerCamelCase__=3					,		lowerCamelCase__=16					,		lowerCamelCase__=[1, 2, 1]					,		lowerCamelCase__=[2, 2, 4]					,		lowerCamelCase__=2					,		lowerCamelCase__=2.0					,		lowerCamelCase__=True					,		lowerCamelCase__=0.0					,		lowerCamelCase__=0.0					,		lowerCamelCase__=0.1					,		lowerCamelCase__="gelu"					,		lowerCamelCase__=False					,		lowerCamelCase__=True					,		lowerCamelCase__=0.02					,		lowerCamelCase__=1E-5					,		lowerCamelCase__=True					,		lowerCamelCase__=None					,		lowerCamelCase__=True					,		lowerCamelCase__=10					,		lowerCamelCase__=8					,		lowerCamelCase__=["stage1", "stage2"]					,		lowerCamelCase__=[1, 2]					,		)    ->					Optional[int]:

           """simple docstring"""



           __lowercase							=						parent
           __lowercase							=						batch_size
           __lowercase							=						image_size
           __lowercase							=						patch_size
           __lowercase							=						num_channels
           __lowercase							=						embed_dim
           __lowercase							=						depths
           __lowercase							=						num_heads
           __lowercase							=						window_size
           __lowercase							=						mlp_ratio
           __lowercase							=						qkv_bias
           __lowercase							=						hidden_dropout_prob
           __lowercase							=						attention_probs_dropout_prob
           __lowercase							=						drop_path_rate
           __lowercase							=						hidden_act
           __lowercase							=						use_absolute_embeddings
           __lowercase							=						patch_norm
           __lowercase							=						layer_norm_eps
           __lowercase							=						initializer_range
           __lowercase							=						is_training
           __lowercase							=						scope
           __lowercase							=						use_labels
           __lowercase							=						type_sequence_label_size
           __lowercase							=						encoder_stride
           __lowercase							=						out_features
           __lowercase							=						out_indices

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

           """simple docstring"""



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

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

           __lowercase							=						self.get_config()

           return config, pixel_values, labels

       def     snake_case__			(    self     )    ->					Optional[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     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )    ->					str:

           """simple docstring"""



           __lowercase							=						SwinModel(config=lowerCamelCase__     )
           model.to(lowerCamelCase__     )
           model.eval()
           __lowercase							=						model(lowerCamelCase__     )

           __lowercase							=						((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths     ) - 1))
           __lowercase							=						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					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )    ->					Union[str, Any]:

           """simple docstring"""



           __lowercase							=						SwinBackbone(config=lowerCamelCase__     )
           model.to(lowerCamelCase__     )
           model.eval()
           __lowercase							=						model(lowerCamelCase__     )

           # 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
           __lowercase							=						None
           __lowercase							=						SwinBackbone(config=lowerCamelCase__     )
           model.to(lowerCamelCase__     )
           model.eval()
           __lowercase							=						model(lowerCamelCase__     )

           # 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					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )    ->					List[str]:

           """simple docstring"""



           __lowercase							=						SwinForMaskedImageModeling(config=lowerCamelCase__     )
           model.to(lowerCamelCase__     )
           model.eval()
           __lowercase							=						model(lowerCamelCase__     )
           self.parent.assertEqual(
               result.logits.shape					,		(self.batch_size, self.num_channels, self.image_size, self.image_size)     )

           # test greyscale images
           __lowercase							=						1
           __lowercase							=						SwinForMaskedImageModeling(lowerCamelCase__     )
           model.to(lowerCamelCase__     )
           model.eval()

           __lowercase							=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
           __lowercase							=						model(lowerCamelCase__     )
           self.parent.assertEqual(result.logits.shape					,		(self.batch_size, 1, self.image_size, self.image_size)     )

       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )    ->					int:

           """simple docstring"""



           __lowercase							=						self.type_sequence_label_size
           __lowercase							=						SwinForImageClassification(lowerCamelCase__     )
           model.to(lowerCamelCase__     )
           model.eval()
           __lowercase							=						model(lowerCamelCase__					,		labels=lowerCamelCase__     )
           self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.type_sequence_label_size)     )

           # test greyscale images
           __lowercase							=						1
           __lowercase							=						SwinForImageClassification(lowerCamelCase__     )
           model.to(lowerCamelCase__     )
           model.eval()

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




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

           """simple docstring"""



           __lowercase							=						self.prepare_config_and_inputs()
           (
               (
               __lowercase
           )				,(
               __lowercase
           )				,(
               __lowercase
           )				,
           )							=						config_and_inputs
           __lowercase							=						{"""pixel_values""": pixel_values}
           return config, inputs_dict






@require_torch
class        SCREAMING_SNAKE_CASE(				__A   ,      __A   ,      unittest.TestCase     ):
       snake_case_ :     Optional[Any]    =					(
           (
               SwinModel,
               SwinBackbone,
               SwinForImageClassification,
               SwinForMaskedImageModeling,
           )
           if is_torch_available()
           else ()
       )
       snake_case_ :     Any    =					(
           {"""feature-extraction""": SwinModel, """image-classification""": SwinForImageClassification}
           if is_torch_available()
           else {}
       )
       snake_case_ :     Any    =					True

       snake_case_ :     int    =					False
       snake_case_ :     Tuple    =					False
       snake_case_ :     Optional[int]    =					False

       def     snake_case__			(    self     )    ->					Any:

           """simple docstring"""



           __lowercase							=						SwinModelTester(self     )
           __lowercase							=						ConfigTester(self					,		config_class=lowerCamelCase__					,		embed_dim=37     )

       def     snake_case__			(    self     )    ->					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     )    ->					List[Any]:

           """simple docstring"""



           return

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

           """simple docstring"""



           __lowercase							=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowerCamelCase__     )

       @unittest.skip(reason="""Got `CUDA error: misaligned address` with PyTorch 2.0.0."""     )
       def     snake_case__			(    self     )    ->					Union[str, Any]:

           """simple docstring"""



           pass

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

           """simple docstring"""



           super().test_training_gradient_checkpointing()

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

           """simple docstring"""



           __lowercase							=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_backbone(*lowerCamelCase__     )

       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           __lowercase							=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase__     )

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

           """simple docstring"""



           __lowercase							=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__     )

       @unittest.skip(reason="""Swin does not use inputs_embeds"""     )
       def     snake_case__			(    self     )    ->					Tuple:

           """simple docstring"""



           pass

       @unittest.skip(reason="""Swin Transformer does not use feedforward chunking"""     )
       def     snake_case__			(    self     )    ->					Any:

           """simple docstring"""



           pass

       def     snake_case__			(    self     )    ->					int:

           """simple docstring"""



           __lowercase				,__lowercase							=						self.model_tester.prepare_config_and_inputs_for_common()

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

       def     snake_case__			(    self     )    ->					Any:

           """simple docstring"""



           __lowercase				,__lowercase							=						self.model_tester.prepare_config_and_inputs_for_common()

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

               __lowercase							=						["""pixel_values"""]
               self.assertListEqual(arg_names[:1]					,		lowerCamelCase__     )

       def     snake_case__			(    self     )    ->					Any:

           """simple docstring"""



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

           for model_class in self.all_model_classes:
               __lowercase							=						True
               __lowercase							=						False
               __lowercase							=						True
               __lowercase							=						model_class(lowerCamelCase__     )
               model.to(lowerCamelCase__     )
               model.eval()
               with torch.no_grad():
                   __lowercase							=						model(**self._prepare_for_class(lowerCamelCase__					,		lowerCamelCase__     )     )
               __lowercase							=						outputs.attentions
               __lowercase							=						len(self.model_tester.depths     )
               self.assertEqual(len(lowerCamelCase__     )					,		lowerCamelCase__     )

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

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

               # Check attention is always last and order is fine
               __lowercase							=						True
               __lowercase							=						True
               __lowercase							=						model_class(lowerCamelCase__     )
               model.to(lowerCamelCase__     )
               model.eval()
               with torch.no_grad():
                   __lowercase							=						model(**self._prepare_for_class(lowerCamelCase__					,		lowerCamelCase__     )     )

               # also another +1 for reshaped_hidden_states
               __lowercase							=						1 if model_class.__name__ == """SwinBackbone""" else 2
               self.assertEqual(out_len + added_hidden_states					,		len(lowerCamelCase__     )     )

               __lowercase							=						outputs.attentions

               self.assertEqual(len(lowerCamelCase__     )					,		lowerCamelCase__     )

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

       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )    ->					Optional[int]:

           """simple docstring"""



           __lowercase							=						model_class(lowerCamelCase__     )
           model.to(lowerCamelCase__     )
           model.eval()

           with torch.no_grad():
               __lowercase							=						model(**self._prepare_for_class(lowerCamelCase__					,		lowerCamelCase__     )     )

           __lowercase							=						outputs.hidden_states

           __lowercase							=						getattr(
               self.model_tester					,		"""expected_num_hidden_layers"""					,		len(self.model_tester.depths     ) + 1     )
           self.assertEqual(len(lowerCamelCase__     )					,		lowerCamelCase__     )

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

           __lowercase							=						(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":
               __lowercase							=						outputs.reshaped_hidden_states
               self.assertEqual(len(lowerCamelCase__     )					,		lowerCamelCase__     )

               __lowercase				,__lowercase				,__lowercase				,__lowercase							=						reshaped_hidden_states[0].shape
               __lowercase							=						(
                   reshaped_hidden_states[0].view(lowerCamelCase__					,		lowerCamelCase__					,		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     )    ->					int:

           """simple docstring"""



           __lowercase				,__lowercase							=						self.model_tester.prepare_config_and_inputs_for_common()

           __lowercase							=						(
               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:
               __lowercase							=						True
               self.check_hidden_states_output(lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               __lowercase							=						True

               self.check_hidden_states_output(lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )

       def     snake_case__			(    self     )    ->					Dict:

           """simple docstring"""



           __lowercase				,__lowercase							=						self.model_tester.prepare_config_and_inputs_for_common()
           __lowercase							=						3

           __lowercase							=						(
               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)
           )
           __lowercase							=						(
               config.patch_size
               if isinstance(config.patch_size					,		collections.abc.Iterable     )
               else (config.patch_size, config.patch_size)
           )

           __lowercase							=						image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
           __lowercase							=						image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

           for model_class in self.all_model_classes:
               __lowercase							=						True
               self.check_hidden_states_output(lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		(padded_height, padded_width)     )

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               __lowercase							=						True
               self.check_hidden_states_output(lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		(padded_height, padded_width)     )

       @slow
       def     snake_case__			(    self     )    ->					Optional[Any]:

           """simple docstring"""



           for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               __lowercase							=						SwinModel.from_pretrained(lowerCamelCase__     )
               self.assertIsNotNone(lowerCamelCase__     )




       def     snake_case__			(    self     )    ->					Dict:

           """simple docstring"""



           __lowercase				,__lowercase							=						self.model_tester.prepare_config_and_inputs_for_common()

           __lowercase							=						_config_zero_init(lowerCamelCase__     )
           for model_class in self.all_model_classes:
               __lowercase							=						model_class(config=lowerCamelCase__     )
               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        SCREAMING_SNAKE_CASE(				unittest.TestCase     ):

       @cached_property
       def     snake_case__			(    self     )    ->					List[Any]:

           """simple docstring"""



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




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

           """simple docstring"""



           __lowercase							=						SwinForImageClassification.from_pretrained("""microsoft/swin-tiny-patch4-window7-224"""     ).to(lowerCamelCase__     )
           __lowercase							=						self.default_image_processor

           __lowercase							=						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
           __lowercase							=						image_processor(images=lowerCamelCase__					,		return_tensors="""pt"""     ).to(lowerCamelCase__     )

           # forward pass
           with torch.no_grad():
               __lowercase							=						model(**lowerCamelCase__     )

           # verify the logits
           __lowercase							=						torch.Size((1, 1000)     )
           self.assertEqual(outputs.logits.shape					,		lowerCamelCase__     )
           __lowercase							=						torch.tensor([-0.09_48, -0.64_54, -0.09_21]     ).to(lowerCamelCase__     )
           self.assertTrue(torch.allclose(outputs.logits[0, :3]					,		lowerCamelCase__					,		atol=1E-4     )     )






@require_torch
class        SCREAMING_SNAKE_CASE(				unittest.TestCase   ,      __A     ):
       snake_case_ :     List[Any]    =					(SwinBackbone,) if is_torch_available() else ()
       snake_case_ :     Optional[Any]    =					SwinConfig

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

           """simple docstring"""



           __lowercase							=						SwinModelTester(self     )




