code: 
  '''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  : str	, lowerCAmelCase_  : List[str]	, lowerCAmelCase_  : Union[str, Any]=3_60_00	, lowerCAmelCase_  : Union[str, Any]=13	, lowerCAmelCase_  : int=7	, lowerCAmelCase_  : int=True	, lowerCAmelCase_  : str=32	, lowerCAmelCase_  : List[Any]=42	, lowerCAmelCase_  : Any=5	, lowerCAmelCase_  : List[Any]=2	, lowerCAmelCase_  : int=4	, lowerCAmelCase_  : Tuple=2	, lowerCAmelCase_  : Any=32	, lowerCAmelCase_  : Tuple=80	, lowerCAmelCase_  : Union[str, Any]=33	, lowerCAmelCase_  : str=0.0	, lowerCAmelCase_  : Optional[Any]=1E-6	, lowerCAmelCase_  : List[Any]=1_00	, lowerCAmelCase_  : Optional[Any]=0.0	, )   ->       Union[str, Any]:
          snake_case_						       =							vocab_size
          snake_case_						       =							parent
          snake_case_						       =							batch_size
          snake_case_						       =							num_contexts
          # For common tests
          snake_case_						       =							self.num_contexts
          snake_case_						       =							is_training
          snake_case_						       =							hidden_size
          snake_case_						       =							num_ext_layers
          snake_case_						       =							ext_size
          snake_case_						       =							num_hidden_layers
          snake_case_						       =							num_attention_heads
          snake_case_						       =							num_experts
          snake_case_						       =							d_ff
          snake_case_						       =							d_ext
          snake_case_						       =							d_spout
          snake_case_						       =							dropout_rate
          snake_case_						       =							layer_norm_epsilon
          snake_case_						       =							expert_capacity
          snake_case_						       =							router_jitter_noise
  
    def 		_lowerCamelCase	(self  : Union[str, Any]    )   ->       Dict:
          return GPTSanJapaneseConfig.from_pretrained("Tanrei/GPTSAN-japanese"    )
  
    def 		_lowerCamelCase	(self  : Optional[Any]    )   ->       List[str]:
          snake_case_						       =							ids_tensor([self.batch_size, self.seq_length]	, self.vocab_size    )
  
          snake_case_						       =							self.get_config()
  
          return (config, input_ids)
  
    def 		_lowerCamelCase	(self  : Optional[int]    )   ->       Union[str, Any]:
          snake_case_						       =							ids_tensor([self.batch_size, self.seq_length]	, self.vocab_size    )
  
          snake_case_						       =							self.get_config()
  
          return (config, {"input_ids": input_ids})
  
    def 		_lowerCamelCase	(self  : Dict    )   ->       Optional[int]:
          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 		_lowerCamelCase	(self  : Optional[int]	, lowerCAmelCase_  : int	, lowerCAmelCase_  : Dict	, )   ->       List[str]:
          snake_case_						       =							GPTSanJapaneseForConditionalGeneration(config=lowerCAmelCase_    )
          model.to(lowerCAmelCase_    )
          model.eval()
          snake_case_						       =							model(
              input_ids=lowerCAmelCase_	, )
          self.parent.assertIsNotNone(lowerCAmelCase_    )
  
  
  
  @require_torch
  class  lowerCAmelCase__   (       UpperCamelCase					,							UpperCamelCase					,							unittest.TestCase       ):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    _UpperCamelCase						= (GPTSanJapaneseModel,) if is_torch_available() else ()
    _UpperCamelCase						= (
        {
            'conversational': GPTSanJapaneseForConditionalGeneration,
            'feature-extraction': GPTSanJapaneseForConditionalGeneration,
            'summarization': GPTSanJapaneseForConditionalGeneration,
            'text2text-generation': GPTSanJapaneseForConditionalGeneration,
            'translation': GPTSanJapaneseForConditionalGeneration,
        }
        if is_torch_available()
        else {}
    )
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    # The small GPTSAN_JAPANESE model needs higher percentages for CPU/MP tests
    _UpperCamelCase						= [0.8, 0.9]
  
    def 		_lowerCamelCase	(self  : Optional[Any]	, lowerCAmelCase_  : Any	, lowerCAmelCase_  : Tuple	, lowerCAmelCase_  : List[str]	, lowerCAmelCase_  : Dict	, lowerCAmelCase_  : int    )   ->       List[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 		_lowerCamelCase	(self  : Tuple    )   ->       Dict:
          snake_case_						       =							GPTSanJapaneseTester(self    )
          snake_case_						       =							ConfigTester(self	, config_class=lowerCAmelCase_	, d_model=37    )
  
    def 		_lowerCamelCase	(self  : Optional[int]    )   ->       Tuple:
          GPTSanJapaneseConfig()
  
    def 		_lowerCamelCase	(self  : Union[str, Any]    )   ->       Any:
          snake_case_						       =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowerCAmelCase_    )
  
    @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 		_lowerCamelCase	(self  : List[str]    )   ->       List[Any]:
          super().test_model_parallelism()
  
  
    @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."    )
    def 		_lowerCamelCase	(self  : Any    )   ->       int:
          pass
  
  
  
  @require_torch
  class  lowerCAmelCase__   (       UpperCamelCase					,							UpperCamelCase					,							unittest.TestCase       ):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    _UpperCamelCase						= (GPTSanJapaneseForConditionalGeneration,) if is_torch_available() else ()
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    # The small GPTSAN_JAPANESE model needs higher percentages for CPU/MP tests
    _UpperCamelCase						= [0.8, 0.9]
  
    def 		_lowerCamelCase	(self  : str    )   ->       Union[str, Any]:
          snake_case_						       =							GPTSanJapaneseTester(self    )
          snake_case_						       =							ConfigTester(self	, config_class=lowerCAmelCase_	, d_model=37    )
  
    def 		_lowerCamelCase	(self  : Optional[int]    )   ->       Tuple:
          GPTSanJapaneseConfig()
  
    def 		_lowerCamelCase	(self  : Optional[Any]    )   ->       List[Any]:
          snake_case_						       =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowerCAmelCase_    )
  
    @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 		_lowerCamelCase	(self  : Tuple    )   ->       Dict:
          super().test_model_parallelism()
  
    @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."    )
    def 		_lowerCamelCase	(self  : Tuple    )   ->       Dict:
          pass
  
    @slow
    def 		_lowerCamelCase	(self  : Optional[Any]    )   ->       Tuple:
          snake_case_						       =							GPTSanJapaneseForConditionalGeneration.from_pretrained("Tanrei/GPTSAN-japanese"    )
          snake_case_						       =							GPTSanJapaneseTokenizer.from_pretrained("Tanrei/GPTSAN-japanese"    )
          snake_case_						       =							tokenizer.encode("武田信玄は"	, return_tensors="pt"    )
          snake_case_						       =							model(lowerCAmelCase_    )
          snake_case_						       =							outputs.logits.detach().cpu().numpy()
          # Output of original model created with mesh-tensoflow
          snake_case_						       =							[
              # 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
          ]
          snake_case_						       =							np.array(lowerCAmelCase_    ).flatten()
          snake_case_						       =							output_logits[0, :, :20].flatten()
  
          def check(lowerCAmelCase_  : str	, lowerCAmelCase_  : Any	, lowerCAmelCase_  : Optional[Any]=5E-4    ):
                return abs(a - b    ) < epsilon * max(abs(lowerCAmelCase_    )	, abs(lowerCAmelCase_    )    )
  
          self.assertTrue(np.all([check(target[i]	, predict[i]    ) for i in range(len(lowerCAmelCase_    )    )]    )    )
  
    @slow
    def 		_lowerCamelCase	(self  : str    )   ->       Dict:
          snake_case_						       =							GPTSanJapaneseForConditionalGeneration.from_pretrained("Tanrei/GPTSAN-japanese"    )
          snake_case_						       =							GPTSanJapaneseTokenizer.from_pretrained("Tanrei/GPTSAN-japanese"    )
          model.to(lowerCAmelCase_    )
  
          # set deterministically
          snake_case_						       =							GenerationConfig.from_pretrained("Tanrei/GPTSAN-japanese"    )
          snake_case_						       =							1
  
          # use different length sentences to test batching
          snake_case_						       =							[
              "甲斐なら武田と言うほど",
              "織田信長は、",
          ]
  
          snake_case_						       =							"left"
          snake_case_						       =							tokenizer(lowerCAmelCase_	, return_tensors="pt"	, padding=lowerCAmelCase_    )
          snake_case_						       =							inputs["input_ids"].to(lowerCAmelCase_    )
  
          self.assertNotEqual(inputs["attention_mask"][0].numpy().tolist()	, inputs["attention_mask"][1].numpy().tolist()    )
  
          snake_case_						       =							model.generate(
              input_ids=lowerCAmelCase_	, attention_mask=inputs["attention_mask"].to(lowerCAmelCase_    )	, max_new_tokens=3	, generation_config=lowerCAmelCase_	, )
  
          snake_case_						       =							tokenizer(sentences[0]	, return_tensors="pt"    ).input_ids.to(lowerCAmelCase_    )
          snake_case_						       =							model.generate(
              input_ids=lowerCAmelCase_	, max_new_tokens=3	, generation_config=lowerCAmelCase_    )
  
          snake_case_						       =							tokenizer(sentences[1]	, return_tensors="pt"    ).input_ids.to(lowerCAmelCase_    )
          snake_case_						       =							model.generate(input_ids=lowerCAmelCase_	, max_new_tokens=3	, generation_config=lowerCAmelCase_    )
  
          self.assertNotEqual(inputs_non_padded.shape	, inputs_padded.shape    )
  
          snake_case_						       =							tokenizer.batch_decode(lowerCAmelCase_	, skip_special_tokens=lowerCAmelCase_    )
          snake_case_						       =							tokenizer.decode(output_non_padded[0]	, skip_special_tokens=lowerCAmelCase_    )
          snake_case_						       =							tokenizer.decode(output_padded[0]	, skip_special_tokens=lowerCAmelCase_    )
  
          snake_case_						       =							[
              "甲斐なら武田と言うほど甲斐の武田",
              "織田信長は、このような",
          ]
          self.assertListEqual(lowerCAmelCase_	, lowerCAmelCase_    )
          self.assertListEqual(lowerCAmelCase_	, [non_padded_sentence, padded_sentence]    )
  
    @tooslow
    def 		_lowerCamelCase	(self  : List[str]    )   ->       int:
          snake_case_						       =							GPTSanJapaneseForConditionalGeneration.from_pretrained("Tanrei/GPTSAN-japanese"    )
          snake_case_						       =							GPTSanJapaneseTokenizer.from_pretrained("Tanrei/GPTSAN-japanese"    )
          # Output of original model created with mesh-tensoflow
          snake_case_						       =							[
              ("武田信玄は", 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:
                snake_case_						       =							tokenizer.encode(lowerCAmelCase_	, return_tensors="pt"    )
                snake_case_						       =							model(lowerCAmelCase_    )
                snake_case_						       =							outputs.logits.detach().cpu().numpy()[0]
                snake_case_						       =							np.argmax(output_logits[-1]    )
                self.assertEqual(lowerCAmelCase_	, lowerCAmelCase_    )
  
    @slow
    def 		_lowerCamelCase	(self  : int    )   ->       Tuple:
          snake_case_						       =							GPTSanJapaneseForConditionalGeneration.from_pretrained("Tanrei/GPTSAN-japanese"    )
          snake_case_						       =							GPTSanJapaneseTokenizer.from_pretrained("Tanrei/GPTSAN-japanese"    )
          model.to(lowerCAmelCase_    )
  
          # set deterministically
          snake_case_						       =							GenerationConfig.from_pretrained("Tanrei/GPTSAN-japanese"    )
          snake_case_						       =							1
  
          snake_case_						       =							"武田信玄は、"
          snake_case_						       =							tokenizer(lowerCAmelCase_	, return_tensors="pt"    ).input_ids.to(lowerCAmelCase_    )
          snake_case_						       =							tokenizer([input_text, input_text]	, return_tensors="pt"    ).input_ids.to(lowerCAmelCase_    )
  
          # spout from uniform and one-hot
          snake_case_						       =							[
              # 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
          ]
  
          snake_case_						       =							model.generate(
              input_ids=lowerCAmelCase_	, spout=spouts[0]	, max_new_tokens=20	, generation_config=lowerCAmelCase_	, )
  
          snake_case_						       =							model.generate(
              input_ids=lowerCAmelCase_	, spout=spouts[1]	, max_new_tokens=20	, generation_config=lowerCAmelCase_	, )
  
          snake_case_						       =							model.generate(
              input_ids=lowerCAmelCase_	, spout=lowerCAmelCase_	, max_new_tokens=20	, generation_config=lowerCAmelCase_	, )
  
          snake_case_						       =							tokenizer.decode(outputa[0]    )
          snake_case_						       =							tokenizer.decode(outputa[0]    )
          snake_case_						       =							tokenizer.batch_decode(lowerCAmelCase_    )
  
          snake_case_						       =							[
              "武田信玄は、武田氏の滅亡後、武田氏の居城であった甲斐武田氏の居城である",
              "武田信玄は、武田家の滅亡を防ぐため、武田家の家臣である武田信虎を討",
          ]
          self.assertListEqual(lowerCAmelCase_	, lowerCAmelCase_    )
          self.assertListEqual(lowerCAmelCase_	, [outa_sentence, outa_sentence]    )
  
  
    @slow
    def 		_lowerCamelCase	(self  : Tuple    )   ->       List[str]:
          snake_case_						       =							GPTSanJapaneseForConditionalGeneration.from_pretrained("Tanrei/GPTSAN-japanese"    )
          snake_case_						       =							GPTSanJapaneseTokenizer.from_pretrained("Tanrei/GPTSAN-japanese"    )
          model.to(lowerCAmelCase_    )
  
          # set deterministically
          snake_case_						       =							GenerationConfig.from_pretrained("Tanrei/GPTSAN-japanese"    )
          snake_case_						       =							1
  
          snake_case_						       =							"武田信玄"
          snake_case_						       =							"織田信長"
          snake_case_						       =							"は、"
          snake_case_						       =							"が、"
          snake_case_						       =							tokenizer(lowerCAmelCase_	, prefix_text=lowerCAmelCase_	, return_tensors="pt"    )
          snake_case_						       =							tokenizer(lowerCAmelCase_	, prefix_text=lowerCAmelCase_	, return_tensors="pt"    )
          snake_case_						       =							tokenizer([[prefix_text_a, input_text_a], [prefix_text_a, input_text_a]]	, return_tensors="pt"    )
  
          snake_case_						       =							model.generate(
              input_ids=input_tok_a.input_ids.to(lowerCAmelCase_    )	, token_type_ids=input_tok_a.token_type_ids.to(lowerCAmelCase_    )	, max_new_tokens=20	, generation_config=lowerCAmelCase_	, )
  
          snake_case_						       =							model.generate(
              input_ids=input_tok_a.input_ids.to(lowerCAmelCase_    )	, token_type_ids=input_tok_a.token_type_ids.to(lowerCAmelCase_    )	, max_new_tokens=20	, generation_config=lowerCAmelCase_	, )
  
          snake_case_						       =							model.generate(
              input_ids=input_tok_a.input_ids.to(lowerCAmelCase_    )	, token_type_ids=input_tok_a.token_type_ids.to(lowerCAmelCase_    )	, attention_mask=input_tok_a.attention_mask.to(lowerCAmelCase_    )	, max_new_tokens=20	, generation_config=lowerCAmelCase_	, )
  
          snake_case_						       =							tokenizer.decode(outputa[0]    )
          snake_case_						       =							tokenizer.decode(outputa[0]    )
          snake_case_						       =							tokenizer.batch_decode(lowerCAmelCase_    )
  
          snake_case_						       =							[
              "武田信玄は、武田氏の祖である武田信虎を、その子・武田信友を擁して",
              "織田信長が、織田信長の妻・お市の方を妻として迎えたという逸話が残",
          ]
          self.assertListEqual(lowerCAmelCase_	, lowerCAmelCase_    )
          self.assertListEqual(lowerCAmelCase_	, [outa_sentence, outa_sentence]    )
  
  
style_context: 
  '''simple docstring'''
  
  
  
  
  
  from __future__ import annotations
  
  import inspect
  import unittest
  
  import numpy as np
  
  from transformers import SwinConfig
  from transformers.testing_utils import require_tf, require_vision, slow, to_atuple
  from transformers.utils import cached_property, is_tf_available, is_vision_available
  
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
  from ...test_pipeline_mixin import PipelineTesterMixin
  
  
  if is_tf_available():
       import tensorflow as tf
  
       from transformers.models.swin.modeling_tf_swin import (
           TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
           TFSwinForImageClassification,
           TFSwinForMaskedImageModeling,
           TFSwinModel,
       )
  
  
  if is_vision_available():
       from PIL import Image
  
       from transformers import AutoImageProcessor
  
  
  
  class  lowerCAmelCase__   :
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    def __init__(self  : Optional[int]	, lowerCAmelCase_  : List[Any]	, lowerCAmelCase_  : Any=13	, lowerCAmelCase_  : int=32	, lowerCAmelCase_  : int=2	, lowerCAmelCase_  : List[str]=3	, lowerCAmelCase_  : Any=16	, lowerCAmelCase_  : Tuple=[1, 2, 1]	, lowerCAmelCase_  : List[str]=[2, 2, 4]	, lowerCAmelCase_  : Dict=2	, lowerCAmelCase_  : str=2.0	, lowerCAmelCase_  : int=True	, lowerCAmelCase_  : List[Any]=0.0	, lowerCAmelCase_  : List[Any]=0.0	, lowerCAmelCase_  : List[Any]=0.1	, lowerCAmelCase_  : Optional[int]="gelu"	, lowerCAmelCase_  : Dict=False	, lowerCAmelCase_  : str=True	, lowerCAmelCase_  : Optional[int]=0.02	, lowerCAmelCase_  : List[str]=1E-5	, lowerCAmelCase_  : Dict=True	, lowerCAmelCase_  : Tuple=None	, lowerCAmelCase_  : List[str]=True	, lowerCAmelCase_  : str=10	, lowerCAmelCase_  : Tuple=8	, )   ->       None:
          snake_case_						       =							parent
          snake_case_						       =							batch_size
          snake_case_						       =							image_size
          snake_case_						       =							patch_size
          snake_case_						       =							num_channels
          snake_case_						       =							embed_dim
          snake_case_						       =							depths
          snake_case_						       =							num_heads
          snake_case_						       =							window_size
          snake_case_						       =							mlp_ratio
          snake_case_						       =							qkv_bias
          snake_case_						       =							hidden_dropout_prob
          snake_case_						       =							attention_probs_dropout_prob
          snake_case_						       =							drop_path_rate
          snake_case_						       =							hidden_act
          snake_case_						       =							use_absolute_embeddings
          snake_case_						       =							patch_norm
          snake_case_						       =							layer_norm_eps
          snake_case_						       =							initializer_range
          snake_case_						       =							is_training
          snake_case_						       =							scope
          snake_case_						       =							use_labels
          snake_case_						       =							type_sequence_label_size
          snake_case_						       =							encoder_stride
  
    def 		_lowerCamelCase	(self  : Optional[int]    )   ->       Dict:
          snake_case_						       =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )
  
          snake_case_						       =							None
          if self.use_labels:
                snake_case_						       =							ids_tensor([self.batch_size]	, self.type_sequence_label_size    )
  
          snake_case_						       =							self.get_config()
  
          return config, pixel_values, labels
  
    def 		_lowerCamelCase	(self  : str    )   ->       Tuple:
          return SwinConfig(
              image_size=self.image_size	, patch_size=self.patch_size	, num_channels=self.num_channels	, embed_dim=self.embed_dim	, depths=self.depths	, num_heads=self.num_heads	, window_size=self.window_size	, mlp_ratio=self.mlp_ratio	, qkv_bias=self.qkv_bias	, hidden_dropout_prob=self.hidden_dropout_prob	, attention_probs_dropout_prob=self.attention_probs_dropout_prob	, drop_path_rate=self.drop_path_rate	, hidden_act=self.hidden_act	, use_absolute_embeddings=self.use_absolute_embeddings	, path_norm=self.patch_norm	, layer_norm_eps=self.layer_norm_eps	, initializer_range=self.initializer_range	, encoder_stride=self.encoder_stride	, )
  
    def 		_lowerCamelCase	(self  : Optional[int]	, lowerCAmelCase_  : int	, lowerCAmelCase_  : List[Any]	, lowerCAmelCase_  : Optional[int]    )   ->       Any:
          snake_case_						       =							TFSwinModel(config=lowerCAmelCase_    )
          snake_case_						       =							model(lowerCAmelCase_    )
  
          snake_case_						       =							((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths    ) - 1))
          snake_case_						       =							int(config.embed_dim * 2 ** (len(config.depths    ) - 1)    )
  
          self.parent.assertEqual(result.last_hidden_state.shape	, (self.batch_size, expected_seq_len, expected_dim)    )
  
    def 		_lowerCamelCase	(self  : Optional[Any]	, lowerCAmelCase_  : Optional[int]	, lowerCAmelCase_  : Dict	, lowerCAmelCase_  : Optional[int]    )   ->       Optional[int]:
          snake_case_						       =							TFSwinForMaskedImageModeling(config=lowerCAmelCase_    )
          snake_case_						       =							model(lowerCAmelCase_    )
          self.parent.assertEqual(
              result.logits.shape	, (self.batch_size, self.num_channels, self.image_size, self.image_size)    )
  
          # test greyscale images
          snake_case_						       =							1
          snake_case_						       =							TFSwinForMaskedImageModeling(lowerCAmelCase_    )
  
          snake_case_						       =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
          snake_case_						       =							model(lowerCAmelCase_    )
          self.parent.assertEqual(result.logits.shape	, (self.batch_size, 1, self.image_size, self.image_size)    )
  
    def 		_lowerCamelCase	(self  : Dict	, lowerCAmelCase_  : Tuple	, lowerCAmelCase_  : List[str]	, lowerCAmelCase_  : Optional[Any]    )   ->       Tuple:
          snake_case_						       =							self.type_sequence_label_size
          snake_case_						       =							TFSwinForImageClassification(lowerCAmelCase_    )
          snake_case_						       =							model(lowerCAmelCase_	, labels=lowerCAmelCase_    )
          self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.type_sequence_label_size)    )
  
          # test greyscale images
          snake_case_						       =							1
          snake_case_						       =							TFSwinForImageClassification(lowerCAmelCase_    )
          snake_case_						       =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
          snake_case_						       =							model(lowerCAmelCase_    )
          self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.type_sequence_label_size)    )
  
  
    def 		_lowerCamelCase	(self  : Optional[Any]    )   ->       str:
          snake_case_						       =							self.prepare_config_and_inputs()
          snake_case_       ,	snake_case_       ,	snake_case_						       =							config_and_inputs
          snake_case_						       =							{"pixel_values": pixel_values}
          return config, inputs_dict
  
  
  
  @require_tf
  class  lowerCAmelCase__   (       UpperCamelCase					,							UpperCamelCase					,							unittest.TestCase       ):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    _UpperCamelCase						= (
        (
            TFSwinModel,
            TFSwinForImageClassification,
            TFSwinForMaskedImageModeling,
        )
        if is_tf_available()
        else ()
    )
    _UpperCamelCase						= (
        {'feature-extraction': TFSwinModel, 'image-classification': TFSwinForImageClassification}
        if is_tf_available()
        else {}
    )
  
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
    _UpperCamelCase						= False
  
    def 		_lowerCamelCase	(self  : Any    )   ->       List[Any]:
          snake_case_						       =							TFSwinModelTester(self    )
          snake_case_						       =							ConfigTester(self	, config_class=lowerCAmelCase_	, embed_dim=37    )
  
    def 		_lowerCamelCase	(self  : Optional[int]    )   ->       Optional[Any]:
          self.create_and_test_config_common_properties()
          self.config_tester.create_and_test_config_to_json_string()
          self.config_tester.create_and_test_config_to_json_file()
          self.config_tester.create_and_test_config_from_and_save_pretrained()
          self.config_tester.create_and_test_config_with_num_labels()
          self.config_tester.check_config_can_be_init_without_params()
          self.config_tester.check_config_arguments_init()
  
    def 		_lowerCamelCase	(self  : List[Any]    )   ->       List[str]:
          return
  
    def 		_lowerCamelCase	(self  : Any    )   ->       Dict:
          snake_case_						       =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : int    )   ->       Any:
          snake_case_						       =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : Tuple    )   ->       Optional[Any]:
          snake_case_						       =							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_    )
  
    @unittest.skip(reason="Swin does not use inputs_embeds"    )
    def 		_lowerCamelCase	(self  : Optional[Any]    )   ->       Union[str, Any]:
          pass
  
    def 		_lowerCamelCase	(self  : List[str]    )   ->       Any:
          snake_case_       ,	snake_case_						       =							self.model_tester.prepare_config_and_inputs_for_common()
  
          for model_class in self.all_model_classes:
                snake_case_						       =							model_class(lowerCAmelCase_    )
                self.assertIsInstance(model.get_input_embeddings()	, tf.keras.layers.Layer    )
                snake_case_						       =							model.get_output_embeddings()
                self.assertTrue(x is None or isinstance(lowerCAmelCase_	, tf.keras.layers.Dense    )    )
  
    def 		_lowerCamelCase	(self  : Dict    )   ->       Dict:
          snake_case_       ,	snake_case_						       =							self.model_tester.prepare_config_and_inputs_for_common()
  
          for model_class in self.all_model_classes:
                snake_case_						       =							model_class(lowerCAmelCase_    )
                snake_case_						       =							inspect.signature(model.call    )
                # signature.parameters is an OrderedDict => so arg_names order is deterministic
                snake_case_						       =							[*signature.parameters.keys()]
  
                snake_case_						       =							["pixel_values"]
                self.assertListEqual(arg_names[:1]	, lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : Dict    )   ->       Any:
          snake_case_       ,	snake_case_						       =							self.model_tester.prepare_config_and_inputs_for_common()
          snake_case_						       =							True
  
          for model_class in self.all_model_classes:
                snake_case_						       =							True
                snake_case_						       =							False
                snake_case_						       =							True
                snake_case_						       =							model_class(lowerCAmelCase_    )
                snake_case_						       =							model(**self._prepare_for_class(lowerCAmelCase_	, lowerCAmelCase_    )    )
                snake_case_						       =							outputs.attentions
                snake_case_						       =							len(self.model_tester.depths    )
                self.assertEqual(len(lowerCAmelCase_    )	, lowerCAmelCase_    )
  
                # check that output_attentions also work using config
                del inputs_dict["output_attentions"]
                snake_case_						       =							True
                snake_case_						       =							config.window_size**2
                snake_case_						       =							model_class(lowerCAmelCase_    )
                snake_case_						       =							model(**self._prepare_for_class(lowerCAmelCase_	, lowerCAmelCase_    )    )
                snake_case_						       =							outputs.attentions
                self.assertEqual(len(lowerCAmelCase_    )	, lowerCAmelCase_    )
  
                self.assertListEqual(
                    list(attentions[0].shape[-3:]    )	, [self.model_tester.num_heads[0], window_size_squared, window_size_squared]	, )
                snake_case_						       =							len(lowerCAmelCase_    )
  
                # Check attention is always last and order is fine
                snake_case_						       =							True
                snake_case_						       =							True
                snake_case_						       =							model_class(lowerCAmelCase_    )
                snake_case_						       =							model(**self._prepare_for_class(lowerCAmelCase_	, lowerCAmelCase_    )    )
  
                if hasattr(self.model_tester	, "num_hidden_states_types"    ):
                      snake_case_						       =							self.model_tester.num_hidden_states_types
                else:
                      # also another +1 for reshaped_hidden_states
                      snake_case_						       =							2
                self.assertEqual(out_len + added_hidden_states	, len(lowerCAmelCase_    )    )
  
                snake_case_						       =							outputs.attentions
  
                self.assertEqual(len(lowerCAmelCase_    )	, lowerCAmelCase_    )
  
                self.assertListEqual(
                    list(self_attentions[0].shape[-3:]    )	, [self.model_tester.num_heads[0], window_size_squared, window_size_squared]	, )
  
    def 		_lowerCamelCase	(self  : Optional[int]	, lowerCAmelCase_  : Union[str, Any]	, lowerCAmelCase_  : int	, lowerCAmelCase_  : Dict	, lowerCAmelCase_  : List[Any]    )   ->       Optional[int]:
          snake_case_						       =							model_class(lowerCAmelCase_    )
          snake_case_						       =							model(**self._prepare_for_class(lowerCAmelCase_	, lowerCAmelCase_    )    )
          snake_case_						       =							outputs.hidden_states
  
          snake_case_						       =							getattr(
              self.model_tester	, "expected_num_hidden_layers"	, len(self.model_tester.depths    ) + 1    )
          self.assertEqual(len(lowerCAmelCase_    )	, lowerCAmelCase_    )
  
          # Swin has a different seq_length
          snake_case_						       =							to_atuple(config.patch_size    )
  
          snake_case_						       =							(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
  
          self.assertListEqual(
              list(hidden_states[0].shape[-2:]    )	, [num_patches, self.model_tester.embed_dim]	, )
  
          snake_case_						       =							outputs.reshaped_hidden_states
          self.assertEqual(len(lowerCAmelCase_    )	, lowerCAmelCase_    )
  
          snake_case_       ,	snake_case_       ,	snake_case_       ,	snake_case_						       =							reshaped_hidden_states[0].shape
  
          snake_case_						       =							tf.reshape(reshaped_hidden_states[0]	, (batch_size, num_channels, height * width)    )
          snake_case_						       =							tf.transpose(lowerCAmelCase_	, (0, 2, 1)    )
  
          self.assertListEqual(
              list(reshaped_hidden_states.shape[-2:]    )	, [num_patches, self.model_tester.embed_dim]	, )
  
    def 		_lowerCamelCase	(self  : Dict    )   ->       str:
          snake_case_       ,	snake_case_						       =							self.model_tester.prepare_config_and_inputs_for_common()
  
          snake_case_						       =							to_atuple(self.model_tester.image_size    )
  
          for model_class in self.all_model_classes:
                snake_case_						       =							True
                self.check_hidden_states_output(lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_    )
  
                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                snake_case_						       =							True
  
                self.check_hidden_states_output(lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_    )
  
    def 		_lowerCamelCase	(self  : Tuple    )   ->       List[Any]:
          snake_case_       ,	snake_case_						       =							self.model_tester.prepare_config_and_inputs_for_common()
          snake_case_						       =							3
  
          snake_case_						       =							to_atuple(self.model_tester.image_size    )
          snake_case_						       =							to_atuple(config.patch_size    )
  
          snake_case_						       =							image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
          snake_case_						       =							image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])
  
          for model_class in self.all_model_classes:
                snake_case_						       =							True
                self.check_hidden_states_output(lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_	, (padded_height, padded_width)    )
  
                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                snake_case_						       =							True
                self.check_hidden_states_output(lowerCAmelCase_	, lowerCAmelCase_	, lowerCAmelCase_	, (padded_height, padded_width)    )
  
  
    @slow
    def 		_lowerCamelCase	(self  : Any    )   ->       List[str]:
          for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                snake_case_						       =							TFSwinModel.from_pretrained(lowerCAmelCase_    )
                self.assertIsNotNone(lowerCAmelCase_    )
  
  
  
  @require_vision
  @require_tf
  class  lowerCAmelCase__   (       unittest.TestCase       ):
  
  
  
    '''simple docstring'''
  
  
  
  
  
  
    @cached_property
    def 		_lowerCamelCase	(self  : Dict    )   ->       str:
          return (
              AutoImageProcessor.from_pretrained("microsoft/swin-tiny-patch4-window7-224"    )
              if is_vision_available()
              else None
          )
  
  
    @slow
    def 		_lowerCamelCase	(self  : Optional[int]    )   ->       str:
          snake_case_						       =							TFSwinForImageClassification.from_pretrained("microsoft/swin-tiny-patch4-window7-224"    )
          snake_case_						       =							self.default_image_processor
  
          snake_case_						       =							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
          snake_case_						       =							image_processor(images=lowerCAmelCase_	, return_tensors="tf"    )
  
          # forward pass
          snake_case_						       =							model(lowerCAmelCase_    )
  
          # verify the logits
          snake_case_						       =							tf.TensorShape((1, 10_00)    )
          self.assertEqual(outputs.logits.shape	, lowerCAmelCase_    )
          snake_case_						       =							tf.constant([-0.0_948, -0.6_454, -0.0_921]    )
          self.assertTrue(np.allclose(outputs.logits[0, :3]	, lowerCAmelCase_	, atol=1E-4    )    )
  
  