






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.0_2  ,      _lowerCamelCase=3  ,      _lowerCamelCase=4  ,      _lowerCamelCase=None  ,      ):
         UpperCamelCase_: Any   				=    parent
         UpperCamelCase_: Union[str, Any]   				=    batch_size
         UpperCamelCase_: List[str]   				=    seq_length
         UpperCamelCase_: Union[str, Any]   				=    is_training
         UpperCamelCase_: List[str]   				=    use_input_mask
         UpperCamelCase_: Union[str, Any]   				=    use_token_type_ids
         UpperCamelCase_: int   				=    use_labels
         UpperCamelCase_: List[Any]   				=    vocab_size
         UpperCamelCase_: Optional[int]   				=    hidden_size
         UpperCamelCase_: Tuple   				=    num_hidden_layers
         UpperCamelCase_: Union[str, Any]   				=    num_attention_heads
         UpperCamelCase_: Any   				=    intermediate_multiple_size
         UpperCamelCase_: str   				=    hidden_act
         UpperCamelCase_: Optional[Any]   				=    hidden_dropout
         UpperCamelCase_: Optional[int]   				=    attention_dropout
         UpperCamelCase_: Dict   				=    weight_tying
         UpperCamelCase_: List[Any]   				=    max_position_embeddings
         UpperCamelCase_: Dict   				=    type_vocab_size
         UpperCamelCase_: str   				=    type_sequence_label_size
         UpperCamelCase_: List[str]   				=    initializer_range
         UpperCamelCase_: Optional[Any]   				=    num_labels
         UpperCamelCase_: List[Any]   				=    num_choices
         UpperCamelCase_: List[str]   				=    scope





       def       _a   ( self							):
         UpperCamelCase_: int   				=    ids_tensor([self.batch_size, self.seq_length]  ,      self.vocab_size							)

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

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

         UpperCamelCase_: Dict   				=    self.get_config()

         return config, input_ids, input_mask, token_labels





       def       _a   ( self							):
         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       _a   ( self							):
         UpperCamelCase_				,UpperCamelCase_				,UpperCamelCase_				,UpperCamelCase_: Optional[Any]   				=    self.prepare_config_and_inputs()

         UpperCamelCase_: Any   				=    True

         return config, input_ids, input_mask, token_labels





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: Optional[Any]   				=    GPTNeoXJapaneseModel(config=_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()
         UpperCamelCase_: Union[str, Any]   				=    model(_lowerCamelCase  ,      attention_mask=_lowerCamelCase							)
         UpperCamelCase_: Optional[Any]   				=    model(_lowerCamelCase							)
         self.parent.assertEqual(result.last_hidden_state.shape  ,      (self.batch_size, self.seq_length, self.hidden_size)							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: str   				=    True
         UpperCamelCase_: Optional[Any]   				=    GPTNeoXJapaneseModel(_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()
         UpperCamelCase_: Dict   				=    model(_lowerCamelCase  ,      attention_mask=_lowerCamelCase							)
         self.parent.assertEqual(result.last_hidden_state.shape  ,      (self.batch_size, self.seq_length, self.hidden_size)							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: int   				=    GPTNeoXJapaneseForCausalLM(config=_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()
         UpperCamelCase_: Any   				=    model(_lowerCamelCase  ,      attention_mask=_lowerCamelCase  ,      labels=_lowerCamelCase							)
         self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.seq_length, self.vocab_size)							)





       def       _a   ( self  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							):
         UpperCamelCase_: List[str]   				=    True
         UpperCamelCase_: Any   				=    GPTNeoXJapaneseForCausalLM(config=_lowerCamelCase							)
         model.to(_lowerCamelCase							)
         model.eval()

         # first forward pass
         UpperCamelCase_: Dict   				=    model(_lowerCamelCase  ,      attention_mask=_lowerCamelCase  ,      use_cache=_lowerCamelCase							)
         UpperCamelCase_: List[Any]   				=    outputs.past_key_values

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

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

         UpperCamelCase_: Dict   				=    model(_lowerCamelCase  ,      attention_mask=_lowerCamelCase  ,      output_hidden_states=_lowerCamelCase							)
         UpperCamelCase_: int   				=    output_from_no_past['hidden_states'][0]
         UpperCamelCase_: Optional[Any]   				=    model(
             _lowerCamelCase  ,      attention_mask=_lowerCamelCase  ,      past_key_values=_lowerCamelCase  ,      output_hidden_states=_lowerCamelCase  ,      )['hidden_states'][0]

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







@require_torch
class     _lowerCAmelCase(	UpperCAmelCase_							,     UpperCAmelCase_							,     unittest.TestCase ):





       """simple docstring"""







       a    :		int									=(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
       a    :		int									=(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
       a    :		Any									=(
           {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM}
           if is_torch_available()
           else {}
       )
       a    :		Any									=False
       a    :		Union[str, Any]									=False
       a    :		Dict									=False
       a    :		Union[str, Any]									=False





       def       _a   ( self							):
         UpperCamelCase_: Optional[Any]   				=    GPTNeoXJapaneseModelTester(self							)
         UpperCamelCase_: str   				=    ConfigTester(self  ,      config_class=_lowerCamelCase  ,      hidden_size=3_7							)





       def       _a   ( self							):
         self.config_tester.run_common_tests()





       def       _a   ( self							):
         UpperCamelCase_				,UpperCamelCase_				,UpperCamelCase_				,UpperCamelCase_: str   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(_lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_				,UpperCamelCase_				,UpperCamelCase_				,UpperCamelCase_: Dict   				=    self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_model_as_decoder(_lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							)





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

         UpperCamelCase_: Optional[Any]   				=    None

         self.model_tester.create_and_check_model_as_decoder(_lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_				,UpperCamelCase_				,UpperCamelCase_				,UpperCamelCase_: Dict   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_decoder_model_past_large_inputs(_lowerCamelCase  ,      _lowerCamelCase  ,      _lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_: List[str]   				=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_causal_lm(*_lowerCamelCase							)





       @slow
       def       _a   ( self							):
         UpperCamelCase_: List[str]   				=    'abeja/gpt-neox-japanese-2.7b'

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

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

         UpperCamelCase_: Dict   				=    GPTNeoXJapaneseTokenizer.from_pretrained(_lowerCamelCase							)
         UpperCamelCase_: Union[str, Any]   				=    GPTNeoXJapaneseForCausalLM.from_pretrained(_lowerCamelCase							)

         UpperCamelCase_: Tuple   				=    []
         for prompt in prompts:
           UpperCamelCase_: Tuple   				=    tokenizer(_lowerCamelCase  ,      return_tensors='pt'							).input_ids
           UpperCamelCase_: int   				=    model.generate(_lowerCamelCase  ,      max_length=5_0							)
           UpperCamelCase_: Tuple   				=    tokenizer.batch_decode(_lowerCamelCase  ,      skip_special_tokens=_lowerCamelCase							)
           predicted_outputs += generated_string
         self.assertListEqual(_lowerCamelCase  ,      _lowerCamelCase							)







def   snake_case (UpperCAmelCase__  )	->    int:
  assert isinstance(UpperCAmelCase__     ,   UpperCAmelCase__  ), F'''The input value of [n={number}] is not an integer'''

  if number == 1:
    return 2
  elif number < 1:
    UpperCamelCase_: List[Any]   				=    F'''The input value of [n={number}] has to be > 0'''
    raise ValueError(UpperCAmelCase__  )
  else:
    UpperCamelCase_: str   				=    sylvester(number - 1  )
    UpperCamelCase_: str   				=    num - 1
    UpperCamelCase_: Any   				=    num
    return lower * upper + 1


if __name__ == "__main__":
   print(F'''The 8th number in Sylvester\'s sequence: {sylvester(8)}''')