


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





		"""simple docstring"""






		def __init__(					self							,    _lowercase							,    _lowercase=1_3							,    _lowercase=7							,    _lowercase=True							,    _lowercase=True							,    _lowercase=True							,    _lowercase=True							,    _lowercase=9_9							,    _lowercase=3_2							,    _lowercase=5							,    _lowercase=4							,    _lowercase=4							,    _lowercase="gelu"							,    _lowercase=0.0							,    _lowercase=0.1							,    _lowercase=True							,    _lowercase=5_1_2							,    _lowercase=1_6							,    _lowercase=2							,    _lowercase=0.02							,    _lowercase=3							,    _lowercase=4							,    _lowercase=None							,    ) ->						List[str]:







								'''simple docstring'''

								snake_case_   : str						=			parent
								snake_case_   : Any						=			batch_size
								snake_case_   : Any						=			seq_length
								snake_case_   : str						=			is_training
								snake_case_   : Any						=			use_input_mask
								snake_case_   : int						=			use_token_type_ids
								snake_case_   : int						=			use_labels
								snake_case_   : str						=			vocab_size
								snake_case_   : List[str]						=			hidden_size
								snake_case_   : Union[str, Any]						=			num_hidden_layers
								snake_case_   : Union[str, Any]						=			num_attention_heads
								snake_case_   : Any						=			intermediate_multiple_size
								snake_case_   : Dict						=			hidden_act
								snake_case_   : Optional[int]						=			hidden_dropout
								snake_case_   : Optional[Any]						=			attention_dropout
								snake_case_   : Tuple						=			weight_tying
								snake_case_   : Tuple						=			max_position_embeddings
								snake_case_   : Union[str, Any]						=			type_vocab_size
								snake_case_   : str						=			type_sequence_label_size
								snake_case_   : Any						=			initializer_range
								snake_case_   : str						=			num_labels
								snake_case_   : int						=			num_choices
								snake_case_   : Optional[int]						=			scope





		def    UpperCAmelCase__							(					self   ) ->						int:







								'''simple docstring'''

								snake_case_   : Tuple						=			ids_tensor([self.batch_size, self.seq_length]							,    self.vocab_size   )

								snake_case_   : Dict						=			None
								if self.use_input_mask:
														snake_case_   : Union[str, Any]						=			random_attention_mask([self.batch_size, self.seq_length]   )

								snake_case_   : Optional[Any]						=			None
								if self.use_labels:
														snake_case_   : Union[str, Any]						=			ids_tensor([self.batch_size, self.seq_length]							,    self.num_labels   )

								snake_case_   : Optional[Any]						=			self.get_config()

								return config, input_ids, input_mask, token_labels





		def    UpperCAmelCase__							(					self   ) ->						str:







								'''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=lowercase_							,    initializer_range=self.initializer_range							,    )





		def    UpperCAmelCase__							(					self   ) ->						str:







								'''simple docstring'''

								snake_case_			,		snake_case_			,		snake_case_			,		snake_case_   : str						=			self.prepare_config_and_inputs()

								snake_case_   : Optional[int]						=			True

								return config, input_ids, input_mask, token_labels





		def    UpperCAmelCase__							(					self							,    _lowercase							,    _lowercase							,    _lowercase   ) ->						int:







								'''simple docstring'''

								snake_case_   : Optional[int]						=			GPTNeoXJapaneseModel(config=lowercase_   )
								model.to(lowercase_   )
								model.eval()
								snake_case_   : Union[str, Any]						=			model(lowercase_							,    attention_mask=lowercase_   )
								snake_case_   : Union[str, Any]						=			model(lowercase_   )
								self.parent.assertEqual(result.last_hidden_state.shape							,    (self.batch_size, self.seq_length, self.hidden_size)   )





		def    UpperCAmelCase__							(					self							,    _lowercase							,    _lowercase							,    _lowercase   ) ->						Any:







								'''simple docstring'''

								snake_case_   : int						=			True
								snake_case_   : Tuple						=			GPTNeoXJapaneseModel(lowercase_   )
								model.to(lowercase_   )
								model.eval()
								snake_case_   : Union[str, Any]						=			model(lowercase_							,    attention_mask=lowercase_   )
								self.parent.assertEqual(result.last_hidden_state.shape							,    (self.batch_size, self.seq_length, self.hidden_size)   )





		def    UpperCAmelCase__							(					self							,    _lowercase							,    _lowercase							,    _lowercase							,    _lowercase   ) ->						Optional[int]:







								'''simple docstring'''

								snake_case_   : Tuple						=			GPTNeoXJapaneseForCausalLM(config=lowercase_   )
								model.to(lowercase_   )
								model.eval()
								snake_case_   : Optional[int]						=			model(lowercase_							,    attention_mask=lowercase_							,    labels=lowercase_   )
								self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.seq_length, self.vocab_size)   )





		def    UpperCAmelCase__							(					self							,    _lowercase							,    _lowercase							,    _lowercase   ) ->						str:







								'''simple docstring'''

								snake_case_   : Any						=			True
								snake_case_   : Optional[int]						=			GPTNeoXJapaneseForCausalLM(config=lowercase_   )
								model.to(lowercase_   )
								model.eval()

								# first forward pass
								snake_case_   : Dict						=			model(lowercase_							,    attention_mask=lowercase_							,    use_cache=lowercase_   )
								snake_case_   : Any						=			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_   : Tuple						=			ids_tensor((self.batch_size, 3)							,    vocab_size=2   )

								# append to next input_ids and
								snake_case_   : List[str]						=			torch.cat([input_ids, next_tokens]							,    dim=-1   )
								snake_case_   : int						=			torch.cat([input_mask, next_mask]							,    dim=-1   )

								snake_case_   : int						=			model(lowercase_							,    attention_mask=lowercase_							,    output_hidden_states=lowercase_   )
								snake_case_   : List[str]						=			output_from_no_past["""hidden_states"""][0]
								snake_case_   : Optional[int]						=			model(
								    lowercase_							,    attention_mask=lowercase_							,    past_key_values=lowercase_							,    output_hidden_states=lowercase_							,    )["""hidden_states"""][0]

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





		def    UpperCAmelCase__							(					self   ) ->						Dict:







								'''simple docstring'''

								snake_case_   : List[Any]						=			self.prepare_config_and_inputs()
								snake_case_			,		snake_case_			,		snake_case_			,		snake_case_   : List[str]						=			config_and_inputs
								snake_case_   : str						=			{"""input_ids""": input_ids, """attention_mask""": input_mask}
								return config, inputs_dict




@require_torch
class    _lowerCAmelCase    (				SCREAMING_SNAKE_CASE__  ,				SCREAMING_SNAKE_CASE__  ,				unittest.TestCase		):





		"""simple docstring"""






		_lowerCamelCase			  =       (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
		_lowerCamelCase			  =       (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
		_lowerCamelCase			  =       (
		    {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM}
		    if is_torch_available()
		    else {}
		)
		_lowerCamelCase			  =       False
		_lowerCamelCase			  =       False
		_lowerCamelCase			  =       False
		_lowerCamelCase			  =       False





		def    UpperCAmelCase__							(					self   ) ->						Any:







								'''simple docstring'''

								snake_case_   : Union[str, Any]						=			GPTNeoXJapaneseModelTester(self   )
								snake_case_   : Dict						=			ConfigTester(self							,    config_class=lowercase_							,    hidden_size=3_7   )





		def    UpperCAmelCase__							(					self   ) ->						List[str]:







								'''simple docstring'''

								self.config_tester.run_common_tests()





		def    UpperCAmelCase__							(					self   ) ->						str:







								'''simple docstring'''

								snake_case_			,		snake_case_			,		snake_case_			,		snake_case_   : Any						=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(lowercase_							,    lowercase_							,    lowercase_   )





		def    UpperCAmelCase__							(					self   ) ->						Tuple:







								'''simple docstring'''

								snake_case_			,		snake_case_			,		snake_case_			,		snake_case_   : List[str]						=			self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_model_as_decoder(lowercase_							,    lowercase_							,    lowercase_   )





		def    UpperCAmelCase__							(					self   ) ->						List[str]:







								'''simple docstring'''

								snake_case_			,		snake_case_			,		snake_case_			,		snake_case_   : Optional[int]						=			self.model_tester.prepare_config_and_inputs_for_decoder()

								snake_case_   : List[Any]						=			None

								self.model_tester.create_and_check_model_as_decoder(lowercase_							,    lowercase_							,    lowercase_   )





		def    UpperCAmelCase__							(					self   ) ->						List[Any]:







								'''simple docstring'''

								snake_case_			,		snake_case_			,		snake_case_			,		snake_case_   : str						=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_decoder_model_past_large_inputs(lowercase_							,    lowercase_							,    lowercase_   )





		def    UpperCAmelCase__							(					self   ) ->						List[Any]:







								'''simple docstring'''

								snake_case_   : Union[str, Any]						=			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_causal_lm(*lowercase_   )





		@slow
		def    UpperCAmelCase__							(					self   ) ->						Union[str, Any]:







								'''simple docstring'''

								snake_case_   : Tuple						=			"""abeja/gpt-neox-japanese-2.7b"""

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

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

								snake_case_   : int						=			GPTNeoXJapaneseTokenizer.from_pretrained(lowercase_   )
								snake_case_   : Any						=			GPTNeoXJapaneseForCausalLM.from_pretrained(lowercase_   )

								snake_case_   : Any						=			[]
								for prompt in prompts:
														snake_case_   : int						=			tokenizer(lowercase_							,    return_tensors="""pt"""   ).input_ids
														snake_case_   : Union[str, Any]						=			model.generate(lowercase_							,    max_length=5_0   )
														snake_case_   : str						=			tokenizer.batch_decode(lowercase_							,    skip_special_tokens=lowercase_   )
														predicted_outputs += generated_string
								self.assertListEqual(lowercase_							,    lowercase_   )






"""simple docstring"""





from collections.abc import Callable
from math import pi, sqrt
from random import uniform
from statistics import mean
def 	__lowerCAmelCase						(		__UpperCamelCase      :			int    ):


						'''simple docstring'''






						def is_in_circle(__UpperCamelCase      :			float					, __UpperCamelCase      :			float    ) -> bool:
												snake_case_   : Dict						=			sqrt((x**2) + (y**2)    )
												# Our circle has a radius of 1, so a distance
												# greater than 1 would land outside the circle.
												return distance_from_centre <= 1

						# The proportion of guesses that landed in the circle
						snake_case_   : Tuple						=			mean(
						    int(is_in_circle(uniform(-1.0					, 1.0    )					, uniform(-1.0					, 1.0    )    )    )
						    for _ in range(__UpperCamelCase    )    )
						# The ratio of the area for circle to square is pi/4.
						snake_case_   : Union[str, Any]						=			proportion * 4
						print(F'The estimated value of pi is {pi_estimate}'    )
						print(F'The numpy value of pi is {pi}'    )
						print(F'The total error is {abs(pi - pi_estimate    )}'    )
def 	__lowerCAmelCase						(		__UpperCamelCase      :			int					, __UpperCamelCase      :			Callable[[float], float]					, __UpperCamelCase      :			float = 0.0					, __UpperCamelCase      :			float = 1.0					, ):


						'''simple docstring'''






						return mean(
						    function_to_integrate(uniform(__UpperCamelCase					, __UpperCamelCase    )    ) for _ in range(__UpperCamelCase    )    ) * (max_value - min_value)
def 	__lowerCAmelCase						(		__UpperCamelCase      :			int					, __UpperCamelCase      :			float = 0.0					, __UpperCamelCase      :			float = 1.0    ):


						'''simple docstring'''






						def identity_function(__UpperCamelCase      :			float    ) -> float:
												return x

						snake_case_   : int						=			area_under_curve_estimator(
						    __UpperCamelCase					, __UpperCamelCase					, __UpperCamelCase					, __UpperCamelCase    )
						snake_case_   : str						=			(max_value * max_value - min_value * min_value) / 2

						print("""******************"""    )
						print(F'Estimating area under y=x where x varies from {min_value} to {max_value}'    )
						print(F'Estimated value is {estimated_value}'    )
						print(F'Expected value is {expected_value}'    )
						print(F'Total error is {abs(estimated_value - expected_value    )}'    )
						print("""******************"""    )
def 	__lowerCAmelCase						(		__UpperCamelCase      :			int    ):


						'''simple docstring'''






						def function_to_integrate(__UpperCamelCase      :			float    ) -> float:
												return sqrt(4.0 - x * x    )

						snake_case_   : List[Any]						=			area_under_curve_estimator(
						    __UpperCamelCase					, __UpperCamelCase					, 0.0					, 2.0    )

						print("""******************"""    )
						print("""Estimating pi using area_under_curve_estimator"""    )
						print(F'Estimated value is {estimated_value}'    )
						print(F'Expected value is {pi}'    )
						print(F'Total error is {abs(estimated_value - pi    )}'    )
						print("""******************"""    )


if __name__ == "__main__":
					import doctest

					doctest.testmod()

