


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






				'''simple docstring'''



				def __init__(       self			:Union[str, Any]			,			lowerCAmelCase__			:List[str]			,			lowerCAmelCase__			:str=None			,			lowerCAmelCase__			:str=None			,			lowerCAmelCase__			:Tuple=None			,			lowerCAmelCase__			:str="resnet50"			,			lowerCAmelCase__			:Optional[int]=3			,			lowerCAmelCase__			:Union[str, Any]=32			,			lowerCAmelCase__			:Optional[Any]=3			,			lowerCAmelCase__			:int=True			,			lowerCAmelCase__			:Optional[Any]=True			,			)		->			List[Any]:
										__SCREAMING_SNAKE_CASE						:     Dict					=		parent
										__SCREAMING_SNAKE_CASE						:     Dict					=		out_indices if out_indices is not None else [4]
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		stage_names
										__SCREAMING_SNAKE_CASE						:     Dict					=		out_features
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		backbone
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		batch_size
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		image_size
										__SCREAMING_SNAKE_CASE						:     str					=		num_channels
										__SCREAMING_SNAKE_CASE						:     Dict					=		use_pretrained_backbone
										__SCREAMING_SNAKE_CASE						:     Tuple					=		is_training


				def 		__magic_name__(       self			:Optional[Any]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     Dict					=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)
										__SCREAMING_SNAKE_CASE						:     List[str]					=		self.get_config()

										return config, pixel_values


				def 		__magic_name__(       self			:Dict	)		->			Dict:
										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 		__magic_name__(       self			:int			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:Optional[int]	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Any					=		TimmBackbone(config=lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()
										with torch.no_grad():
																__SCREAMING_SNAKE_CASE						:     List[str]					=		model(lowerCAmelCase__	)
										self.parent.assertEqual(
										    result.feature_map[-1].shape			,			(self.batch_size, model.channels[-1], 14, 14)			,			)



				def 		__magic_name__(       self			:int	)		->			Tuple:
										__SCREAMING_SNAKE_CASE						:     int					=		self.prepare_config_and_inputs()
										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     List[Any]					=		config_and_inputs
										__SCREAMING_SNAKE_CASE						:     Tuple					=		{'''pixel_values''': pixel_values}
										return config, inputs_dict




@require_torch
@require_timm
class        _lowercase      (			A__						,			A__						,			A__						,			unittest.TestCase       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			List[Any]						=		(TimmBackbone,) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE__				:			List[str]						=		{'''feature-extraction''': TimmBackbone} if is_torch_available() else {}
				SCREAMING_SNAKE_CASE__				:			List[Any]						=		False
				SCREAMING_SNAKE_CASE__				:			Optional[Any]						=		False
				SCREAMING_SNAKE_CASE__				:			Optional[int]						=		False
				SCREAMING_SNAKE_CASE__				:			Optional[Any]						=		False


				def 		__magic_name__(       self			:Any	)		->			List[str]:
										__SCREAMING_SNAKE_CASE						:     List[str]					=		TimmBackboneModelTester(self	)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		ConfigTester(self			,			config_class=lowerCAmelCase__			,			has_text_modality=lowerCAmelCase__	)


				def 		__magic_name__(       self			:List[str]	)		->			Optional[int]:
										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 		__magic_name__(       self			:List[Any]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     Any					=		'''resnet18'''
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		'''microsoft/resnet-18'''

										__SCREAMING_SNAKE_CASE						:     str					=		AutoBackbone.from_pretrained(lowerCAmelCase__			,			use_timm_backbone=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[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						:     Any					=		AutoBackbone.from_pretrained(lowerCAmelCase__			,			use_timm_backbone=lowerCAmelCase__			,			out_indices=[1, 2, 3]	)
										__SCREAMING_SNAKE_CASE						:     Dict					=		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 		__magic_name__(       self			:str	)		->			List[Any]:
										pass


				@unittest.skip('''TimmBackbone doesn\'t have num_hidden_layers attribute'''	)
				def 		__magic_name__(       self			:Tuple	)		->			Any:
										pass


				@unittest.skip('''TimmBackbone initialization is managed on the timm side'''	)
				def 		__magic_name__(       self			:List[str]	)		->			int:
										pass


				@unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds'''	)
				def 		__magic_name__(       self			:List[str]	)		->			Tuple:
										pass


				@unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds'''	)
				def 		__magic_name__(       self			:str	)		->			str:
										pass


				@unittest.skip('''TimmBackbone model cannot be created without specifying a backbone checkpoint'''	)
				def 		__magic_name__(       self			:Dict	)		->			Tuple:
										pass


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


				@unittest.skip('''model weights aren\'t tied in TimmBackbone.'''	)
				def 		__magic_name__(       self			:Dict	)		->			Optional[Any]:
										pass


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


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


				@unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone'''	)
				def 		__magic_name__(       self			:Dict	)		->			str:
										pass


				@unittest.skip('''TimmBackbone doesn\'t have hidden size info in its configuration.'''	)
				def 		__magic_name__(       self			:Dict	)		->			Any:
										pass


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


				@unittest.skip('''Safetensors is not supported by timm.'''	)
				def 		__magic_name__(       self			:int	)		->			str:
										pass


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


				def 		__magic_name__(       self			:List[Any]	)		->			List[str]:
										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     str					=		self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__SCREAMING_SNAKE_CASE						:     Union[str, 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						:     str					=		['''pixel_values''']
																self.assertListEqual(arg_names[:1]			,			lowerCAmelCase__	)


				def 		__magic_name__(       self			:Dict	)		->			List[Any]:
										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Dict					=		self.model_tester.prepare_config_and_inputs_for_common()
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		True
										__SCREAMING_SNAKE_CASE						:     Tuple					=		self.has_attentions

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

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

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

										if self.has_attentions:
																__SCREAMING_SNAKE_CASE						:     List[Any]					=		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 		__magic_name__(       self			:Optional[Any]	)		->			Tuple:
										__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__SCREAMING_SNAKE_CASE						:     Any					=		model_class(lowerCAmelCase__	)
																model.to(lowerCAmelCase__	)
																model.eval()
																__SCREAMING_SNAKE_CASE						:     Any					=		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						:     Dict					=		copy.deepcopy(lowerCAmelCase__	)
																__SCREAMING_SNAKE_CASE						:     Dict					=		None
																__SCREAMING_SNAKE_CASE						:     str					=		model_class(lowerCAmelCase__	)
																model.to(lowerCAmelCase__	)
																model.eval()
																__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		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						:     Dict					=		copy.deepcopy(lowerCAmelCase__	)
																__SCREAMING_SNAKE_CASE						:     Optional[int]					=		False
																__SCREAMING_SNAKE_CASE						:     List[str]					=		model_class(lowerCAmelCase__	)
																model.to(lowerCAmelCase__	)
																model.eval()
																__SCREAMING_SNAKE_CASE						:     List[str]					=		model(**lowerCAmelCase__	)




import importlib.metadata
import operator
import re
import sys
from typing import Optional

from packaging import version


__lowerCAmelCase	: Union[str, Any]  				={
    '<': operator.lt,
    '<=': operator.le,
    '==': operator.eq,
    '!=': operator.ne,
    '>=': operator.ge,
    '>': operator.gt,
}


def        _UpperCamelCase							(      lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__      ):
						if got_ver is None or want_ver is None:
												raise ValueError(
												    F'''Unable to compare versions for {requirement}: need={want_ver} found={got_ver}. This is unusual. Consider'''
												    F''' reinstalling {pkg}.'''      )
						if not ops[op](version.parse(lowercase__      )    ,       version.parse(lowercase__      )      ):
												raise ImportError(
												    F'''{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}'''      )


def        _UpperCamelCase							(      lowercase__    ,       lowercase__ = None      ):
						__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		F'''\n{hint}''' if hint is not None else ''''''

						# non-versioned check
						if re.match(R'''^[\w_\-\d]+$'''    ,       lowercase__      ):
												__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     str					=		requirement, None, None
						else:
												__SCREAMING_SNAKE_CASE						:     List[Any]					=		re.findall(R'''^([^!=<>\s]+)([\s!=<>]{1,2}.+)'''    ,       lowercase__      )
												if not match:
																		raise ValueError(
																		    '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but'''
																		    F''' got {requirement}'''      )
												__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Tuple					=		match[0]
												__SCREAMING_SNAKE_CASE						:     Optional[int]					=		want_full.split(''','''      )  # there could be multiple requirements
												__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		{}
												for w in want_range:
																		__SCREAMING_SNAKE_CASE						:     Any					=		re.findall(R'''^([\s!=<>]{1,2})(.+)'''    ,       lowercase__      )
																		if not match:
																								raise ValueError(
																								    '''requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,'''
																								    F''' but got {requirement}'''      )
																		__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE						:     Tuple					=		match[0]
																		__SCREAMING_SNAKE_CASE						:     List[Any]					=		want_ver
																		if op not in ops:
																								raise ValueError(F'''{requirement}: need one of {list(ops.keys()      )}, but got {op}'''      )

    # special case
						if pkg == "python":
												__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		'''.'''.join([str(lowercase__      ) for x in sys.version_info[:3]]      )
												for op, want_ver in wanted.items():
																		_compare_versions(lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__      )
												return

						# check if any version is installed
						try:
												__SCREAMING_SNAKE_CASE						:     Optional[int]					=		importlib.metadata.version(lowercase__      )
						except importlib.metadata.PackageNotFoundError:
												raise importlib.metadata.PackageNotFoundError(
												    F'''The \'{requirement}\' distribution was not found and is required by this application. {hint}'''      )

						# check that the right version is installed if version number or a range was provided
						if want_ver is not None:
												for op, want_ver in wanted.items():
																		_compare_versions(lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__      )


def        _UpperCamelCase							(      lowercase__      ):
						__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		'''Try: pip install transformers -U or pip install -e \'.[dev]\' if you\'re working with git main'''
						return require_version(lowercase__    ,       lowercase__      )
