



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  __snake_case      (   SCREAMING_SNAKE_CASE):







							'''simple docstring'''






							def __init__(							self  ,  *a_  ,  a_=None  ,  a_=None  ,  **a_		):
											super().__init__(*a_  ,  **a_		)
											a__				      =		eval_examples
											a__				      =		post_process_function


							def _a      (							self  ,  a_ = None  ,  a_=None  ,  a_ = None  ,  a_ = "eval"  ,  **a_  ,  ):
											a__				      =		gen_kwargs.copy()
											a__				      =		(
											    gen_kwargs["""max_length"""] if gen_kwargs.get("""max_length"""		) is not None else self.args.generation_max_length
											)
											a__				      =		(
											    gen_kwargs["""num_beams"""] if gen_kwargs.get("""num_beams"""		) is not None else self.args.generation_num_beams
											)
											a__				      =		gen_kwargs

											a__				      =		self.eval_dataset if eval_dataset is None else eval_dataset
											a__				      =		self.get_eval_dataloader(a_		)
											a__				      =		self.eval_examples if eval_examples is None else eval_examples

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

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

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

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

											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__				      =		self.callback_handler.on_evaluate(self.args  ,  self.state  ,  self.control  ,  a_		)
											return metrics







							def _a      (							self  ,  a_  ,  a_  ,  a_=None  ,  a_ = "test"  ,  **a_		):
											a__				      =		gen_kwargs.copy()

											a__				      =		self.get_test_dataloader(a_		)

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

											a__				      =		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(
											        a_  ,  a_  ,  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__				      =		self.post_process_function(a_  ,  a_  ,  a_  ,  """predict"""		)
											a__				      =		self.compute_metrics(a_		)

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






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase							 =					{
    """configuration_xlm_roberta""": [
        """XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """XLMRobertaConfig""",
        """XLMRobertaOnnxConfig""",
    ],
}

try:
	if not is_sentencepiece_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase							 =					["""XLMRobertaTokenizer"""]

try:
	if not is_tokenizers_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase							 =					["""XLMRobertaTokenizerFast"""]

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase							 =					[
	    """XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """XLMRobertaForCausalLM""",
	    """XLMRobertaForMaskedLM""",
	    """XLMRobertaForMultipleChoice""",
	    """XLMRobertaForQuestionAnswering""",
	    """XLMRobertaForSequenceClassification""",
	    """XLMRobertaForTokenClassification""",
	    """XLMRobertaModel""",
	    """XLMRobertaPreTrainedModel""",
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase							 =					[
	    """TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """TFXLMRobertaForCausalLM""",
	    """TFXLMRobertaForMaskedLM""",
	    """TFXLMRobertaForMultipleChoice""",
	    """TFXLMRobertaForQuestionAnswering""",
	    """TFXLMRobertaForSequenceClassification""",
	    """TFXLMRobertaForTokenClassification""",
	    """TFXLMRobertaModel""",
	    """TFXLMRobertaPreTrainedModel""",
	]

try:
	if not is_flax_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	UpperCAmelCase							 =					[
	    """FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """FlaxXLMRobertaForMaskedLM""",
	    """FlaxXLMRobertaForCausalLM""",
	    """FlaxXLMRobertaForMultipleChoice""",
	    """FlaxXLMRobertaForQuestionAnswering""",
	    """FlaxXLMRobertaForSequenceClassification""",
	    """FlaxXLMRobertaForTokenClassification""",
	    """FlaxXLMRobertaModel""",
	    """FlaxXLMRobertaPreTrainedModel""",
	]

if TYPE_CHECKING:
	from .configuration_xlm_roberta import (
	    XLM_ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    XLMRobertaConfig,
	    XLMRobertaOnnxConfig,
	)

	try:
		if not is_sentencepiece_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_xlm_roberta import XLMRobertaTokenizer

	try:
		if not is_tokenizers_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_xlm_roberta_fast import XLMRobertaTokenizerFast

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_xlm_roberta import (
		    XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
		    XLMRobertaForCausalLM,
		    XLMRobertaForMaskedLM,
		    XLMRobertaForMultipleChoice,
		    XLMRobertaForQuestionAnswering,
		    XLMRobertaForSequenceClassification,
		    XLMRobertaForTokenClassification,
		    XLMRobertaModel,
		    XLMRobertaPreTrainedModel,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_xlm_roberta import (
		    TF_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFXLMRobertaForCausalLM,
		    TFXLMRobertaForMaskedLM,
		    TFXLMRobertaForMultipleChoice,
		    TFXLMRobertaForQuestionAnswering,
		    TFXLMRobertaForSequenceClassification,
		    TFXLMRobertaForTokenClassification,
		    TFXLMRobertaModel,
		    TFXLMRobertaPreTrainedModel,
		)

	try:
		if not is_flax_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_flax_xlm_roberta import (
		    FLAX_XLM_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
		    FlaxXLMRobertaForCausalLM,
		    FlaxXLMRobertaForMaskedLM,
		    FlaxXLMRobertaForMultipleChoice,
		    FlaxXLMRobertaForQuestionAnswering,
		    FlaxXLMRobertaForSequenceClassification,
		    FlaxXLMRobertaForTokenClassification,
		    FlaxXLMRobertaModel,
		    FlaxXLMRobertaPreTrainedModel,
		)

else:
	import sys

	UpperCAmelCase							 =					_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

