




'''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				:							List[Any]					,  SCREAMING_SNAKE_CASE_				:							List[str]					,  SCREAMING_SNAKE_CASE_				:							Optional[int]=3_60_00					,  SCREAMING_SNAKE_CASE_				:							Union[str, Any]=13					,  SCREAMING_SNAKE_CASE_				:							List[str]=7					,  SCREAMING_SNAKE_CASE_				:							Optional[Any]=True					,  SCREAMING_SNAKE_CASE_				:							List[str]=32					,  SCREAMING_SNAKE_CASE_				:							Union[str, Any]=42					,  SCREAMING_SNAKE_CASE_				:							Optional[int]=5					,  SCREAMING_SNAKE_CASE_				:							Optional[Any]=2					,  SCREAMING_SNAKE_CASE_				:							Any=4					,  SCREAMING_SNAKE_CASE_				:							List[str]=2					,  SCREAMING_SNAKE_CASE_				:							Optional[Any]=32					,  SCREAMING_SNAKE_CASE_				:							List[str]=80					,  SCREAMING_SNAKE_CASE_				:							Dict=33					,  SCREAMING_SNAKE_CASE_				:							Tuple=0.0					,  SCREAMING_SNAKE_CASE_				:							Tuple=1E-6					,  SCREAMING_SNAKE_CASE_				:							Union[str, Any]=1_00					,  SCREAMING_SNAKE_CASE_				:							List[str]=0.0					,  )			->		int:







       '''simple docstring'''



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



  def 					_snake_case     ( self				:							Tuple	)			->		List[Any]:







       '''simple docstring'''



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



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







       '''simple docstring'''



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

       A:		Tuple          =		self.get_config()

       return (config, input_ids)



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







       '''simple docstring'''



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

       A:		Optional[int]          =		self.get_config()

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



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







       '''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				:							Dict					,  SCREAMING_SNAKE_CASE_				:							List[Any]					,  SCREAMING_SNAKE_CASE_				:							Optional[Any]					,  )			->		List[Any]:







       '''simple docstring'''



       A:		Dict          =		GPTSanJapaneseForConditionalGeneration(config=SCREAMING_SNAKE_CASE_	)
       model.to(SCREAMING_SNAKE_CASE_	)
       model.eval()
       A:		Union[str, Any]          =		model(
           input_ids=SCREAMING_SNAKE_CASE_					,  )
       self.parent.assertIsNotNone(SCREAMING_SNAKE_CASE_	)






@require_torch
class 						lowerCAmelCase_	(       UpperCAmelCase_       ,					UpperCAmelCase_       ,					unittest.TestCase							):







  '''simple docstring'''

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



  def 					_snake_case     ( self				:							Union[str, Any]					,  SCREAMING_SNAKE_CASE_				:							Optional[Any]					,  SCREAMING_SNAKE_CASE_				:							Any					,  SCREAMING_SNAKE_CASE_				:							str					,  SCREAMING_SNAKE_CASE_				:							List[str]					,  SCREAMING_SNAKE_CASE_				:							Union[str, Any]	)			->		Union[str, Any]:







       '''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]	)			->		Tuple:







       '''simple docstring'''



       A:		Dict          =		GPTSanJapaneseTester(self	)
       A:		Optional[Any]          =		ConfigTester(self					,  config_class=SCREAMING_SNAKE_CASE_					,  d_model=37	)



  def 					_snake_case     ( self				:							Dict	)			->		List[Any]:







       '''simple docstring'''



       GPTSanJapaneseConfig()



  def 					_snake_case     ( self				:							Tuple	)			->		Optional[Any]:







       '''simple docstring'''



       A:		List[Any]          =		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 					_snake_case     ( self				:							int	)			->		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 					_snake_case     ( self				:							int	)			->		Optional[int]:







       '''simple docstring'''



       pass






@require_torch
class 						lowerCAmelCase_	(       UpperCAmelCase_       ,					UpperCAmelCase_       ,					unittest.TestCase							):







  '''simple docstring'''

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



  def 					_snake_case     ( self				:							int	)			->		str:







       '''simple docstring'''



       A:		Tuple          =		GPTSanJapaneseTester(self	)
       A:		Optional[int]          =		ConfigTester(self					,  config_class=SCREAMING_SNAKE_CASE_					,  d_model=37	)



  def 					_snake_case     ( self				:							Dict	)			->		str:







       '''simple docstring'''



       GPTSanJapaneseConfig()



  def 					_snake_case     ( self				:							str	)			->		Dict:







       '''simple docstring'''



       A:		List[str]          =		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 					_snake_case     ( self				:							Any	)			->		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				:							str	)			->		List[str]:







       '''simple docstring'''



       pass



  @slow
  def 					_snake_case     ( self				:							int	)			->		str:







       '''simple docstring'''



       A:		Any          =		GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese'''	)
       A:		Dict          =		GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese'''	)
       A:		Optional[int]          =		tokenizer.encode('''武田信玄は'''					,  return_tensors='''pt'''	)
       A:		Union[str, Any]          =		model(SCREAMING_SNAKE_CASE_	)
       A:		List[str]          =		outputs.logits.detach().cpu().numpy()
       # Output of original model created with mesh-tensoflow
       A:		Any          =		[
           # fmt: off
           [-12.037_8398_8952_6367, -12.433_0615_9973_1445, -14.333_8403_7017_8223, -12.450_3459_9304_1992, -11.1_6613_7695_3125,
           -11.930_1376_3427_7344, -10.659_7404_4799_8047, -12.909_5745_0866_6992, -13.241_0430_9082_0312, -13.398_5795_9747_3145,
           -11.107_5248_7182_6172, -12.3_6859_4169_6167, -22.97_9431_1523_4375, -10.481_0676_5747_0703, -12.484_0307_2357_1777,
           -12.807_3606_4910_8887, -14.769_7000_5035_4004, -12.233_5796_3562_0117, -13.428_1454_0863_0371, -22.624_1779_3273_9258],
           [-7.511_1498_8327_0264, -8.281_8517_6849_3652, -7.943_1271_5530_3955, -7.55_0213_3369_4458, -6.49_8699_6650_6958,
           -7.586_7962_8372_1924, -6.978_0859_9472_0459, -7.839_1451_8356_3232, -8.21_9641_6854_8584, -8.695_0912_4755_8594,
           -6.706_9106_1019_8975, -6.6585_7982_6354_9805, -19.565_6986_2365_7227, -5.353_8427_3529_0527, -8.350_6860_7330_3223,
           -8.039_3886_5661_6211, -10.856_5692_9016_1133, -7.75_1544_4755_5542, -8.819_0221_7864_9902, -19.51_5327_4536_1328],
           [-9.73_0662_3458_8623, -10.223_9227_2949_2188, -9.932_9814_9108_8867, -11.857_8367_2332_7637, -7.662_6262_6647_9492,
           -11.13_5291_0995_4834, -7.765_0976_1810_3027, -11.472_9232_7880_8594, -9.543_1499_4812_0117, -11.905_6339_2639_1602,
           -9.366_1642_0745_8496, -11.5_7342_8153_9917, -23.699_0032_1960_4492, -9.429_5902_2521_9727, -10.42_8392_4102_7832,
           -10.585_2403_6407_4707, -10.94_7719_5739_7461, -11.095_4160_6903_0762, -10.390_2406_6925_0488, -23.769_3729_4006_3477],
           [-9.728_2657_6232_9102, -9.859_7126_0070_8008, -10.09_7292_9000_8545, -9.678_5221_0998_5352, -6.879_5199_3942_2607,
           -9.68_4875_4882_8125, -4.2803_4257_8887_9395, -10.018_9142_2271_7285, -9.308_4459_3048_0957, -10.63_3945_4650_8789,
           -8.083_6467_7429_1992, -9.06_3014_9841_3086, -21.904_2663_5742_1875, -8.90_1608_4671_0205, -8.841_8760_2996_8262,
           -11.856_7199_7070_3125, -12.079_3981_5521_2402, -11.233_7532_0434_5703, -10.177_3386_0015_8691, -21.87_2566_2231_4453],
           [-9.669_7645_1873_7793, -9.614_1986_8469_2383, -9.814_5103_4545_8984, -9.996_5019_2260_7422, -11.375_6904_6020_5078,
           -10.113_4052_2766_1133, -10.546_8673_7060_5469, -10.04_3690_6814_5752, -10.907_8092_5750_7324, -10.504_2161_9415_2832,
           -11.129_1990_2801_5137, -10.151_1240_0054_9316, -21.96_5867_9962_1582, -9.086_3494_8730_4688, -11.730_3390_5029_2969,
           -10.460_6676_1016_8457, -10.298_0499_2675_7812, -10.784_1482_1624_7559, -10.840_6934_7381_5918, -22.03_1528_4729_0039],
           # fmt: on
       ]
       A:		Optional[int]          =		np.array(SCREAMING_SNAKE_CASE_	).flatten()
       A:		List[Any]          =		output_logits[0, :, :20].flatten()

       def check(SCREAMING_SNAKE_CASE_				:							Optional[int]					,  SCREAMING_SNAKE_CASE_				:							Any					,  SCREAMING_SNAKE_CASE_				:							Union[str, Any]=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 					_snake_case     ( self				:							Optional[Any]	)			->		Optional[Any]:







       '''simple docstring'''



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

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

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

       A:		List[Any]          =		'''left'''
       A:		Dict          =		tokenizer(SCREAMING_SNAKE_CASE_					,  return_tensors='''pt'''					,  padding=SCREAMING_SNAKE_CASE_	)
       A:		Any          =		inputs['''input_ids'''].to(SCREAMING_SNAKE_CASE_	)

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

       A:		Union[str, Any]          =		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_					,  )

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

       A:		Dict          =		tokenizer(sentences[1]					,  return_tensors='''pt'''	).input_ids.to(SCREAMING_SNAKE_CASE_	)
       A:		int          =		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	)

       A:		List[str]          =		tokenizer.batch_decode(SCREAMING_SNAKE_CASE_					,  skip_special_tokens=SCREAMING_SNAKE_CASE_	)
       A:		List[Any]          =		tokenizer.decode(output_non_padded[0]					,  skip_special_tokens=SCREAMING_SNAKE_CASE_	)
       A:		Optional[int]          =		tokenizer.decode(output_padded[0]					,  skip_special_tokens=SCREAMING_SNAKE_CASE_	)

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



  @tooslow
  def 					_snake_case     ( self				:							List[Any]	)			->		List[Any]:







       '''simple docstring'''



       A:		Any          =		GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese'''	)
       A:		Union[str, Any]          =		GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese'''	)
       # Output of original model created with mesh-tensoflow
       A:		Union[str, Any]          =		[
           ('''武田信玄は''', 3_56_75),
           ('''武田信玄は、''', 45),
           ('''武田信玄は、この''', 29),
           ('''武田信玄は、このよう''', 3_06_42),
           ('''武田信玄は、このような''', 3_56_80),
           ('''武田信玄は、このような「''', 86_40),
           ('''武田信玄は、このような「武田''', 3_16_17),
           ('''武田信玄は、このような「武田家''', 3_06_46),
           ('''武田信玄は、このような「武田家の''', 3_16_17),
           ('''武田信玄は、このような「武田家の家''', 3_13_81),
       ]
       for input, output in target:
            A:		str          =		tokenizer.encode(SCREAMING_SNAKE_CASE_					,  return_tensors='''pt'''	)
            A:		Any          =		model(SCREAMING_SNAKE_CASE_	)
            A:		List[Any]          =		outputs.logits.detach().cpu().numpy()[0]
            A:		List[str]          =		np.argmax(output_logits[-1]	)
            self.assertEqual(SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_	)



  @slow
  def 					_snake_case     ( self				:							Tuple	)			->		int:







       '''simple docstring'''



       A:		Tuple          =		GPTSanJapaneseForConditionalGeneration.from_pretrained('''Tanrei/GPTSAN-japanese'''	)
       A:		Optional[int]          =		GPTSanJapaneseTokenizer.from_pretrained('''Tanrei/GPTSAN-japanese'''	)
       model.to(SCREAMING_SNAKE_CASE_	)

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

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

       # spout from uniform and one-hot
       A:		Tuple          =		[
           # fmt: off
           [0.8788_2208, 0.3842_6396, 0.3322_0248, 0.4389_0406, 0.1656_2252,
           0.0480_3985, 0.21_1572  , 0.2318_8473, 0.3715_3068, 0.783_6377 ,
           0.0216_0172, 0.3876_1719, 0.7529_0772, 0.9019_8857, 0.3436_5777,
           0.6416_8169, 0.4431_8471, 0.1457_5746, 0.9256_2881, 0.4081_2148,
           0.2901_9122, 0.8886_1599, 0.6552_4846, 0.4356_3456, 0.3817_7187,
           0.7083_2965, 0.8152_7892, 0.6883_2812, 0.3883_3192, 0.456_1522 ,
           0.1482_8817, 0.4724_8213, 0.5435_7335, 0.8200_9566, 0.133_8884 ,
           0.0275_5417, 0.1976_4677, 0.242_2084 , 0.0475_7674, 0.6540_9606,
           0.082_4589 , 0.0330_4383, 0.9438_7689, 0.9876_4509, 0.8243_3901,
           0.2764_6741, 0.6490_7493, 0.7600_9406, 0.3008_7915, 0.1790_4689,
           0.4160_1714, 0.6704_6398, 0.1042_2822, 0.0844_7374, 0.0735_4344,
           0.6142_3565, 0.7028_4866, 0.753_2333 , 0.197_2038 , 0.2957_5659,
           0.9058_3886, 0.2926_5307, 0.5000_0175, 0.7040_7655, 0.88_9363  ,
           0.8190_4418, 0.6682_9128, 0.6446_8815, 0.5656_3723, 0.8560_1875,
           0.9492_4672, 0.0016_6762, 0.2522_0643, 0.7454_0219, 0.6799_3247,
           0.154_9675 , 0.3938_5352, 0.9215_3607, 0.6374_5931, 0.2775_9043,
           0.8470_2295, 0.6590_4271, 0.5867_6614, 0.866_6936 , 0.3960_7438,
           0.7995_4983, 0.4222_0697, 0.3965_0381, 0.784_9864 , 0.5615_0201,
           0.1567_8925, 0.1474_6032, 0.3454_2114, 0.4702_6783, 0.1195_6489,
           0.2542_1435, 0.3378_8901, 0.6893_4842, 0.3642_4685, 0.7173_7898,
           0.3898_3449, 0.9439_3779, 0.3957_5588, 0.3661_6553, 0.8710_4665,
           0.6463_0203, 0.2251_6905, 0.8827_0804, 0.1503_1338, 0.7514_4345,
           0.4645_9025, 0.8539_6454, 0.8635_5643, 0.6513_9851, 0.7026_6061,
           0.3024_1389, 0.8105_6497, 0.8886_5969, 0.3877_3807, 0.7063_5849,
           0.9071_8459, 0.4324_5789, 0.2800_0654, 0.4593_5562, 0.0877_3519,
           0.955_2151 , 0.9390_1511, 0.2248_9288], # 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
       ]

       A:		Union[str, Any]          =		model.generate(
           input_ids=SCREAMING_SNAKE_CASE_					,  spout=spouts[0]					,  max_new_tokens=20					,  generation_config=SCREAMING_SNAKE_CASE_					,  )

       A:		Optional[Any]          =		model.generate(
           input_ids=SCREAMING_SNAKE_CASE_					,  spout=spouts[1]					,  max_new_tokens=20					,  generation_config=SCREAMING_SNAKE_CASE_					,  )

       A:		Union[str, Any]          =		model.generate(
           input_ids=SCREAMING_SNAKE_CASE_					,  spout=SCREAMING_SNAKE_CASE_					,  max_new_tokens=20					,  generation_config=SCREAMING_SNAKE_CASE_					,  )

       A:		Optional[int]          =		tokenizer.decode(outputa[0]	)
       A:		Union[str, Any]          =		tokenizer.decode(outputa[0]	)
       A:		Any          =		tokenizer.batch_decode(SCREAMING_SNAKE_CASE_	)

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




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







       '''simple docstring'''



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

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

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

       A:		Tuple          =		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_					,  )

       A:		Optional[Any]          =		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_					,  )

       A:		Any          =		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_					,  )

       A:		Dict          =		tokenizer.decode(outputa[0]	)
       A:		Optional[Any]          =		tokenizer.decode(outputa[0]	)
       A:		Dict          =		tokenizer.batch_decode(SCREAMING_SNAKE_CASE_	)

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












'''simple docstring'''


import gc
import unittest

import numpy as np
import torch

from diffusers import RePaintPipeline, RePaintScheduler, UNetaDModel
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    load_image,
    load_numpy,
    nightly,
    require_torch_gpu,
    skip_mps,
    torch_device,
)

from ..pipeline_params import IMAGE_INPAINTING_BATCH_PARAMS, IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()






class 						lowerCAmelCase_	(       UpperCAmelCase_       ,					unittest.TestCase							):







  '''simple docstring'''

  UpperCamelCase_			:      Any										=				RePaintPipeline
  UpperCamelCase_			:      int										=				IMAGE_INPAINTING_PARAMS - {"""width""", """height""", """guidance_scale"""}
  UpperCamelCase_			:      List[str]										=				PipelineTesterMixin.required_optional_params - {
      """latents""",
      """num_images_per_prompt""",
      """callback""",
      """callback_steps""",
  }
  UpperCamelCase_			:      List[str]										=				IMAGE_INPAINTING_BATCH_PARAMS
  UpperCamelCase_			:      str										=				False



  def 					_snake_case     ( self				:							Dict	)			->		Optional[Any]:







       '''simple docstring'''



       torch.manual_seed(0	)
       torch.manual_seed(0	)
       A:		Optional[Any]          =		UNetaDModel(
           block_out_channels=(32, 64)					,  layers_per_block=2					,  sample_size=32					,  in_channels=3					,  out_channels=3					,  down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')					,  up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')					,  )
       A:		Optional[int]          =		RePaintScheduler()
       A:		Optional[int]          =		{'''unet''': unet, '''scheduler''': scheduler}
       return components



  def 					_snake_case     ( self				:							Optional[Any]					,  SCREAMING_SNAKE_CASE_				:							List[Any]					,  SCREAMING_SNAKE_CASE_				:							Tuple=0	)			->		Optional[int]:







       '''simple docstring'''



       if str(SCREAMING_SNAKE_CASE_	).startswith('''mps'''	):
            A:		str          =		torch.manual_seed(SCREAMING_SNAKE_CASE_	)
       else:
            A:		Any          =		torch.Generator(device=SCREAMING_SNAKE_CASE_	).manual_seed(SCREAMING_SNAKE_CASE_	)
       A:		str          =		np.random.RandomState(SCREAMING_SNAKE_CASE_	).standard_normal((1, 3, 32, 32)	)
       A:		Tuple          =		torch.from_numpy(SCREAMING_SNAKE_CASE_	).to(device=SCREAMING_SNAKE_CASE_					,  dtype=torch.floataa	)
       A:		List[Any]          =		(image > 0).to(device=SCREAMING_SNAKE_CASE_					,  dtype=torch.floataa	)
       A:		Tuple          =		{
           '''image''': image,
           '''mask_image''': mask,
           '''generator''': generator,
           '''num_inference_steps''': 5,
           '''eta''': 0.0,
           '''jump_length''': 2,
           '''jump_n_sample''': 2,
           '''output_type''': '''numpy''',
       }
       return inputs



  def 					_snake_case     ( self				:							int	)			->		Tuple:







       '''simple docstring'''



       A:		int          =		'''cpu'''  # ensure determinism for the device-dependent torch.Generator
       A:		List[str]          =		self.get_dummy_components()
       A:		int          =		RePaintPipeline(**SCREAMING_SNAKE_CASE_	)
       A:		List[Any]          =		sd_pipe.to(SCREAMING_SNAKE_CASE_	)
       sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

       A:		Dict          =		self.get_dummy_inputs(SCREAMING_SNAKE_CASE_	)
       A:		List[str]          =		sd_pipe(**SCREAMING_SNAKE_CASE_	).images
       A:		Optional[Any]          =		image[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       A:		List[Any]          =		np.array([1.0000, 0.5426, 0.5497, 0.2200, 1.0000, 1.0000, 0.5623, 1.0000, 0.6274]	)

       assert np.abs(image_slice.flatten() - expected_slice	).max() < 1E-3



  @skip_mps
  def 					_snake_case     ( self				:							Union[str, Any]	)			->		Optional[Any]:







       '''simple docstring'''



       return super().test_save_load_local()



  @unittest.skip('''non-deterministic pipeline'''	)
  def 					_snake_case     ( self				:							Tuple	)			->		Optional[Any]:







       '''simple docstring'''



       return super().test_inference_batch_single_identical()



  @skip_mps
  def 					_snake_case     ( self				:							List[Any]	)			->		str:







       '''simple docstring'''



       return super().test_dict_tuple_outputs_equivalent()



  @skip_mps
  def 					_snake_case     ( self				:							List[Any]	)			->		str:







       '''simple docstring'''



       return super().test_save_load_optional_components()




  @skip_mps
  def 					_snake_case     ( self				:							Any	)			->		Optional[Any]:







       '''simple docstring'''



       return super().test_attention_slicing_forward_pass()






@nightly
@require_torch_gpu
class 						lowerCAmelCase_	(       unittest.TestCase							):







  '''simple docstring'''

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







       '''simple docstring'''



       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()




  def 					_snake_case     ( self				:							Optional[int]	)			->		List[Any]:







       '''simple docstring'''



       A:		str          =		load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/'''
           '''repaint/celeba_hq_256.png'''	)
       A:		Union[str, Any]          =		load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/repaint/mask_256.png'''	)
       A:		Optional[Any]          =		load_numpy(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/'''
           '''repaint/celeba_hq_256_result.npy'''	)

       A:		Dict          =		'''google/ddpm-ema-celebahq-256'''
       A:		Tuple          =		UNetaDModel.from_pretrained(SCREAMING_SNAKE_CASE_	)
       A:		List[Any]          =		RePaintScheduler.from_pretrained(SCREAMING_SNAKE_CASE_	)

       A:		Optional[Any]          =		RePaintPipeline(unet=SCREAMING_SNAKE_CASE_					,  scheduler=SCREAMING_SNAKE_CASE_	).to(SCREAMING_SNAKE_CASE_	)
       repaint.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)
       repaint.enable_attention_slicing()

       A:		str          =		torch.manual_seed(0	)
       A:		str          =		repaint(
           SCREAMING_SNAKE_CASE_					,  SCREAMING_SNAKE_CASE_					,  num_inference_steps=2_50					,  eta=0.0					,  jump_length=10					,  jump_n_sample=10					,  generator=SCREAMING_SNAKE_CASE_					,  output_type='''np'''					,  )
       A:		Optional[Any]          =		output.images[0]

       assert image.shape == (2_56, 2_56, 3)
       assert np.abs(expected_image - image	).mean() < 1E-2






