





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    ,						__lowerCamelCase    ,						__lowerCamelCase=1_3    ,						__lowerCamelCase=7    ,						__lowerCamelCase=True    ,						__lowerCamelCase=True    ,						__lowerCamelCase=True    ,						__lowerCamelCase=True    ,						__lowerCamelCase=9_9    ,						__lowerCamelCase=3_2    ,						__lowerCamelCase=5    ,						__lowerCamelCase=4    ,						__lowerCamelCase=4    ,						__lowerCamelCase="gelu"    ,						__lowerCamelCase=0.0    ,						__lowerCamelCase=0.1    ,						__lowerCamelCase=True    ,						__lowerCamelCase=5_1_2    ,						__lowerCamelCase=1_6    ,						__lowerCamelCase=2    ,						__lowerCamelCase=0.02    ,						__lowerCamelCase=3    ,						__lowerCamelCase=4    ,						__lowerCamelCase=None    ,						)			->     Tuple:
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							parent
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							batch_size
            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							seq_length
            _SCREAMING_SNAKE_CASE       :	int						    =							is_training
            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							use_input_mask
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							use_token_type_ids
            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							use_labels
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							vocab_size
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							hidden_size
            _SCREAMING_SNAKE_CASE       :	str						    =							num_hidden_layers
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							num_attention_heads
            _SCREAMING_SNAKE_CASE       :	Any						    =							intermediate_multiple_size
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							hidden_act
            _SCREAMING_SNAKE_CASE       :	Any						    =							hidden_dropout
            _SCREAMING_SNAKE_CASE       :	Dict						    =							attention_dropout
            _SCREAMING_SNAKE_CASE       :	Dict						    =							weight_tying
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							max_position_embeddings
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							type_vocab_size
            _SCREAMING_SNAKE_CASE       :	Any						    =							type_sequence_label_size
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							initializer_range
            _SCREAMING_SNAKE_CASE       :	Dict						    =							num_labels
            _SCREAMING_SNAKE_CASE       :	int						    =							num_choices
            _SCREAMING_SNAKE_CASE       :	str						    =							scope




       def     UpperCamelCase_			(      self							)			->     int:
            _SCREAMING_SNAKE_CASE       :	Tuple						    =							ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size							)

            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							None
            if self.use_input_mask:
                 _SCREAMING_SNAKE_CASE       :	int						    =							random_attention_mask([self.batch_size, self.seq_length]							)

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

            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							self.get_config()

            return config, input_ids, input_mask, token_labels




       def     UpperCamelCase_			(      self							)			->     Optional[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     UpperCamelCase_			(      self							)			->     str:
            _SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE       :	int						    =							self.prepare_config_and_inputs()

            _SCREAMING_SNAKE_CASE       :	Tuple						    =							True

            return config, input_ids, input_mask, token_labels




       def     UpperCamelCase_			(      self    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase							)			->     Any:
            _SCREAMING_SNAKE_CASE       :	Dict						    =							GPTNeoXJapaneseModel(config=__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()
            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							model(__lowerCamelCase    ,						attention_mask=__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							model(__lowerCamelCase							)
            self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.hidden_size)							)




       def     UpperCamelCase_			(      self    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase							)			->     Optional[int]:
            _SCREAMING_SNAKE_CASE       :	int						    =							True
            _SCREAMING_SNAKE_CASE       :	str						    =							GPTNeoXJapaneseModel(__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							model(__lowerCamelCase    ,						attention_mask=__lowerCamelCase							)
            self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.hidden_size)							)




       def     UpperCamelCase_			(      self    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase							)			->     Tuple:
            _SCREAMING_SNAKE_CASE       :	str						    =							GPTNeoXJapaneseForCausalLM(config=__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()
            _SCREAMING_SNAKE_CASE       :	Any						    =							model(__lowerCamelCase    ,						attention_mask=__lowerCamelCase    ,						labels=__lowerCamelCase							)
            self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.vocab_size)							)




       def     UpperCamelCase_			(      self    ,						__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase							)			->     Union[str, Any]:
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							True
            _SCREAMING_SNAKE_CASE       :	int						    =							GPTNeoXJapaneseForCausalLM(config=__lowerCamelCase							)
            model.to(__lowerCamelCase							)
            model.eval()

            # first forward pass
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							model(__lowerCamelCase    ,						attention_mask=__lowerCamelCase    ,						use_cache=__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	List[Any]						    =							outputs.past_key_values

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

            # append to next input_ids and
            _SCREAMING_SNAKE_CASE       :	str						    =							torch.cat([input_ids, next_tokens]    ,						dim=-1							)
            _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							torch.cat([input_mask, next_mask]    ,						dim=-1							)

            _SCREAMING_SNAKE_CASE       :	Tuple						    =							model(__lowerCamelCase    ,						attention_mask=__lowerCamelCase    ,						output_hidden_states=__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							output_from_no_past["hidden_states"][0]
            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							model(
                __lowerCamelCase    ,						attention_mask=__lowerCamelCase    ,						past_key_values=__lowerCamelCase    ,						output_hidden_states=__lowerCamelCase    ,						)["hidden_states"][0]

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





@require_torch
class 			lowerCAmelCase__( __lowercase  ,     __lowercase  ,     unittest.TestCase    ):






       '''simple docstring'''


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




       def     UpperCamelCase_			(      self							)			->     Tuple:
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							GPTNeoXJapaneseModelTester(self							)
            _SCREAMING_SNAKE_CASE       :	str						    =							ConfigTester(self    ,						config_class=__lowerCamelCase    ,						hidden_size=3_7							)




       def     UpperCamelCase_			(      self							)			->     Dict:
            self.config_tester.run_common_tests()




       def     UpperCamelCase_			(      self							)			->     List[Any]:
            _SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE       :	Tuple						    =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase							)




       def     UpperCamelCase_			(      self							)			->     Optional[Any]:
            _SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE       :	Dict						    =							self.model_tester.prepare_config_and_inputs_for_decoder()
            self.model_tester.create_and_check_model_as_decoder(__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase							)




       def     UpperCamelCase_			(      self							)			->     Optional[Any]:
            # This regression test was failing with PyTorch < 1.3
            _SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE       :	Any						    =							self.model_tester.prepare_config_and_inputs_for_decoder()

            _SCREAMING_SNAKE_CASE       :	str						    =							None

            self.model_tester.create_and_check_model_as_decoder(__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase							)




       def     UpperCamelCase_			(      self							)			->     int:
            _SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE       :	Dict						    =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_decoder_model_past_large_inputs(__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase							)




       def     UpperCamelCase_			(      self							)			->     Dict:
            _SCREAMING_SNAKE_CASE       :	Dict						    =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_causal_lm(*__lowerCamelCase							)







       @slow
       def     UpperCamelCase_			(      self							)			->     List[Any]:
            _SCREAMING_SNAKE_CASE       :	List[str]						    =							"abeja/gpt-neox-japanese-2.7b"

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

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

            _SCREAMING_SNAKE_CASE       :	Tuple						    =							GPTNeoXJapaneseTokenizer.from_pretrained(__lowerCamelCase							)
            _SCREAMING_SNAKE_CASE       :	Optional[int]						    =							GPTNeoXJapaneseForCausalLM.from_pretrained(__lowerCamelCase							)

            _SCREAMING_SNAKE_CASE       :	Optional[Any]						    =							[]
            for prompt in prompts:
                 _SCREAMING_SNAKE_CASE       :	int						    =							tokenizer(__lowerCamelCase    ,						return_tensors="pt"							).input_ids
                 _SCREAMING_SNAKE_CASE       :	Dict						    =							model.generate(__lowerCamelCase    ,						max_length=5_0							)
                 _SCREAMING_SNAKE_CASE       :	List[Any]						    =							tokenizer.batch_decode(__lowerCamelCase    ,						skip_special_tokens=__lowerCamelCase							)
                 predicted_outputs += generated_string
            self.assertListEqual(__lowerCamelCase    ,						__lowerCamelCase							)






def     lowerCamelCase__			(__lowerCamelCase,       __lowerCamelCase	):
     while a != 0:
          _SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE       :	Any						    =							b % a, a
     return b






def     lowerCamelCase__			(__lowerCamelCase,       __lowerCamelCase	):
     if gcd(__lowerCamelCase,       __lowerCamelCase	) != 1:
          _SCREAMING_SNAKE_CASE       :	int						    =							f"""mod inverse of {a!r} and {m!r} does not exist"""
          raise ValueError(__lowerCamelCase	)
     _SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE       :	List[str]						    =							1, 0, a
     _SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE       :	List[str]						    =							0, 1, m
     while va != 0:
          _SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							ua // va
          _SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE    ,			_SCREAMING_SNAKE_CASE       :	Union[str, Any]						    =							(ua - q * va), (ua - q * va), (ua - q * va), va, va, va
     return ua % m