





'''simple docstring'''




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 			UpperCamelCase__ ( __SCREAMING_SNAKE_CASE       ):




					"""simple docstring"""


					def __init__(		self							:       str	,		*lowerCamelCase_							:       Any	,		lowerCamelCase_							:       Any=None	,		lowerCamelCase_							:       List[Any]=None	,		**lowerCamelCase_							:       Optional[int]		):

							'''simple docstring'''



							super().__init__(*UpperCAmelCase_	,		**UpperCAmelCase_		)
							SCREAMING_SNAKE_CASE      :							Any						=       eval_examples
							SCREAMING_SNAKE_CASE      :							Dict						=       post_process_function


					def lowerCamelCase_       (		self							:       Dict	,		lowerCamelCase_							:       int=None	,		lowerCamelCase_							:       Any=None	,		lowerCamelCase_							:       Optional[Any]=None	,		lowerCamelCase_							:       str = "eval"		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE      :							Any						=       self.eval_dataset if eval_dataset is None else eval_dataset
							SCREAMING_SNAKE_CASE      :							Tuple						=       self.get_eval_dataloader(UpperCAmelCase_		)
							SCREAMING_SNAKE_CASE      :							Any						=       self.eval_examples if eval_examples is None else eval_examples

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

									# Prefix all keys with metric_key_prefix + '_'
									for key in list(metrics.keys()		):
											if not key.startswith(f'''{metric_key_prefix}_'''		):
													SCREAMING_SNAKE_CASE      :							str						=       metrics.pop(UpperCAmelCase_		)
									metrics.update(output.metrics		)
							else:
									SCREAMING_SNAKE_CASE      :							Any						=       output.metrics

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

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

							SCREAMING_SNAKE_CASE      :							List[Any]						=       self.callback_handler.on_evaluate(self.args	,		self.state	,		self.control	,		UpperCAmelCase_		)
							return metrics



					def lowerCamelCase_       (		self							:       str	,		lowerCamelCase_							:       Any	,		lowerCamelCase_							:       Dict	,		lowerCamelCase_							:       Dict=None	,		lowerCamelCase_							:       str = "test"		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE      :							List[Any]						=       self.get_test_dataloader(UpperCAmelCase_		)

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

							SCREAMING_SNAKE_CASE      :							Union[str, Any]						=       self.post_process_function(UpperCAmelCase_	,		UpperCAmelCase_	,		output.predictions	,		"""predict"""		)
							SCREAMING_SNAKE_CASE      :							List[str]						=       self.compute_metrics(UpperCAmelCase_		)

							# Prefix all keys with metric_key_prefix + '_'
							for key in list(metrics.keys()		):
									if not key.startswith(f'''{metric_key_prefix}_'''		):
											SCREAMING_SNAKE_CASE      :							Optional[Any]						=       metrics.pop(UpperCAmelCase_		)
							metrics.update(output.metrics		)
							return PredictionOutput(predictions=predictions.predictions	,		label_ids=predictions.label_ids	,		metrics=UpperCAmelCase_		)










from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__A									=				{"configuration_ibert": ["IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "IBertConfig", "IBertOnnxConfig"]}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __A									=				[
         "IBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
         "IBertForMaskedLM",
         "IBertForMultipleChoice",
         "IBertForQuestionAnswering",
         "IBertForSequenceClassification",
         "IBertForTokenClassification",
         "IBertModel",
         "IBertPreTrainedModel",
     ]

if TYPE_CHECKING:
     from .configuration_ibert import IBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, IBertConfig, IBertOnnxConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_ibert import (
              IBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
              IBertForMaskedLM,
              IBertForMultipleChoice,
              IBertForQuestionAnswering,
              IBertForSequenceClassification,
              IBertForTokenClassification,
              IBertModel,
              IBertPreTrainedModel,
          )

else:
     import sys

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

