


'''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 	a__       :





							'''simple docstring'''






							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_=5_12      ,					lowerCamelCase_=16      ,					lowerCamelCase_=2      ,					lowerCamelCase_=0.02      ,					lowerCamelCase_=3      ,					lowerCamelCase_=4      ,					lowerCamelCase_=None      ,					) ->							Dict:
														lowerCAmelCase__      =			parent
														lowerCAmelCase__      =			batch_size
														lowerCAmelCase__      =			seq_length
														lowerCAmelCase__      =			is_training
														lowerCAmelCase__      =			use_input_mask
														lowerCAmelCase__      =			use_token_type_ids
														lowerCAmelCase__      =			use_labels
														lowerCAmelCase__      =			vocab_size
														lowerCAmelCase__      =			hidden_size
														lowerCAmelCase__      =			num_hidden_layers
														lowerCAmelCase__      =			num_attention_heads
														lowerCAmelCase__      =			intermediate_multiple_size
														lowerCAmelCase__      =			hidden_act
														lowerCAmelCase__      =			hidden_dropout
														lowerCAmelCase__      =			attention_dropout
														lowerCAmelCase__      =			weight_tying
														lowerCAmelCase__      =			max_position_embeddings
														lowerCAmelCase__      =			type_vocab_size
														lowerCAmelCase__      =			type_sequence_label_size
														lowerCAmelCase__      =			initializer_range
														lowerCAmelCase__      =			num_labels
														lowerCAmelCase__      =			num_choices
														lowerCAmelCase__      =			scope







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														lowerCAmelCase__      =			ids_tensor([self.batch_size, self.seq_length]      ,					self.vocab_size     )

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

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

														lowerCAmelCase__      =			self.get_config()

														return config, input_ids, input_mask, token_labels







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			self.prepare_config_and_inputs()

														lowerCAmelCase__      =			True

														return config, input_ids, input_mask, token_labels







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							str:
														lowerCAmelCase__      =			GPTNeoXJapaneseModel(config=lowerCamelCase_     )
														model.to(lowerCamelCase_     )
														model.eval()
														lowerCAmelCase__      =			model(lowerCamelCase_      ,					attention_mask=lowerCamelCase_     )
														lowerCAmelCase__      =			model(lowerCamelCase_     )
														self.parent.assertEqual(result.last_hidden_state.shape      ,					(self.batch_size, self.seq_length, self.hidden_size)     )







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							int:
														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			GPTNeoXJapaneseModel(lowerCamelCase_     )
														model.to(lowerCamelCase_     )
														model.eval()
														lowerCAmelCase__      =			model(lowerCamelCase_      ,					attention_mask=lowerCamelCase_     )
														self.parent.assertEqual(result.last_hidden_state.shape      ,					(self.batch_size, self.seq_length, self.hidden_size)     )







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							Any:
														lowerCAmelCase__      =			GPTNeoXJapaneseForCausalLM(config=lowerCamelCase_     )
														model.to(lowerCamelCase_     )
														model.eval()
														lowerCAmelCase__      =			model(lowerCamelCase_      ,					attention_mask=lowerCamelCase_      ,					labels=lowerCamelCase_     )
														self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.seq_length, self.vocab_size)     )







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							List[str]:
														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			GPTNeoXJapaneseForCausalLM(config=lowerCamelCase_     )
														model.to(lowerCamelCase_     )
														model.eval()

														# first forward pass
														lowerCAmelCase__      =			model(lowerCamelCase_      ,					attention_mask=lowerCamelCase_      ,					use_cache=lowerCamelCase_     )
														lowerCAmelCase__      =			outputs.past_key_values

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

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

														lowerCAmelCase__      =			model(lowerCamelCase_      ,					attention_mask=lowerCamelCase_      ,					output_hidden_states=lowerCamelCase_     )
														lowerCAmelCase__      =			output_from_no_past['''hidden_states'''][0]
														lowerCAmelCase__      =			model(
														    lowerCamelCase_      ,					attention_mask=lowerCamelCase_      ,					past_key_values=lowerCamelCase_      ,					output_hidden_states=lowerCamelCase_      ,					)['''hidden_states'''][0]

														# select random slice
														lowerCAmelCase__      =			ids_tensor((1,)      ,					output_from_past.shape[-1]     ).item()
														lowerCAmelCase__      =			output_from_no_past[:, -3:, random_slice_idx].detach()
														lowerCAmelCase__      =			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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														lowerCAmelCase__      =			self.prepare_config_and_inputs()
														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			config_and_inputs
														lowerCAmelCase__      =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
														return config, inputs_dict




@require_torch
class 	a__       (							a__       ,       a__       ,       unittest.TestCase						):





							'''simple docstring'''






							lowercase__    :     Optional[Any]					=   (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
							lowercase__    :     int					=   (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
							lowercase__    :     Tuple					=   (
							    {"feature-extraction": GPTNeoXJapaneseModel, "text-generation": GPTNeoXJapaneseForCausalLM}
							    if is_torch_available()
							    else {}
							)
							lowercase__    :     int					=   False
							lowercase__    :     Union[str, Any]					=   False
							lowercase__    :     Union[str, Any]					=   False
							lowercase__    :     str					=   False







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														lowerCAmelCase__      =			GPTNeoXJapaneseModelTester(self     )
														lowerCAmelCase__      =			ConfigTester(self      ,					config_class=lowerCamelCase_      ,					hidden_size=37     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							str:
														self.config_tester.run_common_tests()







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs_for_decoder()
														self.model_tester.create_and_check_model_as_decoder(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														# This regression test was failing with PyTorch < 1.3
														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs_for_decoder()

														lowerCAmelCase__      =			None

														self.model_tester.create_and_check_model_as_decoder(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_decoder_model_past_large_inputs(lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_causal_lm(*lowerCamelCase_     )







							@slow
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							str:
														lowerCAmelCase__      =			'''abeja/gpt-neox-japanese-2.7b'''

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

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

														lowerCAmelCase__      =			GPTNeoXJapaneseTokenizer.from_pretrained(lowerCamelCase_     )
														lowerCAmelCase__      =			GPTNeoXJapaneseForCausalLM.from_pretrained(lowerCamelCase_     )

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



'''simple docstring'''






from __future__ import annotations




class 	a__       :





							'''simple docstring'''






							def __init__(	self      ,					lowerCamelCase_ = 0     ) ->							List[Any]:
														lowerCAmelCase__      =			key







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							list[str]:
														assert isinstance(lowerCamelCase_      ,					lowerCamelCase_     ) and isinstance(lowerCamelCase_      ,					lowerCamelCase_     )

														lowerCAmelCase__      =			key or self.__key or 1

														# make sure key is an appropriate size
														key %= 2_55

														return [chr(ord(lowerCamelCase_     ) ^ key     ) for ch in content]







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							list[str]:
														assert isinstance(lowerCamelCase_      ,					lowerCamelCase_     ) and isinstance(lowerCamelCase_      ,					lowerCamelCase_     )

														lowerCAmelCase__      =			key or self.__key or 1

														# make sure key is an appropriate size
														key %= 2_55

														return [chr(ord(lowerCamelCase_     ) ^ key     ) for ch in content]







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_ = 0     ) ->							str:
														assert isinstance(lowerCamelCase_      ,					lowerCamelCase_     ) and isinstance(lowerCamelCase_      ,					lowerCamelCase_     )

														lowerCAmelCase__      =			key or self.__key or 1

														# make sure key can be any size
														while key > 2_55:
																					key -= 2_55

														# This will be returned
														lowerCAmelCase__      =			''''''

														for ch in content:
																					ans += chr(ord(lowerCamelCase_     ) ^ key     )

														return ans







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_ = 0     ) ->							str:
														assert isinstance(lowerCamelCase_      ,					lowerCamelCase_     ) and isinstance(lowerCamelCase_      ,					lowerCamelCase_     )

														lowerCAmelCase__      =			key or self.__key or 1

														# make sure key can be any size
														while key > 2_55:
																					key -= 2_55

														# This will be returned
														lowerCAmelCase__      =			''''''

														for ch in content:
																					ans += chr(ord(lowerCamelCase_     ) ^ key     )

														return ans







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_ = 0     ) ->							bool:
														assert isinstance(lowerCamelCase_      ,					lowerCamelCase_     ) and isinstance(lowerCamelCase_      ,					lowerCamelCase_     )

														try:
																					with open(lowerCamelCase_     ) as fin, open('''encrypt.out'''      ,					'''w+'''     ) as fout:
																												# actual encrypt-process
																												for line in fin:
																																			fout.write(self.encrypt_string(lowerCamelCase_      ,					lowerCamelCase_     )     )

														except OSError:
																					return False

														return True







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							bool:
														assert isinstance(lowerCamelCase_      ,					lowerCamelCase_     ) and isinstance(lowerCamelCase_      ,					lowerCamelCase_     )

														try:
																					with open(lowerCamelCase_     ) as fin, open('''decrypt.out'''      ,					'''w+'''     ) as fout:
																												# actual encrypt-process
																												for line in fin:
																																			fout.write(self.decrypt_string(lowerCamelCase_      ,					lowerCamelCase_     )     )

														except OSError:
																					return False

														return True


# Tests
# crypt = XORCipher()
# key = 67

# # test encrypt
# print(crypt.encrypt("hallo welt",key))
# # test decrypt
# print(crypt.decrypt(crypt.encrypt("hallo welt",key), key))

# # test encrypt_string
# print(crypt.encrypt_string("hallo welt",key))

# # test decrypt_string
# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key))

# if (crypt.encrypt_file("test.txt",key)):
#       print("encrypt successful")
# else:
#       print("encrypt unsuccessful")

# if (crypt.decrypt_file("encrypt.out",key)):
#       print("decrypt successful")
# else:
#       print("decrypt unsuccessful")