'''simple docstring'''




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,
)


__lowercase        =    logging.get_logger(__name__)

__lowercase        =    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'''),
    ]
)

__lowercase        =    _LazyAutoMapping(CONFIG_MAPPING_NAMES, FEATURE_EXTRACTOR_MAPPING_NAMES)






def 							snake_case__				(     _A:   str					)       ->      List[Any]:

		'''simple docstring'''


		for module_name, extractors in FEATURE_EXTRACTOR_MAPPING_NAMES.items():
				if class_name in extractors:
						lowerCAmelCase     =    model_type_to_module_name(_A					)

						lowerCAmelCase     =    importlib.import_module(f".{module_name}" ,  """transformers.models"""					)
						try:
								return getattr(_A ,  _A					)
						except AttributeError:
								continue

		for _, extractor in FEATURE_EXTRACTOR_MAPPING._extra_content.items():
				if getattr(_A ,  """__name__""" ,  _A					) == 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.
		lowerCAmelCase     =    importlib.import_module("""transformers"""					)
		if hasattr(_A ,  _A					):
				return getattr(_A ,  _A					)

		return None






def 							snake_case__				(     _A:   Union[str, os.PathLike] ,  _A:   Optional[Union[str, os.PathLike]] = None ,  _A:   bool = False ,  _A:   bool = False ,  _A:   Optional[Dict[str, str]] = None ,  _A:   Optional[Union[bool, str]] = None ,  _A:   Optional[str] = None ,  _A:   bool = False ,  **_A:   Optional[int] ,  )       ->      Tuple:

		'''simple docstring'''


		lowerCAmelCase     =    get_file_from_repo(
		    _A ,  _A ,  cache_dir=_A ,  force_download=_A ,  resume_download=_A ,  proxies=_A ,  use_auth_token=_A ,  revision=_A ,  local_files_only=_A ,  )
		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(_A ,  encoding="""utf-8"""					) as reader:
				return json.load(_A					)






class 							a__:


			'''simple docstring'''






			def __init__(  self):




					"""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(__lowerCAmelCase)
			def 				a_			(  cls			,       __lowerCAmelCase			,       **__lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase     =    kwargs.pop("""config"""			,       __lowerCAmelCase)
					lowerCAmelCase     =    kwargs.pop("""trust_remote_code"""			,       __lowerCAmelCase)
					lowerCAmelCase     =    True

					lowerCAmelCase   ,	lowerCAmelCase     =    FeatureExtractionMixin.get_feature_extractor_dict(__lowerCAmelCase			,       **__lowerCAmelCase)
					lowerCAmelCase     =    config_dict.get("""feature_extractor_type"""			,       __lowerCAmelCase)
					lowerCAmelCase     =    None
					if "AutoFeatureExtractor" in config_dict.get("""auto_map"""			,       {}):
							lowerCAmelCase     =    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(__lowerCAmelCase			,       __lowerCAmelCase):
									lowerCAmelCase     =    AutoConfig.from_pretrained(__lowerCAmelCase			,       **__lowerCAmelCase)
							# It could be in `config.feature_extractor_type``
							lowerCAmelCase     =    getattr(__lowerCAmelCase			,       """feature_extractor_type"""			,       __lowerCAmelCase)
							if hasattr(__lowerCAmelCase			,       """auto_map""") and "AutoFeatureExtractor" in config.auto_map:
									lowerCAmelCase     =    config.auto_map["""AutoFeatureExtractor"""]

					if feature_extractor_class is not None:
							lowerCAmelCase     =    feature_extractor_class_from_name(__lowerCAmelCase)

					lowerCAmelCase     =    feature_extractor_auto_map is not None
					lowerCAmelCase     =    feature_extractor_class is not None or type(__lowerCAmelCase) in FEATURE_EXTRACTOR_MAPPING
					lowerCAmelCase     =    resolve_trust_remote_code(
					    __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase)

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

					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 				a_			(  __lowerCAmelCase			,       __lowerCAmelCase):




					"""simple docstring"""


					FEATURE_EXTRACTOR_MAPPING.register(__lowerCAmelCase			,       __lowerCAmelCase)





'''simple docstring'''




import math
import time

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
						import torch_xla.core.xla_model as xm
						import torch_xla.debug.metrics as met






class 							a__(   lowerCAmelCase__      ):


			'''simple docstring'''






			def __init__(  self			,       *__lowerCAmelCase			,       __lowerCAmelCase=None			,       __lowerCAmelCase=None			,       **__lowerCAmelCase):




					"""simple docstring"""


					super().__init__(*__lowerCAmelCase			,       **__lowerCAmelCase)
					lowerCAmelCase     =    eval_examples
					lowerCAmelCase     =    post_process_function



			def 				a_			(  self			,       __lowerCAmelCase=None			,       __lowerCAmelCase=None			,       __lowerCAmelCase=None			,       __lowerCAmelCase = "eval"):




					"""simple docstring"""


					lowerCAmelCase     =    self.eval_dataset if eval_dataset is None else eval_dataset
					lowerCAmelCase     =    self.get_eval_dataloader(__lowerCAmelCase)
					lowerCAmelCase     =    self.eval_examples if eval_examples is None else eval_examples

					# Temporarily disable metric computation, we will do it in the loop here.
					lowerCAmelCase     =    self.compute_metrics
					lowerCAmelCase     =    None
					lowerCAmelCase     =    self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
					lowerCAmelCase     =    time.time()
					try:
							lowerCAmelCase     =    eval_loop(
							    __lowerCAmelCase			,       description="""Evaluation"""			,       prediction_loss_only=True if compute_metrics is None else None			,       ignore_keys=__lowerCAmelCase			,       metric_key_prefix=__lowerCAmelCase			,       )
					finally:
							lowerCAmelCase     =    compute_metrics
					lowerCAmelCase     =    self.args.eval_batch_size * self.args.world_size
					if f"{metric_key_prefix}_jit_compilation_time" in output.metrics:
							start_time += output.metrics[f"{metric_key_prefix}_jit_compilation_time"]
					output.metrics.update(
					    speed_metrics(
					        __lowerCAmelCase			,       __lowerCAmelCase			,       num_samples=output.num_samples			,       num_steps=math.ceil(output.num_samples / total_batch_size)			,       ))
					if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
							# Only the main node write the results by default
							lowerCAmelCase     =    self.post_process_function(__lowerCAmelCase			,       __lowerCAmelCase			,       output.predictions)
							lowerCAmelCase     =    self.compute_metrics(__lowerCAmelCase)

							# Prefix all keys with metric_key_prefix + '_'
							for key in list(metrics.keys()):
									if not key.startswith(f"{metric_key_prefix}_"):
											lowerCAmelCase     =    metrics.pop(__lowerCAmelCase)
							metrics.update(output.metrics)
					else:
							lowerCAmelCase     =    output.metrics

					if self.args.should_log:
							# Only the main node log the results by default
							self.log(__lowerCAmelCase)

					if self.args.tpu_metrics_debug or self.args.debug:
							# tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
							xm.master_print(met.metrics_report())

					lowerCAmelCase     =    self.callback_handler.on_evaluate(self.args			,       self.state			,       self.control			,       __lowerCAmelCase)
					return metrics







			def 				a_			(  self			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase=None			,       __lowerCAmelCase = "test"):




					"""simple docstring"""


					lowerCAmelCase     =    self.get_test_dataloader(__lowerCAmelCase)

					# Temporarily disable metric computation, we will do it in the loop here.
					lowerCAmelCase     =    self.compute_metrics
					lowerCAmelCase     =    None
					lowerCAmelCase     =    self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
					lowerCAmelCase     =    time.time()
					try:
							lowerCAmelCase     =    eval_loop(
							    __lowerCAmelCase			,       description="""Prediction"""			,       prediction_loss_only=True if compute_metrics is None else None			,       ignore_keys=__lowerCAmelCase			,       metric_key_prefix=__lowerCAmelCase			,       )
					finally:
							lowerCAmelCase     =    compute_metrics
					lowerCAmelCase     =    self.args.eval_batch_size * self.args.world_size
					if f"{metric_key_prefix}_jit_compilation_time" in output.metrics:
							start_time += output.metrics[f"{metric_key_prefix}_jit_compilation_time"]
					output.metrics.update(
					    speed_metrics(
					        __lowerCAmelCase			,       __lowerCAmelCase			,       num_samples=output.num_samples			,       num_steps=math.ceil(output.num_samples / total_batch_size)			,       ))

					if self.post_process_function is None or self.compute_metrics is None:
							return output

					lowerCAmelCase     =    self.post_process_function(__lowerCAmelCase			,       __lowerCAmelCase			,       output.predictions			,       """predict""")
					lowerCAmelCase     =    self.compute_metrics(__lowerCAmelCase)

					# Prefix all keys with metric_key_prefix + '_'
					for key in list(metrics.keys()):
							if not key.startswith(f"{metric_key_prefix}_"):
									lowerCAmelCase     =    metrics.pop(__lowerCAmelCase)
					metrics.update(output.metrics)
					return PredictionOutput(predictions=predictions.predictions			,       label_ids=predictions.label_ids			,       metrics=__lowerCAmelCase)




