


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






     def __init__(		self     :    int    , UpperCamelCase_     :    int    , UpperCamelCase_     :    Union[str, Any]=1_3    , UpperCamelCase_     :    List[str]=7    , UpperCamelCase_     :    List[str]=True    , UpperCamelCase_     :    Dict=True    , UpperCamelCase_     :    List[Any]=True    , UpperCamelCase_     :    Any=True    , UpperCamelCase_     :    Optional[Any]=9_9    , UpperCamelCase_     :    List[str]=3_2    , UpperCamelCase_     :    str=5    , UpperCamelCase_     :    Optional[Any]=4    , UpperCamelCase_     :    str=4    , UpperCamelCase_     :    Tuple="gelu"    , UpperCamelCase_     :    List[str]=0.0    , UpperCamelCase_     :    Optional[int]=0.1    , UpperCamelCase_     :    Optional[Any]=True    , UpperCamelCase_     :    Any=5_1_2    , UpperCamelCase_     :    Optional[int]=1_6    , UpperCamelCase_     :    Tuple=2    , UpperCamelCase_     :    Optional[Any]=0.02    , UpperCamelCase_     :    str=3    , UpperCamelCase_     :    int=4    , UpperCamelCase_     :    Union[str, Any]=None    , ):
           lowerCAmelCase    :		Optional[Any]				=      parent
           lowerCAmelCase    :		List[str]				=      batch_size
           lowerCAmelCase    :		str				=      seq_length
           lowerCAmelCase    :		int				=      is_training
           lowerCAmelCase    :		Optional[Any]				=      use_input_mask
           lowerCAmelCase    :		Optional[int]				=      use_token_type_ids
           lowerCAmelCase    :		Dict				=      use_labels
           lowerCAmelCase    :		Union[str, Any]				=      vocab_size
           lowerCAmelCase    :		int				=      hidden_size
           lowerCAmelCase    :		Tuple				=      num_hidden_layers
           lowerCAmelCase    :		int				=      num_attention_heads
           lowerCAmelCase    :		Dict				=      intermediate_multiple_size
           lowerCAmelCase    :		Optional[int]				=      hidden_act
           lowerCAmelCase    :		str				=      hidden_dropout
           lowerCAmelCase    :		int				=      attention_dropout
           lowerCAmelCase    :		Union[str, Any]				=      weight_tying
           lowerCAmelCase    :		Any				=      max_position_embeddings
           lowerCAmelCase    :		Any				=      type_vocab_size
           lowerCAmelCase    :		List[Any]				=      type_sequence_label_size
           lowerCAmelCase    :		int				=      initializer_range
           lowerCAmelCase    :		Tuple				=      num_labels
           lowerCAmelCase    :		Optional[Any]				=      num_choices
           lowerCAmelCase    :		List[str]				=      scope






     def    lowerCamelCase__   (		self     :    Tuple ):
           lowerCAmelCase    :		Optional[int]				=      ids_tensor([self.batch_size, self.seq_length]    , self.vocab_size )

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

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

           lowerCAmelCase    :		Any				=      self.get_config()

           return config, input_ids, input_mask, token_labels






     def    lowerCamelCase__   (		self     :    Dict ):
           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[Any] ):
           lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase    :		List[Any]				=      self.prepare_config_and_inputs()

           lowerCAmelCase    :		List[Any]				=      True

           return config, input_ids, input_mask, token_labels






     def    lowerCamelCase__   (		self     :    List[Any]    , UpperCamelCase_     :    Union[str, Any]    , UpperCamelCase_     :    Optional[Any]    , UpperCamelCase_     :    Optional[Any] ):
           lowerCAmelCase    :		Optional[int]				=      GPTNeoXJapaneseModel(config=UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()
           lowerCAmelCase    :		Tuple				=      model(UpperCamelCase_    , attention_mask=UpperCamelCase_ )
           lowerCAmelCase    :		Optional[Any]				=      model(UpperCamelCase_ )
           self.parent.assertEqual(result.last_hidden_state.shape    , (self.batch_size, self.seq_length, self.hidden_size) )






     def    lowerCamelCase__   (		self     :    str    , UpperCamelCase_     :    str    , UpperCamelCase_     :    int    , UpperCamelCase_     :    Optional[int] ):
           lowerCAmelCase    :		Dict				=      True
           lowerCAmelCase    :		Optional[Any]				=      GPTNeoXJapaneseModel(UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()
           lowerCAmelCase    :		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     :    Dict    , UpperCamelCase_     :    List[str]    , UpperCamelCase_     :    List[Any]    , UpperCamelCase_     :    str    , UpperCamelCase_     :    List[Any] ):
           lowerCAmelCase    :		List[Any]				=      GPTNeoXJapaneseForCausalLM(config=UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()
           lowerCAmelCase    :		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     :    Dict    , UpperCamelCase_     :    Tuple    , UpperCamelCase_     :    Any    , UpperCamelCase_     :    List[Any] ):
           lowerCAmelCase    :		Any				=      True
           lowerCAmelCase    :		Tuple				=      GPTNeoXJapaneseForCausalLM(config=UpperCamelCase_ )
           model.to(UpperCamelCase_ )
           model.eval()

           # first forward pass
           lowerCAmelCase    :		str				=      model(UpperCamelCase_    , attention_mask=UpperCamelCase_    , use_cache=UpperCamelCase_ )
           lowerCAmelCase    :		int				=      outputs.past_key_values

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

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

           lowerCAmelCase    :		List[Any]				=      model(UpperCamelCase_    , attention_mask=UpperCamelCase_    , output_hidden_states=UpperCamelCase_ )
           lowerCAmelCase    :		List[str]				=      output_from_no_past['''hidden_states'''][0]
           lowerCAmelCase    :		Tuple				=      model(
               UpperCamelCase_    , attention_mask=UpperCamelCase_    , past_key_values=UpperCamelCase_    , output_hidden_states=UpperCamelCase_    , )['''hidden_states'''][0]

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






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



@require_torch
class 						snake_case_(				a__							,    a__							,    unittest.TestCase	):
     __UpperCamelCase     =    (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
     __UpperCamelCase     =    (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
     __UpperCamelCase     =    (
         {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM}
         if is_torch_available()
         else {}
     )
     __UpperCamelCase     =    False
     __UpperCamelCase     =    False
     __UpperCamelCase     =    False
     __UpperCamelCase     =    False






     def    lowerCamelCase__   (		self     :    Union[str, Any] ):
           lowerCAmelCase    :		Union[str, Any]				=      GPTNeoXJapaneseModelTester(self )
           lowerCAmelCase    :		Union[str, Any]				=      ConfigTester(self    , config_class=UpperCamelCase_    , hidden_size=3_7 )






     def    lowerCamelCase__   (		self     :    int ):
           self.config_tester.run_common_tests()






     def    lowerCamelCase__   (		self     :    Dict ):
           lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase    :		str				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    Tuple ):
           lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase    :		List[str]				=      self.model_tester.prepare_config_and_inputs_for_decoder()
           self.model_tester.create_and_check_model_as_decoder(UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    List[str] ):
           # This regression test was failing with PyTorch < 1.3
           lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase    :		int				=      self.model_tester.prepare_config_and_inputs_for_decoder()

           lowerCAmelCase    :		Dict				=      None

           self.model_tester.create_and_check_model_as_decoder(UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    Union[str, Any] ):
           lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase,					lowerCAmelCase    :		Union[str, Any]				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_decoder_model_past_large_inputs(UpperCamelCase_    , UpperCamelCase_    , UpperCamelCase_ )






     def    lowerCamelCase__   (		self     :    List[Any] ):
           lowerCAmelCase    :		Union[str, Any]				=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_causal_lm(*UpperCamelCase_ )






     @slow
     def    lowerCamelCase__   (		self     :    List[str] ):
           lowerCAmelCase    :		Optional[int]				=      '''abeja/gpt-neox-japanese-2.7b'''

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

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

           lowerCAmelCase    :		int				=      GPTNeoXJapaneseTokenizer.from_pretrained(UpperCamelCase_ )
           lowerCAmelCase    :		Tuple				=      GPTNeoXJapaneseForCausalLM.from_pretrained(UpperCamelCase_ )

           lowerCAmelCase    :		List[Any]				=      []
           for prompt in prompts:
                 lowerCAmelCase    :		str				=      tokenizer(UpperCamelCase_    , return_tensors='''pt''' ).input_ids
                 lowerCAmelCase    :		Dict				=      model.generate(UpperCamelCase_    , max_length=5_0 )
                 lowerCAmelCase    :		Any				=      tokenizer.batch_decode(UpperCamelCase_    , skip_special_tokens=UpperCamelCase_ )
                 predicted_outputs += generated_string
           self.assertListEqual(UpperCamelCase_    , UpperCamelCase_ )






'''simple docstring'''


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


UpperCAmelCase_		:   Dict							   =		logging.get_logger(__name__)

UpperCAmelCase_		:   Optional[int]							   =		{
    'google/canine-s': 'https://huggingface.co/google/canine-s/resolve/main/config.json',
    # See all CANINE models at https://huggingface.co/models?filter=canine
}





class     lowercase__  (						_snake_case					):





							'''simple docstring'''


							A_							:      Optional[int]      	=  """canine"""






							def __init__(    self    ,					__snake_case=768    ,					__snake_case=12    ,					__snake_case=12    ,					__snake_case=3072    ,					__snake_case="gelu"    ,					__snake_case=0.1    ,					__snake_case=0.1    ,					__snake_case=1_6384    ,					__snake_case=16    ,					__snake_case=0.02    ,					__snake_case=1e-12    ,					__snake_case=0    ,					__snake_case=0XE000    ,					__snake_case=0XE001    ,					__snake_case=4    ,					__snake_case=4    ,					__snake_case=8    ,					__snake_case=1_6384    ,					__snake_case=128    ,					**__snake_case    ,					):
											super().__init__(pad_token_id=__snake_case    ,					bos_token_id=__snake_case    ,					eos_token_id=__snake_case    ,					**__snake_case  )

											_SCREAMING_SNAKE_CASE :       str     		=      max_position_embeddings
											_SCREAMING_SNAKE_CASE :       Any     		=      hidden_size
											_SCREAMING_SNAKE_CASE :       Dict     		=      num_hidden_layers
											_SCREAMING_SNAKE_CASE :       Tuple     		=      num_attention_heads
											_SCREAMING_SNAKE_CASE :       List[str]     		=      intermediate_size
											_SCREAMING_SNAKE_CASE :       Tuple     		=      hidden_act
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      hidden_dropout_prob
											_SCREAMING_SNAKE_CASE :       str     		=      attention_probs_dropout_prob
											_SCREAMING_SNAKE_CASE :       Dict     		=      initializer_range
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      type_vocab_size
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      layer_norm_eps

											# Character config:
											_SCREAMING_SNAKE_CASE :       Any     		=      downsampling_rate
											_SCREAMING_SNAKE_CASE :       List[str]     		=      upsampling_kernel_size
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      num_hash_functions
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      num_hash_buckets
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      local_transformer_stride




