





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






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


							'''simple docstring'''



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






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


							'''simple docstring'''



							snake_case_						:    int				=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

							snake_case_						:    Optional[Any]				=  self.get_config()

							return config, pixel_values, labels






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


							'''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 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   str:


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  SwinvaModel(config=SCREAMING_SNAKE_CASE_    )
							model.to(SCREAMING_SNAKE_CASE_    )
							model.eval()
							snake_case_						:    int				=  model(SCREAMING_SNAKE_CASE_    )

							snake_case_						:    str				=  ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths    ) - 1))
							snake_case_						:    int				=  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					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   List[str]:


							'''simple docstring'''



							snake_case_						:    Optional[Any]				=  SwinvaForMaskedImageModeling(config=SCREAMING_SNAKE_CASE_    )
							model.to(SCREAMING_SNAKE_CASE_    )
							model.eval()
							snake_case_						:    Optional[int]				=  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
							snake_case_						:    Any				=  1
							snake_case_						:    Optional[Any]				=  SwinvaForMaskedImageModeling(SCREAMING_SNAKE_CASE_    )
							model.to(SCREAMING_SNAKE_CASE_    )
							model.eval()

							snake_case_						:    Any				=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
							snake_case_						:    Any				=  model(SCREAMING_SNAKE_CASE_    )
							self.parent.assertEqual(result.logits.shape					,							(self.batch_size, 1, self.image_size, self.image_size)    )






			def 			lowerCamelCase  (self					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   Optional[Any]:


							'''simple docstring'''



							snake_case_						:    Dict				=  self.type_sequence_label_size
							snake_case_						:    Any				=  SwinvaForImageClassification(SCREAMING_SNAKE_CASE_    )
							model.to(SCREAMING_SNAKE_CASE_    )
							model.eval()
							snake_case_						:    Dict				=  model(SCREAMING_SNAKE_CASE_					,							labels=SCREAMING_SNAKE_CASE_    )
							self.parent.assertEqual(result.logits.shape					,							(self.batch_size, self.type_sequence_label_size)    )






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


							'''simple docstring'''



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




@require_torch
class 					__lowerCAmelCase       (			_UpperCAmelCase,     _UpperCAmelCase,     unittest.TestCase     ):
			lowerCamelCase_     :     int       			=      (
			    (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
			)
			lowerCamelCase_     :     int       			=      (
			    {'feature-extraction': SwinvaModel, 'image-classification': SwinvaForImageClassification}
			    if is_torch_available()
			    else {}
			)

			lowerCamelCase_     :     str       			=      False
			lowerCamelCase_     :     List[str]       			=      False
			lowerCamelCase_     :     Dict       			=      False
			lowerCamelCase_     :     Tuple       			=      False






			def 			lowerCamelCase  (self    )     ->   Dict:


							'''simple docstring'''



							snake_case_						:    Dict				=  SwinvaModelTester(self    )
							snake_case_						:    int				=  ConfigTester(self					,							config_class=SCREAMING_SNAKE_CASE_					,							embed_dim=37    )






			def 			lowerCamelCase  (self    )     ->   Union[str, 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 			lowerCamelCase  (self    )     ->   Any:


							'''simple docstring'''



							snake_case_						:    List[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    )     ->   List[Any]:


							'''simple docstring'''



							pass






			@unittest.skip(reason='''Swinv2 does not use inputs_embeds'''    )
			def 			lowerCamelCase  (self    )     ->   Any:


							'''simple docstring'''



							pass






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


							'''simple docstring'''



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

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






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


							'''simple docstring'''



							snake_case_						:    Any				=  self.model_tester.prepare_config_and_inputs_for_common()

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

											snake_case_						:    Any				=  ['pixel_values']
											self.assertListEqual(arg_names[:1]					,							SCREAMING_SNAKE_CASE_    )






			def 			lowerCamelCase  (self    )     ->   Tuple:


							'''simple docstring'''



							snake_case_						:    str				=  self.model_tester.prepare_config_and_inputs_for_common()
							snake_case_						:    str				=  True

							for model_class in self.all_model_classes:
											snake_case_						:    Tuple				=  True
											snake_case_						:    Any				=  False
											snake_case_						:    int				=  True
											snake_case_						:    List[Any]				=  model_class(SCREAMING_SNAKE_CASE_    )
											model.to(SCREAMING_SNAKE_CASE_    )
											model.eval()
											with torch.no_grad():
															snake_case_						:    int				=  model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_					,							SCREAMING_SNAKE_CASE_    )    )
											snake_case_						:    Dict				=  outputs.attentions
											snake_case_						:    Any				=  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"]
											snake_case_						:    str				=  True
											snake_case_						:    List[str]				=  config.window_size**2
											snake_case_						:    List[str]				=  model_class(SCREAMING_SNAKE_CASE_    )
											model.to(SCREAMING_SNAKE_CASE_    )
											model.eval()
											with torch.no_grad():
															snake_case_						:    List[str]				=  model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_					,							SCREAMING_SNAKE_CASE_    )    )
											snake_case_						:    Any				=  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]					,							)
											snake_case_						:    int				=  len(SCREAMING_SNAKE_CASE_    )

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

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

											snake_case_						:    Tuple				=  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					,							__magic_name__					,							__magic_name__					,							__magic_name__					,							__magic_name__    )     ->   List[str]:


							'''simple docstring'''



							snake_case_						:    Dict				=  model_class(SCREAMING_SNAKE_CASE_    )
							model.to(SCREAMING_SNAKE_CASE_    )
							model.eval()

							with torch.no_grad():
											snake_case_						:    Optional[Any]				=  model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_					,							SCREAMING_SNAKE_CASE_    )    )

							snake_case_						:    List[Any]				=  outputs.hidden_states

							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_    )

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

							snake_case_						:    List[str]				=  (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]					,							)

							snake_case_						:    Tuple				=  outputs.reshaped_hidden_states
							self.assertEqual(len(SCREAMING_SNAKE_CASE_    )					,							SCREAMING_SNAKE_CASE_    )

							snake_case_						:    List[Any]				=  reshaped_hidden_states[0].shape
							snake_case_						:    Optional[int]				=  (
							    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    )     ->   Dict:


							'''simple docstring'''



							snake_case_						:    Tuple				=  self.model_tester.prepare_config_and_inputs_for_common()

							snake_case_						:    Any				=  (
							    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:
											snake_case_						:    Tuple				=  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"]
											snake_case_						:    int				=  True

											self.check_hidden_states_output(SCREAMING_SNAKE_CASE_					,							SCREAMING_SNAKE_CASE_					,							SCREAMING_SNAKE_CASE_					,							SCREAMING_SNAKE_CASE_    )






			def 			lowerCamelCase  (self    )     ->   str:


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  self.model_tester.prepare_config_and_inputs_for_common()
							snake_case_						:    Optional[Any]				=  3

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

							snake_case_						:    Tuple				=  image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
							snake_case_						:    Any				=  image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

							for model_class in self.all_model_classes:
											snake_case_						:    Union[str, Any]				=  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"]
											snake_case_						:    Tuple				=  True
											self.check_hidden_states_output(SCREAMING_SNAKE_CASE_					,							SCREAMING_SNAKE_CASE_					,							SCREAMING_SNAKE_CASE_					,							(padded_height, padded_width)    )






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


							'''simple docstring'''



							snake_case_						:    str				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE_    )






			def 			lowerCamelCase  (self    )     ->   str:


							'''simple docstring'''



							snake_case_						:    Any				=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_    )






			@slow
			def 			lowerCamelCase  (self    )     ->   Dict:


							'''simple docstring'''



							for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											snake_case_						:    Optional[int]				=  SwinvaModel.from_pretrained(SCREAMING_SNAKE_CASE_    )
											self.assertIsNotNone(SCREAMING_SNAKE_CASE_    )






			def 			lowerCamelCase  (self    )     ->   Tuple:


							'''simple docstring'''



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

							snake_case_						:    Any				=  _config_zero_init(SCREAMING_SNAKE_CASE_    )
							for model_class in self.all_model_classes:
											snake_case_						:    str				=  model_class(config=SCREAMING_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 					__lowerCAmelCase       (			unittest.TestCase     ):






			@cached_property
			def 			lowerCamelCase  (self    )     ->   Tuple:


							'''simple docstring'''



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






			@slow
			def 			lowerCamelCase  (self    )     ->   Optional[int]:


							'''simple docstring'''



							snake_case_						:    List[str]				=  SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256'''    ).to(
							    SCREAMING_SNAKE_CASE_    )
							snake_case_						:    Tuple				=  self.default_image_processor

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

							# forward pass
							with torch.no_grad():
											snake_case_						:    str				=  model(**SCREAMING_SNAKE_CASE_    )

							# verify the logits
							snake_case_						:    Optional[Any]				=  torch.Size((1, 1000)    )
							self.assertEqual(outputs.logits.shape					,							SCREAMING_SNAKE_CASE_    )
							snake_case_						:    str				=  torch.tensor([-0.3_947, -0.4_306, 0.0_026]    ).to(SCREAMING_SNAKE_CASE_    )
							self.assertTrue(torch.allclose(outputs.logits[0, :3]					,							SCREAMING_SNAKE_CASE_					,							atol=1e-4    )    )




'''simple docstring'''



import unittest

from knapsack import knapsack as k





class    UpperCAmelCase_  (unittest.TestCase  ):





    """simple docstring"""






    def       lowercase_				(							self	)				->     Optional[Any]:
         __lowerCamelCase   :	int								=		0
         __lowerCamelCase   :	Union[str, Any]								=		[0]
         __lowerCamelCase   :	Any								=		[0]
         __lowerCamelCase   :	Union[str, Any]								=		len(SCREAMING_SNAKE_CASE_	)
         self.assertEqual(k.knapsack(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)			,     0	)

         __lowerCamelCase   :	List[str]								=		[60]
         __lowerCamelCase   :	Union[str, Any]								=		[10]
         __lowerCamelCase   :	List[Any]								=		len(SCREAMING_SNAKE_CASE_	)
         self.assertEqual(k.knapsack(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)			,     0	)







    def       lowercase_				(							self	)				->     List[str]:
         __lowerCamelCase   :	Optional[int]								=		3
         __lowerCamelCase   :	int								=		[1, 2, 3]
         __lowerCamelCase   :	str								=		[3, 2, 1]
         __lowerCamelCase   :	Union[str, Any]								=		len(SCREAMING_SNAKE_CASE_	)
         self.assertEqual(k.knapsack(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)			,     5	)







    def       lowercase_				(							self	)				->     int:
         __lowerCamelCase   :	Optional[int]								=		50
         __lowerCamelCase   :	List[str]								=		[60, 1_00, 1_20]
         __lowerCamelCase   :	List[str]								=		[10, 20, 30]
         __lowerCamelCase   :	List[str]								=		len(SCREAMING_SNAKE_CASE_	)
         self.assertEqual(k.knapsack(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)			,     2_20	)


if __name__ == "__main__":
      unittest.main()






