





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  , __lowerCAmelCase  , __lowerCAmelCase=13  , __lowerCAmelCase=7  , __lowerCAmelCase=True  , __lowerCAmelCase=True  , __lowerCAmelCase=True  , __lowerCAmelCase=True  , __lowerCAmelCase=99  , __lowerCAmelCase=32  , __lowerCAmelCase=5  , __lowerCAmelCase=4  , __lowerCAmelCase=4  , __lowerCAmelCase="gelu"  , __lowerCAmelCase=0.0  , __lowerCAmelCase=0.1  , __lowerCAmelCase=True  , __lowerCAmelCase=512  , __lowerCAmelCase=16  , __lowerCAmelCase=2  , __lowerCAmelCase=0.02  , __lowerCAmelCase=3  , __lowerCAmelCase=4  , __lowerCAmelCase=None  , ):
														UpperCamelCase__				     =			parent
														UpperCamelCase__				     =			batch_size
														UpperCamelCase__				     =			seq_length
														UpperCamelCase__				     =			is_training
														UpperCamelCase__				     =			use_input_mask
														UpperCamelCase__				     =			use_token_type_ids
														UpperCamelCase__				     =			use_labels
														UpperCamelCase__				     =			vocab_size
														UpperCamelCase__				     =			hidden_size
														UpperCamelCase__				     =			num_hidden_layers
														UpperCamelCase__				     =			num_attention_heads
														UpperCamelCase__				     =			intermediate_multiple_size
														UpperCamelCase__				     =			hidden_act
														UpperCamelCase__				     =			hidden_dropout
														UpperCamelCase__				     =			attention_dropout
														UpperCamelCase__				     =			weight_tying
														UpperCamelCase__				     =			max_position_embeddings
														UpperCamelCase__				     =			type_vocab_size
														UpperCamelCase__				     =			type_sequence_label_size
														UpperCamelCase__				     =			initializer_range
														UpperCamelCase__				     =			num_labels
														UpperCamelCase__				     =			num_choices
														UpperCamelCase__				     =			scope






							def 				_lowerCamelCase		(						self  ):
														UpperCamelCase__				     =			ids_tensor([self.batch_size, self.seq_length]  , self.vocab_size  )

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

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

														UpperCamelCase__				     =			self.get_config()

														return config, input_ids, input_mask, token_labels






							def 				_lowerCamelCase		(						self  ):
														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 				_lowerCamelCase		(						self  ):
														UpperCamelCase__     ,     UpperCamelCase__     ,     UpperCamelCase__     ,     UpperCamelCase__				     =			self.prepare_config_and_inputs()

														UpperCamelCase__				     =			True

														return config, input_ids, input_mask, token_labels






							def 				_lowerCamelCase		(						self  , __lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  ):
														UpperCamelCase__				     =			GPTNeoXJapaneseModel(config=__lowerCAmelCase  )
														model.to(__lowerCAmelCase  )
														model.eval()
														UpperCamelCase__				     =			model(__lowerCAmelCase  , attention_mask=__lowerCAmelCase  )
														UpperCamelCase__				     =			model(__lowerCAmelCase  )
														self.parent.assertEqual(result.last_hidden_state.shape  , (self.batch_size, self.seq_length, self.hidden_size)  )






							def 				_lowerCamelCase		(						self  , __lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  ):
														UpperCamelCase__				     =			True
														UpperCamelCase__				     =			GPTNeoXJapaneseModel(__lowerCAmelCase  )
														model.to(__lowerCAmelCase  )
														model.eval()
														UpperCamelCase__				     =			model(__lowerCAmelCase  , attention_mask=__lowerCAmelCase  )
														self.parent.assertEqual(result.last_hidden_state.shape  , (self.batch_size, self.seq_length, self.hidden_size)  )






							def 				_lowerCamelCase		(						self  , __lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  ):
														UpperCamelCase__				     =			GPTNeoXJapaneseForCausalLM(config=__lowerCAmelCase  )
														model.to(__lowerCAmelCase  )
														model.eval()
														UpperCamelCase__				     =			model(__lowerCAmelCase  , attention_mask=__lowerCAmelCase  , labels=__lowerCAmelCase  )
														self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.seq_length, self.vocab_size)  )






							def 				_lowerCamelCase		(						self  , __lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  ):
														UpperCamelCase__				     =			True
														UpperCamelCase__				     =			GPTNeoXJapaneseForCausalLM(config=__lowerCAmelCase  )
														model.to(__lowerCAmelCase  )
														model.eval()

														# first forward pass
														UpperCamelCase__				     =			model(__lowerCAmelCase  , attention_mask=__lowerCAmelCase  , use_cache=__lowerCAmelCase  )
														UpperCamelCase__				     =			outputs.past_key_values

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

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

														UpperCamelCase__				     =			model(__lowerCAmelCase  , attention_mask=__lowerCAmelCase  , output_hidden_states=__lowerCAmelCase  )
														UpperCamelCase__				     =			output_from_no_past["""hidden_states"""][0]
														UpperCamelCase__				     =			model(
														    __lowerCAmelCase  , attention_mask=__lowerCAmelCase  , past_key_values=__lowerCAmelCase  , output_hidden_states=__lowerCAmelCase  , )["""hidden_states"""][0]

														# select random slice
														UpperCamelCase__				     =			ids_tensor((1,)  , output_from_past.shape[-1]  ).item()
														UpperCamelCase__				     =			output_from_no_past[:, -3:, random_slice_idx].detach()
														UpperCamelCase__				     =			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 				_lowerCamelCase		(						self  ):
														UpperCamelCase__				     =			self.prepare_config_and_inputs()
														UpperCamelCase__     ,     UpperCamelCase__     ,     UpperCamelCase__     ,     UpperCamelCase__				     =			config_and_inputs
														UpperCamelCase__				     =			{"""input_ids""": input_ids, """attention_mask""": input_mask}
														return config, inputs_dict






@require_torch
class        __SCREAMING_SNAKE_CASE					(     _a   ,		_a   ,		unittest.TestCase       ):
							snake_case     :      int						     =       (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
							snake_case     :      Optional[Any]						     =       (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
							snake_case     :      Dict						     =       (
							    {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM}
							    if is_torch_available()
							    else {}
							)
							snake_case     :      int						     =       False
							snake_case     :      Optional[Any]						     =       False
							snake_case     :      List[str]						     =       False
							snake_case     :      List[Any]						     =       False






							def 				_lowerCamelCase		(						self  ):
														UpperCamelCase__				     =			GPTNeoXJapaneseModelTester(self  )
														UpperCamelCase__				     =			ConfigTester(self  , config_class=__lowerCAmelCase  , hidden_size=37  )






							def 				_lowerCamelCase		(						self  ):
														self.config_tester.run_common_tests()






							def 				_lowerCamelCase		(						self  ):
														UpperCamelCase__     ,     UpperCamelCase__     ,     UpperCamelCase__     ,     UpperCamelCase__				     =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(__lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  )






							def 				_lowerCamelCase		(						self  ):
														UpperCamelCase__     ,     UpperCamelCase__     ,     UpperCamelCase__     ,     UpperCamelCase__				     =			self.model_tester.prepare_config_and_inputs_for_decoder()
														self.model_tester.create_and_check_model_as_decoder(__lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  )






							def 				_lowerCamelCase		(						self  ):
														# This regression test was failing with PyTorch < 1.3
														UpperCamelCase__     ,     UpperCamelCase__     ,     UpperCamelCase__     ,     UpperCamelCase__				     =			self.model_tester.prepare_config_and_inputs_for_decoder()

														UpperCamelCase__				     =			None

														self.model_tester.create_and_check_model_as_decoder(__lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  )






							def 				_lowerCamelCase		(						self  ):
														UpperCamelCase__     ,     UpperCamelCase__     ,     UpperCamelCase__     ,     UpperCamelCase__				     =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_decoder_model_past_large_inputs(__lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  )






							def 				_lowerCamelCase		(						self  ):
														UpperCamelCase__				     =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_causal_lm(*__lowerCAmelCase  )







							@slow
							def 				_lowerCamelCase		(						self  ):
														UpperCamelCase__				     =			"""abeja/gpt-neox-japanese-2.7b"""

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

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

														UpperCamelCase__				     =			GPTNeoXJapaneseTokenizer.from_pretrained(__lowerCAmelCase  )
														UpperCamelCase__				     =			GPTNeoXJapaneseForCausalLM.from_pretrained(__lowerCAmelCase  )

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











import unittest

from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow


if is_flax_available():
					import jax

					from transformers.models.auto.modeling_flax_auto import FlaxAutoModel
					from transformers.models.bert.modeling_flax_bert import FlaxBertModel
					from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel

@require_flax
class        __SCREAMING_SNAKE_CASE					(     unittest.TestCase       ):






							@slow
							def 				_lowerCamelCase		(						self  ):
														for model_name in ["bert-base-cased", "bert-large-uncased"]:
																					with self.subTest(__lowerCAmelCase  ):
																												UpperCamelCase__				     =			AutoConfig.from_pretrained(__lowerCAmelCase  )
																												self.assertIsNotNone(__lowerCAmelCase  )
																												self.assertIsInstance(__lowerCAmelCase  , __lowerCAmelCase  )

																												UpperCamelCase__				     =			FlaxAutoModel.from_pretrained(__lowerCAmelCase  )
																												self.assertIsNotNone(__lowerCAmelCase  )
																												self.assertIsInstance(__lowerCAmelCase  , __lowerCAmelCase  )






							@slow
							def 				_lowerCamelCase		(						self  ):
														for model_name in ["roberta-base", "roberta-large"]:
																					with self.subTest(__lowerCAmelCase  ):
																												UpperCamelCase__				     =			AutoConfig.from_pretrained(__lowerCAmelCase  )
																												self.assertIsNotNone(__lowerCAmelCase  )
																												self.assertIsInstance(__lowerCAmelCase  , __lowerCAmelCase  )

																												UpperCamelCase__				     =			FlaxAutoModel.from_pretrained(__lowerCAmelCase  )
																												self.assertIsNotNone(__lowerCAmelCase  )
																												self.assertIsInstance(__lowerCAmelCase  , __lowerCAmelCase  )






							@slow
							def 				_lowerCamelCase		(						self  ):
														for model_name in ["bert-base-cased", "bert-large-uncased"]:
																					UpperCamelCase__				     =			AutoTokenizer.from_pretrained(__lowerCAmelCase  )
																					UpperCamelCase__				     =			FlaxBertModel.from_pretrained(__lowerCAmelCase  )
																					UpperCamelCase__				     =			tokenizer("""Do you support jax jitted function?"""  , return_tensors=TensorType.JAX  )

																					@jax.jit
																					def eval(**__lowerCAmelCase  ):
																												return model(**__lowerCAmelCase  )

																					eval(**__lowerCAmelCase  ).block_until_ready()






							@slow
							def 				_lowerCamelCase		(						self  ):
														for model_name in ["roberta-base", "roberta-large"]:
																					UpperCamelCase__				     =			AutoTokenizer.from_pretrained(__lowerCAmelCase  )
																					UpperCamelCase__				     =			FlaxRobertaModel.from_pretrained(__lowerCAmelCase  )
																					UpperCamelCase__				     =			tokenizer("""Do you support jax jitted function?"""  , return_tensors=TensorType.JAX  )

																					@jax.jit
																					def eval(**__lowerCAmelCase  ):
																												return model(**__lowerCAmelCase  )

																					eval(**__lowerCAmelCase  ).block_until_ready()






							def 				_lowerCamelCase		(						self  ):
														with self.assertRaisesRegex(
														    __lowerCAmelCase  , """bert-base is not a local folder and is not a valid model identifier"""  ):
																					UpperCamelCase__				     =			FlaxAutoModel.from_pretrained("""bert-base"""  )






							def 				_lowerCamelCase		(						self  ):
														with self.assertRaisesRegex(
														    __lowerCAmelCase  , r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""  ):
																					UpperCamelCase__				     =			FlaxAutoModel.from_pretrained(__lowerCAmelCase  , revision="""aaaaaa"""  )






							def 				_lowerCamelCase		(						self  ):
														with self.assertRaisesRegex(
														    __lowerCAmelCase  , """hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack"""  , ):
																					UpperCamelCase__				     =			FlaxAutoModel.from_pretrained("""hf-internal-testing/config-no-model"""  )







							def 				_lowerCamelCase		(						self  ):
														with self.assertRaisesRegex(__lowerCAmelCase  , """Use `from_pt=True` to load this model"""  ):
																					UpperCamelCase__				     =			FlaxAutoModel.from_pretrained("""hf-internal-testing/tiny-bert-pt-only"""  )




