






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




							def __init__(   self     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Dict=13 ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=7 ,							SCREAMING_SNAKE_CASE_     :   str=True ,							SCREAMING_SNAKE_CASE_     :   List[str]=True ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=True ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=True ,							SCREAMING_SNAKE_CASE_     :   Tuple=99 ,							SCREAMING_SNAKE_CASE_     :   Any=32 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=5 ,							SCREAMING_SNAKE_CASE_     :   str=4 ,							SCREAMING_SNAKE_CASE_     :   int=4 ,							SCREAMING_SNAKE_CASE_     :   List[Any]="gelu" ,							SCREAMING_SNAKE_CASE_     :   Dict=0.0 ,							SCREAMING_SNAKE_CASE_     :   str=0.1 ,							SCREAMING_SNAKE_CASE_     :   int=True ,							SCREAMING_SNAKE_CASE_     :   str=512 ,							SCREAMING_SNAKE_CASE_     :   Dict=16 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=2 ,							SCREAMING_SNAKE_CASE_     :   str=0.02 ,							SCREAMING_SNAKE_CASE_     :   str=3 ,							SCREAMING_SNAKE_CASE_     :   Any=4 ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=None ,							):
											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 						__snake_case					(   self     :   Tuple	):
											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 						__snake_case					(   self     :   Optional[int]	):
											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 						__snake_case					(   self     :   Dict	):
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						self.prepare_config_and_inputs()

											lowerCAmelCase__      			=						True

											return config, input_ids, input_mask, token_labels




							def 						__snake_case					(   self     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Any ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any]	):
											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 						__snake_case					(   self     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   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 						__snake_case					(   self     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[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 						__snake_case					(   self     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]	):
											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 						__snake_case					(   self     :   str	):
											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  lowerCAmelCase_       (				__lowerCAmelCase     ,    __lowerCAmelCase     ,    unittest.TestCase					):
							UpperCamelCase_		:str		      =    (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
							UpperCamelCase_		:str		      =    (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
							UpperCamelCase_		:Dict		      =    (
							    {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM}
							    if is_torch_available()
							    else {}
							)
							UpperCamelCase_		:Any		      =    False
							UpperCamelCase_		:List[Any]		      =    False
							UpperCamelCase_		:Union[str, Any]		      =    False
							UpperCamelCase_		:str		      =    False




							def 						__snake_case					(   self     :   Any	):
											lowerCAmelCase__      			=						GPTNeoXJapaneseModelTester(self	)
											lowerCAmelCase__      			=						ConfigTester(self ,							config_class=lowerCAmelCase_ ,							hidden_size=37	)




							def 						__snake_case					(   self     :   List[Any]	):
											self.config_tester.run_common_tests()




							def 						__snake_case					(   self     :   Optional[Any]	):
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(lowerCAmelCase_ ,							lowerCAmelCase_ ,							lowerCAmelCase_	)




							def 						__snake_case					(   self     :   int	):
											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 						__snake_case					(   self     :   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 						__snake_case					(   self     :   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 						__snake_case					(   self     :   List[str]	):
											lowerCAmelCase__      			=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_causal_lm(*lowerCAmelCase_	)




							@slow
							def 						__snake_case					(   self     :   Optional[int]	):
											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_	)













from __future__ import annotations





def 	lowerCAmelCase_		(lowercase__						:       list[int]       )		->    bool:


				'''simple docstring'''




				return len(set(lowercase__       )       ) == len(lowercase__       )


if __name__ == "__main__":
			import doctest

			doctest.testmod()





