



import random
import timeit
from functools import wraps
from typing import Callable, Optional

from ..configuration_utils import PretrainedConfig
from ..models.auto.modeling_tf_auto import TF_MODEL_MAPPING, TF_MODEL_WITH_LM_HEAD_MAPPING
from ..utils import is_pyanvml_available, is_tf_available, logging
from .benchmark_utils import (
    Benchmark,
    Memory,
    MemorySummary,
    measure_peak_memory_cpu,
    start_memory_tracing,
    stop_memory_tracing,
)


if is_tf_available():
							import tensorflow as tf
							from tensorflow.python.framework.errors_impl import ResourceExhaustedError

							from .benchmark_args_tf import TensorFlowBenchmarkArguments

if is_pyanvml_available():
							import pyanvml.pyanvml as nvml

__snake_case    :		Any  =logging.get_logger(__name__)






def      lowerCAmelCase__		(   lowerCamelCase_		:		bool	,lowerCamelCase_		:		bool):





				'''simple docstring'''

				def run_func(lowerCamelCase_		:		Union[str, Any]):
								@wraps(lowerCamelCase_)
								def run_in_eager_mode(*lowerCamelCase_		:		Any	,**lowerCamelCase_		:		int):
												return func(*lowerCamelCase_	,**lowerCamelCase_)

								@wraps(lowerCamelCase_)
								@tf.function(experimental_compile=lowerCamelCase_)
								def run_in_graph_mode(*lowerCamelCase_		:		List[str]	,**lowerCamelCase_		:		Dict):
												return func(*lowerCamelCase_	,**lowerCamelCase_)

								if do_eager_mode is True:
												if use_xla is not False:
																raise ValueError(
																    '''Cannot run model in XLA, if `args.eager_mode` is set to `True`. Please set `args.eager_mode=False`.''')
												return run_in_eager_mode
								else:
												return run_in_graph_mode

				return run_func






def      lowerCAmelCase__		(   lowerCamelCase_		:		int	,lowerCamelCase_		:		int	,lowerCamelCase_		:		int):





				'''simple docstring'''

				lowerCAmelCase__					:    int												=					random.Random()
				lowerCAmelCase__					:    int												=					[rng.randint(0	,vocab_size - 1) for i in range(batch_size * sequence_length)]
				return tf.constant(lowerCamelCase_	,shape=(batch_size, sequence_length)	,dtype=tf.intaa)



class 	lowerCamelCase__      (							lowerCamelCase__):




		'''simple docstring'''



		snake_case_     =42
		snake_case_     =42
		snake_case_     ="""TensorFlow"""



		@property
		def 		lowerCAmelCase__				(self			)					->      Any:

						"""simple docstring"""






						return tf.__version__



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      float:

						"""simple docstring"""






						lowerCAmelCase__					:    int												=					self.args.strategy
						if strategy is None:
										raise ValueError('''A device strategy has to be initialized before using TensorFlow.'''			)
						lowerCAmelCase__					:    List[Any]												=					self._prepare_inference_func(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)
						return self._measure_speed(_inference			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      float:

						"""simple docstring"""






						lowerCAmelCase__					:    List[Any]												=					self.args.strategy
						if strategy is None:
										raise ValueError('''A device strategy has to be initialized before using TensorFlow.'''			)
						lowerCAmelCase__					:    Union[str, Any]												=					self._prepare_train_func(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)
						return self._measure_speed(_train			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      [Memory, Optional[MemorySummary]]:

						"""simple docstring"""






						if self.args.is_gpu:
										tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]		,__lowerCamelCase			)
						lowerCAmelCase__					:    Union[str, Any]												=					self.args.strategy
						if strategy is None:
										raise ValueError('''A device strategy has to be initialized before using TensorFlow.'''			)
						lowerCAmelCase__					:    List[str]												=					self._prepare_inference_func(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)
						return self._measure_memory(_inference			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      [Memory, Optional[MemorySummary]]:

						"""simple docstring"""






						if self.args.is_gpu:
										tf.config.experimental.set_memory_growth(self.args.gpu_list[self.args.device_idx]		,__lowerCamelCase			)
						lowerCAmelCase__					:    int												=					self.args.strategy
						if strategy is None:
										raise ValueError('''A device strategy has to be initialized before using TensorFlow.'''			)

						lowerCAmelCase__					:    Union[str, Any]												=					self._prepare_train_func(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)
						return self._measure_memory(_train			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      Callable[[], None]:

						"""simple docstring"""






						lowerCAmelCase__					:    List[str]												=					self.config_dict[model_name]

						if self.args.fpaa:
										raise NotImplementedError('''Mixed precision is currently not supported.'''			)

						lowerCAmelCase__					:    int												=					(
						    hasattr(__lowerCamelCase		,'''architectures'''			)
						    and isinstance(config.architectures		,__lowerCamelCase			)
						    and len(config.architectures			) > 0
						)
						if not self.args.only_pretrain_model and has_model_class_in_config:
										try:
														lowerCAmelCase__					:    List[Any]												=					'''TF''' + config.architectures[0]  # prepend 'TF' for tensorflow model
														lowerCAmelCase__					:    Optional[int]												=					__import__('''transformers'''		,fromlist=[model_class]			)
														lowerCAmelCase__					:    List[Any]												=					getattr(__lowerCamelCase		,__lowerCamelCase			)
														lowerCAmelCase__					:    Dict												=					model_cls(__lowerCamelCase			)
										except ImportError:
														raise ImportError(
														    f"""{model_class} does not exist. If you just want to test the pretrained model, you might want to"""
														    ''' set `--only_pretrain_model` or `args.only_pretrain_model=True`.'''			)
						else:
										lowerCAmelCase__					:    List[Any]												=					TF_MODEL_MAPPING[config.__class__](__lowerCamelCase			)

						# encoder-decoder has vocab size saved differently
						lowerCAmelCase__					:    List[Any]												=					config.vocab_size if hasattr(__lowerCamelCase		,'''vocab_size'''			) else config.encoder.vocab_size
						lowerCAmelCase__					:    str												=					random_input_ids(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)

						@run_with_tf_optimizations(self.args.eager_mode		,self.args.use_xla			)
						def encoder_decoder_forward():
										return model(__lowerCamelCase		,decoder_input_ids=__lowerCamelCase		,training=__lowerCamelCase			)

						@run_with_tf_optimizations(self.args.eager_mode		,self.args.use_xla			)
						def encoder_forward():
										return model(__lowerCamelCase		,training=__lowerCamelCase			)

						lowerCAmelCase__					:    Union[str, Any]												=					encoder_decoder_forward if config.is_encoder_decoder else encoder_forward

						return _inference



		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      Callable[[], None]:

						"""simple docstring"""






						lowerCAmelCase__					:    List[str]												=					self.config_dict[model_name]

						if self.args.eager_mode is not False:
										raise ValueError('''Training cannot be done in eager mode. Please make sure that `args.eager_mode = False`.'''			)

						if self.args.fpaa:
										raise NotImplementedError('''Mixed precision is currently not supported.'''			)

						lowerCAmelCase__					:    Union[str, Any]												=					(
						    hasattr(__lowerCamelCase		,'''architectures'''			)
						    and isinstance(config.architectures		,__lowerCamelCase			)
						    and len(config.architectures			) > 0
						)
						if not self.args.only_pretrain_model and has_model_class_in_config:
										try:
														lowerCAmelCase__					:    Dict												=					'''TF''' + config.architectures[0]  # prepend 'TF' for tensorflow model
														lowerCAmelCase__					:    List[str]												=					__import__('''transformers'''		,fromlist=[model_class]			)
														lowerCAmelCase__					:    Union[str, Any]												=					getattr(__lowerCamelCase		,__lowerCamelCase			)
														lowerCAmelCase__					:    Union[str, Any]												=					model_cls(__lowerCamelCase			)
										except ImportError:
														raise ImportError(
														    f"""{model_class} does not exist. If you just want to test the pretrained model, you might want to"""
														    ''' set `--only_pretrain_model` or `args.only_pretrain_model=True`.'''			)
						else:
										lowerCAmelCase__					:    Dict												=					TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](__lowerCamelCase			)

						# encoder-decoder has vocab size saved differently
						lowerCAmelCase__					:    Tuple												=					config.vocab_size if hasattr(__lowerCamelCase		,'''vocab_size'''			) else config.encoder.vocab_size
						lowerCAmelCase__					:    Optional[int]												=					random_input_ids(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)

						@run_with_tf_optimizations(self.args.eager_mode		,self.args.use_xla			)
						def encoder_decoder_train():
										lowerCAmelCase__					:    Optional[Any]												=					model(__lowerCamelCase		,decoder_input_ids=__lowerCamelCase		,labels=__lowerCamelCase		,training=__lowerCamelCase			)[0]
										lowerCAmelCase__					:    int												=					tf.gradients(__lowerCamelCase		,model.trainable_variables			)
										return gradients

						@run_with_tf_optimizations(self.args.eager_mode		,self.args.use_xla			)
						def encoder_train():
										lowerCAmelCase__					:    Dict												=					model(__lowerCamelCase		,labels=__lowerCamelCase		,training=__lowerCamelCase			)[0]
										lowerCAmelCase__					:    List[str]												=					tf.gradients(__lowerCamelCase		,model.trainable_variables			)
										return gradients

						lowerCAmelCase__					:    Optional[int]												=					encoder_decoder_train if config.is_encoder_decoder else encoder_train

						return _train



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      float:

						"""simple docstring"""






						with self.args.strategy.scope():
										try:
														if self.args.is_tpu or self.args.use_xla:
																		# run additional 10 times to stabilize compilation for tpu
																		logger.info('''Do inference on TPU. Running model 5 times to stabilize compilation'''			)
																		timeit.repeat(__lowerCamelCase		,repeat=1		,number=5			)

														# as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average
														lowerCAmelCase__					:    str												=					timeit.repeat(
														    __lowerCamelCase		,repeat=self.args.repeat		,number=10		,)

														return min(__lowerCamelCase			) / 10.0
										except ResourceExhaustedError as e:
														self.print_fn(f"""Doesn't fit on GPU. {e}"""			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      [Memory, MemorySummary]:

						"""simple docstring"""






						logger.info(
						    '''Note that TensorFlow allocates more memory than '''
						    '''it might need to speed up computation. '''
						    '''The memory reported here corresponds to the memory '''
						    '''reported by `nvidia-smi`, which can vary depending '''
						    '''on total available memory on the GPU that is used.'''			)
						with self.args.strategy.scope():
										try:
														if self.args.trace_memory_line_by_line:
																		if not self.args.eager_mode:
																						raise ValueError(
																						    '''`args.eager_mode` is set to `False`. Make sure to run model in eager mode to measure memory'''
																						    ''' consumption line by line.'''			)
																		lowerCAmelCase__					:    Tuple												=					start_memory_tracing('''transformers'''			)

														if self.args.is_tpu:
																		# tpu
																		raise NotImplementedError(
																		    '''Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking'''
																		    ''' with `args.memory=False`'''			)
														elif self.args.is_gpu:
																		# gpu
																		if not is_pyanvml_available():
																						logger.warning(
																						    '''py3nvml not installed, we won\'t log GPU memory usage. '''
																						    '''Install py3nvml (pip install py3nvml) to log information about GPU.'''			)
																						lowerCAmelCase__					:    Any												=					'''N/A'''
																		else:
																						logger.info(
																						    '''Measuring total GPU usage on GPU device. Make sure to not have additional processes'''
																						    ''' running on the same GPU.'''			)
																						# init nvml
																						nvml.nvmlInit()
																						func()
																						lowerCAmelCase__					:    List[str]												=					nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx			)
																						lowerCAmelCase__					:    str												=					nvml.nvmlDeviceGetMemoryInfo(__lowerCamelCase			)
																						lowerCAmelCase__					:    Union[str, Any]												=					meminfo.used
																						lowerCAmelCase__					:    int												=					Memory(__lowerCamelCase			)
																						# shutdown nvml
																						nvml.nvmlShutdown()
														else:
																		# cpu
																		if self.args.trace_memory_line_by_line:
																						logger.info(
																						    '''When enabling line by line tracing, the max peak memory for CPU is inaccurate in'''
																						    ''' TensorFlow.'''			)
																						lowerCAmelCase__					:    Dict												=					None
																		else:
																						lowerCAmelCase__					:    int												=					measure_peak_memory_cpu(__lowerCamelCase			)
																						lowerCAmelCase__					:    int												=					Memory(__lowerCamelCase			) if isinstance(__lowerCamelCase		,__lowerCamelCase			) else memory_bytes
														if self.args.trace_memory_line_by_line:
																		lowerCAmelCase__					:    Optional[int]												=					stop_memory_tracing(__lowerCamelCase			)
																		if memory is None:
																						lowerCAmelCase__					:    Tuple												=					summary.total
														else:
																		lowerCAmelCase__					:    Tuple												=					None

														return memory, summary
										except ResourceExhaustedError as e:
														self.print_fn(f"""Doesn't fit on GPU. {e}"""			)
														return "N/A", None






import string

import numpy






def      lowerCAmelCase__		(   lowerCamelCase_		:		int	,lowerCamelCase_		:		int):





				'''simple docstring'''

				return b if a == 0 else greatest_common_divisor(b % a	,lowerCamelCase_)



class 	lowerCamelCase__      :




		'''simple docstring'''



		snake_case_     =string.ascii_uppercase + string.digits
		# This cipher takes alphanumerics into account
		# i.e. a total of 36 characters

		# take x and return x % len(key_string)
		snake_case_     =numpy.vectorize(lambda lowerCamelCase__: x % 36)

		snake_case_     =numpy.vectorize(lowerCamelCase__)



		def __init__(self		,__lowerCamelCase			)					->      None:

						"""simple docstring"""






						lowerCAmelCase__					:    Tuple												=					self.modulus(__lowerCamelCase			)  # mod36 calc's on the encrypt key
						self.check_determinant()  # validate the determinant of the encryption key
						lowerCAmelCase__					:    Optional[int]												=					encrypt_key.shape[0]



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      int:

						"""simple docstring"""






						return self.key_string.index(__lowerCamelCase			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      str:

						"""simple docstring"""






						return self.key_string[round(__lowerCamelCase			)]



		def 		lowerCAmelCase__				(self			)					->      None:

						"""simple docstring"""






						lowerCAmelCase__					:    Tuple												=					round(numpy.linalg.det(self.encrypt_key			)			)

						if det < 0:
										lowerCAmelCase__					:    str												=					det % len(self.key_string			)

						lowerCAmelCase__					:    Optional[Any]												=					len(self.key_string			)
						if greatest_common_divisor(__lowerCamelCase		,len(self.key_string			)			) != 1:
										lowerCAmelCase__					:    List[str]												=					(
										    f"""determinant modular {req_l} of encryption key({det}) """
										    f"""is not co prime w.r.t {req_l}.\nTry another key."""
										)
										raise ValueError(__lowerCamelCase			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      str:

						"""simple docstring"""






						lowerCAmelCase__					:    Dict												=					[char for char in text.upper() if char in self.key_string]

						lowerCAmelCase__					:    int												=					chars[-1]
						while len(__lowerCamelCase			) % self.break_key != 0:
										chars.append(__lowerCamelCase			)

						return "".join(__lowerCamelCase			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      str:

						"""simple docstring"""






						lowerCAmelCase__					:    List[str]												=					self.process_text(text.upper()			)
						lowerCAmelCase__					:    str												=					''''''

						for i in range(0		,len(__lowerCamelCase			) - self.break_key + 1		,self.break_key			):
										lowerCAmelCase__					:    Any												=					text[i : i + self.break_key]
										lowerCAmelCase__					:    Dict												=					[self.replace_letters(__lowerCamelCase			) for char in batch]
										lowerCAmelCase__					:    int												=					numpy.array([vec]			).T
										lowerCAmelCase__					:    Union[str, Any]												=					self.modulus(self.encrypt_key.dot(__lowerCamelCase			)			).T.tolist()[
										    0
										]
										lowerCAmelCase__					:    Union[str, Any]												=					''''''.join(
										    self.replace_digits(__lowerCamelCase			) for num in batch_encrypted			)
										encrypted += encrypted_batch

						return encrypted



		def 		lowerCAmelCase__				(self			)					->      numpy.ndarray:

						"""simple docstring"""






						lowerCAmelCase__					:    Tuple												=					round(numpy.linalg.det(self.encrypt_key			)			)

						if det < 0:
										lowerCAmelCase__					:    int												=					det % len(self.key_string			)
						lowerCAmelCase__					:    Dict												=					None
						for i in range(len(self.key_string			)			):
										if (det * i) % len(self.key_string			) == 1:
														lowerCAmelCase__					:    Optional[Any]												=					i
														break

						lowerCAmelCase__					:    Optional[Any]												=					(
						    det_inv
						    * numpy.linalg.det(self.encrypt_key			)
						    * numpy.linalg.inv(self.encrypt_key			)
						)

						return self.to_int(self.modulus(__lowerCamelCase			)			)



		def 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      str:

						"""simple docstring"""






						lowerCAmelCase__					:    str												=					self.make_decrypt_key()
						lowerCAmelCase__					:    List[str]												=					self.process_text(text.upper()			)
						lowerCAmelCase__					:    Optional[Any]												=					''''''

						for i in range(0		,len(__lowerCamelCase			) - self.break_key + 1		,self.break_key			):
										lowerCAmelCase__					:    List[Any]												=					text[i : i + self.break_key]
										lowerCAmelCase__					:    Tuple												=					[self.replace_letters(__lowerCamelCase			) for char in batch]
										lowerCAmelCase__					:    Optional[Any]												=					numpy.array([vec]			).T
										lowerCAmelCase__					:    Tuple												=					self.modulus(decrypt_key.dot(__lowerCamelCase			)			).T.tolist()[0]
										lowerCAmelCase__					:    Optional[int]												=					''''''.join(
										    self.replace_digits(__lowerCamelCase			) for num in batch_decrypted			)
										decrypted += decrypted_batch

						return decrypted






def      lowerCAmelCase__		(   ):





				'''simple docstring'''

				lowerCAmelCase__					:    Any												=					int(input('''Enter the order of the encryption key: '''))
				lowerCAmelCase__					:    Union[str, Any]												=					[]

				print('''Enter each row of the encryption key with space separated integers''')
				for _ in range(lowerCamelCase_):
								lowerCAmelCase__					:    int												=					[int(lowerCamelCase_) for x in input().split()]
								hill_matrix.append(lowerCamelCase_)

				lowerCAmelCase__					:    List[str]												=					HillCipher(numpy.array(lowerCamelCase_))

				print('''Would you like to encrypt or decrypt some text? (1 or 2)''')
				lowerCAmelCase__					:    List[Any]												=					input('''\n1. Encrypt\n2. Decrypt\n''')
				if option == "1":
								lowerCAmelCase__					:    Optional[int]												=					input('''What text would you like to encrypt?: ''')
								print('''Your encrypted text is:''')
								print(hc.encrypt(lowerCamelCase_))
				elif option == "2":
								lowerCAmelCase__					:    Dict												=					input('''What text would you like to decrypt?: ''')
								print('''Your decrypted text is:''')
								print(hc.decrypt(lowerCamelCase_))


if __name__ == "__main__":
							import doctest

							doctest.testmod()

							main()

