

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 			lowercase    (	lowercase_      ):




				def __init__( self				,       *snake_case				,       snake_case=None				,       snake_case=None				,       **snake_case	):
											super().__init__(*snake_case				,       **snake_case	)
											snake_case_          =   eval_examples
											snake_case_          =   post_process_function




				def    a							( self				,       snake_case = None				,       snake_case=None				,       snake_case = None				,       snake_case = "eval"				,       **snake_case				,       ):
											snake_case_          =   gen_kwargs.copy()
											snake_case_          =   (
											    gen_kwargs['max_length'] if gen_kwargs.get('max_length'	) is not None else self.args.generation_max_length
											)
											snake_case_          =   (
											    gen_kwargs['num_beams'] if gen_kwargs.get('num_beams'	) is not None else self.args.generation_num_beams
											)
											snake_case_          =   gen_kwargs

											snake_case_          =   self.eval_dataset if eval_dataset is None else eval_dataset
											snake_case_          =   self.get_eval_dataloader(snake_case	)
											snake_case_          =   self.eval_examples if eval_examples is None else eval_examples

											# Temporarily disable metric computation, we will do it in the loop here.
											snake_case_          =   self.compute_metrics
											snake_case_          =   None
											snake_case_          =   time.time()
											snake_case_          =   self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
											try:
																		snake_case_          =   eval_loop(
																		    snake_case				,       description='Evaluation'				,       prediction_loss_only=True if compute_metrics is None else None				,       ignore_keys=snake_case				,       metric_key_prefix=snake_case				,       )
											finally:
																		snake_case_          =   compute_metrics
											snake_case_          =   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(
											        snake_case				,       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
																		snake_case_          =   self.post_process_function(snake_case				,       snake_case				,       snake_case	)
																		snake_case_          =   self.compute_metrics(snake_case	)

																		# Prefix all keys with metric_key_prefix + '_'
																		for key in list(metrics.keys()	):
																									if not key.startswith(F'''{metric_key_prefix}_'''	):
																																snake_case_          =   metrics.pop(snake_case	)

																		metrics.update(output.metrics	)
											else:
																		snake_case_          =   output.metrics

											if self.args.should_log:
																		# Only the main node log the results by default
																		self.log(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()	)

											snake_case_          =   self.callback_handler.on_evaluate(self.args				,       self.state				,       self.control				,       snake_case	)
											return metrics




				def    a							( self				,       snake_case				,       snake_case				,       snake_case=None				,       snake_case = "test"				,       **snake_case	):
											snake_case_          =   gen_kwargs.copy()

											snake_case_          =   self.get_test_dataloader(snake_case	)

											# Temporarily disable metric computation, we will do it in the loop here.
											snake_case_          =   self.compute_metrics
											snake_case_          =   None
											snake_case_          =   time.time()
											snake_case_          =   self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
											try:
																		snake_case_          =   eval_loop(
																		    snake_case				,       description='Prediction'				,       prediction_loss_only=True if compute_metrics is None else None				,       ignore_keys=snake_case				,       metric_key_prefix=snake_case				,       )
											finally:
																		snake_case_          =   compute_metrics

											snake_case_          =   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(
											        snake_case				,       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

											snake_case_          =   self.post_process_function(snake_case				,       snake_case				,       snake_case				,       'predict'	)
											snake_case_          =   self.compute_metrics(snake_case	)

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







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_UpperCAmelCase		:       Tuple						    =	{
    """configuration_xlm_roberta_xl""": [
        """XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """XLMRobertaXLConfig""",
        """XLMRobertaXLOnnxConfig""",
    ],
}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_UpperCAmelCase		:       int						    =	[
					    """XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST""",
					    """XLMRobertaXLForCausalLM""",
					    """XLMRobertaXLForMaskedLM""",
					    """XLMRobertaXLForMultipleChoice""",
					    """XLMRobertaXLForQuestionAnswering""",
					    """XLMRobertaXLForSequenceClassification""",
					    """XLMRobertaXLForTokenClassification""",
					    """XLMRobertaXLModel""",
					    """XLMRobertaXLPreTrainedModel""",
					]

if TYPE_CHECKING:
					from .configuration_xlm_roberta_xl import (
					    XLM_ROBERTA_XL_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    XLMRobertaXLConfig,
					    XLMRobertaXLOnnxConfig,
					)

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_xlm_roberta_xl import (
										    XLM_ROBERTA_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
										    XLMRobertaXLForCausalLM,
										    XLMRobertaXLForMaskedLM,
										    XLMRobertaXLForMultipleChoice,
										    XLMRobertaXLForQuestionAnswering,
										    XLMRobertaXLForSequenceClassification,
										    XLMRobertaXLForTokenClassification,
										    XLMRobertaXLModel,
										    XLMRobertaXLPreTrainedModel,
										)

else:
					import sys

					_UpperCAmelCase		:       Optional[Any]						    =	_LazyModule(__name__, globals()["""__file__"""], _import_structure)




