





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


   def __init__(					self    :       Optional[Any]    ,  a    :       Optional[Any]    ,  a    :       str=13    ,  a    :       Optional[int]=7    ,  a    :       Dict=True    ,  a    :       List[str]=True    ,  a    :       Any=True    ,  a    :       Optional[int]=True    ,  a    :       Tuple=99    ,  a    :       List[str]=32    ,  a    :       Optional[Any]=5    ,  a    :       List[Any]=4    ,  a    :       Tuple=4    ,  a    :       Dict="gelu"    ,  a    :       str=0.0    ,  a    :       int=0.1    ,  a    :       str=True    ,  a    :       List[str]=512    ,  a    :       Any=16    ,  a    :       Optional[Any]=2    ,  a    :       List[Any]=0.02    ,  a    :       Tuple=3    ,  a    :       Union[str, Any]=4    ,  a    :       Optional[int]=None    ,  )->     str:





     """simple docstring"""







     lowercase__			    =						parent
     lowercase__			    =						batch_size
     lowercase__			    =						seq_length
     lowercase__			    =						is_training
     lowercase__			    =						use_input_mask
     lowercase__			    =						use_token_type_ids
     lowercase__			    =						use_labels
     lowercase__			    =						vocab_size
     lowercase__			    =						hidden_size
     lowercase__			    =						num_hidden_layers
     lowercase__			    =						num_attention_heads
     lowercase__			    =						intermediate_multiple_size
     lowercase__			    =						hidden_act
     lowercase__			    =						hidden_dropout
     lowercase__			    =						attention_dropout
     lowercase__			    =						weight_tying
     lowercase__			    =						max_position_embeddings
     lowercase__			    =						type_vocab_size
     lowercase__			    =						type_sequence_label_size
     lowercase__			    =						initializer_range
     lowercase__			    =						num_labels
     lowercase__			    =						num_choices
     lowercase__			    =						scope


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any] )->     str:





     """simple docstring"""







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

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

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

     lowercase__			    =						self.get_config()

     return config, input_ids, input_mask, token_labels


   def  SCREAMING_SNAKE_CASE_							(					self    :       Tuple )->     Any:





     """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=a    ,  initializer_range=self.initializer_range    ,  )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     str:





     """simple docstring"""







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

     lowercase__			    =						True

     return config, input_ids, input_mask, token_labels


   def  SCREAMING_SNAKE_CASE_							(					self    :       Tuple    ,  a    :       Union[str, Any]    ,  a    :       List[str]    ,  a    :       Tuple )->     Optional[int]:





     """simple docstring"""







     lowercase__			    =						GPTNeoXJapaneseModel(config=a )
     model.to(a )
     model.eval()
     lowercase__			    =						model(a    ,  attention_mask=a )
     lowercase__			    =						model(a )
     self.parent.assertEqual(result.last_hidden_state.shape    ,  (self.batch_size, self.seq_length, self.hidden_size) )


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any]    ,  a    :       List[str]    ,  a    :       Any    ,  a    :       int )->     int:





     """simple docstring"""







     lowercase__			    =						True
     lowercase__			    =						GPTNeoXJapaneseModel(a )
     model.to(a )
     model.eval()
     lowercase__			    =						model(a    ,  attention_mask=a )
     self.parent.assertEqual(result.last_hidden_state.shape    ,  (self.batch_size, self.seq_length, self.hidden_size) )


   def  SCREAMING_SNAKE_CASE_							(					self    :       int    ,  a    :       Tuple    ,  a    :       Optional[int]    ,  a    :       Dict    ,  a    :       str )->     int:





     """simple docstring"""







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


   def  SCREAMING_SNAKE_CASE_							(					self    :       str    ,  a    :       str    ,  a    :       Optional[int]    ,  a    :       Optional[Any] )->     List[Any]:





     """simple docstring"""







     lowercase__			    =						True
     lowercase__			    =						GPTNeoXJapaneseForCausalLM(config=a )
     model.to(a )
     model.eval()

     # first forward pass
     lowercase__			    =						model(a    ,  attention_mask=a    ,  use_cache=a )
     lowercase__			    =						outputs.past_key_values

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

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

     lowercase__			    =						model(a    ,  attention_mask=a    ,  output_hidden_states=a )
     lowercase__			    =						output_from_no_past['hidden_states'][0]
     lowercase__			    =						model(
         a    ,  attention_mask=a    ,  past_key_values=a    ,  output_hidden_states=a    ,  )['hidden_states'][0]

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


   def  SCREAMING_SNAKE_CASE_							(					self    :       str )->     Tuple:





     """simple docstring"""







     lowercase__			    =						self.prepare_config_and_inputs()
     lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__			    =						config_and_inputs
     lowercase__			    =						{'input_ids': input_ids, 'attention_mask': input_mask}
     return config, inputs_dict






@require_torch
class        SCREAMING_SNAKE_CASE     (UpperCAmelCase    ,			UpperCAmelCase    ,			unittest.TestCase   ):
   _UpperCamelCase					:	str				      = (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
   _UpperCamelCase					:	Optional[int]				      = (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
   _UpperCamelCase					:	str				      = (
       {'feature-extraction': GPTNeoXJapaneseModel, 'text-generation': GPTNeoXJapaneseForCausalLM}
       if is_torch_available()
       else {}
   )
   _UpperCamelCase					:	Optional[int]				      = False
   _UpperCamelCase					:	str				      = False
   _UpperCamelCase					:	Optional[int]				      = False
   _UpperCamelCase					:	str				      = False


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any] )->     Tuple:





     """simple docstring"""







     lowercase__			    =						GPTNeoXJapaneseModelTester(self )
     lowercase__			    =						ConfigTester(self    ,  config_class=a    ,  hidden_size=37 )


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[str] )->     Any:





     """simple docstring"""







     self.config_tester.run_common_tests()


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any] )->     int:





     """simple docstring"""







     lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__			    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(a    ,  a    ,  a )


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any] )->     Dict:





     """simple docstring"""







     lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__			    =						self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_model_as_decoder(a    ,  a    ,  a )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     Tuple:





     """simple docstring"""







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

     lowercase__			    =						None

     self.model_tester.create_and_check_model_as_decoder(a    ,  a    ,  a )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Tuple )->     str:





     """simple docstring"""







     lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__			    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_decoder_model_past_large_inputs(a    ,  a    ,  a )


   def  SCREAMING_SNAKE_CASE_							(					self    :       str )->     Any:





     """simple docstring"""







     lowercase__			    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_causal_lm(*a )


   @slow
   def  SCREAMING_SNAKE_CASE_							(					self    :       Tuple )->     List[Any]:





     """simple docstring"""







     lowercase__			    =						'abeja/gpt-neox-japanese-2.7b'

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

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

     lowercase__			    =						GPTNeoXJapaneseTokenizer.from_pretrained(a )
     lowercase__			    =						GPTNeoXJapaneseForCausalLM.from_pretrained(a )

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









def     __UpperCamelCase						(_SCREAMING_SNAKE_CASE = 50	)       ->     int:
  lowercase__			    =						[[0] * 3 for _ in range(length + 1	)]

  for row_length in range(length + 1	):
    for tile_length in range(2	,		5	):
      for tile_start in range(row_length - tile_length + 1	):
        different_colour_ways_number[row_length][tile_length - 2] += (
            different_colour_ways_number[row_length - tile_start - tile_length][
                tile_length - 2
            ]
            + 1
        )

  return sum(different_colour_ways_number[length]	)


if __name__ == "__main__":
      print(f'''{solution() = }''')


