





'''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    lowerCAmelCase_		(		_lowercase						,_lowercase		):
 __lowerCamelCase						:							str         =					'''pixel_values'''
 __lowerCamelCase						:							Any         =					False
 __lowerCamelCase						:							List[Any]         =					TimmBackboneConfig




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

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

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




 @classmethod
 def 							_snake_case   (	cls			,     _lowerCAmelCase			,     *_lowerCAmelCase			,     **_lowerCAmelCase		)    ->							Optional[Any]:
     requires_backends(cls			,     ["vision", "timm"]		)
     from ...models.timm_backbone import TimmBackboneConfig

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

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

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




 def 							_snake_case   (	self			,     _lowerCAmelCase		)    ->							Any:
     pass





 def 							_snake_case   (	self			,     _lowerCAmelCase			,     _lowerCAmelCase=None			,     _lowerCAmelCase=None			,     _lowerCAmelCase=None			,     **_lowerCAmelCase		)    ->							Union[BackboneOutput, Tuple[Tensor, ...]]:
     _lowerCAmelCase				=      return_dict if return_dict is not None else self.config.use_return_dict
     _lowerCAmelCase				=      (
         output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
     )
     _lowerCAmelCase				=      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
         _lowerCAmelCase				=      self._all_layers
         _lowerCAmelCase				=      self._backbone(A_			,     **A_		)
         _lowerCAmelCase				=      self._return_layers
         _lowerCAmelCase				=      tuple(hidden_states[i] for i in self.out_indices		)
     else:
         _lowerCAmelCase				=      self._backbone(A_			,     **A_		)
         _lowerCAmelCase				=      None

     _lowerCAmelCase				=      tuple(A_		)
     _lowerCAmelCase				=      tuple(A_		) if hidden_states is not None else None

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

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









'''simple docstring'''


from ...configuration_utils import PretrainedConfig

class    lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase						:							List[str]         =					"bert-generation"




 def __init__(	self			,     _lowerCAmelCase=50358			,     _lowerCAmelCase=1024			,     _lowerCAmelCase=24			,     _lowerCAmelCase=16			,     _lowerCAmelCase=4096			,     _lowerCAmelCase="gelu"			,     _lowerCAmelCase=0.1			,     _lowerCAmelCase=0.1			,     _lowerCAmelCase=512			,     _lowerCAmelCase=0.02			,     _lowerCAmelCase=1E-12			,     _lowerCAmelCase=0			,     _lowerCAmelCase=2			,     _lowerCAmelCase=1			,     _lowerCAmelCase="absolute"			,     _lowerCAmelCase=True			,     **_lowerCAmelCase			,     )    ->							Tuple:
     super().__init__(pad_token_id=_lowerCAmelCase			,     bos_token_id=_lowerCAmelCase			,     eos_token_id=_lowerCAmelCase			,     **_lowerCAmelCase		)

     _lowerCAmelCase				=      vocab_size
     _lowerCAmelCase				=      hidden_size
     _lowerCAmelCase				=      num_hidden_layers
     _lowerCAmelCase				=      num_attention_heads
     _lowerCAmelCase				=      hidden_act
     _lowerCAmelCase				=      intermediate_size
     _lowerCAmelCase				=      hidden_dropout_prob
     _lowerCAmelCase				=      attention_probs_dropout_prob
     _lowerCAmelCase				=      max_position_embeddings
     _lowerCAmelCase				=      initializer_range
     _lowerCAmelCase				=      layer_norm_eps
     _lowerCAmelCase				=      position_embedding_type
     _lowerCAmelCase				=      use_cache


