

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







				def __init__(					self    :		List[Any]		,				UpperCAmelCase__    :		List[Any]		,				UpperCAmelCase__    :		Union[str, Any]=13		,				UpperCAmelCase__    :		Any=7		,				UpperCAmelCase__    :		List[Any]=True		,				UpperCAmelCase__    :		Tuple=True		,				UpperCAmelCase__    :		Any=True		,				UpperCAmelCase__    :		Union[str, Any]=True		,				UpperCAmelCase__    :		Tuple=99		,				UpperCAmelCase__    :		Optional[int]=32		,				UpperCAmelCase__    :		Dict=5		,				UpperCAmelCase__    :		List[Any]=4		,				UpperCAmelCase__    :		List[str]=4		,				UpperCAmelCase__    :		Dict="gelu"		,				UpperCAmelCase__    :		Optional[Any]=0.0		,				UpperCAmelCase__    :		Dict=0.1		,				UpperCAmelCase__    :		Optional[int]=True		,				UpperCAmelCase__    :		Any=512		,				UpperCAmelCase__    :		List[str]=16		,				UpperCAmelCase__    :		str=2		,				UpperCAmelCase__    :		Optional[Any]=0.02		,				UpperCAmelCase__    :		List[str]=3		,				UpperCAmelCase__    :		Optional[Any]=4		,				UpperCAmelCase__    :		Optional[int]=None		,				):
										'''simple docstring'''
										lowercase   :       Optional[int]								=parent
										lowercase   :       Tuple								=batch_size
										lowercase   :       Optional[int]								=seq_length
										lowercase   :       Optional[Any]								=is_training
										lowercase   :       Any								=use_input_mask
										lowercase   :       Any								=use_token_type_ids
										lowercase   :       Dict								=use_labels
										lowercase   :       Any								=vocab_size
										lowercase   :       int								=hidden_size
										lowercase   :       List[Any]								=num_hidden_layers
										lowercase   :       str								=num_attention_heads
										lowercase   :       Optional[int]								=intermediate_multiple_size
										lowercase   :       str								=hidden_act
										lowercase   :       List[Any]								=hidden_dropout
										lowercase   :       Tuple								=attention_dropout
										lowercase   :       Optional[int]								=weight_tying
										lowercase   :       List[Any]								=max_position_embeddings
										lowercase   :       Union[str, Any]								=type_vocab_size
										lowercase   :       Optional[int]								=type_sequence_label_size
										lowercase   :       List[str]								=initializer_range
										lowercase   :       List[Any]								=num_labels
										lowercase   :       Union[str, Any]								=num_choices
										lowercase   :       Union[str, Any]								=scope







				def    lowerCamelCase_  (					self    :		List[Any]      ):
										'''simple docstring'''
										lowercase   :       Any								=ids_tensor([self.batch_size, self.seq_length]		,				self.vocab_size      )

										lowercase   :       Tuple								=None
										if self.use_input_mask:
																lowercase   :       Optional[int]								=random_attention_mask([self.batch_size, self.seq_length]      )

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

										lowercase   :       Tuple								=self.get_config()

										return config, input_ids, input_mask, token_labels







				def    lowerCamelCase_  (					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=UpperCAmelCase__		,				initializer_range=self.initializer_range		,				)







				def    lowerCamelCase_  (					self    :		Optional[Any]      ):
										'''simple docstring'''
										lowercase   :       Optional[int]								=self.prepare_config_and_inputs()

										lowercase   :       Dict								=True

										return config, input_ids, input_mask, token_labels







				def    lowerCamelCase_  (					self    :		Any		,				UpperCAmelCase__    :		str		,				UpperCAmelCase__    :		str		,				UpperCAmelCase__    :		List[str]      ):
										'''simple docstring'''
										lowercase   :       int								=GPTNeoXJapaneseModel(config=UpperCAmelCase__      )
										model.to(UpperCAmelCase__      )
										model.eval()
										lowercase   :       List[Any]								=model(UpperCAmelCase__		,				attention_mask=UpperCAmelCase__      )
										lowercase   :       Optional[Any]								=model(UpperCAmelCase__      )
										self.parent.assertEqual(result.last_hidden_state.shape		,				(self.batch_size, self.seq_length, self.hidden_size)      )







				def    lowerCamelCase_  (					self    :		Tuple		,				UpperCAmelCase__    :		Optional[int]		,				UpperCAmelCase__    :		int		,				UpperCAmelCase__    :		Dict      ):
										'''simple docstring'''
										lowercase   :       str								=True
										lowercase   :       Dict								=GPTNeoXJapaneseModel(UpperCAmelCase__      )
										model.to(UpperCAmelCase__      )
										model.eval()
										lowercase   :       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    :		Tuple		,				UpperCAmelCase__    :		List[str]		,				UpperCAmelCase__    :		List[str]		,				UpperCAmelCase__    :		Union[str, Any]		,				UpperCAmelCase__    :		List[str]      ):
										'''simple docstring'''
										lowercase   :       int								=GPTNeoXJapaneseForCausalLM(config=UpperCAmelCase__      )
										model.to(UpperCAmelCase__      )
										model.eval()
										lowercase   :       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    :		List[Any]		,				UpperCAmelCase__    :		str		,				UpperCAmelCase__    :		Union[str, Any]		,				UpperCAmelCase__    :		int      ):
										'''simple docstring'''
										lowercase   :       int								=True
										lowercase   :       Optional[Any]								=GPTNeoXJapaneseForCausalLM(config=UpperCAmelCase__      )
										model.to(UpperCAmelCase__      )
										model.eval()

										# first forward pass
										lowercase   :       Tuple								=model(UpperCAmelCase__		,				attention_mask=UpperCAmelCase__		,				use_cache=UpperCAmelCase__      )
										lowercase   :       Dict								=outputs.past_key_values

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

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

										lowercase   :       List[Any]								=model(UpperCAmelCase__		,				attention_mask=UpperCAmelCase__		,				output_hidden_states=UpperCAmelCase__      )
										lowercase   :       Any								=output_from_no_past['''hidden_states'''][0]
										lowercase   :       Optional[int]								=model(
										    UpperCAmelCase__		,				attention_mask=UpperCAmelCase__		,				past_key_values=UpperCAmelCase__		,				output_hidden_states=UpperCAmelCase__		,				)['''hidden_states'''][0]

										# select random slice
										lowercase   :       Tuple								=ids_tensor((1,)		,				output_from_past.shape[-1]      ).item()
										lowercase   :       List[Any]								=output_from_no_past[:, -3:, random_slice_idx].detach()
										lowercase   :       int								=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      ):
										'''simple docstring'''
										lowercase   :       Optional[Any]								=self.prepare_config_and_inputs()
										lowercase   :       Union[str, Any]								=config_and_inputs
										lowercase   :       Optional[int]								={'''input_ids''': input_ids, '''attention_mask''': input_mask}
										return config, inputs_dict





@require_torch
class 		__SCREAMING_SNAKE_CASE       ( lowercase__	,       lowercase__	,       unittest.TestCase			):
				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    :		Tuple      ):
										'''simple docstring'''
										lowercase   :       Union[str, Any]								=GPTNeoXJapaneseModelTester(self      )
										lowercase   :       str								=ConfigTester(self		,				config_class=UpperCAmelCase__		,				hidden_size=37      )







				def    lowerCamelCase_  (					self    :		Any      ):
										'''simple docstring'''
										self.config_tester.run_common_tests()







				def    lowerCamelCase_  (					self    :		int      ):
										'''simple docstring'''
										lowercase   :       int								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		Any      ):
										'''simple docstring'''
										lowercase   :       Dict								=self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_model_as_decoder(UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		Tuple      ):
										'''simple docstring'''
										lowercase   :       Tuple								=self.model_tester.prepare_config_and_inputs_for_decoder()

										lowercase   :       Any								=None

										self.model_tester.create_and_check_model_as_decoder(UpperCAmelCase__		,				UpperCAmelCase__		,				UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		List[Any]      ):
										'''simple docstring'''
										lowercase   :       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    :		str      ):
										'''simple docstring'''
										lowercase   :       Union[str, Any]								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_causal_lm(*UpperCAmelCase__      )







				@slow
				def    lowerCamelCase_  (					self    :		int      ):
										'''simple docstring'''
										lowercase   :       Any								='''abeja/gpt-neox-japanese-2.7b'''

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

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

										lowercase   :       Optional[int]								=GPTNeoXJapaneseTokenizer.from_pretrained(UpperCAmelCase__      )
										lowercase   :       List[Any]								=GPTNeoXJapaneseForCausalLM.from_pretrained(UpperCAmelCase__      )

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



'''simple docstring'''





from datetime import datetime

import matplotlib.pyplot as plt
import torch






def 							_lowerCAmelCase (      __magic_name__	:    Dict	)							->     Dict:
						for param in module.parameters():
												lowercase   :       List[str]								=False






def 							_lowerCAmelCase (      )							->     List[str]:
						lowercase   :       Dict								='''cuda''' if torch.cuda.is_available() else '''cpu'''
						if torch.backends.mps.is_available() and torch.backends.mps.is_built():
												lowercase   :       Optional[int]								='''mps'''
						if device == "mps":
												print(
												    '''WARNING: MPS currently doesn\'t seem to work, and messes up backpropagation without any visible torch'''
												    ''' errors. I recommend using CUDA on a colab notebook or CPU instead if you\'re facing inexplicable issues'''
												    ''' with generations.'''	)
						return device






def 							_lowerCAmelCase (      __magic_name__	:    Union[str, Any]	)							->     str:
						lowercase   :       Optional[int]								=plt.imshow(__magic_name__	)
						fig.axes.get_xaxis().set_visible(__magic_name__	)
						fig.axes.get_yaxis().set_visible(__magic_name__	)
						plt.show()







def 							_lowerCAmelCase (      )							->     List[Any]:
						lowercase   :       Any								=datetime.now()
						lowercase   :       Dict								=current_time.strftime('''%H:%M:%S'''	)
						return timestamp





