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







		"""simple docstring"""

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






				"""simple docstring"""







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



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






				"""simple docstring"""







				snake_case  :       Union[str, Any]     			=						ids_tensor([self.batch_size, self.seq_length]  ,	self.vocab_size      )

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

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

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

				return config, input_ids, input_mask, token_labels



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






				"""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				:			Tuple      )						->			int:






				"""simple docstring"""







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

				snake_case  :       List[str]     			=						True

				return config, input_ids, input_mask, token_labels



		def       lowerCAmelCase				(      self				:			Optional[Any]  ,	UpperCamelCase__				:			Optional[int]  ,	UpperCamelCase__				:			List[Any]  ,	UpperCamelCase__				:			str      )						->			List[str]:






				"""simple docstring"""







				snake_case  :       Union[str, Any]     			=						GPTNeoXJapaneseModel(config=UpperCamelCase__      )
				model.to(UpperCamelCase__      )
				model.eval()
				snake_case  :       Optional[Any]     			=						model(UpperCamelCase__  ,	attention_mask=UpperCamelCase__      )
				snake_case  :       Union[str, 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__				:			Optional[Any]  ,	UpperCamelCase__				:			List[Any]  ,	UpperCamelCase__				:			Optional[Any]      )						->			Any:






				"""simple docstring"""







				snake_case  :       Optional[Any]     			=						True
				snake_case  :       List[str]     			=						GPTNeoXJapaneseModel(UpperCamelCase__      )
				model.to(UpperCamelCase__      )
				model.eval()
				snake_case  :       str     			=						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				:			Optional[int]  ,	UpperCamelCase__				:			Union[str, Any]  ,	UpperCamelCase__				:			int  ,	UpperCamelCase__				:			str  ,	UpperCamelCase__				:			int      )						->			int:






				"""simple docstring"""







				snake_case  :       Optional[int]     			=						GPTNeoXJapaneseForCausalLM(config=UpperCamelCase__      )
				model.to(UpperCamelCase__      )
				model.eval()
				snake_case  :       Optional[int]     			=						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				:			int  ,	UpperCamelCase__				:			Any  ,	UpperCamelCase__				:			Optional[Any]  ,	UpperCamelCase__				:			List[str]      )						->			Dict:






				"""simple docstring"""







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

				# first forward pass
				snake_case  :       Union[str, Any]     			=						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  :       Dict     			=						ids_tensor((self.batch_size, 3)  ,	config.vocab_size      )
				snake_case  :       str     			=						ids_tensor((self.batch_size, 3)  ,	vocab_size=2      )

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

				snake_case  :       str     			=						model(UpperCamelCase__  ,	attention_mask=UpperCamelCase__  ,	output_hidden_states=UpperCamelCase__      )
				snake_case  :       Optional[int]     			=						output_from_no_past['''hidden_states'''][0]
				snake_case  :       Optional[Any]     			=						model(
				    UpperCamelCase__  ,	attention_mask=UpperCamelCase__  ,	past_key_values=UpperCamelCase__  ,	output_hidden_states=UpperCamelCase__  ,	)['''hidden_states'''][0]

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






				"""simple docstring"""







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







@require_torch
class 	snake_case__       (      __SCREAMING_SNAKE_CASE    ,					__SCREAMING_SNAKE_CASE    ,					unittest.TestCase					):







		"""simple docstring"""

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



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






				"""simple docstring"""







				snake_case  :       List[Any]     			=						GPTNeoXJapaneseModelTester(self      )
				snake_case  :       Optional[int]     			=						ConfigTester(self  ,	config_class=UpperCamelCase__  ,	hidden_size=37      )



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






				"""simple docstring"""







				self.config_tester.run_common_tests()



		def       lowerCAmelCase				(      self				:			Tuple      )						->			List[str]:






				"""simple docstring"""







				snake_case			,snake_case			,snake_case			,snake_case  :       Optional[int]     			=						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__      )



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






				"""simple docstring"""







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



		def       lowerCAmelCase				(      self				:			int      )						->			int:






				"""simple docstring"""







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

				snake_case  :       str     			=						None

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



		def       lowerCAmelCase				(      self				:			str      )						->			Tuple:






				"""simple docstring"""







				snake_case			,snake_case			,snake_case			,snake_case  :       Optional[int]     			=						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_decoder_model_past_large_inputs(UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__      )



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






				"""simple docstring"""







				snake_case  :       List[Any]     			=						self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_causal_lm(*UpperCamelCase__      )



		@slow
		def       lowerCAmelCase				(      self				:			Optional[Any]      )						->			Optional[Any]:






				"""simple docstring"""







				snake_case  :       int     			=						'''abeja/gpt-neox-japanese-2.7b'''

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

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

				snake_case  :       int     			=						GPTNeoXJapaneseTokenizer.from_pretrained(UpperCamelCase__      )
				snake_case  :       Tuple     			=						GPTNeoXJapaneseForCausalLM.from_pretrained(UpperCamelCase__      )

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




'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


lowercase__						       =      {"configuration_encoder_decoder": ["EncoderDecoderConfig"]}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowercase__						       =      ["EncoderDecoderModel"]

try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowercase__						       =      ["TFEncoderDecoderModel"]

try:
						if not is_flax_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowercase__						       =      ["FlaxEncoderDecoderModel"]

if TYPE_CHECKING:
						from .configuration_encoder_decoder import EncoderDecoderConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_encoder_decoder import EncoderDecoderModel

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_encoder_decoder import TFEncoderDecoderModel

						try:
												if not is_flax_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_flax_encoder_decoder import FlaxEncoderDecoderModel

else:
						import sys

						lowercase__						       =      _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)


