


'''simple docstring'''



import inspect
import unittest
import warnings
from math import ceil, floor

from transformers import LevitConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import (
	    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
	    MODEL_MAPPING,
	    LevitForImageClassification,
	    LevitForImageClassificationWithTeacher,
	    LevitModel,
	)
	from transformers.models.levit.modeling_levit import LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import LevitImageProcessor






class 	UpperCamelCase__(       lowerCAmelCase	):




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

										"""simple docstring"""




										UpperCAmelCase					    =     self.config_class(**self.inputs_dict				)
										self.parent.assertTrue(hasattr(lowerCAmelCase				,   '''hidden_sizes'''				)				)
										self.parent.assertTrue(hasattr(lowerCAmelCase				,   '''num_attention_heads'''				)				)







class 	UpperCamelCase__:




				def __init__(		self      :			Optional[int]				,   lowerCAmelCase      :			Optional[int]				,   lowerCAmelCase      :			str=13				,   lowerCAmelCase      :			int=64				,   lowerCAmelCase      :			str=3				,   lowerCAmelCase      :			Tuple=3				,   lowerCAmelCase      :			Optional[Any]=2				,   lowerCAmelCase      :			Tuple=1				,   lowerCAmelCase      :			Union[str, Any]=16				,   lowerCAmelCase      :			Any=[128, 256, 384]				,   lowerCAmelCase      :			List[Any]=[4, 6, 8]				,   lowerCAmelCase      :			Union[str, Any]=[2, 3, 4]				,   lowerCAmelCase      :			Union[str, Any]=[16, 16, 16]				,   lowerCAmelCase      :			Optional[Any]=0				,   lowerCAmelCase      :			List[str]=[2, 2, 2]				,   lowerCAmelCase      :			int=[2, 2, 2]				,   lowerCAmelCase      :			List[str]=0.02				,   lowerCAmelCase      :			Tuple=True				,   lowerCAmelCase      :			int=True				,   lowerCAmelCase      :			Dict=2				,   )->					List[Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     parent
										UpperCAmelCase					    =     batch_size
										UpperCAmelCase					    =     image_size
										UpperCAmelCase					    =     num_channels
										UpperCAmelCase					    =     kernel_size
										UpperCAmelCase					    =     stride
										UpperCAmelCase					    =     padding
										UpperCAmelCase					    =     hidden_sizes
										UpperCAmelCase					    =     num_attention_heads
										UpperCAmelCase					    =     depths
										UpperCAmelCase					    =     key_dim
										UpperCAmelCase					    =     drop_path_rate
										UpperCAmelCase					    =     patch_size
										UpperCAmelCase					    =     attention_ratio
										UpperCAmelCase					    =     mlp_ratio
										UpperCAmelCase					    =     initializer_range
										UpperCAmelCase					    =     [
										    ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
										    ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
										]
										UpperCAmelCase					    =     is_training
										UpperCAmelCase					    =     use_labels
										UpperCAmelCase					    =     num_labels
										UpperCAmelCase					    =     initializer_range




				def 					a__(		self      :			str				)->					str:

										"""simple docstring"""




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

										UpperCAmelCase					    =     None
										if self.use_labels:
																UpperCAmelCase					    =     ids_tensor([self.batch_size]				,   self.num_labels				)

										UpperCAmelCase					    =     self.get_config()
										return config, pixel_values, labels




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

										"""simple docstring"""




										return LevitConfig(
										    image_size=self.image_size				,   num_channels=self.num_channels				,   kernel_size=self.kernel_size				,   stride=self.stride				,   padding=self.padding				,   patch_size=self.patch_size				,   hidden_sizes=self.hidden_sizes				,   num_attention_heads=self.num_attention_heads				,   depths=self.depths				,   key_dim=self.key_dim				,   drop_path_rate=self.drop_path_rate				,   mlp_ratio=self.mlp_ratio				,   attention_ratio=self.attention_ratio				,   initializer_range=self.initializer_range				,   down_ops=self.down_ops				,   )




				def 					a__(		self      :			str				,   lowerCAmelCase      :			Union[str, Any]				,   lowerCAmelCase      :			Optional[Any]				,   lowerCAmelCase      :			int				)->					Optional[Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     LevitModel(config=lowerCAmelCase				)
										model.to(lowerCAmelCase				)
										model.eval()
										UpperCAmelCase					    =     model(lowerCAmelCase				)
										UpperCAmelCase					    =     (self.image_size, self.image_size)
										UpperCAmelCase    ,    UpperCAmelCase					    =     image_size[0], image_size[1]
										for _ in range(4				):
																UpperCAmelCase					    =     floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1				)
																UpperCAmelCase					    =     floor(((width + 2 * self.padding - self.kernel_size) / self.stride) + 1				)
										self.parent.assertEqual(
										    result.last_hidden_state.shape				,   (self.batch_size, ceil(height / 4				) * ceil(width / 4				), self.hidden_sizes[-1])				,   )




				def 					a__(		self      :			Optional[int]				,   lowerCAmelCase      :			Optional[Any]				,   lowerCAmelCase      :			Any				,   lowerCAmelCase      :			str				)->					Tuple:

										"""simple docstring"""




										UpperCAmelCase					    =     self.num_labels
										UpperCAmelCase					    =     LevitForImageClassification(lowerCAmelCase				)
										model.to(lowerCAmelCase				)
										model.eval()
										UpperCAmelCase					    =     model(lowerCAmelCase				,   labels=lowerCAmelCase				)
										self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.num_labels)				)





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

										"""simple docstring"""




										UpperCAmelCase					    =     self.prepare_config_and_inputs()
										UpperCAmelCase    ,    UpperCAmelCase    ,    UpperCAmelCase					    =     config_and_inputs
										UpperCAmelCase					    =     {'''pixel_values''': pixel_values}
										return config, inputs_dict







@require_torch
class 	UpperCamelCase__(       lowerCAmelCase						,    lowerCAmelCase						,    unittest.TestCase	):
				__magic_name__    :				str  		=	(
				    (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher)
				    if is_torch_available()
				    else ()
				)
				__magic_name__    :				List[Any]  		=	(
				    {
				        "feature-extraction": LevitModel,
				        "image-classification": (LevitForImageClassification, LevitForImageClassificationWithTeacher),
				    }
				    if is_torch_available()
				    else {}
				)

				__magic_name__    :				Optional[int]  		=	False
				__magic_name__    :				str  		=	False
				__magic_name__    :				List[Any]  		=	False
				__magic_name__    :				Optional[int]  		=	False
				__magic_name__    :				Tuple  		=	False




				def 					a__(		self      :			str				)->					str:

										"""simple docstring"""




										UpperCAmelCase					    =     LevitModelTester(self				)
										UpperCAmelCase					    =     ConfigTester(self				,   config_class=lowerCAmelCase				,   has_text_modality=lowerCAmelCase				,   hidden_size=37				)




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

										"""simple docstring"""




										self.create_and_test_config_common_properties()
										self.config_tester.create_and_test_config_to_json_string()
										self.config_tester.create_and_test_config_to_json_file()
										self.config_tester.create_and_test_config_from_and_save_pretrained()
										self.config_tester.create_and_test_config_with_num_labels()
										self.config_tester.check_config_can_be_init_without_params()
										self.config_tester.check_config_arguments_init()




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

										"""simple docstring"""




										return




				@unittest.skip(reason='''Levit does not use inputs_embeds'''				)
				def 					a__(		self      :			Any				)->					str:

										"""simple docstring"""




										pass




				@unittest.skip(reason='''Levit does not support input and output embeddings'''				)
				def 					a__(		self      :			List[str]				)->					List[Any]:

										"""simple docstring"""




										pass




				@unittest.skip(reason='''Levit does not output attentions'''				)
				def 					a__(		self      :			List[Any]				)->					Optional[Any]:

										"""simple docstring"""




										pass




				def 					a__(		self      :			str				)->					Dict:

										"""simple docstring"""




										UpperCAmelCase    ,    UpperCAmelCase					    =     self.model_tester.prepare_config_and_inputs_for_common()

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

																UpperCAmelCase					    =     ['''pixel_values''']
																self.assertListEqual(arg_names[:1]				,   lowerCAmelCase				)




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

										"""simple docstring"""




										def check_hidden_states_output(lowerCAmelCase      :			List[str]				,   lowerCAmelCase      :			List[Any]				,   lowerCAmelCase      :			List[Any]				):
																UpperCAmelCase					    =     model_class(lowerCAmelCase				)
																model.to(lowerCAmelCase				)
																model.eval()

																with torch.no_grad():
																						UpperCAmelCase					    =     model(**self._prepare_for_class(lowerCAmelCase				,   lowerCAmelCase				)				)

																UpperCAmelCase					    =     outputs.hidden_states

																UpperCAmelCase					    =     len(self.model_tester.depths				) + 1
																self.assertEqual(len(lowerCAmelCase				)				,   lowerCAmelCase				)

																UpperCAmelCase					    =     (self.model_tester.image_size, self.model_tester.image_size)
																UpperCAmelCase    ,    UpperCAmelCase					    =     image_size[0], image_size[1]
																for _ in range(4				):
																						UpperCAmelCase					    =     floor(
																						    (
																						        (height + 2 * self.model_tester.padding - self.model_tester.kernel_size)
																						        / self.model_tester.stride
																						    )
																						    + 1				)
																						UpperCAmelCase					    =     floor(
																						    (
																						        (width + 2 * self.model_tester.padding - self.model_tester.kernel_size)
																						        / self.model_tester.stride
																						    )
																						    + 1				)
																# verify the first hidden states (first block)
																self.assertListEqual(
																    list(hidden_states[0].shape[-2:]				)				,   [
																        height * width,
																        self.model_tester.hidden_sizes[0],
																    ]				,   )

										UpperCAmelCase    ,    UpperCAmelCase					    =     self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																UpperCAmelCase					    =     True
																check_hidden_states_output(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				)

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

																check_hidden_states_output(lowerCAmelCase				,   lowerCAmelCase				,   lowerCAmelCase				)




				@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''				)
				def 					a__(		self      :			Optional[int]				)->					Union[str, Any]:

										"""simple docstring"""




										pass




				def 					a__(		self      :			Any				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			int				,   lowerCAmelCase      :			Union[str, Any]=False				)->					Tuple:

										"""simple docstring"""




										UpperCAmelCase					    =     super()._prepare_for_class(lowerCAmelCase				,   lowerCAmelCase				,   return_labels=lowerCAmelCase				)

										if return_labels:
																if model_class.__name__ == "LevitForImageClassificationWithTeacher":
																						del inputs_dict["labels"]

										return inputs_dict




				def 					a__(		self      :			int				)->					str:

										"""simple docstring"""




										UpperCAmelCase					    =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase				)




				def 					a__(		self      :			Any				)->					Dict:

										"""simple docstring"""




										UpperCAmelCase					    =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase				)




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

										"""simple docstring"""




										if not self.model_tester.is_training:
																return

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

										for model_class in self.all_model_classes:
																# LevitForImageClassificationWithTeacher supports inference-only
																if (
																    model_class in get_values(lowerCAmelCase				)
																    or model_class.__name__ == "LevitForImageClassificationWithTeacher"
																):
																						continue
																UpperCAmelCase					    =     model_class(lowerCAmelCase				)
																model.to(lowerCAmelCase				)
																model.train()
																UpperCAmelCase					    =     self._prepare_for_class(lowerCAmelCase				,   lowerCAmelCase				,   return_labels=lowerCAmelCase				)
																UpperCAmelCase					    =     model(**lowerCAmelCase				).loss
																loss.backward()




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

										"""simple docstring"""




										UpperCAmelCase    ,    UpperCAmelCase					    =     self.model_tester.prepare_config_and_inputs_for_common()
										if not self.model_tester.is_training:
																return

										UpperCAmelCase					    =     False
										UpperCAmelCase					    =     True

										for model_class in self.all_model_classes:
																if model_class in get_values(lowerCAmelCase				) or not model_class.supports_gradient_checkpointing:
																						continue
																# LevitForImageClassificationWithTeacher supports inference-only
																if model_class.__name__ == "LevitForImageClassificationWithTeacher":
																						continue
																UpperCAmelCase					    =     model_class(lowerCAmelCase				)
																model.gradient_checkpointing_enable()
																model.to(lowerCAmelCase				)
																model.train()
																UpperCAmelCase					    =     self._prepare_for_class(lowerCAmelCase				,   lowerCAmelCase				,   return_labels=lowerCAmelCase				)
																UpperCAmelCase					    =     model(**lowerCAmelCase				).loss
																loss.backward()




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

										"""simple docstring"""




										UpperCAmelCase    ,    UpperCAmelCase					    =     self.model_tester.prepare_config_and_inputs_for_common()

										UpperCAmelCase					    =     [
										    {'''title''': '''multi_label_classification''', '''num_labels''': 2, '''dtype''': torch.float},
										    {'''title''': '''single_label_classification''', '''num_labels''': 1, '''dtype''': torch.long},
										    {'''title''': '''regression''', '''num_labels''': 1, '''dtype''': torch.float},
										]

										for model_class in self.all_model_classes:
																if (
																    model_class
																    not in [
																        *get_values(lowerCAmelCase				),
																    ]
																    or model_class.__name__ == "LevitForImageClassificationWithTeacher"
																):
																						continue

																for problem_type in problem_types:
																						with self.subTest(msg=F"""Testing {model_class} with {problem_type["title"]}"""				):
																												UpperCAmelCase					    =     problem_type['''title''']
																												UpperCAmelCase					    =     problem_type['''num_labels''']

																												UpperCAmelCase					    =     model_class(lowerCAmelCase				)
																												model.to(lowerCAmelCase				)
																												model.train()

																												UpperCAmelCase					    =     self._prepare_for_class(lowerCAmelCase				,   lowerCAmelCase				,   return_labels=lowerCAmelCase				)

																												if problem_type["num_labels"] > 1:
																																		UpperCAmelCase					    =     inputs['''labels'''].unsqueeze(1				).repeat(1				,   problem_type['''num_labels''']				)

																												UpperCAmelCase					    =     inputs['''labels'''].to(problem_type['''dtype''']				)

																												# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
																												# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
																												# they have the same size." which is a symptom something in wrong for the regression problem.
																												# See https://github.com/huggingface/transformers/issues/11780
																												with warnings.catch_warnings(record=lowerCAmelCase				) as warning_list:
																																		UpperCAmelCase					    =     model(**lowerCAmelCase				).loss
																												for w in warning_list:
																																		if "Using a target size that is different to the input size" in str(w.message				):
																																								raise ValueError(
																																								    F"""Something is going wrong in the regression problem: intercepted {w.message}"""				)

																												loss.backward()





				@slow
				def 					a__(		self      :			Optional[int]				)->					List[Any]:

										"""simple docstring"""




										for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																UpperCAmelCase					    =     LevitModel.from_pretrained(lowerCAmelCase				)
																self.assertIsNotNone(lowerCAmelCase				)







def 				lowerCamelCase__				(	):




						'''simple docstring'''






						UpperCAmelCase					    =     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					)
						return image






@require_torch
@require_vision
class 	UpperCamelCase__(       unittest.TestCase	):




				@cached_property
				def 					a__(		self      :			int				)->					Optional[int]:

										"""simple docstring"""




										return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]				)





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

										"""simple docstring"""




										UpperCAmelCase					    =     LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]				).to(
										    lowerCAmelCase				)

										UpperCAmelCase					    =     self.default_image_processor
										UpperCAmelCase					    =     prepare_img()
										UpperCAmelCase					    =     image_processor(images=lowerCAmelCase				,   return_tensors='''pt'''				).to(lowerCAmelCase				)

										# forward pass
										with torch.no_grad():
																UpperCAmelCase					    =     model(**lowerCAmelCase				)

										# verify the logits
										UpperCAmelCase					    =     torch.Size((1, 1000)				)
										self.assertEqual(outputs.logits.shape				,   lowerCAmelCase				)

										UpperCAmelCase					    =     torch.tensor([1.0448, -0.3745, -1.8317]				).to(lowerCAmelCase				)

										self.assertTrue(torch.allclose(outputs.logits[0, :3]				,   lowerCAmelCase				,   atol=1E-4				)				)









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




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

										"""simple docstring"""




										UpperCAmelCase					    =     parent
										UpperCAmelCase					    =     out_indices if out_indices is not None else [4]
										UpperCAmelCase					    =     stage_names
										UpperCAmelCase					    =     out_features
										UpperCAmelCase					    =     backbone
										UpperCAmelCase					    =     batch_size
										UpperCAmelCase					    =     image_size
										UpperCAmelCase					    =     num_channels
										UpperCAmelCase					    =     use_pretrained_backbone
										UpperCAmelCase					    =     is_training




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

										"""simple docstring"""




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

										return config, pixel_values




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

										"""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 					a__(		self      :			Dict				,   lowerCAmelCase      :			str				,   lowerCAmelCase      :			Dict				)->					Tuple:

										"""simple docstring"""




										UpperCAmelCase					    =     TimmBackbone(config=lowerCAmelCase				)
										model.to(lowerCAmelCase				)
										model.eval()
										with torch.no_grad():
																UpperCAmelCase					    =     model(lowerCAmelCase				)
										self.parent.assertEqual(
										    result.feature_map[-1].shape				,   (self.batch_size, model.channels[-1], 14, 14)				,   )





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

										"""simple docstring"""




										UpperCAmelCase					    =     self.prepare_config_and_inputs()
										UpperCAmelCase    ,    UpperCAmelCase					    =     config_and_inputs
										UpperCAmelCase					    =     {'''pixel_values''': pixel_values}
										return config, inputs_dict







@require_torch
@require_timm
class 	UpperCamelCase__(       lowerCAmelCase						,    lowerCAmelCase						,    lowerCAmelCase						,    unittest.TestCase	):
				__magic_name__    :				List[str]  		=	(TimmBackbone,) if is_torch_available() else ()
				__magic_name__    :				Any  		=	{"feature-extraction": TimmBackbone} if is_torch_available() else {}
				__magic_name__    :				Union[str, Any]  		=	False
				__magic_name__    :				int  		=	False
				__magic_name__    :				Tuple  		=	False
				__magic_name__    :				List[str]  		=	False




				def 					a__(		self      :			int				)->					str:

										"""simple docstring"""




										UpperCAmelCase					    =     TimmBackboneModelTester(self				)
										UpperCAmelCase					    =     ConfigTester(self				,   config_class=lowerCAmelCase				,   has_text_modality=lowerCAmelCase				)




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

										"""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 					a__(		self      :			List[Any]				)->					Optional[Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     '''resnet18'''
										UpperCAmelCase					    =     '''microsoft/resnet-18'''

										UpperCAmelCase					    =     AutoBackbone.from_pretrained(lowerCAmelCase				,   use_timm_backbone=lowerCAmelCase				)
										UpperCAmelCase					    =     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]				)

										UpperCAmelCase					    =     AutoBackbone.from_pretrained(lowerCAmelCase				,   use_timm_backbone=lowerCAmelCase				,   out_indices=[1, 2, 3]				)
										UpperCAmelCase					    =     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 					a__(		self      :			Union[str, Any]				)->					Tuple:

										"""simple docstring"""




										pass




				@unittest.skip('''TimmBackbone doesn\'t have num_hidden_layers attribute'''				)
				def 					a__(		self      :			Dict				)->					Dict:

										"""simple docstring"""




										pass




				@unittest.skip('''TimmBackbone initialization is managed on the timm side'''				)
				def 					a__(		self      :			Tuple				)->					List[Any]:

										"""simple docstring"""




										pass




				@unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds'''				)
				def 					a__(		self      :			List[str]				)->					Optional[Any]:

										"""simple docstring"""




										pass




				@unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds'''				)
				def 					a__(		self      :			Optional[Any]				)->					Optional[Any]:

										"""simple docstring"""




										pass




				@unittest.skip('''TimmBackbone model cannot be created without specifying a backbone checkpoint'''				)
				def 					a__(		self      :			Any				)->					Optional[int]:

										"""simple docstring"""




										pass




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

										"""simple docstring"""




										pass




				@unittest.skip('''model weights aren\'t tied in TimmBackbone.'''				)
				def 					a__(		self      :			str				)->					str:

										"""simple docstring"""




										pass




				@unittest.skip('''model weights aren\'t tied in TimmBackbone.'''				)
				def 					a__(		self      :			Any				)->					int:

										"""simple docstring"""




										pass




				@unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone'''				)
				def 					a__(		self      :			List[Any]				)->					int:

										"""simple docstring"""




										pass




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

										"""simple docstring"""




										pass




				@unittest.skip('''TimmBackbone doesn\'t have hidden size info in its configuration.'''				)
				def 					a__(		self      :			List[str]				)->					Dict:

										"""simple docstring"""




										pass




				@unittest.skip('''TimmBackbone doesn\'t support output_attentions.'''				)
				def 					a__(		self      :			str				)->					Tuple:

										"""simple docstring"""




										pass




				@unittest.skip('''Safetensors is not supported by timm.'''				)
				def 					a__(		self      :			List[Any]				)->					List[Any]:

										"""simple docstring"""




										pass




				@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''				)
				def 					a__(		self      :			int				)->					Tuple:

										"""simple docstring"""




										pass




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

										"""simple docstring"""




										UpperCAmelCase    ,    UpperCAmelCase					    =     self.model_tester.prepare_config_and_inputs_for_common()

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

																UpperCAmelCase					    =     ['''pixel_values''']
																self.assertListEqual(arg_names[:1]				,   lowerCAmelCase				)




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

										"""simple docstring"""




										UpperCAmelCase    ,    UpperCAmelCase					    =     self.model_tester.prepare_config_and_inputs_for_common()
										UpperCAmelCase					    =     True
										UpperCAmelCase					    =     self.has_attentions

										# no need to test all models as different heads yield the same functionality
										UpperCAmelCase					    =     self.all_model_classes[0]
										UpperCAmelCase					    =     model_class(lowerCAmelCase				)
										model.to(lowerCAmelCase				)

										UpperCAmelCase					    =     self._prepare_for_class(lowerCAmelCase				,   lowerCAmelCase				)
										UpperCAmelCase					    =     model(**lowerCAmelCase				)
										UpperCAmelCase					    =     outputs[0][-1]

										# Encoder-/Decoder-only models
										UpperCAmelCase					    =     outputs.hidden_states[0]
										hidden_states.retain_grad()

										if self.has_attentions:
																UpperCAmelCase					    =     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 					a__(		self      :			Optional[int]				)->					Optional[Any]:

										"""simple docstring"""




										UpperCAmelCase    ,    UpperCAmelCase					    =     self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																UpperCAmelCase					    =     model_class(lowerCAmelCase				)
																model.to(lowerCAmelCase				)
																model.eval()
																UpperCAmelCase					    =     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
																UpperCAmelCase					    =     copy.deepcopy(lowerCAmelCase				)
																UpperCAmelCase					    =     None
																UpperCAmelCase					    =     model_class(lowerCAmelCase				)
																model.to(lowerCAmelCase				)
																model.eval()
																UpperCAmelCase					    =     model(**lowerCAmelCase				)

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

																# Check backbone can be initialized with fresh weights
																UpperCAmelCase					    =     copy.deepcopy(lowerCAmelCase				)
																UpperCAmelCase					    =     False
																UpperCAmelCase					    =     model_class(lowerCAmelCase				)
																model.to(lowerCAmelCase				)
																model.eval()
																UpperCAmelCase					    =     model(**lowerCAmelCase				)





