



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



 """simple docstring"""

 def __init__(						self				:				int ,	__lowerCAmelCase				:				List[str] ,	__lowerCAmelCase				:				Tuple=13 ,	__lowerCAmelCase				:				Optional[Any]=7 ,	__lowerCAmelCase				:				Dict=True ,	__lowerCAmelCase				:				List[Any]=True ,	__lowerCAmelCase				:				Dict=True ,	__lowerCAmelCase				:				Dict=True ,	__lowerCAmelCase				:				Optional[int]=99 ,	__lowerCAmelCase				:				Optional[Any]=32 ,	__lowerCAmelCase				:				Dict=5 ,	__lowerCAmelCase				:				List[str]=4 ,	__lowerCAmelCase				:				Optional[Any]=37 ,	__lowerCAmelCase				:				Dict="gelu" ,	__lowerCAmelCase				:				Optional[int]=0.1 ,	__lowerCAmelCase				:				List[Any]=0.1 ,	__lowerCAmelCase				:				int=5_12 ,	__lowerCAmelCase				:				str=16 ,	__lowerCAmelCase				:				List[Any]=2 ,	__lowerCAmelCase				:				List[Any]=0.02 ,	__lowerCAmelCase				:				Optional[Any]=3 ,	__lowerCAmelCase				:				List[str]=4 ,	__lowerCAmelCase				:				Optional[Any]=None ,	)      ->  Dict:
    _A						     =					parent
    _A						     =					batch_size
    _A						     =					seq_length
    _A						     =					is_training
    _A						     =					use_input_mask
    _A						     =					use_token_type_ids
    _A						     =					use_labels
    _A						     =					vocab_size
    _A						     =					hidden_size
    _A						     =					num_hidden_layers
    _A						     =					num_attention_heads
    _A						     =					intermediate_size
    _A						     =					hidden_act
    _A						     =					hidden_dropout_prob
    _A						     =					attention_probs_dropout_prob
    _A						     =					max_position_embeddings
    _A						     =					type_vocab_size
    _A						     =					type_sequence_label_size
    _A						     =					initializer_range
    _A						     =					num_labels
    _A						     =					num_choices
    _A						     =					scope

 def 						snake_case_			(						self				:				Dict			)      ->  Tuple:
    _A						     =					ids_tensor([self.batch_size, self.seq_length] ,	self.vocab_size			)

    _A						     =					None
    if self.use_input_mask:
       _A						     =					random_attention_mask([self.batch_size, self.seq_length]			)

    _A						     =					None
    if self.use_token_type_ids:
       _A						     =					ids_tensor([self.batch_size, self.seq_length] ,	self.type_vocab_size			)

    _A						     =					None
    _A						     =					None
    _A						     =					None
    if self.use_labels:
       _A						     =					ids_tensor([self.batch_size] ,	self.type_sequence_label_size			)
       _A						     =					ids_tensor([self.batch_size, self.seq_length] ,	self.num_labels			)
       _A						     =					ids_tensor([self.batch_size] ,	self.num_choices			)

    _A						     =					self.get_config()

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

 def 						snake_case_			(						self				:				Tuple			)      ->  str:
    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 						snake_case_			(						self				:				List[str]			)      ->  str:
    _A						     =					self.get_config()
    _A						     =					3_00
    return config

 def 						snake_case_			(						self				:				Dict			)      ->  Any:
    (
        (
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						
    )						     =					self.prepare_config_and_inputs()

    _A						     =					True
    _A						     =					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
    _A						     =					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				:				Any ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				List[Any] ,	__lowerCAmelCase				:				List[str] ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				int ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				List[str]			)      ->  int:
    _A						     =					YosoModel(config=__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					model(__lowerCAmelCase ,	attention_mask=__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase			)
    _A						     =					model(__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase			)
    _A						     =					model(__lowerCAmelCase			)
    self.parent.assertEqual(result.last_hidden_state.shape ,	(self.batch_size, self.seq_length, self.hidden_size)			)

 def 						snake_case_			(						self				:				List[str] ,	__lowerCAmelCase				:				Optional[Any] ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				List[str] ,	__lowerCAmelCase				:				Optional[int] ,	__lowerCAmelCase				:				List[Any] ,	__lowerCAmelCase				:				List[Any] ,	__lowerCAmelCase				:				Optional[Any] ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				Union[str, Any] ,	)      ->  str:
    _A						     =					True
    _A						     =					YosoModel(__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					model(
        __lowerCAmelCase ,	attention_mask=__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase ,	encoder_hidden_states=__lowerCAmelCase ,	encoder_attention_mask=__lowerCAmelCase ,	)
    _A						     =					model(
        __lowerCAmelCase ,	attention_mask=__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase ,	encoder_hidden_states=__lowerCAmelCase ,	)
    _A						     =					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 						snake_case_			(						self				:				List[Any] ,	__lowerCAmelCase				:				List[Any] ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				List[str] ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				Optional[int] ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				Optional[int]			)      ->  List[Any]:
    _A						     =					YosoForMaskedLM(config=__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					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 						snake_case_			(						self				:				int ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				Any ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				Optional[int] ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				int ,	__lowerCAmelCase				:				Union[str, Any]			)      ->  int:
    _A						     =					YosoForQuestionAnswering(config=__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					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 						snake_case_			(						self				:				Optional[int] ,	__lowerCAmelCase				:				Optional[Any] ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				Optional[Any] ,	__lowerCAmelCase				:				int ,	__lowerCAmelCase				:				Optional[Any] ,	__lowerCAmelCase				:				str			)      ->  int:
    _A						     =					self.num_labels
    _A						     =					YosoForSequenceClassification(__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					model(__lowerCAmelCase ,	attention_mask=__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase ,	labels=__lowerCAmelCase			)
    self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.num_labels)			)

 def 						snake_case_			(						self				:				Optional[Any] ,	__lowerCAmelCase				:				int ,	__lowerCAmelCase				:				Union[str, Any] ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				Dict ,	__lowerCAmelCase				:				List[Any] ,	__lowerCAmelCase				:				int ,	__lowerCAmelCase				:				List[Any]			)      ->  Optional[int]:
    _A						     =					self.num_labels
    _A						     =					YosoForTokenClassification(config=__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					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 						snake_case_			(						self				:				List[str] ,	__lowerCAmelCase				:				Union[str, Any] ,	__lowerCAmelCase				:				Tuple ,	__lowerCAmelCase				:				Optional[int] ,	__lowerCAmelCase				:				Union[str, Any] ,	__lowerCAmelCase				:				str ,	__lowerCAmelCase				:				List[str] ,	__lowerCAmelCase				:				List[Any]			)      ->  Any:
    _A						     =					self.num_choices
    _A						     =					YosoForMultipleChoice(config=__lowerCAmelCase			)
    model.to(__lowerCAmelCase			)
    model.eval()
    _A						     =					input_ids.unsqueeze(1			).expand(-1 ,	self.num_choices ,	-1			).contiguous()
    _A						     =					token_type_ids.unsqueeze(1			).expand(-1 ,	self.num_choices ,	-1			).contiguous()
    _A						     =					input_mask.unsqueeze(1			).expand(-1 ,	self.num_choices ,	-1			).contiguous()
    _A						     =					model(
        __lowerCAmelCase ,	attention_mask=__lowerCAmelCase ,	token_type_ids=__lowerCAmelCase ,	labels=__lowerCAmelCase ,	)
    self.parent.assertEqual(result.logits.shape ,	(self.batch_size, self.num_choices)			)

 def 						snake_case_			(						self				:				List[str]			)      ->  Union[str, Any]:
    _A						     =					self.prepare_config_and_inputs()
    (
        (
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						(
        _A
    )     ,						
    )						     =					config_and_inputs
    _A						     =					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
    return config, inputs_dict


@require_torch
class   lowerCamelCase__  (					_A       ,     _A       ,     unittest.TestCase):



 """simple docstring"""

 a__				:   Optional[Any]   =     (
     (
         YosoModel,
         YosoForMaskedLM,
         YosoForMultipleChoice,
         YosoForQuestionAnswering,
         YosoForSequenceClassification,
         YosoForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 a__				:   List[str]   =     False
 a__				:   Tuple   =     False
 a__				:   Optional[int]   =     False

 a__				:   Union[str, Any]   =     ()
 a__				:   Tuple   =     (
     {
         "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				:				Optional[Any]			)      ->  List[Any]:
    _A						     =					YosoModelTester(self			)
    _A						     =					ConfigTester(self ,	config_class=__lowerCAmelCase ,	hidden_size=37			)

 def 						snake_case_			(						self				:				Optional[Any]			)      ->  Dict:
    self.config_tester.run_common_tests()

 def 						snake_case_			(						self				:				List[Any]			)      ->  Dict:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				Tuple			)      ->  str:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    for type in ["absolute", "relative_key", "relative_key_query"]:
       _A						     =					type
       self.model_tester.create_and_check_model(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				Optional[int]			)      ->  Union[str, Any]:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				Tuple			)      ->  Any:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_multiple_choice(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				List[Any]			)      ->  str:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				Dict			)      ->  str:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*__lowerCAmelCase			)

 def 						snake_case_			(						self				:				List[str]			)      ->  Optional[Any]:
    _A						     =					self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase			)

 @slow
 def 						snake_case_			(						self				:				Union[str, Any]			)      ->  Dict:
    for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _A						     =					YosoModel.from_pretrained(__lowerCAmelCase			)
       self.assertIsNotNone(__lowerCAmelCase			)

 def 						snake_case_			(						self				:				List[str]			)      ->  str:
    return


@require_torch
class   lowerCamelCase__  (					unittest.TestCase):



 """simple docstring"""

 @slow
 def 						snake_case_			(						self				:				Optional[Any]			)      ->  Optional[Any]:
    _A						     =					YosoModel.from_pretrained('''uw-madison/yoso-4096'''			)
    _A						     =					torch.tensor([[0, 1, 2, 3, 4, 5]]			)

    with torch.no_grad():
       _A						     =					model(__lowerCAmelCase			)[0]

    _A						     =					torch.Size((1, 6, 7_68)			)
    self.assertEqual(output.shape ,	__lowerCAmelCase			)

    _A						     =					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] ,	__lowerCAmelCase ,	atol=1E-4			)			)

 @slow
 def 						snake_case_			(						self				:				List[Any]			)      ->  Optional[int]:
    _A						     =					YosoForMaskedLM.from_pretrained('''uw-madison/yoso-4096'''			)
    _A						     =					torch.tensor([[0, 1, 2, 3, 4, 5]]			)

    with torch.no_grad():
       _A						     =					model(__lowerCAmelCase			)[0]

    _A						     =					5_02_65

    _A						     =					torch.Size((1, 6, vocab_size)			)
    self.assertEqual(output.shape ,	__lowerCAmelCase			)

    _A						     =					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] ,	__lowerCAmelCase ,	atol=1E-4			)			)

 @slow
 def 						snake_case_			(						self				:				int			)      ->  List[str]:
    _A						     =					YosoForMaskedLM.from_pretrained('''uw-madison/yoso-4096'''			)
    _A						     =					torch.arange(40_96			).unsqueeze(0			)

    with torch.no_grad():
       _A						     =					model(__lowerCAmelCase			)[0]

    _A						     =					5_02_65

    _A						     =					torch.Size((1, 40_96, vocab_size)			)
    self.assertEqual(output.shape ,	__lowerCAmelCase			)

    _A						     =					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] ,	__lowerCAmelCase ,	atol=1E-4			)			)








def   SCREAMING_SNAKE_CASE_       (  _snake_case  :list							)     ->    list:
   if len(_snake_case							) <= 1:
      return data
   else:
      return [
          *quick_sort([e for e in data[1:] if e <= data[0]]							),
          data[0],
          *quick_sort([e for e in data[1:] if e > data[0]]							),
      ]


if __name__ == "__main__":
   import doctest

   doctest.testmod()



