



'''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 ,				_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 ,				)    ->    List[str]:
							'''simple docstring'''






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







	def   _a   (    self						)    ->    Union[str, Any]:
							'''simple docstring'''






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

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

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

							lowercase        =							self.get_config()

							return config, input_ids, input_mask, token_labels







	def   _a   (    self						)    ->    List[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=_lowerCAmelCase ,				initializer_range=self.initializer_range ,				)







	def   _a   (    self						)    ->    Tuple:
							'''simple docstring'''






							lowercase     ,   lowercase     ,   lowercase     ,   lowercase        =							self.prepare_config_and_inputs()

							lowercase        =							True

							return config, input_ids, input_mask, token_labels







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    List[Any]:
							'''simple docstring'''






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







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    str:
							'''simple docstring'''






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







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    List[str]:
							'''simple docstring'''






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







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    int:
							'''simple docstring'''






							lowercase        =							True
							lowercase        =							GPTNeoXJapaneseForCausalLM(config=_lowerCAmelCase						)
							model.to(_lowerCAmelCase						)
							model.eval()

							# first forward pass
							lowercase        =							model(_lowerCAmelCase ,				attention_mask=_lowerCAmelCase ,				use_cache=_lowerCAmelCase						)
							lowercase        =							outputs.past_key_values

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

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

							lowercase        =							model(_lowerCAmelCase ,				attention_mask=_lowerCAmelCase ,				output_hidden_states=_lowerCAmelCase						)
							lowercase        =							output_from_no_past["""hidden_states"""][0]
							lowercase        =							model(
							    _lowerCAmelCase ,				attention_mask=_lowerCAmelCase ,				past_key_values=_lowerCAmelCase ,				output_hidden_states=_lowerCAmelCase ,				)["""hidden_states"""][0]

							# select random slice
							lowercase        =							ids_tensor((1,) ,				output_from_past.shape[-1]						).item()
							lowercase        =							output_from_no_past[:, -3:, random_slice_idx].detach()
							lowercase        =							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   _a   (    self						)    ->    Optional[Any]:
							'''simple docstring'''






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




@require_torch
class      __UpperCamelCase       (_UpperCAmelCase			,   _UpperCAmelCase			,   unittest.TestCase			):
	__A			  =					(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
	__A			  =					(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
	__A			  =					(
	    {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM}
	    if is_torch_available()
	    else {}
	)
	__A			  =					False
	__A			  =					False
	__A			  =					False
	__A			  =					False







	def   _a   (    self						)    ->    Optional[int]:
							'''simple docstring'''






							lowercase        =							GPTNeoXJapaneseModelTester(self						)
							lowercase        =							ConfigTester(self ,				config_class=_lowerCAmelCase ,				hidden_size=37						)







	def   _a   (    self						)    ->    int:
							'''simple docstring'''






							self.config_tester.run_common_tests()







	def   _a   (    self						)    ->    int:
							'''simple docstring'''






							lowercase     ,   lowercase     ,   lowercase     ,   lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)







	def   _a   (    self						)    ->    List[str]:
							'''simple docstring'''






							lowercase     ,   lowercase     ,   lowercase     ,   lowercase        =							self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)







	def   _a   (    self						)    ->    Dict:
							'''simple docstring'''






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

							lowercase        =							None

							self.model_tester.create_and_check_model_as_decoder(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)







	def   _a   (    self						)    ->    Dict:
							'''simple docstring'''






							lowercase     ,   lowercase     ,   lowercase     ,   lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past_large_inputs(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)







	def   _a   (    self						)    ->    List[str]:
							'''simple docstring'''






							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_causal_lm(*_lowerCAmelCase						)







	@slow
	def   _a   (    self						)    ->    Dict:
							'''simple docstring'''






							lowercase        =							"""abeja/gpt-neox-japanese-2.7b"""

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

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

							lowercase        =							GPTNeoXJapaneseTokenizer.from_pretrained(_lowerCAmelCase						)
							lowercase        =							GPTNeoXJapaneseForCausalLM.from_pretrained(_lowerCAmelCase						)

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







'''simple docstring'''


from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_tf_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_tf_available():
						import tensorflow as tf


lowercase_      :				Union[str, Any]   					=    logging.get_logger(__name__)




@dataclass
class      __UpperCamelCase       (_UpperCAmelCase			):
	__A			  =					[
	    '''no_inference''',
	    '''no_cuda''',
	    '''no_tpu''',
	    '''no_speed''',
	    '''no_memory''',
	    '''no_env_print''',
	    '''no_multi_process''',
	]







	def __init__(    self ,				**_lowerCAmelCase						)    ->    Optional[int]:
							'''simple docstring'''






							for deprecated_arg in self.deprecated_args:
													if deprecated_arg in kwargs:
																			lowercase        =							deprecated_arg[3:]
																			lowercase        =							not kwargs.pop(_lowerCAmelCase						)
																			logger.warning(
																			    F"""{deprecated_arg} is depreciated. Please use --no-{positive_arg} or"""
																			    F""" {positive_arg}={kwargs[positive_arg]}"""						)
							lowercase        =							kwargs.pop("""tpu_name""" ,				self.tpu_name						)
							lowercase        =							kwargs.pop("""device_idx""" ,				self.device_idx						)
							lowercase        =							kwargs.pop("""eager_mode""" ,				self.eager_mode						)
							lowercase        =							kwargs.pop("""use_xla""" ,				self.use_xla						)
							super().__init__(**_lowerCAmelCase						)

	__A			  =					field(
	    default=_UpperCAmelCase			,   metadata={'''help''': '''Name of TPU'''}			,   )
	__A			  =					field(
	    default=0			,   metadata={'''help''': '''CPU / GPU device index. Defaults to 0.'''}			,   )
	__A			  =					field(default=_UpperCAmelCase			,   metadata={'''help''': '''Benchmark models in eager model.'''}			)
	__A			  =					field(
	    default=_UpperCAmelCase			,   metadata={
	        '''help''': '''Benchmark models using XLA JIT compilation. Note that `eager_model` has to be set to `False`.'''
	    }			,   )







	@cached_property
	def   _a   (    self						)    ->    Tuple["tf.distribute.cluster_resolver.TPUClusterResolver"]:
							'''simple docstring'''






							requires_backends(self ,				["""tf"""]						)
							lowercase        =							None
							if self.tpu:
													try:
																			if self.tpu_name:
																									lowercase        =							tf.distribute.cluster_resolver.TPUClusterResolver(self.tpu_name						)
																			else:
																									lowercase        =							tf.distribute.cluster_resolver.TPUClusterResolver()
													except ValueError:
																			lowercase        =							None
							return tpu







	@cached_property
	def   _a   (    self						)    ->    Tuple["tf.distribute.Strategy", "tf.distribute.cluster_resolver.TPUClusterResolver"]:
							'''simple docstring'''






							requires_backends(self ,				["""tf"""]						)
							if self.is_tpu:
													tf.config.experimental_connect_to_cluster(self._setup_tpu						)
													tf.tpu.experimental.initialize_tpu_system(self._setup_tpu						)

													lowercase        =							tf.distribute.TPUStrategy(self._setup_tpu						)
							else:
													# currently no multi gpu is allowed
													if self.is_gpu:
																			# TODO: Currently only single GPU is supported
																			tf.config.set_visible_devices(self.gpu_list[self.device_idx] ,				"""GPU"""						)
																			lowercase        =							tf.distribute.OneDeviceStrategy(device=F"""/gpu:{self.device_idx}"""						)
													else:
																			tf.config.set_visible_devices([] ,				"""GPU"""						)  # disable GPU
																			lowercase        =							tf.distribute.OneDeviceStrategy(device=F"""/cpu:{self.device_idx}"""						)

							return strategy







	@property
	def   _a   (    self						)    ->    bool:
							'''simple docstring'''






							requires_backends(self ,				["""tf"""]						)
							return self._setup_tpu is not None







	@property
	def   _a   (    self						)    ->    "tf.distribute.Strategy":
							'''simple docstring'''






							requires_backends(self ,				["""tf"""]						)
							return self._setup_strategy







	@property
	def   _a   (    self						)    ->    Tuple:
							'''simple docstring'''






							requires_backends(self ,				["""tf"""]						)
							return tf.config.list_physical_devices("""GPU"""						)







	@property
	def   _a   (    self						)    ->    int:
							'''simple docstring'''






							requires_backends(self ,				["""tf"""]						)
							if self.cuda:
													return len(self.gpu_list						)
							return 0







	@property
	def   _a   (    self						)    ->    bool:
							'''simple docstring'''






							return self.n_gpu > 0


