

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    : str				,				UpperCAmelCase__    : Tuple				,				UpperCAmelCase__    : List[Any]=1_3				,				UpperCAmelCase__    : str=7				,				UpperCAmelCase__    : Union[str, Any]=True				,				UpperCAmelCase__    : Any=True				,				UpperCAmelCase__    : Optional[Any]=True				,				UpperCAmelCase__    : Any=True				,				UpperCAmelCase__    : Union[str, Any]=9_9				,				UpperCAmelCase__    : Optional[int]=3_2				,				UpperCAmelCase__    : Any=5				,				UpperCAmelCase__    : str=4				,				UpperCAmelCase__    : Union[str, Any]=4				,				UpperCAmelCase__    : str="gelu"				,				UpperCAmelCase__    : Union[str, Any]=0.0				,				UpperCAmelCase__    : str=0.1				,				UpperCAmelCase__    : int=True				,				UpperCAmelCase__    : List[Any]=5_1_2				,				UpperCAmelCase__    : int=1_6				,				UpperCAmelCase__    : List[str]=2				,				UpperCAmelCase__    : Optional[int]=0.02				,				UpperCAmelCase__    : List[Any]=3				,				UpperCAmelCase__    : List[str]=4				,				UpperCAmelCase__    : List[str]=None				,				)					->Dict:
								"""simple docstring"""







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


		def  _lowercase      (			self    : Optional[int]		)					->List[Any]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      int       =   ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size		)

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

								SCREAMING_SNAKE_CASE :      str       =   None
								if self.use_labels:
														SCREAMING_SNAKE_CASE :      int       =   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  _lowercase      (			self    : Optional[int]		)					->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=A__				,				initializer_range=self.initializer_range				,				)


		def  _lowercase      (			self    : Optional[Any]		)					->int:
								"""simple docstring"""







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

								SCREAMING_SNAKE_CASE :      Dict       =   True

								return config, input_ids, input_mask, token_labels


		def  _lowercase      (			self    : List[str]				,				UpperCAmelCase__    : str				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : Dict		)					->Dict:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Any       =   GPTNeoXJapaneseModel(config=A__		)
								model.to(A__		)
								model.eval()
								SCREAMING_SNAKE_CASE :      str       =   model(A__				,				attention_mask=A__		)
								SCREAMING_SNAKE_CASE :      int       =   model(A__		)
								self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size)		)


		def  _lowercase      (			self    : int				,				UpperCAmelCase__    : Optional[int]				,				UpperCAmelCase__    : Dict				,				UpperCAmelCase__    : Optional[Any]		)					->str:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Any       =   True
								SCREAMING_SNAKE_CASE :      Optional[int]       =   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  _lowercase      (			self    : Union[str, Any]				,				UpperCAmelCase__    : Optional[Any]				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : List[str]		)					->Any:
								"""simple docstring"""







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


		def  _lowercase      (			self    : int				,				UpperCAmelCase__    : Tuple				,				UpperCAmelCase__    : Dict				,				UpperCAmelCase__    : List[Any]		)					->Optional[Any]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Tuple       =   True
								SCREAMING_SNAKE_CASE :      Optional[Any]       =   GPTNeoXJapaneseForCausalLM(config=A__		)
								model.to(A__		)
								model.eval()

								# first forward pass
								SCREAMING_SNAKE_CASE :      Tuple       =   model(A__				,				attention_mask=A__				,				use_cache=A__		)
								SCREAMING_SNAKE_CASE :      Dict       =   outputs.past_key_values

								# create hypothetical multiple next token and extent to next_input_ids
								SCREAMING_SNAKE_CASE :      Optional[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 :      Optional[int]       =   torch.cat([input_ids, next_tokens]				,				dim=-1		)
								SCREAMING_SNAKE_CASE :      List[str]       =   torch.cat([input_mask, next_mask]				,				dim=-1		)

								SCREAMING_SNAKE_CASE :      Optional[int]       =   model(A__				,				attention_mask=A__				,				output_hidden_states=A__		)
								SCREAMING_SNAKE_CASE :      Optional[Any]       =   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 :      Any       =   ids_tensor((1,)				,				output_from_past.shape[-1]		).item()
								SCREAMING_SNAKE_CASE :      Tuple       =   output_from_no_past[:, -3:, random_slice_idx].detach()
								SCREAMING_SNAKE_CASE :      Union[str, 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  _lowercase      (			self    : str		)					->int:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      List[str]       =   self.prepare_config_and_inputs()
								SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      Dict       =   config_and_inputs
								SCREAMING_SNAKE_CASE :      Optional[Any]       =   {"""input_ids""": input_ids, """attention_mask""": input_mask}
								return config, inputs_dict







@require_torch
class        a__       (   _lowerCamelCase	,			_lowerCamelCase	,			unittest.TestCase       ):







		"""simple docstring"""




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


		def  _lowercase      (			self    : Tuple		)					->List[str]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Optional[Any]       =   GPTNeoXJapaneseModelTester(self		)
								SCREAMING_SNAKE_CASE :      str       =   ConfigTester(self				,				config_class=A__				,				hidden_size=3_7		)


		def  _lowercase      (			self    : int		)					->Optional[int]:
								"""simple docstring"""







								self.config_tester.run_common_tests()


		def  _lowercase      (			self    : Any		)					->Dict:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      Dict       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(A__				,				A__				,				A__		)


		def  _lowercase      (			self    : Dict		)					->int:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      Optional[Any]       =   self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_model_as_decoder(A__				,				A__				,				A__		)


		def  _lowercase      (			self    : Any		)					->List[Any]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      List[str]       =   self.model_tester.prepare_config_and_inputs_for_decoder()

								SCREAMING_SNAKE_CASE :      str       =   None

								self.model_tester.create_and_check_model_as_decoder(A__				,				A__				,				A__		)


		def  _lowercase      (			self    : int		)					->Dict:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE     ,	SCREAMING_SNAKE_CASE :      int       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_decoder_model_past_large_inputs(A__				,				A__				,				A__		)


		def  _lowercase      (			self    : Optional[Any]		)					->List[Any]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      Optional[int]       =   self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_causal_lm(*A__		)




		@slow
		def  _lowercase      (			self    : Any		)					->List[Any]:
								"""simple docstring"""







								SCREAMING_SNAKE_CASE :      List[str]       =   """abeja/gpt-neox-japanese-2.7b"""

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

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

								SCREAMING_SNAKE_CASE :      Union[str, Any]       =   GPTNeoXJapaneseTokenizer.from_pretrained(A__		)
								SCREAMING_SNAKE_CASE :      List[str]       =   GPTNeoXJapaneseForCausalLM.from_pretrained(A__		)

								SCREAMING_SNAKE_CASE :      List[str]       =   []
								for prompt in prompts:
														SCREAMING_SNAKE_CASE :      Union[str, Any]       =   tokenizer(A__				,				return_tensors="""pt"""		).input_ids
														SCREAMING_SNAKE_CASE :      Any       =   model.generate(A__				,				max_length=5_0		)
														SCREAMING_SNAKE_CASE :      Union[str, Any]       =   tokenizer.batch_decode(A__				,				skip_special_tokens=A__		)
														predicted_outputs += generated_string
								self.assertListEqual(A__				,				A__		)







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


UpperCAmelCase__ :     List[Any]				    =    logging.get_logger(__name__)

UpperCAmelCase__ :     int				    =    {
    """unc-nlp/lxmert-base-uncased""": """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/config.json""",
}







class        a__       (   UpperCAmelCase       ):







  """simple docstring"""




  UpperCAmelCase__      : List[str]			       ="""lxmert"""
  UpperCAmelCase__      : str			       ={}


  def __init__(			self    : int				,				UpperCAmelCase__    : Dict=3_0_5_2_2				,				UpperCAmelCase__    : int=7_6_8				,				UpperCAmelCase__    : Union[str, Any]=1_2				,				UpperCAmelCase__    : int=9_5_0_0				,				UpperCAmelCase__    : Union[str, Any]=1_6_0_0				,				UpperCAmelCase__    : Optional[Any]=4_0_0				,				UpperCAmelCase__    : List[str]=3_0_7_2				,				UpperCAmelCase__    : List[Any]="gelu"				,				UpperCAmelCase__    : int=0.1				,				UpperCAmelCase__    : Optional[Any]=0.1				,				UpperCAmelCase__    : Union[str, Any]=5_1_2				,				UpperCAmelCase__    : int=2				,				UpperCAmelCase__    : List[str]=0.02				,				UpperCAmelCase__    : str=1e-12				,				UpperCAmelCase__    : int=9				,				UpperCAmelCase__    : Optional[int]=5				,				UpperCAmelCase__    : int=5				,				UpperCAmelCase__    : List[str]=2_0_4_8				,				UpperCAmelCase__    : Tuple=4				,				UpperCAmelCase__    : List[str]=6.67				,				UpperCAmelCase__    : Optional[int]=True				,				UpperCAmelCase__    : Dict=True				,				UpperCAmelCase__    : Optional[Any]=True				,				UpperCAmelCase__    : Optional[int]=True				,				UpperCAmelCase__    : List[Any]=True				,				UpperCAmelCase__    : Dict=True				,				UpperCAmelCase__    : Optional[Any]=True				,				**UpperCAmelCase__    : Tuple				,				)					->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[str]       =   vocab_size
        SCREAMING_SNAKE_CASE :      List[Any]       =   hidden_size
        SCREAMING_SNAKE_CASE :      Any       =   num_attention_heads
        SCREAMING_SNAKE_CASE :      str       =   hidden_act
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   intermediate_size
        SCREAMING_SNAKE_CASE :      List[str]       =   hidden_dropout_prob
        SCREAMING_SNAKE_CASE :      str       =   attention_probs_dropout_prob
        SCREAMING_SNAKE_CASE :      Dict       =   max_position_embeddings
        SCREAMING_SNAKE_CASE :      Any       =   type_vocab_size
        SCREAMING_SNAKE_CASE :      Optional[int]       =   initializer_range
        SCREAMING_SNAKE_CASE :      Optional[int]       =   layer_norm_eps
        SCREAMING_SNAKE_CASE :      Dict       =   num_qa_labels
        SCREAMING_SNAKE_CASE :      str       =   num_object_labels
        SCREAMING_SNAKE_CASE :      str       =   num_attr_labels
        SCREAMING_SNAKE_CASE :      List[str]       =   l_layers
        SCREAMING_SNAKE_CASE :      Optional[int]       =   x_layers
        SCREAMING_SNAKE_CASE :      List[str]       =   r_layers
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   visual_feat_dim
        SCREAMING_SNAKE_CASE :      Dict       =   visual_pos_dim
        SCREAMING_SNAKE_CASE :      List[Any]       =   visual_loss_normalizer
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   task_matched
        SCREAMING_SNAKE_CASE :      int       =   task_mask_lm
        SCREAMING_SNAKE_CASE :      int       =   task_obj_predict
        SCREAMING_SNAKE_CASE :      Optional[int]       =   task_qa
        SCREAMING_SNAKE_CASE :      Any       =   visual_obj_loss
        SCREAMING_SNAKE_CASE :      str       =   visual_attr_loss
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   visual_feat_loss
        SCREAMING_SNAKE_CASE :      List[Any]       =   {"""vision""": r_layers, """cross_encoder""": x_layers, """language""": l_layers}
        super().__init__(**UpperCAmelCase__		)




