



'''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 UpperCAmelCase_					:




      """simple docstring"""




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


       '''simple docstring'''

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






      def 				A__      ( self			:		Tuple		)  ->  List[Any]:


       '''simple docstring'''

       lowercase					:   List[Any]				  =ids_tensor([self.batch_size, self.seq_length]     ,      self.vocab_size		)

       lowercase					:   Dict				  =None
       if self.use_input_mask:
        lowercase					:   List[Any]				  =random_attention_mask([self.batch_size, self.seq_length]		)

       lowercase					:   Union[str, Any]				  =None
       if self.use_labels:
        lowercase					:   int				  =ids_tensor([self.batch_size, self.seq_length]     ,      self.num_labels		)

       lowercase					:   List[Any]				  =self.get_config()

       return config, input_ids, input_mask, token_labels






      def 				A__      ( 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 				A__      ( self			:		List[str]		)  ->  Any:


       '''simple docstring'''

       lowercase  ,		lowercase  ,		lowercase  ,		lowercase					:   Any				  =self.prepare_config_and_inputs()

       lowercase					:   Optional[int]				  =True

       return config, input_ids, input_mask, token_labels






      def 				A__      ( self			:		Optional[int]     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		List[Any]		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   Optional[int]				  =GPTNeoXJapaneseModel(config=A_		)
       model.to(A_		)
       model.eval()
       lowercase					:   Any				  =model(A_     ,      attention_mask=A_		)
       lowercase					:   Dict				  =model(A_		)
       self.parent.assertEqual(result.last_hidden_state.shape     ,      (self.batch_size, self.seq_length, self.hidden_size)		)






      def 				A__      ( self			:		str     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		int		)  ->  Any:


       '''simple docstring'''

       lowercase					:   List[Any]				  =True
       lowercase					:   List[Any]				  =GPTNeoXJapaneseModel(A_		)
       model.to(A_		)
       model.eval()
       lowercase					:   Union[str, Any]				  =model(A_     ,      attention_mask=A_		)
       self.parent.assertEqual(result.last_hidden_state.shape     ,      (self.batch_size, self.seq_length, self.hidden_size)		)






      def 				A__      ( self			:		str     ,      UpperCAmelCase			:		Dict     ,      UpperCAmelCase			:		Any     ,      UpperCAmelCase			:		Tuple     ,      UpperCAmelCase			:		List[str]		)  ->  Any:


       '''simple docstring'''

       lowercase					:   Tuple				  =GPTNeoXJapaneseForCausalLM(config=A_		)
       model.to(A_		)
       model.eval()
       lowercase					:   List[str]				  =model(A_     ,      attention_mask=A_     ,      labels=A_		)
       self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.seq_length, self.vocab_size)		)






      def 				A__      ( self			:		List[Any]     ,      UpperCAmelCase			:		List[Any]     ,      UpperCAmelCase			:		Optional[int]     ,      UpperCAmelCase			:		Optional[Any]		)  ->  List[Any]:


       '''simple docstring'''

       lowercase					:   Any				  =True
       lowercase					:   List[str]				  =GPTNeoXJapaneseForCausalLM(config=A_		)
       model.to(A_		)
       model.eval()

       # first forward pass
       lowercase					:   str				  =model(A_     ,      attention_mask=A_     ,      use_cache=A_		)
       lowercase					:   Optional[int]				  =outputs.past_key_values

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

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

       lowercase					:   Union[str, Any]				  =model(A_     ,      attention_mask=A_     ,      output_hidden_states=A_		)
       lowercase					:   str				  =output_from_no_past['''hidden_states'''][0]
       lowercase					:   Any				  =model(
           A_     ,      attention_mask=A_     ,      past_key_values=A_     ,      output_hidden_states=A_     ,      )['''hidden_states'''][0]

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


       '''simple docstring'''

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





@require_torch
class UpperCAmelCase_					(    _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       unittest.TestCase				):




      """simple docstring"""




      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 				A__      ( self			:		List[Any]		)  ->  List[Any]:


       '''simple docstring'''

       lowercase					:   List[str]				  =GPTNeoXJapaneseModelTester(self		)
       lowercase					:   List[Any]				  =ConfigTester(self     ,      config_class=A_     ,      hidden_size=37		)






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


       '''simple docstring'''

       self.config_tester.run_common_tests()






      def 				A__      ( self			:		Dict		)  ->  str:


       '''simple docstring'''

       lowercase  ,		lowercase  ,		lowercase  ,		lowercase					:   List[str]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(A_     ,      A_     ,      A_		)






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


       '''simple docstring'''

       lowercase  ,		lowercase  ,		lowercase  ,		lowercase					:   Union[str, Any]				  =self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_model_as_decoder(A_     ,      A_     ,      A_		)






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


       '''simple docstring'''

       lowercase  ,		lowercase  ,		lowercase  ,		lowercase					:   List[Any]				  =self.model_tester.prepare_config_and_inputs_for_decoder()

       lowercase					:   List[Any]				  =None

       self.model_tester.create_and_check_model_as_decoder(A_     ,      A_     ,      A_		)






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


       '''simple docstring'''

       lowercase  ,		lowercase  ,		lowercase  ,		lowercase					:   Optional[int]				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_decoder_model_past_large_inputs(A_     ,      A_     ,      A_		)






      def 				A__      ( self			:		Optional[Any]		)  ->  List[str]:


       '''simple docstring'''

       lowercase					:   str				  =self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_causal_lm(*A_		)






      @slow
      def 				A__      ( self			:		Optional[Any]		)  ->  Union[str, Any]:


       '''simple docstring'''

       lowercase					:   List[Any]				  ='''abeja/gpt-neox-japanese-2.7b'''

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

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

       lowercase					:   Union[str, Any]				  =GPTNeoXJapaneseTokenizer.from_pretrained(A_		)
       lowercase					:   int				  =GPTNeoXJapaneseForCausalLM.from_pretrained(A_		)

       lowercase					:   Tuple				  =[]
       for prompt in prompts:
        lowercase					:   int				  =tokenizer(A_     ,      return_tensors='''pt'''		).input_ids
        lowercase					:   Tuple				  =model.generate(A_     ,      max_length=50		)
        lowercase					:   List[Any]				  =tokenizer.batch_decode(A_     ,      skip_special_tokens=A_		)
        predicted_outputs += generated_string
       self.assertListEqual(A_     ,      A_		)











'''simple docstring'''



import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch
class UpperCAmelCase_					(    __A				):




      """simple docstring"""




      @require_torch
      def 				A__      ( self			:		Tuple		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   Any				  ='''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

       lowercase					:   Optional[int]				  ='''
mname = "hf-internal-testing/tiny-random-bert"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task="fill-mask", model=mname)
print("success")
        '''

       lowercase					:   Any				  ='''
import socket
def offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")
socket.socket = offline_socket
        '''

       # Force fetching the files so that we can use the cache
       lowercase					:   Tuple				  ='''hf-internal-testing/tiny-random-bert'''
       BertConfig.from_pretrained(UpperCAmelCase		)
       BertModel.from_pretrained(UpperCAmelCase		)
       BertTokenizer.from_pretrained(UpperCAmelCase		)
       pipeline(task='''fill-mask'''     ,      model=UpperCAmelCase		)

       # baseline - just load from_pretrained with normal network
       lowercase					:   List[str]				  =[sys.executable, '''-c''', '''\n'''.join([load, run, mock]		)]

       # should succeed
       lowercase					:   Tuple				  =self.get_env()
       # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
       lowercase					:   Optional[Any]				  ='''1'''
       lowercase					:   Any				  =subprocess.run(UpperCAmelCase     ,      env=UpperCAmelCase     ,      check=UpperCAmelCase     ,      capture_output=UpperCAmelCase		)
       self.assertEqual(result.returncode     ,      0     ,      result.stderr		)
       self.assertIn('''success'''     ,      result.stdout.decode()		)






      @require_torch
      def 				A__      ( self			:		str		)  ->  List[str]:


       '''simple docstring'''

       lowercase					:   str				  ='''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

       lowercase					:   Optional[Any]				  ='''
mname = "hf-internal-testing/tiny-random-bert"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task="fill-mask", model=mname)
print("success")
        '''

       lowercase					:   Optional[int]				  ='''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")
socket.socket = offline_socket
        '''

       # Force fetching the files so that we can use the cache
       lowercase					:   Optional[Any]				  ='''hf-internal-testing/tiny-random-bert'''
       BertConfig.from_pretrained(UpperCAmelCase		)
       BertModel.from_pretrained(UpperCAmelCase		)
       BertTokenizer.from_pretrained(UpperCAmelCase		)
       pipeline(task='''fill-mask'''     ,      model=UpperCAmelCase		)

       # baseline - just load from_pretrained with normal network
       lowercase					:   Optional[Any]				  =[sys.executable, '''-c''', '''\n'''.join([load, run, mock]		)]

       # should succeed
       lowercase					:   str				  =self.get_env()
       lowercase					:   Any				  =subprocess.run(UpperCAmelCase     ,      env=UpperCAmelCase     ,      check=UpperCAmelCase     ,      capture_output=UpperCAmelCase		)
       self.assertEqual(result.returncode     ,      0     ,      result.stderr		)
       self.assertIn('''success'''     ,      result.stdout.decode()		)






      @require_torch
      def 				A__      ( self			:		Any		)  ->  Optional[Any]:


       '''simple docstring'''

       lowercase					:   Optional[Any]				  ='''
from transformers import BertConfig, BertModel, BertTokenizer
        '''

       lowercase					:   List[Any]				  ='''
mname = "hf-internal-testing/tiny-random-bert-sharded"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
print("success")
        '''

       lowercase					:   int				  ='''
import socket
def offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")
socket.socket = offline_socket
        '''

       # baseline - just load from_pretrained with normal network
       lowercase					:   Tuple				  =[sys.executable, '''-c''', '''\n'''.join([load, run]		)]

       # should succeed
       lowercase					:   Optional[Any]				  =self.get_env()
       lowercase					:   List[Any]				  =subprocess.run(UpperCAmelCase     ,      env=UpperCAmelCase     ,      check=UpperCAmelCase     ,      capture_output=UpperCAmelCase		)
       self.assertEqual(result.returncode     ,      0     ,      result.stderr		)
       self.assertIn('''success'''     ,      result.stdout.decode()		)

       # next emulate no network
       lowercase					:   Tuple				  =[sys.executable, '''-c''', '''\n'''.join([load, mock, run]		)]

       # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
       # env["TRANSFORMERS_OFFLINE"] = "0"
       # result = subprocess.run(cmd, env=env, check=False, capture_output=True)
       # self.assertEqual(result.returncode, 1, result.stderr)

       # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
       lowercase					:   Any				  ='''1'''
       lowercase					:   Optional[Any]				  =subprocess.run(UpperCAmelCase     ,      env=UpperCAmelCase     ,      check=UpperCAmelCase     ,      capture_output=UpperCAmelCase		)
       self.assertEqual(result.returncode     ,      0     ,      result.stderr		)
       self.assertIn('''success'''     ,      result.stdout.decode()		)






      @require_torch
      def 				A__      ( self			:		Tuple		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   Optional[int]				  ='''
from transformers import pipeline
        '''
       lowercase					:   List[Any]				  ='''
mname = "hf-internal-testing/tiny-random-bert"
pipe = pipeline(model=mname)
        '''

       lowercase					:   Tuple				  ='''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")
socket.socket = offline_socket
        '''
       lowercase					:   Tuple				  =self.get_env()
       lowercase					:   Optional[int]				  ='''1'''
       lowercase					:   Union[str, Any]				  =[sys.executable, '''-c''', '''\n'''.join([load, mock, run]		)]
       lowercase					:   Dict				  =subprocess.run(UpperCAmelCase     ,      env=UpperCAmelCase     ,      check=UpperCAmelCase     ,      capture_output=UpperCAmelCase		)
       self.assertEqual(result.returncode     ,      1     ,      result.stderr		)
       self.assertIn(
           '''You cannot infer task automatically within `pipeline` when using offline mode'''     ,      result.stderr.decode().replace('''\n'''     ,      ''''''		)     ,      )






      @require_torch
      def 				A__      ( self			:		int		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   List[str]				  ='''
from transformers import AutoModel
        '''
       lowercase					:   Dict				  ='''
mname = "hf-internal-testing/test_dynamic_model"
AutoModel.from_pretrained(mname, trust_remote_code=True)
print("success")
        '''

       # baseline - just load from_pretrained with normal network
       lowercase					:   Dict				  =[sys.executable, '''-c''', '''\n'''.join([load, run]		)]

       # should succeed
       lowercase					:   Optional[Any]				  =self.get_env()
       lowercase					:   int				  =subprocess.run(UpperCAmelCase     ,      env=UpperCAmelCase     ,      check=UpperCAmelCase     ,      capture_output=UpperCAmelCase		)
       self.assertEqual(result.returncode     ,      0     ,      result.stderr		)
       self.assertIn('''success'''     ,      result.stdout.decode()		)

       # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
       lowercase					:   List[str]				  ='''1'''
       lowercase					:   List[Any]				  =subprocess.run(UpperCAmelCase     ,      env=UpperCAmelCase     ,      check=UpperCAmelCase     ,      capture_output=UpperCAmelCase		)
       self.assertEqual(result.returncode     ,      0     ,      result.stderr		)
       self.assertIn('''success'''     ,      result.stdout.decode()		)



