






import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
       CTRLForSequenceClassification,
       CTRLLMHeadModel,
       CTRLModel,
   )



class 				_A			:
    """simple docstring"""

    def __init__(				self      :     Union[str, Any] ,     __SCREAMING_SNAKE_CASE      :     Tuple ,     __SCREAMING_SNAKE_CASE      :     Union[str, Any]=14 ,     __SCREAMING_SNAKE_CASE      :     Union[str, Any]=7 ,     __SCREAMING_SNAKE_CASE      :     List[str]=True ,     __SCREAMING_SNAKE_CASE      :     str=True ,     __SCREAMING_SNAKE_CASE      :     List[str]=True ,     __SCREAMING_SNAKE_CASE      :     Optional[int]=True ,     __SCREAMING_SNAKE_CASE      :     Optional[int]=True ,     __SCREAMING_SNAKE_CASE      :     Tuple=99 ,     __SCREAMING_SNAKE_CASE      :     Optional[int]=32 ,     __SCREAMING_SNAKE_CASE      :     Any=5 ,     __SCREAMING_SNAKE_CASE      :     Dict=4 ,     __SCREAMING_SNAKE_CASE      :     List[Any]=37 ,     __SCREAMING_SNAKE_CASE      :     Optional[int]="gelu" ,     __SCREAMING_SNAKE_CASE      :     Tuple=0.1 ,     __SCREAMING_SNAKE_CASE      :     Optional[Any]=0.1 ,     __SCREAMING_SNAKE_CASE      :     Any=512 ,     __SCREAMING_SNAKE_CASE      :     str=16 ,     __SCREAMING_SNAKE_CASE      :     List[str]=2 ,     __SCREAMING_SNAKE_CASE      :     Tuple=0.02 ,     __SCREAMING_SNAKE_CASE      :     Dict=3 ,     __SCREAMING_SNAKE_CASE      :     List[str]=4 ,     __SCREAMING_SNAKE_CASE      :     Union[str, Any]=None ,     )      ->							Optional[Any]:
           __UpperCAmelCase     				=parent
           __UpperCAmelCase     				=batch_size
           __UpperCAmelCase     				=seq_length
           __UpperCAmelCase     				=is_training
           __UpperCAmelCase     				=use_token_type_ids
           __UpperCAmelCase     				=use_input_mask
           __UpperCAmelCase     				=use_labels
           __UpperCAmelCase     				=use_mc_token_ids
           __UpperCAmelCase     				=vocab_size
           __UpperCAmelCase     				=hidden_size
           __UpperCAmelCase     				=num_hidden_layers
           __UpperCAmelCase     				=num_attention_heads
           __UpperCAmelCase     				=intermediate_size
           __UpperCAmelCase     				=hidden_act
           __UpperCAmelCase     				=hidden_dropout_prob
           __UpperCAmelCase     				=attention_probs_dropout_prob
           __UpperCAmelCase     				=max_position_embeddings
           __UpperCAmelCase     				=type_vocab_size
           __UpperCAmelCase     				=type_sequence_label_size
           __UpperCAmelCase     				=initializer_range
           __UpperCAmelCase     				=num_labels
           __UpperCAmelCase     				=num_choices
           __UpperCAmelCase     				=scope
           __UpperCAmelCase     				=self.vocab_size - 1



    def _a							(				self      :     Union[str, Any]      )      ->							Any:
           __UpperCAmelCase     				=ids_tensor([self.batch_size, self.seq_length] ,     self.vocab_size      )

           __UpperCAmelCase     				=None
           if self.use_input_mask:
                  __UpperCAmelCase     				=random_attention_mask([self.batch_size, self.seq_length]      )

           __UpperCAmelCase     				=None
           if self.use_token_type_ids:
                  __UpperCAmelCase     				=ids_tensor([self.batch_size, self.seq_length] ,     self.type_vocab_size      )

           __UpperCAmelCase     				=None
           if self.use_mc_token_ids:
                  __UpperCAmelCase     				=ids_tensor([self.batch_size, self.num_choices] ,     self.seq_length      )

           __UpperCAmelCase     				=None
           __UpperCAmelCase     				=None
           __UpperCAmelCase     				=None
           if self.use_labels:
                  __UpperCAmelCase     				=ids_tensor([self.batch_size] ,     self.type_sequence_label_size      )
                  __UpperCAmelCase     				=ids_tensor([self.batch_size, self.seq_length] ,     self.num_labels      )
                  __UpperCAmelCase     				=ids_tensor([self.batch_size] ,     self.num_choices      )

           __UpperCAmelCase     				=self.get_config()

           __UpperCAmelCase     				=ids_tensor([self.num_hidden_layers, self.num_attention_heads] ,     2      )

           return (
               config,
               input_ids,
               input_mask,
               head_mask,
               token_type_ids,
               mc_token_ids,
               sequence_labels,
               token_labels,
               choice_labels,
           )



    def _a							(				self      :     Optional[Any]      )      ->							List[str]:
           return CTRLConfig(
               vocab_size=self.vocab_size ,     n_embd=self.hidden_size ,     n_layer=self.num_hidden_layers ,     n_head=self.num_attention_heads ,     n_positions=self.max_position_embeddings ,     pad_token_id=self.pad_token_id ,     )



    def _a							(				self      :     Optional[int] ,     __SCREAMING_SNAKE_CASE      :     Tuple ,     __SCREAMING_SNAKE_CASE      :     Union[str, Any] ,     __SCREAMING_SNAKE_CASE      :     Any ,     __SCREAMING_SNAKE_CASE      :     Dict ,     __SCREAMING_SNAKE_CASE      :     Optional[int] ,     *__SCREAMING_SNAKE_CASE      :     Tuple      )      ->							Optional[int]:
           __UpperCAmelCase     				=CTRLModel(config=__SCREAMING_SNAKE_CASE      )
           model.to(__SCREAMING_SNAKE_CASE      )
           model.eval()

           model(__SCREAMING_SNAKE_CASE ,     token_type_ids=__SCREAMING_SNAKE_CASE ,     head_mask=__SCREAMING_SNAKE_CASE      )
           model(__SCREAMING_SNAKE_CASE ,     token_type_ids=__SCREAMING_SNAKE_CASE      )
           __UpperCAmelCase     				=model(__SCREAMING_SNAKE_CASE      )
           self.parent.assertEqual(result.last_hidden_state.shape ,     (self.batch_size, self.seq_length, self.hidden_size)      )
           self.parent.assertEqual(len(result.past_key_values      ) ,     config.n_layer      )



    def _a							(				self      :     Optional[int] ,     __SCREAMING_SNAKE_CASE      :     List[str] ,     __SCREAMING_SNAKE_CASE      :     Optional[Any] ,     __SCREAMING_SNAKE_CASE      :     Tuple ,     __SCREAMING_SNAKE_CASE      :     Dict ,     __SCREAMING_SNAKE_CASE      :     Optional[int] ,     *__SCREAMING_SNAKE_CASE      :     Dict      )      ->							Dict:
           __UpperCAmelCase     				=CTRLLMHeadModel(__SCREAMING_SNAKE_CASE      )
           model.to(__SCREAMING_SNAKE_CASE      )
           model.eval()

           __UpperCAmelCase     				=model(__SCREAMING_SNAKE_CASE ,     token_type_ids=__SCREAMING_SNAKE_CASE ,     labels=__SCREAMING_SNAKE_CASE      )
           self.parent.assertEqual(result.loss.shape ,     ()      )
           self.parent.assertEqual(result.logits.shape ,     (self.batch_size, self.seq_length, self.vocab_size)      )



    def _a							(				self      :     Any      )      ->							Optional[int]:
           __UpperCAmelCase     				=self.prepare_config_and_inputs()

           (
               __UpperCAmelCase			
           )     				=config_and_inputs

           __UpperCAmelCase     				={"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask}

           return config, inputs_dict



    def _a							(				self      :     Dict ,     __SCREAMING_SNAKE_CASE      :     int ,     __SCREAMING_SNAKE_CASE      :     Union[str, Any] ,     __SCREAMING_SNAKE_CASE      :     int ,     __SCREAMING_SNAKE_CASE      :     str ,     *__SCREAMING_SNAKE_CASE      :     str      )      ->							Union[str, Any]:
           __UpperCAmelCase     				=self.num_labels
           __UpperCAmelCase     				=CTRLForSequenceClassification(__SCREAMING_SNAKE_CASE      )
           model.to(__SCREAMING_SNAKE_CASE      )
           model.eval()
           __UpperCAmelCase     				=ids_tensor([self.batch_size] ,     self.type_sequence_label_size      )
           __UpperCAmelCase     				=model(__SCREAMING_SNAKE_CASE ,     token_type_ids=__SCREAMING_SNAKE_CASE ,     labels=__SCREAMING_SNAKE_CASE      )
           self.parent.assertEqual(result.logits.shape ,     (self.batch_size, self.num_labels)      )




@require_torch
class 				_A			(     _UpperCamelCase					,					_UpperCamelCase					,					_UpperCamelCase					,					unittest.TestCase				):
    """simple docstring"""

    lowerCamelCase	:							Dict				= (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
    lowerCamelCase	:							Optional[int]				= (CTRLLMHeadModel,) if is_torch_available() else ()
    lowerCamelCase	:							Union[str, Any]				= (
        {
            'feature-extraction': CTRLModel,
            'text-classification': CTRLForSequenceClassification,
            'text-generation': CTRLLMHeadModel,
            'zero-shot': CTRLForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    lowerCamelCase	:							List[Any]				= True
    lowerCamelCase	:							Optional[int]				= False
    lowerCamelCase	:							Tuple				= False



    def _a							(				self      :     List[Any] ,     __SCREAMING_SNAKE_CASE      :     Optional[Any] ,     __SCREAMING_SNAKE_CASE      :     Any ,     __SCREAMING_SNAKE_CASE      :     Optional[int] ,     __SCREAMING_SNAKE_CASE      :     Any ,     __SCREAMING_SNAKE_CASE      :     List[str]      )      ->							int:







           if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
                  # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
                  # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
                  # config could not be created.
                  return True

           return False



    def _a							(				self      :     List[str]      )      ->							int:
           __UpperCAmelCase     				=CTRLModelTester(self      )
           __UpperCAmelCase     				=ConfigTester(self ,     config_class=__SCREAMING_SNAKE_CASE ,     n_embd=37      )



    def _a							(				self      :     List[Any]      )      ->							List[Any]:
           super().tearDown()
           # clean-up as much as possible GPU memory occupied by PyTorch
           gc.collect()
           torch.cuda.empty_cache()



    def _a							(				self      :     Tuple      )      ->							Union[str, Any]:
           self.config_tester.run_common_tests()



    def _a							(				self      :     int      )      ->							Tuple:
           __UpperCAmelCase     				=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_ctrl_model(*__SCREAMING_SNAKE_CASE      )



    def _a							(				self      :     Optional[int]      )      ->							Dict:
           __UpperCAmelCase     				=self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_lm_head_model(*__SCREAMING_SNAKE_CASE      )



    @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""      )
    def _a							(				self      :     Optional[Any]      )      ->							Union[str, Any]:
           pass



    @slow
    def _a							(				self      :     str      )      ->							Union[str, Any]:







           for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  __UpperCAmelCase     				=CTRLModel.from_pretrained(__SCREAMING_SNAKE_CASE      )
                  self.assertIsNotNone(__SCREAMING_SNAKE_CASE      )



    @unittest.skip("""The model doesn't support left padding"""      )  # and it's not used enough to be worth fixing :)
    def _a							(				self      :     Tuple      )      ->							Union[str, Any]:
           pass




@require_torch
class 				_A			(     unittest.TestCase				):
    """simple docstring"""

    def _a							(				self      :     Optional[Any]      )      ->							Union[str, Any]:
           super().tearDown()
           # clean-up as much as possible GPU memory occupied by PyTorch
           gc.collect()
           torch.cuda.empty_cache()



    @slow
    def _a							(				self      :     List[Any]      )      ->							int:
           __UpperCAmelCase     				=CTRLLMHeadModel.from_pretrained("""ctrl"""      )
           model.to(__SCREAMING_SNAKE_CASE      )
           __UpperCAmelCase     				=torch.tensor(
               [[11859, 0, 1611, 8]] ,     dtype=torch.long ,     device=__SCREAMING_SNAKE_CASE      )  # Legal the president is
           __UpperCAmelCase     				=[
               11859,
               0,
               1611,
               8,
               5,
               150,
               26449,
               2,
               19,
               348,
               469,
               3,
               2595,
               48,
               20740,
               246533,
               246533,
               19,
               30,
               5,
           ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

           __UpperCAmelCase     				=model.generate(__SCREAMING_SNAKE_CASE ,     do_sample=__SCREAMING_SNAKE_CASE      )
           self.assertListEqual(output_ids[0].tolist() ,     __SCREAMING_SNAKE_CASE      )










import argparse
import hashlib  # hashlib is only used inside the Test class
import struct


class    SCREAMING_SNAKE_CASE_				:



	"""simple docstring"""


	def __init__(					self		:						Tuple					,     lowerCAmelCase		:						Tuple	)	->       Dict:







						"""simple docstring"""







						__UpperCamelCase					:	Any             =      data
						__UpperCamelCase					:	List[str]             =      [0x6_7_4_5_2_3_0_1, 0xe_f_c_d_a_b_8_9, 0x9_8_b_a_d_c_f_e, 0x1_0_3_2_5_4_7_6, 0xc_3_d_2_e_1_f_0]






	@staticmethod
	def 					lowerCamelCase__     (					lowerCAmelCase		:						str					,     lowerCAmelCase		:						Union[str, Any]	)	->       str:







						"""simple docstring"""







						return ((n << b) | (n >> (32 - b))) & 0xf_f_f_f_f_f_f_f






	def 					lowerCamelCase__     (					self		:						Optional[int]	)	->       Dict:







						"""simple docstring"""







						__UpperCamelCase					:	int             =      b"""\x80""" + b"""\x00""" * (63 - (len(self.data	) + 8) % 64)
						__UpperCamelCase					:	int             =      self.data + padding + struct.pack(""">Q"""					,     8 * len(self.data	)	)
						return padded_data






	def 					lowerCamelCase__     (					self		:						str	)	->       str:







						"""simple docstring"""







						return [
						    self.padded_data[i : i + 64] for i in range(0					,     len(self.padded_data	)					,     64	)
						]






	def 					lowerCamelCase__     (					self		:						Union[str, Any]					,     lowerCAmelCase		:						Tuple	)	->       List[Any]:







						"""simple docstring"""







						__UpperCamelCase					:	Tuple             =      list(struct.unpack(""">16L"""					,     lowerCAmelCase	)	) + [0] * 64
						for i in range(16					,     80	):
											__UpperCamelCase					:	Dict             =      self.rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16])					,     1	)
						return w






	def 					lowerCamelCase__     (					self		:						Union[str, Any]	)	->       Dict:







						"""simple docstring"""







						__UpperCamelCase					:	Dict             =      self.padding()
						__UpperCamelCase					:	int             =      self.split_blocks()
						for block in self.blocks:
											__UpperCamelCase					:	Union[str, Any]             =      self.expand_block(lowerCAmelCase	)
											__UpperCamelCase      ,			__UpperCamelCase      ,			__UpperCamelCase      ,			__UpperCamelCase      ,			__UpperCamelCase					:	str             =      self.h
											for i in range(0					,     80	):
																if 0 <= i < 20:
																					__UpperCamelCase					:	List[Any]             =      (b & c) | ((~b) & d)
																					__UpperCamelCase					:	str             =      0x5_a_8_2_7_9_9_9
																elif 20 <= i < 40:
																					__UpperCamelCase					:	Tuple             =      b ^ c ^ d
																					__UpperCamelCase					:	List[Any]             =      0x6_e_d_9_e_b_a_1
																elif 40 <= i < 60:
																					__UpperCamelCase					:	Tuple             =      (b & c) | (b & d) | (c & d)
																					__UpperCamelCase					:	List[str]             =      0x8_f_1_b_b_c_d_c
																elif 60 <= i < 80:
																					__UpperCamelCase					:	Optional[int]             =      b ^ c ^ d
																					__UpperCamelCase					:	Tuple             =      0xc_a_6_2_c_1_d_6
																__UpperCamelCase      ,			__UpperCamelCase      ,			__UpperCamelCase      ,			__UpperCamelCase      ,			__UpperCamelCase					:	Tuple             =      (
																    self.rotate(lowerCAmelCase					,     5	) + f + e + k + expanded_block[i] & 0xf_f_f_f_f_f_f_f,
																    a,
																    self.rotate(lowerCAmelCase					,     30	),
																    c,
																    d,
																)
											__UpperCamelCase					:	List[Any]             =      (
											    self.h[0] + a & 0xf_f_f_f_f_f_f_f,
											    self.h[1] + b & 0xf_f_f_f_f_f_f_f,
											    self.h[2] + c & 0xf_f_f_f_f_f_f_f,
											    self.h[3] + d & 0xf_f_f_f_f_f_f_f,
											    self.h[4] + e & 0xf_f_f_f_f_f_f_f,
											)
						return ("{:08x}" * 5).format(*self.h	)






def 				A__ ()     ->						int:
					__UpperCamelCase					:	Optional[int]             =      b"""Test String"""
					assert SHAaHash(snake_case				).final_hash() == hashlib.shaa(snake_case				).hexdigest()  # noqa: S324





def 				A__ ()     ->						Tuple:
					__UpperCamelCase					:	Optional[Any]             =      argparse.ArgumentParser(description="""Process some strings or files"""				)
					parser.add_argument(
					    """--string"""		,	dest="""input_string"""		,	default="""Hello World!! Welcome to Cryptography"""		,	help="""Hash the string"""		,	)
					parser.add_argument("""--file"""		,	dest="""input_file"""		,	help="""Hash contents of a file"""				)
					__UpperCamelCase					:	List[Any]             =      parser.parse_args()
					__UpperCamelCase					:	List[str]             =      args.input_string
					# In any case hash input should be a bytestring
					if args.input_file:
										with open(args.input_file		,	"""rb"""				) as f:
															__UpperCamelCase					:	Tuple             =      f.read()
					else:
										__UpperCamelCase					:	Any             =      bytes(snake_case		,	"""utf-8"""				)
					print(SHAaHash(snake_case				).final_hash()				)


if __name__ == "__main__":
	main()
	import doctest

	doctest.testmod()






