



import importlib
import json
import os
from collections import OrderedDict
from typing import Dict, Optional, Union

# Build the list of all feature extractors
from ...configuration_utils import PretrainedConfig
from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code
from ...feature_extraction_utils import FeatureExtractionMixin
from ...utils import CONFIG_NAME, FEATURE_EXTRACTOR_NAME, get_file_from_repo, logging
from .auto_factory import _LazyAutoMapping
from .configuration_auto import (
    CONFIG_MAPPING_NAMES,
    AutoConfig,
    model_type_to_module_name,
    replace_list_option_in_docstrings,
)


__snake_case						:Tuple      		=     logging.get_logger(__name__)

__snake_case						:Tuple      		=     OrderedDict(
    [
        ('''audio-spectrogram-transformer''', '''ASTFeatureExtractor'''),
        ('''beit''', '''BeitFeatureExtractor'''),
        ('''chinese_clip''', '''ChineseCLIPFeatureExtractor'''),
        ('''clap''', '''ClapFeatureExtractor'''),
        ('''clip''', '''CLIPFeatureExtractor'''),
        ('''clipseg''', '''ViTFeatureExtractor'''),
        ('''conditional_detr''', '''ConditionalDetrFeatureExtractor'''),
        ('''convnext''', '''ConvNextFeatureExtractor'''),
        ('''cvt''', '''ConvNextFeatureExtractor'''),
        ('''data2vec-audio''', '''Wav2Vec2FeatureExtractor'''),
        ('''data2vec-vision''', '''BeitFeatureExtractor'''),
        ('''deformable_detr''', '''DeformableDetrFeatureExtractor'''),
        ('''deit''', '''DeiTFeatureExtractor'''),
        ('''detr''', '''DetrFeatureExtractor'''),
        ('''dinat''', '''ViTFeatureExtractor'''),
        ('''donut-swin''', '''DonutFeatureExtractor'''),
        ('''dpt''', '''DPTFeatureExtractor'''),
        ('''encodec''', '''EncodecFeatureExtractor'''),
        ('''flava''', '''FlavaFeatureExtractor'''),
        ('''glpn''', '''GLPNFeatureExtractor'''),
        ('''groupvit''', '''CLIPFeatureExtractor'''),
        ('''hubert''', '''Wav2Vec2FeatureExtractor'''),
        ('''imagegpt''', '''ImageGPTFeatureExtractor'''),
        ('''layoutlmv2''', '''LayoutLMv2FeatureExtractor'''),
        ('''layoutlmv3''', '''LayoutLMv3FeatureExtractor'''),
        ('''levit''', '''LevitFeatureExtractor'''),
        ('''maskformer''', '''MaskFormerFeatureExtractor'''),
        ('''mctct''', '''MCTCTFeatureExtractor'''),
        ('''mobilenet_v1''', '''MobileNetV1FeatureExtractor'''),
        ('''mobilenet_v2''', '''MobileNetV2FeatureExtractor'''),
        ('''mobilevit''', '''MobileViTFeatureExtractor'''),
        ('''nat''', '''ViTFeatureExtractor'''),
        ('''owlvit''', '''OwlViTFeatureExtractor'''),
        ('''perceiver''', '''PerceiverFeatureExtractor'''),
        ('''poolformer''', '''PoolFormerFeatureExtractor'''),
        ('''regnet''', '''ConvNextFeatureExtractor'''),
        ('''resnet''', '''ConvNextFeatureExtractor'''),
        ('''segformer''', '''SegformerFeatureExtractor'''),
        ('''sew''', '''Wav2Vec2FeatureExtractor'''),
        ('''sew-d''', '''Wav2Vec2FeatureExtractor'''),
        ('''speech_to_text''', '''Speech2TextFeatureExtractor'''),
        ('''speecht5''', '''SpeechT5FeatureExtractor'''),
        ('''swiftformer''', '''ViTFeatureExtractor'''),
        ('''swin''', '''ViTFeatureExtractor'''),
        ('''swinv2''', '''ViTFeatureExtractor'''),
        ('''table-transformer''', '''DetrFeatureExtractor'''),
        ('''timesformer''', '''VideoMAEFeatureExtractor'''),
        ('''tvlt''', '''TvltFeatureExtractor'''),
        ('''unispeech''', '''Wav2Vec2FeatureExtractor'''),
        ('''unispeech-sat''', '''Wav2Vec2FeatureExtractor'''),
        ('''van''', '''ConvNextFeatureExtractor'''),
        ('''videomae''', '''VideoMAEFeatureExtractor'''),
        ('''vilt''', '''ViltFeatureExtractor'''),
        ('''vit''', '''ViTFeatureExtractor'''),
        ('''vit_mae''', '''ViTFeatureExtractor'''),
        ('''vit_msn''', '''ViTFeatureExtractor'''),
        ('''wav2vec2''', '''Wav2Vec2FeatureExtractor'''),
        ('''wav2vec2-conformer''', '''Wav2Vec2FeatureExtractor'''),
        ('''wavlm''', '''Wav2Vec2FeatureExtractor'''),
        ('''whisper''', '''WhisperFeatureExtractor'''),
        ('''xclip''', '''CLIPFeatureExtractor'''),
        ('''yolos''', '''YolosFeatureExtractor'''),
    ]
)

__snake_case						:Dict      		=     _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES)





def      __snake_case	(			_UpperCAmelCase				):
    for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items():
        if class_name in extractors:
            __a						       =				model_type_to_module_name(_UpperCAmelCase				)

            __a						       =				importlib.import_module(f'.{module_name}'   , '''transformers.models'''				)
            try:
                return getattr(_UpperCAmelCase   , _UpperCAmelCase				)
            except AttributeError:
                continue

    for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items():
        if getattr(_UpperCAmelCase   , '''__name__'''   , _UpperCAmelCase				) == class_name:
            return extractor

    # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main
    # init and we return the proper dummy to get an appropriate error message.
    __a						       =				importlib.import_module('''transformers'''				)
    if hasattr(_UpperCAmelCase   , _UpperCAmelCase				):
        return getattr(_UpperCAmelCase   , _UpperCAmelCase				)

    return None





def      __snake_case	(			_UpperCAmelCase   , _UpperCAmelCase = None   , _UpperCAmelCase = False   , _UpperCAmelCase = False   , _UpperCAmelCase = None   , _UpperCAmelCase = None   , _UpperCAmelCase = None   , _UpperCAmelCase = False   , **_UpperCAmelCase   , ):
    __a						       =				get_file_from_repo(
        _UpperCAmelCase   , _UpperCAmelCase   , cache_dir=_UpperCAmelCase   , force_download=_UpperCAmelCase   , resume_download=_UpperCAmelCase   , proxies=_UpperCAmelCase   , use_auth_token=_UpperCAmelCase   , revision=_UpperCAmelCase   , local_files_only=_UpperCAmelCase   , )
    if resolved_config_file is None:
        logger.info(
            '''Could not locate the feature extractor configuration file, will try to use the model config instead.'''				)
        return {}

    with open(_UpperCAmelCase   , encoding='''utf-8'''				) as reader:
        return json.load(_UpperCAmelCase				)

class     _A       :



       def __init__(		self							:     int):


           '''simple docstring'''

           raise EnvironmentError(
               '''AutoFeatureExtractor is designed to be instantiated '''
               '''using the `AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path)` method.''')



       @classmethod
       @replace_list_option_in_docstrings(__SCREAMING_SNAKE_CASE)
       def 		_lowerCamelCase			(		cls							:     Any     ,		__SCREAMING_SNAKE_CASE							:     str     ,		**__SCREAMING_SNAKE_CASE							:     Optional[Any]):


           '''simple docstring'''

           __a						       =				kwargs.pop('''config'''     ,		__SCREAMING_SNAKE_CASE)
           __a						       =				kwargs.pop('''trust_remote_code'''     ,		__SCREAMING_SNAKE_CASE)
           __a						       =				True

           __a       ,   __a						       =				FeatureExtractionMixin.get_feature_extractor_dict(__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE)
           __a						       =				config_dict.get('''feature_extractor_type'''     ,		__SCREAMING_SNAKE_CASE)
           __a						       =				None
           if "AutoFeatureExtractor" in config_dict.get('''auto_map'''     ,		{}):
               __a						       =				config_dict['''auto_map''']['''AutoFeatureExtractor''']

           # If we don't find the feature extractor class in the feature extractor config, let's try the model config.
           if feature_extractor_class is None and feature_extractor_auto_map is None:
               if not isinstance(__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE):
                   __a						       =				AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE)
               # It could be in `config.feature_extractor_type``
               __a						       =				getattr(__SCREAMING_SNAKE_CASE     ,		'''feature_extractor_type'''     ,		__SCREAMING_SNAKE_CASE)
               if hasattr(__SCREAMING_SNAKE_CASE     ,		'''auto_map''') and "AutoFeatureExtractor" in config.auto_map:
                   __a						       =				config.auto_map['''AutoFeatureExtractor''']

           if feature_extractor_class is not None:
               __a						       =				feature_extractor_class_from_name(__SCREAMING_SNAKE_CASE)

           __a						       =				feature_extractor_auto_map is not None
           __a						       =				feature_extractor_class is not None or type(__SCREAMING_SNAKE_CASE) in FEATURE_EXTRACTOR_MAPPING
           __a						       =				resolve_trust_remote_code(
               __SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE)

           if has_remote_code and trust_remote_code:
               __a						       =				get_class_from_dynamic_module(
                   __SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE)
               __a						       =				kwargs.pop('''code_revision'''     ,		__SCREAMING_SNAKE_CASE)
               if os.path.isdir(__SCREAMING_SNAKE_CASE):
                   feature_extractor_class.register_for_auto_class()
               return feature_extractor_class.from_dict(__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE)
           elif feature_extractor_class is not None:
               return feature_extractor_class.from_dict(__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE)
           # Last try: we use the FEATURE_EXTRACTOR_MAPPING.
           elif type(__SCREAMING_SNAKE_CASE) in FEATURE_EXTRACTOR_MAPPING:
               __a						       =				FEATURE_EXTRACTOR_MAPPING[type(__SCREAMING_SNAKE_CASE)]
               return feature_extractor_class.from_dict(__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE)

           raise ValueError(
               F'Unrecognized feature extractor in {pretrained_model_name_or_path}. Should have a '
               F'`feature_extractor_type` key in its {FEATURE_EXTRACTOR_NAME} of {CONFIG_NAME}, or one of the following '
               F'`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in FEATURE_EXTRACTOR_MAPPING_NAMES.keys())}')





       @staticmethod
       def 		_lowerCamelCase			(		__SCREAMING_SNAKE_CASE							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]):


           '''simple docstring'''

           FEATURE_EXTRACTOR_MAPPING.register(__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE)










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


__snake_case						:Union[str, Any]      		=     logging.get_logger(__name__)

__snake_case						:Any      		=     {
    '''google/switch-base-8''': '''https://huggingface.co/google/switch-base-8/blob/main/config.json''',
}

class     _A       (	__UpperCAmelCase    ):
       UpperCamelCase__  :    Optional[int]											=						'''switch_transformers'''
       UpperCamelCase__  :    Optional[Any]											=						['''past_key_values''']
       UpperCamelCase__  :    Optional[Any]											=						{'''hidden_size''': '''d_model''', '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers'''}



       def __init__(		self							:     Optional[Any]     ,		__SCREAMING_SNAKE_CASE							:     str=32_128     ,		__SCREAMING_SNAKE_CASE							:     int=768     ,		__SCREAMING_SNAKE_CASE							:     Any=64     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=2_048     ,		__SCREAMING_SNAKE_CASE							:     List[str]=64     ,		__SCREAMING_SNAKE_CASE							:     int=12     ,		__SCREAMING_SNAKE_CASE							:     Any=3     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=12     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=3     ,		__SCREAMING_SNAKE_CASE							:     Any=12     ,		__SCREAMING_SNAKE_CASE							:     Tuple=8     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=False     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]=0.01     ,		__SCREAMING_SNAKE_CASE							:     Dict="float32"     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=False     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=32     ,		__SCREAMING_SNAKE_CASE							:     int=128     ,		__SCREAMING_SNAKE_CASE							:     Any=0.1     ,		__SCREAMING_SNAKE_CASE							:     int=1E-6     ,		__SCREAMING_SNAKE_CASE							:     Dict=0.0_01     ,		__SCREAMING_SNAKE_CASE							:     List[str]=0.0_01     ,		__SCREAMING_SNAKE_CASE							:     List[Any]=1.0     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]="relu"     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=True     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=False     ,		__SCREAMING_SNAKE_CASE							:     str=True     ,		__SCREAMING_SNAKE_CASE							:     int=0     ,		__SCREAMING_SNAKE_CASE							:     List[Any]=1     ,		**__SCREAMING_SNAKE_CASE							:     Dict     ,		):


           '''simple docstring'''

           __a						       =				vocab_size
           __a						       =				d_model
           __a						       =				d_kv
           __a						       =				d_ff

           __a						       =				num_sparse_encoder_layers

           __a						       =				num_layers
           __a						       =				(
               num_decoder_layers if num_decoder_layers is not None else self.num_layers
           )  # default = symmetry
           __a						       =				num_sparse_decoder_layers

           # This tells us, each how many encoder layer we'll have to set a sparse layer.
           if self.num_sparse_encoder_layers > 0:
               __a						       =				self.num_layers // self.num_sparse_encoder_layers
           else:
               __a						       =				self.num_layers  # HACK: this will create 0 sparse layers

           # This tells us, each how many encoder layer we'll have to set a sparse layer.
           if self.num_sparse_decoder_layers > 0:
               __a						       =				self.num_decoder_layers // self.num_sparse_decoder_layers
           else:
               __a						       =				self.num_decoder_layers  # HACK: this will create 0 sparse layers

           __a						       =				num_heads
           __a						       =				num_experts
           __a						       =				expert_capacity
           __a						       =				router_bias
           __a						       =				router_jitter_noise
           if router_dtype not in ["float32", "float16", "bfloat16"]:
               raise ValueError(F'`router_dtype` must be one of \'float32\', \'float16\' or \'bfloat16\', got {router_dtype}')
           __a						       =				router_dtype

           __a						       =				router_ignore_padding_tokens
           __a						       =				relative_attention_num_buckets
           __a						       =				relative_attention_max_distance

           __a						       =				dropout_rate
           __a						       =				layer_norm_epsilon
           __a						       =				initializer_factor
           __a						       =				feed_forward_proj
           __a						       =				use_cache
           __a						       =				add_router_probs

           __a						       =				router_z_loss_coef
           __a						       =				router_aux_loss_coef

           __a						       =				self.feed_forward_proj.split('''-''')
           __a						       =				act_info[-1]
           __a						       =				act_info[0] == '''gated'''

           if len(__SCREAMING_SNAKE_CASE) > 1 and act_info[0] != "gated" or len(__SCREAMING_SNAKE_CASE) > 2:
               raise ValueError(
                   F'`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.'
                   '''Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. '''
                   '''\'gated-gelu\' or \'relu\'''')

           # for backwards compatibility
           if feed_forward_proj == "gated-gelu":
               __a						       =				'''gelu_new'''

           super().__init__(
               pad_token_id=__SCREAMING_SNAKE_CASE     ,		eos_token_id=__SCREAMING_SNAKE_CASE     ,		is_encoder_decoder=__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE     ,		)





