




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






   """simple docstring"""






   UpperCAmelCase__										=    vocab_size
   UpperCAmelCase__										=    parent
   UpperCAmelCase__										=    batch_size
   UpperCAmelCase__										=    num_contexts
   # For common tests
   UpperCAmelCase__										=    self.num_contexts
   UpperCAmelCase__										=    is_training
   UpperCAmelCase__										=    hidden_size
   UpperCAmelCase__										=    num_ext_layers
   UpperCAmelCase__										=    ext_size
   UpperCAmelCase__										=    num_hidden_layers
   UpperCAmelCase__										=    num_attention_heads
   UpperCAmelCase__										=    num_experts
   UpperCAmelCase__										=    d_ff
   UpperCAmelCase__										=    d_ext
   UpperCAmelCase__										=    d_spout
   UpperCAmelCase__										=    dropout_rate
   UpperCAmelCase__										=    layer_norm_epsilon
   UpperCAmelCase__										=    expert_capacity
   UpperCAmelCase__										=    router_jitter_noise


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Dict ):






   """simple docstring"""






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


  def 							SCREAMING_SNAKE_CASE__			(						self    :	List[str] ):






   """simple docstring"""






   UpperCAmelCase__										=    ids_tensor([self.batch_size, self.seq_length]						,		self.vocab_size )

   UpperCAmelCase__										=    self.get_config()

   return (config, input_ids)


  def 							SCREAMING_SNAKE_CASE__			(						self    :	List[Any] ):






   """simple docstring"""






   UpperCAmelCase__										=    ids_tensor([self.batch_size, self.seq_length]						,		self.vocab_size )

   UpperCAmelCase__										=    self.get_config()

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


  def 							SCREAMING_SNAKE_CASE__			(						self    :	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 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[Any]						,		_UpperCAmelCase    :	Dict						,		_UpperCAmelCase    :	Optional[Any]						,		):






   """simple docstring"""






   UpperCAmelCase__										=    GPTSanJapaneseForConditionalGeneration(config=_UpperCAmelCase )
   model.to(_UpperCAmelCase )
   model.eval()
   UpperCAmelCase__										=    model(
       input_ids=_UpperCAmelCase						,		)
   self.parent.assertIsNotNone(_UpperCAmelCase )

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







  '''simple docstring'''







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


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int]						,		_UpperCAmelCase    :	str						,		_UpperCAmelCase    :	List[Any]						,		_UpperCAmelCase    :	Optional[int]						,		_UpperCAmelCase    :	Tuple						,		_UpperCAmelCase    :	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 							SCREAMING_SNAKE_CASE__			(						self    :	List[Any] ):






   """simple docstring"""






   UpperCAmelCase__										=    GPTSanJapaneseTester(self )
   UpperCAmelCase__										=    ConfigTester(self						,		config_class=_UpperCAmelCase						,		d_model=37 )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	str ):






   """simple docstring"""






   GPTSanJapaneseConfig()


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Dict ):






   """simple docstring"""






   UpperCAmelCase__										=    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 							SCREAMING_SNAKE_CASE__			(						self    :	List[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 							SCREAMING_SNAKE_CASE__			(						self    :	Any ):






   """simple docstring"""






   pass

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







  '''simple docstring'''







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


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Dict ):






   """simple docstring"""






   UpperCAmelCase__										=    GPTSanJapaneseTester(self )
   UpperCAmelCase__										=    ConfigTester(self						,		config_class=_UpperCAmelCase						,		d_model=37 )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	str ):






   """simple docstring"""






   GPTSanJapaneseConfig()


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Union[str, Any] ):






   """simple docstring"""






   UpperCAmelCase__										=    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 							SCREAMING_SNAKE_CASE__			(						self    :	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 							SCREAMING_SNAKE_CASE__			(						self    :	Tuple ):






   """simple docstring"""






   pass


  @slow
  def 							SCREAMING_SNAKE_CASE__			(						self    :	str ):






   """simple docstring"""






   UpperCAmelCase__										=    GPTSanJapaneseForConditionalGeneration.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   UpperCAmelCase__										=    GPTSanJapaneseTokenizer.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   UpperCAmelCase__										=    tokenizer.encode("""武田信玄は"""						,		return_tensors="""pt""" )
   UpperCAmelCase__										=    model(_UpperCAmelCase )
   UpperCAmelCase__										=    outputs.logits.detach().cpu().numpy()
   # Output of original model created with mesh-tensoflow
   UpperCAmelCase__										=    [
       # 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
   ]
   UpperCAmelCase__										=    np.array(_UpperCAmelCase ).flatten()
   UpperCAmelCase__										=    output_logits[0, :, :20].flatten()

   def check(_UpperCAmelCase    :	Union[str, Any]						,		_UpperCAmelCase    :	Optional[Any]						,		_UpperCAmelCase    :	List[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 							SCREAMING_SNAKE_CASE__			(						self    :	Union[str, Any] ):






   """simple docstring"""






   UpperCAmelCase__										=    GPTSanJapaneseForConditionalGeneration.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   UpperCAmelCase__										=    GPTSanJapaneseTokenizer.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   model.to(_UpperCAmelCase )

   # set deterministically
   UpperCAmelCase__										=    GenerationConfig.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   UpperCAmelCase__										=    1

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

   UpperCAmelCase__										=    """left"""
   UpperCAmelCase__										=    tokenizer(_UpperCAmelCase						,		return_tensors="""pt"""						,		padding=_UpperCAmelCase )
   UpperCAmelCase__										=    inputs["""input_ids"""].to(_UpperCAmelCase )

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

   UpperCAmelCase__										=    model.generate(
       input_ids=_UpperCAmelCase						,		attention_mask=inputs["""attention_mask"""].to(_UpperCAmelCase )						,		max_new_tokens=3						,		generation_config=_UpperCAmelCase						,		)

   UpperCAmelCase__										=    tokenizer(sentences[0]						,		return_tensors="""pt""" ).input_ids.to(_UpperCAmelCase )
   UpperCAmelCase__										=    model.generate(
       input_ids=_UpperCAmelCase						,		max_new_tokens=3						,		generation_config=_UpperCAmelCase )

   UpperCAmelCase__										=    tokenizer(sentences[1]						,		return_tensors="""pt""" ).input_ids.to(_UpperCAmelCase )
   UpperCAmelCase__										=    model.generate(input_ids=_UpperCAmelCase						,		max_new_tokens=3						,		generation_config=_UpperCAmelCase )

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

   UpperCAmelCase__										=    tokenizer.batch_decode(_UpperCAmelCase						,		skip_special_tokens=_UpperCAmelCase )
   UpperCAmelCase__										=    tokenizer.decode(output_non_padded[0]						,		skip_special_tokens=_UpperCAmelCase )
   UpperCAmelCase__										=    tokenizer.decode(output_padded[0]						,		skip_special_tokens=_UpperCAmelCase )

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


  @tooslow
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Tuple ):






   """simple docstring"""






   UpperCAmelCase__										=    GPTSanJapaneseForConditionalGeneration.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   UpperCAmelCase__										=    GPTSanJapaneseTokenizer.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   # Output of original model created with mesh-tensoflow
   UpperCAmelCase__										=    [
       ("""武田信玄は""", 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:
    UpperCAmelCase__										=    tokenizer.encode(_UpperCAmelCase						,		return_tensors="""pt""" )
    UpperCAmelCase__										=    model(_UpperCAmelCase )
    UpperCAmelCase__										=    outputs.logits.detach().cpu().numpy()[0]
    UpperCAmelCase__										=    np.argmax(output_logits[-1] )
    self.assertEqual(_UpperCAmelCase						,		_UpperCAmelCase )


  @slow
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Union[str, Any] ):






   """simple docstring"""






   UpperCAmelCase__										=    GPTSanJapaneseForConditionalGeneration.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   UpperCAmelCase__										=    GPTSanJapaneseTokenizer.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   model.to(_UpperCAmelCase )

   # set deterministically
   UpperCAmelCase__										=    GenerationConfig.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   UpperCAmelCase__										=    1

   UpperCAmelCase__										=    """武田信玄は、"""
   UpperCAmelCase__										=    tokenizer(_UpperCAmelCase						,		return_tensors="""pt""" ).input_ids.to(_UpperCAmelCase )
   UpperCAmelCase__										=    tokenizer([input_text, input_text]						,		return_tensors="""pt""" ).input_ids.to(_UpperCAmelCase )

   # spout from uniform and one-hot
   UpperCAmelCase__										=    [
       # 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
   ]

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

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

   UpperCAmelCase__										=    model.generate(
       input_ids=_UpperCAmelCase						,		spout=_UpperCAmelCase						,		max_new_tokens=20						,		generation_config=_UpperCAmelCase						,		)

   UpperCAmelCase__										=    tokenizer.decode(outputa[0] )
   UpperCAmelCase__										=    tokenizer.decode(outputa[0] )
   UpperCAmelCase__										=    tokenizer.batch_decode(_UpperCAmelCase )

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



  @slow
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Any ):






   """simple docstring"""






   UpperCAmelCase__										=    GPTSanJapaneseForConditionalGeneration.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   UpperCAmelCase__										=    GPTSanJapaneseTokenizer.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   model.to(_UpperCAmelCase )

   # set deterministically
   UpperCAmelCase__										=    GenerationConfig.from_pretrained("""Tanrei/GPTSAN-japanese""" )
   UpperCAmelCase__										=    1

   UpperCAmelCase__										=    """武田信玄"""
   UpperCAmelCase__										=    """織田信長"""
   UpperCAmelCase__										=    """は、"""
   UpperCAmelCase__										=    """が、"""
   UpperCAmelCase__										=    tokenizer(_UpperCAmelCase						,		prefix_text=_UpperCAmelCase						,		return_tensors="""pt""" )
   UpperCAmelCase__										=    tokenizer(_UpperCAmelCase						,		prefix_text=_UpperCAmelCase						,		return_tensors="""pt""" )
   UpperCAmelCase__										=    tokenizer([[prefix_text_a, input_text_a], [prefix_text_a, input_text_a]]						,		return_tensors="""pt""" )

   UpperCAmelCase__										=    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						,		)

   UpperCAmelCase__										=    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						,		)

   UpperCAmelCase__										=    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						,		)

   UpperCAmelCase__										=    tokenizer.decode(outputa[0] )
   UpperCAmelCase__										=    tokenizer.decode(outputa[0] )
   UpperCAmelCase__										=    tokenizer.batch_decode(_UpperCAmelCase )

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






'''simple docstring'''




import collections
import inspect
import unittest

from transformers import DinatConfig
from transformers.testing_utils import require_natten, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import DinatBackbone, DinatForImageClassification, DinatModel
    from transformers.models.dinat.modeling_dinat import DINAT_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor

class 						lowerCAmelCase_   :







  '''simple docstring'''







  def __init__(						self    :	Union[str, Any]						,		_UpperCAmelCase    :	List[Any]						,		_UpperCAmelCase    :	int=13						,		_UpperCAmelCase    :	List[str]=64						,		_UpperCAmelCase    :	List[str]=4						,		_UpperCAmelCase    :	int=3						,		_UpperCAmelCase    :	Tuple=16						,		_UpperCAmelCase    :	Optional[Any]=[1, 2, 1]						,		_UpperCAmelCase    :	List[Any]=[2, 4, 8]						,		_UpperCAmelCase    :	str=3						,		_UpperCAmelCase    :	Tuple=[[3], [1, 2], [1]]						,		_UpperCAmelCase    :	Union[str, Any]=2.0						,		_UpperCAmelCase    :	List[str]=True						,		_UpperCAmelCase    :	Optional[int]=0.0						,		_UpperCAmelCase    :	Optional[Any]=0.0						,		_UpperCAmelCase    :	List[str]=0.1						,		_UpperCAmelCase    :	str="gelu"						,		_UpperCAmelCase    :	List[str]=True						,		_UpperCAmelCase    :	Tuple=0.02						,		_UpperCAmelCase    :	Optional[Any]=1E-5						,		_UpperCAmelCase    :	Any=True						,		_UpperCAmelCase    :	List[str]=None						,		_UpperCAmelCase    :	Any=True						,		_UpperCAmelCase    :	List[str]=10						,		_UpperCAmelCase    :	Any=["stage1", "stage2"]						,		_UpperCAmelCase    :	Union[str, Any]=[1, 2]						,		):






   """simple docstring"""






   UpperCAmelCase__										=    parent
   UpperCAmelCase__										=    batch_size
   UpperCAmelCase__										=    image_size
   UpperCAmelCase__										=    patch_size
   UpperCAmelCase__										=    num_channels
   UpperCAmelCase__										=    embed_dim
   UpperCAmelCase__										=    depths
   UpperCAmelCase__										=    num_heads
   UpperCAmelCase__										=    kernel_size
   UpperCAmelCase__										=    dilations
   UpperCAmelCase__										=    mlp_ratio
   UpperCAmelCase__										=    qkv_bias
   UpperCAmelCase__										=    hidden_dropout_prob
   UpperCAmelCase__										=    attention_probs_dropout_prob
   UpperCAmelCase__										=    drop_path_rate
   UpperCAmelCase__										=    hidden_act
   UpperCAmelCase__										=    patch_norm
   UpperCAmelCase__										=    layer_norm_eps
   UpperCAmelCase__										=    initializer_range
   UpperCAmelCase__										=    is_training
   UpperCAmelCase__										=    scope
   UpperCAmelCase__										=    use_labels
   UpperCAmelCase__										=    num_labels
   UpperCAmelCase__										=    out_features
   UpperCAmelCase__										=    out_indices


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int] ):






   """simple docstring"""






   UpperCAmelCase__										=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

   UpperCAmelCase__										=    None
   if self.use_labels:
    UpperCAmelCase__										=    ids_tensor([self.batch_size]						,		self.num_labels )

   UpperCAmelCase__										=    self.get_config()

   return config, pixel_values, labels


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Any ):






   """simple docstring"""






   return DinatConfig(
       num_labels=self.num_labels						,		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						,		kernel_size=self.kernel_size						,		dilations=self.dilations						,		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						,		patch_norm=self.patch_norm						,		layer_norm_eps=self.layer_norm_eps						,		initializer_range=self.initializer_range						,		out_features=self.out_features						,		out_indices=self.out_indices						,		)


  def 							SCREAMING_SNAKE_CASE__			(						self    :	int						,		_UpperCAmelCase    :	Optional[Any]						,		_UpperCAmelCase    :	int						,		_UpperCAmelCase    :	Tuple ):






   """simple docstring"""






   UpperCAmelCase__										=    DinatModel(config=_UpperCAmelCase )
   model.to(_UpperCAmelCase )
   model.eval()
   UpperCAmelCase__										=    model(_UpperCAmelCase )

   UpperCAmelCase__										=    UpperCAmelCase__										=    (config.image_size // config.patch_size) // (2 ** (len(config.depths ) - 1))
   UpperCAmelCase__										=    int(config.embed_dim * 2 ** (len(config.depths ) - 1) )

   self.parent.assertEqual(
       result.last_hidden_state.shape						,		(self.batch_size, expected_height, expected_width, expected_dim) )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int]						,		_UpperCAmelCase    :	str						,		_UpperCAmelCase    :	int						,		_UpperCAmelCase    :	str ):






   """simple docstring"""






   UpperCAmelCase__										=    DinatForImageClassification(_UpperCAmelCase )
   model.to(_UpperCAmelCase )
   model.eval()
   UpperCAmelCase__										=    model(_UpperCAmelCase						,		labels=_UpperCAmelCase )
   self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.num_labels) )

   # test greyscale images
   UpperCAmelCase__										=    1
   UpperCAmelCase__										=    DinatForImageClassification(_UpperCAmelCase )
   model.to(_UpperCAmelCase )
   model.eval()

   UpperCAmelCase__										=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
   UpperCAmelCase__										=    model(_UpperCAmelCase )
   self.parent.assertEqual(result.logits.shape						,		(self.batch_size, self.num_labels) )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Any						,		_UpperCAmelCase    :	List[Any]						,		_UpperCAmelCase    :	List[str]						,		_UpperCAmelCase    :	Any ):






   """simple docstring"""






   UpperCAmelCase__										=    DinatBackbone(config=_UpperCAmelCase )
   model.to(_UpperCAmelCase )
   model.eval()
   UpperCAmelCase__										=    model(_UpperCAmelCase )

   # verify hidden states
   self.parent.assertEqual(len(result.feature_maps )						,		len(config.out_features ) )
   self.parent.assertListEqual(list(result.feature_maps[0].shape )						,		[self.batch_size, model.channels[0], 16, 16] )

   # verify channels
   self.parent.assertEqual(len(model.channels )						,		len(config.out_features ) )

   # verify backbone works with out_features=None
   UpperCAmelCase__										=    None
   UpperCAmelCase__										=    DinatBackbone(config=_UpperCAmelCase )
   model.to(_UpperCAmelCase )
   model.eval()
   UpperCAmelCase__										=    model(_UpperCAmelCase )

   # verify feature maps
   self.parent.assertEqual(len(result.feature_maps )						,		1 )
   self.parent.assertListEqual(list(result.feature_maps[0].shape )						,		[self.batch_size, model.channels[-1], 4, 4] )

   # verify channels
   self.parent.assertEqual(len(model.channels )						,		1 )



  def 							SCREAMING_SNAKE_CASE__			(						self    :	Tuple ):






   """simple docstring"""






   UpperCAmelCase__										=    self.prepare_config_and_inputs()
   UpperCAmelCase__							,			UpperCAmelCase__							,			UpperCAmelCase__										=    config_and_inputs
   UpperCAmelCase__										=    {"""pixel_values""": pixel_values}
   return config, inputs_dict

@require_natten
@require_torch
class 						lowerCAmelCase_   ( lowerCamelCase_			,  lowerCamelCase_			,  unittest.TestCase   ):







  '''simple docstring'''







  lowerCAmelCase_				:					List[str]				     =						(
      (
          DinatModel,
          DinatForImageClassification,
          DinatBackbone,
      )
      if is_torch_available()
      else ()
  )
  lowerCAmelCase_				:					List[str]				     =						(
      {"""feature-extraction""": DinatModel, """image-classification""": DinatForImageClassification}
      if is_torch_available()
      else {}
  )
  lowerCAmelCase_				:					Union[str, Any]				     =						False

  lowerCAmelCase_				:					Union[str, Any]				     =						False
  lowerCAmelCase_				:					List[str]				     =						False
  lowerCAmelCase_				:					Any				     =						False
  lowerCAmelCase_				:					Any				     =						False


  def 							SCREAMING_SNAKE_CASE__			(						self    :	List[Any] ):






   """simple docstring"""






   UpperCAmelCase__										=    DinatModelTester(self )
   UpperCAmelCase__										=    ConfigTester(self						,		config_class=_UpperCAmelCase						,		embed_dim=37 )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[Any] ):






   """simple docstring"""






   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 							SCREAMING_SNAKE_CASE__			(						self    :	int ):






   """simple docstring"""






   return


  def 							SCREAMING_SNAKE_CASE__			(						self    :	List[str] ):






   """simple docstring"""






   UpperCAmelCase__										=    self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*_UpperCAmelCase )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Union[str, Any] ):






   """simple docstring"""






   UpperCAmelCase__										=    self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	str ):






   """simple docstring"""






   UpperCAmelCase__										=    self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_backbone(*_UpperCAmelCase )


  @unittest.skip(reason="""Dinat does not use inputs_embeds""" )
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Dict ):






   """simple docstring"""






   pass


  @unittest.skip(reason="""Dinat does not use feedforward chunking""" )
  def 							SCREAMING_SNAKE_CASE__			(						self    :	List[str] ):






   """simple docstring"""






   pass


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int] ):






   """simple docstring"""






   UpperCAmelCase__							,			UpperCAmelCase__										=    self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
    UpperCAmelCase__										=    model_class(_UpperCAmelCase )
    self.assertIsInstance(model.get_input_embeddings()						,		(nn.Module) )
    UpperCAmelCase__										=    model.get_output_embeddings()
    self.assertTrue(x is None or isinstance(_UpperCAmelCase						,		nn.Linear ) )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[Any] ):






   """simple docstring"""






   UpperCAmelCase__							,			UpperCAmelCase__										=    self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
    UpperCAmelCase__										=    model_class(_UpperCAmelCase )
    UpperCAmelCase__										=    inspect.signature(model.forward )
    # signature.parameters is an OrderedDict => so arg_names order is deterministic
    UpperCAmelCase__										=    [*signature.parameters.keys()]

    UpperCAmelCase__										=    ["""pixel_values"""]
    self.assertListEqual(arg_names[:1]						,		_UpperCAmelCase )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Any ):






   """simple docstring"""






   self.skipTest("""Dinat's attention operation is handled entirely by NATTEN.""" )


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Any						,		_UpperCAmelCase    :	Optional[Any]						,		_UpperCAmelCase    :	List[str]						,		_UpperCAmelCase    :	Optional[Any]						,		_UpperCAmelCase    :	Dict ):






   """simple docstring"""






   UpperCAmelCase__										=    model_class(_UpperCAmelCase )
   model.to(_UpperCAmelCase )
   model.eval()

   with torch.no_grad():
    UpperCAmelCase__										=    model(**self._prepare_for_class(_UpperCAmelCase						,		_UpperCAmelCase ) )

   UpperCAmelCase__										=    outputs.hidden_states

   UpperCAmelCase__										=    getattr(
       self.model_tester						,		"""expected_num_hidden_layers"""						,		len(self.model_tester.depths ) + 1 )
   self.assertEqual(len(_UpperCAmelCase )						,		_UpperCAmelCase )

   # Dinat has a different seq_length
   UpperCAmelCase__										=    (
       config.patch_size
       if isinstance(config.patch_size						,		collections.abc.Iterable )
       else (config.patch_size, config.patch_size)
   )

   UpperCAmelCase__										=    image_size[0] // patch_size[0]
   UpperCAmelCase__										=    image_size[1] // patch_size[1]

   self.assertListEqual(
       list(hidden_states[0].shape[-3:] )						,		[height, width, self.model_tester.embed_dim]						,		)

   if model_class.__name__ != "DinatBackbone":
    UpperCAmelCase__										=    outputs.reshaped_hidden_states
    self.assertEqual(len(_UpperCAmelCase )						,		_UpperCAmelCase )

    UpperCAmelCase__							,			UpperCAmelCase__							,			UpperCAmelCase__							,			UpperCAmelCase__										=    reshaped_hidden_states[0].shape
    UpperCAmelCase__										=    (
        reshaped_hidden_states[0].view(_UpperCAmelCase						,		_UpperCAmelCase						,		_UpperCAmelCase						,		_UpperCAmelCase ).permute(0						,		2						,		3						,		1 )
    )
    self.assertListEqual(
        list(reshaped_hidden_states.shape[-3:] )						,		[height, width, self.model_tester.embed_dim]						,		)


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int] ):






   """simple docstring"""






   UpperCAmelCase__							,			UpperCAmelCase__										=    self.model_tester.prepare_config_and_inputs_for_common()

   UpperCAmelCase__										=    (
       self.model_tester.image_size
       if isinstance(self.model_tester.image_size						,		collections.abc.Iterable )
       else (self.model_tester.image_size, self.model_tester.image_size)
   )

   for model_class in self.all_model_classes:
    UpperCAmelCase__										=    True
    self.check_hidden_states_output(_UpperCAmelCase						,		_UpperCAmelCase						,		_UpperCAmelCase						,		_UpperCAmelCase )

    # check that output_hidden_states also work using config
    del inputs_dict["output_hidden_states"]
    UpperCAmelCase__										=    True

    self.check_hidden_states_output(_UpperCAmelCase						,		_UpperCAmelCase						,		_UpperCAmelCase						,		_UpperCAmelCase )


  @slow
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[Any] ):






   """simple docstring"""






   for model_name in DINAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
    UpperCAmelCase__										=    DinatModel.from_pretrained(_UpperCAmelCase )
    self.assertIsNotNone(_UpperCAmelCase )



  def 							SCREAMING_SNAKE_CASE__			(						self    :	Dict ):






   """simple docstring"""






   UpperCAmelCase__							,			UpperCAmelCase__										=    self.model_tester.prepare_config_and_inputs_for_common()

   UpperCAmelCase__										=    _config_zero_init(_UpperCAmelCase )
   for model_class in self.all_model_classes:
    UpperCAmelCase__										=    model_class(config=_UpperCAmelCase )
    for name, param in model.named_parameters():
     if "embeddings" not in name and param.requires_grad:
      self.assertIn(
          ((param.data.mean() * 1E9).round() / 1E9).item()						,		[0.0, 1.0]						,		msg=f'''Parameter {name} of model {model_class} seems not properly initialized'''						,		)

@require_natten
@require_vision
@require_torch
class 						lowerCAmelCase_   ( unittest.TestCase   ):







  '''simple docstring'''







  @cached_property
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Any ):






   """simple docstring"""






   return AutoImageProcessor.from_pretrained("""shi-labs/dinat-mini-in1k-224""" ) if is_vision_available() else None



  @slow
  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int] ):






   """simple docstring"""






   UpperCAmelCase__										=    DinatForImageClassification.from_pretrained("""shi-labs/dinat-mini-in1k-224""" ).to(_UpperCAmelCase )
   UpperCAmelCase__										=    self.default_image_processor

   UpperCAmelCase__										=    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
   UpperCAmelCase__										=    image_processor(images=_UpperCAmelCase						,		return_tensors="""pt""" ).to(_UpperCAmelCase )

   # forward pass
   with torch.no_grad():
    UpperCAmelCase__										=    model(**_UpperCAmelCase )

   # verify the logits
   UpperCAmelCase__										=    torch.Size((1, 10_00) )
   self.assertEqual(outputs.logits.shape						,		_UpperCAmelCase )
   UpperCAmelCase__										=    torch.tensor([-0.1545, -0.7667, 0.4642] ).to(_UpperCAmelCase )
   self.assertTrue(torch.allclose(outputs.logits[0, :3]						,		_UpperCAmelCase						,		atol=1E-4 ) )

@require_torch
@require_natten
class 						lowerCAmelCase_   ( unittest.TestCase			,  lowerCamelCase_   ):







  '''simple docstring'''







  lowerCAmelCase_				:					Any				     =						(DinatBackbone,) if is_torch_available() else ()
  lowerCAmelCase_				:					Optional[Any]				     =						DinatConfig


  def 							SCREAMING_SNAKE_CASE__			(						self    :	Optional[int] ):






   """simple docstring"""






   UpperCAmelCase__										=    DinatModelTester(self )
