






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

__UpperCamelCase   :   Union[str, Any]      =    logging.get_logger(__name__)





def 					snake_case	(  lowerCamelCase					,	lowerCamelCase       ):




	'''simple docstring'''







	def run_func(lowerCamelCase       ):
		@wraps(lowerCamelCase       )
		def run_in_eager_mode(*lowerCamelCase					,	**lowerCamelCase       ):
			return func(*lowerCamelCase					,	**lowerCamelCase       )

		@wraps(lowerCamelCase       )
		@tf.function(experimental_compile=lowerCamelCase       )
		def run_in_graph_mode(*lowerCamelCase					,	**lowerCamelCase       ):
			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 					snake_case	(  lowerCamelCase					,	lowerCamelCase					,	lowerCamelCase       ):




	'''simple docstring'''







	__lowercase   							=	random.Random()
	__lowercase   							=	[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 							__UpperCamelCase     (  _lowerCAmelCase ):
	__snake_case						:TensorFlowBenchmarkArguments
	__snake_case						:PretrainedConfig
	__snake_case						:str =							"TensorFlow"







	@property
	def 			_a (	self   :				str      )		->			Any:
		"""simple docstring"""







		return tf.__version__







	def 			_a (	self   :				List[str]							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				int      )		->			float:
		"""simple docstring"""







		__lowercase   							=	self.args.strategy
		if strategy is None:
			raise ValueError("""A device strategy has to be initialized before using TensorFlow."""      )
		__lowercase   							=	self._prepare_inference_func(_lowerCAmelCase							,					_lowerCAmelCase							,					_lowerCAmelCase      )
		return self._measure_speed(_inference      )







	def 			_a (	self   :				Union[str, Any]							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				int      )		->			float:
		"""simple docstring"""







		__lowercase   							=	self.args.strategy
		if strategy is None:
			raise ValueError("""A device strategy has to be initialized before using TensorFlow."""      )
		__lowercase   							=	self._prepare_train_func(_lowerCAmelCase							,					_lowerCAmelCase							,					_lowerCAmelCase      )
		return self._measure_speed(_train      )







	def 			_a (	self   :				Dict							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				int      )		->			[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      )
		__lowercase   							=	self.args.strategy
		if strategy is None:
			raise ValueError("""A device strategy has to be initialized before using TensorFlow."""      )
		__lowercase   							=	self._prepare_inference_func(_lowerCAmelCase							,					_lowerCAmelCase							,					_lowerCAmelCase      )
		return self._measure_memory(_inference      )







	def 			_a (	self   :				Optional[int]							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				int      )		->			[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      )
		__lowercase   							=	self.args.strategy
		if strategy is None:
			raise ValueError("""A device strategy has to be initialized before using TensorFlow."""      )

		__lowercase   							=	self._prepare_train_func(_lowerCAmelCase							,					_lowerCAmelCase							,					_lowerCAmelCase      )
		return self._measure_memory(_train      )







	def 			_a (	self   :				Tuple							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				int      )		->			Callable[[], None]:
		"""simple docstring"""







		__lowercase   							=	self.config_dict[model_name]

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

		__lowercase   							=	(
		    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:
				__lowercase   							=	"""TF""" + config.architectures[0]  # prepend 'TF' for tensorflow model
				__lowercase   							=	__import__("""transformers"""							,					fromlist=[model_class]      )
				__lowercase   							=	getattr(_lowerCAmelCase							,					_lowerCAmelCase      )
				__lowercase   							=	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:
			__lowercase   							=	TF_MODEL_MAPPING[config.__class__](_lowerCAmelCase      )

		# encoder-decoder has vocab size saved differently
		__lowercase   							=	config.vocab_size if hasattr(_lowerCAmelCase							,					"""vocab_size"""      ) else config.encoder.vocab_size
		__lowercase   							=	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      )

		__lowercase   							=	encoder_decoder_forward if config.is_encoder_decoder else encoder_forward

		return _inference







	def 			_a (	self   :				Union[str, Any]							,					_lowerCAmelCase   :				str							,					_lowerCAmelCase   :				int							,					_lowerCAmelCase   :				int      )		->			Callable[[], None]:
		"""simple docstring"""







		__lowercase   							=	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."""      )

		__lowercase   							=	(
		    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:
				__lowercase   							=	"""TF""" + config.architectures[0]  # prepend 'TF' for tensorflow model
				__lowercase   							=	__import__("""transformers"""							,					fromlist=[model_class]      )
				__lowercase   							=	getattr(_lowerCAmelCase							,					_lowerCAmelCase      )
				__lowercase   							=	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:
			__lowercase   							=	TF_MODEL_WITH_LM_HEAD_MAPPING[config.__class__](_lowerCAmelCase      )

		# encoder-decoder has vocab size saved differently
		__lowercase   							=	config.vocab_size if hasattr(_lowerCAmelCase							,					"""vocab_size"""      ) else config.encoder.vocab_size
		__lowercase   							=	random_input_ids(_lowerCAmelCase							,					_lowerCAmelCase							,					_lowerCAmelCase      )

		@run_with_tf_optimizations(self.args.eager_mode							,					self.args.use_xla      )
		def encoder_decoder_train():
			__lowercase   							=	model(_lowerCAmelCase							,					decoder_input_ids=_lowerCAmelCase							,					labels=_lowerCAmelCase							,					training=_lowerCAmelCase      )[0]
			__lowercase   							=	tf.gradients(_lowerCAmelCase							,					model.trainable_variables      )
			return gradients

		@run_with_tf_optimizations(self.args.eager_mode							,					self.args.use_xla      )
		def encoder_train():
			__lowercase   							=	model(_lowerCAmelCase							,					labels=_lowerCAmelCase							,					training=_lowerCAmelCase      )[0]
			__lowercase   							=	tf.gradients(_lowerCAmelCase							,					model.trainable_variables      )
			return gradients

		__lowercase   							=	encoder_decoder_train if config.is_encoder_decoder else encoder_train

		return _train







	def 			_a (	self   :				Dict							,					_lowerCAmelCase   :				Any      )		->			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
				__lowercase   							=	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 			_a (	self   :				str							,					_lowerCAmelCase   :				Callable[[], None]      )		->			[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."""      )
					__lowercase   							=	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."""      )
						__lowercase   							=	"""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()
						__lowercase   							=	nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx      )
						__lowercase   							=	nvml.nvmlDeviceGetMemoryInfo(_lowerCAmelCase      )
						__lowercase   							=	meminfo.used
						__lowercase   							=	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."""      )
						__lowercase   							=	None
					else:
						__lowercase   							=	measure_peak_memory_cpu(_lowerCAmelCase      )
						__lowercase   							=	Memory(_lowerCAmelCase      ) if isinstance(_lowerCAmelCase							,					_lowerCAmelCase      ) else memory_bytes
				if self.args.trace_memory_line_by_line:
					__lowercase   							=	stop_memory_tracing(_lowerCAmelCase      )
					if memory is None:
						__lowercase   							=	summary.total
				else:
					__lowercase   							=	None

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




import argparse
import struct
import unittest







class 					lowercase							:




				def __init__(   self ,  snake_case			):
											snake_case_     							=       data

											# Initialize hash values
											snake_case_     							=       [
											    0x6A09E667,
											    0xBB67AE85,
											    0x3C6EF372,
											    0xA54FF53A,
											    0x510E527F,
											    0x9B05688C,
											    0x1F83D9AB,
											    0x5BE0CD19,
											]

											# Initialize round constants
											snake_case_     							=       [
											    0x428A2F98,
											    0x71374491,
											    0xB5C0FBCF,
											    0xE9B5DBA5,
											    0x3956C25B,
											    0x59F111F1,
											    0x923F82A4,
											    0xAB1C5ED5,
											    0xD807AA98,
											    0x12835B01,
											    0x243185BE,
											    0x550C7DC3,
											    0x72BE5D74,
											    0x80DEB1FE,
											    0x9BDC06A7,
											    0xC19BF174,
											    0xE49B69C1,
											    0xEFBE4786,
											    0x0FC19DC6,
											    0x240CA1CC,
											    0x2DE92C6F,
											    0x4A7484AA,
											    0x5CB0A9DC,
											    0x76F988DA,
											    0x983E5152,
											    0xA831C66D,
											    0xB00327C8,
											    0xBF597FC7,
											    0xC6E00BF3,
											    0xD5A79147,
											    0x06CA6351,
											    0x14292967,
											    0x27B70A85,
											    0x2E1B2138,
											    0x4D2C6DFC,
											    0x53380D13,
											    0x650A7354,
											    0x766A0ABB,
											    0x81C2C92E,
											    0x92722C85,
											    0xA2BFE8A1,
											    0xA81A664B,
											    0xC24B8B70,
											    0xC76C51A3,
											    0xD192E819,
											    0xD6990624,
											    0xF40E3585,
											    0x106AA070,
											    0x19A4C116,
											    0x1E376C08,
											    0x2748774C,
											    0x34B0BCB5,
											    0x391C0CB3,
											    0x4ED8AA4A,
											    0x5B9CCA4F,
											    0x682E6FF3,
											    0x748F82EE,
											    0x78A5636F,
											    0x84C87814,
											    0x8CC70208,
											    0x90BEFFFA,
											    0xA4506CEB,
											    0xBEF9A3F7,
											    0xC67178F2,
											]

											snake_case_     							=       self.preprocessing(self.data			)
											self.final_hash()




				@staticmethod
				def        a				(   snake_case			):
											snake_case_     							=       b'\x80' + (b'\x00' * (63 - (len(snake_case			) + 8) % 64))
											snake_case_     							=       struct.pack('>Q' ,  (len(snake_case			) * 8)			)
											return data + padding + big_endian_integer




				def        a				(   self			):
											# Convert into blocks of 64 bytes
											snake_case_     							=       [
											    self.preprocessed_data[x : x + 64]
											    for x in range(0 ,  len(self.preprocessed_data			) ,  64			)
											]

											for block in self.blocks:
																		# Convert the given block into a list of 4 byte integers
																		snake_case_     							=       list(struct.unpack('>16L' ,  snake_case			)			)
																		# add 48 0-ed integers
																		words += [0] * 48

																		snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_     							=       self.hashes

																		for index in range(0 ,  64			):
																									if index > 15:
																																# modify the zero-ed indexes at the end of the array
																																snake_case_     							=       (
																																    self.ror(words[index - 15] ,  7			)
																																    ^ self.ror(words[index - 15] ,  18			)
																																    ^ (words[index - 15] >> 3)
																																)
																																snake_case_     							=       (
																																    self.ror(words[index - 2] ,  17			)
																																    ^ self.ror(words[index - 2] ,  19			)
																																    ^ (words[index - 2] >> 10)
																																)

																																snake_case_     							=       (
																																    words[index - 16] + sa + words[index - 7] + sa
																																) % 0x100000000

																									# Compression
																									snake_case_     							=       self.ror(snake_case ,  6			) ^ self.ror(snake_case ,  11			) ^ self.ror(snake_case ,  25			)
																									snake_case_     							=       (e & f) ^ ((~e & 0xFFFFFFFF) & g)
																									snake_case_     							=       (
																									    h + sa + ch + self.round_constants[index] + words[index]
																									) % 0x100000000
																									snake_case_     							=       self.ror(snake_case ,  2			) ^ self.ror(snake_case ,  13			) ^ self.ror(snake_case ,  22			)
																									snake_case_     							=       (a & b) ^ (a & c) ^ (b & c)
																									snake_case_     							=       (sa + maj) % 0x100000000

																									snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_     							=       (
																									    g,
																									    f,
																									    e,
																									    ((d + tempa) % 0x100000000),
																									    c,
																									    b,
																									    a,
																									    ((tempa + tempa) % 0x100000000),
																									)

																		snake_case_     							=       [a, b, c, d, e, f, g, h]

																		# Modify final values
																		snake_case_     							=       [
																		    ((element + mutated_hash_values[index]) % 0x100000000)
																		    for index, element in enumerate(self.hashes			)
																		]

											snake_case_     							=       ''.join([hex(snake_case			)[2:].zfill(8			) for value in self.hashes]			)




				def        a				(   self ,  snake_case ,  snake_case			):
											return 0xFFFFFFFF & (value << (32 - rotations)) | (value >> rotations)







class 					lowercase							(			unittest.TestCase     ):




				def        a				(   self			):
											import hashlib

											snake_case_     							=       bytes('Test String' ,  'utf-8'			)
											self.assertEqual(SHAaaa(snake_case			).hash ,  hashlib.shaaaa(snake_case			).hexdigest()			)





def    __lowerCamelCase      (					):







							'''simple docstring'''






							import doctest

							doctest.testmod()

							snake_case_     							=       argparse.ArgumentParser()
							parser.add_argument(
							    '-s'  ,				'--string'  ,				dest='input_string'  ,				default='Hello World!! Welcome to Cryptography'  ,				help='Hash the string'  ,				)
							parser.add_argument(
							    '-f'  ,				'--file'  ,				dest='input_file'  ,				help='Hash contents of a file' )

							snake_case_     							=       parser.parse_args()

							snake_case_     							=       args.input_string

							# hash input should be a bytestring
							if args.input_file:
														with open(args.input_file  ,				'rb' ) as f:
																					snake_case_     							=       f.read()
							else:
														snake_case_     							=       bytes(UpperCamelCase__  ,				'utf-8' )

							print(SHAaaa(UpperCamelCase__ ).hash )


if __name__ == "__main__":
					main()




