
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 __snake_case		(      lowerCAmelCase					,	lowerCAmelCase     ):
    _a		:						int=				"pixel_values"
    _a		:						List[str]=				False
    _a		:						Union[str, Any]=				TimmBackboneConfig
    def __init__(  self		,snake_case		,**snake_case      ):



           '''simple docstring'''







           requires_backends(self		,"""timm"""      )
           super().__init__(snake_case      )
           lowercase							:     int       	=      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."""      )

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

           lowercase							:     List[Any]       	=      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.
           lowercase							:     Optional[int]       	=      self._backbone.return_layers
           lowercase							:     Tuple       	=      {layer["""module"""]: str(snake_case      ) for i, layer in enumerate(self._backbone.feature_info.info      )}
           super()._init_backbone(snake_case      )
    @classmethod
    def       _SCREAMING_SNAKE_CASE       (  cls		,snake_case		,*snake_case		,**snake_case      ):



           '''simple docstring'''







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

           lowercase							:     Tuple       	=      kwargs.pop("""config"""		,TimmBackboneConfig()      )

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

           lowercase							:     Any       	=      kwargs.pop("""num_channels"""		,config.num_channels      )
           lowercase							:     List[Any]       	=      kwargs.pop("""features_only"""		,config.features_only      )
           lowercase							:     Any       	=      kwargs.pop("""use_pretrained_backbone"""		,config.use_pretrained_backbone      )
           lowercase							:     Dict       	=      kwargs.pop("""out_indices"""		,config.out_indices      )
           lowercase							:     Any       	=      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       _SCREAMING_SNAKE_CASE       (  self		,snake_case      ):



           '''simple docstring'''







           pass
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case=None		,snake_case=None		,snake_case=None		,**snake_case      ):



           '''simple docstring'''







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

           lowercase							:     Any       	=      tuple(snake_case      )
           lowercase							:     Dict       	=      tuple(snake_case      ) if hidden_states is not None else None

           if not return_dict:
                  lowercase							:     Union[str, Any]       	=      (feature_maps,)
                  if output_hidden_states:
                         lowercase							:     Optional[Any]       	=      output + (hidden_states,)
                  return output

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




"""simple docstring"""






def      lowerCamelCase       (     _UpperCamelCase		:      float       ,			_UpperCamelCase		:      float       ,			_UpperCamelCase		:      int							)		->       float:

							'''simple docstring'''



							if principal <= 0:
														raise Exception("""Principal borrowed must be > 0"""							)
							if rate_per_annum < 0:
														raise Exception("""Rate of interest must be >= 0"""							)
							if years_to_repay <= 0 or not isinstance(_UpperCamelCase       ,			_UpperCamelCase							):
														raise Exception("""Years to repay must be an integer > 0"""							)

							# Yearly rate is divided by 12 to get monthly rate
							__UpperCAmelCase   :						List[Any]      							=					rate_per_annum / 1_2

							# Years to repay is multiplied by 12 to get number of payments as payment is monthly
							__UpperCAmelCase   :						Optional[int]      							=					years_to_repay * 1_2

							return (
							    principal
							    * rate_per_month
							    * (1 + rate_per_month) ** number_of_payments
							    / ((1 + rate_per_month) ** number_of_payments - 1)
							)


if __name__ == "__main__":
							import doctest

							doctest.testmod()
