




'''simple docstring'''







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


A									=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     _a						(			__a				):
       def __init__(			self		:			Dict     ,       *lowercase		:			int     ,       lowercase		:			int=None     ,       lowercase		:			List[str]=None     ,       lowercase		:			Dict=None     ,       **lowercase		:			Tuple       ):



           '''simple docstring'''

           super().__init__(*lowercase     ,       **lowercase       )
           UpperCAmelCase     =	eval_examples
           UpperCAmelCase     =	post_process_function
           UpperCAmelCase     =	quant_trainer_args
           UpperCAmelCase     =	128  # default number of calibration samples
       def 					A   (			self		:			Dict     ,       lowercase		:			Dict=None       ):



           '''simple docstring'''

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

           UpperCAmelCase     =	self._remove_unused_columns(lowercase     ,       description='''Calibration'''       )

           return DataLoader(
               lowercase     ,       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=lowercase     ,       )
       def 					A   (			self		:			List[Any]     ,       lowercase		:			List[Any]=None       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.train_dataset if calib_dataset is None else calib_dataset
           UpperCAmelCase     =	self.get_calib_dataloader(lowercase       )

           UpperCAmelCase     =	self.model
           quant_trainer.configure_model(lowercase     ,       self.quant_trainer_args     ,       calib=lowercase       )
           model.eval()
           quant_trainer.enable_calibration(lowercase       )

           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(lowercase       ):
               # Prediction step
               UpperCAmelCase ,  UpperCAmelCase ,  UpperCAmelCase     =	self.prediction_step(lowercase     ,       lowercase     ,       prediction_loss_only=lowercase       )
               if (step + 1) * calib_dataloader.batch_size >= self.calib_num:
                   break

           quant_trainer.finish_calibration(lowercase     ,       self.quant_trainer_args       )
           UpperCAmelCase     =	model
       def 					A   (			self		:			Union[str, Any]     ,       lowercase		:			Union[str, Any]=None     ,       lowercase		:			List[Any]=None     ,       lowercase		:			List[str]=None     ,       lowercase		:			str = "eval"       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.eval_dataset if eval_dataset is None else eval_dataset
           UpperCAmelCase     =	self.get_eval_dataloader(lowercase       )
           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     =	self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
           try:
               UpperCAmelCase     =	eval_loop(
                   lowercase     ,       description='''Evaluation'''     ,       prediction_loss_only=True if compute_metrics is None else None     ,       ignore_keys=lowercase     ,       )
           finally:
               UpperCAmelCase     =	compute_metrics

           if self.post_process_function is not None and self.compute_metrics is not None:
               UpperCAmelCase     =	self.post_process_function(lowercase     ,       lowercase     ,       output.predictions       )
               UpperCAmelCase     =	self.compute_metrics(lowercase       )

               # Prefix all keys with metric_key_prefix + '_'
               for key in list(metrics.keys()       ):
                   if not key.startswith(f"{metric_key_prefix}_"       ):
                       UpperCAmelCase     =	metrics.pop(lowercase       )

               self.log(lowercase       )
           else:
               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()       )

           UpperCAmelCase     =	self.callback_handler.on_evaluate(self.args     ,       self.state     ,       self.control     ,       lowercase       )
           return metrics
       def 					A   (			self		:			Optional[Any]     ,       lowercase		:			List[Any]     ,       lowercase		:			List[str]     ,       lowercase		:			Any=None     ,       lowercase		:			str = "test"       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.get_test_dataloader(lowercase       )

           # Temporarily disable metric computation, we will do it in the loop here.
           UpperCAmelCase     =	self.compute_metrics
           UpperCAmelCase     =	None
           UpperCAmelCase     =	self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
           try:
               UpperCAmelCase     =	eval_loop(
                   lowercase     ,       description='''Prediction'''     ,       prediction_loss_only=True if compute_metrics is None else None     ,       ignore_keys=lowercase     ,       )
           finally:
               UpperCAmelCase     =	compute_metrics

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

           UpperCAmelCase     =	self.post_process_function(lowercase     ,       lowercase     ,       output.predictions     ,       '''predict'''       )
           UpperCAmelCase     =	self.compute_metrics(lowercase       )

           # Prefix all keys with metric_key_prefix + '_'
           for key in list(metrics.keys()       ):
               if not key.startswith(f"{metric_key_prefix}_"       ):
                   UpperCAmelCase     =	metrics.pop(lowercase       )

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







       def 					A   (			self		:			Optional[Any]     ,       lowercase		:			Dict="./"       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.eval_dataset
           UpperCAmelCase     =	self.get_eval_dataloader(lowercase       )

           UpperCAmelCase     =	next(iter(lowercase       )       )

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

           # convert to tuple
           UpperCAmelCase     =	tuple(v.to(lowercase       ) for k, v in batch.items()       )

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

           UpperCAmelCase     =	True

           UpperCAmelCase     =	self.model.to(lowercase       )

           model.eval()
           model.float()

           UpperCAmelCase     =	model.module if hasattr(lowercase     ,       '''module'''       ) else model
           quant_trainer.configure_model(lowercase     ,       self.quant_trainer_args       )

           UpperCAmelCase     =	os.path.join(lowercase     ,       '''model.onnx'''       )
           logger.info(f"exporting model to {output_model_file}"       )

           UpperCAmelCase     =	{0: '''batch_size''', 1: '''seq_len'''}

           torch.onnx.export(
               lowercase     ,       lowercase     ,       lowercase     ,       export_params=lowercase     ,       opset_version=13     ,       do_constant_folding=lowercase     ,       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=lowercase     ,       )
           logger.info('''onnx export finished'''       )










'''simple docstring'''







def 		snake_case_							():
    for n in range(1					,     1_0_0_0_0_0_0  ):
        yield n * (n + 1) // 2





def 		snake_case_							(_a			:							Union[str, Any]  ):
    UpperCAmelCase     =	1
    UpperCAmelCase     =	2
    while i * i <= n:
        UpperCAmelCase     =	0
        while n % i == 0:
            n //= i
            multiplicity += 1
        divisors_count *= multiplicity + 1
        i += 1
    if n > 1:
        divisors_count *= 2
    return divisors_count





def 		snake_case_							():
    return next(i for i in triangle_number_generator() if count_divisors(_a  ) > 5_0_0  )


if __name__ == "__main__":
   print(solution())




