



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





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







							'''simple docstring'''
							super().__init__(*lowerCamelCase__			,**lowerCamelCase__			)
							_UpperCamelCase :		Any			    =       eval_examples
							_UpperCamelCase :		Optional[int]			    =       post_process_function





	def      UpperCamelCase_  (			self :    Tuple			,lowerCamelCase__ :    Optional[Dataset] = None			,lowerCamelCase__ :    List[Any]=None			,lowerCamelCase__ :    Optional[List[str]] = None			,lowerCamelCase__ :    str = "eval"			,**lowerCamelCase__ :    Any			,):







							'''simple docstring'''
							_UpperCamelCase :		Optional[int]			    =       gen_kwargs.copy()
							_UpperCamelCase :		int			    =       (
							    gen_kwargs['max_length'] if gen_kwargs.get('max_length'			) is not None else self.args.generation_max_length
							)
							_UpperCamelCase :		Union[str, Any]			    =       (
							    gen_kwargs['num_beams'] if gen_kwargs.get('num_beams'			) is not None else self.args.generation_num_beams
							)
							_UpperCamelCase :		str			    =       gen_kwargs

							_UpperCamelCase :		List[str]			    =       self.eval_dataset if eval_dataset is None else eval_dataset
							_UpperCamelCase :		Tuple			    =       self.get_eval_dataloader(lowerCamelCase__			)
							_UpperCamelCase :		Optional[Any]			    =       self.eval_examples if eval_examples is None else eval_examples

							# Temporarily disable metric computation, we will do it in the loop here.
							_UpperCamelCase :		Dict			    =       self.compute_metrics
							_UpperCamelCase :		str			    =       None
							_UpperCamelCase :		Optional[int]			    =       time.time()
							_UpperCamelCase :		Any			    =       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
							try:
													_UpperCamelCase :		Any			    =       eval_loop(
													    lowerCamelCase__			,description='Evaluation'			,prediction_loss_only=True if compute_metrics is None else None			,ignore_keys=lowerCamelCase__			,metric_key_prefix=lowerCamelCase__			,)
							finally:
													_UpperCamelCase :		Optional[int]			    =       compute_metrics
							_UpperCamelCase :		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(
							        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
													_UpperCamelCase :		int			    =       self.post_process_function(lowerCamelCase__			,lowerCamelCase__			,lowerCamelCase__			)
													_UpperCamelCase :		int			    =       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}_'			):
																									_UpperCamelCase :		List[Any]			    =       metrics.pop(lowerCamelCase__			)

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

							_UpperCamelCase :		Union[str, Any]			    =       self.callback_handler.on_evaluate(self.args			,self.state			,self.control			,lowerCamelCase__			)
							return metrics





	def      UpperCamelCase_  (			self :    int			,lowerCamelCase__ :    Optional[Any]			,lowerCamelCase__ :    Optional[int]			,lowerCamelCase__ :    Dict=None			,lowerCamelCase__ :    str = "test"			,**lowerCamelCase__ :    Union[str, Any]			):







							'''simple docstring'''
							_UpperCamelCase :		int			    =       gen_kwargs.copy()

							_UpperCamelCase :		List[Any]			    =       self.get_test_dataloader(lowerCamelCase__			)

							# Temporarily disable metric computation, we will do it in the loop here.
							_UpperCamelCase :		str			    =       self.compute_metrics
							_UpperCamelCase :		int			    =       None
							_UpperCamelCase :		int			    =       time.time()
							_UpperCamelCase :		int			    =       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
							try:
													_UpperCamelCase :		Optional[Any]			    =       eval_loop(
													    lowerCamelCase__			,description='Prediction'			,prediction_loss_only=True if compute_metrics is None else None			,ignore_keys=lowerCamelCase__			,metric_key_prefix=lowerCamelCase__			,)
							finally:
													_UpperCamelCase :		Tuple			    =       compute_metrics

							_UpperCamelCase :		Union[str, Any]			    =       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

							_UpperCamelCase :		Dict			    =       self.post_process_function(lowerCamelCase__			,lowerCamelCase__			,lowerCamelCase__			,'predict'			)
							_UpperCamelCase :		Optional[Any]			    =       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}_'			):
																			_UpperCamelCase :		Dict			    =       metrics.pop(lowerCamelCase__			)
							metrics.update(output.metrics			)
							return PredictionOutput(predictions=predictions.predictions			,label_ids=predictions.label_ids			,metrics=lowerCamelCase__			)







'''simple docstring'''
from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


snake_case_			:     int  	=  logging.get_logger(__name__)

snake_case_			:     List[str]  	=  {
    'EleutherAI/gpt-j-6B': 'https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json',
    # See all GPT-J models at https://huggingface.co/models?filter=gpt_j
}






class 							lowercase__     (							lowercase       ):
	lowercase__      =      """gptj"""
	lowercase__      =      {
	    """max_position_embeddings""": """n_positions""",
	    """hidden_size""": """n_embd""",
	    """num_attention_heads""": """n_head""",
	    """num_hidden_layers""": """n_layer""",
	}





	def __init__(			self :    Any			,lowerCamelCase__ :    Optional[Any]=50400			,lowerCamelCase__ :    Tuple=2048			,lowerCamelCase__ :    Tuple=4096			,lowerCamelCase__ :    int=28			,lowerCamelCase__ :    Optional[Any]=16			,lowerCamelCase__ :    Optional[Any]=64			,lowerCamelCase__ :    List[Any]=None			,lowerCamelCase__ :    List[Any]="gelu_new"			,lowerCamelCase__ :    Optional[Any]=0.0			,lowerCamelCase__ :    List[str]=0.0			,lowerCamelCase__ :    List[Any]=0.0			,lowerCamelCase__ :    Tuple=1E-5			,lowerCamelCase__ :    int=0.0_2			,lowerCamelCase__ :    Any=True			,lowerCamelCase__ :    str=50256			,lowerCamelCase__ :    Any=50256			,lowerCamelCase__ :    Tuple=False			,**lowerCamelCase__ :    Optional[Any]			,):







							'''simple docstring'''
							_UpperCamelCase :		Optional[Any]			    =       vocab_size
							_UpperCamelCase :		Optional[Any]			    =       n_positions
							_UpperCamelCase :		Union[str, Any]			    =       n_embd
							_UpperCamelCase :		Any			    =       n_layer
							_UpperCamelCase :		Optional[int]			    =       n_head
							_UpperCamelCase :		List[str]			    =       n_inner
							_UpperCamelCase :		List[Any]			    =       rotary_dim
							_UpperCamelCase :		int			    =       activation_function
							_UpperCamelCase :		Dict			    =       resid_pdrop
							_UpperCamelCase :		Any			    =       embd_pdrop
							_UpperCamelCase :		Union[str, Any]			    =       attn_pdrop
							_UpperCamelCase :		Union[str, Any]			    =       layer_norm_epsilon
							_UpperCamelCase :		Optional[Any]			    =       initializer_range
							_UpperCamelCase :		str			    =       use_cache

							_UpperCamelCase :		Union[str, Any]			    =       bos_token_id
							_UpperCamelCase :		Any			    =       eos_token_id

							super().__init__(
							    bos_token_id=lowerCamelCase__			,eos_token_id=lowerCamelCase__			,tie_word_embeddings=lowerCamelCase__			,**lowerCamelCase__			)






class 							lowercase__     (							lowercase       ):





	def __init__(			self :    Tuple			,lowerCamelCase__ :    PretrainedConfig			,lowerCamelCase__ :    str = "default"			,lowerCamelCase__ :    List[PatchingSpec] = None			,lowerCamelCase__ :    bool = False			,):







							'''simple docstring'''
							super().__init__(lowerCamelCase__			,task=lowerCamelCase__			,patching_specs=lowerCamelCase__			,use_past=lowerCamelCase__			)
							if not getattr(self._config			,'pad_token_id'			,lowerCamelCase__			):
													# TODO: how to do that better?
													_UpperCamelCase :		int			    =       0





	@property
	def      UpperCamelCase_  (			self :    Optional[Any]			):







							'''simple docstring'''
							_UpperCamelCase :		List[str]			    =       OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}			)
							if self.use_past:
													self.fill_with_past_key_values_(lowerCamelCase__			,direction='inputs'			)
													_UpperCamelCase :		Tuple			    =       {0: 'batch', 1: 'past_sequence + sequence'}
							else:
													_UpperCamelCase :		Any			    =       {0: 'batch', 1: 'sequence'}

							return common_inputs





	@property
	def      UpperCamelCase_  (			self :    Tuple			):







							'''simple docstring'''
							return self._config.n_layer





	@property
	def      UpperCamelCase_  (			self :    List[str]			):







							'''simple docstring'''
							return self._config.n_head





	def      UpperCamelCase_  (			self :    List[str]			,lowerCamelCase__ :    PreTrainedTokenizer			,lowerCamelCase__ :    int = -1			,lowerCamelCase__ :    int = -1			,lowerCamelCase__ :    bool = False			,lowerCamelCase__ :    Optional[TensorType] = None			,):







							'''simple docstring'''
							_UpperCamelCase :		Union[str, Any]			    =       super(lowerCamelCase__			,self			).generate_dummy_inputs(
							    lowerCamelCase__			,batch_size=lowerCamelCase__			,seq_length=lowerCamelCase__			,is_pair=lowerCamelCase__			,framework=lowerCamelCase__			)

							# We need to order the input in the way they appears in the forward()
							_UpperCamelCase :		Tuple			    =       OrderedDict({'input_ids': common_inputs['input_ids']}			)

							# Need to add the past_keys
							if self.use_past:
													if not is_torch_available():
																			raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'			)
													else:
																			import torch

																			_UpperCamelCase    ,      _UpperCamelCase :		str			    =       common_inputs['input_ids'].shape
																			# Not using the same length for past_key_values
																			_UpperCamelCase :		Optional[int]			    =       seqlen + 2
																			_UpperCamelCase :		List[Any]			    =       (
																			    batch,
																			    self.num_attention_heads,
																			    past_key_values_length,
																			    self._config.hidden_size // self.num_attention_heads,
																			)
																			_UpperCamelCase :		Optional[Any]			    =       [
																			    (torch.zeros(lowerCamelCase__			), torch.zeros(lowerCamelCase__			)) for _ in range(self.num_layers			)
																			]

							_UpperCamelCase :		Union[str, Any]			    =       common_inputs['attention_mask']
							if self.use_past:
													_UpperCamelCase :		Any			    =       ordered_inputs['attention_mask'].dtype
													_UpperCamelCase :		List[str]			    =       torch.cat(
													    [ordered_inputs['attention_mask'], torch.ones(lowerCamelCase__			,lowerCamelCase__			,dtype=lowerCamelCase__			)]			,dim=1			)

							return ordered_inputs





	@property
	def      UpperCamelCase_  (			self :    str			):







							'''simple docstring'''
							return 13


