






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						:    Optional[int],  lowerCamelCase						:    Optional[int],  lowerCamelCase						:    Optional[int]=13,  lowerCamelCase						:    str=7,  lowerCamelCase						:    Optional[int]=True,  lowerCamelCase						:    int=True,  lowerCamelCase						:    Optional[Any]=True,  lowerCamelCase						:    Tuple=True,  lowerCamelCase						:    Any=99,  lowerCamelCase						:    Any=32,  lowerCamelCase						:    Optional[Any]=5,  lowerCamelCase						:    Union[str, Any]=4,  lowerCamelCase						:    Tuple=4,  lowerCamelCase						:    List[str]="gelu",  lowerCamelCase						:    Dict=0.0,  lowerCamelCase						:    Any=0.1,  lowerCamelCase						:    List[str]=True,  lowerCamelCase						:    Tuple=512,  lowerCamelCase						:    Union[str, Any]=16,  lowerCamelCase						:    Union[str, Any]=2,  lowerCamelCase						:    str=0.02,  lowerCamelCase						:    Tuple=3,  lowerCamelCase						:    Optional[int]=4,  lowerCamelCase						:    Any=None,  ):

								'''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 				lowercase__   (     self						:    Optional[int]  ):

								'''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 				lowercase__   (     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=lowerCamelCase,  initializer_range=self.initializer_range,  )




				def 				lowercase__   (     self						:    List[Any]  ):

								'''simple docstring'''
								lowercase__ ,						lowercase__ ,						lowercase__ ,						lowercase__    =			self.prepare_config_and_inputs()

								lowercase__    =			True

								return config, input_ids, input_mask, token_labels




				def 				lowercase__   (     self						:    Union[str, Any],  lowerCamelCase						:    int,  lowerCamelCase						:    List[str],  lowerCamelCase						:    List[Any]  ):

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




				def 				lowercase__   (     self						:    Any,  lowerCamelCase						:    Dict,  lowerCamelCase						:    Any,  lowerCamelCase						:    List[str]  ):

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




				def 				lowercase__   (     self						:    Union[str, Any],  lowerCamelCase						:    Optional[Any],  lowerCamelCase						:    str,  lowerCamelCase						:    Union[str, Any],  lowerCamelCase						:    Any  ):

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




				def 				lowercase__   (     self						:    List[Any],  lowerCamelCase						:    Any,  lowerCamelCase						:    Any,  lowerCamelCase						:    List[Any]  ):

								'''simple docstring'''
								lowercase__    =			True
								lowercase__    =			GPTNeoXJapaneseForCausalLM(config=lowerCamelCase  )
								model.to(lowerCamelCase  )
								model.eval()

								# first forward pass
								lowercase__    =			model(lowerCamelCase,  attention_mask=lowerCamelCase,  use_cache=lowerCamelCase  )
								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(lowerCamelCase,  attention_mask=lowerCamelCase,  output_hidden_states=lowerCamelCase  )
								lowercase__    =			output_from_no_past['''hidden_states'''][0]
								lowercase__    =			model(
								    lowerCamelCase,  attention_mask=lowerCamelCase,  past_key_values=lowerCamelCase,  output_hidden_states=lowerCamelCase,  )['''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(lowerCamelCase,  lowerCamelCase,  atol=1E-3  )  )




				def 				lowercase__   (     self						:    Optional[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       _UpperCAmelCase					(       A__		,A__		,unittest.TestCase	):

				"""simple docstring"""




				lowercase__											=       (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
				lowercase__											=       (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
				lowercase__											=       (
				    {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM}
				    if is_torch_available()
				    else {}
				)
				lowercase__											=       False
				lowercase__											=       False
				lowercase__											=       False
				lowercase__											=       False




				def 				lowercase__   (     self						:    Optional[int]  ):

								'''simple docstring'''
								lowercase__    =			GPTNeoXJapaneseModelTester(self  )
								lowercase__    =			ConfigTester(self,  config_class=lowerCamelCase,  hidden_size=37  )




				def 				lowercase__   (     self						:    List[str]  ):

								'''simple docstring'''
								self.config_tester.run_common_tests()




				def 				lowercase__   (     self						:    Tuple  ):

								'''simple docstring'''
								lowercase__ ,						lowercase__ ,						lowercase__ ,						lowercase__    =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(lowerCamelCase,  lowerCamelCase,  lowerCamelCase  )




				def 				lowercase__   (     self						:    Any  ):

								'''simple docstring'''
								lowercase__ ,						lowercase__ ,						lowercase__ ,						lowercase__    =			self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_model_as_decoder(lowerCamelCase,  lowerCamelCase,  lowerCamelCase  )




				def 				lowercase__   (     self						:    Optional[int]  ):

								'''simple docstring'''
								# This regression test was failing with PyTorch < 1.3
								lowercase__ ,						lowercase__ ,						lowercase__ ,						lowercase__    =			self.model_tester.prepare_config_and_inputs_for_decoder()

								lowercase__    =			None

								self.model_tester.create_and_check_model_as_decoder(lowerCamelCase,  lowerCamelCase,  lowerCamelCase  )




				def 				lowercase__   (     self						:    List[Any]  ):

								'''simple docstring'''
								lowercase__ ,						lowercase__ ,						lowercase__ ,						lowercase__    =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_decoder_model_past_large_inputs(lowerCamelCase,  lowerCamelCase,  lowerCamelCase  )




				def 				lowercase__   (     self						:    List[str]  ):

								'''simple docstring'''
								lowercase__    =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_causal_lm(*lowerCamelCase  )




				@slow
				def 				lowercase__   (     self						:    Any  ):

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

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

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

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

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












from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


A__	:			Optional[int]			   = {}

try:
							if not is_sentencepiece_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A__	:			Optional[int]			   = ['GPTSw3Tokenizer']


if TYPE_CHECKING:
							try:
														if not is_sentencepiece_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_gpt_swa import GPTSwaTokenizer

else:
							import sys

							A__	:			Optional[int]			   = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




