






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






						def __init__(		self   ,  _UpperCamelCase   ,  _UpperCamelCase=3_60_00   ,  _UpperCamelCase=13   ,  _UpperCamelCase=7   ,  _UpperCamelCase=True   ,  _UpperCamelCase=32   ,  _UpperCamelCase=42   ,  _UpperCamelCase=5   ,  _UpperCamelCase=2   ,  _UpperCamelCase=4   ,  _UpperCamelCase=2   ,  _UpperCamelCase=32   ,  _UpperCamelCase=80   ,  _UpperCamelCase=33   ,  _UpperCamelCase=0.0   ,  _UpperCamelCase=1E-6   ,  _UpperCamelCase=1_00   ,  _UpperCamelCase=0.0   ,  ):






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






						def 					UpperCamelCase__					(		self      ):






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






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  ids_tensor([self.batch_size, self.seq_length]   ,  self.vocab_size      )

												lowerCAmelCase__			  =  self.get_config()

												return (config, input_ids)






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  ids_tensor([self.batch_size, self.seq_length]   ,  self.vocab_size      )

												lowerCAmelCase__			  =  self.get_config()

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






						def 					UpperCamelCase__					(		self      ):






												"""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 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase   ,  ):






												"""simple docstring"""
												lowerCAmelCase__			  =  GPTSanJapaneseForConditionalGeneration(config=_UpperCamelCase      )
												model.to(_UpperCamelCase      )
												model.eval()
												lowerCAmelCase__			  =  model(
												    input_ids=_UpperCamelCase   ,  )
												self.parent.assertIsNotNone(_UpperCamelCase      )




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






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase      ):






												"""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 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  GPTSanJapaneseTester(self      )
												lowerCAmelCase__			  =  ConfigTester(self   ,  config_class=_UpperCamelCase   ,  d_model=37      )






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												GPTSanJapaneseConfig()






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  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 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												super().test_model_parallelism()







						@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'      )
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												pass




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






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  GPTSanJapaneseTester(self      )
												lowerCAmelCase__			  =  ConfigTester(self   ,  config_class=_UpperCamelCase   ,  d_model=37      )






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												GPTSanJapaneseConfig()






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  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 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												super().test_model_parallelism()






						@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'      )
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												pass






						@slow
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  GPTSanJapaneseForConditionalGeneration.from_pretrained('Tanrei/GPTSAN-japanese'      )
												lowerCAmelCase__			  =  GPTSanJapaneseTokenizer.from_pretrained('Tanrei/GPTSAN-japanese'      )
												lowerCAmelCase__			  =  tokenizer.encode('武田信玄は'   ,  return_tensors='pt'      )
												lowerCAmelCase__			  =  model(_UpperCamelCase      )
												lowerCAmelCase__			  =  outputs.logits.detach().cpu().numpy()
												# Output of original model created with mesh-tensoflow
												lowerCAmelCase__			  =  [
												    # fmt: off
												    [-12.0_37_83_98_89_52_63_67, -12.4_33_06_15_99_73_14_45, -14.3_33_84_03_70_17_82_23, -12.4_50_34_59_93_04_19_92, -11.1_66_13_76_95_31_25,
												    -11.9_30_13_76_34_27_73_44, -10.6_59_74_04_47_99_80_47, -12.9_09_57_45_08_66_69_92, -13.2_41_04_30_90_82_03_12, -13.3_98_57_95_97_47_31_45,
												    -11.1_07_52_48_71_82_61_72, -12.3_68_59_41_69_61_67, -22.97_94_31_15_23_43_75, -10.4_81_06_76_57_47_07_03, -12.4_84_03_07_23_57_17_77,
												    -12.8_07_36_06_49_10_88_87, -14.7_69_70_00_50_35_40_04, -12.2_33_57_96_35_62_01_17, -13.4_28_14_54_08_63_03_71, -22.6_24_17_79_32_73_92_58],
												    [-7.5_11_14_98_83_27_02_64, -8.2_81_85_17_68_49_36_52, -7.9_43_12_71_55_30_39_55, -7.55_02_13_33_69_44_58, -6.49_86_99_66_50_69_58,
												    -7.5_86_79_62_83_72_19_24, -6.9_78_08_59_94_72_04_59, -7.8_39_14_51_83_56_32_32, -8.21_96_41_68_54_85_84, -8.6_95_09_12_47_55_85_94,
												    -6.7_06_91_06_10_19_89_75, -6.65_85_79_82_63_54_98_05, -19.5_65_69_86_23_65_72_27, -5.3_53_84_27_35_29_05_27, -8.3_50_68_60_73_30_32_23,
												    -8.0_39_38_86_56_61_62_11, -10.8_56_56_92_90_16_11_33, -7.75_15_44_47_55_55_42, -8.8_19_02_21_78_64_99_02, -19.51_53_27_45_36_13_28],
												    [-9.73_06_62_34_58_86_23, -10.2_23_92_27_29_49_21_88, -9.9_32_98_14_91_08_88_67, -11.8_57_83_67_23_32_76_37, -7.6_62_62_62_66_47_94_92,
												    -11.13_52_91_09_95_48_34, -7.7_65_09_76_18_10_30_27, -11.4_72_92_32_78_80_85_94, -9.5_43_14_99_48_12_01_17, -11.9_05_63_39_26_39_16_02,
												    -9.3_66_16_42_07_45_84_96, -11.5_73_42_81_53_99_17, -23.6_99_00_32_19_60_44_92, -9.4_29_59_02_25_21_97_27, -10.42_83_92_41_02_78_32,
												    -10.5_85_24_03_64_07_47_07, -10.94_77_19_57_39_74_61, -11.0_95_41_60_69_03_07_62, -10.3_90_24_06_69_25_04_88, -23.7_69_37_29_40_06_34_77],
												    [-9.7_28_26_57_62_32_91_02, -9.8_59_71_26_00_70_80_08, -10.09_72_92_90_00_85_45, -9.6_78_52_21_09_98_53_52, -6.8_79_51_99_39_42_26_07,
												    -9.68_48_75_48_82_81_25, -4.28_03_42_57_88_87_93_95, -10.0_18_91_42_22_71_72_85, -9.3_08_44_59_30_48_09_57, -10.63_39_45_46_50_87_89,
												    -8.0_83_64_67_74_29_19_92, -9.06_30_14_98_41_30_86, -21.9_04_26_63_57_42_18_75, -8.90_16_08_46_71_02_05, -8.8_41_87_60_29_96_82_62,
												    -11.8_56_71_99_70_70_31_25, -12.0_79_39_81_55_21_24_02, -11.2_33_75_32_04_34_57_03, -10.1_77_33_86_00_15_86_91, -21.87_25_66_22_31_44_53],
												    [-9.6_69_76_45_18_73_77_93, -9.6_14_19_86_84_69_23_83, -9.8_14_51_03_45_45_89_84, -9.9_96_50_19_22_60_74_22, -11.3_75_69_04_60_20_50_78,
												    -10.1_13_40_52_27_66_11_33, -10.5_46_86_73_70_60_54_69, -10.04_36_90_68_14_57_52, -10.9_07_80_92_57_50_73_24, -10.5_04_21_61_94_15_28_32,
												    -11.1_29_19_90_28_01_51_37, -10.1_51_12_40_00_54_93_16, -21.96_58_67_99_62_15_82, -9.0_86_34_94_87_30_46_88, -11.7_30_33_90_50_29_29_69,
												    -10.4_60_66_76_10_16_84_57, -10.2_98_04_99_26_75_78_12, -10.7_84_14_82_16_24_75_59, -10.8_40_69_34_73_81_59_18, -22.03_15_28_47_29_00_39],
												    # fmt: on
												]
												lowerCAmelCase__			  =  np.array(_UpperCamelCase      ).flatten()
												lowerCAmelCase__			  =  output_logits[0, :, :20].flatten()

												def check(_UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase=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 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  GPTSanJapaneseForConditionalGeneration.from_pretrained('Tanrei/GPTSAN-japanese'      )
												lowerCAmelCase__			  =  GPTSanJapaneseTokenizer.from_pretrained('Tanrei/GPTSAN-japanese'      )
												model.to(_UpperCamelCase      )

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

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

												lowerCAmelCase__			  =  'left'
												lowerCAmelCase__			  =  tokenizer(_UpperCamelCase   ,  return_tensors='pt'   ,  padding=_UpperCamelCase      )
												lowerCAmelCase__			  =  inputs['input_ids'].to(_UpperCamelCase      )

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

												lowerCAmelCase__			  =  model.generate(
												    input_ids=_UpperCamelCase   ,  attention_mask=inputs['attention_mask'].to(_UpperCamelCase      )   ,  max_new_tokens=3   ,  generation_config=_UpperCamelCase   ,  )

												lowerCAmelCase__			  =  tokenizer(sentences[0]   ,  return_tensors='pt'      ).input_ids.to(_UpperCamelCase      )
												lowerCAmelCase__			  =  model.generate(
												    input_ids=_UpperCamelCase   ,  max_new_tokens=3   ,  generation_config=_UpperCamelCase      )

												lowerCAmelCase__			  =  tokenizer(sentences[1]   ,  return_tensors='pt'      ).input_ids.to(_UpperCamelCase      )
												lowerCAmelCase__			  =  model.generate(input_ids=_UpperCamelCase   ,  max_new_tokens=3   ,  generation_config=_UpperCamelCase      )

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

												lowerCAmelCase__			  =  tokenizer.batch_decode(_UpperCamelCase   ,  skip_special_tokens=_UpperCamelCase      )
												lowerCAmelCase__			  =  tokenizer.decode(output_non_padded[0]   ,  skip_special_tokens=_UpperCamelCase      )
												lowerCAmelCase__			  =  tokenizer.decode(output_padded[0]   ,  skip_special_tokens=_UpperCamelCase      )

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






						@tooslow
						def 					UpperCamelCase__					(		self      ):






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






						@slow
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  GPTSanJapaneseForConditionalGeneration.from_pretrained('Tanrei/GPTSAN-japanese'      )
												lowerCAmelCase__			  =  GPTSanJapaneseTokenizer.from_pretrained('Tanrei/GPTSAN-japanese'      )
												model.to(_UpperCamelCase      )

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

												lowerCAmelCase__			  =  '武田信玄は、'
												lowerCAmelCase__			  =  tokenizer(_UpperCamelCase   ,  return_tensors='pt'      ).input_ids.to(_UpperCamelCase      )
												lowerCAmelCase__			  =  tokenizer([input_text, input_text]   ,  return_tensors='pt'      ).input_ids.to(_UpperCamelCase      )

												# spout from uniform and one-hot
												lowerCAmelCase__			  =  [
												    # fmt: off
												    [0.87_88_22_08, 0.38_42_63_96, 0.33_22_02_48, 0.43_89_04_06, 0.16_56_22_52,
												    0.04_80_39_85, 0.21_15_72  , 0.23_18_84_73, 0.37_15_30_68, 0.7_83_63_77 ,
												    0.02_16_01_72, 0.38_76_17_19, 0.75_29_07_72, 0.90_19_88_57, 0.34_36_57_77,
												    0.64_16_81_69, 0.44_31_84_71, 0.14_57_57_46, 0.92_56_28_81, 0.40_81_21_48,
												    0.29_01_91_22, 0.88_86_15_99, 0.65_52_48_46, 0.43_56_34_56, 0.38_17_71_87,
												    0.70_83_29_65, 0.81_52_78_92, 0.68_83_28_12, 0.38_83_31_92, 0.4_56_15_22 ,
												    0.14_82_88_17, 0.47_24_82_13, 0.54_35_73_35, 0.82_00_95_66, 0.1_33_88_84 ,
												    0.02_75_54_17, 0.19_76_46_77, 0.2_42_20_84 , 0.04_75_76_74, 0.65_40_96_06,
												    0.0_82_45_89 , 0.03_30_43_83, 0.94_38_76_89, 0.98_76_45_09, 0.82_43_39_01,
												    0.27_64_67_41, 0.64_90_74_93, 0.76_00_94_06, 0.30_08_79_15, 0.17_90_46_89,
												    0.41_60_17_14, 0.67_04_63_98, 0.10_42_28_22, 0.08_44_73_74, 0.07_35_43_44,
												    0.61_42_35_65, 0.70_28_48_66, 0.7_53_23_33 , 0.1_97_20_38 , 0.29_57_56_59,
												    0.90_58_38_86, 0.29_26_53_07, 0.50_00_01_75, 0.70_40_76_55, 0.88_93_63  ,
												    0.81_90_44_18, 0.66_82_91_28, 0.64_46_88_15, 0.56_56_37_23, 0.85_60_18_75,
												    0.94_92_46_72, 0.00_16_67_62, 0.25_22_06_43, 0.74_54_02_19, 0.67_99_32_47,
												    0.1_54_96_75 , 0.39_38_53_52, 0.92_15_36_07, 0.63_74_59_31, 0.27_75_90_43,
												    0.84_70_22_95, 0.65_90_42_71, 0.58_67_66_14, 0.8_66_69_36 , 0.39_60_74_38,
												    0.79_95_49_83, 0.42_22_06_97, 0.39_65_03_81, 0.7_84_98_64 , 0.56_15_02_01,
												    0.15_67_89_25, 0.14_74_60_32, 0.34_54_21_14, 0.47_02_67_83, 0.11_95_64_89,
												    0.25_42_14_35, 0.33_78_89_01, 0.68_93_48_42, 0.36_42_46_85, 0.71_73_78_98,
												    0.38_98_34_49, 0.94_39_37_79, 0.39_57_55_88, 0.36_61_65_53, 0.87_10_46_65,
												    0.64_63_02_03, 0.22_51_69_05, 0.88_27_08_04, 0.15_03_13_38, 0.75_14_43_45,
												    0.46_45_90_25, 0.85_39_64_54, 0.86_35_56_43, 0.65_13_98_51, 0.70_26_60_61,
												    0.30_24_13_89, 0.81_05_64_97, 0.88_86_59_69, 0.38_77_38_07, 0.70_63_58_49,
												    0.90_71_84_59, 0.43_24_57_89, 0.28_00_06_54, 0.45_93_55_62, 0.08_77_35_19,
												    0.9_55_21_51 , 0.93_90_15_11, 0.22_48_92_88], # 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
												]

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

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

												lowerCAmelCase__			  =  model.generate(
												    input_ids=_UpperCamelCase   ,  spout=_UpperCamelCase   ,  max_new_tokens=20   ,  generation_config=_UpperCamelCase   ,  )

												lowerCAmelCase__			  =  tokenizer.decode(outputa[0]      )
												lowerCAmelCase__			  =  tokenizer.decode(outputa[0]      )
												lowerCAmelCase__			  =  tokenizer.batch_decode(_UpperCamelCase      )

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







						@slow
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  GPTSanJapaneseForConditionalGeneration.from_pretrained('Tanrei/GPTSAN-japanese'      )
												lowerCAmelCase__			  =  GPTSanJapaneseTokenizer.from_pretrained('Tanrei/GPTSAN-japanese'      )
												model.to(_UpperCamelCase      )

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

												lowerCAmelCase__			  =  '武田信玄'
												lowerCAmelCase__			  =  '織田信長'
												lowerCAmelCase__			  =  'は、'
												lowerCAmelCase__			  =  'が、'
												lowerCAmelCase__			  =  tokenizer(_UpperCamelCase   ,  prefix_text=_UpperCamelCase   ,  return_tensors='pt'      )
												lowerCAmelCase__			  =  tokenizer(_UpperCamelCase   ,  prefix_text=_UpperCamelCase   ,  return_tensors='pt'      )
												lowerCAmelCase__			  =  tokenizer([[prefix_text_a, input_text_a], [prefix_text_a, input_text_a]]   ,  return_tensors='pt'      )

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

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

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

												lowerCAmelCase__			  =  tokenizer.decode(outputa[0]      )
												lowerCAmelCase__			  =  tokenizer.decode(outputa[0]      )
												lowerCAmelCase__			  =  tokenizer.batch_decode(_UpperCamelCase      )

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













import argparse
import collections

import jax
import jax.numpy as jnp
import torch
import torch.nn as nn
from clip.model import CLIP
from flax.training import checkpoints
from huggingface_hub import Repository

from transformers import (
    CLIPTokenizer,
    OwlViTConfig,
    OwlViTForObjectDetection,
    OwlViTImageProcessor,
    OwlViTModel,
    OwlViTProcessor,
)


__snake_case			:			int			   =   {
    """vit_b32""": {
        """embed_dim""": 5_12,
        """image_resolution""": 7_68,
        """context_length""": 16,
        """vocab_size""": 4_94_08,
        """vision_layers""": 12,
        """vision_width""": 7_68,
        """vision_patch_size""": 32,
        """transformer_width""": 5_12,
        """transformer_heads""": 8,
        """transformer_layers""": 12,
    },
    """vit_b16""": {
        """embed_dim""": 5_12,
        """image_resolution""": 7_68,
        """context_length""": 16,
        """vocab_size""": 4_94_08,
        """vision_layers""": 12,
        """vision_width""": 7_68,
        """vision_patch_size""": 16,
        """transformer_width""": 5_12,
        """transformer_heads""": 8,
        """transformer_layers""": 12,
    },
    """vit_l14""": {
        """embed_dim""": 7_68,
        """image_resolution""": 8_40,
        """context_length""": 16,
        """vocab_size""": 4_94_08,
        """vision_layers""": 24,
        """vision_width""": 10_24,
        """vision_patch_size""": 14,
        """transformer_width""": 7_68,
        """transformer_heads""": 12,
        """transformer_layers""": 12,
    },
}







def 		_UpperCamelCase      (			UpperCamelCase_				:    Dict			,				UpperCamelCase_				:    Any=""			,				UpperCamelCase_				:    List[str]="/"						)     ->      List[str]:

						"""simple docstring"""



						lowerCAmelCase__			  =  []

						for k, v in params.items():
												lowerCAmelCase__			  =  parent_key + sep + k if parent_key else k

												if isinstance(UpperCamelCase_			,				collections.MutableMapping						):
																		items.extend(flatten_nested_dict(UpperCamelCase_			,				UpperCamelCase_			,				sep=UpperCamelCase_						).items()						)
												else:
																		items.append((new_key, v)						)
						return dict(UpperCamelCase_						)







def 		_UpperCamelCase      (			UpperCamelCase_				:    Tuple						)     ->      Optional[int]:

						"""simple docstring"""



						return jax.tree_util.tree_map(lambda UpperCamelCase_						: x.astype(jnp.floataa						) if x.dtype == jnp.bfloataa else x			,				UpperCamelCase_						)







def 		_UpperCamelCase      (			UpperCamelCase_				:    Optional[int]			,				UpperCamelCase_				:    Union[str, Any]						)     ->      List[Any]:

						"""simple docstring"""



						lowerCAmelCase__  ,				lowerCAmelCase__  ,				lowerCAmelCase__			  =  pt_attn_layer.in_proj_weight.chunk(3			,				dim=0						)
						lowerCAmelCase__  ,				lowerCAmelCase__  ,				lowerCAmelCase__			  =  pt_attn_layer.in_proj_bias.chunk(3			,				dim=0						)

						lowerCAmelCase__			  =  pt_attn_layer.out_proj.weight
						lowerCAmelCase__			  =  pt_attn_layer.out_proj.bias

						lowerCAmelCase__			  =  q_proj
						lowerCAmelCase__			  =  q_proj_bias

						lowerCAmelCase__			  =  k_proj
						lowerCAmelCase__			  =  k_proj_bias

						lowerCAmelCase__			  =  v_proj
						lowerCAmelCase__			  =  v_proj_bias

						lowerCAmelCase__			  =  out_proj_weights
						lowerCAmelCase__			  =  out_proj_bias







def 		_UpperCamelCase      (			UpperCamelCase_				:    Tuple			,				UpperCamelCase_				:    Union[str, Any]						)     ->      Union[str, Any]:

						"""simple docstring"""



						copy_linear(hf_mlp.fca			,				pt_mlp.c_fc						)
						copy_linear(hf_mlp.fca			,				pt_mlp.c_proj						)







def 		_UpperCamelCase      (			UpperCamelCase_				:    Union[str, Any]			,				UpperCamelCase_				:    Tuple						)     ->      Optional[Any]:

						"""simple docstring"""



						lowerCAmelCase__			  =  pt_linear.weight
						lowerCAmelCase__			  =  pt_linear.bias







def 		_UpperCamelCase      (			UpperCamelCase_				:    Any			,				UpperCamelCase_				:    Optional[Any]						)     ->      Tuple:

						"""simple docstring"""



						copy_linear(hf_layer.layer_norma			,				pt_layer.ln_a						)
						copy_linear(hf_layer.layer_norma			,				pt_layer.ln_a						)

						# copy MLP
						copy_mlp(hf_layer.mlp			,				pt_layer.mlp						)

						# copy attn
						copy_attn_layer(hf_layer.self_attn			,				pt_layer.attn						)







def 		_UpperCamelCase      (			UpperCamelCase_				:    Optional[int]			,				UpperCamelCase_				:    List[str]						)     ->      Optional[int]:

						"""simple docstring"""



						for hf_layer, pt_layer in zip(UpperCamelCase_			,				UpperCamelCase_						):
												copy_layer(UpperCamelCase_			,				UpperCamelCase_						)







def 		_UpperCamelCase      (			UpperCamelCase_				:    Dict			,				UpperCamelCase_				:    Union[str, Any]						)     ->      Optional[Any]:

						"""simple docstring"""



						lowerCAmelCase__			  =  pt_model.token_embedding.weight
						lowerCAmelCase__			  =  pt_model.positional_embedding

						# copy layer norm
						copy_linear(hf_encoder.final_layer_norm			,				pt_model.ln_final						)

						# copy hidden layers
						copy_layers(hf_encoder.encoder.layers			,				pt_model.transformer.resblocks						)







def 		_UpperCamelCase      (			UpperCamelCase_				:    Optional[int]			,				UpperCamelCase_				:    Dict						)     ->      Union[str, Any]:

						"""simple docstring"""



						lowerCAmelCase__			  =  pt_model.text_projection.data.T

						# copy text encoder
						copy_encoder(hf_model.text_model			,				UpperCamelCase_						)







def 		_UpperCamelCase      (			UpperCamelCase_				:    Any			,				UpperCamelCase_				:    str						)     ->      Tuple:

						"""simple docstring"""



						lowerCAmelCase__			  =  pt_model.visual.proj.data.T

						# copy layer norms
						copy_linear(hf_model.vision_model.pre_layernorm			,				pt_model.visual.ln_pre						)
						copy_linear(hf_model.vision_model.post_layernorm			,				pt_model.visual.ln_post						)

						# copy embeds
						lowerCAmelCase__			  =  pt_model.visual.conva.weight.data
						lowerCAmelCase__			  =  pt_model.visual.class_embedding
						lowerCAmelCase__			  =  pt_model.visual.positional_embedding.data

						# copy encoder
						copy_layers(hf_model.vision_model.encoder.layers			,				pt_model.visual.transformer.resblocks						)







def 		_UpperCamelCase      (			UpperCamelCase_				:    List[Any]			,				UpperCamelCase_				:    Any						)     ->      List[str]:

						"""simple docstring"""



						lowerCAmelCase__			  =  flatten_nested_dict(flax_params['backbone']['merged_class_token']						)

						lowerCAmelCase__			  =  torch.from_numpy(flax_class_token_params['scale']						)
						lowerCAmelCase__			  =  torch.from_numpy(flax_class_token_params['bias']						)
						lowerCAmelCase__			  =  nn.Parameter(UpperCamelCase_						)
						lowerCAmelCase__			  =  nn.Parameter(UpperCamelCase_						)







def 		_UpperCamelCase      (			UpperCamelCase_				:    str			,				UpperCamelCase_				:    Union[str, Any]						)     ->      Union[str, Any]:

						"""simple docstring"""



						lowerCAmelCase__			  =  hf_model.state_dict()
						lowerCAmelCase__			  =  {}

						# Rename class prediction head flax params to pytorch HF
						lowerCAmelCase__			  =  flatten_nested_dict(flax_params['class_head']						)

						for flax_key, v in flax_class_params.items():
												lowerCAmelCase__			  =  flax_key.replace('/'			,				'.'						)
												lowerCAmelCase__			  =  torch_key.replace('.kernel'			,				'.weight'						)
												lowerCAmelCase__			  =  torch_key.replace('Dense_0'			,				'dense0'						)
												lowerCAmelCase__			  =  'class_head.' + torch_key

												if "weight" in torch_key and v.ndim == 2:
																		lowerCAmelCase__			  =  v.T

												lowerCAmelCase__			  =  nn.Parameter(torch.from_numpy(UpperCamelCase_						)						)

						# Rename box prediction box flax params to pytorch HF
						lowerCAmelCase__			  =  flatten_nested_dict(flax_params['obj_box_head']						)

						for flax_key, v in flax_box_params.items():
												lowerCAmelCase__			  =  flax_key.replace('/'			,				'.'						)
												lowerCAmelCase__			  =  torch_key.replace('.kernel'			,				'.weight'						)
												lowerCAmelCase__			  =  torch_key.replace('_'			,				''						).lower()
												lowerCAmelCase__			  =  'box_head.' + torch_key

												if "weight" in torch_key and v.ndim == 2:
																		lowerCAmelCase__			  =  v.T

												lowerCAmelCase__			  =  nn.Parameter(torch.from_numpy(UpperCamelCase_						)						)

						# Copy flax params to PyTorch params
						for name, param in new_params.items():
												if name in pt_params.keys():
																		pt_params[name].copy_(UpperCamelCase_						)







def 		_UpperCamelCase      (			UpperCamelCase_				:    Tuple			,				UpperCamelCase_				:    List[str]						)     ->      str:

						"""simple docstring"""



						for k, v in flax_attn_params.items():
												if k.startswith('transformer'						):
																		lowerCAmelCase__			  =  k.replace('transformer.resblocks'			,				'text_model.encoder.layers'						)
												else:
																		lowerCAmelCase__			  =  k.replace('visual.transformer.resblocks'			,				'vision_model.encoder.layers'						)

												lowerCAmelCase__			  =  torch_key.replace('attn'			,				'self_attn'						)
												lowerCAmelCase__			  =  torch_key.replace('key'			,				'k_proj'						)
												lowerCAmelCase__			  =  torch_key.replace('value'			,				'v_proj'						)
												lowerCAmelCase__			  =  torch_key.replace('query'			,				'q_proj'						)
												lowerCAmelCase__			  =  torch_key.replace('out'			,				'out_proj'						)

												if "bias" in torch_key and v.ndim == 2:
																		lowerCAmelCase__			  =  v.shape[0] * v.shape[1]
																		lowerCAmelCase__			  =  v.reshape(UpperCamelCase_						)

												if "weight" in torch_key and "out" in torch_key:
																		lowerCAmelCase__			  =  (v.shape[0] * v.shape[1], v.shape[2])
																		lowerCAmelCase__			  =  v.reshape(UpperCamelCase_						).T

												if "weight" in torch_key and "out" not in torch_key:
																		lowerCAmelCase__			  =  (v.shape[0], v.shape[1] * v.shape[2])
																		lowerCAmelCase__			  =  v.reshape(UpperCamelCase_						).T

												# Copy flax CLIP attn params to HF PyTorch params
												lowerCAmelCase__			  =  torch.from_numpy(UpperCamelCase_						)
												hf_backbone.state_dict()[torch_key].copy_(UpperCamelCase_						)







def 		_UpperCamelCase      (			UpperCamelCase_				:    Any						)     ->      Dict:

						"""simple docstring"""



						lowerCAmelCase__			  =  {}
						lowerCAmelCase__			  =  []

						for k, v in params.items():
												if "attn." in k:
																		lowerCAmelCase__			  =  k[: k.rindex('attn.'						) + 5]
																		if base in processed_attn_layers:
																								continue

																		processed_attn_layers.append(UpperCamelCase_						)
																		lowerCAmelCase__			  =  params[base + 'out.weight'].shape[-1]
																		lowerCAmelCase__			  =  params[base + 'out.weight'].reshape(UpperCamelCase_			,				UpperCamelCase_						).T
																		lowerCAmelCase__			  =  params[base + 'out.bias']
												else:
																		lowerCAmelCase__			  =  v
						return new_params







def 		_UpperCamelCase      (			UpperCamelCase_				:    List[Any]			,				UpperCamelCase_				:    int						)     ->      Optional[Any]:

						"""simple docstring"""



						lowerCAmelCase__			  =  CLIP(**UpperCamelCase_						)
						torch_model.eval()
						lowerCAmelCase__			  =  torch_model.state_dict()

						lowerCAmelCase__			  =  flatten_nested_dict(flax_params['backbone']['clip']						)
						lowerCAmelCase__			  =  {}

						for flax_key, v in flax_clip_params.items():
												lowerCAmelCase__			  =  flax_key.replace('/'			,				'.'						)
												lowerCAmelCase__			  =  torch_key.replace('text.token_embedding.embedding'			,				'token_embedding.kernel'						)

												if (
												    torch_key.startswith('text.transformer'						)
												    or torch_key.startswith('text.text_projection'						)
												    or torch_key.startswith('text.ln_final'						)
												    or torch_key.startswith('text.positional_embedding'						)
												):
																		lowerCAmelCase__			  =  torch_key[5:]

												lowerCAmelCase__			  =  torch_key.replace('text_projection.kernel'			,				'text_projection'						)
												lowerCAmelCase__			  =  torch_key.replace('visual.proj.kernel'			,				'visual.proj'						)
												lowerCAmelCase__			  =  torch_key.replace('.scale'			,				'.weight'						)
												lowerCAmelCase__			  =  torch_key.replace('.kernel'			,				'.weight'						)

												if "conv" in torch_key or "downsample.0.weight" in torch_key:
																		lowerCAmelCase__			  =  v.transpose(3			,				2			,				0			,				1						)

												elif "weight" in torch_key and v.ndim == 2 and "embedding" not in torch_key:
																		# Fully connected layers are transposed, embeddings are not
																		lowerCAmelCase__			  =  v.T

												lowerCAmelCase__			  =  v

						lowerCAmelCase__			  =  _convert_attn_layers(UpperCamelCase_						)
						new_torch_params.update(UpperCamelCase_						)
						lowerCAmelCase__			  =  {}

						# Copy flax CLIP backbone params to PyTorch params
						for name, param in new_torch_params.items():
												if name in torch_clip_params.keys():
																		lowerCAmelCase__			  =  torch.from_numpy(new_torch_params[name]						)
																		torch_clip_params[name].copy_(UpperCamelCase_						)
												else:
																		lowerCAmelCase__			  =  param

						return torch_clip_params, torch_model, attn_params







@torch.no_grad()
def 		_UpperCamelCase      (			UpperCamelCase_				:    Any			,				UpperCamelCase_				:    Union[str, Any]			,				UpperCamelCase_				:    Dict			,				UpperCamelCase_				:    List[Any]			,				UpperCamelCase_				:    List[Any]=None						)     ->      Any:

						"""simple docstring"""



						lowerCAmelCase__			  =  Repository(UpperCamelCase_			,				clone_from=F"google/{pytorch_dump_folder_path}"						)
						repo.git_pull()

						if config_path is not None:
												lowerCAmelCase__			  =  OwlViTConfig.from_pretrained(UpperCamelCase_						)
						else:
												lowerCAmelCase__			  =  OwlViTConfig()

						lowerCAmelCase__			  =  OwlViTModel(UpperCamelCase_						).eval()
						lowerCAmelCase__			  =  OwlViTForObjectDetection(UpperCamelCase_						).eval()

						copy_text_model_and_projection(UpperCamelCase_			,				UpperCamelCase_						)
						copy_vision_model_and_projection(UpperCamelCase_			,				UpperCamelCase_						)
						lowerCAmelCase__			  =  pt_backbone.logit_scale
						copy_flax_attn_params(UpperCamelCase_			,				UpperCamelCase_						)

						lowerCAmelCase__			  =  hf_backbone
						copy_class_merge_token(UpperCamelCase_			,				UpperCamelCase_						)
						copy_class_box_heads(UpperCamelCase_			,				UpperCamelCase_						)

						# Save HF model
						hf_model.save_pretrained(repo.local_dir						)

						# Initialize image processor
						lowerCAmelCase__			  =  OwlViTImageProcessor(
						    size=config.vision_config.image_size			,				crop_size=config.vision_config.image_size						)
						# Initialize tokenizer
						lowerCAmelCase__			  =  CLIPTokenizer.from_pretrained('openai/clip-vit-base-patch32'			,				pad_token='!'			,				model_max_length=16						)

						# Initialize processor
						lowerCAmelCase__			  =  OwlViTProcessor(image_processor=UpperCamelCase_			,				tokenizer=UpperCamelCase_						)
						image_processor.save_pretrained(repo.local_dir						)
						processor.save_pretrained(repo.local_dir						)

						repo.git_add()
						repo.git_commit('Upload model and processor'						)
						repo.git_push()


if __name__ == "__main__":
					__snake_case			:			Union[str, Any]			   =   argparse.ArgumentParser()
					# Required parameters
					parser.add_argument(
					    """--owlvit_version""",
					    default=None,
					    type=str,
					    required=True,
					    help="""OWL-ViT model name [clip_b16, clip_b32, clip_l14].""",
					)
					parser.add_argument(
					    """--owlvit_checkpoint""", default=None, type=str, required=True, help="""Path to flax model checkpoint."""
					)
					parser.add_argument("""--hf_config""", default=None, type=str, required=True, help="""Path to HF model config.""")
					parser.add_argument(
					    """--pytorch_dump_folder_path""", default="""hf_model""", type=str, help="""Path to the output PyTorch model."""
					)
					__snake_case			:			Any			   =   parser.parse_args()

					# Initialize PyToch clip model
					__snake_case			:			int			   =   args.owlvit_version
					if model_name == "clip_b16":
										__snake_case			:			Optional[Any]			   =   CONFIGS["""vit_b16"""]
					elif model_name == "clip_b32":
										__snake_case			:			Optional[int]			   =   CONFIGS["""vit_b32"""]
					elif model_name == "clip_l14":
										__snake_case			:			int			   =   CONFIGS["""vit_l14"""]

					# Load from checkpoint and convert params to float-32
					__snake_case			:			List[str]			   =   checkpoints.restore_checkpoint(args.owlvit_checkpoint, target=None)["""optimizer"""]["""target"""]
					__snake_case			:			List[str]			   =   jax.tree_util.tree_map(lambda x: x.astype(jnp.floataa) if x.dtype == jnp.bfloataa else x, variables)
					del variables

					# Convert CLIP backbone
					__snake_case   ,       __snake_case   ,       __snake_case			:			Tuple			   =   convert_clip_backbone(flax_params, torch_config)

					convert_owlvit_checkpoint(clip_pt, flax_params, attn_params, args.pytorch_dump_folder_path, args.hf_config)





