

import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union







class 						__magic_name__      (							enum.Enum							):

   '''simple docstring'''




   lowerCamelCase__			:     int    =       'timm'
   lowerCamelCase__			:     int    =       'transformers'





def       UpperCAmelCase__    (      _A				:			Optional[Iterable[str]]     ,				_A				:			Optional[Iterable[int]]     ,				_A				:			Optional[Iterable[str]]							):





  '''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(_A     ,				(list,)							):
      raise ValueError(F"""out_features must be a list {type(_A							)}"""							)
    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(_A     ,				(list, tuple)							):
      raise ValueError(F"""out_indices must be a list or tuple, got {type(_A							)}"""							)
    if any(idx >= len(_A							) 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(_A							) != len(_A							):
      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       UpperCAmelCase__    (      _A				:			Optional[List[str]]     ,				_A				:			Optional[Union[List[int], Tuple[int]]]     ,				_A				:			List[str]     ,				):





  '''simple docstring'''







  if out_indices is None and out_features is None:
    a__  =[len(_A							) - 1]
    a__  =[stage_names[-1]]
  elif out_indices is None and out_features is not None:
    a__  =[stage_names.index(_A							) for layer in out_features]
  elif out_features is None and out_indices is not None:
    a__  =[stage_names[idx] for idx in out_indices]
  return out_features, out_indices




def       UpperCAmelCase__    (      _A				:			Optional[List[str]]     ,				_A				:			Optional[Union[List[int], Tuple[int]]]     ,				_A				:			List[str]     ,				):





  '''simple docstring'''







  verify_out_features_out_indices(out_features=_A     ,				out_indices=_A     ,				stage_names=_A							)
  a__,				a__  =_align_output_features_output_indices(
      out_features=_A     ,				out_indices=_A     ,				stage_names=_A							)
  # Verify that the aligned out_features and out_indices are valid
  verify_out_features_out_indices(out_features=_A     ,				out_indices=_A     ,				stage_names=_A							)
  return output_features, output_indices







class 						__magic_name__      :

   '''simple docstring'''




   lowerCamelCase__			:     Optional[BackboneType]    =       None



   def 			_UpperCAmelCase			(    self,   lowercase_	)							->   None:






     """simple docstring"""






     if getattr(self,   '''_backbone''',   lowercase_	) 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']
     a__  =[stage['''module'''] for stage in self._backbone.feature_info.info]
     a__  =[stage['''num_chs'''] for stage in self._backbone.feature_info.info]
     a__  =self._backbone.feature_info.out_indices
     a__  =self._backbone.feature_info.module_name()

     # We verify the out indices and out features are valid
     verify_out_features_out_indices(
         out_features=lowercase_,   out_indices=lowercase_,   stage_names=self.stage_names	)
     a__,				a__  =out_features, out_indices



   def 			_UpperCAmelCase			(    self,   lowercase_	)							->   None:






     """simple docstring"""






     a__  =getattr(lowercase_,   '''stage_names'''	)
     a__  =getattr(lowercase_,   '''out_features''',   lowercase_	)
     a__  =getattr(lowercase_,   '''out_indices''',   lowercase_	)

     a__  =stage_names
     a__,				a__  =get_aligned_output_features_output_indices(
         out_features=lowercase_,   out_indices=lowercase_,   stage_names=lowercase_	)
     # Number of channels for each stage. This is set in the transformer backbone model init
     a__  =None



   def 			_UpperCAmelCase			(    self,   lowercase_	)							->   None:






     """simple docstring"""






     a__  =config

     a__  =getattr(lowercase_,   '''use_timm_backbone''',   lowercase_	)
     a__  =BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

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



   @property
   def 			_UpperCAmelCase			(    self	)							->   int:






     """simple docstring"""






     return self._out_features



   @out_features.setter
   def 			_UpperCAmelCase			(    self,   lowercase_	)							->   Tuple:






     """simple docstring"""






     a__,				a__  =get_aligned_output_features_output_indices(
         out_features=lowercase_,   out_indices=lowercase_,   stage_names=self.stage_names	)



   @property
   def 			_UpperCAmelCase			(    self	)							->   Union[str, Any]:






     """simple docstring"""






     return self._out_indices



   @out_indices.setter
   def 			_UpperCAmelCase			(    self,   lowercase_	)							->   Optional[int]:






     """simple docstring"""






     a__,				a__  =get_aligned_output_features_output_indices(
         out_features=lowercase_,   out_indices=lowercase_,   stage_names=self.stage_names	)



   @property
   def 			_UpperCAmelCase			(    self	)							->   List[str]:






     """simple docstring"""






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



   @property
   def 			_UpperCAmelCase			(    self	)							->   Any:






     """simple docstring"""






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



   def 			_UpperCAmelCase			(    self,   *lowercase_,   **lowercase_	)							->   str:






     """simple docstring"""






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



   def 			_UpperCAmelCase			(    self,   lowercase_,   lowercase_ = None,   lowercase_ = None,   lowercase_ = None,   )							->   List[Any]:






     """simple docstring"""






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



   def 			_UpperCAmelCase			(    self	)							->   Optional[int]:






     """simple docstring"""






     a__  =super().to_dict()
     a__  =output.pop('''_out_features'''	)
     a__  =output.pop('''_out_indices'''	)
     return output







class 						__magic_name__      :

   '''simple docstring'''




   @property
   def 			_UpperCAmelCase			(    self	)							->   int:






     """simple docstring"""






     return self._out_features



   @out_features.setter
   def 			_UpperCAmelCase			(    self,   lowercase_	)							->   str:






     """simple docstring"""






     a__,				a__  =get_aligned_output_features_output_indices(
         out_features=lowercase_,   out_indices=lowercase_,   stage_names=self.stage_names	)



   @property
   def 			_UpperCAmelCase			(    self	)							->   str:






     """simple docstring"""






     return self._out_indices



   @out_indices.setter
   def 			_UpperCAmelCase			(    self,   lowercase_	)							->   Optional[int]:






     """simple docstring"""






     a__,				a__  =get_aligned_output_features_output_indices(
         out_features=lowercase_,   out_indices=lowercase_,   stage_names=self.stage_names	)



   def 			_UpperCAmelCase			(    self	)							->   str:






     """simple docstring"""






     a__  =super().to_dict()
     a__  =output.pop('''_out_features'''	)
     a__  =output.pop('''_out_indices'''	)
     return output



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


lowerCamelCase									= {
    '''configuration_flaubert''': ['''FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FlaubertConfig''', '''FlaubertOnnxConfig'''],
    '''tokenization_flaubert''': ['''FlaubertTokenizer'''],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       lowerCamelCase									= [
           '''FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
           '''FlaubertForMultipleChoice''',
           '''FlaubertForQuestionAnswering''',
           '''FlaubertForQuestionAnsweringSimple''',
           '''FlaubertForSequenceClassification''',
           '''FlaubertForTokenClassification''',
           '''FlaubertModel''',
           '''FlaubertWithLMHeadModel''',
           '''FlaubertPreTrainedModel''',
       ]

try:
       if not is_tf_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       lowerCamelCase									= [
           '''TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
           '''TFFlaubertForMultipleChoice''',
           '''TFFlaubertForQuestionAnsweringSimple''',
           '''TFFlaubertForSequenceClassification''',
           '''TFFlaubertForTokenClassification''',
           '''TFFlaubertModel''',
           '''TFFlaubertPreTrainedModel''',
           '''TFFlaubertWithLMHeadModel''',
       ]


if TYPE_CHECKING:
       from .configuration_flaubert import FLAUBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, FlaubertConfig, FlaubertOnnxConfig
       from .tokenization_flaubert import FlaubertTokenizer

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_flaubert import (
                  FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                  FlaubertForMultipleChoice,
                  FlaubertForQuestionAnswering,
                  FlaubertForQuestionAnsweringSimple,
                  FlaubertForSequenceClassification,
                  FlaubertForTokenClassification,
                  FlaubertModel,
                  FlaubertPreTrainedModel,
                  FlaubertWithLMHeadModel,
              )

       try:
              if not is_tf_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_tf_flaubert import (
                  TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                  TFFlaubertForMultipleChoice,
                  TFFlaubertForQuestionAnsweringSimple,
                  TFFlaubertForSequenceClassification,
                  TFFlaubertForTokenClassification,
                  TFFlaubertModel,
                  TFFlaubertPreTrainedModel,
                  TFFlaubertWithLMHeadModel,
              )

else:
       import sys

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