




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 		UpperCamelCase__	(lowerCAmelCase__    ,				lowerCAmelCase__     ):

    '''simple docstring'''



    lowerCamelCase_      :	Optional[Any]	     =      """pixel_values"""
    lowerCamelCase_      :	Optional[Any]	     =      False
    lowerCamelCase_      :	str	     =      TimmBackboneConfig







    def __init__(    self     ,  UpperCamelCase__     ,  **UpperCamelCase__     )     ->      List[Any]:
           requires_backends(self     ,  "timm"     )
           super().__init__(UpperCamelCase__     )
           lowerCamelCase   :      Any			 =					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."     )

           lowerCamelCase   :      List[Any]			 =					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.
           lowerCamelCase   :      str			 =					config.out_indices if getattr(UpperCamelCase__     ,  "out_indices"     ,  UpperCamelCase__     ) is not None else (-1,)

           lowerCamelCase   :      Optional[Any]			 =					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.
           lowerCamelCase   :      List[Any]			 =					self._backbone.return_layers
           lowerCamelCase   :      Optional[int]			 =					{layer["module"]: str(UpperCamelCase__     ) for i, layer in enumerate(self._backbone.feature_info.info     )}
           super()._init_backbone(UpperCamelCase__     )







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

           lowerCamelCase   :      Dict			 =					kwargs.pop("config"     ,  TimmBackboneConfig()     )

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

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







    def     _lowercase     (    self     ,  UpperCamelCase__     )     ->      Union[str, Any]:
           pass







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

           lowerCamelCase   :      Optional[int]			 =					tuple(UpperCamelCase__     )
           lowerCamelCase   :      List[str]			 =					tuple(UpperCamelCase__     ) if hidden_states is not None else None

           if not return_dict:
                  lowerCamelCase   :      Dict			 =					(feature_maps,)
                  if output_hidden_states:
                         lowerCamelCase   :      List[Any]			 =					output + (hidden_states,)
                  return output

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












"""simple docstring"""






from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase__     						=						logging.get_logger(__name__)

lowercase__     						=						{"""ctrl""": """https://huggingface.co/ctrl/resolve/main/config.json"""}






class lowerCAmelCase__	(     lowercase						):






			'''simple docstring'''



			lowerCamelCase__      =			"""ctrl"""
			lowerCamelCase__      =			["""past_key_values"""]
			lowerCamelCase__      =			{
			    """max_position_embeddings""": """n_positions""",
			    """hidden_size""": """n_embd""",
			    """num_attention_heads""": """n_head""",
			    """num_hidden_layers""": """n_layer""",
			}
			def __init__(  self  ,      lowercase=246534  ,      lowercase=256  ,      lowercase=1280  ,      lowercase=8192  ,      lowercase=48  ,      lowercase=16  ,      lowercase=0.1  ,      lowercase=0.1  ,      lowercase=1E-6  ,      lowercase=0.02  ,      lowercase=True  ,      **lowercase  ,      ):
							_lowerCamelCase     :			Any    =    vocab_size
							_lowerCamelCase     :			Dict    =    n_positions
							_lowerCamelCase     :			Optional[int]    =    n_embd
							_lowerCamelCase     :			str    =    n_layer
							_lowerCamelCase     :			Union[str, Any]    =    n_head
							_lowerCamelCase     :			Any    =    dff
							_lowerCamelCase     :			int    =    resid_pdrop
							_lowerCamelCase     :			Dict    =    embd_pdrop
							_lowerCamelCase     :			Union[str, Any]    =    layer_norm_epsilon
							_lowerCamelCase     :			Tuple    =    initializer_range

							_lowerCamelCase     :			str    =    use_cache

							super().__init__(**lowercase  )