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


 def __init__(					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_=13					,  SCREAMING_SNAKE_CASE_=7					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=99					,  SCREAMING_SNAKE_CASE_=32					,  SCREAMING_SNAKE_CASE_=5					,  SCREAMING_SNAKE_CASE_=4					,  SCREAMING_SNAKE_CASE_=4					,  SCREAMING_SNAKE_CASE_="gelu"					,  SCREAMING_SNAKE_CASE_=0.0					,  SCREAMING_SNAKE_CASE_=0.1					,  SCREAMING_SNAKE_CASE_=True					,  SCREAMING_SNAKE_CASE_=5_12					,  SCREAMING_SNAKE_CASE_=16					,  SCREAMING_SNAKE_CASE_=2					,  SCREAMING_SNAKE_CASE_=0.0_2					,  SCREAMING_SNAKE_CASE_=3					,  SCREAMING_SNAKE_CASE_=4					,  SCREAMING_SNAKE_CASE_=None					,  ):


   '''simple docstring'''




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


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Any    		=      ids_tensor([self.batch_size, self.seq_length]					,  self.vocab_size)

   lowercase__  :   Any    		=      None
   if self.use_input_mask:
     lowercase__  :   Dict    		=      random_attention_mask([self.batch_size, self.seq_length])

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

   lowercase__  :   List[Any]    		=      self.get_config()

   return config, input_ids, input_mask, token_labels


 def      lowercase__     (					self):


   '''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=SCREAMING_SNAKE_CASE_					,  initializer_range=self.initializer_range					,  )


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__		,    lowercase__		,    lowercase__		,    lowercase__  :   Dict    		=      self.prepare_config_and_inputs()

   lowercase__  :   Any    		=      True

   return config, input_ids, input_mask, token_labels


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   Tuple    		=      GPTNeoXJapaneseModel(config=SCREAMING_SNAKE_CASE_)
   model.to(SCREAMING_SNAKE_CASE_)
   model.eval()
   lowercase__  :   Any    		=      model(SCREAMING_SNAKE_CASE_					,  attention_mask=SCREAMING_SNAKE_CASE_)
   lowercase__  :   int    		=      model(SCREAMING_SNAKE_CASE_)
   self.parent.assertEqual(result.last_hidden_state.shape					,  (self.batch_size, self.seq_length, self.hidden_size))


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   Dict    		=      True
   lowercase__  :   Tuple    		=      GPTNeoXJapaneseModel(SCREAMING_SNAKE_CASE_)
   model.to(SCREAMING_SNAKE_CASE_)
   model.eval()
   lowercase__  :   Optional[int]    		=      model(SCREAMING_SNAKE_CASE_					,  attention_mask=SCREAMING_SNAKE_CASE_)
   self.parent.assertEqual(result.last_hidden_state.shape					,  (self.batch_size, self.seq_length, self.hidden_size))


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   Any    		=      GPTNeoXJapaneseForCausalLM(config=SCREAMING_SNAKE_CASE_)
   model.to(SCREAMING_SNAKE_CASE_)
   model.eval()
   lowercase__  :   Dict    		=      model(SCREAMING_SNAKE_CASE_					,  attention_mask=SCREAMING_SNAKE_CASE_					,  labels=SCREAMING_SNAKE_CASE_)
   self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.seq_length, self.vocab_size))


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__  :   List[str]    		=      True
   lowercase__  :   Optional[int]    		=      GPTNeoXJapaneseForCausalLM(config=SCREAMING_SNAKE_CASE_)
   model.to(SCREAMING_SNAKE_CASE_)
   model.eval()

   # first forward pass
   lowercase__  :   int    		=      model(SCREAMING_SNAKE_CASE_					,  attention_mask=SCREAMING_SNAKE_CASE_					,  use_cache=SCREAMING_SNAKE_CASE_)
   lowercase__  :   Optional[int]    		=      outputs.past_key_values

   # create hypothetical multiple next token and extent to next_input_ids
   lowercase__  :   Optional[int]    		=      ids_tensor((self.batch_size, 3)					,  config.vocab_size)
   lowercase__  :   str    		=      ids_tensor((self.batch_size, 3)					,  vocab_size=2)

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

   lowercase__  :   Tuple    		=      model(SCREAMING_SNAKE_CASE_					,  attention_mask=SCREAMING_SNAKE_CASE_					,  output_hidden_states=SCREAMING_SNAKE_CASE_)
   lowercase__  :   Tuple    		=      output_from_no_past["""hidden_states"""][0]
   lowercase__  :   Optional[int]    		=      model(
       SCREAMING_SNAKE_CASE_					,  attention_mask=SCREAMING_SNAKE_CASE_					,  past_key_values=SCREAMING_SNAKE_CASE_					,  output_hidden_states=SCREAMING_SNAKE_CASE_					,  )["""hidden_states"""][0]

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



 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   List[str]    		=      self.prepare_config_and_inputs()
   lowercase__		,    lowercase__		,    lowercase__		,    lowercase__  :   Dict    		=      config_and_inputs
   lowercase__  :   List[str]    		=      {"""input_ids""": input_ids, """attention_mask""": input_mask}
   return config, inputs_dict



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


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   List[str]    		=      GPTNeoXJapaneseModelTester(self)
   lowercase__  :   Optional[int]    		=      ConfigTester(self					,  config_class=SCREAMING_SNAKE_CASE_					,  hidden_size=37)


 def      lowercase__     (					self):


   '''simple docstring'''




   self.config_tester.run_common_tests()


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__		,    lowercase__		,    lowercase__		,    lowercase__  :   Any    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__		,    lowercase__		,    lowercase__		,    lowercase__  :   Optional[int]    		=      self.model_tester.prepare_config_and_inputs_for_decoder()
   self.model_tester.create_and_check_model_as_decoder(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__		,    lowercase__		,    lowercase__		,    lowercase__  :   Tuple    		=      self.model_tester.prepare_config_and_inputs_for_decoder()

   lowercase__  :   str    		=      None

   self.model_tester.create_and_check_model_as_decoder(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__		,    lowercase__		,    lowercase__		,    lowercase__  :   Union[str, Any]    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_decoder_model_past_large_inputs(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Any    		=      self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_causal_lm(*SCREAMING_SNAKE_CASE_)



 @slow
 def      lowercase__     (					self):


   '''simple docstring'''




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

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

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

   lowercase__  :   Union[str, Any]    		=      GPTNeoXJapaneseTokenizer.from_pretrained(SCREAMING_SNAKE_CASE_)
   lowercase__  :   List[Any]    		=      GPTNeoXJapaneseForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE_)

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








def 		UpperCamelCase	(      lowercase_ = 10_00   )		->	int:


  '''simple docstring'''






  return sum(2 * a * ((a - 1) // 2) for a in range(3 , n + 1   )   )


if __name__ == "__main__":
     print(solution())






