



'''simple docstring'''






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






		'''simple docstring'''
		def __init__(						self							:       int      ,		UpperCamelCase_							:       List[Any]      ,		UpperCamelCase_							:       List[Any]=36_000      ,		UpperCamelCase_							:       Union[str, Any]=13      ,		UpperCamelCase_							:       str=7      ,		UpperCamelCase_							:       List[str]=True      ,		UpperCamelCase_							:       Tuple=32      ,		UpperCamelCase_							:       Any=42      ,		UpperCamelCase_							:       Union[str, Any]=5      ,		UpperCamelCase_							:       Optional[Any]=2      ,		UpperCamelCase_							:       List[Any]=4      ,		UpperCamelCase_							:       Optional[Any]=2      ,		UpperCamelCase_							:       Dict=32      ,		UpperCamelCase_							:       Dict=80      ,		UpperCamelCase_							:       Optional[int]=33      ,		UpperCamelCase_							:       List[Any]=0.0      ,		UpperCamelCase_							:       Optional[int]=1e-6      ,		UpperCamelCase_							:       Tuple=100      ,		UpperCamelCase_							:       str=0.0      ,		)      ->			int:







									'''simple docstring'''





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







									'''simple docstring'''





									return GPTSanJapaneseConfig.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
		def 						__lowercase      (						self							:       Optional[Any]		)      ->			str:







									'''simple docstring'''





									_lowercase							:		Optional[Any]				=   ids_tensor([self.batch_size, self.seq_length]      ,		self.vocab_size		)

									_lowercase							:		Union[str, Any]				=   self.get_config()

									return (config, input_ids)
		def 						__lowercase      (						self							:       Union[str, Any]		)      ->			str:







									'''simple docstring'''





									_lowercase							:		Optional[Any]				=   ids_tensor([self.batch_size, self.seq_length]      ,		self.vocab_size		)

									_lowercase							:		int				=   self.get_config()

									return (config, {"input_ids": input_ids})
		def 						__lowercase      (						self							:       Any		)      ->			Any:







									'''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 						__lowercase      (						self							:       List[str]      ,		UpperCamelCase_							:       int      ,		UpperCamelCase_							:       Optional[int]      ,		)      ->			int:







									'''simple docstring'''





									_lowercase							:		Union[str, Any]				=   GPTSanJapaneseForConditionalGeneration(config=UpperCAmelCase_		)
									model.to(UpperCAmelCase_		)
									model.eval()
									_lowercase							:		Tuple				=   model(
									    input_ids=UpperCAmelCase_      ,		)
									self.parent.assertIsNotNone(UpperCAmelCase_		)




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






		'''simple docstring'''
		snake_case_							= (GPTSanJapaneseModel,) if is_torch_available() else ()
		snake_case_							= (
		    {
		        'conversational': GPTSanJapaneseForConditionalGeneration,
		        'feature-extraction': GPTSanJapaneseForConditionalGeneration,
		        'summarization': GPTSanJapaneseForConditionalGeneration,
		        'text2text-generation': GPTSanJapaneseForConditionalGeneration,
		        'translation': GPTSanJapaneseForConditionalGeneration,
		    }
		    if is_torch_available()
		    else {}
		)
		snake_case_							= False
		snake_case_							= False
		snake_case_							= False
		snake_case_							= False
		snake_case_							= False
		snake_case_							= False
		snake_case_							= False
		snake_case_							= False
		# The small GPTSAN_JAPANESE model needs higher percentages for CPU/MP tests
		snake_case_							= [0.8, 0.9]
		def 						__lowercase      (						self							:       Union[str, Any]      ,		UpperCamelCase_							:       str      ,		UpperCamelCase_							:       Optional[int]      ,		UpperCamelCase_							:       Optional[int]      ,		UpperCamelCase_							:       Any      ,		UpperCamelCase_							:       Optional[Any]		)      ->			Tuple:







									'''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 						__lowercase      (						self							:       Optional[Any]		)      ->			Any:







									'''simple docstring'''





									_lowercase							:		Any				=   GPTSanJapaneseTester(self		)
									_lowercase							:		Union[str, Any]				=   ConfigTester(self      ,		config_class=UpperCAmelCase_      ,		d_model=37		)
		def 						__lowercase      (						self							:       str		)      ->			List[Any]:







									'''simple docstring'''





									GPTSanJapaneseConfig()
		def 						__lowercase      (						self							:       Optional[Any]		)      ->			str:







									'''simple docstring'''





									_lowercase							:		List[Any]				=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*UpperCAmelCase_		)
		@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 						__lowercase      (						self							:       Any		)      ->			Optional[int]:







									'''simple docstring'''





									super().test_model_parallelism()





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







									'''simple docstring'''





									pass




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






		'''simple docstring'''
		snake_case_							= (GPTSanJapaneseForConditionalGeneration,) if is_torch_available() else ()
		snake_case_							= False
		snake_case_							= False
		snake_case_							= False
		snake_case_							= False
		snake_case_							= False
		snake_case_							= False
		# The small GPTSAN_JAPANESE model needs higher percentages for CPU/MP tests
		snake_case_							= [0.8, 0.9]
		def 						__lowercase      (						self							:       List[Any]		)      ->			List[Any]:







									'''simple docstring'''





									_lowercase							:		Optional[Any]				=   GPTSanJapaneseTester(self		)
									_lowercase							:		int				=   ConfigTester(self      ,		config_class=UpperCAmelCase_      ,		d_model=37		)
		def 						__lowercase      (						self							:       str		)      ->			List[Any]:







									'''simple docstring'''





									GPTSanJapaneseConfig()
		def 						__lowercase      (						self							:       List[str]		)      ->			Tuple:







									'''simple docstring'''





									_lowercase							:		Optional[Any]				=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*UpperCAmelCase_		)
		@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 						__lowercase      (						self							:       Union[str, Any]		)      ->			str:







									'''simple docstring'''





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







									'''simple docstring'''





									pass
		@slow
		def 						__lowercase      (						self							:       Tuple		)      ->			int:







									'''simple docstring'''





									_lowercase							:		List[str]				=   GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									_lowercase							:		Dict				=   GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									_lowercase							:		Dict				=   tokenizer.encode('''武田信玄は'''      ,		return_tensors='''pt'''		)
									_lowercase							:		int				=   model(UpperCAmelCase_		)
									_lowercase							:		Union[str, Any]				=   outputs.logits.detach().cpu().numpy()
									# Output of original model created with mesh-tensoflow
									_lowercase							:		List[Any]				=   [
									    # fmt: off
									    [-12.037_839_889_526_367, -12.433_061_599_731_445, -14.333_840_370_178_223, -12.450_345_993_041_992, -11.1_661_376_953_125,
									    -11.930_137_634_277_344, -10.659_740_447_998_047, -12.909_574_508_666_992, -13.241_043_090_820_312, -13.398_579_597_473_145,
									    -11.107_524_871_826_172, -12.3_685_941_696_167, -22.97_943_115_234_375, -10.481_067_657_470_703, -12.484_030_723_571_777,
									    -12.807_360_649_108_887, -14.769_700_050_354_004, -12.233_579_635_620_117, -13.428_145_408_630_371, -22.624_177_932_739_258],
									    [-7.511_149_883_270_264, -8.281_851_768_493_652, -7.943_127_155_303_955, -7.55_021_333_694_458, -6.49_869_966_506_958,
									    -7.586_796_283_721_924, -6.978_085_994_720_459, -7.839_145_183_563_232, -8.21_964_168_548_584, -8.695_091_247_558_594,
									    -6.706_910_610_198_975, -6.6_585_798_263_549_805, -19.565_698_623_657_227, -5.353_842_735_290_527, -8.350_686_073_303_223,
									    -8.039_388_656_616_211, -10.856_569_290_161_133, -7.75_154_447_555_542, -8.819_022_178_649_902, -19.51_532_745_361_328],
									    [-9.73_066_234_588_623, -10.223_922_729_492_188, -9.932_981_491_088_867, -11.857_836_723_327_637, -7.662_626_266_479_492,
									    -11.13_529_109_954_834, -7.765_097_618_103_027, -11.472_923_278_808_594, -9.543_149_948_120_117, -11.905_633_926_391_602,
									    -9.366_164_207_458_496, -11.5_734_281_539_917, -23.699_003_219_604_492, -9.429_590_225_219_727, -10.42_839_241_027_832,
									    -10.585_240_364_074_707, -10.94_771_957_397_461, -11.095_416_069_030_762, -10.390_240_669_250_488, -23.769_372_940_063_477],
									    [-9.728_265_762_329_102, -9.859_712_600_708_008, -10.09_729_290_008_545, -9.678_522_109_985_352, -6.879_519_939_422_607,
									    -9.68_487_548_828_125, -4.2_803_425_788_879_395, -10.018_914_222_717_285, -9.308_445_930_480_957, -10.63_394_546_508_789,
									    -8.083_646_774_291_992, -9.06_301_498_413_086, -21.904_266_357_421_875, -8.90_160_846_710_205, -8.841_876_029_968_262,
									    -11.856_719_970_703_125, -12.079_398_155_212_402, -11.233_753_204_345_703, -10.177_338_600_158_691, -21.87_256_622_314_453],
									    [-9.669_764_518_737_793, -9.614_198_684_692_383, -9.814_510_345_458_984, -9.996_501_922_607_422, -11.375_690_460_205_078,
									    -10.113_405_227_661_133, -10.546_867_370_605_469, -10.04_369_068_145_752, -10.907_809_257_507_324, -10.504_216_194_152_832,
									    -11.129_199_028_015_137, -10.151_124_000_549_316, -21.96_586_799_621_582, -9.086_349_487_304_688, -11.730_339_050_292_969,
									    -10.460_667_610_168_457, -10.298_049_926_757_812, -10.784_148_216_247_559, -10.840_693_473_815_918, -22.03_152_847_290_039],
									    # fmt: on
									]
									_lowercase							:		Optional[int]				=   np.array(UpperCAmelCase_		).flatten()
									_lowercase							:		int				=   output_logits[0, :, :20].flatten()

									def check(UpperCamelCase_							:       List[Any]      ,		UpperCamelCase_							:       List[Any]      ,		UpperCamelCase_							:       Any=5e-4		):
																return abs(a - b		) < epsilon * max(abs(UpperCAmelCase_		)      ,		abs(UpperCAmelCase_		)		)

									self.assertTrue(np.all([check(target[i]      ,		predict[i]		) for i in range(len(UpperCAmelCase_		)		)]		)		)
		@slow
		def 						__lowercase      (						self							:       Any		)      ->			Tuple:







									'''simple docstring'''





									_lowercase							:		List[str]				=   GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									_lowercase							:		List[Any]				=   GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									model.to(UpperCAmelCase_		)

									# set deterministically
									_lowercase							:		List[str]				=   GenerationConfig.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									_lowercase							:		Dict				=   1

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

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

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

									_lowercase							:		List[str]				=   model.generate(
									    input_ids=UpperCAmelCase_      ,		attention_mask=inputs['''attention_mask'''].to(UpperCAmelCase_		)      ,		max_new_tokens=3      ,		generation_config=UpperCAmelCase_      ,		)

									_lowercase							:		Union[str, Any]				=   tokenizer(sentences[0]      ,		return_tensors='''pt'''		).input_ids.to(UpperCAmelCase_		)
									_lowercase							:		List[Any]				=   model.generate(
									    input_ids=UpperCAmelCase_      ,		max_new_tokens=3      ,		generation_config=UpperCAmelCase_		)

									_lowercase							:		Union[str, Any]				=   tokenizer(sentences[1]      ,		return_tensors='''pt'''		).input_ids.to(UpperCAmelCase_		)
									_lowercase							:		Optional[int]				=   model.generate(input_ids=UpperCAmelCase_      ,		max_new_tokens=3      ,		generation_config=UpperCAmelCase_		)

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

									_lowercase							:		Optional[Any]				=   tokenizer.batch_decode(UpperCAmelCase_      ,		skip_special_tokens=UpperCAmelCase_		)
									_lowercase							:		Union[str, Any]				=   tokenizer.decode(output_non_padded[0]      ,		skip_special_tokens=UpperCAmelCase_		)
									_lowercase							:		Dict				=   tokenizer.decode(output_padded[0]      ,		skip_special_tokens=UpperCAmelCase_		)

									_lowercase							:		str				=   [
									    '''甲斐なら武田と言うほど甲斐の武田''',
									    '''織田信長は、このような''',
									]
									self.assertListEqual(UpperCAmelCase_      ,		UpperCAmelCase_		)
									self.assertListEqual(UpperCAmelCase_      ,		[non_padded_sentence, padded_sentence]		)
		@tooslow
		def 						__lowercase      (						self							:       Dict		)      ->			Optional[Any]:







									'''simple docstring'''





									_lowercase							:		List[Any]				=   GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									_lowercase							:		int				=   GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									# Output of original model created with mesh-tensoflow
									_lowercase							:		Tuple				=   [
									    ('''武田信玄は''', 35_675),
									    ('''武田信玄は、''', 45),
									    ('''武田信玄は、この''', 29),
									    ('''武田信玄は、このよう''', 30_642),
									    ('''武田信玄は、このような''', 35_680),
									    ('''武田信玄は、このような「''', 8_640),
									    ('''武田信玄は、このような「武田''', 31_617),
									    ('''武田信玄は、このような「武田家''', 30_646),
									    ('''武田信玄は、このような「武田家の''', 31_617),
									    ('''武田信玄は、このような「武田家の家''', 31_381),
									]
									for input, output in target:
																_lowercase							:		int				=   tokenizer.encode(UpperCAmelCase_      ,		return_tensors='''pt'''		)
																_lowercase							:		Dict				=   model(UpperCAmelCase_		)
																_lowercase							:		Union[str, Any]				=   outputs.logits.detach().cpu().numpy()[0]
																_lowercase							:		Dict				=   np.argmax(output_logits[-1]		)
																self.assertEqual(UpperCAmelCase_      ,		UpperCAmelCase_		)
		@slow
		def 						__lowercase      (						self							:       Any		)      ->			List[Any]:







									'''simple docstring'''





									_lowercase							:		List[Any]				=   GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									_lowercase							:		Tuple				=   GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									model.to(UpperCAmelCase_		)

									# set deterministically
									_lowercase							:		Optional[int]				=   GenerationConfig.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									_lowercase							:		Optional[int]				=   1

									_lowercase							:		Optional[int]				=   '''武田信玄は、'''
									_lowercase							:		Any				=   tokenizer(UpperCAmelCase_      ,		return_tensors='''pt'''		).input_ids.to(UpperCAmelCase_		)
									_lowercase							:		Dict				=   tokenizer([input_text, input_text]      ,		return_tensors='''pt'''		).input_ids.to(UpperCAmelCase_		)

									# spout from uniform and one-hot
									_lowercase							:		Dict				=   [
									    # fmt: off
									    [0.87_882_208, 0.38_426_396, 0.33_220_248, 0.43_890_406, 0.16_562_252,
									    0.04_803_985, 0.211_572  , 0.23_188_473, 0.37_153_068, 0.7_836_377 ,
									    0.02_160_172, 0.38_761_719, 0.75_290_772, 0.90_198_857, 0.34_365_777,
									    0.64_168_169, 0.44_318_471, 0.14_575_746, 0.92_562_881, 0.40_812_148,
									    0.29_019_122, 0.88_861_599, 0.65_524_846, 0.43_563_456, 0.38_177_187,
									    0.70_832_965, 0.81_527_892, 0.68_832_812, 0.38_833_192, 0.4_561_522 ,
									    0.14_828_817, 0.47_248_213, 0.54_357_335, 0.82_009_566, 0.1_338_884 ,
									    0.02_755_417, 0.19_764_677, 0.2_422_084 , 0.04_757_674, 0.65_409_606,
									    0.0_824_589 , 0.03_304_383, 0.94_387_689, 0.98_764_509, 0.82_433_901,
									    0.27_646_741, 0.64_907_493, 0.76_009_406, 0.30_087_915, 0.17_904_689,
									    0.41_601_714, 0.67_046_398, 0.10_422_822, 0.08_447_374, 0.07_354_344,
									    0.61_423_565, 0.70_284_866, 0.7_532_333 , 0.1_972_038 , 0.29_575_659,
									    0.90_583_886, 0.29_265_307, 0.50_000_175, 0.70_407_655, 0.889_363  ,
									    0.81_904_418, 0.66_829_128, 0.64_468_815, 0.56_563_723, 0.85_601_875,
									    0.94_924_672, 0.00_166_762, 0.25_220_643, 0.74_540_219, 0.67_993_247,
									    0.1_549_675 , 0.39_385_352, 0.92_153_607, 0.63_745_931, 0.27_759_043,
									    0.84_702_295, 0.65_904_271, 0.58_676_614, 0.8_666_936 , 0.39_607_438,
									    0.79_954_983, 0.42_220_697, 0.39_650_381, 0.7_849_864 , 0.56_150_201,
									    0.15_678_925, 0.14_746_032, 0.34_542_114, 0.47_026_783, 0.11_956_489,
									    0.25_421_435, 0.33_788_901, 0.68_934_842, 0.36_424_685, 0.71_737_898,
									    0.38_983_449, 0.94_393_779, 0.39_575_588, 0.36_616_553, 0.87_104_665,
									    0.64_630_203, 0.22_516_905, 0.88_270_804, 0.15_031_338, 0.75_144_345,
									    0.46_459_025, 0.85_396_454, 0.86_355_643, 0.65_139_851, 0.70_266_061,
									    0.30_241_389, 0.81_056_497, 0.88_865_969, 0.38_773_807, 0.70_635_849,
									    0.90_718_459, 0.43_245_789, 0.28_000_654, 0.45_935_562, 0.08_773_519,
									    0.9_552_151 , 0.93_901_511, 0.22_489_288], # 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
									]

									_lowercase							:		List[Any]				=   model.generate(
									    input_ids=UpperCAmelCase_      ,		spout=spouts[0]      ,		max_new_tokens=20      ,		generation_config=UpperCAmelCase_      ,		)

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

									_lowercase							:		Tuple				=   model.generate(
									    input_ids=UpperCAmelCase_      ,		spout=UpperCAmelCase_      ,		max_new_tokens=20      ,		generation_config=UpperCAmelCase_      ,		)

									_lowercase							:		Tuple				=   tokenizer.decode(outputa[0]		)
									_lowercase							:		Any				=   tokenizer.decode(outputa[0]		)
									_lowercase							:		Optional[Any]				=   tokenizer.batch_decode(UpperCAmelCase_		)

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





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







									'''simple docstring'''





									_lowercase							:		List[Any]				=   GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									_lowercase							:		List[str]				=   GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									model.to(UpperCAmelCase_		)

									# set deterministically
									_lowercase							:		str				=   GenerationConfig.from_pretrained('''Tanrei/GPTSAN-japanese'''		)
									_lowercase							:		List[Any]				=   1

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

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

									_lowercase							:		int				=   model.generate(
									    input_ids=input_tok_a.input_ids.to(UpperCAmelCase_		)      ,		token_type_ids=input_tok_a.token_type_ids.to(UpperCAmelCase_		)      ,		max_new_tokens=20      ,		generation_config=UpperCAmelCase_      ,		)

									_lowercase							:		str				=   model.generate(
									    input_ids=input_tok_a.input_ids.to(UpperCAmelCase_		)      ,		token_type_ids=input_tok_a.token_type_ids.to(UpperCAmelCase_		)      ,		attention_mask=input_tok_a.attention_mask.to(UpperCAmelCase_		)      ,		max_new_tokens=20      ,		generation_config=UpperCAmelCase_      ,		)

									_lowercase							:		List[str]				=   tokenizer.decode(outputa[0]		)
									_lowercase							:		Optional[int]				=   tokenizer.decode(outputa[0]		)
									_lowercase							:		int				=   tokenizer.batch_decode(UpperCAmelCase_		)

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






"""simple docstring"""

import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import cached_download, hf_hub_url
from PIL import Image

from transformers import DeformableDetrConfig, DeformableDetrForObjectDetection, DeformableDetrImageProcessor
from transformers.utils import logging


logging.set_verbosity_info()
a__			:						Optional[int]							=			logging.get_logger(__name__)





def 					__UpperCamelCase (     a__:			str      )					->      List[str]:




					"""simple docstring"""



					if "backbone.0.body" in orig_key:
										__lowercase        =      orig_key.replace('''backbone.0.body'''			,						'''backbone.conv_encoder.model'''      )
					if "transformer" in orig_key:
										__lowercase        =      orig_key.replace('''transformer.'''			,						''''''      )
					if "norm1" in orig_key:
										if "encoder" in orig_key:
															__lowercase        =      orig_key.replace('''norm1'''			,						'''self_attn_layer_norm'''      )
										else:
															__lowercase        =      orig_key.replace('''norm1'''			,						'''encoder_attn_layer_norm'''      )
					if "norm2" in orig_key:
										if "encoder" in orig_key:
															__lowercase        =      orig_key.replace('''norm2'''			,						'''final_layer_norm'''      )
										else:
															__lowercase        =      orig_key.replace('''norm2'''			,						'''self_attn_layer_norm'''      )
					if "norm3" in orig_key:
										__lowercase        =      orig_key.replace('''norm3'''			,						'''final_layer_norm'''      )
					if "linear1" in orig_key:
										__lowercase        =      orig_key.replace('''linear1'''			,						'''fc1'''      )
					if "linear2" in orig_key:
										__lowercase        =      orig_key.replace('''linear2'''			,						'''fc2'''      )
					if "query_embed" in orig_key:
										__lowercase        =      orig_key.replace('''query_embed'''			,						'''query_position_embeddings'''      )
					if "cross_attn" in orig_key:
										__lowercase        =      orig_key.replace('''cross_attn'''			,						'''encoder_attn'''      )

					return orig_key





def 					__UpperCamelCase (     a__:			Union[str, Any]      )					->      Tuple:




					"""simple docstring"""



					for i in range(6      ):
										# read in weights + bias of input projection layer of self-attention
										__lowercase        =      state_dict.pop(f'''decoder.layers.{i}.self_attn.in_proj_weight'''      )
										__lowercase        =      state_dict.pop(f'''decoder.layers.{i}.self_attn.in_proj_bias'''      )
										# next, add query, keys and values (in that order) to the state dict
										__lowercase        =      in_proj_weight[:2_56, :]
										__lowercase        =      in_proj_bias[:2_56]
										__lowercase        =      in_proj_weight[2_56:5_12, :]
										__lowercase        =      in_proj_bias[2_56:5_12]
										__lowercase        =      in_proj_weight[-2_56:, :]
										__lowercase        =      in_proj_bias[-2_56:]





def 					__UpperCamelCase (     )					->      str:




					"""simple docstring"""



					__lowercase        =      '''http://images.cocodataset.org/val2017/000000039769.jpg'''
					__lowercase        =      Image.open(requests.get(a__			,						stream=a__      ).raw      )

					return im





@torch.no_grad()
def 					__UpperCamelCase (     a__:			List[Any]			,						a__:			Union[str, Any]			,						a__:			Optional[int]			,						a__:			Optional[int]			,						a__:			List[Any]			,						a__:			Optional[Any]			,						a__:			str			,						)					->      List[str]:




					"""simple docstring"""



					__lowercase        =      DeformableDetrConfig()
					# set config attributes
					if single_scale:
										__lowercase        =      1
					__lowercase        =      dilation
					__lowercase        =      with_box_refine
					__lowercase        =      two_stage
					# set labels
					__lowercase        =      91
					__lowercase        =      '''huggingface/label-files'''
					__lowercase        =      '''coco-detection-id2label.json'''
					__lowercase        =      json.load(open(cached_download(hf_hub_url(a__			,						a__			,						repo_type='''dataset'''      )      )			,						'''r'''      )      )
					__lowercase        =      {int(a__      ): v for k, v in idalabel.items()}
					__lowercase        =      idalabel
					__lowercase        =      {v: k for k, v in idalabel.items()}

					# load image processor
					__lowercase        =      DeformableDetrImageProcessor(format='''coco_detection'''      )

					# prepare image
					__lowercase        =      prepare_img()
					__lowercase        =      image_processor(images=a__			,						return_tensors='''pt'''      )
					__lowercase        =      encoding['''pixel_values''']

					logger.info('''Converting model...'''      )

					# load original state dict
					__lowercase        =      torch.load(a__			,						map_location='''cpu'''      )['''model''']
					# rename keys
					for key in state_dict.copy().keys():
										__lowercase        =      state_dict.pop(a__      )
										__lowercase        =      val
					# query, key and value matrices need special treatment
					read_in_q_k_v(a__      )
					# important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them
					__lowercase        =      '''model.'''
					for key in state_dict.copy().keys():
										if not key.startswith('''class_embed'''      ) and not key.startswith('''bbox_embed'''      ):
															__lowercase        =      state_dict.pop(a__      )
															__lowercase        =      val
    # finally, create HuggingFace model and load state dict
					__lowercase        =      DeformableDetrForObjectDetection(a__      )
					model.load_state_dict(a__      )
					model.eval()

					__lowercase        =      '''cuda''' if torch.cuda.is_available() else '''cpu'''
					model.to(a__      )
					# verify our conversion
					__lowercase        =      model(pixel_values.to(a__      )      )

					__lowercase        =      torch.tensor(
					    [[-9.66_45, -4.34_49, -5.87_05], [-9.70_35, -3.85_04, -5.07_24], [-10.56_34, -5.33_79, -7.51_16]]      )
					__lowercase        =      torch.tensor([[0.86_93, 0.22_89, 0.24_92], [0.31_50, 0.54_89, 0.58_45], [0.55_63, 0.75_80, 0.85_18]]      )

					if single_scale:
										__lowercase        =      torch.tensor(
										    [[-9.90_51, -4.25_41, -6.48_52], [-9.69_47, -4.08_54, -6.80_33], [-10.06_65, -5.84_70, -7.70_03]]      )
										__lowercase        =      torch.tensor([[0.72_92, 0.49_91, 0.55_32], [0.79_59, 0.24_26, 0.42_36], [0.75_82, 0.35_18, 0.44_51]]      )

					if single_scale and dilation:
										__lowercase        =      torch.tensor(
										    [[-8.96_52, -4.10_74, -5.66_35], [-9.05_96, -4.94_47, -6.60_75], [-10.11_78, -4.52_75, -6.26_71]]      )
										__lowercase        =      torch.tensor([[0.76_65, 0.41_30, 0.47_69], [0.83_64, 0.18_41, 0.33_91], [0.62_61, 0.38_95, 0.79_78]]      )

					if with_box_refine:
										__lowercase        =      torch.tensor(
										    [[-8.88_95, -5.41_87, -6.81_53], [-8.47_06, -6.16_68, -7.61_84], [-9.00_42, -5.53_59, -6.91_41]]      )
										__lowercase        =      torch.tensor([[0.78_28, 0.22_08, 0.43_23], [0.08_92, 0.59_96, 0.13_19], [0.55_24, 0.63_89, 0.89_14]]      )

					if with_box_refine and two_stage:
										__lowercase        =      torch.tensor(
										    [[-6.71_08, -4.32_13, -6.37_77], [-8.90_14, -6.17_99, -6.72_40], [-6.93_15, -4.47_35, -6.22_98]]      )
										__lowercase        =      torch.tensor([[0.25_83, 0.54_99, 0.46_83], [0.76_52, 0.90_68, 0.48_82], [0.54_90, 0.27_63, 0.05_64]]      )

					print('''Logits:'''			,						outputs.logits[0, :3, :3]      )

					assert torch.allclose(outputs.logits[0, :3, :3]			,						expected_logits.to(a__      )			,						atol=1e-4      )
					assert torch.allclose(outputs.pred_boxes[0, :3, :3]			,						expected_boxes.to(a__      )			,						atol=1e-4      )

					print('''Everything ok!'''      )

					# Save model and image processor
					logger.info(f'''Saving PyTorch model and image processor to {pytorch_dump_folder_path}...'''      )
					Path(a__      ).mkdir(exist_ok=a__      )
					model.save_pretrained(a__      )
					image_processor.save_pretrained(a__      )

					# Push to hub
					if push_to_hub:
										__lowercase        =      '''deformable-detr'''
										model_name += "-single-scale" if single_scale else ""
										model_name += "-dc5" if dilation else ""
										model_name += "-with-box-refine" if with_box_refine else ""
										model_name += "-two-stage" if two_stage else ""
										print('''Pushing model to hub...'''      )
										model.push_to_hub(repo_path_or_name=a__			,						organization='''nielsr'''			,						commit_message='''Add model'''      )


if __name__ == "__main__":
							a__			:						str							=			argparse.ArgumentParser()

							parser.add_argument(
							    """--checkpoint_path""",
							    type=str,
							    default="""/home/niels/checkpoints/deformable_detr/r50_deformable_detr-checkpoint.pth""",
							    help="""Path to Pytorch checkpoint (.pth file) you'd like to convert.""",
							)
							parser.add_argument("""--single_scale""", action="""store_true""", help="""Whether to set config.num_features_levels = 1.""")
							parser.add_argument("""--dilation""", action="""store_true""", help="""Whether to set config.dilation=True.""")
							parser.add_argument("""--with_box_refine""", action="""store_true""", help="""Whether to set config.with_box_refine=True.""")
							parser.add_argument("""--two_stage""", action="""store_true""", help="""Whether to set config.two_stage=True.""")
							parser.add_argument(
							    """--pytorch_dump_folder_path""",
							    default=None,
							    type=str,
							    required=True,
							    help="""Path to the folder to output PyTorch model.""",
							)
							parser.add_argument(
							    """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub."""
							)
							a__			:						Union[str, Any]							=			parser.parse_args()
							convert_deformable_detr_checkpoint(
							    args.checkpoint_path,
							    args.single_scale,
							    args.dilation,
							    args.with_box_refine,
							    args.two_stage,
							    args.pytorch_dump_folder_path,
							    args.push_to_hub,
							)


