

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


UpperCAmelCase__ :     Dict				    =    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__       (   UpperCAmelCase       ):







		"""simple docstring"""




		def __init__(			self    : Tuple				,				*UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : List[Any]=None				,				UpperCAmelCase__    : str=None				,				UpperCAmelCase__    : str=None				,				**UpperCAmelCase__    : List[str]		)					->Tuple:
								"""simple docstring"""







								super().__init__(*UpperCAmelCase__				,				**UpperCAmelCase__		)
								SCREAMING_SNAKE_CASE :      str       =   eval_examples
								SCREAMING_SNAKE_CASE :      Optional[Any]       =   post_process_function
								SCREAMING_SNAKE_CASE :      List[str]       =   quant_trainer_args
								SCREAMING_SNAKE_CASE :      Tuple       =   1_2_8  # default number of calibration samples


		def  _lowercase      (			self    : Dict				,				UpperCAmelCase__    : Tuple=None		)					->Union[str, Any]:
								"""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 :      Any       =   self._remove_unused_columns(UpperCAmelCase__				,				description="""Calibration"""		)

								return DataLoader(
								    UpperCAmelCase__				,				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=UpperCAmelCase__				,				)


		def  _lowercase      (			self    : Any				,				UpperCAmelCase__    : Optional[int]=None		)					->Any:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      int       =   self.train_dataset if calib_dataset is None else calib_dataset
								SCREAMING_SNAKE_CASE :      List[Any]       =   self.get_calib_dataloader(UpperCAmelCase__		)

								SCREAMING_SNAKE_CASE :      int       =   self.model
								quant_trainer.configure_model(UpperCAmelCase__				,				self.quant_trainer_args				,				calib=UpperCAmelCase__		)
								model.eval()
								quant_trainer.enable_calibration(UpperCAmelCase__		)

								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(UpperCAmelCase__		):
														# Prediction step
														SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      Optional[int]       =   self.prediction_step(UpperCAmelCase__				,				UpperCAmelCase__				,				prediction_loss_only=UpperCAmelCase__		)
														if (step + 1) * calib_dataloader.batch_size >= self.calib_num:
																				break

								quant_trainer.finish_calibration(UpperCAmelCase__				,				self.quant_trainer_args		)
								SCREAMING_SNAKE_CASE :      Optional[Any]       =   model


		def  _lowercase      (			self    : List[str]				,				UpperCAmelCase__    : Tuple=None				,				UpperCAmelCase__    : Optional[Any]=None				,				UpperCAmelCase__    : Optional[Any]=None				,				UpperCAmelCase__    : str = "eval"		)					->List[str]:
								"""simple docstring"""







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

								if self.post_process_function is not None and self.compute_metrics is not None:
														SCREAMING_SNAKE_CASE :      List[Any]       =   self.post_process_function(UpperCAmelCase__				,				UpperCAmelCase__				,				output.predictions		)
														SCREAMING_SNAKE_CASE :      Dict       =   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 :      Any       =   metrics.pop(UpperCAmelCase__		)

														self.log(UpperCAmelCase__		)
								else:
														SCREAMING_SNAKE_CASE :      List[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 :      Any       =   self.callback_handler.on_evaluate(self.args				,				self.state				,				self.control				,				UpperCAmelCase__		)
								return metrics


		def  _lowercase      (			self    : List[Any]				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : List[str]				,				UpperCAmelCase__    : Dict=None				,				UpperCAmelCase__    : str = "test"		)					->Dict:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      List[str]       =   self.get_test_dataloader(UpperCAmelCase__		)

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

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

								SCREAMING_SNAKE_CASE :      str       =   self.post_process_function(UpperCAmelCase__				,				UpperCAmelCase__				,				output.predictions				,				"""predict"""		)
								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 :      Optional[int]       =   metrics.pop(UpperCAmelCase__		)

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




		def  _lowercase      (			self    : Any				,				UpperCAmelCase__    : Dict="./"		)					->List[str]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Any       =   self.eval_dataset
								SCREAMING_SNAKE_CASE :      str       =   self.get_eval_dataloader(UpperCAmelCase__		)

								SCREAMING_SNAKE_CASE :      Dict       =   next(iter(UpperCAmelCase__		)		)

								# saving device - to make it consistent
								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   torch.device("""cuda""" if torch.cuda.is_available() else """cpu"""		)

								# convert to tuple
								SCREAMING_SNAKE_CASE :      Dict       =   tuple(v.to(UpperCAmelCase__		) for k, v in batch.items()		)

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

								SCREAMING_SNAKE_CASE :      Tuple       =   True

								SCREAMING_SNAKE_CASE :      str       =   self.model.to(UpperCAmelCase__		)

								model.eval()
								model.float()

								SCREAMING_SNAKE_CASE :      str       =   model.module if hasattr(UpperCAmelCase__				,				"""module"""		) else model
								quant_trainer.configure_model(UpperCAmelCase__				,				self.quant_trainer_args		)

								SCREAMING_SNAKE_CASE :      Any       =   os.path.join(UpperCAmelCase__				,				"""model.onnx"""		)
								logger.info(f"exporting model to {output_model_file}"		)

								SCREAMING_SNAKE_CASE :      Any       =   {0: """batch_size""", 1: """seq_len"""}

								torch.onnx.export(
								    UpperCAmelCase__				,				UpperCAmelCase__				,				UpperCAmelCase__				,				export_params=UpperCAmelCase__				,				opset_version=1_3				,				do_constant_folding=UpperCAmelCase__				,				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=UpperCAmelCase__				,				)
								logger.info("""onnx export finished"""		)







import os
import random
import sys

from . import cryptomath_module as cryptomath
from . import rabin_miller

UpperCAmelCase__ :     Optional[Any]				    =    3






def 					__lowercase      (     _A			)     ->						int:
      print("""Generating primitive root of p"""			)
      while True:
            SCREAMING_SNAKE_CASE :      int       =   random.randrange(3						,    _A			)
            if pow(_A						,    2						,    _A			) == 1:
                  continue
            if pow(_A						,    _A						,    _A			) == 1:
                  continue
            return g






def 					__lowercase      (     _A			)     ->						tuple[tuple[int, int, int, int], tuple[int, int]]:
      print("""Generating prime p..."""			)
      SCREAMING_SNAKE_CASE :      Optional[Any]       =   rabin_miller.generate_large_prime(_A			)  # select large prime number.
      SCREAMING_SNAKE_CASE :      Optional[Any]       =   primitive_root(_A			)  # one primitive root on modulo p.
      SCREAMING_SNAKE_CASE :      Optional[Any]       =   random.randrange(3						,    _A			)  # private_key -> have to be greater than 2 for safety.
      SCREAMING_SNAKE_CASE :      List[str]       =   cryptomath.find_mod_inverse(pow(_A						,    _A						,    _A			)						,    _A			)

      SCREAMING_SNAKE_CASE :      str       =   (key_size, e_a, e_a, p)
      SCREAMING_SNAKE_CASE :      Union[str, Any]       =   (key_size, d)

      return public_key, private_key






def 					__lowercase      (     _A						,    _A			)     ->						None:
      if os.path.exists(F"{name}_pubkey.txt"			) or os.path.exists(F"{name}_privkey.txt"			):
            print("""\nWARNING:"""			)
            print(
                F"\"{name}_pubkey.txt\" or \"{name}_privkey.txt\" already exists. \n"
                """Use a different name or delete these files and re-run this program."""			)
            sys.exit()

      SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      List[str]       =   generate_key(_A			)
      print(F"\nWriting public key to file {name}_pubkey.txt..."			)
      with open(F"{name}_pubkey.txt"						,    """w"""			) as fo:
            fo.write(F"{public_key[0]},{public_key[1]},{public_key[2]},{public_key[3]}"			)

      print(F"Writing private key to file {name}_privkey.txt..."			)
      with open(F"{name}_privkey.txt"						,    """w"""			) as fo:
            fo.write(F"{private_key[0]},{private_key[1]}"			)






def 					__lowercase      (     )     ->						None:
      print("""Making key files..."""			)
      make_key_files("""elgamal"""						,    2048			)
      print("""Key files generation successful"""			)


if __name__ == "__main__":
  main()




