





"""simple docstring"""




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






				def __init__(					self						:       str,					lowerCAmelCase__						:       Tuple,					lowerCAmelCase__						:       str=1_3,					lowerCAmelCase__						:       Optional[int]=7,					lowerCAmelCase__						:       Tuple=True,					lowerCAmelCase__						:       int=True,					lowerCAmelCase__						:       Optional[int]=True,					lowerCAmelCase__						:       List[Any]=True,					lowerCAmelCase__						:       int=9_9,					lowerCAmelCase__						:       Union[str, Any]=3_2,					lowerCAmelCase__						:       Optional[int]=5,					lowerCAmelCase__						:       List[str]=4,					lowerCAmelCase__						:       str=3_7,					lowerCAmelCase__						:       Any="gelu",					lowerCAmelCase__						:       Dict=0.1,					lowerCAmelCase__						:       Any=0.1,					lowerCAmelCase__						:       Union[str, Any]=5_1_2,					lowerCAmelCase__						:       Optional[Any]=1_6,					lowerCAmelCase__						:       Optional[int]=2,					lowerCAmelCase__						:       Optional[Any]=0.02,					lowerCAmelCase__						:       Union[str, Any]=3,					lowerCAmelCase__						:       List[Any]=4,					lowerCAmelCase__						:       int=None,					)      -> str:




										'''simple docstring'''



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






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




										'''simple docstring'''



										_UpperCamelCase     :   int      =       ids_tensor([self.batch_size, self.seq_length],					self.vocab_size				)

										_UpperCamelCase     :   Optional[int]      =       None
										if self.use_input_mask:
																_UpperCamelCase     :   str      =       random_attention_mask([self.batch_size, self.seq_length]				)

										_UpperCamelCase     :   Any      =       None
										if self.use_token_type_ids:
																_UpperCamelCase     :   Optional[int]      =       ids_tensor([self.batch_size, self.seq_length],					self.type_vocab_size				)

										_UpperCamelCase     :   Tuple      =       None
										_UpperCamelCase     :   Optional[Any]      =       None
										_UpperCamelCase     :   List[str]      =       None
										if self.use_labels:
																_UpperCamelCase     :   Tuple      =       ids_tensor([self.batch_size],					self.type_sequence_label_size				)
																_UpperCamelCase     :   List[str]      =       ids_tensor([self.batch_size, self.seq_length],					self.num_labels				)
																_UpperCamelCase     :   Union[str, Any]      =       ids_tensor([self.batch_size],					self.num_choices				)

										_UpperCamelCase     :   Union[str, Any]      =       self.get_config()

										return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






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




										'''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=A_,					initializer_range=self.initializer_range,					)






				def 							snake_case      (					self						:       int				)      -> str:




										'''simple docstring'''



										_UpperCamelCase     :   Dict      =       self.get_config()
										_UpperCamelCase     :   Tuple      =       3_0_0
										return config






				def 							snake_case      (					self						:       Any				)      -> List[str]:




										'''simple docstring'''



										(
										    (
										    _UpperCamelCase       
										)  ,				(
										    _UpperCamelCase       
										)  ,				(
										    _UpperCamelCase       
										)  ,				(
										    _UpperCamelCase       
										)  ,				(
										    _UpperCamelCase       
										)  ,				(
										    _UpperCamelCase       
										)  ,				(
										    _UpperCamelCase       
										)  ,				       
										)     :   Optional[Any]      =       self.prepare_config_and_inputs()

										_UpperCamelCase     :   Any      =       True
										_UpperCamelCase     :   List[str]      =       floats_tensor([self.batch_size, self.seq_length, self.hidden_size]				)
										_UpperCamelCase     :   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 							snake_case      (					self						:       str,					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       str,					lowerCAmelCase__						:       List[Any],					lowerCAmelCase__						:       Optional[Any],					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       Any				)      -> List[str]:




										'''simple docstring'''



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






				def 							snake_case      (					self						:       Optional[Any],					lowerCAmelCase__						:       Optional[Any],					lowerCAmelCase__						:       Optional[Any],					lowerCAmelCase__						:       Any,					lowerCAmelCase__						:       List[Any],					lowerCAmelCase__						:       str,					lowerCAmelCase__						:       Optional[int],					lowerCAmelCase__						:       int,					lowerCAmelCase__						:       List[str],					lowerCAmelCase__						:       int,					)      -> List[Any]:




										'''simple docstring'''



										_UpperCamelCase     :   int      =       True
										_UpperCamelCase     :   Dict      =       YosoModel(A_				)
										model.to(A_				)
										model.eval()
										_UpperCamelCase     :   str      =       model(
										    A_,					attention_mask=A_,					token_type_ids=A_,					encoder_hidden_states=A_,					encoder_attention_mask=A_,					)
										_UpperCamelCase     :   Any      =       model(
										    A_,					attention_mask=A_,					token_type_ids=A_,					encoder_hidden_states=A_,					)
										_UpperCamelCase     :   List[Any]      =       model(A_,					attention_mask=A_,					token_type_ids=A_				)
										self.parent.assertEqual(result.last_hidden_state.shape,					(self.batch_size, self.seq_length, self.hidden_size)				)






				def 							snake_case      (					self						:       Any,					lowerCAmelCase__						:       Union[str, Any],					lowerCAmelCase__						:       int,					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       List[str],					lowerCAmelCase__						:       Tuple,					lowerCAmelCase__						:       List[Any],					lowerCAmelCase__						:       Union[str, Any]				)      -> Any:




										'''simple docstring'''



										_UpperCamelCase     :   Optional[Any]      =       YosoForMaskedLM(config=A_				)
										model.to(A_				)
										model.eval()
										_UpperCamelCase     :   List[Any]      =       model(A_,					attention_mask=A_,					token_type_ids=A_,					labels=A_				)
										self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.seq_length, self.vocab_size)				)






				def 							snake_case      (					self						:       List[Any],					lowerCAmelCase__						:       Optional[Any],					lowerCAmelCase__						:       List[str],					lowerCAmelCase__						:       str,					lowerCAmelCase__						:       Union[str, Any],					lowerCAmelCase__						:       Any,					lowerCAmelCase__						:       int,					lowerCAmelCase__						:       Any				)      -> List[str]:




										'''simple docstring'''



										_UpperCamelCase     :   Union[str, Any]      =       YosoForQuestionAnswering(config=A_				)
										model.to(A_				)
										model.eval()
										_UpperCamelCase     :   Dict      =       model(
										    A_,					attention_mask=A_,					token_type_ids=A_,					start_positions=A_,					end_positions=A_,					)
										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 							snake_case      (					self						:       Any,					lowerCAmelCase__						:       Tuple,					lowerCAmelCase__						:       int,					lowerCAmelCase__						:       Tuple,					lowerCAmelCase__						:       Optional[Any],					lowerCAmelCase__						:       Union[str, Any],					lowerCAmelCase__						:       str,					lowerCAmelCase__						:       int				)      -> Optional[int]:




										'''simple docstring'''



										_UpperCamelCase     :   Any      =       self.num_labels
										_UpperCamelCase     :   str      =       YosoForSequenceClassification(A_				)
										model.to(A_				)
										model.eval()
										_UpperCamelCase     :   int      =       model(A_,					attention_mask=A_,					token_type_ids=A_,					labels=A_				)
										self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.num_labels)				)






				def 							snake_case      (					self						:       Union[str, Any],					lowerCAmelCase__						:       List[Any],					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       Union[str, Any],					lowerCAmelCase__						:       Union[str, Any],					lowerCAmelCase__						:       str,					lowerCAmelCase__						:       Union[str, Any],					lowerCAmelCase__						:       str				)      -> Dict:




										'''simple docstring'''



										_UpperCamelCase     :   Dict      =       self.num_labels
										_UpperCamelCase     :   Optional[Any]      =       YosoForTokenClassification(config=A_				)
										model.to(A_				)
										model.eval()
										_UpperCamelCase     :   Optional[int]      =       model(A_,					attention_mask=A_,					token_type_ids=A_,					labels=A_				)
										self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.seq_length, self.num_labels)				)






				def 							snake_case      (					self						:       List[str],					lowerCAmelCase__						:       Any,					lowerCAmelCase__						:       Optional[Any],					lowerCAmelCase__						:       int,					lowerCAmelCase__						:       List[str],					lowerCAmelCase__						:       int,					lowerCAmelCase__						:       int,					lowerCAmelCase__						:       List[Any]				)      -> List[Any]:




										'''simple docstring'''



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






				def 							snake_case      (					self						:       Dict				)      -> Tuple:




										'''simple docstring'''



										_UpperCamelCase     :   int      =       self.prepare_config_and_inputs()
										(
										    (
										    _UpperCamelCase       
										)  ,				(
										    _UpperCamelCase       
										)  ,				(
										    _UpperCamelCase       
										)  ,				(
										    _UpperCamelCase       
										)  ,				(
										    _UpperCamelCase       
										)  ,				(
										    _UpperCamelCase       
										)  ,				(
										    _UpperCamelCase       
										)  ,				       
										)     :   Union[str, Any]      =       config_and_inputs
										_UpperCamelCase     :   Any      =       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
										return config, inputs_dict




@require_torch
class 					_a		( __lowercase      ,							__lowercase      ,							unittest.TestCase							):
				UpperCamelCase								=       (
				    (
				        YosoModel,
				        YosoForMaskedLM,
				        YosoForMultipleChoice,
				        YosoForQuestionAnswering,
				        YosoForSequenceClassification,
				        YosoForTokenClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				UpperCamelCase								=       False
				UpperCamelCase								=       False
				UpperCamelCase								=       False

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






				def 							snake_case      (					self						:       Any				)      -> Dict:




										'''simple docstring'''



										_UpperCamelCase     :   Tuple      =       YosoModelTester(self				)
										_UpperCamelCase     :   Dict      =       ConfigTester(self,					config_class=A_,					hidden_size=3_7				)






				def 							snake_case      (					self						:       List[Any]				)      -> Tuple:




										'''simple docstring'''



										self.config_tester.run_common_tests()






				def 							snake_case      (					self						:       Optional[Any]				)      -> List[str]:




										'''simple docstring'''



										_UpperCamelCase     :   Tuple      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*A_				)






				def 							snake_case      (					self						:       int				)      -> Dict:




										'''simple docstring'''



										_UpperCamelCase     :   Tuple      =       self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
																_UpperCamelCase     :   Any      =       type
																self.model_tester.create_and_check_model(*A_				)






				def 							snake_case      (					self						:       List[str]				)      -> Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase     :   Dict      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*A_				)






				def 							snake_case      (					self						:       Optional[int]				)      -> List[Any]:




										'''simple docstring'''



										_UpperCamelCase     :   Optional[int]      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*A_				)






				def 							snake_case      (					self						:       Any				)      -> int:




										'''simple docstring'''



										_UpperCamelCase     :   Any      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*A_				)






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




										'''simple docstring'''



										_UpperCamelCase     :   Union[str, Any]      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*A_				)






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




										'''simple docstring'''



										_UpperCamelCase     :   Optional[Any]      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*A_				)






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




										'''simple docstring'''



										for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																_UpperCamelCase     :   List[str]      =       YosoModel.from_pretrained(A_				)
																self.assertIsNotNone(A_				)






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




										'''simple docstring'''



										return




@require_torch
class 					_a		( unittest.TestCase							):






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




										'''simple docstring'''



										_UpperCamelCase     :   Union[str, Any]      =       YosoModel.from_pretrained('''uw-madison/yoso-4096'''				)
										_UpperCamelCase     :   Optional[Any]      =       torch.tensor([[0, 1, 2, 3, 4, 5]]				)

										with torch.no_grad():
																_UpperCamelCase     :   Dict      =       model(A_				)[0]

										_UpperCamelCase     :   Dict      =       torch.Size((1, 6, 7_6_8)				)
										self.assertEqual(output.shape,					A_				)

										_UpperCamelCase     :   int      =       torch.tensor(
										    [[[-0.0_611, 0.1_242, 0.0_840], [0.0_280, -0.0_048, 0.1_125], [0.0_106, 0.0_226, 0.0_751]]]				)

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






				@slow
				def 							snake_case      (					self						:       Tuple				)      -> Optional[int]:




										'''simple docstring'''



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

										with torch.no_grad():
																_UpperCamelCase     :   Optional[int]      =       model(A_				)[0]

										_UpperCamelCase     :   Optional[int]      =       5_0_2_6_5

										_UpperCamelCase     :   List[str]      =       torch.Size((1, 6, vocab_size)				)
										self.assertEqual(output.shape,					A_				)

										_UpperCamelCase     :   Union[str, Any]      =       torch.tensor(
										    [[[-2.1_313, -3.7_285, -2.2_407], [-2.7_047, -3.3_314, -2.6_408], [0.0_629, -2.5_166, -0.3_356]]]				)

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






				@slow
				def 							snake_case      (					self						:       Optional[int]				)      -> int:




										'''simple docstring'''



										_UpperCamelCase     :   List[str]      =       YosoForMaskedLM.from_pretrained('''uw-madison/yoso-4096'''				)
										_UpperCamelCase     :   List[Any]      =       torch.arange(4_0_9_6				).unsqueeze(0				)

										with torch.no_grad():
																_UpperCamelCase     :   int      =       model(A_				)[0]

										_UpperCamelCase     :   Any      =       5_0_2_6_5

										_UpperCamelCase     :   Optional[Any]      =       torch.Size((1, 4_0_9_6, vocab_size)				)
										self.assertEqual(output.shape,					A_				)

										_UpperCamelCase     :   List[str]      =       torch.tensor(
										    [[[-2.3_914, -4.3_742, -5.0_956], [-4.0_988, -4.2_384, -7.0_406], [-3.1_427, -3.7_192, -6.6_800]]]				)

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




import argparse

from transformers import CanineConfig, CanineModel, CanineTokenizer, load_tf_weights_in_canine
from transformers.utils import logging


logging.set_verbosity_info()
def    __magic_name__(	__UpperCAmelCase			,    __UpperCAmelCase		)    ->     Tuple:



      '''simple docstring'''

      _lowerCamelCase           =					CanineConfig()
      _lowerCamelCase           =					CanineModel(__UpperCAmelCase		)
      model.eval()

      print(F'Building PyTorch model from configuration: {config}'		)

      # Load weights from tf checkpoint
      load_tf_weights_in_canine(__UpperCAmelCase			,    __UpperCAmelCase			,    __UpperCAmelCase		)

      # Save pytorch-model (weights and configuration)
      print(F'Save PyTorch model to {pytorch_dump_path}'		)
      model.save_pretrained(__UpperCAmelCase		)

      # Save tokenizer files
      _lowerCamelCase           =					CanineTokenizer()
      print(F'Save tokenizer files to {pytorch_dump_path}'		)
      tokenizer.save_pretrained(__UpperCAmelCase		)


if __name__ == "__main__":
     snake_case__						      =			argparse.ArgumentParser()
     # Required parameters
     parser.add_argument(
         '--tf_checkpoint_path',
         default=None,
         type=str,
         required=True,
         help='Path to the TensorFlow checkpoint. Should end with model.ckpt',
     )
     parser.add_argument(
         '--pytorch_dump_path',
         default=None,
         type=str,
         required=True,
         help='Path to a folder where the PyTorch model will be placed.',
     )
     snake_case__						      =			parser.parse_args()
     convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.pytorch_dump_path)