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    A_ (       a_			,       a_ ):
    _SCREAMING_SNAKE_CASE    				=			"""pixel_values"""
    _SCREAMING_SNAKE_CASE    				=			False
    _SCREAMING_SNAKE_CASE    				=			TimmBackboneConfig

    def __init__(		self :      Optional[int]     ,							__SCREAMING_SNAKE_CASE :      Optional[int]     ,							**__SCREAMING_SNAKE_CASE :      List[Any]       ):
     requires_backends(self     ,							"timm"       )
     super().__init__(__SCREAMING_SNAKE_CASE       )
     __a       =  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(__SCREAMING_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."       )

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

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

    @classmethod
    def 		_UpperCAmelCase   (		cls :      Dict     ,							__SCREAMING_SNAKE_CASE :      Dict     ,							*__SCREAMING_SNAKE_CASE :      Optional[int]     ,							**__SCREAMING_SNAKE_CASE :      Tuple       ):
     requires_backends(cls     ,							["vision", "timm"]       )
     from ...models.timm_backbone import TimmBackboneConfig

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

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

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

    def 		_UpperCAmelCase   (		self :      Optional[Any]     ,							__SCREAMING_SNAKE_CASE :      List[Any]       ):
     pass

    def 		_UpperCAmelCase   (		self :      Dict     ,							__SCREAMING_SNAKE_CASE :      Union[str, Any]     ,							__SCREAMING_SNAKE_CASE :      Optional[int]=None     ,							__SCREAMING_SNAKE_CASE :      Optional[Any]=None     ,							__SCREAMING_SNAKE_CASE :      Tuple=None     ,							**__SCREAMING_SNAKE_CASE :      List[Any]       ):
     __a       =  return_dict if return_dict is not None else self.config.use_return_dict
     __a       =  (
         output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
     )
     __a       =  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
      __a       =  self._all_layers
      __a       =  self._backbone(__SCREAMING_SNAKE_CASE     ,							**__SCREAMING_SNAKE_CASE       )
      __a       =  self._return_layers
      __a       =  tuple(hidden_states[i] for i in self.out_indices       )
     else:
      __a       =  self._backbone(__SCREAMING_SNAKE_CASE     ,							**__SCREAMING_SNAKE_CASE       )
      __a       =  None

     __a       =  tuple(__SCREAMING_SNAKE_CASE       )
     __a       =  tuple(__SCREAMING_SNAKE_CASE       ) if hidden_states is not None else None

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

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



from ... import PretrainedConfig


SCREAMING_SNAKE_CASE :	Any							     =		{
    """sijunhe/nezha-cn-base""": """https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json""",
}
class    A_ (       a_ ):
  _SCREAMING_SNAKE_CASE    				=			NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP
  _SCREAMING_SNAKE_CASE    				=			"""nezha"""

  def __init__(		self :      Any     ,							__SCREAMING_SNAKE_CASE :      List[str]=2_11_28     ,							__SCREAMING_SNAKE_CASE :      Dict=7_68     ,							__SCREAMING_SNAKE_CASE :      List[Any]=12     ,							__SCREAMING_SNAKE_CASE :      str=12     ,							__SCREAMING_SNAKE_CASE :      Optional[Any]=30_72     ,							__SCREAMING_SNAKE_CASE :      Any="gelu"     ,							__SCREAMING_SNAKE_CASE :      int=0.1     ,							__SCREAMING_SNAKE_CASE :      List[str]=0.1     ,							__SCREAMING_SNAKE_CASE :      int=5_12     ,							__SCREAMING_SNAKE_CASE :      Union[str, Any]=64     ,							__SCREAMING_SNAKE_CASE :      List[Any]=2     ,							__SCREAMING_SNAKE_CASE :      int=0.02     ,							__SCREAMING_SNAKE_CASE :      Optional[Any]=1E-12     ,							__SCREAMING_SNAKE_CASE :      int=0.1     ,							__SCREAMING_SNAKE_CASE :      Any=0     ,							__SCREAMING_SNAKE_CASE :      Any=2     ,							__SCREAMING_SNAKE_CASE :      Optional[Any]=3     ,							__SCREAMING_SNAKE_CASE :      List[Any]=True     ,							**__SCREAMING_SNAKE_CASE :      List[str]     ,							):
   super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE     ,							bos_token_id=__SCREAMING_SNAKE_CASE     ,							eos_token_id=__SCREAMING_SNAKE_CASE     ,							**__SCREAMING_SNAKE_CASE       )

   __a       =  vocab_size
   __a       =  hidden_size
   __a       =  num_hidden_layers
   __a       =  num_attention_heads
   __a       =  hidden_act
   __a       =  intermediate_size
   __a       =  hidden_dropout_prob
   __a       =  attention_probs_dropout_prob
   __a       =  max_position_embeddings
   __a       =  max_relative_position
   __a       =  type_vocab_size
   __a       =  initializer_range
   __a       =  layer_norm_eps
   __a       =  classifier_dropout
   __a       =  use_cache


