





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






     """simple docstring"""






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



     def      lowerCAmelCase			(   self							:			List[Any]						)->	Any:
        snake_case 						=   ids_tensor([self.batch_size, self.seq_length]   ,       self.vocab_size						)

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

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

        snake_case 						=   self.get_config()

        return config, input_ids, input_mask, token_labels



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

        snake_case 						=   True

        return config, input_ids, input_mask, token_labels



     def      lowerCAmelCase			(   self							:			Tuple   ,       __snake_case							:			int   ,       __snake_case							:			List[Any]   ,       __snake_case							:			Tuple						)->	Any:
        snake_case 						=   GPTNeoXJapaneseModel(config=lowerCAmelCase__						)
        model.to(lowerCAmelCase__						)
        model.eval()
        snake_case 						=   model(lowerCAmelCase__   ,       attention_mask=lowerCAmelCase__						)
        snake_case 						=   model(lowerCAmelCase__						)
        self.parent.assertEqual(result.last_hidden_state.shape   ,       (self.batch_size, self.seq_length, self.hidden_size)						)



     def      lowerCAmelCase			(   self							:			Optional[Any]   ,       __snake_case							:			Optional[int]   ,       __snake_case							:			str   ,       __snake_case							:			int						)->	List[Any]:
        snake_case 						=   True
        snake_case 						=   GPTNeoXJapaneseModel(lowerCAmelCase__						)
        model.to(lowerCAmelCase__						)
        model.eval()
        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[Any]   ,       __snake_case							:			Tuple   ,       __snake_case							:			str   ,       __snake_case							:			Tuple   ,       __snake_case							:			List[Any]						)->	List[Any]:
        snake_case 						=   GPTNeoXJapaneseForCausalLM(config=lowerCAmelCase__						)
        model.to(lowerCAmelCase__						)
        model.eval()
        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							:			List[str]   ,       __snake_case							:			Any   ,       __snake_case							:			Tuple   ,       __snake_case							:			Any						)->	Any:
        snake_case 						=   True
        snake_case 						=   GPTNeoXJapaneseForCausalLM(config=lowerCAmelCase__						)
        model.to(lowerCAmelCase__						)
        model.eval()

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

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

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

        snake_case 						=   model(lowerCAmelCase__   ,       attention_mask=lowerCAmelCase__   ,       output_hidden_states=lowerCAmelCase__						)
        snake_case 						=   output_from_no_past["""hidden_states"""][0]
        snake_case 						=   model(
            lowerCAmelCase__   ,       attention_mask=lowerCAmelCase__   ,       past_key_values=lowerCAmelCase__   ,       output_hidden_states=lowerCAmelCase__   ,       )["""hidden_states"""][0]

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




@require_torch
class 			_lowerCAmelCase		(			A__							,     A__							,     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      lowerCAmelCase			(   self							:			Dict						)->	Dict:
        snake_case 						=   GPTNeoXJapaneseModelTester(self						)
        snake_case 						=   ConfigTester(self   ,       config_class=lowerCAmelCase__   ,       hidden_size=37						)



     def      lowerCAmelCase			(   self							:			int						)->	str:
        self.config_tester.run_common_tests()



     def      lowerCAmelCase			(   self							:			Tuple						)->	Dict:
        snake_case						,    snake_case						,    snake_case						,    snake_case 						=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(lowerCAmelCase__   ,       lowerCAmelCase__   ,       lowerCAmelCase__						)



     def      lowerCAmelCase			(   self							:			Optional[int]						)->	List[str]:
        snake_case						,    snake_case						,    snake_case						,    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							:			Dict						)->	Dict:
        snake_case						,    snake_case						,    snake_case						,    snake_case 						=   self.model_tester.prepare_config_and_inputs_for_decoder()

        snake_case 						=   None

        self.model_tester.create_and_check_model_as_decoder(lowerCAmelCase__   ,       lowerCAmelCase__   ,       lowerCAmelCase__						)



     def      lowerCAmelCase			(   self							:			List[Any]						)->	Dict:
        snake_case						,    snake_case						,    snake_case						,    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							:			Union[str, Any]						)->	List[Any]:
        snake_case 						=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_causal_lm(*lowerCAmelCase__						)



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

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

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

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

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








'''simple docstring'''

import unittest

from accelerate import debug_launcher
from accelerate.test_utils import require_cpu, test_ops, test_script
@require_cpu
class 							__lowerCAmelCase	(       unittest.TestCase					):






       """simple docstring"""





       def snake_case__		(				self  :						str	)       ->	Optional[int]:

            '''simple docstring'''




            debug_launcher(test_script.main	)







       def snake_case__		(				self  :						Any	)       ->	Optional[Any]:

            '''simple docstring'''




            debug_launcher(test_ops.main	)



