




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    ,					lowerCamelCase    ,					lowerCamelCase=13    ,					lowerCamelCase=7    ,					lowerCamelCase=True    ,					lowerCamelCase=True    ,					lowerCamelCase=True    ,					lowerCamelCase=True    ,					lowerCamelCase=99    ,					lowerCamelCase=32    ,					lowerCamelCase=5    ,					lowerCamelCase=4    ,					lowerCamelCase=37    ,					lowerCamelCase="gelu"    ,					lowerCamelCase=0.1    ,					lowerCamelCase=0.1    ,					lowerCamelCase=512    ,					lowerCamelCase=16    ,					lowerCamelCase=2    ,					lowerCamelCase=0.0_2    ,					lowerCamelCase=3    ,					lowerCamelCase=4    ,					lowerCamelCase=None    ,					)				->       Optional[int]:




						"""simple docstring"""






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






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




						"""simple docstring"""






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

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

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

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

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

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






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




						"""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=lowerCamelCase    ,					initializer_range=self.initializer_range    ,					)






	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






						__magic_name__ :							Dict		       =							self.get_config()
						__magic_name__ :							Optional[Any]		       =							300
						return config






	def 			lowercase				(     self						)				->       Tuple:




						"""simple docstring"""






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

						__magic_name__ :							Union[str, Any]		       =							True
						__magic_name__ :							str		       =							floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
						__magic_name__ :							Dict		       =							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 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)				->       str:




						"""simple docstring"""






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






	def 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					)				->       int:




						"""simple docstring"""






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






	def 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)				->       Tuple:




						"""simple docstring"""






						__magic_name__ :							Tuple		       =							YosoForMaskedLM(config=lowerCamelCase						)
						model.to(lowerCamelCase						)
						model.eval()
						__magic_name__ :							List[str]		       =							model(lowerCamelCase    ,					attention_mask=lowerCamelCase    ,					token_type_ids=lowerCamelCase    ,					labels=lowerCamelCase						)
						self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.seq_length, self.vocab_size)						)






	def 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)				->       Optional[int]:




						"""simple docstring"""






						__magic_name__ :							Union[str, Any]		       =							YosoForQuestionAnswering(config=lowerCamelCase						)
						model.to(lowerCamelCase						)
						model.eval()
						__magic_name__ :							Union[str, Any]		       =							model(
						    lowerCamelCase    ,					attention_mask=lowerCamelCase    ,					token_type_ids=lowerCamelCase    ,					start_positions=lowerCamelCase    ,					end_positions=lowerCamelCase    ,					)
						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 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)				->       str:




						"""simple docstring"""






						__magic_name__ :							Optional[int]		       =							self.num_labels
						__magic_name__ :							Optional[int]		       =							YosoForSequenceClassification(lowerCamelCase						)
						model.to(lowerCamelCase						)
						model.eval()
						__magic_name__ :							int		       =							model(lowerCamelCase    ,					attention_mask=lowerCamelCase    ,					token_type_ids=lowerCamelCase    ,					labels=lowerCamelCase						)
						self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_labels)						)






	def 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)				->       Optional[int]:




						"""simple docstring"""






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






	def 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)				->       str:




						"""simple docstring"""






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






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




						"""simple docstring"""






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





@require_torch
class 		A__    (       __SCREAMING_SNAKE_CASE		,					__SCREAMING_SNAKE_CASE		,					unittest.TestCase			):
	lowerCamelCase__		:			List[str]       =(
	    (
	        YosoModel,
	        YosoForMaskedLM,
	        YosoForMultipleChoice,
	        YosoForQuestionAnswering,
	        YosoForSequenceClassification,
	        YosoForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	lowerCamelCase__		:			Dict       =False
	lowerCamelCase__		:			Any       =False
	lowerCamelCase__		:			Dict       =False

	lowerCamelCase__		:			Optional[int]       =()
	lowerCamelCase__		:			int       =(
	    {
	        "feature-extraction": YosoModel,
	        "fill-mask": YosoForMaskedLM,
	        "question-answering": YosoForQuestionAnswering,
	        "text-classification": YosoForSequenceClassification,
	        "token-classification": YosoForTokenClassification,
	        "zero-shot": YosoForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)






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




						"""simple docstring"""






						__magic_name__ :							Union[str, Any]		       =							YosoModelTester(self						)
						__magic_name__ :							Dict		       =							ConfigTester(self    ,					config_class=lowerCamelCase    ,					hidden_size=37						)






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




						"""simple docstring"""






						self.config_tester.run_common_tests()






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




						"""simple docstring"""






						__magic_name__ :							List[str]		       =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowerCamelCase						)






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




						"""simple docstring"""






						__magic_name__ :							Union[str, Any]		       =							self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
											__magic_name__ :							Optional[int]		       =							type
											self.model_tester.create_and_check_model(*lowerCamelCase						)






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




						"""simple docstring"""






						__magic_name__ :							Optional[Any]		       =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase						)






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




						"""simple docstring"""






						__magic_name__ :							Optional[Any]		       =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase						)






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




						"""simple docstring"""






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






	def 			lowercase				(     self						)				->       Tuple:




						"""simple docstring"""






						__magic_name__ :							str		       =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase						)






	def 			lowercase				(     self						)				->       int:




						"""simple docstring"""






						__magic_name__ :							List[Any]		       =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*lowerCamelCase						)






	@slow
	def 			lowercase				(     self						)				->       List[Any]:




						"""simple docstring"""






						for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__magic_name__ :							Optional[int]		       =							YosoModel.from_pretrained(lowerCamelCase						)
											self.assertIsNotNone(lowerCamelCase						)






	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






						return





@require_torch
class 		A__    (       unittest.TestCase			):






	@slow
	def 			lowercase				(     self						)				->       Any:




						"""simple docstring"""






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

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

						__magic_name__ :							List[str]		       =							torch.Size((1, 6, 768)						)
						self.assertEqual(output.shape    ,					lowerCamelCase						)

						__magic_name__ :							Any		       =							torch.tensor(
						    [[[-0.0_6_1_1, 0.1_2_4_2, 0.0_8_4_0], [0.0_2_8_0, -0.0_0_4_8, 0.1_1_2_5], [0.0_1_0_6, 0.0_2_2_6, 0.0_7_5_1]]]						)

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






	@slow
	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






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

						with torch.no_grad():
											__magic_name__ :							List[str]		       =							model(lowerCamelCase						)[0]

						__magic_name__ :							int		       =							50265

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

						__magic_name__ :							List[str]		       =							torch.tensor(
						    [[[-2.1_3_1_3, -3.7_2_8_5, -2.2_4_0_7], [-2.7_0_4_7, -3.3_3_1_4, -2.6_4_0_8], [0.0_6_2_9, -2.5_1_6_6, -0.3_3_5_6]]]						)

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






	@slow
	def 			lowercase				(     self						)				->       Tuple:




						"""simple docstring"""






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

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

						__magic_name__ :							List[str]		       =							50265

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

						__magic_name__ :							Optional[Any]		       =							torch.tensor(
						    [[[-2.3_9_1_4, -4.3_7_4_2, -5.0_9_5_6], [-4.0_9_8_8, -4.2_3_8_4, -7.0_4_0_6], [-3.1_4_2_7, -3.7_1_9_2, -6.6_8_0_0]]]						)

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









from ..utils import requires_backends


lowercase_		=							None





class 		A__    :






	def __init__(     self    ,					*lowerCamelCase    ,					**lowerCamelCase						)				->       Optional[Any]:




						"""simple docstring"""






						requires_backends(self    ,					['''detectron2''']						)






	@classmethod
	def 			lowercase				(     cls    ,					*lowerCamelCase    ,					**lowerCamelCase						)				->       List[Any]:




						"""simple docstring"""






						requires_backends(cls    ,					['''detectron2''']						)



