






'''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 							_UpperCAmelCase    (		snake_case					,       snake_case      ):
    __lowerCamelCase: Any									=     'pixel_values'
    __lowerCamelCase: str									=     False
    __lowerCamelCase: Optional[Any]									=     TimmBackboneConfig




    def __init__(      self	:							List[Any]	,  a	:							Tuple	,  **a	:							List[Any]      ):






       '''simple docstring'''






       requires_backends(self	,  "timm"      )
       super().__init__(a      )
       lowercase_						:     List[str]							     =  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."      )

       lowercase_						:     Optional[int]							     =  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.
       lowercase_						:     Optional[Any]							     =  config.out_indices if getattr(a	,  "out_indices"	,  a      ) is not None else (-1,)

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




    @classmethod
    def 					lowerCAmelCase__					(      cls	:							List[str]	,  a	:							int	,  *a	:							int	,  **a	:							Optional[int]      ):






       '''simple docstring'''






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

       lowercase_						:     List[str]							     =  kwargs.pop("config"	,  TimmBackboneConfig()      )

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

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




    def 					lowerCAmelCase__					(      self	:							Optional[Any]	,  a	:							List[Any]      ):






       '''simple docstring'''






       pass





    def 					lowerCAmelCase__					(      self	:							Optional[int]	,  a	:							Union[str, Any]	,  a	:							List[str]=None	,  a	:							str=None	,  a	:							str=None	,  **a	:							Dict      ):






       '''simple docstring'''






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

       lowercase_						:     str							     =  tuple(a      )
       lowercase_						:     Tuple							     =  tuple(a      ) if hidden_states is not None else None

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

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












'''simple docstring'''
def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase = 100				):


   """simple docstring"""




   lowercase_						:     Dict							     =  n * (n + 1) * (2 * n + 1) / 6
   lowercase_						:     int							     =  (n * (n + 1) / 2) ** 2
   return int(square_of_sum - sum_of_squares				)


if __name__ == "__main__":
 print(f"""{solution() = }""")




