




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_							:


 '''simple docstring'''







 def __init__(							self  :		str				,				_lowerCAmelCase  :		Tuple				,				_lowerCAmelCase  :		Any=13				,				_lowerCAmelCase  :		str=7				,				_lowerCAmelCase  :		Tuple=True				,				_lowerCAmelCase  :		str=True				,				_lowerCAmelCase  :		Tuple=True				,				_lowerCAmelCase  :		Tuple=True				,				_lowerCAmelCase  :		Union[str, Any]=99				,				_lowerCAmelCase  :		Tuple=32				,				_lowerCAmelCase  :		Optional[int]=5				,				_lowerCAmelCase  :		str=4				,				_lowerCAmelCase  :		Optional[Any]=4				,				_lowerCAmelCase  :		List[str]="gelu"				,				_lowerCAmelCase  :		Optional[int]=0.0				,				_lowerCAmelCase  :		List[str]=0.1				,				_lowerCAmelCase  :		Dict=True				,				_lowerCAmelCase  :		List[str]=512				,				_lowerCAmelCase  :		Optional[int]=16				,				_lowerCAmelCase  :		List[str]=2				,				_lowerCAmelCase  :		Union[str, Any]=0.02				,				_lowerCAmelCase  :		int=3				,				_lowerCAmelCase  :		Tuple=4				,				_lowerCAmelCase  :		Any=None				,				):
       SCREAMING_SNAKE_CASE_							=			parent
       SCREAMING_SNAKE_CASE_							=			batch_size
       SCREAMING_SNAKE_CASE_							=			seq_length
       SCREAMING_SNAKE_CASE_							=			is_training
       SCREAMING_SNAKE_CASE_							=			use_input_mask
       SCREAMING_SNAKE_CASE_							=			use_token_type_ids
       SCREAMING_SNAKE_CASE_							=			use_labels
       SCREAMING_SNAKE_CASE_							=			vocab_size
       SCREAMING_SNAKE_CASE_							=			hidden_size
       SCREAMING_SNAKE_CASE_							=			num_hidden_layers
       SCREAMING_SNAKE_CASE_							=			num_attention_heads
       SCREAMING_SNAKE_CASE_							=			intermediate_multiple_size
       SCREAMING_SNAKE_CASE_							=			hidden_act
       SCREAMING_SNAKE_CASE_							=			hidden_dropout
       SCREAMING_SNAKE_CASE_							=			attention_dropout
       SCREAMING_SNAKE_CASE_							=			weight_tying
       SCREAMING_SNAKE_CASE_							=			max_position_embeddings
       SCREAMING_SNAKE_CASE_							=			type_vocab_size
       SCREAMING_SNAKE_CASE_							=			type_sequence_label_size
       SCREAMING_SNAKE_CASE_							=			initializer_range
       SCREAMING_SNAKE_CASE_							=			num_labels
       SCREAMING_SNAKE_CASE_							=			num_choices
       SCREAMING_SNAKE_CASE_							=			scope



 def       lowerCAmelCase_  (							self  :		str						):
       SCREAMING_SNAKE_CASE_							=			ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size						)

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

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

       SCREAMING_SNAKE_CASE_							=			self.get_config()

       return config, input_ids, input_mask, token_labels



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

       SCREAMING_SNAKE_CASE_							=			True

       return config, input_ids, input_mask, token_labels



 def       lowerCAmelCase_  (							self  :		Union[str, Any]				,				_lowerCAmelCase  :		List[str]				,				_lowerCAmelCase  :		List[Any]				,				_lowerCAmelCase  :		str						):
       SCREAMING_SNAKE_CASE_							=			GPTNeoXJapaneseModel(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_							=			model(_lowerCAmelCase				,				attention_mask=_lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_							=			model(_lowerCAmelCase						)
       self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size)						)



 def       lowerCAmelCase_  (							self  :		Any				,				_lowerCAmelCase  :		List[str]				,				_lowerCAmelCase  :		int				,				_lowerCAmelCase  :		int						):
       SCREAMING_SNAKE_CASE_							=			True
       SCREAMING_SNAKE_CASE_							=			GPTNeoXJapaneseModel(_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_							=			model(_lowerCAmelCase				,				attention_mask=_lowerCAmelCase						)
       self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size)						)



 def       lowerCAmelCase_  (							self  :		Optional[int]				,				_lowerCAmelCase  :		Tuple				,				_lowerCAmelCase  :		Dict				,				_lowerCAmelCase  :		List[str]				,				_lowerCAmelCase  :		Union[str, Any]						):
       SCREAMING_SNAKE_CASE_							=			GPTNeoXJapaneseForCausalLM(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_							=			model(_lowerCAmelCase				,				attention_mask=_lowerCAmelCase				,				labels=_lowerCAmelCase						)
       self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.vocab_size)						)



 def       lowerCAmelCase_  (							self  :		Any				,				_lowerCAmelCase  :		Tuple				,				_lowerCAmelCase  :		Tuple				,				_lowerCAmelCase  :		List[str]						):
       SCREAMING_SNAKE_CASE_							=			True
       SCREAMING_SNAKE_CASE_							=			GPTNeoXJapaneseForCausalLM(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()

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

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

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

       SCREAMING_SNAKE_CASE_							=			model(_lowerCAmelCase				,				attention_mask=_lowerCAmelCase				,				output_hidden_states=_lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_							=			output_from_no_past['hidden_states'][0]
       SCREAMING_SNAKE_CASE_							=			model(
           _lowerCAmelCase				,				attention_mask=_lowerCAmelCase				,				past_key_values=_lowerCAmelCase				,				output_hidden_states=_lowerCAmelCase				,				)['hidden_states'][0]

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





@require_torch
class        lowerCamelCase_							(      _SCREAMING_SNAKE_CASE       ,   _SCREAMING_SNAKE_CASE       ,   unittest.TestCase					):


 '''simple docstring'''







 lowercase_				    =    (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
 lowercase_				    =    (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
 lowercase_				    =    (
     {"feature-extraction": GPTNeoXJapaneseModel, "text-generation": GPTNeoXJapaneseForCausalLM}
     if is_torch_available()
     else {}
 )
 lowercase_				    =    False
 lowercase_				    =    False
 lowercase_				    =    False
 lowercase_				    =    False



 def       lowerCAmelCase_  (							self  :		Tuple						):
       SCREAMING_SNAKE_CASE_							=			GPTNeoXJapaneseModelTester(self						)
       SCREAMING_SNAKE_CASE_							=			ConfigTester(self				,				config_class=_lowerCAmelCase				,				hidden_size=37						)



 def       lowerCAmelCase_  (							self  :		Optional[int]						):
       self.config_tester.run_common_tests()



 def       lowerCAmelCase_  (							self  :		int						):
       SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_							=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(_lowerCAmelCase				,				_lowerCAmelCase				,				_lowerCAmelCase						)



 def       lowerCAmelCase_  (							self  :		int						):
       SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_							=			self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_model_as_decoder(_lowerCAmelCase				,				_lowerCAmelCase				,				_lowerCAmelCase						)



 def       lowerCAmelCase_  (							self  :		Tuple						):
       # This regression test was failing with PyTorch < 1.3
       SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_							=			self.model_tester.prepare_config_and_inputs_for_decoder()

       SCREAMING_SNAKE_CASE_							=			None

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



 def       lowerCAmelCase_  (							self  :		Any						):
       SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_						, SCREAMING_SNAKE_CASE_							=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_decoder_model_past_large_inputs(_lowerCAmelCase				,				_lowerCAmelCase				,				_lowerCAmelCase						)



 def       lowerCAmelCase_  (							self  :		Optional[int]						):
       SCREAMING_SNAKE_CASE_							=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_causal_lm(*_lowerCAmelCase						)




 @slow
 def       lowerCAmelCase_  (							self  :		Any						):
       SCREAMING_SNAKE_CASE_							=			'abeja/gpt-neox-japanese-2.7b'

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

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

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

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





def 					UpperCAmelCase_							(      __UpperCAmelCase						:	int			,			__UpperCAmelCase						:	int							)					-> int:
      return number | (1 << position)

def 					UpperCAmelCase_							(      __UpperCAmelCase						:	int			,			__UpperCAmelCase						:	int							)					-> int:
      return number & ~(1 << position)

def 					UpperCAmelCase_							(      __UpperCAmelCase						:	int			,			__UpperCAmelCase						:	int							)					-> int:
      return number ^ (1 << position)

def 					UpperCAmelCase_							(      __UpperCAmelCase						:	int			,			__UpperCAmelCase						:	int							)					-> bool:
      return ((number >> position) & 1) == 1

def 					UpperCAmelCase_							(      __UpperCAmelCase						:	int			,			__UpperCAmelCase						:	int							)					-> int:
      return int((number & (1 << position)) != 0							)


if __name__ == "__main__":
   import doctest

   doctest.testmod()