


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      ,						_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.02      ,						_lowerCamelCase=3      ,						_lowerCamelCase=4      ,						_lowerCamelCase=None      ,						):
       a		:Union[str, Any]											= parent
       a		:Any											= batch_size
       a		:List[Any]											= seq_length
       a		:List[Any]											= is_training
       a		:str											= use_input_mask
       a		:List[Any]											= use_token_type_ids
       a		:Union[str, Any]											= use_labels
       a		:Union[str, Any]											= vocab_size
       a		:Any											= hidden_size
       a		:Tuple											= num_hidden_layers
       a		:List[str]											= num_attention_heads
       a		:Dict											= intermediate_size
       a		:str											= hidden_act
       a		:Union[str, Any]											= hidden_dropout_prob
       a		:List[str]											= attention_probs_dropout_prob
       a		:str											= max_position_embeddings
       a		:List[str]											= type_vocab_size
       a		:int											= type_sequence_label_size
       a		:Dict											= initializer_range
       a		:Dict											= num_labels
       a		:Union[str, Any]											= num_choices
       a		:Union[str, Any]											= scope




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Tuple											= ids_tensor([self.batch_size, self.seq_length]      ,						self.vocab_size					)

       a		:Union[str, Any]											= None
       if self.use_input_mask:
             a		:str											= random_attention_mask([self.batch_size, self.seq_length]					)

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

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

       a		:Optional[int]											= self.get_config()

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




 def  SCREAMING_SNAKE_CASE__       (	self					):
       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  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Dict											= self.get_config()
       a		:Tuple											= 300
       return config




 def  SCREAMING_SNAKE_CASE__       (	self					):
       (
           (
           a
       )							,       (
           a
       )							,       (
           a
       )							,       (
           a
       )							,       (
           a
       )							,       (
           a
       )							,       (
           a
       )							,       
       )		:List[Any]											= self.prepare_config_and_inputs()

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




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




 def  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase					):
       a		:Optional[Any]											= self.num_labels
       a		:Tuple											= YosoForTokenClassification(config=_lowerCamelCase					)
       model.to(_lowerCamelCase					)
       model.eval()
       a		:Optional[Any]											= 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  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase					):
       a		:Union[str, Any]											= self.num_choices
       a		:Union[str, Any]											= YosoForMultipleChoice(config=_lowerCamelCase					)
       model.to(_lowerCamelCase					)
       model.eval()
       a		:List[Any]											= input_ids.unsqueeze(1					).expand(-1      ,						self.num_choices      ,						-1					).contiguous()
       a		:Tuple											= token_type_ids.unsqueeze(1					).expand(-1      ,						self.num_choices      ,						-1					).contiguous()
       a		:Union[str, Any]											= input_mask.unsqueeze(1					).expand(-1      ,						self.num_choices      ,						-1					).contiguous()
       a		: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  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Dict											= self.prepare_config_and_inputs()
       (
           (
           a
       )							,       (
           a
       )							,       (
           a
       )							,       (
           a
       )							,       (
           a
       )							,       (
           a
       )							,       (
           a
       )							,       
       )		:List[Any]											= config_and_inputs
       a		:int											= {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
       return config, inputs_dict







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

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




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:str											= YosoModelTester(self					)
       a		:Tuple											= ConfigTester(self      ,						config_class=_lowerCamelCase      ,						hidden_size=37					)




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




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Any											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Optional[int]											= self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
             a		:Any											= type
             self.model_tester.create_and_check_model(*_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Tuple											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Dict											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Optional[int]											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Union[str, Any]											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Optional[int]											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*_lowerCamelCase					)




 @slow
 def  SCREAMING_SNAKE_CASE__       (	self					):
       for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             a		:str											= YosoModel.from_pretrained(_lowerCamelCase					)
             self.assertIsNotNone(_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       return







@require_torch
class 	_snake_case						(   unittest.TestCase							):




 @slow
 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Tuple											= YosoModel.from_pretrained('''uw-madison/yoso-4096'''					)
       a		:Dict											= torch.tensor([[0, 1, 2, 3, 4, 5]]					)

       with torch.no_grad():
             a		:Union[str, Any]											= model(_lowerCamelCase					)[0]

       a		:Dict											= torch.Size((1, 6, 768)					)
       self.assertEqual(output.shape      ,						_lowerCamelCase					)

       a		:Tuple											= 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  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Tuple											= YosoForMaskedLM.from_pretrained('''uw-madison/yoso-4096'''					)
       a		:Any											= torch.tensor([[0, 1, 2, 3, 4, 5]]					)

       with torch.no_grad():
             a		:int											= model(_lowerCamelCase					)[0]

       a		:str											= 5_0265

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

       a		:Optional[int]											= 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  SCREAMING_SNAKE_CASE__       (	self					):
       a		:List[Any]											= YosoForMaskedLM.from_pretrained('''uw-madison/yoso-4096'''					)
       a		:List[str]											= torch.arange(4096					).unsqueeze(0					)

       with torch.no_grad():
             a		:Optional[int]											= model(_lowerCamelCase					)[0]

       a		:List[Any]											= 5_0265

       a		:Tuple											= torch.Size((1, 4096, vocab_size)					)
       self.assertEqual(output.shape      ,						_lowerCamelCase					)

       a		:Optional[int]											= 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					)					)









from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


snake_case       :					Dict        =  {
    '''configuration_align''': [
        '''ALIGN_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''AlignConfig''',
        '''AlignTextConfig''',
        '''AlignVisionConfig''',
    ],
    '''processing_align''': ['''AlignProcessor'''],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case       :					List[str]        =  [
         '''ALIGN_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''AlignModel''',
         '''AlignPreTrainedModel''',
         '''AlignTextModel''',
         '''AlignVisionModel''',
     ]

if TYPE_CHECKING:
     from .configuration_align import (
         ALIGN_PRETRAINED_CONFIG_ARCHIVE_MAP,
         AlignConfig,
         AlignTextConfig,
         AlignVisionConfig,
     )
     from .processing_align import AlignProcessor

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_align import (
              ALIGN_PRETRAINED_MODEL_ARCHIVE_LIST,
              AlignModel,
              AlignPreTrainedModel,
              AlignTextModel,
              AlignVisionModel,
          )

else:
     import sys

     snake_case       :					int        =  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





