

"""simple docstring"""






from typing import Tuple, Union

from ...modeling_outputs import BackboneOutput
from ...modeling_utils import PreTrainedModel
from ...utils import is_timm_available, is_torch_available, requires_backends
from ...utils.backbone_utils import BackboneMixin
from .configuration_timm_backbone import TimmBackboneConfig


if is_timm_available():
	import timm


if is_torch_available():
	from torch import Tensor







class    snake_case__	(			__SCREAMING_SNAKE_CASE       ,	__SCREAMING_SNAKE_CASE  ):
			SCREAMING_SNAKE_CASE__  					=    '''pixel_values'''
			SCREAMING_SNAKE_CASE__  					=    False
			SCREAMING_SNAKE_CASE__  					=    TimmBackboneConfig







			def __init__(      self      :			Dict      ,	lowercase      :			Optional[Any]      ,	**lowercase      :			Dict	):





								'''simple docstring'''



								requires_backends(self      ,	"timm"	)
								super().__init__(UpperCamelCase__	)
								UpperCAmelCase      : Optional[int]   =					config

								if config.backbone is None:
													raise ValueError("backbone is not set in the config. Please set it to a timm model name."	)

								if config.backbone not in timm.list_models():
													raise ValueError(f"""backbone {config.backbone} is not supported by timm."""	)

								if hasattr(UpperCamelCase__      ,	"out_features"	) and config.out_features is not None:
													raise ValueError("out_features is not supported by TimmBackbone. Please use out_indices instead."	)

								UpperCAmelCase      : Tuple   =					getattr(UpperCamelCase__      ,	"use_pretrained_backbone"      ,	UpperCamelCase__	)
								if pretrained is None:
													raise ValueError("use_pretrained_backbone is not set in the config. Please set it to True or False."	)

								# We just take the final layer by default. This matches the default for the transformers models.
								UpperCAmelCase      : List[Any]   =					config.out_indices if getattr(UpperCamelCase__      ,	"out_indices"      ,	UpperCamelCase__	) is not None else (-1,)

								UpperCAmelCase      : str   =					timm.create_model(
								    config.backbone      ,	pretrained=UpperCamelCase__      ,	features_only=config.features_only      ,	in_chans=config.num_channels      ,	out_indices=UpperCamelCase__      ,	**UpperCamelCase__      ,	)
								# These are used to control the output of the model when called. If output_hidden_states is True, then
								# return_layers is modified to include all layers.
								UpperCAmelCase      : Dict   =					self._backbone.return_layers
								UpperCAmelCase      : List[str]   =					{layer['''module''']: str(UpperCamelCase__	) for i, layer in enumerate(self._backbone.feature_info.info	)}
								super()._init_backbone(UpperCamelCase__	)







			@classmethod
			def       __lowerCAmelCase					(      cls      :			Tuple      ,	lowercase      :			Dict      ,	*lowercase      :			Optional[Any]      ,	**lowercase      :			Optional[Any]	):





								'''simple docstring'''



								requires_backends(cls      ,	["vision", "timm"]	)
								from ...models.timm_backbone import TimmBackboneConfig

								UpperCAmelCase      : Any   =					kwargs.pop("config"      ,	TimmBackboneConfig()	)

								UpperCAmelCase      : Union[str, Any]   =					kwargs.pop("use_timm_backbone"      ,	UpperCamelCase__	)
								if not use_timm:
													raise ValueError("use_timm_backbone must be True for timm backbones"	)

								UpperCAmelCase      : int   =					kwargs.pop("num_channels"      ,	config.num_channels	)
								UpperCAmelCase      : Tuple   =					kwargs.pop("features_only"      ,	config.features_only	)
								UpperCAmelCase      : Any   =					kwargs.pop("use_pretrained_backbone"      ,	config.use_pretrained_backbone	)
								UpperCAmelCase      : List[str]   =					kwargs.pop("out_indices"      ,	config.out_indices	)
								UpperCAmelCase      : List[str]   =					TimmBackboneConfig(
								    backbone=UpperCamelCase__      ,	num_channels=UpperCamelCase__      ,	features_only=UpperCamelCase__      ,	use_pretrained_backbone=UpperCamelCase__      ,	out_indices=UpperCamelCase__      ,	)
								return super()._from_config(UpperCamelCase__      ,	**UpperCamelCase__	)







			def       __lowerCAmelCase					(      self      :			str      ,	lowercase      :			str	):





								'''simple docstring'''



								pass







			def       __lowerCAmelCase					(      self      :			Tuple      ,	lowercase      :			List[Any]      ,	lowercase      :			Optional[int]=None      ,	lowercase      :			Optional[Any]=None      ,	lowercase      :			str=None      ,	**lowercase      :			Tuple	):





								'''simple docstring'''



								UpperCAmelCase      : Dict   =					return_dict if return_dict is not None else self.config.use_return_dict
								UpperCAmelCase      : str   =					(
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)
								UpperCAmelCase      : Optional[int]   =					output_attentions if output_attentions is not None else self.config.output_attentions

								if output_attentions:
													raise ValueError("Cannot output attentions for timm backbones at the moment"	)

								if output_hidden_states:
													# We modify the return layers to include all the stages of the backbone
													UpperCAmelCase      : List[Any]   =					self._all_layers
													UpperCAmelCase      : Optional[Any]   =					self._backbone(UpperCamelCase__      ,	**UpperCamelCase__	)
													UpperCAmelCase      : Union[str, Any]   =					self._return_layers
													UpperCAmelCase      : Tuple   =					tuple(hidden_states[i] for i in self.out_indices	)
								else:
													UpperCAmelCase      : Dict   =					self._backbone(UpperCamelCase__      ,	**UpperCamelCase__	)
													UpperCAmelCase      : Union[str, Any]   =					None

								UpperCAmelCase      : List[Any]   =					tuple(UpperCamelCase__	)
								UpperCAmelCase      : Any   =					tuple(UpperCamelCase__	) if hidden_states is not None else None

								if not return_dict:
													UpperCAmelCase      : str   =					(feature_maps,)
													if output_hidden_states:
																		UpperCAmelCase      : Any   =					output + (hidden_states,)
													return output

								return BackboneOutput(feature_maps=UpperCamelCase__      ,	hidden_states=UpperCamelCase__      ,	attentions=UpperCamelCase__	)




'''simple docstring'''
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowercase__						       =      logging.get_logger(__name__)

lowercase__						       =      {
    "microsoft/swin-tiny-patch4-window7-224": (
        "https://huggingface.co/microsoft/swin-tiny-patch4-window7-224/resolve/main/config.json"
    ),
    # See all Swin models at https://huggingface.co/models?filter=swin
}






class 	snake_case__       (      __SCREAMING_SNAKE_CASE    ,					__SCREAMING_SNAKE_CASE					):







		"""simple docstring"""

		lowerCamelCase     							=						"""swin"""

		lowerCamelCase     							=						{
		    """num_attention_heads""": """num_heads""",
		    """num_hidden_layers""": """num_layers""",
		}



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






				"""simple docstring"""







				super().__init__(**UpperCamelCase__      )

				snake_case  :       Any     			=						image_size
				snake_case  :       Optional[int]     			=						patch_size
				snake_case  :       List[Any]     			=						num_channels
				snake_case  :       Union[str, Any]     			=						embed_dim
				snake_case  :       str     			=						depths
				snake_case  :       str     			=						len(UpperCamelCase__      )
				snake_case  :       List[Any]     			=						num_heads
				snake_case  :       List[Any]     			=						window_size
				snake_case  :       Optional[int]     			=						mlp_ratio
				snake_case  :       Union[str, Any]     			=						qkv_bias
				snake_case  :       Optional[int]     			=						hidden_dropout_prob
				snake_case  :       Optional[int]     			=						attention_probs_dropout_prob
				snake_case  :       Optional[int]     			=						drop_path_rate
				snake_case  :       List[Any]     			=						hidden_act
				snake_case  :       int     			=						use_absolute_embeddings
				snake_case  :       str     			=						layer_norm_eps
				snake_case  :       Optional[Any]     			=						initializer_range
				snake_case  :       Any     			=						encoder_stride
				# we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
				# this indicates the channel dimension after the last stage of the model
				snake_case  :       str     			=						int(embed_dim * 2 ** (len(UpperCamelCase__      ) - 1)      )
				snake_case  :       int     			=						['''stem'''] + [f'stage{idx}' for idx in range(1  ,	len(UpperCamelCase__      ) + 1      )]
				snake_case			,snake_case  :       List[Any]     			=						get_aligned_output_features_output_indices(
				    out_features=UpperCamelCase__  ,	out_indices=UpperCamelCase__  ,	stage_names=self.stage_names      )







class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""

		lowerCamelCase     							=						version.parse("""1.11"""					)



		@property
		def       lowerCAmelCase				(      self				:			List[Any]      )						->			Mapping[str, Mapping[int, str]]:






				"""simple docstring"""







				return OrderedDict(
				    [
				        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
				    ]      )



		@property
		def       lowerCAmelCase				(      self				:			List[Any]      )						->			float:






				"""simple docstring"""







				return 1e-4


