



'''simple docstring'''



import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
							import torch

							from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin
class 	lowerCamelCase_							:







						def __init__(  self   :      List[str]				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      Tuple=None				,    lowerCAmelCase__   :      Dict=None				,    lowerCAmelCase__   :      Tuple=None				,    lowerCAmelCase__   :      List[Any]="resnet50"				,    lowerCAmelCase__   :      Optional[Any]=3				,    lowerCAmelCase__   :      Any=32				,    lowerCAmelCase__   :      Union[str, Any]=3				,    lowerCAmelCase__   :      Any=True				,    lowerCAmelCase__   :      Dict=True				,    ):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      List[str]     					=			parent
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			out_indices if out_indices is not None else [4]
													SCREAMING_SNAKE_CASE					:      str     					=			stage_names
													SCREAMING_SNAKE_CASE					:      Tuple     					=			out_features
													SCREAMING_SNAKE_CASE					:      str     					=			backbone
													SCREAMING_SNAKE_CASE					:      Dict     					=			batch_size
													SCREAMING_SNAKE_CASE					:      Dict     					=			image_size
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			num_channels
													SCREAMING_SNAKE_CASE					:      Tuple     					=			use_pretrained_backbone
													SCREAMING_SNAKE_CASE					:      Any     					=			is_training







						def 					__lowercase    (  self   :      str				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			self.get_config()

													return config, pixel_values







						def 					__lowercase    (  self   :      Optional[int]				):

													"""simple docstring"""
													return TimmBackboneConfig(
													    image_size=self.image_size				,    num_channels=self.num_channels				,    out_features=self.out_features				,    out_indices=self.out_indices				,    stage_names=self.stage_names				,    use_pretrained_backbone=self.use_pretrained_backbone				,    backbone=self.backbone				,    )







						def 					__lowercase    (  self   :      Union[str, Any]				,    lowerCAmelCase__   :      Any				,    lowerCAmelCase__   :      Tuple				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			TimmBackbone(config=lowerCAmelCase__				)
													model.to(lowerCAmelCase__				)
													model.eval()
													with torch.no_grad():
																				SCREAMING_SNAKE_CASE					:      Dict     					=			model(lowerCAmelCase__				)
													self.parent.assertEqual(
													    result.feature_map[-1].shape				,    (self.batch_size, model.channels[-1], 14, 14)				,    )







						def 					__lowercase    (  self   :      str				):

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


@require_torch
@require_timm
class 	lowerCamelCase_							(					snake_case_    ,				snake_case_    ,				snake_case_    ,				unittest.TestCase			):
						_lowerCAmelCase     :   Any	     =				(TimmBackbone,) if is_torch_available() else ()
						_lowerCAmelCase     :   Union[str, Any]	     =				{'feature-extraction': TimmBackbone} if is_torch_available() else {}
						_lowerCAmelCase     :   Optional[Any]	     =				False
						_lowerCAmelCase     :   List[str]	     =				False
						_lowerCAmelCase     :   Optional[int]	     =				False
						_lowerCAmelCase     :   Optional[Any]	     =				False







						def 					__lowercase    (  self   :      List[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			TimmBackboneModelTester(self				)
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			ConfigTester(self				,    config_class=lowerCAmelCase__				,    has_text_modality=lowerCAmelCase__				)







						def 					__lowercase    (  self   :      str				):

													"""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 					__lowercase    (  self   :      Union[str, Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      int     					=			'''resnet18'''
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			'''microsoft/resnet-18'''

													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			AutoBackbone.from_pretrained(lowerCAmelCase__				,    use_timm_backbone=lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			AutoBackbone.from_pretrained(lowerCAmelCase__				)

													self.assertEqual(len(timm_model.out_features				)				,    len(transformers_model.out_features				)				)
													self.assertEqual(len(timm_model.stage_names				)				,    len(transformers_model.stage_names				)				)
													self.assertEqual(timm_model.channels				,    transformers_model.channels				)
													# Out indices are set to the last layer by default. For timm models, we don't know
													# the number of layers in advance, so we set it to (-1,), whereas for transformers
													# models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
													self.assertEqual(timm_model.out_indices				,    (-1,)				)
													self.assertEqual(transformers_model.out_indices				,    [len(timm_model.stage_names				) - 1]				)

													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			AutoBackbone.from_pretrained(lowerCAmelCase__				,    use_timm_backbone=lowerCAmelCase__				,    out_indices=[1, 2, 3]				)
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			AutoBackbone.from_pretrained(lowerCAmelCase__				,    out_indices=[1, 2, 3]				)

													self.assertEqual(timm_model.out_indices				,    transformers_model.out_indices				)
													self.assertEqual(len(timm_model.out_features				)				,    len(transformers_model.out_features				)				)
													self.assertEqual(timm_model.channels				,    transformers_model.channels				)







						@unittest.skip('''TimmBackbone doesn\'t support feed forward chunking'''				)
						def 					__lowercase    (  self   :      Optional[Any]				):

													"""simple docstring"""
													pass







						@unittest.skip('''TimmBackbone doesn\'t have num_hidden_layers attribute'''				)
						def 					__lowercase    (  self   :      Any				):

													"""simple docstring"""
													pass







						@unittest.skip('''TimmBackbone initialization is managed on the timm side'''				)
						def 					__lowercase    (  self   :      str				):

													"""simple docstring"""
													pass







						@unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds'''				)
						def 					__lowercase    (  self   :      List[Any]				):

													"""simple docstring"""
													pass







						@unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds'''				)
						def 					__lowercase    (  self   :      int				):

													"""simple docstring"""
													pass







						@unittest.skip('''TimmBackbone model cannot be created without specifying a backbone checkpoint'''				)
						def 					__lowercase    (  self   :      Dict				):

													"""simple docstring"""
													pass







						@unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone'''				)
						def 					__lowercase    (  self   :      List[str]				):

													"""simple docstring"""
													pass







						@unittest.skip('''model weights aren\'t tied in TimmBackbone.'''				)
						def 					__lowercase    (  self   :      Union[str, Any]				):

													"""simple docstring"""
													pass







						@unittest.skip('''model weights aren\'t tied in TimmBackbone.'''				)
						def 					__lowercase    (  self   :      Optional[int]				):

													"""simple docstring"""
													pass







						@unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone'''				)
						def 					__lowercase    (  self   :      List[str]				):

													"""simple docstring"""
													pass







						@unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone'''				)
						def 					__lowercase    (  self   :      Optional[int]				):

													"""simple docstring"""
													pass







						@unittest.skip('''TimmBackbone doesn\'t have hidden size info in its configuration.'''				)
						def 					__lowercase    (  self   :      Optional[int]				):

													"""simple docstring"""
													pass







						@unittest.skip('''TimmBackbone doesn\'t support output_attentions.'''				)
						def 					__lowercase    (  self   :      List[Any]				):

													"""simple docstring"""
													pass







						@unittest.skip('''Safetensors is not supported by timm.'''				)
						def 					__lowercase    (  self   :      Dict				):

													"""simple docstring"""
													pass







						@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''				)
						def 					__lowercase    (  self   :      Optional[Any]				):

													"""simple docstring"""
													pass







						def 					__lowercase    (  self   :      Any				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      int     					=			self.model_tester.prepare_config_and_inputs_for_common()

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

																				SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			['''pixel_values''']
																				self.assertListEqual(arg_names[:1]				,    lowerCAmelCase__				)







						def 					__lowercase    (  self   :      Optional[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      Optional[int]     					=			self.model_tester.prepare_config_and_inputs_for_common()
													SCREAMING_SNAKE_CASE					:      Any     					=			True
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			self.has_attentions

													# no need to test all models as different heads yield the same functionality
													SCREAMING_SNAKE_CASE					:      Any     					=			self.all_model_classes[0]
													SCREAMING_SNAKE_CASE					:      List[str]     					=			model_class(lowerCAmelCase__				)
													model.to(lowerCAmelCase__				)

													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			self._prepare_for_class(lowerCAmelCase__				,    lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Tuple     					=			model(**lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			outputs[0][-1]

													# Encoder-/Decoder-only models
													SCREAMING_SNAKE_CASE					:      List[str]     					=			outputs.hidden_states[0]
													hidden_states.retain_grad()

													if self.has_attentions:
																				SCREAMING_SNAKE_CASE					:      Optional[int]     					=			outputs.attentions[0]
																				attentions.retain_grad()

													output.flatten()[0].backward(retain_graph=lowerCAmelCase__				)

													self.assertIsNotNone(hidden_states.grad				)

													if self.has_attentions:
																				self.assertIsNotNone(attentions.grad				)







						def 					__lowercase    (  self   :      str				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      Dict     					=			self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				SCREAMING_SNAKE_CASE					:      str     					=			model_class(lowerCAmelCase__				)
																				model.to(lowerCAmelCase__				)
																				model.eval()
																				SCREAMING_SNAKE_CASE					:      List[str]     					=			model(**lowerCAmelCase__				)

																				self.assertEqual(len(result.feature_maps				)				,    len(config.out_indices				)				)
																				self.assertEqual(len(model.channels				)				,    len(config.out_indices				)				)

																				# Check output of last stage is taken if out_features=None, out_indices=None
																				SCREAMING_SNAKE_CASE					:      str     					=			copy.deepcopy(lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      Tuple     					=			None
																				SCREAMING_SNAKE_CASE					:      int     					=			model_class(lowerCAmelCase__				)
																				model.to(lowerCAmelCase__				)
																				model.eval()
																				SCREAMING_SNAKE_CASE					:      List[str]     					=			model(**lowerCAmelCase__				)

																				self.assertEqual(len(result.feature_maps				)				,    1				)
																				self.assertEqual(len(model.channels				)				,    1				)

																				# Check backbone can be initialized with fresh weights
																				SCREAMING_SNAKE_CASE					:      str     					=			copy.deepcopy(lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      Optional[int]     					=			False
																				SCREAMING_SNAKE_CASE					:      Dict     					=			model_class(lowerCAmelCase__				)
																				model.to(lowerCAmelCase__				)
																				model.eval()
																				SCREAMING_SNAKE_CASE					:      Any     					=			model(**lowerCAmelCase__				)









'''simple docstring'''



from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase_     :       Optional[int]								=						logging.get_logger(__name__)

lowerCAmelCase_     :       Optional[Any]								=						{
    'microsoft/swinv2-tiny-patch4-window8-256': (
        'https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json'
    ),
}
class 	lowerCamelCase_							(					snake_case_			):
						_lowerCAmelCase     :   str	     =				'swinv2'

						_lowerCAmelCase     :   Optional[int]	     =				{
						    'num_attention_heads': 'num_heads',
						    'num_hidden_layers': 'num_layers',
						}







						def __init__(  self   :      Optional[int]				,    lowerCAmelCase__   :      Any=2_24				,    lowerCAmelCase__   :      Optional[Any]=4				,    lowerCAmelCase__   :      Tuple=3				,    lowerCAmelCase__   :      Tuple=96				,    lowerCAmelCase__   :      Optional[int]=[2, 2, 6, 2]				,    lowerCAmelCase__   :      Tuple=[3, 6, 12, 24]				,    lowerCAmelCase__   :      List[str]=7				,    lowerCAmelCase__   :      Tuple=4.0				,    lowerCAmelCase__   :      Dict=True				,    lowerCAmelCase__   :      Any=0.0				,    lowerCAmelCase__   :      Optional[int]=0.0				,    lowerCAmelCase__   :      Optional[Any]=0.1				,    lowerCAmelCase__   :      int="gelu"				,    lowerCAmelCase__   :      Tuple=False				,    lowerCAmelCase__   :      Tuple=0.02				,    lowerCAmelCase__   :      Dict=1e-5				,    lowerCAmelCase__   :      List[str]=32				,    **lowerCAmelCase__   :      Union[str, Any]				,    ):

													"""simple docstring"""
													super().__init__(**lowerCAmelCase__				)

													SCREAMING_SNAKE_CASE					:      int     					=			image_size
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			patch_size
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			num_channels
													SCREAMING_SNAKE_CASE					:      int     					=			embed_dim
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			depths
													SCREAMING_SNAKE_CASE					:      str     					=			len(lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			num_heads
													SCREAMING_SNAKE_CASE					:      Tuple     					=			window_size
													SCREAMING_SNAKE_CASE					:      Any     					=			mlp_ratio
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			qkv_bias
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			hidden_dropout_prob
													SCREAMING_SNAKE_CASE					:      int     					=			attention_probs_dropout_prob
													SCREAMING_SNAKE_CASE					:      List[str]     					=			drop_path_rate
													SCREAMING_SNAKE_CASE					:      Any     					=			hidden_act
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			use_absolute_embeddings
													SCREAMING_SNAKE_CASE					:      List[str]     					=			layer_norm_eps
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			initializer_range
													SCREAMING_SNAKE_CASE					:      Dict     					=			encoder_stride
													# we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel
													# this indicates the channel dimension after the last stage of the model
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			int(embed_dim * 2 ** (len(lowerCAmelCase__				) - 1)				)
													SCREAMING_SNAKE_CASE					:      int     					=			(0, 0, 0, 0)




