
import logging
import os

import quant_trainer
import torch
from torch.utils.data import DataLoader

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput


lowercase_ 				=			logging.getLogger(__name__)

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






     """simple docstring"""







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







       """simple docstring"""




       super().__init__(*_A     ,						**_A )
       __SCREAMING_SNAKE_CASE      :	List[str]					=		eval_examples
       __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		post_process_function
       __SCREAMING_SNAKE_CASE      :	Tuple					=		quant_trainer_args
       __SCREAMING_SNAKE_CASE      :	int					=		128  # default number of calibration samples



     def  UpperCAmelCase__							(				self							:							int     ,						_A							:							Optional[int]=None ):







       """simple docstring"""




       if calib_dataset is None and self.calib_dataset is None:
         raise ValueError('''Trainer: calibration requires an calib_dataset.''' )
       __SCREAMING_SNAKE_CASE      :	Tuple					=		calib_dataset if calib_dataset is not None else self.calib_dataset

       __SCREAMING_SNAKE_CASE      :	str					=		self._remove_unused_columns(_A     ,						description='''Calibration''' )

       return DataLoader(
           _A     ,						batch_size=self.args.eval_batch_size     ,						collate_fn=self.data_collator     ,						drop_last=self.args.dataloader_drop_last     ,						num_workers=self.args.dataloader_num_workers     ,						pin_memory=self.args.dataloader_pin_memory     ,						shuffle=_A     ,						)



     def  UpperCAmelCase__							(				self							:							Optional[int]     ,						_A							:							Tuple=None ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	List[str]					=		self.train_dataset if calib_dataset is None else calib_dataset
       __SCREAMING_SNAKE_CASE      :	Tuple					=		self.get_calib_dataloader(_A )

       __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		self.model
       quant_trainer.configure_model(_A     ,						self.quant_trainer_args     ,						calib=_A )
       model.eval()
       quant_trainer.enable_calibration(_A )

       logger.info('''***** Running calibration *****''' )
       logger.info(F'''  Num examples = {self.calib_num}''' )
       logger.info(F'''  Batch size = {calib_dataloader.batch_size}''' )

       for step, inputs in enumerate(_A ):
         # Prediction step
         __SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE      :	List[str]					=		self.prediction_step(_A     ,						_A     ,						prediction_loss_only=_A )
         if (step + 1) * calib_dataloader.batch_size >= self.calib_num:
           break

       quant_trainer.finish_calibration(_A     ,						self.quant_trainer_args )
       __SCREAMING_SNAKE_CASE      :	Dict					=		model



     def  UpperCAmelCase__							(				self							:							List[str]     ,						_A							:							int=None     ,						_A							:							Optional[Any]=None     ,						_A							:							List[Any]=None     ,						_A							:							str = "eval" ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	List[str]					=		self.eval_dataset if eval_dataset is None else eval_dataset
       __SCREAMING_SNAKE_CASE      :	str					=		self.get_eval_dataloader(_A )
       __SCREAMING_SNAKE_CASE      :	List[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      :	List[str]					=		self.compute_metrics
       __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		None
       __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
       try:
         __SCREAMING_SNAKE_CASE      :	Optional[int]					=		eval_loop(
             _A     ,						description='''Evaluation'''     ,						prediction_loss_only=True if compute_metrics is None else None     ,						ignore_keys=_A     ,						)
       finally:
         __SCREAMING_SNAKE_CASE      :	List[str]					=		compute_metrics

       if self.post_process_function is not None and self.compute_metrics is not None:
         __SCREAMING_SNAKE_CASE      :	Dict					=		self.post_process_function(_A     ,						_A     ,						output.predictions )
         __SCREAMING_SNAKE_CASE      :	str					=		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}_''' ):
             __SCREAMING_SNAKE_CASE      :	Tuple					=		metrics.pop(_A )

         self.log(_A )
       else:
         __SCREAMING_SNAKE_CASE      :	str					=		{}

       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[str]					=		self.callback_handler.on_evaluate(self.args     ,						self.state     ,						self.control     ,						_A )
       return metrics



     def  UpperCAmelCase__							(				self							:							Tuple     ,						_A							:							int     ,						_A							:							Optional[int]     ,						_A							:							str=None     ,						_A							:							str = "test" ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	str					=		self.get_test_dataloader(_A )

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

       if self.post_process_function is None or self.compute_metrics is None:
         return output

       __SCREAMING_SNAKE_CASE      :	Dict					=		self.post_process_function(_A     ,						_A     ,						output.predictions     ,						'''predict''' )
       __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		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}_''' ):
           __SCREAMING_SNAKE_CASE      :	List[str]					=		metrics.pop(_A )

       return PredictionOutput(predictions=predictions.predictions     ,						label_ids=predictions.label_ids     ,						metrics=_A )




     def  UpperCAmelCase__							(				self							:							Dict     ,						_A							:							List[Any]="./" ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Any					=		self.eval_dataset
       __SCREAMING_SNAKE_CASE      :	int					=		self.get_eval_dataloader(_A )

       __SCREAMING_SNAKE_CASE      :	str					=		next(iter(_A ) )

       # saving device - to make it consistent
       __SCREAMING_SNAKE_CASE      :	str					=		torch.device('''cuda''' if torch.cuda.is_available() else '''cpu''' )

       # convert to tuple
       __SCREAMING_SNAKE_CASE      :	List[Any]					=		tuple(v.to(_A ) for k, v in batch.items() )

       logger.info('''Converting model to be onnx compatible''' )
       from pytorch_quantization.nn import TensorQuantizer

       __SCREAMING_SNAKE_CASE      :	Dict					=		True

       __SCREAMING_SNAKE_CASE      :	Dict					=		self.model.to(_A )

       model.eval()
       model.float()

       __SCREAMING_SNAKE_CASE      :	str					=		model.module if hasattr(_A     ,						'''module''' ) else model
       quant_trainer.configure_model(_A     ,						self.quant_trainer_args )

       __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		os.path.join(_A     ,						'''model.onnx''' )
       logger.info(F'''exporting model to {output_model_file}''' )

       __SCREAMING_SNAKE_CASE      :	List[str]					=		{0: '''batch_size''', 1: '''seq_len'''}

       torch.onnx.export(
           _A     ,						_A     ,						_A     ,						export_params=_A     ,						opset_version=13     ,						do_constant_folding=_A     ,						input_names=['''input_ids''', '''attention_mask''', '''token_type_ids''']     ,						output_names=['''output_start_logits''', '''output_end_logits''']     ,						dynamic_axes={
               '''input_ids''': axes,
               '''attention_mask''': axes,
               '''token_type_ids''': axes,
               '''output_start_logits''': axes,
               '''output_end_logits''': axes,
           }     ,						verbose=_A     ,						)
       logger.info('''onnx export finished''' )




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


lowerCAmelCase       		=    logging.get_logger(__name__)

lowerCAmelCase       		=    {
    """abeja/gpt-neox-japanese-2.7b""": """https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json""",
}




class 							lowerCamelCase   (  _UpperCamelCase ):
	_lowerCAmelCase     :		Tuple										=						'''gpt_neox_japanese'''


	def __init__(    self				,     lowercase__=3_2_0_0_0				,     lowercase__=2_5_6_0				,     lowercase__=3_2				,     lowercase__=3_2				,     lowercase__=4				,     lowercase__="gelu"				,     lowercase__=1.0_0				,     lowercase__=1_0_0_0_0				,     lowercase__=2_0_4_8				,     lowercase__=0.0_2				,     lowercase__=1e-5				,     lowercase__=True				,     lowercase__=3_1_9_9_6				,     lowercase__=3_1_9_9_9				,     lowercase__=0.1				,     lowercase__=0.0				,     **lowercase__				,     ):
							super().__init__(bos_token_id=lowercase__				,     eos_token_id=lowercase__				,     **lowercase__)
							__UpperCAmelCase			:							Optional[Any]	    =	vocab_size
							__UpperCAmelCase			:							Dict	    =	max_position_embeddings
							__UpperCAmelCase			:							int	    =	hidden_size
							__UpperCAmelCase			:							List[Any]	    =	num_hidden_layers
							__UpperCAmelCase			:							Union[str, Any]	    =	num_attention_heads
							__UpperCAmelCase			:							Optional[int]	    =	intermediate_multiple_size
							__UpperCAmelCase			:							Any	    =	hidden_act
							__UpperCAmelCase			:							str	    =	rotary_pct
							__UpperCAmelCase			:							Any	    =	rotary_emb_base
							__UpperCAmelCase			:							Dict	    =	initializer_range
							__UpperCAmelCase			:							Tuple	    =	layer_norm_eps
							__UpperCAmelCase			:							Optional[int]	    =	use_cache
							__UpperCAmelCase			:							str	    =	attention_dropout
							__UpperCAmelCase			:							Any	    =	hidden_dropout

