





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




   """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.02 ,  lowercase=3 ,  lowercase=4 ,  lowercase=None ,  )      ->						Any:







          '''simple docstring'''

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

   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

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

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

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

          a__						:     List[str]			=    self.get_config()

          return config, input_ids, input_mask, token_labels

   def 							__lowercase		( self)      ->						Optional[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=lowercase ,  initializer_range=self.initializer_range ,  )

   def 							__lowercase		( self)      ->						str:







          '''simple docstring'''

          a__			,							a__			,							a__			,							a__						:     int			=    self.prepare_config_and_inputs()

          a__						:     List[Any]			=    True

          return config, input_ids, input_mask, token_labels

   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase)      ->						str:







          '''simple docstring'''

          a__						:     Optional[Any]			=    GPTNeoXJapaneseModel(config=lowercase)
          model.to(lowercase)
          model.eval()
          a__						:     Any			=    model(lowercase ,  attention_mask=lowercase)
          a__						:     Any			=    model(lowercase)
          self.parent.assertEqual(result.last_hidden_state.shape ,  (self.batch_size, self.seq_length, self.hidden_size))

   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase)      ->						List[Any]:







          '''simple docstring'''

          a__						:     List[str]			=    True
          a__						:     Dict			=    GPTNeoXJapaneseModel(lowercase)
          model.to(lowercase)
          model.eval()
          a__						:     Optional[Any]			=    model(lowercase ,  attention_mask=lowercase)
          self.parent.assertEqual(result.last_hidden_state.shape ,  (self.batch_size, self.seq_length, self.hidden_size))

   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						int:







          '''simple docstring'''

          a__						:     List[Any]			=    GPTNeoXJapaneseForCausalLM(config=lowercase)
          model.to(lowercase)
          model.eval()
          a__						:     Union[str, Any]			=    model(lowercase ,  attention_mask=lowercase ,  labels=lowercase)
          self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.seq_length, self.vocab_size))

   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase)      ->						Tuple:







          '''simple docstring'''

          a__						:     int			=    True
          a__						:     Optional[int]			=    GPTNeoXJapaneseForCausalLM(config=lowercase)
          model.to(lowercase)
          model.eval()

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

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

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

          a__						:     Dict			=    model(lowercase ,  attention_mask=lowercase ,  output_hidden_states=lowercase)
          a__						:     int			=    output_from_no_past['hidden_states'][0]
          a__						:     str			=    model(
              lowercase ,  attention_mask=lowercase ,  past_key_values=lowercase ,  output_hidden_states=lowercase ,  )['hidden_states'][0]

          # select random slice
          a__						:     str			=    ids_tensor((1,) ,  output_from_past.shape[-1]).item()
          a__						:     int			=    output_from_no_past[:, -3:, random_slice_idx].detach()
          a__						:     List[str]			=    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(lowercase ,  lowercase ,  atol=1e-3))

   def 							__lowercase		( self)      ->						List[str]:







          '''simple docstring'''

          a__						:     List[Any]			=    self.prepare_config_and_inputs()
          a__			,							a__			,							a__			,							a__						:     Union[str, Any]			=    config_and_inputs
          a__						:     Optional[int]			=    {'input_ids': input_ids, 'attention_mask': input_mask}
          return config, inputs_dict





@require_torch
class 			A__   (							__UpperCAmelCase			,  __UpperCAmelCase			,  unittest.TestCase     ):




   """simple docstring"""



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

   def 							__lowercase		( self)      ->						Any:







          '''simple docstring'''

          a__						:     List[str]			=    GPTNeoXJapaneseModelTester(self)
          a__						:     Optional[Any]			=    ConfigTester(self ,  config_class=lowercase ,  hidden_size=37)

   def 							__lowercase		( self)      ->						Optional[int]:







          '''simple docstring'''

          self.config_tester.run_common_tests()

   def 							__lowercase		( self)      ->						Union[str, Any]:







          '''simple docstring'''

          a__			,							a__			,							a__			,							a__						:     Optional[Any]			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(lowercase ,  lowercase ,  lowercase)

   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          a__			,							a__			,							a__			,							a__						:     Optional[Any]			=    self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_model_as_decoder(lowercase ,  lowercase ,  lowercase)

   def 							__lowercase		( self)      ->						Optional[Any]:







          '''simple docstring'''

          a__			,							a__			,							a__			,							a__						:     List[Any]			=    self.model_tester.prepare_config_and_inputs_for_decoder()

          a__						:     List[str]			=    None

          self.model_tester.create_and_check_model_as_decoder(lowercase ,  lowercase ,  lowercase)

   def 							__lowercase		( self)      ->						Union[str, Any]:







          '''simple docstring'''

          a__			,							a__			,							a__			,							a__						:     Optional[int]			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_decoder_model_past_large_inputs(lowercase ,  lowercase ,  lowercase)

   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          a__						:     Optional[int]			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_causal_lm(*lowercase)

   @slow
   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          a__						:     Union[str, Any]			=    'abeja/gpt-neox-japanese-2.7b'

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

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

          a__						:     Tuple			=    GPTNeoXJapaneseTokenizer.from_pretrained(lowercase)
          a__						:     int			=    GPTNeoXJapaneseForCausalLM.from_pretrained(lowercase)

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









from typing import TYPE_CHECKING

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


lowercase				:	str      						=  {
    """configuration_vision_encoder_decoder""": ["""VisionEncoderDecoderConfig""", """VisionEncoderDecoderOnnxConfig"""]
}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowercase				:	Optional[Any]      						=  ["""VisionEncoderDecoderModel"""]

try:
    if not is_tf_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowercase				:	Optional[int]      						=  ["""TFVisionEncoderDecoderModel"""]

try:
    if not is_flax_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowercase				:	List[str]      						=  ["""FlaxVisionEncoderDecoderModel"""]

if TYPE_CHECKING:
    from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel

    try:
        if not is_tf_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel

    try:
        if not is_flax_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel

else:
    import sys

    lowercase				:	Optional[int]      						=  _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


