


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        lowerCamelCase__	(							UpperCAmelCase					,	UpperCAmelCase      ):






			"""simple docstring"""






			_UpperCamelCase		:					List[Any]    =		'pixel_values'
			_UpperCamelCase		:					List[Any]    =		False
			_UpperCamelCase		:					Tuple    =		TimmBackboneConfig




			def __init__(							self      ,						snake_case      ,						**snake_case     ):

						'''simple docstring'''


						requires_backends(self      ,						"timm"     )
						super().__init__(snake_case     )
						UpperCamelCase__					=       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(snake_case      ,						"out_features"     ) and config.out_features is not None:
									raise ValueError("out_features is not supported by TimmBackbone. Please use out_indices instead."     )

						UpperCamelCase__					=       getattr(snake_case      ,						"use_pretrained_backbone"      ,						snake_case     )
						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__					=       config.out_indices if getattr(snake_case      ,						"out_indices"      ,						snake_case     ) is not None else (-1,)

						UpperCamelCase__					=       timm.create_model(
						    config.backbone      ,						pretrained=snake_case      ,						features_only=config.features_only      ,						in_chans=config.num_channels      ,						out_indices=snake_case      ,						**snake_case      ,						)
						# 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__					=       self._backbone.return_layers
						UpperCamelCase__					=       {layer["module"]: str(snake_case     ) for i, layer in enumerate(self._backbone.feature_info.info     )}
						super()._init_backbone(snake_case     )




			@classmethod
			def 				snake_case__				(							cls      ,						snake_case      ,						*snake_case      ,						**snake_case     ):

						'''simple docstring'''


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

						UpperCamelCase__					=       kwargs.pop("config"      ,						TimmBackboneConfig()     )

						UpperCamelCase__					=       kwargs.pop("use_timm_backbone"      ,						snake_case     )
						if not use_timm:
									raise ValueError("use_timm_backbone must be True for timm backbones"     )

						UpperCamelCase__					=       kwargs.pop("num_channels"      ,						config.num_channels     )
						UpperCamelCase__					=       kwargs.pop("features_only"      ,						config.features_only     )
						UpperCamelCase__					=       kwargs.pop("use_pretrained_backbone"      ,						config.use_pretrained_backbone     )
						UpperCamelCase__					=       kwargs.pop("out_indices"      ,						config.out_indices     )
						UpperCamelCase__					=       TimmBackboneConfig(
						    backbone=snake_case      ,						num_channels=snake_case      ,						features_only=snake_case      ,						use_pretrained_backbone=snake_case      ,						out_indices=snake_case      ,						)
						return super()._from_config(snake_case      ,						**snake_case     )




			def 				snake_case__				(							self      ,						snake_case     ):

						'''simple docstring'''


						pass







			def 				snake_case__				(							self      ,						snake_case      ,						snake_case=None      ,						snake_case=None      ,						snake_case=None      ,						**snake_case     ):

						'''simple docstring'''


						UpperCamelCase__					=       return_dict if return_dict is not None else self.config.use_return_dict
						UpperCamelCase__					=       (
						    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
						)
						UpperCamelCase__					=       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__					=       self._all_layers
									UpperCamelCase__					=       self._backbone(snake_case      ,						**snake_case     )
									UpperCamelCase__					=       self._return_layers
									UpperCamelCase__					=       tuple(hidden_states[i] for i in self.out_indices     )
						else:
									UpperCamelCase__					=       self._backbone(snake_case      ,						**snake_case     )
									UpperCamelCase__					=       None

						UpperCamelCase__					=       tuple(snake_case     )
						UpperCamelCase__					=       tuple(snake_case     ) if hidden_states is not None else None

						if not return_dict:
									UpperCamelCase__					=       (feature_maps,)
									if output_hidden_states:
												UpperCamelCase__					=       output + (hidden_states,)
									return output

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






from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__UpperCamelCase    		=       logging.get_logger(__name__)

__UpperCamelCase    		=       {
    'kssteven/ibert-roberta-base': 'https://huggingface.co/kssteven/ibert-roberta-base/resolve/main/config.json',
    'kssteven/ibert-roberta-large': 'https://huggingface.co/kssteven/ibert-roberta-large/resolve/main/config.json',
    'kssteven/ibert-roberta-large-mnli': (
        'https://huggingface.co/kssteven/ibert-roberta-large-mnli/resolve/main/config.json'
    ),
}







class        lowerCamelCase__	(							UpperCAmelCase      ):






			"""simple docstring"""






			_UpperCamelCase		:					Any    =		'ibert'




			def __init__(							self      ,						snake_case=30522      ,						snake_case=768      ,						snake_case=12      ,						snake_case=12      ,						snake_case=3072      ,						snake_case="gelu"      ,						snake_case=0.1      ,						snake_case=0.1      ,						snake_case=512      ,						snake_case=2      ,						snake_case=0.02      ,						snake_case=1E-1_2      ,						snake_case=1      ,						snake_case=0      ,						snake_case=2      ,						snake_case="absolute"      ,						snake_case=False      ,						snake_case="none"      ,						**snake_case      ,						):

						'''simple docstring'''


						super().__init__(pad_token_id=snake_case      ,						bos_token_id=snake_case      ,						eos_token_id=snake_case      ,						**snake_case     )

						UpperCamelCase__					=       vocab_size
						UpperCamelCase__					=       hidden_size
						UpperCamelCase__					=       num_hidden_layers
						UpperCamelCase__					=       num_attention_heads
						UpperCamelCase__					=       hidden_act
						UpperCamelCase__					=       intermediate_size
						UpperCamelCase__					=       hidden_dropout_prob
						UpperCamelCase__					=       attention_probs_dropout_prob
						UpperCamelCase__					=       max_position_embeddings
						UpperCamelCase__					=       type_vocab_size
						UpperCamelCase__					=       initializer_range
						UpperCamelCase__					=       layer_norm_eps
						UpperCamelCase__					=       position_embedding_type
						UpperCamelCase__					=       quant_mode
						UpperCamelCase__					=       force_dequant







class        lowerCamelCase__	(							UpperCAmelCase      ):






			"""simple docstring"""






			@property
			def 				snake_case__				(							self     ):

						'''simple docstring'''


						if self.task == "multiple-choice":
									UpperCamelCase__					=       {0: "batch", 1: "choice", 2: "sequence"}
						else:
									UpperCamelCase__					=       {0: "batch", 1: "sequence"}
						return OrderedDict(
						    [
						        ("input_ids", dynamic_axis),
						        ("attention_mask", dynamic_axis),
						    ]     )


