






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     lowerCamelCase_					(   lowerCamelCase			):







				def __init__(    self							,       *__lowerCAmelCase							,       __lowerCAmelCase=None							,       __lowerCAmelCase=None							,       **__lowerCAmelCase  ):
								"""simple docstring"""
								super().__init__(*__lowerCAmelCase							,       **__lowerCAmelCase  )
								__magic_name__		:Dict   =   eval_examples
								__magic_name__		:Tuple   =   post_process_function







				def       A						(    self							,       __lowerCAmelCase=None							,       __lowerCAmelCase=None							,       __lowerCAmelCase=None							,       __lowerCAmelCase = "eval"  ):
								"""simple docstring"""
								__magic_name__		:Union[str, Any]   =   self.eval_dataset if eval_dataset is None else eval_dataset
								__magic_name__		:str   =   self.get_eval_dataloader(__lowerCAmelCase  )
								__magic_name__		:Any   =   self.eval_examples if eval_examples is None else eval_examples

								# Temporarily disable metric computation, we will do it in the loop here.
								__magic_name__		:Union[str, Any]   =   self.compute_metrics
								__magic_name__		:List[str]   =   None
								__magic_name__		:int   =   self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
								__magic_name__		:Tuple   =   time.time()
								try:
												__magic_name__		:Union[str, Any]   =   eval_loop(
												    __lowerCAmelCase							,       description='''Evaluation'''							,       prediction_loss_only=True if compute_metrics is None else None							,       ignore_keys=__lowerCAmelCase							,       metric_key_prefix=__lowerCAmelCase							,       )
								finally:
												__magic_name__		:Optional[Any]   =   compute_metrics
								__magic_name__		:Union[str, Any]   =   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
												__magic_name__		:Dict   =   self.post_process_function(__lowerCAmelCase							,       __lowerCAmelCase							,       output.predictions  )
												__magic_name__		:List[Any]   =   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}_'''  ):
																				__magic_name__		:List[str]   =   metrics.pop(__lowerCAmelCase  )
												metrics.update(output.metrics  )
								else:
												__magic_name__		:str   =   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()  )

								__magic_name__		:List[Any]   =   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"""
								__magic_name__		:Tuple   =   self.get_test_dataloader(__lowerCAmelCase  )

								# Temporarily disable metric computation, we will do it in the loop here.
								__magic_name__		:List[str]   =   self.compute_metrics
								__magic_name__		:List[Any]   =   None
								__magic_name__		:Union[str, Any]   =   self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
								__magic_name__		:List[Any]   =   time.time()
								try:
												__magic_name__		:Union[str, Any]   =   eval_loop(
												    __lowerCAmelCase							,       description='''Prediction'''							,       prediction_loss_only=True if compute_metrics is None else None							,       ignore_keys=__lowerCAmelCase							,       metric_key_prefix=__lowerCAmelCase							,       )
								finally:
												__magic_name__		:Union[str, Any]   =   compute_metrics
								__magic_name__		:str   =   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

								__magic_name__		:List[str]   =   self.post_process_function(__lowerCAmelCase							,       __lowerCAmelCase							,       output.predictions							,       '''predict'''  )
								__magic_name__		:Dict   =   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}_'''  ):
																__magic_name__		:str   =   metrics.pop(__lowerCAmelCase  )
								metrics.update(output.metrics  )
								return PredictionOutput(predictions=predictions.predictions							,       label_ids=predictions.label_ids							,       metrics=__lowerCAmelCase  )








"""simple docstring"""
from typing import List

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


SCREAMING_SNAKE_CASE_					     =	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_					     =	{
    '''snap-research/efficientformer-l1-300''': (
        '''https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json'''
    ),
}
class       lowerCAmelCase_		(				A__ ):



							'''simple docstring'''





							_snake_case						      =		'''efficientformer'''




							def __init__( self    ,				snake_case_ = [3, 2, 6, 4]    ,				snake_case_ = [48, 96, 224, 448]    ,				snake_case_ = [True, True, True, True]    ,				snake_case_ = 448    ,				snake_case_ = 32    ,				snake_case_ = 4    ,				snake_case_ = 7    ,				snake_case_ = 5    ,				snake_case_ = 8    ,				snake_case_ = 4    ,				snake_case_ = 0.0    ,				snake_case_ = 16    ,				snake_case_ = 3    ,				snake_case_ = 3    ,				snake_case_ = 3    ,				snake_case_ = 2    ,				snake_case_ = 1    ,				snake_case_ = 0.0    ,				snake_case_ = 1    ,				snake_case_ = True    ,				snake_case_ = True    ,				snake_case_ = 1e-5    ,				snake_case_ = "gelu"    ,				snake_case_ = 0.02    ,				snake_case_ = 1e-1_2    ,				snake_case_ = 224    ,				snake_case_ = 1e-0_5    ,				**snake_case_    ,				)       ->  None:
													super().__init__(**snake_case_				)

													__lowerCAmelCase     =  hidden_act
													__lowerCAmelCase     =  hidden_dropout_prob
													__lowerCAmelCase     =  hidden_sizes
													__lowerCAmelCase     =  num_hidden_layers
													__lowerCAmelCase     =  num_attention_heads
													__lowerCAmelCase     =  initializer_range
													__lowerCAmelCase     =  layer_norm_eps
													__lowerCAmelCase     =  patch_size
													__lowerCAmelCase     =  num_channels
													__lowerCAmelCase     =  depths
													__lowerCAmelCase     =  mlp_expansion_ratio
													__lowerCAmelCase     =  downsamples
													__lowerCAmelCase     =  dim
													__lowerCAmelCase     =  key_dim
													__lowerCAmelCase     =  attention_ratio
													__lowerCAmelCase     =  resolution
													__lowerCAmelCase     =  pool_size
													__lowerCAmelCase     =  downsample_patch_size
													__lowerCAmelCase     =  downsample_stride
													__lowerCAmelCase     =  downsample_pad
													__lowerCAmelCase     =  drop_path_rate
													__lowerCAmelCase     =  num_metaad_blocks
													__lowerCAmelCase     =  distillation
													__lowerCAmelCase     =  use_layer_scale
													__lowerCAmelCase     =  layer_scale_init_value
													__lowerCAmelCase     =  image_size
													__lowerCAmelCase     =  batch_norm_eps
