




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       :  Dict      ,   __lowerCamelCase       :  List[Any]      ,   __lowerCamelCase       :  int=13      ,   __lowerCamelCase       :  List[str]=7      ,   __lowerCamelCase       :  int=True      ,   __lowerCamelCase       :  Tuple=True      ,   __lowerCamelCase       :  List[Any]=True      ,   __lowerCamelCase       :  Union[str, Any]=True      ,   __lowerCamelCase       :  Optional[Any]=99      ,   __lowerCamelCase       :  Dict=32      ,   __lowerCamelCase       :  Tuple=5      ,   __lowerCamelCase       :  str=4      ,   __lowerCamelCase       :  List[Any]=4      ,   __lowerCamelCase       :  int="gelu"      ,   __lowerCamelCase       :  int=0.0      ,   __lowerCamelCase       :  Any=0.1      ,   __lowerCamelCase       :  Dict=True      ,   __lowerCamelCase       :  Dict=5_12      ,   __lowerCamelCase       :  Tuple=16      ,   __lowerCamelCase       :  Dict=2      ,   __lowerCamelCase       :  str=0.02      ,   __lowerCamelCase       :  Optional[int]=3      ,   __lowerCamelCase       :  Dict=4      ,   __lowerCamelCase       :  int=None      ,   )      ->  int:
							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       :  Any							)      ->  List[Any]:
							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       :  Optional[int]							)      ->  List[Any]:
							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 							__UpperCAmelCase							(   self       :  int							)      ->  str:
							a					,	a					,	a					,	a					=    self.prepare_config_and_inputs()

							a					=    True

							return config, input_ids, input_mask, token_labels





				def 							__UpperCAmelCase							(   self       :  str      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  List[Any]							)      ->  Dict:
							a					=    GPTNeoXJapaneseModel(config=lowerCAmelCase_							)
							model.to(lowerCAmelCase_							)
							model.eval()
							a					=    model(lowerCAmelCase_      ,   attention_mask=lowerCAmelCase_							)
							a					=    model(lowerCAmelCase_							)
							self.parent.assertEqual(result.last_hidden_state.shape      ,   (self.batch_size, self.seq_length, self.hidden_size)							)





				def 							__UpperCAmelCase							(   self       :  Union[str, Any]      ,   __lowerCamelCase       :  Optional[Any]      ,   __lowerCamelCase       :  List[Any]      ,   __lowerCamelCase       :  Optional[Any]							)      ->  Optional[Any]:
							a					=    True
							a					=    GPTNeoXJapaneseModel(lowerCAmelCase_							)
							model.to(lowerCAmelCase_							)
							model.eval()
							a					=    model(lowerCAmelCase_      ,   attention_mask=lowerCAmelCase_							)
							self.parent.assertEqual(result.last_hidden_state.shape      ,   (self.batch_size, self.seq_length, self.hidden_size)							)





				def 							__UpperCAmelCase							(   self       :  int      ,   __lowerCamelCase       :  List[str]      ,   __lowerCamelCase       :  Any      ,   __lowerCamelCase       :  Optional[Any]      ,   __lowerCamelCase       :  Optional[Any]							)      ->  Optional[Any]:
							a					=    GPTNeoXJapaneseForCausalLM(config=lowerCAmelCase_							)
							model.to(lowerCAmelCase_							)
							model.eval()
							a					=    model(lowerCAmelCase_      ,   attention_mask=lowerCAmelCase_      ,   labels=lowerCAmelCase_							)
							self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.seq_length, self.vocab_size)							)





				def 							__UpperCAmelCase							(   self       :  List[str]      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  Dict      ,   __lowerCamelCase       :  Optional[int]							)      ->  List[str]:
							a					=    True
							a					=    GPTNeoXJapaneseForCausalLM(config=lowerCAmelCase_							)
							model.to(lowerCAmelCase_							)
							model.eval()

							# first forward pass
							a					=    model(lowerCAmelCase_      ,   attention_mask=lowerCAmelCase_      ,   use_cache=lowerCAmelCase_							)
							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(lowerCAmelCase_      ,   attention_mask=lowerCAmelCase_      ,   output_hidden_states=lowerCAmelCase_							)
							a					=    output_from_no_past["hidden_states"][0]
							a					=    model(
							    lowerCAmelCase_      ,   attention_mask=lowerCAmelCase_      ,   past_key_values=lowerCAmelCase_      ,   output_hidden_states=lowerCAmelCase_      ,   )["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(lowerCAmelCase_      ,   lowerCAmelCase_      ,   atol=1e-3							)							)





				def 							__UpperCAmelCase							(   self       :  Optional[int]							)      ->  Dict:
							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_ :							str											=      (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE_ :							str											=      (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE_ :							Dict											=      (
				    {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM}
				    if is_torch_available()
				    else {}
				)
				SCREAMING_SNAKE_CASE_ :							Any											=      False
				SCREAMING_SNAKE_CASE_ :							List[Any]											=      False
				SCREAMING_SNAKE_CASE_ :							Union[str, Any]											=      False
				SCREAMING_SNAKE_CASE_ :							str											=      False





				def 							__UpperCAmelCase							(   self       :  Tuple							)      ->  List[Any]:
							a					=    GPTNeoXJapaneseModelTester(self							)
							a					=    ConfigTester(self      ,   config_class=lowerCAmelCase_      ,   hidden_size=37							)





				def 							__UpperCAmelCase							(   self       :  Tuple							)      ->  Optional[int]:
							self.config_tester.run_common_tests()





				def 							__UpperCAmelCase							(   self       :  Optional[Any]							)      ->  List[Any]:
							a					,	a					,	a					,	a					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(lowerCAmelCase_      ,   lowerCAmelCase_      ,   lowerCAmelCase_							)





				def 							__UpperCAmelCase							(   self       :  List[Any]							)      ->  Tuple:
							a					,	a					,	a					,	a					=    self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(lowerCAmelCase_      ,   lowerCAmelCase_      ,   lowerCAmelCase_							)





				def 							__UpperCAmelCase							(   self       :  List[Any]							)      ->  Dict:
							# This regression test was failing with PyTorch < 1.3
							a					,	a					,	a					,	a					=    self.model_tester.prepare_config_and_inputs_for_decoder()

							a					=    None

							self.model_tester.create_and_check_model_as_decoder(lowerCAmelCase_      ,   lowerCAmelCase_      ,   lowerCAmelCase_							)





				def 							__UpperCAmelCase							(   self       :  Optional[Any]							)      ->  Tuple:
							a					,	a					,	a					,	a					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past_large_inputs(lowerCAmelCase_      ,   lowerCAmelCase_      ,   lowerCAmelCase_							)





				def 							__UpperCAmelCase							(   self       :  Optional[int]							)      ->  List[str]:
							a					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_causal_lm(*lowerCAmelCase_							)





				@slow
				def 							__UpperCAmelCase							(   self       :  str							)      ->  int:
							a					=    "abeja/gpt-neox-japanese-2.7b"

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

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

							a					=    GPTNeoXJapaneseTokenizer.from_pretrained(lowerCAmelCase_							)
							a					=    GPTNeoXJapaneseForCausalLM.from_pretrained(lowerCAmelCase_							)

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












from typing import TYPE_CHECKING

from ....utils import _LazyModule


__lowerCAmelCase	:       int     =	{'tokenization_tapex': ['TapexTokenizer']}


if TYPE_CHECKING:
		from .tokenization_tapex import TapexTokenizer


else:
		import sys

		__lowerCAmelCase	:       Tuple     =	_LazyModule(__name__, globals()['__file__'], _import_structure)






