"""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 		UpperCamelCase      :



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



				def  _lowercase					(self    :				Union[str, Any])   ->     Dict:
					__snake_case      :							str						=			ids_tensor([self.batch_size, self.seq_length]     ,  self.vocab_size)

					__snake_case      :							Any						=			None
					if self.use_input_mask:
						__snake_case      :							List[Any]						=			random_attention_mask([self.batch_size, self.seq_length])

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

					__snake_case      :							List[Any]						=			self.get_config()

					return config, input_ids, input_mask, token_labels



				def  _lowercase					(self    :				str)   ->     Optional[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=_A     ,  initializer_range=self.initializer_range     ,  )



				def  _lowercase					(self    :				str)   ->     Optional[int]:
					__snake_case      :							str						=			self.prepare_config_and_inputs()

					__snake_case      :							Dict						=			True

					return config, input_ids, input_mask, token_labels



				def  _lowercase					(self    :				Any     ,  _A    :				Tuple     ,  _A    :				Tuple     ,  _A    :				Any)   ->     str:
					__snake_case      :							Optional[int]						=			GPTNeoXJapaneseModel(config=_A)
					model.to(_A)
					model.eval()
					__snake_case      :							str						=			model(_A     ,  attention_mask=_A)
					__snake_case      :							List[Any]						=			model(_A)
					self.parent.assertEqual(result.last_hidden_state.shape     ,  (self.batch_size, self.seq_length, self.hidden_size))



				def  _lowercase					(self    :				str     ,  _A    :				Union[str, Any]     ,  _A    :				Union[str, Any]     ,  _A    :				List[str])   ->     Union[str, Any]:
					__snake_case      :							Union[str, Any]						=			True
					__snake_case      :							int						=			GPTNeoXJapaneseModel(_A)
					model.to(_A)
					model.eval()
					__snake_case      :							Any						=			model(_A     ,  attention_mask=_A)
					self.parent.assertEqual(result.last_hidden_state.shape     ,  (self.batch_size, self.seq_length, self.hidden_size))



				def  _lowercase					(self    :				int     ,  _A    :				Union[str, Any]     ,  _A    :				Any     ,  _A    :				Optional[Any]     ,  _A    :				Optional[Any])   ->     int:
					__snake_case      :							str						=			GPTNeoXJapaneseForCausalLM(config=_A)
					model.to(_A)
					model.eval()
					__snake_case      :							Optional[int]						=			model(_A     ,  attention_mask=_A     ,  labels=_A)
					self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.seq_length, self.vocab_size))



				def  _lowercase					(self    :				List[Any]     ,  _A    :				Dict     ,  _A    :				Optional[Any]     ,  _A    :				Any)   ->     List[str]:
					__snake_case      :							List[Any]						=			True
					__snake_case      :							str						=			GPTNeoXJapaneseForCausalLM(config=_A)
					model.to(_A)
					model.eval()

					# first forward pass
					__snake_case      :							Tuple						=			model(_A     ,  attention_mask=_A     ,  use_cache=_A)
					__snake_case      :							Optional[int]						=			outputs.past_key_values

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

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

					__snake_case      :							Optional[Any]						=			model(_A     ,  attention_mask=_A     ,  output_hidden_states=_A)
					__snake_case      :							List[str]						=			output_from_no_past['hidden_states'][0]
					__snake_case      :							Union[str, Any]						=			model(
					    _A     ,  attention_mask=_A     ,  past_key_values=_A     ,  output_hidden_states=_A     ,  )['hidden_states'][0]

					# select random slice
					__snake_case      :							Optional[Any]						=			ids_tensor((1,)     ,  output_from_past.shape[-1]).item()
					__snake_case      :							Any						=			output_from_no_past[:, -3:, random_slice_idx].detach()
					__snake_case      :							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(_A     ,  _A     ,  atol=1E-3))






				def  _lowercase					(self    :				Dict)   ->     Optional[int]:
					__snake_case      :							Dict						=			self.prepare_config_and_inputs()
					__snake_case      :							Dict						=			config_and_inputs
					__snake_case      :							Optional[Any]						=			{'input_ids': input_ids, 'attention_mask': input_mask}
					return config, inputs_dict







@require_torch
class 		UpperCamelCase      (   lowercase				,     lowercase				,     unittest.TestCase						):
				UpperCAmelCase    :				Union[str, Any]   =		(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
				UpperCAmelCase    :				Tuple   =		(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
				UpperCAmelCase    :				int   =		(
				    {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM}
				    if is_torch_available()
				    else {}
				)
				UpperCAmelCase    :				int   =		False
				UpperCAmelCase    :				str   =		False
				UpperCAmelCase    :				List[str]   =		False
				UpperCAmelCase    :				int   =		False



				def  _lowercase					(self    :				str)   ->     Any:
					__snake_case      :							Optional[int]						=			GPTNeoXJapaneseModelTester(self)
					__snake_case      :							Tuple						=			ConfigTester(self     ,  config_class=_A     ,  hidden_size=37)



				def  _lowercase					(self    :				Tuple)   ->     List[str]:
					self.config_tester.run_common_tests()



				def  _lowercase					(self    :				Tuple)   ->     List[Any]:
					__snake_case      :							str						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(_A     ,  _A     ,  _A)



				def  _lowercase					(self    :				Any)   ->     Optional[Any]:
					__snake_case      :							Dict						=			self.model_tester.prepare_config_and_inputs_for_decoder()
					self.model_tester.create_and_check_model_as_decoder(_A     ,  _A     ,  _A)



				def  _lowercase					(self    :				str)   ->     Dict:
					# This regression test was failing with PyTorch < 1.3
					__snake_case      :							Dict						=			self.model_tester.prepare_config_and_inputs_for_decoder()

					__snake_case      :							Optional[Any]						=			None

					self.model_tester.create_and_check_model_as_decoder(_A     ,  _A     ,  _A)



				def  _lowercase					(self    :				Optional[Any])   ->     int:
					__snake_case      :							List[str]						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_decoder_model_past_large_inputs(_A     ,  _A     ,  _A)



				def  _lowercase					(self    :				List[str])   ->     int:
					__snake_case      :							str						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_causal_lm(*_A)






				@slow
				def  _lowercase					(self    :				List[Any])   ->     Tuple:
					__snake_case      :							List[str]						=			'abeja/gpt-neox-japanese-2.7b'

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

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

					__snake_case      :							Union[str, Any]						=			GPTNeoXJapaneseTokenizer.from_pretrained(_A)
					__snake_case      :							Dict						=			GPTNeoXJapaneseForCausalLM.from_pretrained(_A)

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






"""simple docstring"""







from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig


_a					:					Tuple=			{
    "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/config.json",
    "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/config.json",
    "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/config.json",
    "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json",
    "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/config.json",
    "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/config.json",
    "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/config.json",
    "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json",
}

class 		UpperCamelCase      (   lowercase						):
				UpperCAmelCase    :				str   =		"""albert"""



				def __init__(self    :				Union[str, Any]     ,  _A    :				int=3_00_00     ,  _A    :				Any=1_28     ,  _A    :				Tuple=40_96     ,  _A    :				int=12     ,  _A    :				Tuple=1     ,  _A    :				int=64     ,  _A    :				Optional[Any]=1_63_84     ,  _A    :				Optional[Any]=1     ,  _A    :				List[str]="gelu_new"     ,  _A    :				Any=0     ,  _A    :				Optional[Any]=0     ,  _A    :				List[Any]=5_12     ,  _A    :				List[Any]=2     ,  _A    :				Dict=0.02     ,  _A    :				Union[str, Any]=1E-12     ,  _A    :				Tuple=0.1     ,  _A    :				int="absolute"     ,  _A    :				List[Any]=0     ,  _A    :				str=2     ,  _A    :				int=3     ,  **_A    :				Tuple     ,  )   ->     Union[str, Any]:
					super().__init__(pad_token_id=_A     ,  bos_token_id=_A     ,  eos_token_id=_A     ,  **_A)

					__snake_case      :							str						=			vocab_size
					__snake_case      :							List[Any]						=			embedding_size
					__snake_case      :							Tuple						=			hidden_size
					__snake_case      :							Optional[Any]						=			num_hidden_layers
					__snake_case      :							List[str]						=			num_hidden_groups
					__snake_case      :							Dict						=			num_attention_heads
					__snake_case      :							Any						=			inner_group_num
					__snake_case      :							Union[str, Any]						=			hidden_act
					__snake_case      :							List[Any]						=			intermediate_size
					__snake_case      :							List[str]						=			hidden_dropout_prob
					__snake_case      :							int						=			attention_probs_dropout_prob
					__snake_case      :							Any						=			max_position_embeddings
					__snake_case      :							Optional[int]						=			type_vocab_size
					__snake_case      :							Optional[Any]						=			initializer_range
					__snake_case      :							List[Any]						=			layer_norm_eps
					__snake_case      :							Optional[Any]						=			classifier_dropout_prob
					__snake_case      :							List[str]						=			position_embedding_type







class 		UpperCamelCase      (   lowercase						):



				@property
				def  _lowercase					(self    :				Tuple)   ->     Mapping[str, Mapping[int, str]]:
					if self.task == "multiple-choice":
						__snake_case      :							int						=			{0: 'batch', 1: 'choice', 2: 'sequence'}
					else:
						__snake_case      :							Optional[Any]						=			{0: 'batch', 1: 'sequence'}
					return OrderedDict(
					    [
					        ('input_ids', dynamic_axis),
					        ('attention_mask', dynamic_axis),
					        ('token_type_ids', dynamic_axis),
					    ])





