'''simple docstring'''





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




    '''simple docstring'''







    def __init__(       self    ,							lowercase__    ,							lowercase__=13    ,							lowercase__=7    ,							lowercase__=True    ,							lowercase__=True    ,							lowercase__=True    ,							lowercase__=True    ,							lowercase__=99    ,							lowercase__=32    ,							lowercase__=5    ,							lowercase__=4    ,							lowercase__=4    ,							lowercase__="gelu"    ,							lowercase__=0.0    ,							lowercase__=0.1    ,							lowercase__=True    ,							lowercase__=512    ,							lowercase__=16    ,							lowercase__=2    ,							lowercase__=0.0_2    ,							lowercase__=3    ,							lowercase__=4    ,							lowercase__=None    ,							)							-> Any:
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       parent
         SCREAMING_SNAKE_CASE					:			str    	=       batch_size
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       seq_length
         SCREAMING_SNAKE_CASE					:			List[str]    	=       is_training
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       use_input_mask
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       use_token_type_ids
         SCREAMING_SNAKE_CASE					:			Any    	=       use_labels
         SCREAMING_SNAKE_CASE					:			Dict    	=       vocab_size
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       hidden_size
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       num_hidden_layers
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       num_attention_heads
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       intermediate_multiple_size
         SCREAMING_SNAKE_CASE					:			int    	=       hidden_act
         SCREAMING_SNAKE_CASE					:			List[str]    	=       hidden_dropout
         SCREAMING_SNAKE_CASE					:			str    	=       attention_dropout
         SCREAMING_SNAKE_CASE					:			Dict    	=       weight_tying
         SCREAMING_SNAKE_CASE					:			str    	=       max_position_embeddings
         SCREAMING_SNAKE_CASE					:			Tuple    	=       type_vocab_size
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       type_sequence_label_size
         SCREAMING_SNAKE_CASE					:			Dict    	=       initializer_range
         SCREAMING_SNAKE_CASE					:			str    	=       num_labels
         SCREAMING_SNAKE_CASE					:			Dict    	=       num_choices
         SCREAMING_SNAKE_CASE					:			int    	=       scope






    def       _UpperCamelCase    (       self )							-> Optional[Any]:
         SCREAMING_SNAKE_CASE					:			Dict    	=       ids_tensor([self.batch_size, self.seq_length]    ,							self.vocab_size )

         SCREAMING_SNAKE_CASE					:			Dict    	=       None
         if self.use_input_mask:
              SCREAMING_SNAKE_CASE					:			Optional[int]    	=       random_attention_mask([self.batch_size, self.seq_length] )

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

         SCREAMING_SNAKE_CASE					:			Any    	=       self.get_config()

         return config, input_ids, input_mask, token_labels






    def       _UpperCamelCase    (       self )							-> Union[str, Any]:
         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       _UpperCamelCase    (       self )							-> int:
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.prepare_config_and_inputs()

         SCREAMING_SNAKE_CASE					:			Dict    	=       True

         return config, input_ids, input_mask, token_labels






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> Dict:
         SCREAMING_SNAKE_CASE					:			Tuple    	=       GPTNeoXJapaneseModel(config=__A )
         model.to(__A )
         model.eval()
         SCREAMING_SNAKE_CASE					:			Dict    	=       model(__A    ,							attention_mask=__A )
         SCREAMING_SNAKE_CASE					:			Dict    	=       model(__A )
         self.parent.assertEqual(result.last_hidden_state.shape    ,							(self.batch_size, self.seq_length, self.hidden_size) )






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> List[str]:
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       True
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       GPTNeoXJapaneseModel(__A )
         model.to(__A )
         model.eval()
         SCREAMING_SNAKE_CASE					:			Dict    	=       model(__A    ,							attention_mask=__A )
         self.parent.assertEqual(result.last_hidden_state.shape    ,							(self.batch_size, self.seq_length, self.hidden_size) )






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> str:
         SCREAMING_SNAKE_CASE					:			Any    	=       GPTNeoXJapaneseForCausalLM(config=__A )
         model.to(__A )
         model.eval()
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       model(__A    ,							attention_mask=__A    ,							labels=__A )
         self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.seq_length, self.vocab_size) )






    def       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       True
         SCREAMING_SNAKE_CASE					:			Tuple    	=       GPTNeoXJapaneseForCausalLM(config=__A )
         model.to(__A )
         model.eval()

         # first forward pass
         SCREAMING_SNAKE_CASE					:			List[str]    	=       model(__A    ,							attention_mask=__A    ,							use_cache=__A )
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       outputs.past_key_values

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

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

         SCREAMING_SNAKE_CASE					:			Any    	=       model(__A    ,							attention_mask=__A    ,							output_hidden_states=__A )
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       output_from_no_past["hidden_states"][0]
         SCREAMING_SNAKE_CASE					:			Dict    	=       model(
             __A    ,							attention_mask=__A    ,							past_key_values=__A    ,							output_hidden_states=__A    ,							)["hidden_states"][0]

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






    def       _UpperCamelCase    (       self )							-> List[Any]:
         SCREAMING_SNAKE_CASE					:			Dict    	=       self.prepare_config_and_inputs()
         SCREAMING_SNAKE_CASE					:			Any    	=       config_and_inputs
         SCREAMING_SNAKE_CASE					:			Tuple    	=       {"input_ids": input_ids, "attention_mask": input_mask}
         return config, inputs_dict






@require_torch
class 				UpperCAmelCase (       UpperCamelCase_   ,    UpperCamelCase_   ,    unittest.TestCase ):




    '''simple docstring'''







    snake_case__							:   str							=						(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
    snake_case__							:   Tuple							=						(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
    snake_case__							:   List[str]							=						(
        {"feature-extraction": GPTNeoXJapaneseModel, "text-generation": GPTNeoXJapaneseForCausalLM}
        if is_torch_available()
        else {}
    )
    snake_case__							:   Optional[Any]							=						False
    snake_case__							:   Any							=						False
    snake_case__							:   str							=						False
    snake_case__							:   Any							=						False






    def       _UpperCamelCase    (       self )							-> Dict:
         SCREAMING_SNAKE_CASE					:			Dict    	=       GPTNeoXJapaneseModelTester(self )
         SCREAMING_SNAKE_CASE					:			Optional[int]    	=       ConfigTester(self    ,							config_class=__A    ,							hidden_size=37 )






    def       _UpperCamelCase    (       self )							-> Any:
         self.config_tester.run_common_tests()






    def       _UpperCamelCase    (       self )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(__A    ,							__A    ,							__A )






    def       _UpperCamelCase    (       self )							-> str:
         SCREAMING_SNAKE_CASE					:			Any    	=       self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_model_as_decoder(__A    ,							__A    ,							__A )






    def       _UpperCamelCase    (       self )							-> Optional[Any]:
         # This regression test was failing with PyTorch < 1.3
         SCREAMING_SNAKE_CASE					:			str    	=       self.model_tester.prepare_config_and_inputs_for_decoder()

         SCREAMING_SNAKE_CASE					:			List[Any]    	=       None

         self.model_tester.create_and_check_model_as_decoder(__A    ,							__A    ,							__A )






    def       _UpperCamelCase    (       self )							-> Any:
         SCREAMING_SNAKE_CASE					:			List[str]    	=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_decoder_model_past_large_inputs(__A    ,							__A    ,							__A )






    def       _UpperCamelCase    (       self )							-> Dict:
         SCREAMING_SNAKE_CASE					:			Dict    	=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_causal_lm(*__A )






    @slow
    def       _UpperCamelCase    (       self )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       "abeja/gpt-neox-japanese-2.7b"

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

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

         SCREAMING_SNAKE_CASE					:			Dict    	=       GPTNeoXJapaneseTokenizer.from_pretrained(__A )
         SCREAMING_SNAKE_CASE					:			Dict    	=       GPTNeoXJapaneseForCausalLM.from_pretrained(__A )

         SCREAMING_SNAKE_CASE					:			Dict    	=       []
         for prompt in prompts:
              SCREAMING_SNAKE_CASE					:			Tuple    	=       tokenizer(__A    ,							return_tensors='pt' ).input_ids
              SCREAMING_SNAKE_CASE					:			List[str]    	=       model.generate(__A    ,							max_length=50 )
              SCREAMING_SNAKE_CASE					:			Optional[int]    	=       tokenizer.batch_decode(__A    ,							skip_special_tokens=__A )
              predicted_outputs += generated_string
         self.assertListEqual(__A    ,							__A )




'''simple docstring'''





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_lowerCAmelCase  :int  				=    {
    """configuration_albert""": ["""ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """AlbertConfig""", """AlbertOnnxConfig"""],
}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase  :str  				=    ["""AlbertTokenizer"""]

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase  :Optional[int]  				=    ["""AlbertTokenizerFast"""]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase  :List[str]  				=    [
     """ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """AlbertForMaskedLM""",
     """AlbertForMultipleChoice""",
     """AlbertForPreTraining""",
     """AlbertForQuestionAnswering""",
     """AlbertForSequenceClassification""",
     """AlbertForTokenClassification""",
     """AlbertModel""",
     """AlbertPreTrainedModel""",
     """load_tf_weights_in_albert""",
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase  :Any  				=    [
     """TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """TFAlbertForMaskedLM""",
     """TFAlbertForMultipleChoice""",
     """TFAlbertForPreTraining""",
     """TFAlbertForQuestionAnswering""",
     """TFAlbertForSequenceClassification""",
     """TFAlbertForTokenClassification""",
     """TFAlbertMainLayer""",
     """TFAlbertModel""",
     """TFAlbertPreTrainedModel""",
 ]

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase  :Optional[int]  				=    [
     """FlaxAlbertForMaskedLM""",
     """FlaxAlbertForMultipleChoice""",
     """FlaxAlbertForPreTraining""",
     """FlaxAlbertForQuestionAnswering""",
     """FlaxAlbertForSequenceClassification""",
     """FlaxAlbertForTokenClassification""",
     """FlaxAlbertModel""",
     """FlaxAlbertPreTrainedModel""",
 ]

if TYPE_CHECKING:
 from .configuration_albert import ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, AlbertOnnxConfig

 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_albert import AlbertTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_albert_fast import AlbertTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_albert import (
      ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      AlbertForMaskedLM,
      AlbertForMultipleChoice,
      AlbertForPreTraining,
      AlbertForQuestionAnswering,
      AlbertForSequenceClassification,
      AlbertForTokenClassification,
      AlbertModel,
      AlbertPreTrainedModel,
      load_tf_weights_in_albert,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_albert import (
      TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFAlbertForMaskedLM,
      TFAlbertForMultipleChoice,
      TFAlbertForPreTraining,
      TFAlbertForQuestionAnswering,
      TFAlbertForSequenceClassification,
      TFAlbertForTokenClassification,
      TFAlbertMainLayer,
      TFAlbertModel,
      TFAlbertPreTrainedModel,
  )

 try:
  if not is_flax_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_flax_albert import (
      FlaxAlbertForMaskedLM,
      FlaxAlbertForMultipleChoice,
      FlaxAlbertForPreTraining,
      FlaxAlbertForQuestionAnswering,
      FlaxAlbertForSequenceClassification,
      FlaxAlbertForTokenClassification,
      FlaxAlbertModel,
      FlaxAlbertPreTrainedModel,
  )
else:
 import sys

 _lowerCAmelCase  :Union[str, Any]  				=    _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



