



'''simple docstring'''


import unittest

from transformers import GPTNeoXJapaneseConfig, is_torch_available
from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer
from transformers.testing_utils import require_torch, slow, torch_device

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 GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel




class      __UpperCamelCase       :







	def __init__(    self ,				_lowerCAmelCase ,				_lowerCAmelCase=13 ,				_lowerCAmelCase=7 ,				_lowerCAmelCase=True ,				_lowerCAmelCase=True ,				_lowerCAmelCase=True ,				_lowerCAmelCase=True ,				_lowerCAmelCase=99 ,				_lowerCAmelCase=32 ,				_lowerCAmelCase=5 ,				_lowerCAmelCase=4 ,				_lowerCAmelCase=4 ,				_lowerCAmelCase="gelu" ,				_lowerCAmelCase=0.0 ,				_lowerCAmelCase=0.1 ,				_lowerCAmelCase=True ,				_lowerCAmelCase=512 ,				_lowerCAmelCase=16 ,				_lowerCAmelCase=2 ,				_lowerCAmelCase=0.02 ,				_lowerCAmelCase=3 ,				_lowerCAmelCase=4 ,				_lowerCAmelCase=None ,				)    ->    List[str]:
							'''simple docstring'''






							lowercase        =							parent
							lowercase        =							batch_size
							lowercase        =							seq_length
							lowercase        =							is_training
							lowercase        =							use_input_mask
							lowercase        =							use_token_type_ids
							lowercase        =							use_labels
							lowercase        =							vocab_size
							lowercase        =							hidden_size
							lowercase        =							num_hidden_layers
							lowercase        =							num_attention_heads
							lowercase        =							intermediate_multiple_size
							lowercase        =							hidden_act
							lowercase        =							hidden_dropout
							lowercase        =							attention_dropout
							lowercase        =							weight_tying
							lowercase        =							max_position_embeddings
							lowercase        =							type_vocab_size
							lowercase        =							type_sequence_label_size
							lowercase        =							initializer_range
							lowercase        =							num_labels
							lowercase        =							num_choices
							lowercase        =							scope







	def   _a   (    self						)    ->    Union[str, Any]:
							'''simple docstring'''






							lowercase        =							ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size						)

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

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

							lowercase        =							self.get_config()

							return config, input_ids, input_mask, token_labels







	def   _a   (    self						)    ->    List[Any]:
							'''simple docstring'''






							return GPTNeoXJapaneseConfig(
							    vocab_size=self.vocab_size ,				hidden_size=self.hidden_size ,				num_hidden_layers=self.num_hidden_layers ,				num_attention_heads=self.num_attention_heads ,				intermediate_multiple_size=self.intermediate_multiple_size ,				hidden_act=self.hidden_act ,				hidden_dropout=self.hidden_dropout ,				attention_dropout=self.attention_dropout ,				weight_tying=self.weight_tying ,				max_position_embeddings=self.max_position_embeddings ,				type_vocab_size=self.type_vocab_size ,				is_decoder=_lowerCAmelCase ,				initializer_range=self.initializer_range ,				)







	def   _a   (    self						)    ->    Tuple:
							'''simple docstring'''






							lowercase     ,   lowercase     ,   lowercase     ,   lowercase        =							self.prepare_config_and_inputs()

							lowercase        =							True

							return config, input_ids, input_mask, token_labels







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    List[Any]:
							'''simple docstring'''






							lowercase        =							GPTNeoXJapaneseModel(config=_lowerCAmelCase						)
							model.to(_lowerCAmelCase						)
							model.eval()
							lowercase        =							model(_lowerCAmelCase ,				attention_mask=_lowerCAmelCase						)
							lowercase        =							model(_lowerCAmelCase						)
							self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size)						)







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    str:
							'''simple docstring'''






							lowercase        =							True
							lowercase        =							GPTNeoXJapaneseModel(_lowerCAmelCase						)
							model.to(_lowerCAmelCase						)
							model.eval()
							lowercase        =							model(_lowerCAmelCase ,				attention_mask=_lowerCAmelCase						)
							self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size)						)







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






							lowercase        =							GPTNeoXJapaneseForCausalLM(config=_lowerCAmelCase						)
							model.to(_lowerCAmelCase						)
							model.eval()
							lowercase        =							model(_lowerCAmelCase ,				attention_mask=_lowerCAmelCase ,				labels=_lowerCAmelCase						)
							self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.vocab_size)						)







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    int:
							'''simple docstring'''






							lowercase        =							True
							lowercase        =							GPTNeoXJapaneseForCausalLM(config=_lowerCAmelCase						)
							model.to(_lowerCAmelCase						)
							model.eval()

							# first forward pass
							lowercase        =							model(_lowerCAmelCase ,				attention_mask=_lowerCAmelCase ,				use_cache=_lowerCAmelCase						)
							lowercase        =							outputs.past_key_values

							# create hypothetical multiple next token and extent to next_input_ids
							lowercase        =							ids_tensor((self.batch_size, 3) ,				config.vocab_size						)
							lowercase        =							ids_tensor((self.batch_size, 3) ,				vocab_size=2						)

							# append to next input_ids and
							lowercase        =							torch.cat([input_ids, next_tokens] ,				dim=-1						)
							lowercase        =							torch.cat([input_mask, next_mask] ,				dim=-1						)

							lowercase        =							model(_lowerCAmelCase ,				attention_mask=_lowerCAmelCase ,				output_hidden_states=_lowerCAmelCase						)
							lowercase        =							output_from_no_past["""hidden_states"""][0]
							lowercase        =							model(
							    _lowerCAmelCase ,				attention_mask=_lowerCAmelCase ,				past_key_values=_lowerCAmelCase ,				output_hidden_states=_lowerCAmelCase ,				)["""hidden_states"""][0]

							# select random slice
							lowercase        =							ids_tensor((1,) ,				output_from_past.shape[-1]						).item()
							lowercase        =							output_from_no_past[:, -3:, random_slice_idx].detach()
							lowercase        =							output_from_past[:, :, random_slice_idx].detach()

							self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]						)

							# test that outputs are equal for slice
							self.parent.assertTrue(torch.allclose(_lowerCAmelCase ,				_lowerCAmelCase ,				atol=1E-3						)						)







	def   _a   (    self						)    ->    Optional[Any]:
							'''simple docstring'''






							lowercase        =							self.prepare_config_and_inputs()
							lowercase     ,   lowercase     ,   lowercase     ,   lowercase        =							config_and_inputs
							lowercase        =							{"""input_ids""": input_ids, """attention_mask""": input_mask}
							return config, inputs_dict




@require_torch
class      __UpperCamelCase       (_UpperCAmelCase			,   _UpperCAmelCase			,   unittest.TestCase			):
	__A			  =					(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
	__A			  =					(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
	__A			  =					(
	    {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM}
	    if is_torch_available()
	    else {}
	)
	__A			  =					False
	__A			  =					False
	__A			  =					False
	__A			  =					False







	def   _a   (    self						)    ->    Optional[int]:
							'''simple docstring'''






							lowercase        =							GPTNeoXJapaneseModelTester(self						)
							lowercase        =							ConfigTester(self ,				config_class=_lowerCAmelCase ,				hidden_size=37						)







	def   _a   (    self						)    ->    int:
							'''simple docstring'''






							self.config_tester.run_common_tests()







	def   _a   (    self						)    ->    int:
							'''simple docstring'''






							lowercase     ,   lowercase     ,   lowercase     ,   lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)







	def   _a   (    self						)    ->    List[str]:
							'''simple docstring'''






							lowercase     ,   lowercase     ,   lowercase     ,   lowercase        =							self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)







	def   _a   (    self						)    ->    Dict:
							'''simple docstring'''






							lowercase     ,   lowercase     ,   lowercase     ,   lowercase        =							self.model_tester.prepare_config_and_inputs_for_decoder()

							lowercase        =							None

							self.model_tester.create_and_check_model_as_decoder(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)







	def   _a   (    self						)    ->    Dict:
							'''simple docstring'''






							lowercase     ,   lowercase     ,   lowercase     ,   lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past_large_inputs(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)







	def   _a   (    self						)    ->    List[str]:
							'''simple docstring'''






							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_causal_lm(*_lowerCAmelCase						)







	@slow
	def   _a   (    self						)    ->    Dict:
							'''simple docstring'''






							lowercase        =							"""abeja/gpt-neox-japanese-2.7b"""

							lowercase        =							["""データサイエンティストとは、""", """100年後に必要とされる会社は、""", """フルリモートの環境で働くために必要なことは、""", """国境の長いトンネルを抜けると""", """美味しい日本食といえば、"""]

							lowercase        =							[
							    """データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。""",
							    """100年後に必要とされる会社は、「人」が中心の会社です。""",
							    """フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。""",
							    """国境の長いトンネルを抜けると、そこは雪国だった。""",
							    """美味しい日本食といえば、やっぱりお寿司ですよね。""",
							]

							lowercase        =							GPTNeoXJapaneseTokenizer.from_pretrained(_lowerCAmelCase						)
							lowercase        =							GPTNeoXJapaneseForCausalLM.from_pretrained(_lowerCAmelCase						)

							lowercase        =							[]
							for prompt in prompts:
													lowercase        =							tokenizer(_lowerCAmelCase ,				return_tensors="""pt"""						).input_ids
													lowercase        =							model.generate(_lowerCAmelCase ,				max_length=50						)
													lowercase        =							tokenizer.batch_decode(_lowerCAmelCase ,				skip_special_tokens=_lowerCAmelCase						)
													predicted_outputs += generated_string
							self.assertListEqual(_lowerCAmelCase ,				_lowerCAmelCase						)







'''simple docstring'''


def      SCREAMING_SNAKE_CASE				(				lowercase_    :      Union[str, Any] ,  lowercase_    :      str						):
						lowercase        =							""""""
						for i in table:
												res += inp[i - 1]
						return res







def      SCREAMING_SNAKE_CASE				(				lowercase_    :      List[Any]						):
						return data[1:] + data[0]







def      SCREAMING_SNAKE_CASE				(				lowercase_    :      Tuple ,  lowercase_    :      Dict						):
						lowercase        =							""""""
						for i in range(len(lowercase_						)						):
												if a[i] == b[i]:
																		res += "0"
												else:
																		res += "1"
						return res







def      SCREAMING_SNAKE_CASE				(				lowercase_    :      str ,  lowercase_    :      str						):
						lowercase        =							int("""0b""" + data[0] + data[-1] ,  2						)
						lowercase        =							int("""0b""" + data[1:3] ,  2						)
						return bin(s[row][col]						)[2:]







def      SCREAMING_SNAKE_CASE				(				lowercase_    :      Dict ,  lowercase_    :      Tuple ,  lowercase_    :      Tuple ,  lowercase_    :      str ,  lowercase_    :      Any						):
						lowercase        =							message[:4]
						lowercase        =							message[4:]
						lowercase        =							apply_table(lowercase_ ,  lowercase_						)
						lowercase        =							xor(lowercase_ ,  lowercase_						)
						lowercase        =							apply_sbox(lowercase_ ,  temp[:4]						)  # noqa: E741
						lowercase        =							apply_sbox(lowercase_ ,  temp[4:]						)
						lowercase        =							"""0""" * (2 - len(lowercase_						)) + l  # noqa: E741
						lowercase        =							"""0""" * (2 - len(lowercase_						)) + r
						lowercase        =							apply_table(l + r ,  lowercase_						)
						lowercase        =							xor(lowercase_ ,  lowercase_						)
						return temp + right


if __name__ == "__main__":
						lowercase_      :				Tuple   					=    input('''Enter 10 bit key: ''')
						lowercase_      :				Any   					=    input('''Enter 8 bit message: ''')

						lowercase_      :				Dict   					=    [6, 3, 7, 4, 8, 5, 10, 9]
						lowercase_      :				str   					=    [3, 5, 2, 7, 4, 10, 1, 9, 8, 6]
						lowercase_      :				List[Any]   					=    [2, 4, 3, 1]
						lowercase_      :				List[str]   					=    [2, 6, 3, 1, 4, 8, 5, 7]
						lowercase_      :				Tuple   					=    [4, 1, 3, 5, 7, 2, 8, 6]
						lowercase_      :				Optional[Any]   					=    [4, 1, 2, 3, 2, 3, 4, 1]
						lowercase_      :				List[str]   					=    [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]]
						lowercase_      :				List[Any]   					=    [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]]

						# key generation
						lowercase_      :				Union[str, Any]   					=    apply_table(key, paa_table)
						lowercase_      :				Optional[Any]   					=    temp[:5]
						lowercase_      :				int   					=    temp[5:]
						lowercase_      :				List[str]   					=    left_shift(left)
						lowercase_      :				int   					=    left_shift(right)
						lowercase_      :				Tuple   					=    apply_table(left + right, pa_table)
						lowercase_      :				List[str]   					=    left_shift(left)
						lowercase_      :				Optional[Any]   					=    left_shift(right)
						lowercase_      :				Union[str, Any]   					=    left_shift(left)
						lowercase_      :				Union[str, Any]   					=    left_shift(right)
						lowercase_      :				Optional[int]   					=    apply_table(left + right, pa_table)

						# encryption
						lowercase_      :				int   					=    apply_table(message, IP)
						lowercase_      :				Dict   					=    function(expansion, sa, sa, keya, temp)
						lowercase_      :				Any   					=    temp[4:] + temp[:4]
						lowercase_      :				List[Any]   					=    function(expansion, sa, sa, keya, temp)
						lowercase_      :				Tuple   					=    apply_table(temp, IP_inv)
						print('''Cipher text is:''', CT)

						# decryption
						lowercase_      :				List[str]   					=    apply_table(CT, IP)
						lowercase_      :				Optional[int]   					=    function(expansion, sa, sa, keya, temp)
						lowercase_      :				Optional[Any]   					=    temp[4:] + temp[:4]
						lowercase_      :				Optional[int]   					=    function(expansion, sa, sa, keya, temp)
						lowercase_      :				Optional[Any]   					=    apply_table(temp, IP_inv)
						print('''Plain text after decypting is:''', PT)


