


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

		"""simple docstring"""

		def __init__(		self   ,       UpperCamelCase_   ,       UpperCamelCase_=13   ,       UpperCamelCase_=7   ,       UpperCamelCase_=True   ,       UpperCamelCase_=True   ,       UpperCamelCase_=True   ,       UpperCamelCase_=True   ,       UpperCamelCase_=99   ,       UpperCamelCase_=32   ,       UpperCamelCase_=5   ,       UpperCamelCase_=4   ,       UpperCamelCase_=4   ,       UpperCamelCase_="gelu"   ,       UpperCamelCase_=0.0   ,       UpperCamelCase_=0.1   ,       UpperCamelCase_=True   ,       UpperCamelCase_=512   ,       UpperCamelCase_=16   ,       UpperCamelCase_=2   ,       UpperCamelCase_=0.02   ,       UpperCamelCase_=3   ,       UpperCamelCase_=4   ,       UpperCamelCase_=None   ,       ):

							'''simple docstring'''


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




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


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

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

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

							UpperCamelCase__       :Any							 =			self.get_config()

							return config, input_ids, input_mask, token_labels




		def    lowerCAmelCase__    (		self							):

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

							'''simple docstring'''


							UpperCamelCase__       :Any							 =			self.prepare_config_and_inputs()

							UpperCamelCase__       :List[str]							 =			True

							return config, input_ids, input_mask, token_labels




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							):

							'''simple docstring'''


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




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							):

							'''simple docstring'''


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

							'''simple docstring'''


							UpperCamelCase__       :Union[str, Any]							 =			GPTNeoXJapaneseForCausalLM(config=UpperCamelCase_							)
							model.to(UpperCamelCase_							)
							model.eval()
							UpperCamelCase__       :Union[str, 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   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__       :Dict							 =			True
							UpperCamelCase__       :Any							 =			GPTNeoXJapaneseForCausalLM(config=UpperCamelCase_							)
							model.to(UpperCamelCase_							)
							model.eval()

							# first forward pass
							UpperCamelCase__       :int							 =			model(UpperCamelCase_   ,       attention_mask=UpperCamelCase_   ,       use_cache=UpperCamelCase_							)
							UpperCamelCase__       :Optional[int]							 =			outputs.past_key_values

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

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

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

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

							'''simple docstring'''


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



@require_torch
class        lowercase     (							A__     ,		A__     ,		unittest.TestCase ):

		"""simple docstring"""

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




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :int							 =			GPTNeoXJapaneseModelTester(self							)
							UpperCamelCase__       :Any							 =			ConfigTester(self   ,       config_class=UpperCamelCase_   ,       hidden_size=37							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							self.config_tester.run_common_tests()




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :str							 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Any							 =			self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[Any]							 =			self.model_tester.prepare_config_and_inputs_for_decoder()

							UpperCamelCase__       :Any							 =			None

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




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Tuple							 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past_large_inputs(UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[Any]							 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_causal_lm(*UpperCamelCase_							)




		@slow
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :str							 =			'''abeja/gpt-neox-japanese-2.7b'''

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

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

							UpperCamelCase__       :List[Any]							 =			GPTNeoXJapaneseTokenizer.from_pretrained(UpperCamelCase_							)
							UpperCamelCase__       :str							 =			GPTNeoXJapaneseForCausalLM.from_pretrained(UpperCamelCase_							)

							UpperCamelCase__       :int							 =			[]
							for prompt in prompts:
												UpperCamelCase__       :Tuple							 =			tokenizer(UpperCamelCase_   ,       return_tensors='''pt'''							).input_ids
												UpperCamelCase__       :Dict							 =			model.generate(UpperCamelCase_   ,       max_length=50							)
												UpperCamelCase__       :List[str]							 =			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_torch_available


__snake_case											=					{'''configuration_vit_msn''': ['''VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViTMSNConfig''']}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case											=					[
		    '''VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''ViTMSNModel''',
		    '''ViTMSNForImageClassification''',
		    '''ViTMSNPreTrainedModel''',
		]

if TYPE_CHECKING:
		from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_vit_msn import (
				    VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ViTMSNForImageClassification,
				    ViTMSNModel,
				    ViTMSNPreTrainedModel,
				)

else:
		import sys

		__snake_case											=					_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)