


import unittest

from transformers import YosoConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    YosoForMaskedLM,
			    YosoForMultipleChoice,
			    YosoForQuestionAnswering,
			    YosoForSequenceClassification,
			    YosoForTokenClassification,
			    YosoModel,
			)
			from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST



class     snake_case__  :
				def __init__(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase=13     ,      _UpperCAmelCase=7     ,      _UpperCAmelCase=True     ,      _UpperCAmelCase=True     ,      _UpperCAmelCase=True     ,      _UpperCAmelCase=True     ,      _UpperCAmelCase=99     ,      _UpperCAmelCase=32     ,      _UpperCAmelCase=5     ,      _UpperCAmelCase=4     ,      _UpperCAmelCase=37     ,      _UpperCAmelCase="gelu"     ,      _UpperCAmelCase=0.1     ,      _UpperCAmelCase=0.1     ,      _UpperCAmelCase=512     ,      _UpperCAmelCase=16     ,      _UpperCAmelCase=2     ,      _UpperCAmelCase=0.02     ,      _UpperCAmelCase=3     ,      _UpperCAmelCase=4     ,      _UpperCAmelCase=None     ,      ):




										"""simple docstring"""




										__magic_name__				:							Optional[Any]				=      parent
										__magic_name__				:							Tuple				=      batch_size
										__magic_name__				:							int				=      seq_length
										__magic_name__				:							Optional[int]				=      is_training
										__magic_name__				:							List[Any]				=      use_input_mask
										__magic_name__				:							Tuple				=      use_token_type_ids
										__magic_name__				:							Tuple				=      use_labels
										__magic_name__				:							str				=      vocab_size
										__magic_name__				:							Any				=      hidden_size
										__magic_name__				:							Any				=      num_hidden_layers
										__magic_name__				:							int				=      num_attention_heads
										__magic_name__				:							Any				=      intermediate_size
										__magic_name__				:							Union[str, Any]				=      hidden_act
										__magic_name__				:							List[str]				=      hidden_dropout_prob
										__magic_name__				:							List[Any]				=      attention_probs_dropout_prob
										__magic_name__				:							List[Any]				=      max_position_embeddings
										__magic_name__				:							str				=      type_vocab_size
										__magic_name__				:							int				=      type_sequence_label_size
										__magic_name__				:							Tuple				=      initializer_range
										__magic_name__				:							List[Any]				=      num_labels
										__magic_name__				:							Optional[int]				=      num_choices
										__magic_name__				:							Union[str, Any]				=      scope
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




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

										__magic_name__				:							str				=      None
										if self.use_input_mask:
																__magic_name__				:							Tuple				=      random_attention_mask([self.batch_size, self.seq_length]      )

										__magic_name__				:							List[str]				=      None
										if self.use_token_type_ids:
																__magic_name__				:							Tuple				=      ids_tensor([self.batch_size, self.seq_length]     ,      self.type_vocab_size      )

										__magic_name__				:							Optional[Any]				=      None
										__magic_name__				:							int				=      None
										__magic_name__				:							Any				=      None
										if self.use_labels:
																__magic_name__				:							Any				=      ids_tensor([self.batch_size]     ,      self.type_sequence_label_size      )
																__magic_name__				:							Dict				=      ids_tensor([self.batch_size, self.seq_length]     ,      self.num_labels      )
																__magic_name__				:							Optional[int]				=      ids_tensor([self.batch_size]     ,      self.num_choices      )

										__magic_name__				:							Optional[Any]				=      self.get_config()

										return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										return YosoConfig(
										    vocab_size=self.vocab_size     ,      hidden_size=self.hidden_size     ,      num_hidden_layers=self.num_hidden_layers     ,      num_attention_heads=self.num_attention_heads     ,      intermediate_size=self.intermediate_size     ,      hidden_act=self.hidden_act     ,      hidden_dropout_prob=self.hidden_dropout_prob     ,      attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,      max_position_embeddings=self.max_position_embeddings     ,      type_vocab_size=self.type_vocab_size     ,      is_decoder=_UpperCAmelCase     ,      initializer_range=self.initializer_range     ,      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Union[str, Any]				=      self.get_config()
										__magic_name__				:							List[Any]				=      300
										return config
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										(
										    (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, 
										)				:							str				=      self.prepare_config_and_inputs()

										__magic_name__				:							List[Any]				=      True
										__magic_name__				:							Optional[int]				=      floats_tensor([self.batch_size, self.seq_length, self.hidden_size]      )
										__magic_name__				:							Any				=      ids_tensor([self.batch_size, self.seq_length]     ,      vocab_size=2      )

										return (
										    config,
										    input_ids,
										    token_type_ids,
										    input_mask,
										    sequence_labels,
										    token_labels,
										    choice_labels,
										    encoder_hidden_states,
										    encoder_attention_mask,
										)
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							int				=      YosoModel(config=_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										__magic_name__				:							Optional[Any]				=      model(_UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      token_type_ids=_UpperCAmelCase      )
										__magic_name__				:							Optional[int]				=      model(_UpperCAmelCase     ,      token_type_ids=_UpperCAmelCase      )
										__magic_name__				:							List[Any]				=      model(_UpperCAmelCase      )
										self.parent.assertEqual(result.last_hidden_state.shape     ,      (self.batch_size, self.seq_length, self.hidden_size)      )
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      ):




										"""simple docstring"""




										__magic_name__				:							str				=      True
										__magic_name__				:							List[Any]				=      YosoModel(_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										__magic_name__				:							Union[str, Any]				=      model(
										    _UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      token_type_ids=_UpperCAmelCase     ,      encoder_hidden_states=_UpperCAmelCase     ,      encoder_attention_mask=_UpperCAmelCase     ,      )
										__magic_name__				:							str				=      model(
										    _UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      token_type_ids=_UpperCAmelCase     ,      encoder_hidden_states=_UpperCAmelCase     ,      )
										__magic_name__				:							Optional[int]				=      model(_UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      token_type_ids=_UpperCAmelCase      )
										self.parent.assertEqual(result.last_hidden_state.shape     ,      (self.batch_size, self.seq_length, self.hidden_size)      )
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							Optional[Any]				=      YosoForMaskedLM(config=_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										__magic_name__				:							str				=      model(_UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      token_type_ids=_UpperCAmelCase     ,      labels=_UpperCAmelCase      )
										self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.seq_length, self.vocab_size)      )
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							Union[str, Any]				=      YosoForQuestionAnswering(config=_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										__magic_name__				:							int				=      model(
										    _UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      token_type_ids=_UpperCAmelCase     ,      start_positions=_UpperCAmelCase     ,      end_positions=_UpperCAmelCase     ,      )
										self.parent.assertEqual(result.start_logits.shape     ,      (self.batch_size, self.seq_length)      )
										self.parent.assertEqual(result.end_logits.shape     ,      (self.batch_size, self.seq_length)      )
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							Tuple				=      self.num_labels
										__magic_name__				:							Optional[Any]				=      YosoForSequenceClassification(_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										__magic_name__				:							List[Any]				=      model(_UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      token_type_ids=_UpperCAmelCase     ,      labels=_UpperCAmelCase      )
										self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.num_labels)      )
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							Tuple				=      self.num_labels
										__magic_name__				:							str				=      YosoForTokenClassification(config=_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										__magic_name__				:							Any				=      model(_UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      token_type_ids=_UpperCAmelCase     ,      labels=_UpperCAmelCase      )
										self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.seq_length, self.num_labels)      )
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							int				=      self.num_choices
										__magic_name__				:							str				=      YosoForMultipleChoice(config=_UpperCAmelCase      )
										model.to(_UpperCAmelCase      )
										model.eval()
										__magic_name__				:							List[Any]				=      input_ids.unsqueeze(1      ).expand(-1     ,      self.num_choices     ,      -1      ).contiguous()
										__magic_name__				:							Dict				=      token_type_ids.unsqueeze(1      ).expand(-1     ,      self.num_choices     ,      -1      ).contiguous()
										__magic_name__				:							Union[str, Any]				=      input_mask.unsqueeze(1      ).expand(-1     ,      self.num_choices     ,      -1      ).contiguous()
										__magic_name__				:							Dict				=      model(
										    _UpperCAmelCase     ,      attention_mask=_UpperCAmelCase     ,      token_type_ids=_UpperCAmelCase     ,      labels=_UpperCAmelCase     ,      )
										self.parent.assertEqual(result.logits.shape     ,      (self.batch_size, self.num_choices)      )





				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Optional[int]				=      self.prepare_config_and_inputs()
										(
										    (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, (
										    __magic_name__
										)		, 
										)				:							List[str]				=      config_and_inputs
										__magic_name__				:							List[str]				=      {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
										return config, inputs_dict






@require_torch
class     snake_case__  (      _A						,       _A						,       unittest.TestCase  ):
				__lowerCAmelCase		=      (
				    (
				        YosoModel,
				        YosoForMaskedLM,
				        YosoForMultipleChoice,
				        YosoForQuestionAnswering,
				        YosoForSequenceClassification,
				        YosoForTokenClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				__lowerCAmelCase		=      False
				__lowerCAmelCase		=      False
				__lowerCAmelCase		=      False

				__lowerCAmelCase		=      ()
				__lowerCAmelCase		=      (
				    {
				        '''feature-extraction''': YosoModel,
				        '''fill-mask''': YosoForMaskedLM,
				        '''question-answering''': YosoForQuestionAnswering,
				        '''text-classification''': YosoForSequenceClassification,
				        '''token-classification''': YosoForTokenClassification,
				        '''zero-shot''': YosoForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							List[str]				=      YosoModelTester(self      )
										__magic_name__				:							Tuple				=      ConfigTester(self     ,      config_class=_UpperCAmelCase     ,      hidden_size=37      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										self.config_tester.run_common_tests()
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Dict				=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							int				=      self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
																__magic_name__				:							List[str]				=      type
																self.model_tester.create_and_check_model(*_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Union[str, Any]				=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							List[Any]				=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Union[str, Any]				=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							List[str]				=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Optional[int]				=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase      )
				@slow
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__magic_name__				:							str				=      YosoModel.from_pretrained(_UpperCAmelCase      )
																self.assertIsNotNone(_UpperCAmelCase      )





				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										return






@require_torch
class     snake_case__  (      unittest.TestCase  ):
				@slow
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							int				=      YosoModel.from_pretrained('uw-madison/yoso-4096'      )
										__magic_name__				:							Dict				=      torch.tensor([[0, 1, 2, 3, 4, 5]]      )

										with torch.no_grad():
																__magic_name__				:							Optional[Any]				=      model(_UpperCAmelCase      )[0]

										__magic_name__				:							str				=      torch.Size((1, 6, 768)      )
										self.assertEqual(output.shape     ,      _UpperCAmelCase      )

										__magic_name__				:							Dict				=      torch.tensor(
										    [[[-0.0611, 0.1242, 0.0840], [0.0280, -0.0048, 0.1125], [0.0106, 0.0226, 0.0751]]]      )

										self.assertTrue(torch.allclose(output[:, :3, :3]     ,      _UpperCAmelCase     ,      atol=1E-4      )      )
				@slow
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Tuple				=      YosoForMaskedLM.from_pretrained('uw-madison/yoso-4096'      )
										__magic_name__				:							List[str]				=      torch.tensor([[0, 1, 2, 3, 4, 5]]      )

										with torch.no_grad():
																__magic_name__				:							Tuple				=      model(_UpperCAmelCase      )[0]

										__magic_name__				:							Any				=      5_0265

										__magic_name__				:							Any				=      torch.Size((1, 6, vocab_size)      )
										self.assertEqual(output.shape     ,      _UpperCAmelCase      )

										__magic_name__				:							Union[str, Any]				=      torch.tensor(
										    [[[-2.1313, -3.7285, -2.2407], [-2.7047, -3.3314, -2.6408], [0.0629, -2.5166, -0.3356]]]      )

										self.assertTrue(torch.allclose(output[:, :3, :3]     ,      _UpperCAmelCase     ,      atol=1E-4      )      )





				@slow
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Union[str, Any]				=      YosoForMaskedLM.from_pretrained('uw-madison/yoso-4096'      )
										__magic_name__				:							Dict				=      torch.arange(4096      ).unsqueeze(0      )

										with torch.no_grad():
																__magic_name__				:							int				=      model(_UpperCAmelCase      )[0]

										__magic_name__				:							int				=      5_0265

										__magic_name__				:							List[Any]				=      torch.Size((1, 4096, vocab_size)      )
										self.assertEqual(output.shape     ,      _UpperCAmelCase      )

										__magic_name__				:							Any				=      torch.tensor(
										    [[[-2.3914, -4.3742, -5.0956], [-4.0988, -4.2384, -7.0406], [-3.1427, -3.7192, -6.6800]]]      )

										self.assertTrue(torch.allclose(output[:, :3, :3]     ,      _UpperCAmelCase     ,      atol=1E-4      )      )






import argparse

from . import (
    BertConfig,
    BertGenerationConfig,
    BertGenerationDecoder,
    BertGenerationEncoder,
    load_tf_weights_in_bert_generation,
    logging,
)


logging.set_verbosity_info()
def  __SCREAMING_SNAKE_CASE       (				lowercase__  :Optional[int]      ,						lowercase__  :List[Any]      ,						lowercase__  :str      ,						lowercase__  :List[str]      ,						lowercase__  :List[Any]				)       ->  List[Any]:
						# Initialise PyTorch model
						__magic_name__				:							Any				=      BertConfig.from_pretrained(
						    'bert-large-cased'      ,						vocab_size=lowercase__      ,						max_position_embeddings=5_1_2      ,						is_decoder=lowercase__      ,						add_cross_attention=lowercase__      ,						)
						__magic_name__				:							Optional[int]				=      bert_config.to_dict()
						del bert_config_dict["type_vocab_size"]
						__magic_name__				:							Optional[int]				=      BertGenerationConfig(**lowercase__				)
						if is_encoder:
												__magic_name__				:							Dict				=      BertGenerationEncoder(lowercase__				)
						else:
												__magic_name__				:							str				=      BertGenerationDecoder(lowercase__				)
						print(f"Building PyTorch model from configuration: {config}"				)

						# Load weights from tf checkpoint
						load_tf_weights_in_bert_generation(
						    lowercase__      ,						lowercase__      ,						model_class='bert'      ,						is_encoder_named_decoder=lowercase__      ,						is_encoder=lowercase__      ,						)

						# Save pytorch-model
						print(f"Save PyTorch model and config to {pytorch_dump_path}"				)
						model.save_pretrained(lowercase__				)


if __name__ == "__main__":
			snake_case__     							= argparse.ArgumentParser()
			# Required parameters
			parser.add_argument(
			    '--tf_hub_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
			)
			parser.add_argument(
			    '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
			)
			parser.add_argument(
			    '--is_encoder_named_decoder',
			    action='store_true',
			    help='If decoder has to be renamed to encoder in PyTorch model.',
			)
			parser.add_argument('--is_encoder', action='store_true', help='If model is an encoder.')
			parser.add_argument('--vocab_size', default=5_0358, type=int, help='Vocab size of model')
			snake_case__     							= parser.parse_args()
			convert_tf_checkpoint_to_pytorch(
			    args.tf_hub_path,
			    args.pytorch_dump_path,
			    args.is_encoder_named_decoder,
			    args.vocab_size,
			    is_encoder=args.is_encoder,
			)


