


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


       """simple docstring"""




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


             '''simple docstring'''



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





       def 						lowerCAmelCase     (   self							:			Dict       ):


             '''simple docstring'''



             lowerCamelCase__		:				Union[str, Any]    =  ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size       )

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

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

             lowerCamelCase__		:				Dict    =  self.get_config()

             return config, input_ids, input_mask, token_labels





       def 						lowerCAmelCase     (   self							:			Tuple       ):


             '''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 						lowerCAmelCase     (   self							:			str       ):


             '''simple docstring'''



             lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__		:				str    =  self.prepare_config_and_inputs()

             lowerCamelCase__		:				List[Any]    =  True

             return config, input_ids, input_mask, token_labels





       def 						lowerCAmelCase     (   self							:			Union[str, Any]					,      __lowerCamelCase							:			str					,      __lowerCamelCase							:			Union[str, Any]					,      __lowerCamelCase							:			str       ):


             '''simple docstring'''



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





       def 						lowerCAmelCase     (   self							:			Union[str, Any]					,      __lowerCamelCase							:			Dict					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			Dict       ):


             '''simple docstring'''



             lowerCamelCase__		:				Optional[int]    =  True
             lowerCamelCase__		:				Optional[int]    =  GPTNeoXJapaneseModel(__lowerCamelCase       )
             model.to(__lowerCamelCase       )
             model.eval()
             lowerCamelCase__		:				List[str]    =  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							:			List[str]					,      __lowerCamelCase							:			Optional[int]					,      __lowerCamelCase							:			List[Any]					,      __lowerCamelCase							:			Tuple					,      __lowerCamelCase							:			Optional[Any]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Optional[Any]    =  GPTNeoXJapaneseForCausalLM(config=__lowerCamelCase       )
             model.to(__lowerCamelCase       )
             model.eval()
             lowerCamelCase__		:				Union[str, Any]    =  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[Any]					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			int					,      __lowerCamelCase							:			Any       ):


             '''simple docstring'''



             lowerCamelCase__		:				List[str]    =  True
             lowerCamelCase__		:				str    =  GPTNeoXJapaneseForCausalLM(config=__lowerCamelCase       )
             model.to(__lowerCamelCase       )
             model.eval()

             # first forward pass
             lowerCamelCase__		:				Optional[int]    =  model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      use_cache=__lowerCamelCase       )
             lowerCamelCase__		:				Tuple    =  outputs.past_key_values

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

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

             lowerCamelCase__		:				int    =  model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      output_hidden_states=__lowerCamelCase       )
             lowerCamelCase__		:				Optional[int]    =  output_from_no_past["hidden_states"][0]
             lowerCamelCase__		:				str    =  model(
                 __lowerCamelCase					,      attention_mask=__lowerCamelCase					,      past_key_values=__lowerCamelCase					,      output_hidden_states=__lowerCamelCase					,      )["hidden_states"][0]

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


             '''simple docstring'''



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







@require_torch
class 			_lowercase (							lowercase__							,  lowercase__							,  unittest.TestCase):


       """simple docstring"""




       A__											=			(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
       A__											=			(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
       A__											=			(
           {"feature-extraction": GPTNeoXJapaneseModel, "text-generation": GPTNeoXJapaneseForCausalLM}
           if is_torch_available()
           else {}
       )
       A__											=			False
       A__											=			False
       A__											=			False
       A__											=			False





       def 						lowerCAmelCase     (   self							:			int       ):


             '''simple docstring'''



             lowerCamelCase__		:				Optional[int]    =  GPTNeoXJapaneseModelTester(self       )
             lowerCamelCase__		:				Dict    =  ConfigTester(self					,      config_class=__lowerCamelCase					,      hidden_size=37       )





       def 						lowerCAmelCase     (   self							:			List[Any]       ):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       def 						lowerCAmelCase     (   self							:			Tuple       ):


             '''simple docstring'''



             lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__		:				Any    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Tuple       ):


             '''simple docstring'''



             lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__		:				Dict    =  self.model_tester.prepare_config_and_inputs_for_decoder()
             self.model_tester.create_and_check_model_as_decoder(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Union[str, Any]       ):


             '''simple docstring'''



             lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__		:				Dict    =  self.model_tester.prepare_config_and_inputs_for_decoder()

             lowerCamelCase__		:				Optional[int]    =  None

             self.model_tester.create_and_check_model_as_decoder(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			str       ):


             '''simple docstring'''



             lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__		:				Optional[Any]    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_decoder_model_past_large_inputs(__lowerCamelCase					,      __lowerCamelCase					,      __lowerCamelCase       )





       def 						lowerCAmelCase     (   self							:			Optional[int]       ):


             '''simple docstring'''



             lowerCamelCase__		:				Dict    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_causal_lm(*__lowerCamelCase       )





       @slow
       def 						lowerCAmelCase     (   self							:			int       ):


             '''simple docstring'''



             lowerCamelCase__		:				Dict    =  "abeja/gpt-neox-japanese-2.7b"

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

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

             lowerCamelCase__		:				int    =  GPTNeoXJapaneseTokenizer.from_pretrained(__lowerCamelCase       )
             lowerCamelCase__		:				List[Any]    =  GPTNeoXJapaneseForCausalLM.from_pretrained(__lowerCamelCase       )

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






from ...configuration_utils import PretrainedConfig
from ...utils import logging


A      :    str		=					logging.get_logger(__name__)

A      :    int		=					{
    "alibaba-damo/mgp-str-base": "https://huggingface.co/alibaba-damo/mgp-str-base/resolve/main/config.json",
}







class 			_lowercase (							lowercase__):


       """simple docstring"""




       A__											=			"mgp-str"





       def __init__(   self							:			List[str]					,      __lowerCamelCase							:			List[Any]=[32, 128]					,      __lowerCamelCase							:			Optional[int]=4					,      __lowerCamelCase							:			str=3					,      __lowerCamelCase							:			Optional[Any]=27					,      __lowerCamelCase							:			List[str]=38					,      __lowerCamelCase							:			Dict=50257					,      __lowerCamelCase							:			List[Any]=30522					,      __lowerCamelCase							:			Optional[Any]=768					,      __lowerCamelCase							:			str=12					,      __lowerCamelCase							:			Optional[int]=12					,      __lowerCamelCase							:			List[str]=4.0					,      __lowerCamelCase							:			Union[str, Any]=True					,      __lowerCamelCase							:			Any=False					,      __lowerCamelCase							:			Dict=1E-5					,      __lowerCamelCase							:			int=0.0					,      __lowerCamelCase							:			Optional[int]=0.0					,      __lowerCamelCase							:			List[Any]=0.0					,      __lowerCamelCase							:			Union[str, Any]=False					,      __lowerCamelCase							:			str=0.0_2					,      **__lowerCamelCase							:			Dict					,      ):


             '''simple docstring'''



             super().__init__(**__lowerCamelCase       )

             lowerCamelCase__		:				int    =  image_size
             lowerCamelCase__		:				Union[str, Any]    =  patch_size
             lowerCamelCase__		:				Dict    =  num_channels
             lowerCamelCase__		:				Union[str, Any]    =  max_token_length
             lowerCamelCase__		:				Optional[int]    =  num_character_labels
             lowerCamelCase__		:				Union[str, Any]    =  num_bpe_labels
             lowerCamelCase__		:				Optional[int]    =  num_wordpiece_labels
             lowerCamelCase__		:				List[str]    =  hidden_size
             lowerCamelCase__		:				Dict    =  num_hidden_layers
             lowerCamelCase__		:				Any    =  num_attention_heads
             lowerCamelCase__		:				Any    =  mlp_ratio
             lowerCamelCase__		:				List[Any]    =  distilled
             lowerCamelCase__		:				Optional[Any]    =  layer_norm_eps
             lowerCamelCase__		:				Union[str, Any]    =  drop_rate
             lowerCamelCase__		:				List[Any]    =  qkv_bias
             lowerCamelCase__		:				int    =  attn_drop_rate
             lowerCamelCase__		:				List[Any]    =  drop_path_rate
             lowerCamelCase__		:				List[str]    =  output_aa_attentions
             lowerCamelCase__		:				Dict    =  initializer_range


