


'''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 	a__       (							a__						):





							'''simple docstring'''






							def __init__(	self      ,					*lowerCamelCase_      ,					lowerCamelCase_=None      ,					lowerCamelCase_=None      ,					**lowerCamelCase_     ) ->							List[str]:
														super().__init__(*lowerCamelCase_      ,					**lowerCamelCase_     )
														lowerCAmelCase__      =			eval_examples
														lowerCAmelCase__      =			post_process_function







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_=None      ,					lowerCamelCase_=None      ,					lowerCamelCase_=None      ,					lowerCamelCase_ = "eval"     ) ->							List[Any]:
														lowerCAmelCase__      =			self.eval_dataset if eval_dataset is None else eval_dataset
														lowerCAmelCase__      =			self.get_eval_dataloader(lowerCamelCase_     )
														lowerCAmelCase__      =			self.eval_examples if eval_examples is None else eval_examples

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

																					# Prefix all keys with metric_key_prefix + '_'
																					for key in list(metrics.keys()     ):
																												if not key.startswith(F"""{metric_key_prefix}_"""     ):
																																			lowerCAmelCase__      =			metrics.pop(lowerCamelCase_     )
																					metrics.update(output.metrics     )
														else:
																					lowerCAmelCase__      =			output.metrics

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

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

														lowerCAmelCase__      =			self.callback_handler.on_evaluate(self.args      ,					self.state      ,					self.control      ,					lowerCamelCase_     )
														return metrics







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_=None      ,					lowerCamelCase_ = "test"     ) ->							Optional[Any]:
														lowerCAmelCase__      =			self.get_test_dataloader(lowerCamelCase_     )

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

														lowerCAmelCase__      =			self.post_process_function(lowerCamelCase_      ,					lowerCamelCase_      ,					output.predictions      ,					'''predict'''     )
														lowerCAmelCase__      =			self.compute_metrics(lowerCamelCase_     )

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



'''simple docstring'''






from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCAmelCase					       =							logging.get_logger(__name__)

__UpperCAmelCase					       =							{'''ctrl''': '''https://huggingface.co/ctrl/resolve/main/config.json'''}




class 	a__       (							a__						):





							'''simple docstring'''






							lowercase__    :     Dict					=   "ctrl"
							lowercase__    :     str					=   ["past_key_values"]
							lowercase__    :     Optional[int]					=   {
							    "max_position_embeddings": "n_positions",
							    "hidden_size": "n_embd",
							    "num_attention_heads": "n_head",
							    "num_hidden_layers": "n_layer",
							}







							def __init__(	self      ,					lowerCamelCase_=24_65_34      ,					lowerCamelCase_=2_56      ,					lowerCamelCase_=12_80      ,					lowerCamelCase_=81_92      ,					lowerCamelCase_=48      ,					lowerCamelCase_=16      ,					lowerCamelCase_=0.1      ,					lowerCamelCase_=0.1      ,					lowerCamelCase_=1e-6      ,					lowerCamelCase_=0.02      ,					lowerCamelCase_=True      ,					**lowerCamelCase_      ,					) ->							Union[str, Any]:
														lowerCAmelCase__      =			vocab_size
														lowerCAmelCase__      =			n_positions
														lowerCAmelCase__      =			n_embd
														lowerCAmelCase__      =			n_layer
														lowerCAmelCase__      =			n_head
														lowerCAmelCase__      =			dff
														lowerCAmelCase__      =			resid_pdrop
														lowerCAmelCase__      =			embd_pdrop
														lowerCAmelCase__      =			layer_norm_epsilon
														lowerCAmelCase__      =			initializer_range

														lowerCAmelCase__      =			use_cache

														super().__init__(**lowerCamelCase_     )