





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		,      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		,      )   ->						Any:



        '''simple docstring'''




        a__	:					str										=parent
        a__	:					Dict										=batch_size
        a__	:					List[str]										=seq_length
        a__	:					Any										=is_training
        a__	:					Tuple										=use_input_mask
        a__	:					List[str]										=use_token_type_ids
        a__	:					Union[str, Any]										=use_labels
        a__	:					Optional[int]										=vocab_size
        a__	:					int										=hidden_size
        a__	:					int										=num_hidden_layers
        a__	:					List[Any]										=num_attention_heads
        a__	:					str										=intermediate_multiple_size
        a__	:					List[str]										=hidden_act
        a__	:					Optional[int]										=hidden_dropout
        a__	:					List[str]										=attention_dropout
        a__	:					int										=weight_tying
        a__	:					Optional[Any]										=max_position_embeddings
        a__	:					Any										=type_vocab_size
        a__	:					Optional[int]										=type_sequence_label_size
        a__	:					Optional[Any]										=initializer_range
        a__	:					Dict										=num_labels
        a__	:					List[str]										=num_choices
        a__	:					Union[str, Any]										=scope







      def    _lowercase  (    self							)   ->						Dict:



        '''simple docstring'''




        a__	:					int										=ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size							)

        a__	:					Optional[int]										=None
        if self.use_input_mask:
          a__	:					List[str]										=random_attention_mask([self.batch_size, self.seq_length]							)

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

        a__	:					Any										=self.get_config()

        return config, input_ids, input_mask, token_labels







      def    _lowercase  (    self							)   ->						Dict:



        '''simple docstring'''




        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    _lowercase  (    self							)   ->						Union[str, Any]:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					Tuple										=self.prepare_config_and_inputs()

        a__	:					List[str]										=True

        return config, input_ids, input_mask, token_labels







      def    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)   ->						Tuple:



        '''simple docstring'''




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







      def    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)   ->						Dict:



        '''simple docstring'''




        a__	:					Optional[int]										=True
        a__	:					Dict										=GPTNeoXJapaneseModel(lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.eval()
        a__	:					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    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)   ->						List[Any]:



        '''simple docstring'''




        a__	:					Optional[int]										=GPTNeoXJapaneseForCausalLM(config=lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.eval()
        a__	:					Optional[Any]										=model(lowerCAmelCase__		,      attention_mask=lowerCAmelCase__		,      labels=lowerCAmelCase__							)
        self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.vocab_size)							)







      def    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)   ->						List[Any]:



        '''simple docstring'''




        a__	:					int										=True
        a__	:					str										=GPTNeoXJapaneseForCausalLM(config=lowerCAmelCase__							)
        model.to(lowerCAmelCase__							)
        model.eval()

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

        # create hypothetical multiple next token and extent to next_input_ids
        a__	:					Tuple										=ids_tensor((self.batch_size, 3)		,      config.vocab_size							)
        a__	:					List[Any]										=ids_tensor((self.batch_size, 3)		,      vocab_size=2							)

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

        a__	:					int										=model(lowerCAmelCase__		,      attention_mask=lowerCAmelCase__		,      output_hidden_states=lowerCAmelCase__							)
        a__	:					Dict										=output_from_no_past["hidden_states"][0]
        a__	:					Any										=model(
            lowerCAmelCase__		,      attention_mask=lowerCAmelCase__		,      past_key_values=lowerCAmelCase__		,      output_hidden_states=lowerCAmelCase__		,      )["hidden_states"][0]

        # select random slice
        a__	:					List[str]										=ids_tensor((1,)		,      output_from_past.shape[-1]							).item()
        a__	:					List[Any]										=output_from_no_past[:, -3:, random_slice_idx].detach()
        a__	:					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    _lowercase  (    self							)   ->						List[str]:



        '''simple docstring'''




        a__	:					List[Any]										=self.prepare_config_and_inputs()
        a__					,							a__					,							a__					,							a__	:					int										=config_and_inputs
        a__	:					Optional[Any]										={"input_ids": input_ids, "attention_mask": input_mask}
        return config, inputs_dict







@require_torch
class 				__lowerCAmelCase       (    UpperCamelCase__						,				UpperCamelCase__						,				unittest.TestCase):
      _lowercase				:					Optional[int]						=			(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
      _lowercase				:					List[str]						=			(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
      _lowercase				:					Optional[int]						=			(
          {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM}
          if is_torch_available()
          else {}
      )
      _lowercase				:					int						=			False
      _lowercase				:					Optional[Any]						=			False
      _lowercase				:					Tuple						=			False
      _lowercase				:					int						=			False







      def    _lowercase  (    self							)   ->						Tuple:



        '''simple docstring'''




        a__	:					Optional[Any]										=GPTNeoXJapaneseModelTester(self							)
        a__	:					Union[str, Any]										=ConfigTester(self		,      config_class=lowerCAmelCase__		,      hidden_size=3_7							)







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



        '''simple docstring'''




        self.config_tester.run_common_tests()







      def    _lowercase  (    self							)   ->						List[Any]:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					Any										=self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)







      def    _lowercase  (    self							)   ->						Any:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					List[str]										=self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_model_as_decoder(lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)







      def    _lowercase  (    self							)   ->						List[str]:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					Optional[int]										=self.model_tester.prepare_config_and_inputs_for_decoder()

        a__	:					str										=None

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







      def    _lowercase  (    self							)   ->						Optional[int]:



        '''simple docstring'''




        a__					,							a__					,							a__					,							a__	:					Any										=self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_decoder_model_past_large_inputs(lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)







      def    _lowercase  (    self							)   ->						List[Any]:



        '''simple docstring'''




        a__	:					List[Any]										=self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_causal_lm(*lowerCAmelCase__							)







      @slow
      def    _lowercase  (    self							)   ->						int:



        '''simple docstring'''




        a__	:					Tuple										="abeja/gpt-neox-japanese-2.7b"

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

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

        a__	:					int										=GPTNeoXJapaneseTokenizer.from_pretrained(lowerCAmelCase__							)
        a__	:					Dict										=GPTNeoXJapaneseForCausalLM.from_pretrained(lowerCAmelCase__							)

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








def        UpperCamelCase    (							lowerCAmelCase__		):
					'''simple docstring'''

					if edge <= 0 or not isinstance(lowerCAmelCase__ ,			lowerCAmelCase__		):
										raise ValueError('''Length must be a positive.'''		)
					return 3 * ((25 + 10 * (5 ** (1 / 2))) ** (1 / 2)) * (edge**2)



def        UpperCamelCase    (							lowerCAmelCase__		):
					'''simple docstring'''

					if edge <= 0 or not isinstance(lowerCAmelCase__ ,			lowerCAmelCase__		):
										raise ValueError('''Length must be a positive.'''		)
					return ((15 + (7 * (5 ** (1 / 2)))) / 4) * (edge**3)


if __name__ == "__main__":
							import doctest

							doctest.testmod()




