






'''simple docstring'''
import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union


class     _UpperCAmelCase  (						enum.Enum     ):
    __lowerCamelCase:				Optional[int]						    =      'timm'
    __lowerCamelCase:				Union[str, Any]						    =      'transformers'




def 						__SCREAMING_SNAKE_CASE   (					_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase							):


   """simple docstring"""




   if stage_names is None:
      raise ValueError("Stage_names must be set for transformers backbones"							)

   if out_features is not None:
      if not isinstance(_UpperCamelCase  ,						(list,)							):
         raise ValueError(F"""out_features must be a list {type(_UpperCamelCase							)}"""							)
      if any(feat not in stage_names for feat in out_features							):
         raise ValueError(F"""out_features must be a subset of stage_names: {stage_names} got {out_features}"""							)

   if out_indices is not None:
      if not isinstance(_UpperCamelCase  ,						(list, tuple)							):
         raise ValueError(F"""out_indices must be a list or tuple, got {type(_UpperCamelCase							)}"""							)
      if any(idx >= len(_UpperCamelCase							) for idx in out_indices							):
         raise ValueError("out_indices must be valid indices for stage_names {stage_names}, got {out_indices}"							)

   if out_features is not None and out_indices is not None:
      if len(_UpperCamelCase							) != len(_UpperCamelCase							):
         raise ValueError("out_features and out_indices should have the same length if both are set"							)
      if out_features != [stage_names[idx] for idx in out_indices]:
         raise ValueError("out_features and out_indices should correspond to the same stages if both are set"							)




def 						__SCREAMING_SNAKE_CASE   (					_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase  ,						):


   """simple docstring"""




   if out_indices is None and out_features is None:
      lowercase_		:      Optional[Any]          =       [len(_UpperCamelCase							) - 1]
      lowercase_		:      List[Any]          =       [stage_names[-1]]
   elif out_indices is None and out_features is not None:
      lowercase_		:      str          =       [stage_names.index(_UpperCamelCase							) for layer in out_features]
   elif out_features is None and out_indices is not None:
      lowercase_		:      List[str]          =       [stage_names[idx] for idx in out_indices]
   return out_features, out_indices




def 						__SCREAMING_SNAKE_CASE   (					_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase  ,						):


   """simple docstring"""




   verify_out_features_out_indices(out_features=_UpperCamelCase  ,						out_indices=_UpperCamelCase  ,						stage_names=_UpperCamelCase							)
   lowercase_      ,						lowercase_		:      int          =       _align_output_features_output_indices(
       out_features=_UpperCamelCase  ,						out_indices=_UpperCamelCase  ,						stage_names=_UpperCamelCase							)
   # Verify that the aligned out_features and out_indices are valid
   verify_out_features_out_indices(out_features=_UpperCamelCase  ,						out_indices=_UpperCamelCase  ,						stage_names=_UpperCamelCase							)
   return output_features, output_indices


class     _UpperCAmelCase  :
    __lowerCamelCase:				Any						    =      None




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






       '''simple docstring'''






       if getattr(self					,       "_backbone"					,       SCREAMING_SNAKE_CASE_     ) is None:
          raise ValueError("self._backbone must be set before calling _init_timm_backbone"     )

       # These will diagree with the defaults for the transformers models e.g. for resnet50
       # the transformer model has out_features = ['stem', 'stage1', 'stage2', 'stage3', 'stage4']
       # the timm model has out_features = ['act', 'layer1', 'layer2', 'layer3', 'layer4']
       lowercase_		:      Any          =       [stage["module"] for stage in self._backbone.feature_info.info]
       lowercase_		:      Tuple          =       [stage["num_chs"] for stage in self._backbone.feature_info.info]
       lowercase_		:      Tuple          =       self._backbone.feature_info.out_indices
       lowercase_		:      Any          =       self._backbone.feature_info.module_name()

       # We verify the out indices and out features are valid
       verify_out_features_out_indices(
           out_features=SCREAMING_SNAKE_CASE_					,       out_indices=SCREAMING_SNAKE_CASE_					,       stage_names=self.stage_names     )
       lowercase_      ,						lowercase_		:      Optional[int]          =       out_features, out_indices




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






       '''simple docstring'''






       lowercase_		:      Any          =       getattr(SCREAMING_SNAKE_CASE_					,       "stage_names"     )
       lowercase_		:      Tuple          =       getattr(SCREAMING_SNAKE_CASE_					,       "out_features"					,       SCREAMING_SNAKE_CASE_     )
       lowercase_		:      int          =       getattr(SCREAMING_SNAKE_CASE_					,       "out_indices"					,       SCREAMING_SNAKE_CASE_     )

       lowercase_		:      int          =       stage_names
       lowercase_      ,						lowercase_		:      Any          =       get_aligned_output_features_output_indices(
           out_features=SCREAMING_SNAKE_CASE_					,       out_indices=SCREAMING_SNAKE_CASE_					,       stage_names=SCREAMING_SNAKE_CASE_     )
       # Number of channels for each stage. This is set in the transformer backbone model init
       lowercase_		:      Optional[Any]          =       None




    def 			lowerCAmelCase__			(     self					:    Any					,       a					:    Dict     ):






       '''simple docstring'''






       lowercase_		:      List[Any]          =       config

       lowercase_		:      Any          =       getattr(SCREAMING_SNAKE_CASE_					,       "use_timm_backbone"					,       SCREAMING_SNAKE_CASE_     )
       lowercase_		:      Any          =       BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

       if self.backbone_type == BackboneType.TIMM:
          self._init_timm_backbone(SCREAMING_SNAKE_CASE_     )
       elif self.backbone_type == BackboneType.TRANSFORMERS:
          self._init_transformers_backbone(SCREAMING_SNAKE_CASE_     )
       else:
          raise ValueError(f"""backbone_type {self.backbone_type} not supported."""     )




    @property
    def 			lowerCAmelCase__			(     self					:    Tuple     ):






       '''simple docstring'''






       return self._out_features




    @out_features.setter
    def 			lowerCAmelCase__			(     self					:    Optional[int]					,       a					:    List[str]     ):






       '''simple docstring'''






       lowercase_      ,						lowercase_		:      List[Any]          =       get_aligned_output_features_output_indices(
           out_features=SCREAMING_SNAKE_CASE_					,       out_indices=SCREAMING_SNAKE_CASE_					,       stage_names=self.stage_names     )




    @property
    def 			lowerCAmelCase__			(     self					:    int     ):






       '''simple docstring'''






       return self._out_indices




    @out_indices.setter
    def 			lowerCAmelCase__			(     self					:    Tuple					,       a					:    Optional[Any]     ):






       '''simple docstring'''






       lowercase_      ,						lowercase_		:      Optional[Any]          =       get_aligned_output_features_output_indices(
           out_features=SCREAMING_SNAKE_CASE_					,       out_indices=SCREAMING_SNAKE_CASE_					,       stage_names=self.stage_names     )




    @property
    def 			lowerCAmelCase__			(     self					:    List[Any]     ):






       '''simple docstring'''






       return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names     )}




    @property
    def 			lowerCAmelCase__			(     self					:    List[Any]     ):






       '''simple docstring'''






       return [self.out_feature_channels[name] for name in self.out_features]




    def 			lowerCAmelCase__			(     self					:    List[str]					,       *a					:    int					,       **a					:    Optional[int]     ):






       '''simple docstring'''






       lowercase_		:      int          =       dict(inspect.signature(self.forward     ).parameters     )
       lowercase_		:      Optional[int]          =       {k: v for k, v in kwargs.items() if k in signature}
       return self(*SCREAMING_SNAKE_CASE_					,       **SCREAMING_SNAKE_CASE_     )




    def 			lowerCAmelCase__			(     self					:    Tuple					,       a					:    str					,       a					:    Optional[int] = None					,       a					:    Optional[Any] = None					,       a					:    int = None					,       ):






       '''simple docstring'''






       raise NotImplementedError("This method should be implemented by the derived class."     )





    def 			lowerCAmelCase__			(     self					:    str     ):






       '''simple docstring'''






       lowercase_		:      Optional[int]          =       super().to_dict()
       lowercase_		:      Union[str, Any]          =       output.pop("_out_features"     )
       lowercase_		:      List[str]          =       output.pop("_out_indices"     )
       return output


class     _UpperCAmelCase  :




    @property
    def 			lowerCAmelCase__			(     self					:    Union[str, Any]     ):






       '''simple docstring'''






       return self._out_features




    @out_features.setter
    def 			lowerCAmelCase__			(     self					:    Union[str, Any]					,       a					:    str     ):






       '''simple docstring'''






       lowercase_      ,						lowercase_		:      Optional[Any]          =       get_aligned_output_features_output_indices(
           out_features=SCREAMING_SNAKE_CASE_					,       out_indices=SCREAMING_SNAKE_CASE_					,       stage_names=self.stage_names     )




    @property
    def 			lowerCAmelCase__			(     self					:    Union[str, Any]     ):






       '''simple docstring'''






       return self._out_indices




    @out_indices.setter
    def 			lowerCAmelCase__			(     self					:    str					,       a					:    List[str]     ):






       '''simple docstring'''






       lowercase_      ,						lowercase_		:      Dict          =       get_aligned_output_features_output_indices(
           out_features=SCREAMING_SNAKE_CASE_					,       out_indices=SCREAMING_SNAKE_CASE_					,       stage_names=self.stage_names     )





    def 			lowerCAmelCase__			(     self					:    str     ):






       '''simple docstring'''






       lowercase_		:      List[str]          =       super().to_dict()
       lowercase_		:      Dict          =       output.pop("_out_features"     )
       lowercase_		:      Tuple          =       output.pop("_out_indices"     )
       return output











'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


UpperCamelCase__	=     {
    'configuration_unispeech_sat': ['UNISPEECH_SAT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'UniSpeechSatConfig'],
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCamelCase__	=     [
     'UNISPEECH_SAT_PRETRAINED_MODEL_ARCHIVE_LIST',
     'UniSpeechSatForAudioFrameClassification',
     'UniSpeechSatForCTC',
     'UniSpeechSatForPreTraining',
     'UniSpeechSatForSequenceClassification',
     'UniSpeechSatForXVector',
     'UniSpeechSatModel',
     'UniSpeechSatPreTrainedModel',
 ]

if TYPE_CHECKING:
 from .configuration_unispeech_sat import UNISPEECH_SAT_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechSatConfig

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_unispeech_sat import (
      UNISPEECH_SAT_PRETRAINED_MODEL_ARCHIVE_LIST,
      UniSpeechSatForAudioFrameClassification,
      UniSpeechSatForCTC,
      UniSpeechSatForPreTraining,
      UniSpeechSatForSequenceClassification,
      UniSpeechSatForXVector,
      UniSpeechSatModel,
      UniSpeechSatPreTrainedModel,
  )

else:
 import sys

 UpperCamelCase__	=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




