



"""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 			lowercase__			(      A    ):
							'''simple docstring'''







							def __init__(  self	,       *snake_case	,       snake_case=None	,       snake_case=None	,       **snake_case						)			->							Optional[Any]:
									super().__init__(*snake_case	,       **snake_case						)
									_UpperCAmelCase							 =		eval_examples
									_UpperCAmelCase							 =		post_process_function






							def        lowerCamelCase_   (  self	,       snake_case = None	,       snake_case=None	,       snake_case = None	,       snake_case = "eval"	,       **snake_case	,       )			->							Dict[str, float]:
									_UpperCAmelCase							 =		gen_kwargs.copy()
									_UpperCAmelCase							 =		(
									    gen_kwargs['max_length'] if gen_kwargs.get('max_length'						) is not None else self.args.generation_max_length
									)
									_UpperCAmelCase							 =		(
									    gen_kwargs['num_beams'] if gen_kwargs.get('num_beams'						) is not None else self.args.generation_num_beams
									)
									_UpperCAmelCase							 =		gen_kwargs

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

									# Temporarily disable metric computation, we will do it in the loop here.
									_UpperCAmelCase							 =		self.compute_metrics
									_UpperCAmelCase							 =		None
									_UpperCAmelCase							 =		time.time()
									_UpperCAmelCase							 =		self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
									try:
											_UpperCAmelCase							 =		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:
											_UpperCAmelCase							 =		compute_metrics
									_UpperCAmelCase							 =		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
											_UpperCAmelCase							 =		self.post_process_function(snake_case	,       snake_case	,       snake_case						)
											_UpperCAmelCase							 =		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}_'						):
															_UpperCAmelCase							 =		metrics.pop(snake_case						)

											metrics.update(output.metrics						)
									else:
											_UpperCAmelCase							 =		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()						)

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






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case=None	,       snake_case = "test"	,       **snake_case						)			->							Optional[int]:
									_UpperCAmelCase							 =		gen_kwargs.copy()

									_UpperCAmelCase							 =		self.get_test_dataloader(snake_case						)

									# Temporarily disable metric computation, we will do it in the loop here.
									_UpperCAmelCase							 =		self.compute_metrics
									_UpperCAmelCase							 =		None
									_UpperCAmelCase							 =		time.time()
									_UpperCAmelCase							 =		self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
									try:
											_UpperCAmelCase							 =		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:
											_UpperCAmelCase							 =		compute_metrics

									_UpperCAmelCase							 =		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

									_UpperCAmelCase							 =		self.post_process_function(snake_case	,       snake_case	,       snake_case	,       'predict'						)
									_UpperCAmelCase							 =		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}_'						):
													_UpperCAmelCase							 =		metrics.pop(snake_case						)
									metrics.update(output.metrics						)
									return PredictionOutput(predictions=predictions.predictions	,       label_ids=predictions.label_ids	,       metrics=snake_case						)







"""simple docstring"""





from typing import TYPE_CHECKING

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


lowercase  							=					{
    '''configuration_roberta''': ['''ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RobertaConfig''', '''RobertaOnnxConfig'''],
    '''tokenization_roberta''': ['''RobertaTokenizer'''],
}

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowercase  							=					['''RobertaTokenizerFast''']

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowercase  							=					[
					    '''ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''RobertaForCausalLM''',
					    '''RobertaForMaskedLM''',
					    '''RobertaForMultipleChoice''',
					    '''RobertaForQuestionAnswering''',
					    '''RobertaForSequenceClassification''',
					    '''RobertaForTokenClassification''',
					    '''RobertaModel''',
					    '''RobertaPreTrainedModel''',
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowercase  							=					[
					    '''TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''TFRobertaForCausalLM''',
					    '''TFRobertaForMaskedLM''',
					    '''TFRobertaForMultipleChoice''',
					    '''TFRobertaForQuestionAnswering''',
					    '''TFRobertaForSequenceClassification''',
					    '''TFRobertaForTokenClassification''',
					    '''TFRobertaMainLayer''',
					    '''TFRobertaModel''',
					    '''TFRobertaPreTrainedModel''',
					]

try:
					if not is_flax_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowercase  							=					[
					    '''FlaxRobertaForCausalLM''',
					    '''FlaxRobertaForMaskedLM''',
					    '''FlaxRobertaForMultipleChoice''',
					    '''FlaxRobertaForQuestionAnswering''',
					    '''FlaxRobertaForSequenceClassification''',
					    '''FlaxRobertaForTokenClassification''',
					    '''FlaxRobertaModel''',
					    '''FlaxRobertaPreTrainedModel''',
					]


if TYPE_CHECKING:
					from .configuration_roberta import ROBERTA_PRETRAINED_CONFIG_ARCHIVE_MAP, RobertaConfig, RobertaOnnxConfig
					from .tokenization_roberta import RobertaTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_roberta_fast import RobertaTokenizerFast

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_roberta import (
										    ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
										    RobertaForCausalLM,
										    RobertaForMaskedLM,
										    RobertaForMultipleChoice,
										    RobertaForQuestionAnswering,
										    RobertaForSequenceClassification,
										    RobertaForTokenClassification,
										    RobertaModel,
										    RobertaPreTrainedModel,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_roberta import (
										    TF_ROBERTA_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFRobertaForCausalLM,
										    TFRobertaForMaskedLM,
										    TFRobertaForMultipleChoice,
										    TFRobertaForQuestionAnswering,
										    TFRobertaForSequenceClassification,
										    TFRobertaForTokenClassification,
										    TFRobertaMainLayer,
										    TFRobertaModel,
										    TFRobertaPreTrainedModel,
										)

					try:
										if not is_flax_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_flax_roberta import (
										    FlaxRobertaForCausalLM,
										    FlaxRobertaForMaskedLM,
										    FlaxRobertaForMultipleChoice,
										    FlaxRobertaForQuestionAnswering,
										    FlaxRobertaForSequenceClassification,
										    FlaxRobertaForTokenClassification,
										    FlaxRobertaModel,
										    FlaxRobertaPreTrainedModel,
										)

else:
					import sys

					lowercase  							=					_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


