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        lowercase_  :



	"""simple docstring"""





	def __init__(							self		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE=36000		,      __SCREAMING_SNAKE_CASE=13		,      __SCREAMING_SNAKE_CASE=7		,      __SCREAMING_SNAKE_CASE=True		,      __SCREAMING_SNAKE_CASE=32		,      __SCREAMING_SNAKE_CASE=42		,      __SCREAMING_SNAKE_CASE=5		,      __SCREAMING_SNAKE_CASE=2		,      __SCREAMING_SNAKE_CASE=4		,      __SCREAMING_SNAKE_CASE=2		,      __SCREAMING_SNAKE_CASE=32		,      __SCREAMING_SNAKE_CASE=80		,      __SCREAMING_SNAKE_CASE=33		,      __SCREAMING_SNAKE_CASE=0.0		,      __SCREAMING_SNAKE_CASE=1e-6		,      __SCREAMING_SNAKE_CASE=100		,      __SCREAMING_SNAKE_CASE=0.0		,      )						->List[Any]:
								lowerCAmelCase     		=		vocab_size
								lowerCAmelCase     		=		parent
								lowerCAmelCase     		=		batch_size
								lowerCAmelCase     		=		num_contexts
								# For common tests
								lowerCAmelCase     		=		self.num_contexts
								lowerCAmelCase     		=		is_training
								lowerCAmelCase     		=		hidden_size
								lowerCAmelCase     		=		num_ext_layers
								lowerCAmelCase     		=		ext_size
								lowerCAmelCase     		=		num_hidden_layers
								lowerCAmelCase     		=		num_attention_heads
								lowerCAmelCase     		=		num_experts
								lowerCAmelCase     		=		d_ff
								lowerCAmelCase     		=		d_ext
								lowerCAmelCase     		=		d_spout
								lowerCAmelCase     		=		dropout_rate
								lowerCAmelCase     		=		layer_norm_epsilon
								lowerCAmelCase     		=		expert_capacity
								lowerCAmelCase     		=		router_jitter_noise


	def       SCREAMING_SNAKE_CASE_    (							self    )						->int:
								return GPTSanJapaneseConfig.from_pretrained('''Tanrei/GPTSAN-japanese'''    )


	def       SCREAMING_SNAKE_CASE_    (							self    )						->List[Any]:
								lowerCAmelCase     		=		ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size    )

								lowerCAmelCase     		=		self.get_config()

								return (config, input_ids)


	def       SCREAMING_SNAKE_CASE_    (							self    )						->Dict:
								lowerCAmelCase     		=		ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size    )

								lowerCAmelCase     		=		self.get_config()

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


	def       SCREAMING_SNAKE_CASE_    (							self    )						->List[Any]:
								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       SCREAMING_SNAKE_CASE_    (							self		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      )						->Tuple:
								lowerCAmelCase     		=		GPTSanJapaneseForConditionalGeneration(config=__SCREAMING_SNAKE_CASE    )
								model.to(__SCREAMING_SNAKE_CASE    )
								model.eval()
								lowerCAmelCase     		=		model(
								    input_ids=__SCREAMING_SNAKE_CASE		,      )
								self.parent.assertIsNotNone(__SCREAMING_SNAKE_CASE    )




@require_torch
class        lowercase_  (  UpperCamelCase_				,					UpperCamelCase_				,					unittest.TestCase						):



	"""simple docstring"""





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


	def       SCREAMING_SNAKE_CASE_    (							self		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE    )						->Optional[Any]:
								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       SCREAMING_SNAKE_CASE_    (							self    )						->Optional[Any]:
								lowerCAmelCase     		=		GPTSanJapaneseTester(self    )
								lowerCAmelCase     		=		ConfigTester(self		,      config_class=__SCREAMING_SNAKE_CASE		,      d_model=37    )


	def       SCREAMING_SNAKE_CASE_    (							self    )						->str:
								GPTSanJapaneseConfig()


	def       SCREAMING_SNAKE_CASE_    (							self    )						->int:
								lowerCAmelCase     		=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE    )


	@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       SCREAMING_SNAKE_CASE_    (							self    )						->int:
								super().test_model_parallelism()




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




@require_torch
class        lowercase_  (  UpperCamelCase_				,					UpperCamelCase_				,					unittest.TestCase						):



	"""simple docstring"""





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


	def       SCREAMING_SNAKE_CASE_    (							self    )						->List[Any]:
								lowerCAmelCase     		=		GPTSanJapaneseTester(self    )
								lowerCAmelCase     		=		ConfigTester(self		,      config_class=__SCREAMING_SNAKE_CASE		,      d_model=37    )


	def       SCREAMING_SNAKE_CASE_    (							self    )						->Union[str, Any]:
								GPTSanJapaneseConfig()


	def       SCREAMING_SNAKE_CASE_    (							self    )						->Dict:
								lowerCAmelCase     		=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE    )


	@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       SCREAMING_SNAKE_CASE_    (							self    )						->Optional[int]:
								super().test_model_parallelism()


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


	@slow
	def       SCREAMING_SNAKE_CASE_    (							self    )						->List[Any]:
								lowerCAmelCase     		=		GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese'''    )
								lowerCAmelCase     		=		GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese'''    )
								lowerCAmelCase     		=		tokenizer.encode('''武田信玄は'''		,      return_tensors='''pt'''    )
								lowerCAmelCase     		=		model(__SCREAMING_SNAKE_CASE    )
								lowerCAmelCase     		=		outputs.logits.detach().cpu().numpy()
								# Output of original model created with mesh-tensoflow
								lowerCAmelCase     		=		[
								    # 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.5_1_1_1_4_9_8_8_3_2_7_0_2_6_4, -8.2_8_1_8_5_1_7_6_8_4_9_3_6_5_2, -7.9_4_3_1_2_7_1_5_5_3_0_3_9_5_5, -7.5_5_0_2_1_3_3_3_6_9_4_4_5_8, -6.4_9_8_6_9_9_6_6_5_0_6_9_5_8,
								    -7.5_8_6_7_9_6_2_8_3_7_2_1_9_2_4, -6.9_7_8_0_8_5_9_9_4_7_2_0_4_5_9, -7.8_3_9_1_4_5_1_8_3_5_6_3_2_3_2, -8.2_1_9_6_4_1_6_8_5_4_8_5_8_4, -8.6_9_5_0_9_1_2_4_7_5_5_8_5_9_4,
								    -6.7_0_6_9_1_0_6_1_0_1_9_8_9_7_5, -6.6_5_8_5_7_9_8_2_6_3_5_4_9_8_0_5, -19.565698623657227, -5.3_5_3_8_4_2_7_3_5_2_9_0_5_2_7, -8.3_5_0_6_8_6_0_7_3_3_0_3_2_2_3,
								    -8.0_3_9_3_8_8_6_5_6_6_1_6_2_1_1, -10.856569290161133, -7.7_5_1_5_4_4_4_7_5_5_5_5_4_2, -8.8_1_9_0_2_2_1_7_8_6_4_9_9_0_2, -19.51532745361328],
								    [-9.7_3_0_6_6_2_3_4_5_8_8_6_2_3, -10.223922729492188, -9.9_3_2_9_8_1_4_9_1_0_8_8_8_6_7, -11.857836723327637, -7.6_6_2_6_2_6_2_6_6_4_7_9_4_9_2,
								    -11.13529109954834, -7.7_6_5_0_9_7_6_1_8_1_0_3_0_2_7, -11.472923278808594, -9.5_4_3_1_4_9_9_4_8_1_2_0_1_1_7, -11.905633926391602,
								    -9.3_6_6_1_6_4_2_0_7_4_5_8_4_9_6, -11.5734281539917, -23.699003219604492, -9.4_2_9_5_9_0_2_2_5_2_1_9_7_2_7, -10.42839241027832,
								    -10.585240364074707, -10.94771957397461, -11.095416069030762, -10.390240669250488, -23.769372940063477],
								    [-9.7_2_8_2_6_5_7_6_2_3_2_9_1_0_2, -9.8_5_9_7_1_2_6_0_0_7_0_8_0_0_8, -10.09729290008545, -9.6_7_8_5_2_2_1_0_9_9_8_5_3_5_2, -6.8_7_9_5_1_9_9_3_9_4_2_2_6_0_7,
								    -9.6_8_4_8_7_5_4_8_8_2_8_1_2_5, -4.2_8_0_3_4_2_5_7_8_8_8_7_9_3_9_5, -10.018914222717285, -9.3_0_8_4_4_5_9_3_0_4_8_0_9_5_7, -10.63394546508789,
								    -8.0_8_3_6_4_6_7_7_4_2_9_1_9_9_2, -9.0_6_3_0_1_4_9_8_4_1_3_0_8_6, -21.904266357421875, -8.9_0_1_6_0_8_4_6_7_1_0_2_0_5, -8.8_4_1_8_7_6_0_2_9_9_6_8_2_6_2,
								    -11.856719970703125, -12.079398155212402, -11.233753204345703, -10.177338600158691, -21.87256622314453],
								    [-9.6_6_9_7_6_4_5_1_8_7_3_7_7_9_3, -9.6_1_4_1_9_8_6_8_4_6_9_2_3_8_3, -9.8_1_4_5_1_0_3_4_5_4_5_8_9_8_4, -9.9_9_6_5_0_1_9_2_2_6_0_7_4_2_2, -11.375690460205078,
								    -10.113405227661133, -10.546867370605469, -10.04369068145752, -10.907809257507324, -10.504216194152832,
								    -11.129199028015137, -10.151124000549316, -21.96586799621582, -9.0_8_6_3_4_9_4_8_7_3_0_4_6_8_8, -11.730339050292969,
								    -10.460667610168457, -10.298049926757812, -10.784148216247559, -10.840693473815918, -22.03152847290039],
								    # fmt: on
								]
								lowerCAmelCase     		=		np.array(__SCREAMING_SNAKE_CASE    ).flatten()
								lowerCAmelCase     		=		output_logits[0, :, :20].flatten()

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

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


	@slow
	def       SCREAMING_SNAKE_CASE_    (							self    )						->Optional[int]:
								lowerCAmelCase     		=		GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese'''    )
								lowerCAmelCase     		=		GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese'''    )
								model.to(__SCREAMING_SNAKE_CASE    )

								# set deterministically
								lowerCAmelCase     		=		GenerationConfig.from_pretrained('''Tanrei/GPTSAN-japanese'''    )
								lowerCAmelCase     		=		1

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

								lowerCAmelCase     		=		'''left'''
								lowerCAmelCase     		=		tokenizer(__SCREAMING_SNAKE_CASE		,      return_tensors='''pt'''		,      padding=__SCREAMING_SNAKE_CASE    )
								lowerCAmelCase     		=		inputs['''input_ids'''].to(__SCREAMING_SNAKE_CASE    )

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

								lowerCAmelCase     		=		model.generate(
								    input_ids=__SCREAMING_SNAKE_CASE		,      attention_mask=inputs['''attention_mask'''].to(__SCREAMING_SNAKE_CASE    )		,      max_new_tokens=3		,      generation_config=__SCREAMING_SNAKE_CASE		,      )

								lowerCAmelCase     		=		tokenizer(sentences[0]		,      return_tensors='''pt'''    ).input_ids.to(__SCREAMING_SNAKE_CASE    )
								lowerCAmelCase     		=		model.generate(
								    input_ids=__SCREAMING_SNAKE_CASE		,      max_new_tokens=3		,      generation_config=__SCREAMING_SNAKE_CASE    )

								lowerCAmelCase     		=		tokenizer(sentences[1]		,      return_tensors='''pt'''    ).input_ids.to(__SCREAMING_SNAKE_CASE    )
								lowerCAmelCase     		=		model.generate(input_ids=__SCREAMING_SNAKE_CASE		,      max_new_tokens=3		,      generation_config=__SCREAMING_SNAKE_CASE    )

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

								lowerCAmelCase     		=		tokenizer.batch_decode(__SCREAMING_SNAKE_CASE		,      skip_special_tokens=__SCREAMING_SNAKE_CASE    )
								lowerCAmelCase     		=		tokenizer.decode(output_non_padded[0]		,      skip_special_tokens=__SCREAMING_SNAKE_CASE    )
								lowerCAmelCase     		=		tokenizer.decode(output_padded[0]		,      skip_special_tokens=__SCREAMING_SNAKE_CASE    )

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


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


	@slow
	def       SCREAMING_SNAKE_CASE_    (							self    )						->List[str]:
								lowerCAmelCase     		=		GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese'''    )
								lowerCAmelCase     		=		GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese'''    )
								model.to(__SCREAMING_SNAKE_CASE    )

								# set deterministically
								lowerCAmelCase     		=		GenerationConfig.from_pretrained('''Tanrei/GPTSAN-japanese'''    )
								lowerCAmelCase     		=		1

								lowerCAmelCase     		=		'''武田信玄は、'''
								lowerCAmelCase     		=		tokenizer(__SCREAMING_SNAKE_CASE		,      return_tensors='''pt'''    ).input_ids.to(__SCREAMING_SNAKE_CASE    )
								lowerCAmelCase     		=		tokenizer([input_text, input_text]		,      return_tensors='''pt'''    ).input_ids.to(__SCREAMING_SNAKE_CASE    )

								# spout from uniform and one-hot
								lowerCAmelCase     		=		[
								    # fmt: off
								    [0.8_7_8_8_2_2_0_8, 0.3_8_4_2_6_3_9_6, 0.3_3_2_2_0_2_4_8, 0.4_3_8_9_0_4_0_6, 0.1_6_5_6_2_2_5_2,
								    0.0_4_8_0_3_9_8_5, 0.2_1_1_5_7_2  , 0.2_3_1_8_8_4_7_3, 0.3_7_1_5_3_0_6_8, 0.7_8_3_6_3_7_7 ,
								    0.0_2_1_6_0_1_7_2, 0.3_8_7_6_1_7_1_9, 0.7_5_2_9_0_7_7_2, 0.9_0_1_9_8_8_5_7, 0.3_4_3_6_5_7_7_7,
								    0.6_4_1_6_8_1_6_9, 0.4_4_3_1_8_4_7_1, 0.1_4_5_7_5_7_4_6, 0.9_2_5_6_2_8_8_1, 0.4_0_8_1_2_1_4_8,
								    0.2_9_0_1_9_1_2_2, 0.8_8_8_6_1_5_9_9, 0.6_5_5_2_4_8_4_6, 0.4_3_5_6_3_4_5_6, 0.3_8_1_7_7_1_8_7,
								    0.7_0_8_3_2_9_6_5, 0.8_1_5_2_7_8_9_2, 0.6_8_8_3_2_8_1_2, 0.3_8_8_3_3_1_9_2, 0.4_5_6_1_5_2_2 ,
								    0.1_4_8_2_8_8_1_7, 0.4_7_2_4_8_2_1_3, 0.5_4_3_5_7_3_3_5, 0.8_2_0_0_9_5_6_6, 0.1_3_3_8_8_8_4 ,
								    0.0_2_7_5_5_4_1_7, 0.1_9_7_6_4_6_7_7, 0.2_4_2_2_0_8_4 , 0.0_4_7_5_7_6_7_4, 0.6_5_4_0_9_6_0_6,
								    0.0_8_2_4_5_8_9 , 0.0_3_3_0_4_3_8_3, 0.9_4_3_8_7_6_8_9, 0.9_8_7_6_4_5_0_9, 0.8_2_4_3_3_9_0_1,
								    0.2_7_6_4_6_7_4_1, 0.6_4_9_0_7_4_9_3, 0.7_6_0_0_9_4_0_6, 0.3_0_0_8_7_9_1_5, 0.1_7_9_0_4_6_8_9,
								    0.4_1_6_0_1_7_1_4, 0.6_7_0_4_6_3_9_8, 0.1_0_4_2_2_8_2_2, 0.0_8_4_4_7_3_7_4, 0.0_7_3_5_4_3_4_4,
								    0.6_1_4_2_3_5_6_5, 0.7_0_2_8_4_8_6_6, 0.7_5_3_2_3_3_3 , 0.1_9_7_2_0_3_8 , 0.2_9_5_7_5_6_5_9,
								    0.9_0_5_8_3_8_8_6, 0.2_9_2_6_5_3_0_7, 0.5_0_0_0_0_1_7_5, 0.7_0_4_0_7_6_5_5, 0.8_8_9_3_6_3  ,
								    0.8_1_9_0_4_4_1_8, 0.6_6_8_2_9_1_2_8, 0.6_4_4_6_8_8_1_5, 0.5_6_5_6_3_7_2_3, 0.8_5_6_0_1_8_7_5,
								    0.9_4_9_2_4_6_7_2, 0.0_0_1_6_6_7_6_2, 0.2_5_2_2_0_6_4_3, 0.7_4_5_4_0_2_1_9, 0.6_7_9_9_3_2_4_7,
								    0.1_5_4_9_6_7_5 , 0.3_9_3_8_5_3_5_2, 0.9_2_1_5_3_6_0_7, 0.6_3_7_4_5_9_3_1, 0.2_7_7_5_9_0_4_3,
								    0.8_4_7_0_2_2_9_5, 0.6_5_9_0_4_2_7_1, 0.5_8_6_7_6_6_1_4, 0.8_6_6_6_9_3_6 , 0.3_9_6_0_7_4_3_8,
								    0.7_9_9_5_4_9_8_3, 0.4_2_2_2_0_6_9_7, 0.3_9_6_5_0_3_8_1, 0.7_8_4_9_8_6_4 , 0.5_6_1_5_0_2_0_1,
								    0.1_5_6_7_8_9_2_5, 0.1_4_7_4_6_0_3_2, 0.3_4_5_4_2_1_1_4, 0.4_7_0_2_6_7_8_3, 0.1_1_9_5_6_4_8_9,
								    0.2_5_4_2_1_4_3_5, 0.3_3_7_8_8_9_0_1, 0.6_8_9_3_4_8_4_2, 0.3_6_4_2_4_6_8_5, 0.7_1_7_3_7_8_9_8,
								    0.3_8_9_8_3_4_4_9, 0.9_4_3_9_3_7_7_9, 0.3_9_5_7_5_5_8_8, 0.3_6_6_1_6_5_5_3, 0.8_7_1_0_4_6_6_5,
								    0.6_4_6_3_0_2_0_3, 0.2_2_5_1_6_9_0_5, 0.8_8_2_7_0_8_0_4, 0.1_5_0_3_1_3_3_8, 0.7_5_1_4_4_3_4_5,
								    0.4_6_4_5_9_0_2_5, 0.8_5_3_9_6_4_5_4, 0.8_6_3_5_5_6_4_3, 0.6_5_1_3_9_8_5_1, 0.7_0_2_6_6_0_6_1,
								    0.3_0_2_4_1_3_8_9, 0.8_1_0_5_6_4_9_7, 0.8_8_8_6_5_9_6_9, 0.3_8_7_7_3_8_0_7, 0.7_0_6_3_5_8_4_9,
								    0.9_0_7_1_8_4_5_9, 0.4_3_2_4_5_7_8_9, 0.2_8_0_0_0_6_5_4, 0.4_5_9_3_5_5_6_2, 0.0_8_7_7_3_5_1_9,
								    0.9_5_5_2_1_5_1 , 0.9_3_9_0_1_5_1_1, 0.2_2_4_8_9_2_8_8], # 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
								]

								lowerCAmelCase     		=		model.generate(
								    input_ids=__SCREAMING_SNAKE_CASE		,      spout=spouts[0]		,      max_new_tokens=20		,      generation_config=__SCREAMING_SNAKE_CASE		,      )

								lowerCAmelCase     		=		model.generate(
								    input_ids=__SCREAMING_SNAKE_CASE		,      spout=spouts[1]		,      max_new_tokens=20		,      generation_config=__SCREAMING_SNAKE_CASE		,      )

								lowerCAmelCase     		=		model.generate(
								    input_ids=__SCREAMING_SNAKE_CASE		,      spout=__SCREAMING_SNAKE_CASE		,      max_new_tokens=20		,      generation_config=__SCREAMING_SNAKE_CASE		,      )

								lowerCAmelCase     		=		tokenizer.decode(outputa[0]    )
								lowerCAmelCase     		=		tokenizer.decode(outputa[0]    )
								lowerCAmelCase     		=		tokenizer.batch_decode(__SCREAMING_SNAKE_CASE    )

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




	@slow
	def       SCREAMING_SNAKE_CASE_    (							self    )						->Union[str, Any]:
								lowerCAmelCase     		=		GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese'''    )
								lowerCAmelCase     		=		GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese'''    )
								model.to(__SCREAMING_SNAKE_CASE    )

								# set deterministically
								lowerCAmelCase     		=		GenerationConfig.from_pretrained('''Tanrei/GPTSAN-japanese'''    )
								lowerCAmelCase     		=		1

								lowerCAmelCase     		=		'''武田信玄'''
								lowerCAmelCase     		=		'''織田信長'''
								lowerCAmelCase     		=		'''は、'''
								lowerCAmelCase     		=		'''が、'''
								lowerCAmelCase     		=		tokenizer(__SCREAMING_SNAKE_CASE		,      prefix_text=__SCREAMING_SNAKE_CASE		,      return_tensors='''pt'''    )
								lowerCAmelCase     		=		tokenizer(__SCREAMING_SNAKE_CASE		,      prefix_text=__SCREAMING_SNAKE_CASE		,      return_tensors='''pt'''    )
								lowerCAmelCase     		=		tokenizer([[prefix_text_a, input_text_a], [prefix_text_a, input_text_a]]		,      return_tensors='''pt'''    )

								lowerCAmelCase     		=		model.generate(
								    input_ids=input_tok_a.input_ids.to(__SCREAMING_SNAKE_CASE    )		,      token_type_ids=input_tok_a.token_type_ids.to(__SCREAMING_SNAKE_CASE    )		,      max_new_tokens=20		,      generation_config=__SCREAMING_SNAKE_CASE		,      )

								lowerCAmelCase     		=		model.generate(
								    input_ids=input_tok_a.input_ids.to(__SCREAMING_SNAKE_CASE    )		,      token_type_ids=input_tok_a.token_type_ids.to(__SCREAMING_SNAKE_CASE    )		,      max_new_tokens=20		,      generation_config=__SCREAMING_SNAKE_CASE		,      )

								lowerCAmelCase     		=		model.generate(
								    input_ids=input_tok_a.input_ids.to(__SCREAMING_SNAKE_CASE    )		,      token_type_ids=input_tok_a.token_type_ids.to(__SCREAMING_SNAKE_CASE    )		,      attention_mask=input_tok_a.attention_mask.to(__SCREAMING_SNAKE_CASE    )		,      max_new_tokens=20		,      generation_config=__SCREAMING_SNAKE_CASE		,      )

								lowerCAmelCase     		=		tokenizer.decode(outputa[0]    )
								lowerCAmelCase     		=		tokenizer.decode(outputa[0]    )
								lowerCAmelCase     		=		tokenizer.batch_decode(__SCREAMING_SNAKE_CASE    )

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




from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

# Load iris file
lowercase__      :    int          =     load_iris()
iris.keys()


print(f'Target names: \n {iris.target_names} ')
print(f'\n Features: \n {iris.feature_names}')

# Train set e Test set
lowercase__     ,   lowercase__     ,   lowercase__     ,   lowercase__      :    Any          =     train_test_split(
    iris['''data'''], iris['''target'''], random_state=4
)

# KNN

lowercase__      :    int          =     KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)

# new array to test
lowercase__      :    List[str]          =     [[1, 2, 1, 4], [2, 3, 4, 5]]

lowercase__      :    int          =     knn.predict(X_new)

print(
    f'\nNew array: \n {X_new}\n\nTarget Names Prediction: \n'
    f' {iris["target_names"][prediction]}'
)



