






"""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  ,					__a  ,					__a=13  ,					__a=7  ,					__a=True  ,					__a=True  ,					__a=True  ,					__a=True  ,					__a=99  ,					__a=32  ,					__a=5  ,					__a=4  ,					__a=4  ,					__a="gelu"  ,					__a=0.0  ,					__a=0.1  ,					__a=True  ,					__a=512  ,					__a=16  ,					__a=2  ,					__a=0.02  ,					__a=3  ,					__a=4  ,					__a=None  ,					):


					"""simple docstring"""

					A__							 =       parent
					A__							 =       batch_size
					A__							 =       seq_length
					A__							 =       is_training
					A__							 =       use_input_mask
					A__							 =       use_token_type_ids
					A__							 =       use_labels
					A__							 =       vocab_size
					A__							 =       hidden_size
					A__							 =       num_hidden_layers
					A__							 =       num_attention_heads
					A__							 =       intermediate_multiple_size
					A__							 =       hidden_act
					A__							 =       hidden_dropout
					A__							 =       attention_dropout
					A__							 =       weight_tying
					A__							 =       max_position_embeddings
					A__							 =       type_vocab_size
					A__							 =       type_sequence_label_size
					A__							 =       initializer_range
					A__							 =       num_labels
					A__							 =       num_choices
					A__							 =       scope


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       ids_tensor([self.batch_size, self.seq_length]  ,					self.vocab_size						)

					A__							 =       None
					if self.use_input_mask:
									A__							 =       random_attention_mask([self.batch_size, self.seq_length]						)

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

					A__							 =       self.get_config()

					return config, input_ids, input_mask, token_labels


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


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__ ,			A__ ,			A__ ,			A__							 =       self.prepare_config_and_inputs()

					A__							 =       True

					return config, input_ids, input_mask, token_labels


	def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a						):


					"""simple docstring"""

					A__							 =       GPTNeoXJapaneseModel(config=__a						)
					model.to(__a						)
					model.eval()
					A__							 =       model(__a  ,					attention_mask=__a						)
					A__							 =       model(__a						)
					self.parent.assertEqual(result.last_hidden_state.shape  ,					(self.batch_size, self.seq_length, self.hidden_size)						)


	def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a						):


					"""simple docstring"""

					A__							 =       True
					A__							 =       GPTNeoXJapaneseModel(__a						)
					model.to(__a						)
					model.eval()
					A__							 =       model(__a  ,					attention_mask=__a						)
					self.parent.assertEqual(result.last_hidden_state.shape  ,					(self.batch_size, self.seq_length, self.hidden_size)						)


	def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a  ,					__a						):


					"""simple docstring"""

					A__							 =       GPTNeoXJapaneseForCausalLM(config=__a						)
					model.to(__a						)
					model.eval()
					A__							 =       model(__a  ,					attention_mask=__a  ,					labels=__a						)
					self.parent.assertEqual(result.logits.shape  ,					(self.batch_size, self.seq_length, self.vocab_size)						)


	def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a						):


					"""simple docstring"""

					A__							 =       True
					A__							 =       GPTNeoXJapaneseForCausalLM(config=__a						)
					model.to(__a						)
					model.eval()

					# first forward pass
					A__							 =       model(__a  ,					attention_mask=__a  ,					use_cache=__a						)
					A__							 =       outputs.past_key_values

					# create hypothetical multiple next token and extent to next_input_ids
					A__							 =       ids_tensor((self.batch_size, 3)  ,					config.vocab_size						)
					A__							 =       ids_tensor((self.batch_size, 3)  ,					vocab_size=2						)

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

					A__							 =       model(__a  ,					attention_mask=__a  ,					output_hidden_states=__a						)
					A__							 =       output_from_no_past['hidden_states'][0]
					A__							 =       model(
					    __a  ,					attention_mask=__a  ,					past_key_values=__a  ,					output_hidden_states=__a  ,					)['hidden_states'][0]

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


					"""simple docstring"""

					A__							 =       self.prepare_config_and_inputs()
					A__ ,			A__ ,			A__ ,			A__							 =       config_and_inputs
					A__							 =       {'input_ids': input_ids, 'attention_mask': input_mask}
					return config, inputs_dict





@require_torch
class       snake_case_       (    _lowerCamelCase    ,							_lowerCamelCase    ,							unittest.TestCase       ):



	"""simple docstring"""

	SCREAMING_SNAKE_CASE_:							Optional[Any]   			=					(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
	SCREAMING_SNAKE_CASE_:							Optional[int]   			=					(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
	SCREAMING_SNAKE_CASE_:							Optional[Any]   			=					(
	    {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM}
	    if is_torch_available()
	    else {}
	)
	SCREAMING_SNAKE_CASE_:							int   			=					False
	SCREAMING_SNAKE_CASE_:							Any   			=					False
	SCREAMING_SNAKE_CASE_:							Dict   			=					False
	SCREAMING_SNAKE_CASE_:							int   			=					False


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       GPTNeoXJapaneseModelTester(self						)
					A__							 =       ConfigTester(self  ,					config_class=__a  ,					hidden_size=37						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					self.config_tester.run_common_tests()


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__ ,			A__ ,			A__ ,			A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(__a  ,					__a  ,					__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__ ,			A__ ,			A__ ,			A__							 =       self.model_tester.prepare_config_and_inputs_for_decoder()
					self.model_tester.create_and_check_model_as_decoder(__a  ,					__a  ,					__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__ ,			A__ ,			A__ ,			A__							 =       self.model_tester.prepare_config_and_inputs_for_decoder()

					A__							 =       None

					self.model_tester.create_and_check_model_as_decoder(__a  ,					__a  ,					__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__ ,			A__ ,			A__ ,			A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_decoder_model_past_large_inputs(__a  ,					__a  ,					__a						)


	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_causal_lm(*__a						)






	@slow
	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       'abeja/gpt-neox-japanese-2.7b'

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

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

					A__							 =       GPTNeoXJapaneseTokenizer.from_pretrained(__a						)
					A__							 =       GPTNeoXJapaneseForCausalLM.from_pretrained(__a						)

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










"""simple docstring"""






from ..utils import DummyObject, requires_backends





class       snake_case_       (    metaclass=_lowerCamelCase       ):



	"""simple docstring"""

	SCREAMING_SNAKE_CASE_:							List[str]   			=					["""note_seq"""]


	def __init__(      self  ,					*__a  ,					**__a						):


					"""simple docstring"""

					requires_backends(self  ,					['note_seq']						)


	@classmethod
	def       _UpperCAmelCase						(      cls  ,					*__a  ,					**__a						):


					"""simple docstring"""

					requires_backends(cls  ,					['note_seq']						)






	@classmethod
	def       _UpperCAmelCase						(      cls  ,					*__a  ,					**__a						):


					"""simple docstring"""

					requires_backends(cls  ,					['note_seq']						)


