


'''simple docstring'''



import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, 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 				__UpperCamelCase							(						UpperCamelCase_   ):




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

						'''simple docstring'''





						super().__init__(*__SCREAMING_SNAKE_CASE		,							**__SCREAMING_SNAKE_CASE				)
						__a     :			List[str]          =    eval_examples
						__a     :			Tuple          =    post_process_function




	def  __UpperCAmelCase				(  self		,							__a = None		,							__a=None		,							__a = None		,							__a = "eval"		,							**__a		,							):

						'''simple docstring'''





						__a     :			Optional[Any]          =    gen_kwargs.copy()
						__a     :			List[Any]          =    (
						    gen_kwargs['''max_length'''] if gen_kwargs.get('max_length'				) is not None else self.args.generation_max_length
						)
						__a     :			Union[str, Any]          =    (
						    gen_kwargs['''num_beams'''] if gen_kwargs.get('num_beams'				) is not None else self.args.generation_num_beams
						)
						__a     :			Union[str, Any]          =    gen_kwargs

						__a     :			int          =    self.eval_dataset if eval_dataset is None else eval_dataset
						__a     :			List[str]          =    self.get_eval_dataloader(__SCREAMING_SNAKE_CASE				)
						__a     :			Union[str, Any]          =    self.eval_examples if eval_examples is None else eval_examples

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

											# Prefix all keys with metric_key_prefix + '_'
											for key in list(metrics.keys()				):
																if not key.startswith(f"""{metric_key_prefix}_"""				):
																					__a     :			List[Any]          =    metrics.pop(__SCREAMING_SNAKE_CASE				)

											metrics.update(output.metrics				)
						else:
											__a     :			str          =    output.metrics

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

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

						__a     :			Any          =    self.callback_handler.on_evaluate(self.args		,							self.state		,							self.control		,							__SCREAMING_SNAKE_CASE				)
						return metrics





	def  __UpperCAmelCase				(  self		,							__a		,							__a		,							__a=None		,							__a = "test"		,							**__a				):

						'''simple docstring'''





						__a     :			str          =    gen_kwargs.copy()

						__a     :			Dict          =    self.get_test_dataloader(__SCREAMING_SNAKE_CASE				)

						# Temporarily disable metric computation, we will do it in the loop here.
						__a     :			Union[str, Any]          =    self.compute_metrics
						__a     :			List[Any]          =    None
						__a     :			Optional[Any]          =    time.time()
						__a     :			Any          =    self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
						try:
											__a     :			Any          =    eval_loop(
											    __SCREAMING_SNAKE_CASE		,							description='Prediction'		,							prediction_loss_only=True if compute_metrics is None else None		,							ignore_keys=__SCREAMING_SNAKE_CASE		,							metric_key_prefix=__SCREAMING_SNAKE_CASE		,							)
						finally:
											__a     :			Union[str, Any]          =    compute_metrics

						__a     :			Optional[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(
						        __SCREAMING_SNAKE_CASE		,							__SCREAMING_SNAKE_CASE		,							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

						__a     :			Optional[Any]          =    self.post_process_function(__SCREAMING_SNAKE_CASE		,							__SCREAMING_SNAKE_CASE		,							__SCREAMING_SNAKE_CASE		,							'predict'				)
						__a     :			Optional[Any]          =    self.compute_metrics(__SCREAMING_SNAKE_CASE				)

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





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase  =logging.get_logger(__name__)

lowerCamelCase  ={
    "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/config.json",
    "distilbert-base-uncased-distilled-squad": (
        "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/config.json",
    "distilbert-base-cased-distilled-squad": (
        "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json",
    "distilbert-base-multilingual-cased": (
        "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json"
    ),
    "distilbert-base-uncased-finetuned-sst-2-english": (
        "https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json"
    ),
}




class 					_lowerCamelCase			(   UpperCamelCase_			):





     """simple docstring"""







     SCREAMING_SNAKE_CASE_		 =	'''distilbert'''
     SCREAMING_SNAKE_CASE_		 =	{
         '''hidden_size''': '''dim''',
         '''num_attention_heads''': '''n_heads''',
         '''num_hidden_layers''': '''n_layers''',
     }






     def __init__(				self    ,    __SCREAMING_SNAKE_CASE=3_0_5_2_2    ,    __SCREAMING_SNAKE_CASE=5_1_2    ,    __SCREAMING_SNAKE_CASE=False    ,    __SCREAMING_SNAKE_CASE=6    ,    __SCREAMING_SNAKE_CASE=1_2    ,    __SCREAMING_SNAKE_CASE=7_6_8    ,    __SCREAMING_SNAKE_CASE=4 * 7_6_8    ,    __SCREAMING_SNAKE_CASE=0.1    ,    __SCREAMING_SNAKE_CASE=0.1    ,    __SCREAMING_SNAKE_CASE="gelu"    ,    __SCREAMING_SNAKE_CASE=0.02    ,    __SCREAMING_SNAKE_CASE=0.1    ,    __SCREAMING_SNAKE_CASE=0.2    ,    __SCREAMING_SNAKE_CASE=0    ,    **__SCREAMING_SNAKE_CASE    ,    )	-> Optional[Any]:




           """simple docstring"""







           UpperCamelCase__     : List[Any]											=					vocab_size
           UpperCamelCase__     : Dict											=					max_position_embeddings
           UpperCamelCase__     : Any											=					sinusoidal_pos_embds
           UpperCamelCase__     : Dict											=					n_layers
           UpperCamelCase__     : List[Any]											=					n_heads
           UpperCamelCase__     : Dict											=					dim
           UpperCamelCase__     : Dict											=					hidden_dim
           UpperCamelCase__     : Optional[int]											=					dropout
           UpperCamelCase__     : Optional[Any]											=					attention_dropout
           UpperCamelCase__     : Tuple											=					activation
           UpperCamelCase__     : Optional[int]											=					initializer_range
           UpperCamelCase__     : Optional[int]											=					qa_dropout
           UpperCamelCase__     : str											=					seq_classif_dropout
           super().__init__(**__SCREAMING_SNAKE_CASE    ,    pad_token_id=__SCREAMING_SNAKE_CASE	)




class 					_lowerCamelCase			(   UpperCamelCase_			):





     """simple docstring"""







     @property
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Mapping[str, Mapping[int, str]]:




           """simple docstring"""







           if self.task == "multiple-choice":
                 UpperCamelCase__     : Union[str, Any]											=					{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
           else:
                 UpperCamelCase__     : int											=					{0: '''batch''', 1: '''sequence'''}
           return OrderedDict(
               [
                   ('''input_ids''', dynamic_axis),
                   ('''attention_mask''', dynamic_axis),
               ]	)


