


'''simple docstring'''



import collections
import inspect
import unittest

from transformers import SwinvaConfig
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_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 SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
	from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor





class 							__UpperCamelCase				:




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

						'''simple docstring'''





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




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       str					    =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

						__a			:       Optional[Any]					    =  None
						if self.use_labels:
											__a			:       Optional[int]					    =  ids_tensor([self.batch_size]						,				self.type_sequence_label_size							)

						__a			:       Union[str, Any]					    =  self.get_config()

						return config, pixel_values, labels




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return SwinvaConfig(
						    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						,				)




	def       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a							):

						'''simple docstring'''





						__a			:       Any					    =  SwinvaModel(config=_snake_case							)
						model.to(_snake_case							)
						model.eval()
						__a			:       str					    =  model(_snake_case							)

						__a			:       List[Any]					    =  ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths							) - 1))
						__a			:       Tuple					    =  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       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a							):

						'''simple docstring'''





						__a			:       Union[str, Any]					    =  SwinvaForMaskedImageModeling(config=_snake_case							)
						model.to(_snake_case							)
						model.eval()
						__a			:       Tuple					    =  model(_snake_case							)
						self.parent.assertEqual(
						    result.logits.shape						,				(self.batch_size, self.num_channels, self.image_size, self.image_size)							)

						# test greyscale images
						__a			:       Optional[int]					    =  1
						__a			:       List[Any]					    =  SwinvaForMaskedImageModeling(_snake_case							)
						model.to(_snake_case							)
						model.eval()

						__a			:       Union[str, Any]					    =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
						__a			:       str					    =  model(_snake_case							)
						self.parent.assertEqual(result.logits.shape						,				(self.batch_size, 1, self.image_size, self.image_size)							)




	def       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a							):

						'''simple docstring'''





						__a			:       Tuple					    =  self.type_sequence_label_size
						__a			:       Dict					    =  SwinvaForImageClassification(_snake_case							)
						model.to(_snake_case							)
						model.eval()
						__a			:       str					    =  model(_snake_case						,				labels=_snake_case							)
						self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.type_sequence_label_size)							)





	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





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





@require_torch
class 							__UpperCamelCase				(		A_		,      A_		,      unittest.TestCase					):
	A_				=    (
	    (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
	)
	A_				=    (
	    {"feature-extraction": SwinvaModel, "image-classification": SwinvaForImageClassification}
	    if is_torch_available()
	    else {}
	)

	A_				=    False
	A_				=    False
	A_				=    False
	A_				=    False




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Optional[Any]					    =  SwinvaModelTester(self							)
						__a			:       List[str]					    =  ConfigTester(self						,				config_class=_snake_case						,				embed_dim=37							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						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       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       int					    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*_snake_case							)




	@unittest.skip(reason='Got `CUDA error: misaligned address` with PyTorch 2.0.0.'							)
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						pass




	@unittest.skip(reason='Swinv2 does not use inputs_embeds'							)
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						pass




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Optional[int]					    =  self.model_tester.prepare_config_and_inputs_for_common()

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




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





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

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

											__a			:       Tuple					    =  ['''pixel_values''']
											self.assertListEqual(arg_names[:1]						,				_snake_case							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       List[str]					    =  self.model_tester.prepare_config_and_inputs_for_common()
						__a			:       Tuple					    =  True

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

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

											self.assertListEqual(
											    list(attentions[0].shape[-3:]							)						,				[self.model_tester.num_heads[0], window_size_squared, window_size_squared]						,				)
											__a			:       Optional[Any]					    =  len(_snake_case							)

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

											if hasattr(self.model_tester						,				'num_hidden_states_types'							):
																__a			:       int					    =  self.model_tester.num_hidden_states_types
											else:
																# also another +1 for reshaped_hidden_states
																__a			:       List[str]					    =  2
											self.assertEqual(out_len + added_hidden_states						,				len(_snake_case							)							)

											__a			:       Optional[int]					    =  outputs.attentions

											self.assertEqual(len(_snake_case							)						,				_snake_case							)

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




	def       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a						,				__a							):

						'''simple docstring'''





						__a			:       List[Any]					    =  model_class(_snake_case							)
						model.to(_snake_case							)
						model.eval()

						with torch.no_grad():
											__a			:       int					    =  model(**self._prepare_for_class(_snake_case						,				_snake_case							)							)

						__a			:       Optional[int]					    =  outputs.hidden_states

						__a			:       List[Any]					    =  getattr(
						    self.model_tester						,				'expected_num_hidden_layers'						,				len(self.model_tester.depths							) + 1							)
						self.assertEqual(len(_snake_case							)						,				_snake_case							)

						# Swinv2 has a different seq_length
						__a			:       Dict					    =  (
						    config.patch_size
						    if isinstance(config.patch_size						,				collections.abc.Iterable							)
						    else (config.patch_size, config.patch_size)
						)

						__a			:       int					    =  (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]						,				)

						__a			:       Tuple					    =  outputs.reshaped_hidden_states
						self.assertEqual(len(_snake_case							)						,				_snake_case							)

						__a			:       List[str]					    =  reshaped_hidden_states[0].shape
						__a			:       int					    =  (
						    reshaped_hidden_states[0].view(_snake_case						,				_snake_case						,				height * width							).permute(0						,				2						,				1							)
						)
						self.assertListEqual(
						    list(reshaped_hidden_states.shape[-2:]							)						,				[num_patches, self.model_tester.embed_dim]						,				)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Optional[int]					    =  self.model_tester.prepare_config_and_inputs_for_common()

						__a			:       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:
											__a			:       List[str]					    =  True
											self.check_hidden_states_output(_snake_case						,				_snake_case						,				_snake_case						,				_snake_case							)

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											__a			:       str					    =  True

											self.check_hidden_states_output(_snake_case						,				_snake_case						,				_snake_case						,				_snake_case							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       List[str]					    =  self.model_tester.prepare_config_and_inputs_for_common()
						__a			:       List[Any]					    =  3

						__a			:       Tuple					    =  (
						    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)
						)
						__a			:       Optional[int]					    =  (
						    config.patch_size
						    if isinstance(config.patch_size						,				collections.abc.Iterable							)
						    else (config.patch_size, config.patch_size)
						)

						__a			:       Dict					    =  image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
						__a			:       Dict					    =  image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

						for model_class in self.all_model_classes:
											__a			:       str					    =  True
											self.check_hidden_states_output(_snake_case						,				_snake_case						,				_snake_case						,				(padded_height, padded_width)							)

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											__a			:       Dict					    =  True
											self.check_hidden_states_output(_snake_case						,				_snake_case						,				_snake_case						,				(padded_height, padded_width)							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Union[str, Any]					    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_image_modeling(*_snake_case							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Tuple					    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*_snake_case							)




	@slow
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__a			:       Union[str, Any]					    =  SwinvaModel.from_pretrained(_snake_case							)
											self.assertIsNotNone(_snake_case							)





	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       str					    =  self.model_tester.prepare_config_and_inputs_for_common()

						__a			:       Tuple					    =  _config_zero_init(_snake_case							)
						for model_class in self.all_model_classes:
											__a			:       Optional[int]					    =  model_class(config=_snake_case							)
											for name, param in model.named_parameters():
																if "embeddings" not in name and "logit_scale" 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 							__UpperCamelCase				(		unittest.TestCase					):




	@cached_property
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						return (
						    AutoImageProcessor.from_pretrained('microsoft/swinv2-tiny-patch4-window8-256'							)
						    if is_vision_available()
						    else None
						)





	@slow
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       str					    =  SwinvaForImageClassification.from_pretrained('microsoft/swinv2-tiny-patch4-window8-256'							).to(
						    _snake_case							)
						__a			:       Union[str, Any]					    =  self.default_image_processor

						__a			:       List[str]					    =  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							)
						__a			:       Dict					    =  image_processor(images=_snake_case						,				return_tensors='pt'							).to(_snake_case							)

						# forward pass
						with torch.no_grad():
											__a			:       Optional[Any]					    =  model(**_snake_case							)

						# verify the logits
						__a			:       str					    =  torch.Size((1, 1000)							)
						self.assertEqual(outputs.logits.shape						,				_snake_case							)
						__a			:       Dict					    =  torch.tensor([-0.3947, -0.4306, 0.0026]							).to(_snake_case							)
						self.assertTrue(torch.allclose(outputs.logits[0, :3]						,				_snake_case						,				atol=1E-4							)							)








"""simple docstring"""







import collections
import inspect
import unittest

from transformers import SwinvaConfig
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_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 SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
							from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor
class        __A       :







		'''simple docstring'''





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






				"""simple docstring"""



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







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Any:






				"""simple docstring"""



				lowercase__   :    str				=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

				lowercase__   :    Optional[Any]				=			None
				if self.use_labels:
						lowercase__   :    Optional[int]				=			ids_tensor([self.batch_size]			,self.type_sequence_label_size						)

				lowercase__   :    Union[str, Any]				=			self.get_config()

				return config, pixel_values, labels







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






				"""simple docstring"""



				return SwinvaConfig(
				    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			,)







		def 					UpperCAmelCase    (	self						:  str			,_snake_case						:  Dict			,_snake_case						:  List[str]			,_snake_case						:  Optional[int]						)					->		Optional[int]:






				"""simple docstring"""



				lowercase__   :    Any				=			SwinvaModel(config=_snake_case						)
				model.to(_snake_case						)
				model.eval()
				lowercase__   :    str				=			model(_snake_case						)

				lowercase__   :    List[Any]				=			((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
				lowercase__   :    Tuple				=			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 					UpperCAmelCase    (	self						:  Union[str, Any]			,_snake_case						:  List[str]			,_snake_case						:  Optional[Any]			,_snake_case						:  int						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Union[str, Any]				=			SwinvaForMaskedImageModeling(config=_snake_case						)
				model.to(_snake_case						)
				model.eval()
				lowercase__   :    Tuple				=			model(_snake_case						)
				self.parent.assertEqual(
				    result.logits.shape			,(self.batch_size, self.num_channels, self.image_size, self.image_size)						)

				# test greyscale images
				lowercase__   :    Optional[int]				=			1
				lowercase__   :    List[Any]				=			SwinvaForMaskedImageModeling(_snake_case						)
				model.to(_snake_case						)
				model.eval()

				lowercase__   :    Union[str, Any]				=			floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
				lowercase__   :    str				=			model(_snake_case						)
				self.parent.assertEqual(result.logits.shape			,(self.batch_size, 1, self.image_size, self.image_size)						)







		def 					UpperCAmelCase    (	self						:  str			,_snake_case						:  str			,_snake_case						:  str			,_snake_case						:  Tuple						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Tuple				=			self.type_sequence_label_size
				lowercase__   :    Dict				=			SwinvaForImageClassification(_snake_case						)
				model.to(_snake_case						)
				model.eval()
				lowercase__   :    str				=			model(_snake_case			,labels=_snake_case						)
				self.parent.assertEqual(result.logits.shape			,(self.batch_size, self.type_sequence_label_size)						)







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Dict:






				"""simple docstring"""



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





@require_torch
class        __A       (	A_     ,A_     ,unittest.TestCase    ):







		'''simple docstring'''





		lowerCAmelCase			:					Union[str, Any]   	=				(
		    (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
		)
		lowerCAmelCase			:					Optional[int]   	=				(
		    {"feature-extraction": SwinvaModel, "image-classification": SwinvaForImageClassification}
		    if is_torch_available()
		    else {}
		)

		lowerCAmelCase			:					List[Any]   	=				False
		lowerCAmelCase			:					Dict   	=				False
		lowerCAmelCase			:					List[Any]   	=				False
		lowerCAmelCase			:					Any   	=				False







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






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			SwinvaModelTester(self						)
				lowercase__   :    List[str]				=			ConfigTester(self			,config_class=_snake_case			,embed_dim=37						)







		def 					UpperCAmelCase    (	self						:  int						)					->		Any:






				"""simple docstring"""



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






				"""simple docstring"""



				lowercase__   :    int				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_snake_case						)







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






				"""simple docstring"""



				pass







		@unittest.skip(reason='''Swinv2 does not use inputs_embeds'''						)
		def 					UpperCAmelCase    (	self						:  List[str]						)					->		str:






				"""simple docstring"""



				pass







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






				"""simple docstring"""



				lowercase__		,			lowercase__   :    Optional[int]				=			self.model_tester.prepare_config_and_inputs_for_common()

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







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






				"""simple docstring"""



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

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

						lowercase__   :    Tuple				=			['''pixel_values''']
						self.assertListEqual(arg_names[:1]			,_snake_case						)







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






				"""simple docstring"""



				lowercase__		,			lowercase__   :    List[str]				=			self.model_tester.prepare_config_and_inputs_for_common()
				lowercase__   :    Tuple				=			True

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

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

						self.assertListEqual(
						    list(attentions[0].shape[-3:]						)			,[self.model_tester.num_heads[0], window_size_squared, window_size_squared]			,)
						lowercase__   :    Optional[Any]				=			len(_snake_case						)

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

						if hasattr(self.model_tester			,'''num_hidden_states_types'''						):
								lowercase__   :    int				=			self.model_tester.num_hidden_states_types
						else:
								# also another +1 for reshaped_hidden_states
								lowercase__   :    List[str]				=			2
						self.assertEqual(out_len + added_hidden_states			,len(_snake_case						)						)

						lowercase__   :    Optional[int]				=			outputs.attentions

						self.assertEqual(len(_snake_case						)			,_snake_case						)

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







		def 					UpperCAmelCase    (	self						:  List[str]			,_snake_case						:  int			,_snake_case						:  List[str]			,_snake_case						:  Optional[int]			,_snake_case						:  List[Any]						)					->		Union[str, Any]:






				"""simple docstring"""



				lowercase__   :    List[Any]				=			model_class(_snake_case						)
				model.to(_snake_case						)
				model.eval()

				with torch.no_grad():
						lowercase__   :    int				=			model(**self._prepare_for_class(_snake_case			,_snake_case						)						)

				lowercase__   :    Optional[int]				=			outputs.hidden_states

				lowercase__   :    List[Any]				=			getattr(
				    self.model_tester			,'''expected_num_hidden_layers'''			,len(self.model_tester.depths						) + 1						)
				self.assertEqual(len(_snake_case						)			,_snake_case						)

				# Swinv2 has a different seq_length
				lowercase__   :    Dict				=			(
				    config.patch_size
				    if isinstance(config.patch_size			,collections.abc.Iterable						)
				    else (config.patch_size, config.patch_size)
				)

				lowercase__   :    int				=			(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]			,)

				lowercase__   :    Tuple				=			outputs.reshaped_hidden_states
				self.assertEqual(len(_snake_case						)			,_snake_case						)

				lowercase__		,			lowercase__		,			lowercase__		,			lowercase__   :    List[str]				=			reshaped_hidden_states[0].shape
				lowercase__   :    int				=			(
				    reshaped_hidden_states[0].view(_snake_case			,_snake_case			,height * width						).permute(0			,2			,1						)
				)
				self.assertListEqual(
				    list(reshaped_hidden_states.shape[-2:]						)			,[num_patches, self.model_tester.embed_dim]			,)







		def 					UpperCAmelCase    (	self						:  Tuple						)					->		int:






				"""simple docstring"""



				lowercase__		,			lowercase__   :    Optional[int]				=			self.model_tester.prepare_config_and_inputs_for_common()

				lowercase__   :    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:
						lowercase__   :    List[str]				=			True
						self.check_hidden_states_output(_snake_case			,_snake_case			,_snake_case			,_snake_case						)

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						lowercase__   :    str				=			True

						self.check_hidden_states_output(_snake_case			,_snake_case			,_snake_case			,_snake_case						)







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






				"""simple docstring"""



				lowercase__		,			lowercase__   :    List[str]				=			self.model_tester.prepare_config_and_inputs_for_common()
				lowercase__   :    List[Any]				=			3

				lowercase__   :    Tuple				=			(
				    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__   :    Optional[int]				=			(
				    config.patch_size
				    if isinstance(config.patch_size			,collections.abc.Iterable						)
				    else (config.patch_size, config.patch_size)
				)

				lowercase__   :    Dict				=			image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
				lowercase__   :    Dict				=			image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

				for model_class in self.all_model_classes:
						lowercase__   :    str				=			True
						self.check_hidden_states_output(_snake_case			,_snake_case			,_snake_case			,(padded_height, padded_width)						)

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						lowercase__   :    Dict				=			True
						self.check_hidden_states_output(_snake_case			,_snake_case			,_snake_case			,(padded_height, padded_width)						)







		def 					UpperCAmelCase    (	self						:  Tuple						)					->		List[Any]:






				"""simple docstring"""



				lowercase__   :    Union[str, Any]				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_image_modeling(*_snake_case						)







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Any:






				"""simple docstring"""



				lowercase__   :    Tuple				=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*_snake_case						)







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






				"""simple docstring"""



				for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowercase__   :    Union[str, Any]				=			SwinvaModel.from_pretrained(_snake_case						)
						self.assertIsNotNone(_snake_case						)







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






				"""simple docstring"""



				lowercase__		,			lowercase__   :    str				=			self.model_tester.prepare_config_and_inputs_for_common()

				lowercase__   :    Tuple				=			_config_zero_init(_snake_case						)
				for model_class in self.all_model_classes:
						lowercase__   :    Optional[int]				=			model_class(config=_snake_case						)
						for name, param in model.named_parameters():
								if "embeddings" not in name and "logit_scale" 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        __A       (	unittest.TestCase    ):







		'''simple docstring'''





		@cached_property
		def 					UpperCAmelCase    (	self						:  Optional[Any]						)					->		Tuple:






				"""simple docstring"""



				return (
				    AutoImageProcessor.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256'''						)
				    if is_vision_available()
				    else None
				)







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






				"""simple docstring"""



				lowercase__   :    str				=			SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256'''						).to(
				    _snake_case						)
				lowercase__   :    Union[str, Any]				=			self.default_image_processor

				lowercase__   :    List[str]				=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
				lowercase__   :    Dict				=			image_processor(images=_snake_case			,return_tensors='''pt'''						).to(_snake_case						)

				# forward pass
				with torch.no_grad():
						lowercase__   :    Optional[Any]				=			model(**_snake_case						)

				# verify the logits
				lowercase__   :    str				=			torch.Size((1, 1_000)						)
				self.assertEqual(outputs.logits.shape			,_snake_case						)
				lowercase__   :    Dict				=			torch.tensor([-0.3947, -0.4306, 0.0026]						).to(_snake_case						)
				self.assertTrue(torch.allclose(outputs.logits[0, :3]			,_snake_case			,atol=1e-4						)						)
