






import unittest

import numpy as np

from transformers import (
    GPTSanJapaneseConfig,
    GPTSanJapaneseForConditionalGeneration,
    GPTSanJapaneseModel,
    GPTSanJapaneseTokenizer,
    is_torch_available,
)
from transformers.generation import GenerationConfig
from transformers.testing_utils import require_torch, slow, tooslow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin
class 							A_      :






 '''simple docstring'''




 def __init__(		self							,					_A							,					_A=36000							,					_A=13							,					_A=7							,					_A=True							,					_A=32							,					_A=42							,					_A=5							,					_A=2							,					_A=4							,					_A=2							,					_A=32							,					_A=80							,					_A=33							,					_A=0.0							,					_A=1e-6							,					_A=100							,					_A=0.0							,					)	->						Any:

     """simple docstring"""



     _UpperCAmelCase			: Tuple     					=			vocab_size
     _UpperCAmelCase			: Dict     					=			parent
     _UpperCAmelCase			: Dict     					=			batch_size
     _UpperCAmelCase			: Dict     					=			num_contexts
     # For common tests
     _UpperCAmelCase			: List[str]     					=			self.num_contexts
     _UpperCAmelCase			: int     					=			is_training
     _UpperCAmelCase			: int     					=			hidden_size
     _UpperCAmelCase			: Union[str, Any]     					=			num_ext_layers
     _UpperCAmelCase			: Dict     					=			ext_size
     _UpperCAmelCase			: Union[str, Any]     					=			num_hidden_layers
     _UpperCAmelCase			: Optional[Any]     					=			num_attention_heads
     _UpperCAmelCase			: int     					=			num_experts
     _UpperCAmelCase			: List[str]     					=			d_ff
     _UpperCAmelCase			: str     					=			d_ext
     _UpperCAmelCase			: Tuple     					=			d_spout
     _UpperCAmelCase			: int     					=			dropout_rate
     _UpperCAmelCase			: List[Any]     					=			layer_norm_epsilon
     _UpperCAmelCase			: List[Any]     					=			expert_capacity
     _UpperCAmelCase			: int     					=			router_jitter_noise


 def 	snake_case__			(		self)	->						Optional[Any]:

     """simple docstring"""



     return GPTSanJapaneseConfig.from_pretrained('''Tanrei/GPTSAN-japanese''')


 def 	snake_case__			(		self)	->						Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase			: str     					=			ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size)

     _UpperCAmelCase			: List[str]     					=			self.get_config()

     return (config, input_ids)


 def 	snake_case__			(		self)	->						Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase			: str     					=			ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size)

     _UpperCAmelCase			: Any     					=			self.get_config()

     return (config, {"input_ids": input_ids})


 def 	snake_case__			(		self)	->						int:

     """simple docstring"""



     return GPTSanJapaneseConfig(
         vocab_size=self.vocab_size							,					num_contexts=self.seq_length							,					d_model=self.hidden_size							,					d_ff=self.d_ff							,					d_ext=self.d_ext							,					d_spout=self.d_spout							,					num_switch_layers=self.num_hidden_layers - self.num_ext_layers							,					num_ext_layers=self.num_ext_layers							,					num_heads=self.num_attention_heads							,					num_experts=self.num_experts							,					expert_capacity=self.expert_capacity							,					dropout_rate=self.dropout_rate							,					layer_norm_epsilon=self.layer_norm_epsilon							,					router_jitter_noise=self.router_jitter_noise							,					)


 def 	snake_case__			(		self							,					_A							,					_A							,					)	->						Optional[int]:

     """simple docstring"""



     _UpperCAmelCase			: str     					=			GPTSanJapaneseForConditionalGeneration(config=_A)
     model.to(_A)
     model.eval()
     _UpperCAmelCase			: Dict     					=			model(
         input_ids=_A							,					)
     self.parent.assertIsNotNone(_A)



@require_torch
class 							A_      (  __lowercase	,				__lowercase	,				unittest.TestCase     ):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:     Union[str, Any]	=   (GPTSanJapaneseModel,) if is_torch_available() else ()
 _SCREAMING_SNAKE_CASE							:     Any	=   (
     {
         "conversational": GPTSanJapaneseForConditionalGeneration,
         "feature-extraction": GPTSanJapaneseForConditionalGeneration,
         "summarization": GPTSanJapaneseForConditionalGeneration,
         "text2text-generation": GPTSanJapaneseForConditionalGeneration,
         "translation": GPTSanJapaneseForConditionalGeneration,
     }
     if is_torch_available()
     else {}
 )
 _SCREAMING_SNAKE_CASE							:     Optional[Any]	=   False
 _SCREAMING_SNAKE_CASE							:     str	=   False
 _SCREAMING_SNAKE_CASE							:     List[str]	=   False
 _SCREAMING_SNAKE_CASE							:     Optional[Any]	=   False
 _SCREAMING_SNAKE_CASE							:     Dict	=   False
 _SCREAMING_SNAKE_CASE							:     List[Any]	=   False
 _SCREAMING_SNAKE_CASE							:     List[Any]	=   False
 _SCREAMING_SNAKE_CASE							:     Optional[Any]	=   False
 # The small GPTSAN_JAPANESE model needs higher percentages for CPU/MP tests
 _SCREAMING_SNAKE_CASE							:     Tuple	=   [0.8, 0.9]


 def 	snake_case__			(		self							,					_A							,					_A							,					_A							,					_A							,					_A)	->						int:

     """simple docstring"""



     if pipeline_test_casse_name == "SummarizationPipelineTests":
         # TODO: fix `_reorder_cache` is not implemented for this model
         return True
     elif pipeline_test_casse_name == "Text2TextGenerationPipelineTests":
         # TODO: check this.
         return True

     return False


 def 	snake_case__			(		self)	->						Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase			: List[Any]     					=			GPTSanJapaneseTester(self)
     _UpperCAmelCase			: str     					=			ConfigTester(self							,					config_class=_A							,					d_model=37)


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

     """simple docstring"""



     GPTSanJapaneseConfig()


 def 	snake_case__			(		self)	->						str:

     """simple docstring"""



     _UpperCAmelCase			: Optional[int]     					=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_A)


 @unittest.skip(
     reason='''skip for now as the computed `max_memory` by `model_split_percents` in the test method will be changed inside `from_pretrained`''')
 def 	snake_case__			(		self)	->						Tuple:

     """simple docstring"""



     super().test_model_parallelism()


 @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''')
 def 	snake_case__			(		self)	->						Union[str, Any]:

     """simple docstring"""



     pass



@require_torch
class 							A_      (  __lowercase	,				__lowercase	,				unittest.TestCase     ):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:     Optional[int]	=   (GPTSanJapaneseForConditionalGeneration,) if is_torch_available() else ()
 _SCREAMING_SNAKE_CASE							:     Tuple	=   False
 _SCREAMING_SNAKE_CASE							:     Optional[int]	=   False
 _SCREAMING_SNAKE_CASE							:     Optional[Any]	=   False
 _SCREAMING_SNAKE_CASE							:     Optional[int]	=   False
 _SCREAMING_SNAKE_CASE							:     Union[str, Any]	=   False
 _SCREAMING_SNAKE_CASE							:     Optional[Any]	=   False
 # The small GPTSAN_JAPANESE model needs higher percentages for CPU/MP tests
 _SCREAMING_SNAKE_CASE							:     int	=   [0.8, 0.9]


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

     """simple docstring"""



     _UpperCAmelCase			: Optional[int]     					=			GPTSanJapaneseTester(self)
     _UpperCAmelCase			: Union[str, Any]     					=			ConfigTester(self							,					config_class=_A							,					d_model=37)


 def 	snake_case__			(		self)	->						int:

     """simple docstring"""



     GPTSanJapaneseConfig()


 def 	snake_case__			(		self)	->						int:

     """simple docstring"""



     _UpperCAmelCase			: Optional[Any]     					=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_A)


 @unittest.skip(
     reason='''skip for now as the computed `max_memory` by `model_split_percents` in the test method will be changed inside `from_pretrained`''')
 def 	snake_case__			(		self)	->						Optional[Any]:

     """simple docstring"""



     super().test_model_parallelism()


 @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''')
 def 	snake_case__			(		self)	->						Dict:

     """simple docstring"""



     pass


 @slow
 def 	snake_case__			(		self)	->						Any:

     """simple docstring"""



     _UpperCAmelCase			: Any     					=			GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese''')
     _UpperCAmelCase			: int     					=			GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese''')
     _UpperCAmelCase			: Union[str, Any]     					=			tokenizer.encode('''武田信玄は'''							,					return_tensors='''pt''')
     _UpperCAmelCase			: Union[str, Any]     					=			model(_A)
     _UpperCAmelCase			: Dict     					=			outputs.logits.detach().cpu().numpy()
     # Output of original model created with mesh-tensoflow
     _UpperCAmelCase			: Union[str, Any]     					=			[
         # fmt: off
         [-12.037839889526367, -12.433061599731445, -14.333840370178223, -12.450345993041992, -11.1661376953125,
         -11.930137634277344, -10.659740447998047, -12.909574508666992, -13.241043090820312, -13.398579597473145,
         -11.107524871826172, -12.3685941696167, -22.97943115234375, -10.481067657470703, -12.484030723571777,
         -12.807360649108887, -14.769700050354004, -12.233579635620117, -13.428145408630371, -22.624177932739258],
         [-7.511149883270264, -8.281851768493652, -7.943127155303955, -7.55021333694458, -6.49869966506958,
         -7.586796283721924, -6.978085994720459, -7.839145183563232, -8.21964168548584, -8.695091247558594,
         -6.706910610198975, -6.6585798263549805, -19.565698623657227, -5.353842735290527, -8.350686073303223,
         -8.039388656616211, -10.856569290161133, -7.75154447555542, -8.819022178649902, -19.51532745361328],
         [-9.73066234588623, -10.223922729492188, -9.932981491088867, -11.857836723327637, -7.662626266479492,
         -11.13529109954834, -7.765097618103027, -11.472923278808594, -9.543149948120117, -11.905633926391602,
         -9.366164207458496, -11.5734281539917, -23.699003219604492, -9.429590225219727, -10.42839241027832,
         -10.585240364074707, -10.94771957397461, -11.095416069030762, -10.390240669250488, -23.769372940063477],
         [-9.728265762329102, -9.859712600708008, -10.09729290008545, -9.678522109985352, -6.879519939422607,
         -9.68487548828125, -4.2803425788879395, -10.018914222717285, -9.308445930480957, -10.63394546508789,
         -8.083646774291992, -9.06301498413086, -21.904266357421875, -8.90160846710205, -8.841876029968262,
         -11.856719970703125, -12.079398155212402, -11.233753204345703, -10.177338600158691, -21.87256622314453],
         [-9.669764518737793, -9.614198684692383, -9.814510345458984, -9.996501922607422, -11.375690460205078,
         -10.113405227661133, -10.546867370605469, -10.04369068145752, -10.907809257507324, -10.504216194152832,
         -11.129199028015137, -10.151124000549316, -21.96586799621582, -9.086349487304688, -11.730339050292969,
         -10.460667610168457, -10.298049926757812, -10.784148216247559, -10.840693473815918, -22.03152847290039],
         # fmt: on
     ]
     _UpperCAmelCase			: List[str]     					=			np.array(_A).flatten()
     _UpperCAmelCase			: Tuple     					=			output_logits[0, :, :20].flatten()

     def check(_A							,					_A							,					_A=5e-4):
         return abs(a - b) < epsilon * max(abs(_A)							,					abs(_A))

     self.assertTrue(np.all([check(target[i]							,					predict[i]) for i in range(len(_A))]))


 @slow
 def 	snake_case__			(		self)	->						Optional[int]:

     """simple docstring"""



     _UpperCAmelCase			: Optional[Any]     					=			GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese''')
     _UpperCAmelCase			: Union[str, Any]     					=			GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese''')
     model.to(_A)

     # set deterministically
     _UpperCAmelCase			: str     					=			GenerationConfig.from_pretrained('''Tanrei/GPTSAN-japanese''')
     _UpperCAmelCase			: Dict     					=			1

     # use different length sentences to test batching
     _UpperCAmelCase			: int     					=			[
         '''甲斐なら武田と言うほど''',
         '''織田信長は、''',
     ]

     _UpperCAmelCase			: Tuple     					=			'''left'''
     _UpperCAmelCase			: Optional[int]     					=			tokenizer(_A							,					return_tensors='''pt'''							,					padding=_A)
     _UpperCAmelCase			: Optional[Any]     					=			inputs['''input_ids'''].to(_A)

     self.assertNotEqual(inputs['''attention_mask'''][0].numpy().tolist()							,					inputs['''attention_mask'''][1].numpy().tolist())

     _UpperCAmelCase			: Any     					=			model.generate(
         input_ids=_A							,					attention_mask=inputs['''attention_mask'''].to(_A)							,					max_new_tokens=3							,					generation_config=_A							,					)

     _UpperCAmelCase			: str     					=			tokenizer(sentences[0]							,					return_tensors='''pt''').input_ids.to(_A)
     _UpperCAmelCase			: str     					=			model.generate(
         input_ids=_A							,					max_new_tokens=3							,					generation_config=_A)

     _UpperCAmelCase			: int     					=			tokenizer(sentences[1]							,					return_tensors='''pt''').input_ids.to(_A)
     _UpperCAmelCase			: Dict     					=			model.generate(input_ids=_A							,					max_new_tokens=3							,					generation_config=_A)

     self.assertNotEqual(inputs_non_padded.shape							,					inputs_padded.shape)

     _UpperCAmelCase			: List[Any]     					=			tokenizer.batch_decode(_A							,					skip_special_tokens=_A)
     _UpperCAmelCase			: Dict     					=			tokenizer.decode(output_non_padded[0]							,					skip_special_tokens=_A)
     _UpperCAmelCase			: int     					=			tokenizer.decode(output_padded[0]							,					skip_special_tokens=_A)

     _UpperCAmelCase			: List[Any]     					=			[
         '''甲斐なら武田と言うほど甲斐の武田''',
         '''織田信長は、このような''',
     ]
     self.assertListEqual(_A							,					_A)
     self.assertListEqual(_A							,					[non_padded_sentence, padded_sentence])


 @tooslow
 def 	snake_case__			(		self)	->						Any:

     """simple docstring"""



     _UpperCAmelCase			: Optional[Any]     					=			GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese''')
     _UpperCAmelCase			: Optional[Any]     					=			GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese''')
     # Output of original model created with mesh-tensoflow
     _UpperCAmelCase			: Optional[Any]     					=			[
         ('''武田信玄は''', 35675),
         ('''武田信玄は、''', 45),
         ('''武田信玄は、この''', 29),
         ('''武田信玄は、このよう''', 30642),
         ('''武田信玄は、このような''', 35680),
         ('''武田信玄は、このような「''', 8640),
         ('''武田信玄は、このような「武田''', 31617),
         ('''武田信玄は、このような「武田家''', 30646),
         ('''武田信玄は、このような「武田家の''', 31617),
         ('''武田信玄は、このような「武田家の家''', 31381),
     ]
     for input, output in target:
         _UpperCAmelCase			: Tuple     					=			tokenizer.encode(_A							,					return_tensors='''pt''')
         _UpperCAmelCase			: Dict     					=			model(_A)
         _UpperCAmelCase			: Union[str, Any]     					=			outputs.logits.detach().cpu().numpy()[0]
         _UpperCAmelCase			: str     					=			np.argmax(output_logits[-1])
         self.assertEqual(_A							,					_A)


 @slow
 def 	snake_case__			(		self)	->						Optional[int]:

     """simple docstring"""



     _UpperCAmelCase			: List[Any]     					=			GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese''')
     _UpperCAmelCase			: Optional[int]     					=			GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese''')
     model.to(_A)

     # set deterministically
     _UpperCAmelCase			: Dict     					=			GenerationConfig.from_pretrained('''Tanrei/GPTSAN-japanese''')
     _UpperCAmelCase			: Tuple     					=			1

     _UpperCAmelCase			: int     					=			'''武田信玄は、'''
     _UpperCAmelCase			: str     					=			tokenizer(_A							,					return_tensors='''pt''').input_ids.to(_A)
     _UpperCAmelCase			: Optional[int]     					=			tokenizer([input_text, input_text]							,					return_tensors='''pt''').input_ids.to(_A)

     # spout from uniform and one-hot
     _UpperCAmelCase			: int     					=			[
         # fmt: off
         [0.87882208, 0.38426396, 0.33220248, 0.43890406, 0.16562252,
         0.04803985, 0.211572  , 0.23188473, 0.37153068, 0.7836377 ,
         0.02160172, 0.38761719, 0.75290772, 0.90198857, 0.34365777,
         0.64168169, 0.44318471, 0.14575746, 0.92562881, 0.40812148,
         0.29019122, 0.88861599, 0.65524846, 0.43563456, 0.38177187,
         0.70832965, 0.81527892, 0.68832812, 0.38833192, 0.4561522 ,
         0.14828817, 0.47248213, 0.54357335, 0.82009566, 0.1338884 ,
         0.02755417, 0.19764677, 0.2422084 , 0.04757674, 0.65409606,
         0.0824589 , 0.03304383, 0.94387689, 0.98764509, 0.82433901,
         0.27646741, 0.64907493, 0.76009406, 0.30087915, 0.17904689,
         0.41601714, 0.67046398, 0.10422822, 0.08447374, 0.07354344,
         0.61423565, 0.70284866, 0.7532333 , 0.1972038 , 0.29575659,
         0.90583886, 0.29265307, 0.50000175, 0.70407655, 0.889363  ,
         0.81904418, 0.66829128, 0.64468815, 0.56563723, 0.85601875,
         0.94924672, 0.00166762, 0.25220643, 0.74540219, 0.67993247,
         0.1549675 , 0.39385352, 0.92153607, 0.63745931, 0.27759043,
         0.84702295, 0.65904271, 0.58676614, 0.8666936 , 0.39607438,
         0.79954983, 0.42220697, 0.39650381, 0.7849864 , 0.56150201,
         0.15678925, 0.14746032, 0.34542114, 0.47026783, 0.11956489,
         0.25421435, 0.33788901, 0.68934842, 0.36424685, 0.71737898,
         0.38983449, 0.94393779, 0.39575588, 0.36616553, 0.87104665,
         0.64630203, 0.22516905, 0.88270804, 0.15031338, 0.75144345,
         0.46459025, 0.85396454, 0.86355643, 0.65139851, 0.70266061,
         0.30241389, 0.81056497, 0.88865969, 0.38773807, 0.70635849,
         0.90718459, 0.43245789, 0.28000654, 0.45935562, 0.08773519,
         0.9552151 , 0.93901511, 0.22489288], # uniform
         [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
          0., 0., 0., 0., 0., 0., 0., 0.],
         # fmt: on
     ]

     _UpperCAmelCase			: Tuple     					=			model.generate(
         input_ids=_A							,					spout=spouts[0]							,					max_new_tokens=20							,					generation_config=_A							,					)

     _UpperCAmelCase			: int     					=			model.generate(
         input_ids=_A							,					spout=spouts[1]							,					max_new_tokens=20							,					generation_config=_A							,					)

     _UpperCAmelCase			: Optional[int]     					=			model.generate(
         input_ids=_A							,					spout=_A							,					max_new_tokens=20							,					generation_config=_A							,					)

     _UpperCAmelCase			: int     					=			tokenizer.decode(outputa[0])
     _UpperCAmelCase			: Any     					=			tokenizer.decode(outputa[0])
     _UpperCAmelCase			: Optional[Any]     					=			tokenizer.batch_decode(_A)

     _UpperCAmelCase			: List[str]     					=			[
         '''武田信玄は、武田氏の滅亡後、武田氏の居城であった甲斐武田氏の居城である''',
         '''武田信玄は、武田家の滅亡を防ぐため、武田家の家臣である武田信虎を討''',
     ]
     self.assertListEqual(_A							,					_A)
     self.assertListEqual(_A							,					[outa_sentence, outa_sentence])


 @slow
 def 	snake_case__			(		self)	->						List[str]:

     """simple docstring"""



     _UpperCAmelCase			: Any     					=			GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese''')
     _UpperCAmelCase			: Optional[int]     					=			GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese''')
     model.to(_A)

     # set deterministically
     _UpperCAmelCase			: Optional[int]     					=			GenerationConfig.from_pretrained('''Tanrei/GPTSAN-japanese''')
     _UpperCAmelCase			: Union[str, Any]     					=			1

     _UpperCAmelCase			: Dict     					=			'''武田信玄'''
     _UpperCAmelCase			: Optional[int]     					=			'''織田信長'''
     _UpperCAmelCase			: Union[str, Any]     					=			'''は、'''
     _UpperCAmelCase			: str     					=			'''が、'''
     _UpperCAmelCase			: List[str]     					=			tokenizer(_A							,					prefix_text=_A							,					return_tensors='''pt''')
     _UpperCAmelCase			: Optional[Any]     					=			tokenizer(_A							,					prefix_text=_A							,					return_tensors='''pt''')
     _UpperCAmelCase			: int     					=			tokenizer([[prefix_text_a, input_text_a], [prefix_text_a, input_text_a]]							,					return_tensors='''pt''')

     _UpperCAmelCase			: Optional[Any]     					=			model.generate(
         input_ids=input_tok_a.input_ids.to(_A)							,					token_type_ids=input_tok_a.token_type_ids.to(_A)							,					max_new_tokens=20							,					generation_config=_A							,					)

     _UpperCAmelCase			: Dict     					=			model.generate(
         input_ids=input_tok_a.input_ids.to(_A)							,					token_type_ids=input_tok_a.token_type_ids.to(_A)							,					max_new_tokens=20							,					generation_config=_A							,					)

     _UpperCAmelCase			: Union[str, Any]     					=			model.generate(
         input_ids=input_tok_a.input_ids.to(_A)							,					token_type_ids=input_tok_a.token_type_ids.to(_A)							,					attention_mask=input_tok_a.attention_mask.to(_A)							,					max_new_tokens=20							,					generation_config=_A							,					)

     _UpperCAmelCase			: List[Any]     					=			tokenizer.decode(outputa[0])
     _UpperCAmelCase			: List[str]     					=			tokenizer.decode(outputa[0])
     _UpperCAmelCase			: Dict     					=			tokenizer.batch_decode(_A)

     _UpperCAmelCase			: int     					=			[
         '''武田信玄は、武田氏の祖である武田信虎を、その子・武田信友を擁して''',
         '''織田信長が、織田信長の妻・お市の方を妻として迎えたという逸話が残''',
     ]
     self.assertListEqual(_A							,					_A)
     self.assertListEqual(_A							,					[outa_sentence, outa_sentence])












import datasets

from .evaluate import (
    apply_no_ans_threshold,
    find_all_best_thresh,
    get_raw_scores,
    make_eval_dict,
    make_qid_to_has_ans,
    merge_eval,
)


SCREAMING_SNAKE_CASE								=  '\\n@inproceedings{Rajpurkar2016SQuAD10,\n  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n  booktitle={EMNLP},\n  year={2016}\n}\n'

SCREAMING_SNAKE_CASE								=  '\nThis metric wrap the official scoring script for version 2 of the Stanford Question\nAnswering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n\nSQuAD2.0 combines the 100,000 questions in SQuAD1.1 with over 50,000 unanswerable questions\nwritten adversarially by crowdworkers to look similar to answerable ones.\nTo do well on SQuAD2.0, systems must not only answer questions when possible, but also\ndetermine when no answer is supported by the paragraph and abstain from answering.\n'

SCREAMING_SNAKE_CASE								=  '\nComputes SQuAD v2 scores (F1 and EM).\nArgs:\n    predictions: List of triple for question-answers to score with the following elements:\n        - the question-answer \'id\' field as given in the references (see below)\n        - the text of the answer\n        - the probability that the question has no answer\n    references: List of question-answers dictionaries with the following key-values:\n            - \'id\': id of the question-answer pair (see above),\n            - \'answers\': a list of Dict {\'text\': text of the answer as a string}\n    no_answer_threshold: float\n        Probability threshold to decide that a question has no answer.\nReturns:\n    \'exact\': Exact match (the normalized answer exactly match the gold answer)\n    \'f1\': The F-score of predicted tokens versus the gold answer\n    \'total\': Number of score considered\n    \'HasAns_exact\': Exact match (the normalized answer exactly match the gold answer)\n    \'HasAns_f1\': The F-score of predicted tokens versus the gold answer\n    \'HasAns_total\': Number of score considered\n    \'NoAns_exact\': Exact match (the normalized answer exactly match the gold answer)\n    \'NoAns_f1\': The F-score of predicted tokens versus the gold answer\n    \'NoAns_total\': Number of score considered\n    \'best_exact\': Best exact match (with varying threshold)\n    \'best_exact_thresh\': No-answer probability threshold associated to the best exact match\n    \'best_f1\': Best F1 (with varying threshold)\n    \'best_f1_thresh\': No-answer probability threshold associated to the best F1\nExamples:\n\n    >>> predictions = [{\'prediction_text\': \'1976\', \'id\': \'56e10a3be3433e1400422b22\', \'no_answer_probability\': 0.}]\n    >>> references = [{\'answers\': {\'answer_start\': [97], \'text\': [\'1976\']}, \'id\': \'56e10a3be3433e1400422b22\'}]\n    >>> squad_v2_metric = datasets.load_metric("squad_v2")\n    >>> results = squad_v2_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {\'exact\': 100.0, \'f1\': 100.0, \'total\': 1, \'HasAns_exact\': 100.0, \'HasAns_f1\': 100.0, \'HasAns_total\': 1, \'best_exact\': 100.0, \'best_exact_thresh\': 0.0, \'best_f1\': 100.0, \'best_f1_thresh\': 0.0}\n'
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION	,				_KWARGS_DESCRIPTION     )
class 							A_      (  datasets.Metric     ):






 '''simple docstring'''




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

     """simple docstring"""



     return datasets.MetricInfo(
         description=_DESCRIPTION							,					citation=_CITATION							,					inputs_description=_KWARGS_DESCRIPTION							,					features=datasets.Features(
             {
                 '''predictions''': {
                     '''id''': datasets.Value('''string'''),
                     '''prediction_text''': datasets.Value('''string'''),
                     '''no_answer_probability''': datasets.Value('''float32'''),
                 },
                 '''references''': {
                     '''id''': datasets.Value('''string'''),
                     '''answers''': datasets.features.Sequence(
                         {'''text''': datasets.Value('''string'''), '''answer_start''': datasets.Value('''int32''')}),
                 },
             })							,					codebase_urls=['''https://rajpurkar.github.io/SQuAD-explorer/''']							,					reference_urls=['''https://rajpurkar.github.io/SQuAD-explorer/''']							,					)


 def 	snake_case__			(		self							,					_A							,					_A							,					_A=1.0)	->						List[Any]:

     """simple docstring"""



     _UpperCAmelCase			: str     					=			{p['''id''']: p['''no_answer_probability'''] for p in predictions}
     _UpperCAmelCase			: List[str]     					=			[{'''paragraphs''': [{'''qas''': references}]}]
     _UpperCAmelCase			: List[Any]     					=			{p['''id''']: p['''prediction_text'''] for p in predictions}

     _UpperCAmelCase			: List[str]     					=			make_qid_to_has_ans(_A)  # maps qid to True/False
     _UpperCAmelCase			: str     					=			[k for k, v in qid_to_has_ans.items() if v]
     _UpperCAmelCase			: List[Any]     					=			[k for k, v in qid_to_has_ans.items() if not v]

     _UpperCAmelCase					,	_UpperCAmelCase			: Union[str, Any]     					=			get_raw_scores(_A							,					_A)
     _UpperCAmelCase			: Optional[Any]     					=			apply_no_ans_threshold(_A							,					_A							,					_A							,					_A)
     _UpperCAmelCase			: List[Any]     					=			apply_no_ans_threshold(_A							,					_A							,					_A							,					_A)
     _UpperCAmelCase			: Optional[Any]     					=			make_eval_dict(_A							,					_A)

     if has_ans_qids:
         _UpperCAmelCase			: Tuple     					=			make_eval_dict(_A							,					_A							,					qid_list=_A)
         merge_eval(_A							,					_A							,					'''HasAns''')
     if no_ans_qids:
         _UpperCAmelCase			: Any     					=			make_eval_dict(_A							,					_A							,					qid_list=_A)
         merge_eval(_A							,					_A							,					'''NoAns''')
     find_all_best_thresh(_A							,					_A							,					_A							,					_A							,					_A							,					_A)
     return dict(_A)




