






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		,		_UpperCAmelCase		,		_UpperCAmelCase=13		,		_UpperCAmelCase=7		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=99		,		_UpperCAmelCase=32		,		_UpperCAmelCase=5		,		_UpperCAmelCase=4		,		_UpperCAmelCase=4		,		_UpperCAmelCase="gelu"		,		_UpperCAmelCase=0.0		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=True		,		_UpperCAmelCase=512		,		_UpperCAmelCase=16		,		_UpperCAmelCase=2		,		_UpperCAmelCase=0.0_2		,		_UpperCAmelCase=3		,		_UpperCAmelCase=4		,		_UpperCAmelCase=None		,		)			-> Union[str, Any]:
     UpperCamelCase_									=     parent
     UpperCamelCase_									=     batch_size
     UpperCamelCase_									=     seq_length
     UpperCamelCase_									=     is_training
     UpperCamelCase_									=     use_input_mask
     UpperCamelCase_									=     use_token_type_ids
     UpperCamelCase_									=     use_labels
     UpperCamelCase_									=     vocab_size
     UpperCamelCase_									=     hidden_size
     UpperCamelCase_									=     num_hidden_layers
     UpperCamelCase_									=     num_attention_heads
     UpperCamelCase_									=     intermediate_multiple_size
     UpperCamelCase_									=     hidden_act
     UpperCamelCase_									=     hidden_dropout
     UpperCamelCase_									=     attention_dropout
     UpperCamelCase_									=     weight_tying
     UpperCamelCase_									=     max_position_embeddings
     UpperCamelCase_									=     type_vocab_size
     UpperCamelCase_									=     type_sequence_label_size
     UpperCamelCase_									=     initializer_range
     UpperCamelCase_									=     num_labels
     UpperCamelCase_									=     num_choices
     UpperCamelCase_									=     scope




  def    _UpperCAmelCase     (     self )			-> Optional[Any]:
     UpperCamelCase_									=     ids_tensor([self.batch_size, self.seq_length]		,		self.vocab_size )

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

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

     UpperCamelCase_									=     self.get_config()

     return config, input_ids, input_mask, token_labels




  def    _UpperCAmelCase     (     self )			-> Optional[int]:
     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=_UpperCAmelCase		,		initializer_range=self.initializer_range		,		)




  def    _UpperCAmelCase     (     self )			-> Optional[int]:
     UpperCamelCase_		,      UpperCamelCase_		,      UpperCamelCase_		,      UpperCamelCase_									=     self.prepare_config_and_inputs()

     UpperCamelCase_									=     True

     return config, input_ids, input_mask, token_labels




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> str:
     UpperCamelCase_									=     GPTNeoXJapaneseModel(config=_UpperCAmelCase )
     model.to(_UpperCAmelCase )
     model.eval()
     UpperCamelCase_									=     model(_UpperCAmelCase		,		attention_mask=_UpperCAmelCase )
     UpperCamelCase_									=     model(_UpperCAmelCase )
     self.parent.assertEqual(result.last_hidden_state.shape		,		(self.batch_size, self.seq_length, self.hidden_size) )




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> List[Any]:
     UpperCamelCase_									=     True
     UpperCamelCase_									=     GPTNeoXJapaneseModel(_UpperCAmelCase )
     model.to(_UpperCAmelCase )
     model.eval()
     UpperCamelCase_									=     model(_UpperCAmelCase		,		attention_mask=_UpperCAmelCase )
     self.parent.assertEqual(result.last_hidden_state.shape		,		(self.batch_size, self.seq_length, self.hidden_size) )




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




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> List[Any]:
     UpperCamelCase_									=     True
     UpperCamelCase_									=     GPTNeoXJapaneseForCausalLM(config=_UpperCAmelCase )
     model.to(_UpperCAmelCase )
     model.eval()

     # first forward pass
     UpperCamelCase_									=     model(_UpperCAmelCase		,		attention_mask=_UpperCAmelCase		,		use_cache=_UpperCAmelCase )
     UpperCamelCase_									=     outputs.past_key_values

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

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

     UpperCamelCase_									=     model(_UpperCAmelCase		,		attention_mask=_UpperCAmelCase		,		output_hidden_states=_UpperCAmelCase )
     UpperCamelCase_									=     output_from_no_past['hidden_states'][0]
     UpperCamelCase_									=     model(
         _UpperCAmelCase		,		attention_mask=_UpperCAmelCase		,		past_key_values=_UpperCAmelCase		,		output_hidden_states=_UpperCAmelCase		,		)['hidden_states'][0]

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




  def    _UpperCAmelCase     (     self )			-> Optional[int]:
     UpperCamelCase_									=     self.prepare_config_and_inputs()
     UpperCamelCase_		,      UpperCamelCase_		,      UpperCamelCase_		,      UpperCamelCase_									=     config_and_inputs
     UpperCamelCase_									=     {'input_ids': input_ids, 'attention_mask': input_mask}
     return config, inputs_dict




@require_torch
class 						_a       (  UpperCAmelCase__						,       UpperCAmelCase__						,       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    _UpperCAmelCase     (     self )			-> Optional[int]:
     UpperCamelCase_									=     GPTNeoXJapaneseModelTester(self )
     UpperCamelCase_									=     ConfigTester(self		,		config_class=_UpperCAmelCase		,		hidden_size=37 )




  def    _UpperCAmelCase     (     self )			-> Union[str, Any]:
     self.config_tester.run_common_tests()




  def    _UpperCAmelCase     (     self )			-> str:
     UpperCamelCase_		,      UpperCamelCase_		,      UpperCamelCase_		,      UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )




  def    _UpperCAmelCase     (     self )			-> Dict:
     UpperCamelCase_		,      UpperCamelCase_		,      UpperCamelCase_		,      UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_model_as_decoder(_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )




  def    _UpperCAmelCase     (     self )			-> Dict:
     # This regression test was failing with PyTorch < 1.3
     UpperCamelCase_		,      UpperCamelCase_		,      UpperCamelCase_		,      UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs_for_decoder()

     UpperCamelCase_									=     None

     self.model_tester.create_and_check_model_as_decoder(_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )




  def    _UpperCAmelCase     (     self )			-> List[str]:
     UpperCamelCase_		,      UpperCamelCase_		,      UpperCamelCase_		,      UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_decoder_model_past_large_inputs(_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )




  def    _UpperCAmelCase     (     self )			-> List[Any]:
     UpperCamelCase_									=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_causal_lm(*_UpperCAmelCase )




  @slow
  def    _UpperCAmelCase     (     self )			-> Tuple:
     UpperCamelCase_									=     'abeja/gpt-neox-japanese-2.7b'

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

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

     UpperCamelCase_									=     GPTNeoXJapaneseTokenizer.from_pretrained(_UpperCAmelCase )
     UpperCamelCase_									=     GPTNeoXJapaneseForCausalLM.from_pretrained(_UpperCAmelCase )

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













import copy
import os
from typing import Union

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


snake_case__      :	Optional[int]       	=      logging.get_logger(__name__)

snake_case__      :	List[Any]       	=      {
    """microsoft/git-base""": """https://huggingface.co/microsoft/git-base/resolve/main/config.json""",
}


class 						_a       (  UpperCAmelCase__    ):






  """simple docstring"""





  A_						=   """git_vision_model"""




  def __init__(     self		,		_UpperCAmelCase=768		,		_UpperCAmelCase=3072		,		_UpperCAmelCase=12		,		_UpperCAmelCase=12		,		_UpperCAmelCase=3		,		_UpperCAmelCase=224		,		_UpperCAmelCase=16		,		_UpperCAmelCase="quick_gelu"		,		_UpperCAmelCase=1e-5		,		_UpperCAmelCase=0.0		,		_UpperCAmelCase=0.0_2		,		**_UpperCAmelCase		,		)			-> Optional[Any]:
     super().__init__(**_UpperCAmelCase )

     UpperCamelCase_									=     hidden_size
     UpperCamelCase_									=     intermediate_size
     UpperCamelCase_									=     num_hidden_layers
     UpperCamelCase_									=     num_attention_heads
     UpperCamelCase_									=     num_channels
     UpperCamelCase_									=     patch_size
     UpperCamelCase_									=     image_size
     UpperCamelCase_									=     initializer_range
     UpperCamelCase_									=     attention_dropout
     UpperCamelCase_									=     layer_norm_eps
     UpperCamelCase_									=     hidden_act




  @classmethod
  def    _UpperCAmelCase     (     cls		,		_UpperCAmelCase		,		**_UpperCAmelCase )			-> "PretrainedConfig":
     cls._set_token_in_kwargs(_UpperCAmelCase )

     UpperCamelCase_		,      UpperCamelCase_									=     cls.get_config_dict(_UpperCAmelCase		,		**_UpperCAmelCase )

     # get the vision config dict if we are loading from GITConfig
     if config_dict.get('model_type' ) == "git":
        UpperCamelCase_									=     config_dict['vision_config']

     if "model_type" in config_dict and hasattr(cls		,		'model_type' ) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """
            f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""" )

     return cls.from_dict(_UpperCAmelCase		,		**_UpperCAmelCase )




class 						_a       (  UpperCAmelCase__    ):






  """simple docstring"""





  A_						=   """git"""




  def __init__(     self		,		_UpperCAmelCase=None		,		_UpperCAmelCase=30522		,		_UpperCAmelCase=768		,		_UpperCAmelCase=6		,		_UpperCAmelCase=12		,		_UpperCAmelCase=3072		,		_UpperCAmelCase="gelu"		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=1024		,		_UpperCAmelCase=0.0_2		,		_UpperCAmelCase=1e-12		,		_UpperCAmelCase=0		,		_UpperCAmelCase="absolute"		,		_UpperCAmelCase=True		,		_UpperCAmelCase=False		,		_UpperCAmelCase=101		,		_UpperCAmelCase=102		,		_UpperCAmelCase=None		,		**_UpperCAmelCase		,		)			-> Optional[int]:
     super().__init__(bos_token_id=_UpperCAmelCase		,		eos_token_id=_UpperCAmelCase		,		pad_token_id=_UpperCAmelCase		,		**_UpperCAmelCase )

     if vision_config is None:
        UpperCamelCase_									=     {}
        logger.info('vision_config is None. initializing the GitVisionConfig with default values.' )

     UpperCamelCase_									=     GitVisionConfig(**_UpperCAmelCase )
     UpperCamelCase_									=     vocab_size
     UpperCamelCase_									=     hidden_size
     UpperCamelCase_									=     num_hidden_layers
     UpperCamelCase_									=     num_attention_heads
     UpperCamelCase_									=     hidden_act
     UpperCamelCase_									=     intermediate_size
     UpperCamelCase_									=     hidden_dropout_prob
     UpperCamelCase_									=     attention_probs_dropout_prob
     UpperCamelCase_									=     max_position_embeddings
     UpperCamelCase_									=     initializer_range
     UpperCamelCase_									=     layer_norm_eps
     UpperCamelCase_									=     position_embedding_type
     UpperCamelCase_									=     use_cache
     UpperCamelCase_									=     tie_word_embeddings
     UpperCamelCase_									=     num_image_with_embedding

     UpperCamelCase_									=     bos_token_id
     UpperCamelCase_									=     eos_token_id




  def    _UpperCAmelCase     (     self )			-> str:
     UpperCamelCase_									=     copy.deepcopy(self.__dict__ )
     UpperCamelCase_									=     self.vision_config.to_dict()
     UpperCamelCase_									=     self.__class__.model_type
     return output





