


"""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 _lowerCAmelCase				:







   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.02						,	UpperCamelCase__=3						,	UpperCamelCase__=4						,	UpperCamelCase__=None						,	)	->		List[Any]:

       '''simple docstring'''




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







   def  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




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

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

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

       snake_case    :      Dict      					=    self.get_config()

       return config, input_ids, input_mask, token_labels







   def  lowerCamelCase		(	self						)	->		Union[str, 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=UpperCamelCase__						,	initializer_range=self.initializer_range						,	)







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

       '''simple docstring'''




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

       snake_case    :      Tuple      					=    True

       return config, input_ids, input_mask, token_labels







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)	->		int:

       '''simple docstring'''




       snake_case    :      Dict      					=    GPTNeoXJapaneseModel(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case    :      int      					=    model(UpperCamelCase__						,	attention_mask=UpperCamelCase__						)
       snake_case    :      List[str]      					=    model(UpperCamelCase__						)
       self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)						)







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)	->		int:

       '''simple docstring'''




       snake_case    :      Optional[int]      					=    True
       snake_case    :      Optional[int]      					=    GPTNeoXJapaneseModel(UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()
       snake_case    :      List[Any]      					=    model(UpperCamelCase__						,	attention_mask=UpperCamelCase__						)
       self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)						)







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)	->		List[Any]:

       '''simple docstring'''




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







   def  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)	->		Any:

       '''simple docstring'''




       snake_case    :      Any      					=    True
       snake_case    :      Optional[Any]      					=    GPTNeoXJapaneseForCausalLM(config=UpperCamelCase__						)
       model.to(UpperCamelCase__						)
       model.eval()

       # first forward pass
       snake_case    :      Dict      					=    model(UpperCamelCase__						,	attention_mask=UpperCamelCase__						,	use_cache=UpperCamelCase__						)
       snake_case    :      Any      					=    outputs.past_key_values

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

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

       snake_case    :      Any      					=    model(UpperCamelCase__						,	attention_mask=UpperCamelCase__						,	output_hidden_states=UpperCamelCase__						)
       snake_case    :      List[Any]      					=    output_from_no_past["hidden_states"][0]
       snake_case    :      int      					=    model(
           UpperCamelCase__						,	attention_mask=UpperCamelCase__						,	past_key_values=UpperCamelCase__						,	output_hidden_states=UpperCamelCase__						,	)["hidden_states"][0]

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







   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




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







@require_torch
class _lowerCAmelCase				(      snake_case_    ,							snake_case_    ,							unittest.TestCase	):
   __UpperCAmelCase :       Optional[Any]							  =  (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
   __UpperCAmelCase :       int							  =  (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
   __UpperCAmelCase :       Any							  =  (
       {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM}
       if is_torch_available()
       else {}
   )
   __UpperCAmelCase :       Tuple							  =  False
   __UpperCAmelCase :       Optional[int]							  =  False
   __UpperCAmelCase :       Optional[Any]							  =  False
   __UpperCAmelCase :       str							  =  False







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

       '''simple docstring'''




       snake_case    :      Dict      					=    GPTNeoXJapaneseModelTester(self						)
       snake_case    :      Union[str, Any]      					=    ConfigTester(self						,	config_class=UpperCamelCase__						,	hidden_size=37						)







   def  lowerCamelCase		(	self						)	->		List[Any]:

       '''simple docstring'''




       self.config_tester.run_common_tests()







   def  lowerCamelCase		(	self						)	->		Dict:

       '''simple docstring'''




       snake_case					,snake_case					,snake_case					,snake_case    :      Union[str, Any]      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)







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

       '''simple docstring'''




       snake_case					,snake_case					,snake_case					,snake_case    :      Dict      					=    self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_model_as_decoder(UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




       snake_case					,snake_case					,snake_case					,snake_case    :      Union[str, Any]      					=    self.model_tester.prepare_config_and_inputs_for_decoder()

       snake_case    :      Any      					=    None

       self.model_tester.create_and_check_model_as_decoder(UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)







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

       '''simple docstring'''




       snake_case					,snake_case					,snake_case					,snake_case    :      Dict      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_decoder_model_past_large_inputs(UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




       snake_case    :      Optional[int]      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_causal_lm(*UpperCamelCase__						)







   @slow
   def  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




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

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

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

       snake_case    :      Any      					=    GPTNeoXJapaneseTokenizer.from_pretrained(UpperCamelCase__						)
       snake_case    :      Optional[int]      					=    GPTNeoXJapaneseForCausalLM.from_pretrained(UpperCamelCase__						)

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









"""simple docstring"""



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__snake_case   			=    logging.get_logger(__name__)

__snake_case   			=    {
    """facebook/xmod-base""": """https://huggingface.co/facebook/xmod-base/resolve/main/config.json""",
    """facebook/xmod-large-prenorm""": """https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json""",
    """facebook/xmod-base-13-125k""": """https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json""",
    """facebook/xmod-base-30-125k""": """https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json""",
    """facebook/xmod-base-30-195k""": """https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json""",
    """facebook/xmod-base-60-125k""": """https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json""",
    """facebook/xmod-base-60-265k""": """https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json""",
    """facebook/xmod-base-75-125k""": """https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json""",
    """facebook/xmod-base-75-269k""": """https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json""",
}







class _lowerCAmelCase				(      snake_case_	):
   __UpperCAmelCase :       Optional[Any]							  =  '''xmod'''







   def __init__(	self						,	UpperCamelCase__=3_0522						,	UpperCamelCase__=768						,	UpperCamelCase__=12						,	UpperCamelCase__=12						,	UpperCamelCase__=3072						,	UpperCamelCase__="gelu"						,	UpperCamelCase__=0.1						,	UpperCamelCase__=0.1						,	UpperCamelCase__=512						,	UpperCamelCase__=2						,	UpperCamelCase__=0.02						,	UpperCamelCase__=1e-12						,	UpperCamelCase__=1						,	UpperCamelCase__=0						,	UpperCamelCase__=2						,	UpperCamelCase__="absolute"						,	UpperCamelCase__=True						,	UpperCamelCase__=None						,	UpperCamelCase__=False						,	UpperCamelCase__=2						,	UpperCamelCase__=False						,	UpperCamelCase__=True						,	UpperCamelCase__=True						,	UpperCamelCase__=("en_XX",)						,	UpperCamelCase__=None						,	**UpperCamelCase__						,	)	->		int:

       '''simple docstring'''




       super().__init__(pad_token_id=UpperCamelCase__						,	bos_token_id=UpperCamelCase__						,	eos_token_id=UpperCamelCase__						,	**UpperCamelCase__						)

       snake_case    :      List[Any]      					=    vocab_size
       snake_case    :      List[Any]      					=    hidden_size
       snake_case    :      Union[str, Any]      					=    num_hidden_layers
       snake_case    :      Any      					=    num_attention_heads
       snake_case    :      List[str]      					=    hidden_act
       snake_case    :      Union[str, Any]      					=    intermediate_size
       snake_case    :      int      					=    hidden_dropout_prob
       snake_case    :      Any      					=    attention_probs_dropout_prob
       snake_case    :      Optional[int]      					=    max_position_embeddings
       snake_case    :      Tuple      					=    type_vocab_size
       snake_case    :      List[str]      					=    initializer_range
       snake_case    :      int      					=    layer_norm_eps
       snake_case    :      Optional[Any]      					=    position_embedding_type
       snake_case    :      int      					=    use_cache
       snake_case    :      Dict      					=    classifier_dropout
       snake_case    :      Dict      					=    pre_norm
       snake_case    :      Union[str, Any]      					=    adapter_reduction_factor
       snake_case    :      Any      					=    adapter_layer_norm
       snake_case    :      Optional[int]      					=    adapter_reuse_layer_norm
       snake_case    :      List[Any]      					=    ln_before_adapter
       snake_case    :      str      					=    list(UpperCamelCase__						)
       snake_case    :      int      					=    default_language







class _lowerCAmelCase				(      snake_case_	):







   @property
   def  lowerCamelCase		(	self						)	->		Mapping[str, Mapping[int, str]]:

       '''simple docstring'''




       if self.task == "multiple-choice":
           snake_case    :      Optional[int]      					=    {0: "batch", 1: "choice", 2: "sequence"}
       else:
           snake_case    :      Optional[Any]      					=    {0: "batch", 1: "sequence"}
       return OrderedDict(
           [
               ("input_ids", dynamic_axis),
               ("attention_mask", dynamic_axis),
           ]						)





