




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







     def __init__(	self  ,		A  ,		A=36000  ,		A=13  ,		A=7  ,		A=True  ,		A=32  ,		A=42  ,		A=5  ,		A=2  ,		A=4  ,		A=2  ,		A=32  ,		A=80  ,		A=33  ,		A=0.0  ,		A=1e-6  ,		A=100  ,		A=0.0  ,		)  ->    Union[str, Any]:



          '''simple docstring'''



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







     def 			lowerCAmelCase_				(	self )  ->    List[Any]:



          '''simple docstring'''



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







     def 			lowerCAmelCase_				(	self )  ->    Optional[int]:



          '''simple docstring'''



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

          a      =      self.get_config()

          return (config, input_ids)







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



          '''simple docstring'''



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

          a      =      self.get_config()

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







     def 			lowerCAmelCase_				(	self )  ->    int:



          '''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 			lowerCAmelCase_				(	self  ,		A  ,		A  ,		)  ->    str:



          '''simple docstring'''



          a      =      GPTSanJapaneseForConditionalGeneration(config=A )
          model.to(A )
          model.eval()
          a      =      model(
              input_ids=A  ,		)
          self.parent.assertIsNotNone(A )







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







     def 			lowerCAmelCase_				(	self  ,		A  ,		A  ,		A  ,		A  ,		A )  ->    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 			lowerCAmelCase_				(	self )  ->    Dict:



          '''simple docstring'''



          a      =      GPTSanJapaneseTester(self )
          a      =      ConfigTester(self  ,		config_class=A  ,		d_model=37 )







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



          '''simple docstring'''



          GPTSanJapaneseConfig()







     def 			lowerCAmelCase_				(	self )  ->    str:



          '''simple docstring'''



          a      =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*A )







     @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 )  ->    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 			lowerCAmelCase_				(	self )  ->    List[str]:



          '''simple docstring'''



          pass







@require_torch
class 			a__       (					UpperCamelCase__   ,					UpperCamelCase__   ,					unittest.TestCase      ):
     a		:			Optional[int]							=	(GPTSanJapaneseForConditionalGeneration,) if is_torch_available() else ()
     a		:			List[Any]							=	False
     a		:			Union[str, Any]							=	False
     a		:			Dict							=	False
     a		:			int							=	False
     a		:			Optional[Any]							=	False
     a		:			Any							=	False
     # The small GPTSAN_JAPANESE model needs higher percentages for CPU/MP tests
     a		:			Any							=	[0.8, 0.9]







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



          '''simple docstring'''



          a      =      GPTSanJapaneseTester(self )
          a      =      ConfigTester(self  ,		config_class=A  ,		d_model=37 )







     def 			lowerCAmelCase_				(	self )  ->    Tuple:



          '''simple docstring'''



          GPTSanJapaneseConfig()







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



          '''simple docstring'''



          a      =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*A )







     @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 )  ->    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 			lowerCAmelCase_				(	self )  ->    Any:



          '''simple docstring'''



          pass







     @slow
     def 			lowerCAmelCase_				(	self )  ->    Any:



          '''simple docstring'''



          a      =      GPTSanJapaneseForConditionalGeneration.from_pretrained("Tanrei/GPTSAN-japanese" )
          a      =      GPTSanJapaneseTokenizer.from_pretrained("Tanrei/GPTSAN-japanese" )
          a      =      tokenizer.encode("武田信玄は"  ,		return_tensors="pt" )
          a      =      model(A )
          a      =      outputs.logits.detach().cpu().numpy()
          # Output of original model created with mesh-tensoflow
          a      =      [
              # fmt: off
              [-1_2.0_3_7_8_3_9_8_8_9_5_2_6_3_6_7, -1_2.4_3_3_0_6_1_5_9_9_7_3_1_4_4_5, -1_4.3_3_3_8_4_0_3_7_0_1_7_8_2_2_3, -1_2.4_5_0_3_4_5_9_9_3_0_4_1_9_9_2, -1_1.1_6_6_1_3_7_6_9_5_3_1_2_5,
              -1_1.9_3_0_1_3_7_6_3_4_2_7_7_3_4_4, -1_0.6_5_9_7_4_0_4_4_7_9_9_8_0_4_7, -1_2.9_0_9_5_7_4_5_0_8_6_6_6_9_9_2, -1_3.2_4_1_0_4_3_0_9_0_8_2_0_3_1_2, -1_3.3_9_8_5_7_9_5_9_7_4_7_3_1_4_5,
              -1_1.1_0_7_5_2_4_8_7_1_8_2_6_1_7_2, -1_2.3_6_8_5_9_4_1_6_9_6_1_6_7, -2_2.9_7_9_4_3_1_1_5_2_3_4_3_7_5, -1_0.4_8_1_0_6_7_6_5_7_4_7_0_7_0_3, -1_2.4_8_4_0_3_0_7_2_3_5_7_1_7_7_7,
              -1_2.8_0_7_3_6_0_6_4_9_1_0_8_8_8_7, -1_4.7_6_9_7_0_0_0_5_0_3_5_4_0_0_4, -1_2.2_3_3_5_7_9_6_3_5_6_2_0_1_1_7, -1_3.4_2_8_1_4_5_4_0_8_6_3_0_3_7_1, -2_2.6_2_4_1_7_7_9_3_2_7_3_9_2_5_8],
              [-7.5_1_1_1_4_9_8_8_3_2_7_0_2_6_4, -8.2_8_1_8_5_1_7_6_8_4_9_3_6_5_2, -7.9_4_3_1_2_7_1_5_5_3_0_3_9_5_5, -7.5_5_0_2_1_3_3_3_6_9_4_4_5_8, -6.4_9_8_6_9_9_6_6_5_0_6_9_5_8,
              -7.5_8_6_7_9_6_2_8_3_7_2_1_9_2_4, -6.9_7_8_0_8_5_9_9_4_7_2_0_4_5_9, -7.8_3_9_1_4_5_1_8_3_5_6_3_2_3_2, -8.2_1_9_6_4_1_6_8_5_4_8_5_8_4, -8.6_9_5_0_9_1_2_4_7_5_5_8_5_9_4,
              -6.7_0_6_9_1_0_6_1_0_1_9_8_9_7_5, -6.6_5_8_5_7_9_8_2_6_3_5_4_9_8_0_5, -1_9.5_6_5_6_9_8_6_2_3_6_5_7_2_2_7, -5.3_5_3_8_4_2_7_3_5_2_9_0_5_2_7, -8.3_5_0_6_8_6_0_7_3_3_0_3_2_2_3,
              -8.0_3_9_3_8_8_6_5_6_6_1_6_2_1_1, -1_0.8_5_6_5_6_9_2_9_0_1_6_1_1_3_3, -7.7_5_1_5_4_4_4_7_5_5_5_5_4_2, -8.8_1_9_0_2_2_1_7_8_6_4_9_9_0_2, -1_9.5_1_5_3_2_7_4_5_3_6_1_3_2_8],
              [-9.7_3_0_6_6_2_3_4_5_8_8_6_2_3, -1_0.2_2_3_9_2_2_7_2_9_4_9_2_1_8_8, -9.9_3_2_9_8_1_4_9_1_0_8_8_8_6_7, -1_1.8_5_7_8_3_6_7_2_3_3_2_7_6_3_7, -7.6_6_2_6_2_6_2_6_6_4_7_9_4_9_2,
              -1_1.1_3_5_2_9_1_0_9_9_5_4_8_3_4, -7.7_6_5_0_9_7_6_1_8_1_0_3_0_2_7, -1_1.4_7_2_9_2_3_2_7_8_8_0_8_5_9_4, -9.5_4_3_1_4_9_9_4_8_1_2_0_1_1_7, -1_1.9_0_5_6_3_3_9_2_6_3_9_1_6_0_2,
              -9.3_6_6_1_6_4_2_0_7_4_5_8_4_9_6, -1_1.5_7_3_4_2_8_1_5_3_9_9_1_7, -2_3.6_9_9_0_0_3_2_1_9_6_0_4_4_9_2, -9.4_2_9_5_9_0_2_2_5_2_1_9_7_2_7, -1_0.4_2_8_3_9_2_4_1_0_2_7_8_3_2,
              -1_0.5_8_5_2_4_0_3_6_4_0_7_4_7_0_7, -1_0.9_4_7_7_1_9_5_7_3_9_7_4_6_1, -1_1.0_9_5_4_1_6_0_6_9_0_3_0_7_6_2, -1_0.3_9_0_2_4_0_6_6_9_2_5_0_4_8_8, -2_3.7_6_9_3_7_2_9_4_0_0_6_3_4_7_7],
              [-9.7_2_8_2_6_5_7_6_2_3_2_9_1_0_2, -9.8_5_9_7_1_2_6_0_0_7_0_8_0_0_8, -1_0.0_9_7_2_9_2_9_0_0_0_8_5_4_5, -9.6_7_8_5_2_2_1_0_9_9_8_5_3_5_2, -6.8_7_9_5_1_9_9_3_9_4_2_2_6_0_7,
              -9.6_8_4_8_7_5_4_8_8_2_8_1_2_5, -4.2_8_0_3_4_2_5_7_8_8_8_7_9_3_9_5, -1_0.0_1_8_9_1_4_2_2_2_7_1_7_2_8_5, -9.3_0_8_4_4_5_9_3_0_4_8_0_9_5_7, -1_0.6_3_3_9_4_5_4_6_5_0_8_7_8_9,
              -8.0_8_3_6_4_6_7_7_4_2_9_1_9_9_2, -9.0_6_3_0_1_4_9_8_4_1_3_0_8_6, -2_1.9_0_4_2_6_6_3_5_7_4_2_1_8_7_5, -8.9_0_1_6_0_8_4_6_7_1_0_2_0_5, -8.8_4_1_8_7_6_0_2_9_9_6_8_2_6_2,
              -1_1.8_5_6_7_1_9_9_7_0_7_0_3_1_2_5, -1_2.0_7_9_3_9_8_1_5_5_2_1_2_4_0_2, -1_1.2_3_3_7_5_3_2_0_4_3_4_5_7_0_3, -1_0.1_7_7_3_3_8_6_0_0_1_5_8_6_9_1, -2_1.8_7_2_5_6_6_2_2_3_1_4_4_5_3],
              [-9.6_6_9_7_6_4_5_1_8_7_3_7_7_9_3, -9.6_1_4_1_9_8_6_8_4_6_9_2_3_8_3, -9.8_1_4_5_1_0_3_4_5_4_5_8_9_8_4, -9.9_9_6_5_0_1_9_2_2_6_0_7_4_2_2, -1_1.3_7_5_6_9_0_4_6_0_2_0_5_0_7_8,
              -1_0.1_1_3_4_0_5_2_2_7_6_6_1_1_3_3, -1_0.5_4_6_8_6_7_3_7_0_6_0_5_4_6_9, -1_0.0_4_3_6_9_0_6_8_1_4_5_7_5_2, -1_0.9_0_7_8_0_9_2_5_7_5_0_7_3_2_4, -1_0.5_0_4_2_1_6_1_9_4_1_5_2_8_3_2,
              -1_1.1_2_9_1_9_9_0_2_8_0_1_5_1_3_7, -1_0.1_5_1_1_2_4_0_0_0_5_4_9_3_1_6, -2_1.9_6_5_8_6_7_9_9_6_2_1_5_8_2, -9.0_8_6_3_4_9_4_8_7_3_0_4_6_8_8, -1_1.7_3_0_3_3_9_0_5_0_2_9_2_9_6_9,
              -1_0.4_6_0_6_6_7_6_1_0_1_6_8_4_5_7, -1_0.2_9_8_0_4_9_9_2_6_7_5_7_8_1_2, -1_0.7_8_4_1_4_8_2_1_6_2_4_7_5_5_9, -1_0.8_4_0_6_9_3_4_7_3_8_1_5_9_1_8, -2_2.0_3_1_5_2_8_4_7_2_9_0_0_3_9],
              # fmt: on
          ]
          a      =      np.array(A ).flatten()
          a      =      output_logits[0, :, :20].flatten()

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

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







     @slow
     def 			lowerCAmelCase_				(	self )  ->    Optional[Any]:



          '''simple docstring'''



          a      =      GPTSanJapaneseForConditionalGeneration.from_pretrained("Tanrei/GPTSAN-japanese" )
          a      =      GPTSanJapaneseTokenizer.from_pretrained("Tanrei/GPTSAN-japanese" )
          model.to(A )

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

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

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

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

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

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

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

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

          a      =      tokenizer.batch_decode(A  ,		skip_special_tokens=A )
          a      =      tokenizer.decode(output_non_padded[0]  ,		skip_special_tokens=A )
          a      =      tokenizer.decode(output_padded[0]  ,		skip_special_tokens=A )

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







     @tooslow
     def 			lowerCAmelCase_				(	self )  ->    str:



          '''simple docstring'''



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







     @slow
     def 			lowerCAmelCase_				(	self )  ->    Optional[Any]:



          '''simple docstring'''



          a      =      GPTSanJapaneseForConditionalGeneration.from_pretrained("Tanrei/GPTSAN-japanese" )
          a      =      GPTSanJapaneseTokenizer.from_pretrained("Tanrei/GPTSAN-japanese" )
          model.to(A )

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

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

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

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

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

          a      =      model.generate(
              input_ids=A  ,		spout=A  ,		max_new_tokens=20  ,		generation_config=A  ,		)

          a      =      tokenizer.decode(outputa[0] )
          a      =      tokenizer.decode(outputa[0] )
          a      =      tokenizer.batch_decode(A )

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







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



          '''simple docstring'''



          a      =      GPTSanJapaneseForConditionalGeneration.from_pretrained("Tanrei/GPTSAN-japanese" )
          a      =      GPTSanJapaneseTokenizer.from_pretrained("Tanrei/GPTSAN-japanese" )
          model.to(A )

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

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

          a      =      model.generate(
              input_ids=input_tok_a.input_ids.to(A )  ,		token_type_ids=input_tok_a.token_type_ids.to(A )  ,		max_new_tokens=20  ,		generation_config=A  ,		)

          a      =      model.generate(
              input_ids=input_tok_a.input_ids.to(A )  ,		token_type_ids=input_tok_a.token_type_ids.to(A )  ,		max_new_tokens=20  ,		generation_config=A  ,		)

          a      =      model.generate(
              input_ids=input_tok_a.input_ids.to(A )  ,		token_type_ids=input_tok_a.token_type_ids.to(A )  ,		attention_mask=input_tok_a.attention_mask.to(A )  ,		max_new_tokens=20  ,		generation_config=A  ,		)

          a      =      tokenizer.decode(outputa[0] )
          a      =      tokenizer.decode(outputa[0] )
          a      =      tokenizer.batch_decode(A )

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







from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFImageClassifierOutputWithNoAttention,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_resnet import ResNetConfig


lowercase__      :			int           =					logging.get_logger(__name__)

# General docstring
lowercase__      :			str           =					"ResNetConfig"

# Base docstring
lowercase__      :			Dict           =					"microsoft/resnet-50"
lowercase__      :			Dict           =					[1, 2_048, 7, 7]

# Image classification docstring
lowercase__      :			Optional[Any]           =					"microsoft/resnet-50"
lowercase__      :			str           =					"tiger cat"

lowercase__      :			Union[str, Any]           =					[
    "microsoft/resnet-50",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]




class 			a__       (					tf.keras.layers.Layer      ):







     def __init__(	self  ,		A  ,		A = 3  ,		A = 1  ,		A = "relu"  ,		**A )  ->    None:



          '''simple docstring'''



          super().__init__(**A )
          a      =      kernel_size // 2
          a      =      tf.keras.layers.ConvaD(
              A  ,		kernel_size=A  ,		strides=A  ,		padding="valid"  ,		use_bias=A  ,		name="convolution" )
          # Use same default momentum and epsilon as PyTorch equivalent
          a      =      tf.keras.layers.BatchNormalization(epsilon=1e-5  ,		momentum=0.9  ,		name="normalization" )
          a      =      ACTaFN[activation] if activation is not None else tf.keras.layers.Activation("linear" )







     def 			lowerCAmelCase_				(	self  ,		A )  ->    tf.Tensor:



          '''simple docstring'''



          a      =      a      =      (self.pad_value, self.pad_value)
          a      =      tf.pad(A  ,		[(0, 0), height_pad, width_pad, (0, 0)] )
          a      =      self.conv(A )
          return hidden_state







     def 			lowerCAmelCase_				(	self  ,		A  ,		A = False )  ->    tf.Tensor:



          '''simple docstring'''



          a      =      self.convolution(A )
          a      =      self.normalization(A  ,		training=A )
          a      =      self.activation(A )
          return hidden_state







class 			a__       (					tf.keras.layers.Layer      ):







     def __init__(	self  ,		A  ,		**A )  ->    None:



          '''simple docstring'''



          super().__init__(**A )
          a      =      TFResNetConvLayer(
              config.embedding_size  ,		kernel_size=7  ,		stride=2  ,		activation=config.hidden_act  ,		name="embedder"  ,		)
          a      =      tf.keras.layers.MaxPoolaD(pool_size=3  ,		strides=2  ,		padding="valid"  ,		name="pooler" )
          a      =      config.num_channels







     def 			lowerCAmelCase_				(	self  ,		A  ,		A = False )  ->    tf.Tensor:



          '''simple docstring'''



          a  ,      a  ,      a  ,      a      =      shape_list(A )
          if tf.executing_eagerly() and num_channels != self.num_channels:
               raise ValueError(
                   "Make sure that the channel dimension of the pixel values match with the one set in the configuration." )
          a      =      pixel_values
          a      =      self.embedder(A )
          a      =      tf.pad(A  ,		[[0, 0], [1, 1], [1, 1], [0, 0]] )
          a      =      self.pooler(A )
          return hidden_state







class 			a__       (					tf.keras.layers.Layer      ):







     def __init__(	self  ,		A  ,		A = 2  ,		**A )  ->    None:



          '''simple docstring'''



          super().__init__(**A )
          a      =      tf.keras.layers.ConvaD(
              A  ,		kernel_size=1  ,		strides=A  ,		use_bias=A  ,		name="convolution" )
          # Use same default momentum and epsilon as PyTorch equivalent
          a      =      tf.keras.layers.BatchNormalization(epsilon=1e-5  ,		momentum=0.9  ,		name="normalization" )







     def 			lowerCAmelCase_				(	self  ,		A  ,		A = False )  ->    tf.Tensor:



          '''simple docstring'''



          a      =      x
          a      =      self.convolution(A )
          a      =      self.normalization(A  ,		training=A )
          return hidden_state







class 			a__       (					tf.keras.layers.Layer      ):







     def __init__(	self  ,		A  ,		A  ,		A = 1  ,		A = "relu"  ,		**A )  ->    None:



          '''simple docstring'''



          super().__init__(**A )
          a      =      in_channels != out_channels or stride != 1
          a      =      TFResNetConvLayer(A  ,		stride=A  ,		name="layer.0" )
          a      =      TFResNetConvLayer(A  ,		activation=A  ,		name="layer.1" )
          a      =      (
              TFResNetShortCut(A  ,		stride=A  ,		name="shortcut" )
              if should_apply_shortcut
              else tf.keras.layers.Activation("linear"  ,		name="shortcut" )
          )
          a      =      ACTaFN[activation]







     def 			lowerCAmelCase_				(	self  ,		A  ,		A = False )  ->    tf.Tensor:



          '''simple docstring'''



          a      =      hidden_state
          a      =      self.conva(A  ,		training=A )
          a      =      self.conva(A  ,		training=A )
          a      =      self.shortcut(A  ,		training=A )
          hidden_state += residual
          a      =      self.activation(A )
          return hidden_state







class 			a__       (					tf.keras.layers.Layer      ):







     def __init__(	self  ,		A  ,		A  ,		A = 1  ,		A = "relu"  ,		A = 4  ,		**A  ,		)  ->    None:



          '''simple docstring'''



          super().__init__(**A )
          a      =      in_channels != out_channels or stride != 1
          a      =      out_channels // reduction
          a      =      TFResNetConvLayer(A  ,		kernel_size=1  ,		name="layer.0" )
          a      =      TFResNetConvLayer(A  ,		stride=A  ,		name="layer.1" )
          a      =      TFResNetConvLayer(A  ,		kernel_size=1  ,		activation=A  ,		name="layer.2" )
          a      =      (
              TFResNetShortCut(A  ,		stride=A  ,		name="shortcut" )
              if should_apply_shortcut
              else tf.keras.layers.Activation("linear"  ,		name="shortcut" )
          )
          a      =      ACTaFN[activation]







     def 			lowerCAmelCase_				(	self  ,		A  ,		A = False )  ->    tf.Tensor:



          '''simple docstring'''



          a      =      hidden_state
          a      =      self.conva(A  ,		training=A )
          a      =      self.conva(A  ,		training=A )
          a      =      self.conva(A  ,		training=A )
          a      =      self.shortcut(A  ,		training=A )
          hidden_state += residual
          a      =      self.activation(A )
          return hidden_state







class 			a__       (					tf.keras.layers.Layer      ):







     def __init__(	self  ,		A  ,		A  ,		A  ,		A = 2  ,		A = 2  ,		**A )  ->    None:



          '''simple docstring'''



          super().__init__(**A )

          a      =      TFResNetBottleNeckLayer if config.layer_type == "bottleneck" else TFResNetBasicLayer

          a      =      [layer(A  ,		A  ,		stride=A  ,		activation=config.hidden_act  ,		name="layers.0" )]
          layers += [
              layer(A  ,		A  ,		activation=config.hidden_act  ,		name=F'''layers.{i + 1}''' )
              for i in range(depth - 1 )
          ]
          a      =      layers







     def 			lowerCAmelCase_				(	self  ,		A  ,		A = False )  ->    tf.Tensor:



          '''simple docstring'''



          for layer in self.stage_layers:
               a      =      layer(A  ,		training=A )
          return hidden_state







class 			a__       (					tf.keras.layers.Layer      ):







     def __init__(	self  ,		A  ,		**A )  ->    None:



          '''simple docstring'''



          super().__init__(**A )
          # based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
          a      =      [
              TFResNetStage(
                  A  ,		config.embedding_size  ,		config.hidden_sizes[0]  ,		stride=2 if config.downsample_in_first_stage else 1  ,		depth=config.depths[0]  ,		name="stages.0"  ,		)
          ]
          for i, (in_channels, out_channels, depth) in enumerate(
              zip(config.hidden_sizes  ,		config.hidden_sizes[1:]  ,		config.depths[1:] ) ):
               self.stages.append(TFResNetStage(A  ,		A  ,		A  ,		depth=A  ,		name=F'''stages.{i + 1}''' ) )







     def 			lowerCAmelCase_				(	self  ,		A  ,		A = False  ,		A = True  ,		A = False  ,		)  ->    TFBaseModelOutputWithNoAttention:



          '''simple docstring'''



          a      =      () if output_hidden_states else None

          for stage_module in self.stages:
               if output_hidden_states:
                    a      =      hidden_states + (hidden_state,)

               a      =      stage_module(A  ,		training=A )

          if output_hidden_states:
               a      =      hidden_states + (hidden_state,)

          if not return_dict:
               return tuple(v for v in [hidden_state, hidden_states] if v is not None )

          return TFBaseModelOutputWithNoAttention(last_hidden_state=A  ,		hidden_states=A )







class 			a__       (					UpperCamelCase__      ):
     a		:			List[Any]							=	ResNetConfig
     a		:			str							=	"""resnet"""
     a		:			List[str]							=	"""pixel_values"""







     @property
     def 			lowerCAmelCase_				(	self )  ->    Union[str, Any]:



          '''simple docstring'''



          return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224)  ,		dtype=tf.floataa )}







lowercase__      :			Any           =					R"\n    This model is a TensorFlow\n    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n    regular TensorFlow Module and refer to the TensorFlow documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n"


lowercase__      :			Dict           =					R"\n    Args:\n        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConvNextImageProcessor.__call__`] for details.\n\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n"




@keras_serializable
class 			a__       (					tf.keras.layers.Layer      ):
     a		:			List[str]							=	ResNetConfig







     def __init__(	self  ,		A  ,		**A )  ->    None:



          '''simple docstring'''



          super().__init__(**A )
          a      =      config
          a      =      TFResNetEmbeddings(A  ,		name="embedder" )
          a      =      TFResNetEncoder(A  ,		name="encoder" )
          a      =      tf.keras.layers.GlobalAveragePoolingaD(keepdims=A )







     @unpack_inputs
     def 			lowerCAmelCase_				(	self  ,		A  ,		A = None  ,		A = None  ,		A = False  ,		)  ->    Union[Tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]:



          '''simple docstring'''



          a      =      (
              output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
          )
          a      =      return_dict if return_dict is not None else self.config.use_return_dict

          # TF 2.0 image layers can't use NCHW format when running on CPU.
          # We transpose to NHWC format and then transpose back after the full forward pass.
          # (batch_size, num_channels, height, width) -> (batch_size, height, width, num_channels)
          a      =      tf.transpose(A  ,		perm=[0, 2, 3, 1] )
          a      =      self.embedder(A  ,		training=A )

          a      =      self.encoder(
              A  ,		output_hidden_states=A  ,		return_dict=A  ,		training=A )

          a      =      encoder_outputs[0]

          a      =      self.pooler(A )

          # Transpose all the outputs to the NCHW format
          # (batch_size, height, width, num_channels) -> (batch_size, num_channels, height, width)
          a      =      tf.transpose(A  ,		(0, 3, 1, 2) )
          a      =      tf.transpose(A  ,		(0, 3, 1, 2) )
          a      =      ()
          for hidden_state in encoder_outputs[1:]:
               a      =      hidden_states + tuple(tf.transpose(A  ,		(0, 3, 1, 2) ) for h in hidden_state )

          if not return_dict:
               return (last_hidden_state, pooled_output) + hidden_states

          a      =      hidden_states if output_hidden_states else None

          return TFBaseModelOutputWithPoolingAndNoAttention(
              last_hidden_state=A  ,		pooler_output=A  ,		hidden_states=A  ,		)







@add_start_docstrings(
    """The bare ResNet model outputting raw features without any specific head on top."""   ,					UpperCamelCase__   ,					)
class 			a__       (					UpperCamelCase__      ):







     def __init__(	self  ,		A  ,		**A )  ->    None:



          '''simple docstring'''



          super().__init__(A  ,		**A )
          a      =      TFResNetMainLayer(config=A  ,		name="resnet" )







     @add_start_docstrings_to_model_forward(A )
     @add_code_sample_docstrings(
         checkpoint=_CHECKPOINT_FOR_DOC  ,		output_type=A  ,		config_class=_CONFIG_FOR_DOC  ,		modality="vision"  ,		expected_output=_EXPECTED_OUTPUT_SHAPE  ,		)
     @unpack_inputs
     def 			lowerCAmelCase_				(	self  ,		A  ,		A = None  ,		A = None  ,		A = False  ,		)  ->    Union[Tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]:



          '''simple docstring'''



          a      =      (
              output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
          )
          a      =      return_dict if return_dict is not None else self.config.use_return_dict

          a      =      self.resnet(
              pixel_values=A  ,		output_hidden_states=A  ,		return_dict=A  ,		training=A  ,		)
          return resnet_outputs







@add_start_docstrings(
    """
    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """   ,					UpperCamelCase__   ,					)
class 			a__       (					UpperCamelCase__   ,					UpperCamelCase__      ):







     def __init__(	self  ,		A  ,		**A )  ->    None:



          '''simple docstring'''



          super().__init__(A  ,		**A )
          a      =      config.num_labels
          a      =      TFResNetMainLayer(A  ,		name="resnet" )
          # classification head
          a      =      (
              tf.keras.layers.Dense(config.num_labels  ,		name="classifier.1" )
              if config.num_labels > 0
              else tf.keras.layers.Activation("linear"  ,		name="classifier.1" )
          )







     def 			lowerCAmelCase_				(	self  ,		A )  ->    tf.Tensor:



          '''simple docstring'''



          a      =      tf.keras.layers.Flatten()(A )
          a      =      self.classifier_layer(A )
          return logits







     @add_start_docstrings_to_model_forward(A )
     @add_code_sample_docstrings(
         checkpoint=_IMAGE_CLASS_CHECKPOINT  ,		output_type=A  ,		config_class=_CONFIG_FOR_DOC  ,		expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT  ,		)
     @unpack_inputs
     def 			lowerCAmelCase_				(	self  ,		A = None  ,		A = None  ,		A = None  ,		A = None  ,		A = False  ,		)  ->    Union[Tuple[tf.Tensor], TFImageClassifierOutputWithNoAttention]:



          '''simple docstring'''



          a      =      return_dict if return_dict is not None else self.config.use_return_dict

          a      =      self.resnet(
              A  ,		output_hidden_states=A  ,		return_dict=A  ,		training=A )

          a      =      outputs.pooler_output if return_dict else outputs[1]

          a      =      self.classifier(A )

          a      =      None if labels is None else self.hf_compute_loss(A  ,		A )

          if not return_dict:
               a      =      (logits,) + outputs[2:]
               return (loss,) + output if loss is not None else output

          return TFImageClassifierOutputWithNoAttention(loss=A  ,		logits=A  ,		hidden_states=outputs.hidden_states )

