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


   def __init__(					self    :       Union[str, Any]    ,  a    :       Optional[Any]    ,  a    :       Union[str, Any]=13    ,  a    :       int=7    ,  a    :       Dict=True    ,  a    :       Any=True    ,  a    :       int=True    ,  a    :       List[Any]=True    ,  a    :       Union[str, Any]=99    ,  a    :       str=32    ,  a    :       Any=5    ,  a    :       Dict=4    ,  a    :       str=4    ,  a    :       Optional[Any]="gelu"    ,  a    :       Optional[int]=0.0    ,  a    :       Dict=0.1    ,  a    :       Any=True    ,  a    :       Tuple=512    ,  a    :       Union[str, Any]=16    ,  a    :       str=2    ,  a    :       int=0.02    ,  a    :       Tuple=3    ,  a    :       Optional[Any]=4    ,  a    :       Union[str, Any]=None    ,  )->     Dict:





     """simple docstring"""







     lowercase__			    =						parent
     lowercase__			    =						batch_size
     lowercase__			    =						seq_length
     lowercase__			    =						is_training
     lowercase__			    =						use_input_mask
     lowercase__			    =						use_token_type_ids
     lowercase__			    =						use_labels
     lowercase__			    =						vocab_size
     lowercase__			    =						hidden_size
     lowercase__			    =						num_hidden_layers
     lowercase__			    =						num_attention_heads
     lowercase__			    =						intermediate_multiple_size
     lowercase__			    =						hidden_act
     lowercase__			    =						hidden_dropout
     lowercase__			    =						attention_dropout
     lowercase__			    =						weight_tying
     lowercase__			    =						max_position_embeddings
     lowercase__			    =						type_vocab_size
     lowercase__			    =						type_sequence_label_size
     lowercase__			    =						initializer_range
     lowercase__			    =						num_labels
     lowercase__			    =						num_choices
     lowercase__			    =						scope


   def  SCREAMING_SNAKE_CASE_							(					self    :       Optional[int] )->     List[Any]:





     """simple docstring"""







     lowercase__			    =						ids_tensor([self.batch_size, self.seq_length]    ,  self.vocab_size )

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

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

     lowercase__			    =						self.get_config()

     return config, input_ids, input_mask, token_labels


   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     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  SCREAMING_SNAKE_CASE_							(					self    :       int )->     int:





     """simple docstring"""







     lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__			    =						self.prepare_config_and_inputs()

     lowercase__			    =						True

     return config, input_ids, input_mask, token_labels


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any]    ,  a    :       str    ,  a    :       Optional[int]    ,  a    :       List[str] )->     Dict:





     """simple docstring"""







     lowercase__			    =						GPTNeoXJapaneseModel(config=A__ )
     model.to(A__ )
     model.eval()
     lowercase__			    =						model(A__    ,  attention_mask=A__ )
     lowercase__			    =						model(A__ )
     self.parent.assertEqual(result.last_hidden_state.shape    ,  (self.batch_size, self.seq_length, self.hidden_size) )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Optional[Any]    ,  a    :       Optional[int]    ,  a    :       Union[str, Any]    ,  a    :       str )->     str:





     """simple docstring"""







     lowercase__			    =						True
     lowercase__			    =						GPTNeoXJapaneseModel(A__ )
     model.to(A__ )
     model.eval()
     lowercase__			    =						model(A__    ,  attention_mask=A__ )
     self.parent.assertEqual(result.last_hidden_state.shape    ,  (self.batch_size, self.seq_length, self.hidden_size) )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Any    ,  a    :       str    ,  a    :       Union[str, Any]    ,  a    :       Any    ,  a    :       int )->     Any:





     """simple docstring"""







     lowercase__			    =						GPTNeoXJapaneseForCausalLM(config=A__ )
     model.to(A__ )
     model.eval()
     lowercase__			    =						model(A__    ,  attention_mask=A__    ,  labels=A__ )
     self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.seq_length, self.vocab_size) )


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any]    ,  a    :       Optional[Any]    ,  a    :       Tuple    ,  a    :       int )->     Optional[Any]:





     """simple docstring"""







     lowercase__			    =						True
     lowercase__			    =						GPTNeoXJapaneseForCausalLM(config=A__ )
     model.to(A__ )
     model.eval()

     # first forward pass
     lowercase__			    =						model(A__    ,  attention_mask=A__    ,  use_cache=A__ )
     lowercase__			    =						outputs.past_key_values

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

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

     lowercase__			    =						model(A__    ,  attention_mask=A__    ,  output_hidden_states=A__ )
     lowercase__			    =						output_from_no_past['hidden_states'][0]
     lowercase__			    =						model(
         A__    ,  attention_mask=A__    ,  past_key_values=A__    ,  output_hidden_states=A__    ,  )['hidden_states'][0]

     # select random slice
     lowercase__			    =						ids_tensor((1,)    ,  output_from_past.shape[-1] ).item()
     lowercase__			    =						output_from_no_past[:, -3:, random_slice_idx].detach()
     lowercase__			    =						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  SCREAMING_SNAKE_CASE_							(					self    :       int )->     int:





     """simple docstring"""







     lowercase__			    =						self.prepare_config_and_inputs()
     lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__			    =						config_and_inputs
     lowercase__			    =						{'input_ids': input_ids, 'attention_mask': input_mask}
     return config, inputs_dict






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


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





     """simple docstring"""







     lowercase__			    =						GPTNeoXJapaneseModelTester(self )
     lowercase__			    =						ConfigTester(self    ,  config_class=A__    ,  hidden_size=37 )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     Optional[int]:





     """simple docstring"""







     self.config_tester.run_common_tests()


   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     Dict:





     """simple docstring"""







     lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__			    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(A__    ,  A__    ,  A__ )


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





     """simple docstring"""







     lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__			    =						self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_model_as_decoder(A__    ,  A__    ,  A__ )


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





     """simple docstring"""







     lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__			    =						self.model_tester.prepare_config_and_inputs_for_decoder()

     lowercase__			    =						None

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


   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict )->     Dict:





     """simple docstring"""







     lowercase__    ,					lowercase__    ,					lowercase__    ,					lowercase__			    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_decoder_model_past_large_inputs(A__    ,  A__    ,  A__ )


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





     """simple docstring"""







     lowercase__			    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_causal_lm(*A__ )


   @slow
   def  SCREAMING_SNAKE_CASE_							(					self    :       Any )->     List[Any]:





     """simple docstring"""







     lowercase__			    =						'abeja/gpt-neox-japanese-2.7b'

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

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

     lowercase__			    =						GPTNeoXJapaneseTokenizer.from_pretrained(A__ )
     lowercase__			    =						GPTNeoXJapaneseForCausalLM.from_pretrained(A__ )

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









from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class        SCREAMING_SNAKE_CASE     (UpperCAmelCase   ):
   _UpperCamelCase					:	Tuple				      = 'ClapFeatureExtractor'
   _UpperCamelCase					:	Union[str, Any]				      = ('RobertaTokenizer', 'RobertaTokenizerFast')


   def __init__(					self    :       List[Any]    ,  a    :       int    ,  a    :       str )->     Any:





     """simple docstring"""







     super().__init__(a    ,  a )


   def __call__(					self    :       Any    ,  a    :       Tuple=None    ,  a    :       Optional[int]=None    ,  a    :       int=None    ,  **a    :       Optional[int] )->     Union[str, Any]:





     """simple docstring"""







     lowercase__			    =						kwargs.pop('sampling_rate'    ,  a )

     if text is None and audios is None:
       raise ValueError('You have to specify either text or audios. Both cannot be none.' )

     if text is not None:
       lowercase__			    =						self.tokenizer(a    ,  return_tensors=a    ,  **a )

     if audios is not None:
       lowercase__			    =						self.feature_extractor(
           a    ,  sampling_rate=a    ,  return_tensors=a    ,  **a )

     if text is not None and audios is not None:
       lowercase__			    =						audio_features.input_features
       return encoding
     elif text is not None:
       return encoding
     else:
       return BatchEncoding(data=dict(**a )    ,  tensor_type=a )


   def  SCREAMING_SNAKE_CASE_							(					self    :       str    ,  *a    :       Dict    ,  **a    :       int )->     Optional[int]:





     """simple docstring"""







     return self.tokenizer.batch_decode(*a    ,  **a )


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[Any]    ,  *a    :       int    ,  **a    :       Dict )->     Dict:





     """simple docstring"""







     return self.tokenizer.decode(*a    ,  **a )


   @property
   def  SCREAMING_SNAKE_CASE_							(					self    :       Union[str, Any] )->     Optional[int]:





     """simple docstring"""







     lowercase__			    =						self.tokenizer.model_input_names
     lowercase__			    =						self.feature_extractor.model_input_names
     return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names ) )


